1 /* mhbuildsbr.c -- routines to expand/translate MIME composition files
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
9 * This code was originally part of mhn.c. I split it into
10 * a separate program (mhbuild.c) and then later split it
11 * again (mhbuildsbr.c). But the code still has some of
12 * the mhn.c code in it. This program needs additional
13 * streamlining and removal of unneeded code.
17 #include "sbr/error.h"
22 #include "h/fmt_scan.h"
24 #include "h/mhparse.h"
27 #include "h/mhcachesbr.h"
29 #include "sbr/m_mktemp.h"
30 #include "sbr/message_id.h"
31 #include "sbr/mime_type.h"
33 #include "mhshowsbr.h"
35 #ifdef HAVE_SYS_TIME_H
36 # include <sys/time.h>
45 extern bool contentidsw
;
47 static char prefix
[] = "----- =_aaaaaaaaaa";
51 struct attach_list
*next
;
54 typedef struct convert_list
{
58 struct convert_list
*next
;
65 static int init_decoded_content (CT
, const char *);
66 static void setup_attach_content(CT
, char *);
67 static void set_disposition (CT
);
68 static void set_charset (CT
, int);
69 static void expand_pseudoheaders (CT
, struct multipart
*, const char *,
70 const convert_list
*);
71 static void expand_pseudoheader (CT
, CT
*, struct multipart
*, const char *,
72 const char *, const char *);
73 static char *fgetstr (char *, int, FILE *);
74 static int user_content (FILE *, char *, CT
*, const char *infilename
);
75 static void set_id (CT
, int);
76 static int compose_content (CT
, int);
77 static int scan_content (CT
, size_t);
78 static int build_headers (CT
, int);
79 static char *calculate_digest (CT
, int);
80 static int extract_headers (CT
, char *, FILE **);
83 static unsigned char directives_stack
[32];
84 static unsigned int directives_index
;
89 return directives_stack
[directives_index
];
93 directive_onoff(int onoff
)
95 if (directives_index
>= sizeof(directives_stack
) - 1) {
96 fprintf(stderr
, "mhbuild: #on/off overflow, continuing\n");
99 directives_stack
[++directives_index
] = onoff
;
103 directive_init(int onoff
)
105 directives_index
= 0;
106 directives_stack
[0] = onoff
;
112 if (directives_index
> 0)
115 fprintf(stderr
, "mhbuild: #pop underflow, continuing\n");
119 * Main routine for translating composition file
120 * into valid MIME message. It translates the draft
121 * into a content structure (actually a tree of content
122 * structures). This message then can be manipulated
123 * in various ways, including being output via
128 build_mime (char *infile
, int autobuild
, int dist
, int directives
,
129 int header_encoding
, size_t maxunencoded
, int verbose
)
132 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
139 m_getfld_state_t gstate
;
140 struct attach_list
*attach_head
= NULL
, *attach_tail
= NULL
, *at_entry
;
141 convert_list
*convert_head
= NULL
, *convert_tail
= NULL
, *convert
;
143 directive_init(directives
);
145 umask (~m_gmprot ());
147 /* open the composition draft */
148 if ((in
= fopen (infile
, "r")) == NULL
)
149 adios (infile
, "unable to open for reading");
152 * Allocate space for primary (outside) content
157 * Allocate structure for handling decoded content
158 * for this part. We don't really need this, but
159 * allocate it to remain consistent.
161 init_decoded_content (ct
, infile
);
164 * Parse some of the header fields in the composition
165 * draft into the linked list of header fields for
166 * the new MIME message.
168 gstate
= m_getfld_state_init(in
);
169 m_getfld_track_filepos2(&gstate
);
170 for (compnum
= 1;;) {
171 int bufsz
= sizeof buf
;
172 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
177 /* abort if draft has Mime-Version or C-T-E header field */
178 if (strcasecmp (name
, VRSN_FIELD
) == 0 ||
179 strcasecmp (name
, ENCODING_FIELD
) == 0) {
185 die("draft shouldn't contain %s: field", name
);
188 /* ignore any Content-Type fields in the header */
189 if (!strcasecmp (name
, TYPE_FIELD
)) {
190 while (state
== FLDPLUS
) {
192 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
197 /* get copies of the buffers */
198 np
= mh_xstrdup(name
);
199 vp
= mh_xstrdup(buf
);
201 /* if necessary, get rest of field */
202 while (state
== FLDPLUS
) {
204 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
205 vp
= add (buf
, vp
); /* add to previous value */
209 * Now add the header data to the list, unless it's an attach
210 * header; in that case, add it to our attach list
213 if (strcasecmp(ATTACH_FIELD
, np
) == 0 ||
214 strcasecmp(ATTACH_FIELD_ALT
, np
) == 0) {
215 struct attach_list
*entry
;
216 char *s
= vp
, *e
= vp
+ strlen(vp
) - 1;
220 * Make sure we can find the start of this filename.
221 * If it's blank, we skip completely. Otherwise, strip
222 * off any leading spaces and trailing newlines.
225 while (isspace((unsigned char) *s
))
228 while (e
> s
&& *e
== '\n')
237 entry
->filename
= mh_xstrdup(s
);
242 attach_tail
->next
= entry
;
245 attach_head
= attach_tail
= entry
;
247 } else if (strncasecmp(MHBUILD_FILE_PSEUDOHEADER
, np
,
248 LEN(MHBUILD_FILE_PSEUDOHEADER
)) == 0) {
250 * Nmh-mhbuild-file-text/calendar: /home/user/Mail/inbox/9
252 char *type
= np
+ LEN(MHBUILD_FILE_PSEUDOHEADER
);
255 /* vp should begin with a space because m_getfld2()
256 includes the space after the colon in buf. */
257 while (isspace((unsigned char) *filename
)) { ++filename
; }
258 /* Trim trailing newline and any other whitespace. */
261 for (convert
= convert_head
; convert
; convert
= convert
->next
) {
262 if (strcasecmp (convert
->type
, type
) == 0) { break; }
265 if (convert
->filename
&&
266 strcasecmp (convert
->filename
, filename
)) {
267 die("Multiple %s headers with different files"
268 " not allowed", type
);
270 convert
->filename
= mh_xstrdup(filename
);
274 convert
->filename
= mh_xstrdup(filename
);
275 convert
->type
= mh_xstrdup(type
);
278 convert_tail
->next
= convert
;
280 convert_head
= convert
;
282 convert_tail
= convert
;
287 } else if (strncasecmp(MHBUILD_ARGS_PSEUDOHEADER
, np
,
288 LEN(MHBUILD_ARGS_PSEUDOHEADER
)) == 0) {
290 * Nmh-mhbuild-args-text/calendar: -reply accept
292 char *type
= np
+ LEN(MHBUILD_ARGS_PSEUDOHEADER
);
293 char *argstring
= vp
;
295 /* vp should begin with a space because m_getfld2()
296 includes the space after the colon in buf. */
297 while (isspace((unsigned char) *argstring
)) { ++argstring
; }
298 /* Trim trailing newline and any other whitespace. */
301 for (convert
= convert_head
; convert
; convert
= convert
->next
) {
302 if (strcasecmp (convert
->type
, type
) == 0) { break; }
305 if (convert
->argstring
&&
306 strcasecmp (convert
->argstring
, argstring
)) {
307 die("Multiple %s headers with different "
308 "argstrings not allowed", type
);
310 convert
->argstring
= mh_xstrdup(argstring
);
314 convert
->type
= mh_xstrdup(type
);
315 convert
->argstring
= mh_xstrdup(argstring
);
318 convert_tail
->next
= convert
;
320 convert_head
= convert
;
322 convert_tail
= convert
;
328 add_header (ct
, np
, vp
);
332 /* if this wasn't the last header field, then continue */
336 fseek (in
, (long) (-strlen (buf
)), SEEK_CUR
);
343 die("message format error in component #%d", compnum
);
346 die("getfld() returned %d", state
);
350 m_getfld_state_destroy (&gstate
);
352 if (header_encoding
!= CE_8BIT
) {
354 * Iterate through the list of headers and call the function to MIME-ify
358 for (hp
= ct
->c_first_hf
; hp
!= NULL
; hp
= hp
->next
) {
359 if (encode_rfc2047(hp
->name
, &hp
->value
, header_encoding
, NULL
)) {
360 die("Unable to encode header \"%s\"", hp
->name
);
366 * Now add the MIME-Version header field
367 * to the list of header fields.
371 np
= mh_xstrdup(VRSN_FIELD
);
372 vp
= concat (" ", VRSN_VALUE
, "\n", NULL
);
373 add_header (ct
, np
, vp
);
377 * We initially assume we will find multiple contents in the
378 * draft. So create a multipart/mixed content to hold everything.
379 * We can remove this later, if it is not needed.
381 if (get_ctinfo ("multipart/mixed", ct
, 0) == NOTOK
)
383 ct
->c_type
= CT_MULTIPART
;
384 ct
->c_subtype
= MULTI_MIXED
;
387 ct
->c_ctparams
= (void *) m
;
391 * read and parse the composition file
392 * and the directives it contains.
394 while (fgetstr (buf
, sizeof(buf
) - 1, in
)) {
398 if (user_content (in
, buf
, &p
, infile
) == DONE
) {
399 inform("ignoring spurious #end, continuing...");
412 * Add any Attach headers to the list of MIME parts at the end of the
416 for (at_entry
= attach_head
; at_entry
; ) {
417 struct attach_list
*at_prev
= at_entry
;
421 if (access(at_entry
->filename
, R_OK
) != 0) {
422 adios("reading", "Unable to open %s for", at_entry
->filename
);
426 init_decoded_content(p
, infile
);
429 * Initialize our content structure based on the filename,
430 * and fill in all of the relevant fields. Also place MIME
431 * parameters in the attributes array.
434 setup_attach_content(p
, at_entry
->filename
);
441 at_entry
= at_entry
->next
;
442 free(at_prev
->filename
);
447 * Handle the mhbuild pseudoheaders, which deal with specific
454 set_done(freects_done
);
456 /* In case there are multiple calls that land here, prevent leak. */
457 for (ctp
= cts
; ctp
&& *ctp
; ++ctp
) { free_content (*ctp
); }
460 /* Extract the type part (as a CT) from filename. */
461 cts
= mh_xcalloc(2, sizeof *cts
);
462 if (! (cts
[0] = parse_mime (convert_head
->filename
))) {
463 die("failed to parse %s", convert_head
->filename
);
466 expand_pseudoheaders (cts
[0], m
, infile
, convert_head
);
468 /* Free the convert list. */
469 for (convert
= convert_head
; convert
; convert
= next
) {
470 next
= convert
->next
;
471 free (convert
->type
);
472 free (convert
->filename
);
473 free (convert
->argstring
);
480 * To allow for empty message bodies, if we've found NO content at all
481 * yet cook up an empty text/plain part.
490 init_decoded_content(p
, infile
);
492 if (get_ctinfo ("text/plain", p
, 0) == NOTOK
)
496 p
->c_subtype
= TEXT_PLAIN
;
497 p
->c_encoding
= CE_7BIT
;
499 * Sigh. ce_file contains the "decoded" contents of this part.
500 * So this seems like the best option available since we're going
501 * to call scan_content() on this.
503 p
->c_cefile
.ce_file
= mh_xstrdup("/dev/null");
504 p
->c_begin
= ftell(in
);
505 p
->c_end
= ftell(in
);
508 t
->tx_charset
= CHARSET_SPECIFIED
;
517 * close the composition draft since
518 * it's not needed any longer.
523 * If only one content was found, then remove and
524 * free the outer multipart content.
526 if (!m
->mp_parts
->mp_next
) {
529 p
= m
->mp_parts
->mp_part
;
530 m
->mp_parts
->mp_part
= NULL
;
532 /* move header fields */
533 p
->c_first_hf
= ct
->c_first_hf
;
534 p
->c_last_hf
= ct
->c_last_hf
;
535 ct
->c_first_hf
= NULL
;
536 ct
->c_last_hf
= NULL
;
545 * Fill out, or expand directives. Parse and execute
546 * commands specified by profile composition strings.
548 compose_content (ct
, verbose
);
550 if ((cp
= strchr(prefix
, 'a')) == NULL
)
551 die("internal error(4)");
554 * If using EAI, force 8-bit charset.
556 if (header_encoding
== CE_8BIT
) {
561 * Scan the contents. Choose a transfer encoding, and
562 * check if prefix for multipart boundary clashes with
563 * any of the contents.
565 while (scan_content (ct
, maxunencoded
) == NOTOK
) {
570 die("giving up trying to find a unique delimiter string");
575 /* Build the rest of the header field structures */
577 build_headers (ct
, header_encoding
);
584 * Set up structures for placing unencoded
585 * content when building parts.
589 init_decoded_content (CT ct
, const char *filename
)
591 ct
->c_ceopenfnx
= open7Bit
; /* since unencoded */
592 ct
->c_ceclosefnx
= close_encoding
;
593 ct
->c_cesizefnx
= NULL
; /* since unencoded */
594 ct
->c_encoding
= CE_7BIT
; /* Seems like a reasonable default */
595 ct
->c_file
= mh_xstrdup(FENDNULL(filename
));
602 fgetstr (char *s
, int n
, FILE *stream
)
608 for (cp
= s
; cp
< ep
;) {
611 if (!fgets (cp
, n
, stream
))
612 return cp
== s
? NULL
: s
; /* "\\\nEOF" ignored. */
614 if (! do_direct() || (cp
== s
&& *cp
!= '#'))
615 return s
; /* Plaintext line. */
619 break; /* Can't contain "\\\n". */
620 cp
+= len
- 1; /* Just before NUL. */
621 if (*cp
-- != '\n' || *cp
!= '\\')
623 *cp
= '\0'; /* Erase the trailing "\\\n". */
627 if (strcmp(s
, "#on\n") == 0) {
629 } else if (strcmp(s
, "#off\n") == 0) {
631 } else if (strcmp(s
, "#pop\n") == 0) {
641 * Parse the composition draft for text and directives.
642 * Do initial setup of Content structure.
646 user_content (FILE *in
, char *buf
, CT
*ctp
, const char *infilename
)
650 char buffer
[NMH_BUFSIZ
];
654 struct str2init
*s2i
;
659 if (buf
[0] == '\n' || (do_direct() && strcmp (buf
, "#\n") == 0)) {
664 /* allocate basic Content structure */
668 /* allocate basic structure for handling decoded content */
669 init_decoded_content (ct
, infilename
);
676 * Handle inline text. Check if line
677 * is one of the following forms:
679 * 1) doesn't begin with '#' (implicit directive)
680 * 2) begins with "##" (implicit directive)
681 * 3) begins with "#<"
683 if (!do_direct() || buf
[0] != '#' || buf
[1] == '#' || buf
[1] == '<') {
687 char content
[BUFSIZ
];
691 if ((cp
= m_mktemp2(NULL
, invo_name
, NULL
, &out
)) == NULL
) {
692 adios("mhbuildsbr", "unable to create temporary file in %s",
696 /* use a temp file to collect the plain text lines */
697 ce
->ce_file
= mh_xstrdup(cp
);
700 if (do_direct() && (buf
[0] == '#' && buf
[1] == '<')) {
701 strncpy (content
, buf
+ 2, sizeof(content
));
707 /* the directive is implicit */
708 strncpy (content
, "text/plain", sizeof(content
));
710 strncpy (buffer
, (!do_direct() || buf
[0] != '#') ? buf
: buf
+ 1, sizeof(buffer
));
714 if (headers
>= 0 && do_direct() && uprf (buffer
, DESCR_FIELD
)
715 && buffer
[i
= LEN(DESCR_FIELD
)] == ':') {
719 ct
->c_descr
= add (buffer
+ i
+ 1, ct
->c_descr
);
720 if (!fgetstr (buffer
, sizeof(buffer
) - 1, in
))
721 die("end-of-file after %s: field in plaintext", DESCR_FIELD
);
729 die("#-directive after %s: field in plaintext", DESCR_FIELD
);
737 if (headers
>= 0 && do_direct() && uprf (buffer
, DISPO_FIELD
)
738 && buffer
[i
= LEN(DISPO_FIELD
)] == ':') {
742 ct
->c_dispo
= add (buffer
+ i
+ 1, ct
->c_dispo
);
743 if (!fgetstr (buffer
, sizeof(buffer
) - 1, in
))
744 die("end-of-file after %s: field in plaintext", DISPO_FIELD
);
752 die("#-directive after %s: field in plaintext", DISPO_FIELD
);
760 if (headers
!= 1 || buffer
[0] != '\n')
766 if ((cp
= fgetstr (buffer
, sizeof(buffer
) - 1, in
)) == NULL
)
768 if (do_direct() && buffer
[0] == '#') {
771 if (buffer
[1] != '#')
773 for (cp
= (bp
= buffer
) + 1; *cp
; cp
++)
780 ct
->c_end
= ftell (out
);
783 /* parse content type */
784 if (get_ctinfo (content
, ct
, inlineD
) == NOTOK
)
787 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
788 if (!strcasecmp (ci
->ci_type
, s2i
->si_key
))
790 if (!s2i
->si_key
&& !uprf (ci
->ci_type
, "X-"))
794 * check type specified (possibly implicitly)
796 switch (ct
->c_type
= s2i
->si_val
) {
798 if (!strcasecmp (ci
->ci_subtype
, "rfc822")) {
799 ct
->c_encoding
= CE_7BIT
;
804 die("it doesn't make sense to define an in-line %s content",
805 ct
->c_type
== CT_MESSAGE
? "message" : "multipart");
810 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
811 (*ct
->c_ctinitfnx
) (ct
);
816 fseek (in
, pos
, SEEK_SET
);
821 * If we've reached this point, the next line
822 * must be some type of explicit directive.
825 /* check if directive is external-type */
826 extrnal
= (buf
[1] == '@');
828 /* parse directive */
829 if (get_ctinfo (buf
+ (extrnal
? 2 : 1), ct
, 1) == NOTOK
)
832 /* check directive against the list of MIME types */
833 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
834 if (!strcasecmp (ci
->ci_type
, s2i
->si_key
))
838 * Check if the directive specified a valid type.
839 * This will happen if it was one of the following forms:
846 die("missing subtype in \"#%s\"", ci
->ci_type
);
848 switch (ct
->c_type
= s2i
->si_val
) {
850 die("use \"#begin ... #end\" instead of \"#%s/%s\"",
851 ci
->ci_type
, ci
->ci_subtype
);
855 if (!strcasecmp (ci
->ci_subtype
, "partial"))
856 die("sorry, \"#%s/%s\" isn't supported",
857 ci
->ci_type
, ci
->ci_subtype
);
858 if (!strcasecmp (ci
->ci_subtype
, "external-body"))
859 die("use \"#@type/subtype ... [] ...\" instead of \"#%s/%s\"",
860 ci
->ci_type
, ci
->ci_subtype
);
862 die( "use \"#forw [+folder] [msgs]\" instead of \"#%s/%s\"",
863 ci
->ci_type
, ci
->ci_subtype
);
867 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
868 (*ct
->c_ctinitfnx
) (ct
);
873 * #@type/subtype (external types directive)
880 die("need external information for \"#@%s/%s\"",
881 ci
->ci_type
, ci
->ci_subtype
);
884 snprintf (buffer
, sizeof(buffer
), "message/external-body; %s", ci
->ci_magic
);
889 * Since we are using the current Content structure to
890 * hold information about the type of the external
891 * reference, we need to create another Content structure
892 * for the message/external-body to wrap it in.
895 init_decoded_content(ct
, infilename
);
897 if (get_ctinfo (buffer
, ct
, 0) == NOTOK
)
899 ct
->c_type
= CT_MESSAGE
;
900 ct
->c_subtype
= MESSAGE_EXTERNAL
;
903 ct
->c_ctparams
= (void *) e
;
909 if (params_external (ct
, 1) == NOTOK
)
915 /* Handle [file] argument */
917 /* check if specifies command to execute */
918 if (*ci
->ci_magic
== '|' || *ci
->ci_magic
== '!') {
919 for (cp
= ci
->ci_magic
+ 1; isspace ((unsigned char) *cp
); cp
++)
922 die("empty pipe command for #%s directive", ci
->ci_type
);
927 /* record filename of decoded contents */
928 ce
->ce_file
= ci
->ci_magic
;
929 if (access (ce
->ce_file
, R_OK
) == NOTOK
)
930 adios ("reading", "unable to access %s for", ce
->ce_file
);
931 if (listsw
&& stat (ce
->ce_file
, &st
) != NOTOK
)
932 ct
->c_end
= (long) st
.st_size
;
939 * No [file] argument, so check profile for
940 * method to compose content.
942 cp
= context_find_by_type ("compose", ci
->ci_type
, ci
->ci_subtype
);
944 content_error (NULL
, ct
, "don't know how to compose content");
947 ci
->ci_magic
= mh_xstrdup(cp
);
952 die("external definition not allowed for \"#%s\"", ci
->ci_type
);
956 * #forw [+folder] [msgs]
958 if (!strcasecmp (ci
->ci_type
, "forw")) {
960 char *folder
, *arguments
[MAXARGS
];
964 ap
= brkstring (ci
->ci_magic
, " ", "\n");
965 copyip (ap
, arguments
, MAXARGS
);
967 arguments
[0] = "cur";
972 /* search the arguments for a folder name */
973 for (ap
= arguments
; *ap
; ap
++) {
975 if (*cp
== '+' || *cp
== '@') {
977 die("only one folder per #forw directive");
978 folder
= pluspath (cp
);
982 /* else, use the current folder */
984 folder
= mh_xstrdup(getfolder(1));
986 if (!(mp
= folder_read (folder
, 0)))
987 die("unable to read folder %s", folder
);
988 for (ap
= arguments
; *ap
; ap
++) {
990 if (*cp
!= '+' && *cp
!= '@')
991 if (!m_convert (mp
, cp
))
998 * If there is more than one message to include, make this
999 * a content of type "multipart/digest" and insert each message
1000 * as a subpart. If there is only one message, then make this
1001 * a content of type "message/rfc822".
1003 if (mp
->numsel
> 1) {
1004 /* we are forwarding multiple messages */
1005 if (get_ctinfo ("multipart/digest", ct
, 0) == NOTOK
)
1007 ct
->c_type
= CT_MULTIPART
;
1008 ct
->c_subtype
= MULTI_DIGEST
;
1011 ct
->c_ctparams
= (void *) m
;
1014 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
1015 if (is_selected(mp
, msgnum
)) {
1021 init_decoded_content (p
, infilename
);
1023 if (get_ctinfo ("message/rfc822", p
, 0) == NOTOK
)
1025 p
->c_type
= CT_MESSAGE
;
1026 p
->c_subtype
= MESSAGE_RFC822
;
1028 snprintf (buffer
, sizeof(buffer
), "%s/%d", mp
->foldpath
, msgnum
);
1029 pe
->ce_file
= mh_xstrdup(buffer
);
1030 if (listsw
&& stat (pe
->ce_file
, &st
) != NOTOK
)
1031 p
->c_end
= (long) st
.st_size
;
1035 pp
= &part
->mp_next
;
1040 /* we are forwarding one message */
1041 if (get_ctinfo ("message/rfc822", ct
, 0) == NOTOK
)
1043 ct
->c_type
= CT_MESSAGE
;
1044 ct
->c_subtype
= MESSAGE_RFC822
;
1046 msgnum
= mp
->lowsel
;
1047 snprintf (buffer
, sizeof(buffer
), "%s/%d", mp
->foldpath
, msgnum
);
1048 ce
->ce_file
= mh_xstrdup(buffer
);
1049 if (listsw
&& stat (ce
->ce_file
, &st
) != NOTOK
)
1050 ct
->c_end
= (long) st
.st_size
;
1053 folder_free (mp
); /* free folder/message structure */
1060 if (!strcasecmp (ci
->ci_type
, "end")) {
1067 * #begin [ alternative | parallel ]
1069 if (!strcasecmp (ci
->ci_type
, "begin")) {
1070 if (!ci
->ci_magic
) {
1072 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
1073 } else if (!strcasecmp (ci
->ci_magic
, "alternative")) {
1074 vrsn
= MULTI_ALTERNATE
;
1075 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
1076 } else if (!strcasecmp (ci
->ci_magic
, "parallel")) {
1077 vrsn
= MULTI_PARALLEL
;
1078 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
1079 } else if (uprf (ci
->ci_magic
, "digest")) {
1082 vrsn
= MULTI_UNKNOWN
;
1087 snprintf (buffer
, sizeof(buffer
), "multipart/%s", cp
);
1088 if (get_ctinfo (buffer
, ct
, 0) == NOTOK
)
1090 ct
->c_type
= CT_MULTIPART
;
1091 ct
->c_subtype
= vrsn
;
1094 ct
->c_ctparams
= (void *) m
;
1097 while (fgetstr (buffer
, sizeof(buffer
) - 1, in
)) {
1101 if (user_content (in
, buffer
, &p
, infilename
) == DONE
) {
1103 die("empty \"#begin ... #end\" sequence");
1111 pp
= &part
->mp_next
;
1114 inform("premature end-of-file, missing #end, continuing...");
1121 die("unknown directive \"#%s\"", ci
->ci_type
);
1122 return NOTOK
; /* NOT REACHED */
1127 set_id (CT ct
, int top
)
1129 char contentid
[BUFSIZ
];
1131 static time_t clock
= 0;
1132 static char *msgfmt
;
1136 snprintf (contentid
, sizeof(contentid
), "%s\n", message_id (clock
, 1));
1138 msgfmt
= mh_xstrdup(contentid
);
1140 snprintf (contentid
, sizeof(contentid
), msgfmt
, top
? 0 : ++partno
);
1141 ct
->c_id
= mh_xstrdup(contentid
);
1146 * Fill out, or expand the various contents in the composition
1147 * draft. Read-in any necessary files. Parse and execute any
1148 * commands specified by profile composition strings.
1152 compose_content (CT ct
, int verbose
)
1154 CE ce
= &ct
->c_cefile
;
1156 switch (ct
->c_type
) {
1161 char partnam
[BUFSIZ
];
1162 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1166 snprintf (partnam
, sizeof(partnam
), "%s.", ct
->c_partno
);
1167 pp
= partnam
+ strlen (partnam
);
1172 /* first, we call compose_content on all the subparts */
1173 for (part
= m
->mp_parts
, partnum
= 1; part
; part
= part
->mp_next
, partnum
++) {
1174 CT p
= part
->mp_part
;
1176 sprintf (pp
, "%d", partnum
);
1177 p
->c_partno
= mh_xstrdup(partnam
);
1178 if (compose_content (p
, verbose
) == NOTOK
)
1183 * If the -rfc934mode switch is given, then check all
1184 * the subparts of a multipart/digest. If they are all
1185 * message/rfc822, then mark this content and all
1186 * subparts with the rfc934 compatibility mode flag.
1188 if (rfc934sw
&& ct
->c_subtype
== MULTI_DIGEST
) {
1191 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1192 CT p
= part
->mp_part
;
1194 if (p
->c_subtype
!= MESSAGE_RFC822
) {
1199 ct
->c_rfc934
= is934
;
1200 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1201 CT p
= part
->mp_part
;
1203 if ((p
->c_rfc934
= is934
))
1209 ct
->c_end
= (partnum
= strlen (prefix
) + 2) + 2;
1213 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
)
1214 ct
->c_end
+= part
->mp_part
->c_end
+ partnum
;
1220 /* Nothing to do for type message */
1224 * Discrete types (text/application/audio/image/video)
1232 char *vec
[4], buffer
[BUFSIZ
];
1234 CI ci
= &ct
->c_ctinfo
;
1237 if (!(cp
= ci
->ci_magic
))
1238 die("internal error(5)");
1240 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1241 adios("mhbuildsbr", "unable to create temporary file in %s",
1244 ce
->ce_file
= mh_xstrdup(tfile
);
1249 /* Get buffer ready to go */
1252 buflen
= sizeof(buffer
);
1255 * Parse composition string into buffer
1257 for ( ; *cp
; cp
++) {
1262 /* insert parameters from directive */
1266 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
1267 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
,
1268 pm
->pm_name
, get_param_value(pm
, '?'));
1278 /* %f, and stdout is not-redirected */
1284 * insert temporary filename where
1285 * content should be written
1287 snprintf (bp
, buflen
, "%s", ce
->ce_file
);
1291 /* insert content subtype */
1292 strncpy (bp
, ci
->ci_subtype
, buflen
);
1296 /* insert character % */
1317 printf ("composing content %s/%s from command\n\t%s\n",
1318 ci
->ci_type
, ci
->ci_subtype
, buffer
);
1320 fflush (stdout
); /* not sure if need for -noverbose */
1327 if ((out
= fopen (ce
->ce_file
, "w")) == NULL
)
1328 adios (ce
->ce_file
, "unable to open for writing");
1333 adios ("fork", "unable to fork");
1338 dup2 (fileno (out
), 1);
1339 close (fileno (out
));
1340 execvp ("/bin/sh", vec
);
1341 fprintf (stderr
, "unable to exec ");
1348 if (pidXwait(child_id
, NULL
))
1354 /* Check size of file */
1355 if (listsw
&& ct
->c_end
== 0L) {
1358 if (stat (ce
->ce_file
, &st
) != NOTOK
)
1359 ct
->c_end
= (long) st
.st_size
;
1371 * 1) choose a transfer encoding.
1372 * 2) check for clashes with multipart boundary string.
1373 * 3) for text content, figure out which character set is being used.
1375 * If there is a clash with one of the contents and the multipart boundary,
1376 * this function will exit with NOTOK. This will cause the scanning process
1377 * to be repeated with a different multipart boundary. It is possible
1378 * (although highly unlikely) that this scan will be repeated multiple times.
1382 scan_content (CT ct
, size_t maxunencoded
)
1385 bool check8bit
= false, contains8bit
= false; /* check if contains 8bit data */
1386 bool checknul
= false, containsnul
= false; /* check if contains NULs */
1387 bool checklinelen
= false, linelen
= false; /* check for long lines */
1388 bool checkllinelen
= false; /* check for extra-long lines */
1389 bool checkboundary
= false, boundaryclash
= false; /* check if clashes with multipart boundary */
1390 bool checklinespace
= false, linespace
= false; /* check if any line ends with space */
1395 struct text
*t
= NULL
;
1397 CE ce
= &ct
->c_cefile
;
1400 * handle multipart by scanning all subparts
1401 * and then checking their encoding.
1403 if (ct
->c_type
== CT_MULTIPART
) {
1404 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1407 /* initially mark the domain of enclosing multipart as 7bit */
1408 ct
->c_encoding
= CE_7BIT
;
1410 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1411 CT p
= part
->mp_part
;
1413 if (scan_content (p
, maxunencoded
) == NOTOK
) /* choose encoding for subpart */
1416 /* if necessary, enlarge encoding for enclosing multipart */
1417 if (p
->c_encoding
== CE_BINARY
)
1418 ct
->c_encoding
= CE_BINARY
;
1419 if (p
->c_encoding
== CE_8BIT
&& ct
->c_encoding
!= CE_BINARY
)
1420 ct
->c_encoding
= CE_8BIT
;
1427 * Decide what to check while scanning this content. Note that
1428 * for text content we always check for 8bit characters if the
1429 * charset is unspecified, because that controls whether or not the
1430 * character set is us-ascii or retrieved from the locale. And
1431 * we check even if the charset is specified, to allow setting
1432 * the proper Content-Transfer-Encoding.
1435 if (ct
->c_type
== CT_TEXT
) {
1436 t
= (struct text
*) ct
->c_ctparams
;
1437 if (t
->tx_charset
== CHARSET_UNSPECIFIED
) {
1443 switch (ct
->c_reqencoding
) {
1445 checkllinelen
= true;
1446 checkboundary
= true;
1449 checkboundary
= true;
1454 /* Use the default rules based on content-type */
1455 switch (ct
->c_type
) {
1457 checkboundary
= true;
1458 checklinelen
= true;
1459 if (ct
->c_subtype
== TEXT_PLAIN
) {
1460 checklinespace
= false;
1462 checklinespace
= true;
1466 case CT_APPLICATION
:
1469 checklinelen
= true;
1470 checklinespace
= true;
1471 checkboundary
= true;
1475 checklinelen
= false;
1476 checklinespace
= false;
1478 /* don't check anything for message/external */
1479 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1480 checkboundary
= false;
1483 checkboundary
= true;
1492 * Don't check anything for these types,
1493 * since we are forcing use of base64, unless
1494 * the content-type was specified by a mhbuild directive.
1497 checklinelen
= false;
1498 checklinespace
= false;
1499 checkboundary
= false;
1505 * Scan the unencoded content
1507 if (check8bit
|| checklinelen
|| checklinespace
|| checkboundary
||
1508 checkllinelen
|| checknul
) {
1509 if ((in
= fopen (ce
->ce_file
, "r")) == NULL
)
1510 adios (ce
->ce_file
, "unable to open for reading");
1511 prefix_len
= strlen (prefix
);
1513 while ((gotlen
= getline(&bufp
, &buflen
, in
)) != -1) {
1515 * Check for 8bit and NUL data.
1517 for (cp
= bufp
; (check8bit
|| checknul
) &&
1518 cp
< bufp
+ gotlen
; cp
++) {
1519 if (!isascii ((unsigned char) *cp
)) {
1520 contains8bit
= true;
1521 check8bit
= false; /* no need to keep checking */
1525 checknul
= false; /* no need to keep checking */
1530 * Check line length.
1532 if (checklinelen
&& ((size_t)gotlen
> maxunencoded
+ 1)) {
1534 checklinelen
= false; /* no need to keep checking */
1538 * RFC 5322 specifies that a message cannot contain a line
1539 * greater than 998 characters (excluding the CRLF). If we
1540 * get one of those lines and linelen is NOT set, then abort.
1543 if (checkllinelen
&& !linelen
&&
1544 (gotlen
> MAXLONGLINE
+ 1)) {
1545 die("Line in content exceeds maximum line limit (%d)",
1550 * Check if line ends with a space.
1552 if (checklinespace
&& (cp
= bufp
+ gotlen
- 2) > bufp
&&
1553 isspace ((unsigned char) *cp
)) {
1555 checklinespace
= false; /* no need to keep checking */
1559 * Check if content contains a line that clashes
1560 * with our standard boundary for multipart messages.
1562 if (checkboundary
&& bufp
[0] == '-' && bufp
[1] == '-') {
1563 for (cp
= bufp
+ gotlen
- 1; cp
>= bufp
; cp
--)
1564 if (!isspace ((unsigned char) *cp
))
1567 if (!strncmp(bufp
+ 2, prefix
, prefix_len
) &&
1568 isdigit((unsigned char) bufp
[2 + prefix_len
])) {
1569 boundaryclash
= true;
1570 checkboundary
= false; /* no need to keep checking */
1579 * If the content is text and didn't specify a character set,
1580 * we need to figure out which one was used.
1582 set_charset (ct
, contains8bit
);
1585 * Decide which transfer encoding to use.
1588 if (ct
->c_reqencoding
!= CE_UNKNOWN
)
1589 ct
->c_encoding
= ct
->c_reqencoding
;
1591 int wants_q_p
= (containsnul
|| linelen
|| linespace
|| checksw
);
1593 switch (ct
->c_type
) {
1596 ct
->c_encoding
= CE_QUOTED
;
1597 else if (contains8bit
)
1598 ct
->c_encoding
= CE_8BIT
;
1600 ct
->c_encoding
= CE_7BIT
;
1604 case CT_APPLICATION
:
1605 /* For application type, use base64, except when postscript */
1606 if (wants_q_p
|| contains8bit
) {
1607 if (ct
->c_subtype
== APPLICATION_POSTSCRIPT
)
1608 ct
->c_encoding
= CE_QUOTED
; /* historical */
1610 ct
->c_encoding
= CE_BASE64
;
1612 ct
->c_encoding
= CE_7BIT
;
1617 ct
->c_encoding
= contains8bit
? CE_8BIT
: CE_7BIT
;
1623 /* For audio, image, and video contents, just use base64 */
1624 ct
->c_encoding
= CE_BASE64
;
1629 return boundaryclash
? NOTOK
: OK
;
1634 * Scan the content structures, and build header
1635 * fields that will need to be output into the
1640 build_headers (CT ct
, int header_encoding
)
1642 int cc
, mailbody
, extbody
, len
;
1643 char *np
, *vp
, buffer
[BUFSIZ
];
1644 CI ci
= &ct
->c_ctinfo
;
1647 * If message is type multipart, then add the multipart
1648 * boundary to the list of attribute/value pairs.
1650 if (ct
->c_type
== CT_MULTIPART
) {
1651 static int level
= 0; /* store nesting level */
1653 snprintf (buffer
, sizeof(buffer
), "%s%d", prefix
, level
++);
1654 add_param(&ci
->ci_first_pm
, &ci
->ci_last_pm
, "boundary", buffer
, 0);
1658 * Skip the output of Content-Type, parameters, content
1659 * description and disposition, and Content-ID if the
1660 * content is of type "message" and the rfc934 compatibility
1661 * flag is set (which means we are inside multipart/digest
1662 * and the switch -rfc934mode was given).
1664 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_rfc934
)
1668 * output the content type and subtype
1670 np
= mh_xstrdup(TYPE_FIELD
);
1671 vp
= concat (" ", ci
->ci_type
, "/", ci
->ci_subtype
, NULL
);
1673 /* keep track of length of line */
1674 len
= LEN(TYPE_FIELD
) + strlen (ci
->ci_type
)
1675 + strlen (ci
->ci_subtype
) + 3;
1677 extbody
= ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_EXTERNAL
;
1678 mailbody
= extbody
&& ((struct exbody
*) ct
->c_ctparams
)->eb_body
;
1681 * Append the attribute/value pairs to
1682 * the end of the Content-Type line.
1685 if (ci
->ci_first_pm
) {
1686 char *s
= output_params(len
, ci
->ci_first_pm
, &len
, mailbody
);
1689 die("Internal error: failed outputting Content-Type "
1697 * Append any RFC-822 comment to the end of
1698 * the Content-Type line.
1700 if (ci
->ci_comment
) {
1701 snprintf (buffer
, sizeof(buffer
), "(%s)", ci
->ci_comment
);
1702 if (len
+ 1 + (cc
= 2 + strlen (ci
->ci_comment
)) >= CPERLIN
) {
1703 vp
= add ("\n\t", vp
);
1709 vp
= add (buffer
, vp
);
1712 vp
= add ("\n", vp
);
1713 add_header (ct
, np
, vp
);
1716 * output the Content-ID, unless disabled by -nocontentid. Note that
1717 * RFC 2045 always requires a Content-ID header for message/external-body
1720 if ((contentidsw
|| ct
->c_ctexbody
) && ct
->c_id
) {
1721 np
= mh_xstrdup(ID_FIELD
);
1722 vp
= concat (" ", ct
->c_id
, NULL
);
1723 add_header (ct
, np
, vp
);
1726 * output the Content-Description
1729 np
= mh_xstrdup(DESCR_FIELD
);
1730 vp
= concat (" ", ct
->c_descr
, NULL
);
1731 if (header_encoding
!= CE_8BIT
) {
1732 if (encode_rfc2047(DESCR_FIELD
, &vp
, header_encoding
, NULL
)) {
1733 die("Unable to encode %s header", DESCR_FIELD
);
1736 add_header (ct
, np
, vp
);
1740 * output the Content-Disposition. If it's NULL but c_dispo_type is
1741 * set, then we need to build it.
1744 np
= mh_xstrdup(DISPO_FIELD
);
1745 vp
= concat (" ", ct
->c_dispo
, NULL
);
1746 add_header (ct
, np
, vp
);
1747 } else if (ct
->c_dispo_type
) {
1748 vp
= concat (" ", ct
->c_dispo_type
, NULL
);
1749 len
= LEN(DISPO_FIELD
) + strlen(vp
) + 1;
1750 np
= output_params(len
, ct
->c_dispo_first
, NULL
, 0);
1754 add_header (ct
, mh_xstrdup(DISPO_FIELD
), vp
);
1759 * If this is the internal content structure for a
1760 * "message/external", then we are done with the
1761 * headers (since it has no body).
1767 * output the Content-MD5
1770 np
= mh_xstrdup(MD5_FIELD
);
1771 vp
= calculate_digest (ct
, ct
->c_encoding
== CE_QUOTED
);
1772 add_header (ct
, np
, vp
);
1776 * output the Content-Transfer-Encoding
1777 * If using EAI and message body is 7-bit, force 8-bit C-T-E.
1779 if (header_encoding
== CE_8BIT
&& ct
->c_encoding
== CE_7BIT
) {
1780 ct
->c_encoding
= CE_8BIT
;
1783 switch (ct
->c_encoding
) {
1785 /* Nothing to output */
1789 np
= mh_xstrdup(ENCODING_FIELD
);
1790 vp
= concat (" ", "8bit", "\n", NULL
);
1791 add_header (ct
, np
, vp
);
1795 if (ct
->c_type
== CT_MESSAGE
|| ct
->c_type
== CT_MULTIPART
)
1796 die("internal error, invalid encoding");
1798 np
= mh_xstrdup(ENCODING_FIELD
);
1799 vp
= concat (" ", "quoted-printable", "\n", NULL
);
1800 add_header (ct
, np
, vp
);
1804 if (ct
->c_type
== CT_MESSAGE
|| ct
->c_type
== CT_MULTIPART
)
1805 die("internal error, invalid encoding");
1807 np
= mh_xstrdup(ENCODING_FIELD
);
1808 vp
= concat (" ", "base64", "\n", NULL
);
1809 add_header (ct
, np
, vp
);
1813 if (ct
->c_type
== CT_MESSAGE
)
1814 die("internal error, invalid encoding");
1816 np
= mh_xstrdup(ENCODING_FIELD
);
1817 vp
= concat (" ", "binary", "\n", NULL
);
1818 add_header (ct
, np
, vp
);
1822 die("unknown transfer encoding in content");
1827 * Additional content specific header processing
1829 switch (ct
->c_type
) {
1832 struct multipart
*m
;
1835 m
= (struct multipart
*) ct
->c_ctparams
;
1836 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1840 build_headers (p
, header_encoding
);
1846 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1849 e
= (struct exbody
*) ct
->c_ctparams
;
1850 build_headers (e
->eb_content
, header_encoding
);
1863 static char nib2b64
[0x40+1] =
1864 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1867 calculate_digest (CT ct
, int asciiP
)
1872 unsigned char digest
[16];
1873 unsigned char outbuf
[25];
1875 CE ce
= &ct
->c_cefile
;
1876 char *infilename
= ce
->ce_file
? ce
->ce_file
: ct
->c_file
;
1880 if ((in
= fopen (infilename
, "r")) == NULL
)
1881 adios (infilename
, "unable to open for reading");
1883 /* Initialize md5 context */
1884 MD5Init (&mdContext
);
1886 /* calculate md5 message digest */
1891 while ((gotlen
= getline(&bufp
, &buflen
, in
)) != -1) {
1894 cp
= bufp
+ gotlen
- 1;
1895 if ((c
= *cp
) == '\n')
1898 MD5Update (&mdContext
, (unsigned char *) bufp
,
1899 (unsigned int) gotlen
);
1902 MD5Update (&mdContext
, (unsigned char *) "\r\n", 2);
1905 char buffer
[BUFSIZ
];
1906 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), in
)) > 0)
1907 MD5Update (&mdContext
, (unsigned char *) buffer
, (unsigned int) cc
);
1910 /* md5 finalization. Write digest and zero md5 context */
1911 MD5Final (digest
, &mdContext
);
1916 /* print debugging info */
1920 fprintf (stderr
, "MD5 digest=");
1921 for (ep
= (dp
= digest
) + sizeof digest
;
1923 fprintf (stderr
, "%02x", *dp
& 0xff);
1924 fprintf (stderr
, "\n");
1927 /* encode the digest using base64 */
1928 for (dp
= digest
, op
= (char *) outbuf
,
1930 cc
> 0; cc
-= 3, op
+= 4) {
1934 bits
= (*dp
++ & 0xff) << 16;
1936 bits
|= (*dp
++ & 0xff) << 8;
1938 bits
|= *dp
++ & 0xff;
1941 for (bp
= op
+ 4; bp
> op
; bits
>>= 6)
1942 *--bp
= nib2b64
[bits
& 0x3f];
1950 /* null terminate string */
1953 /* now make copy and return string */
1954 vp
= concat (" ", outbuf
, "\n", NULL
);
1959 * Set things up for the content structure for file "filename" that
1964 setup_attach_content(CT ct
, char *filename
)
1966 char *type
, *simplename
= r1bindex(filename
, '/');
1967 struct str2init
*s2i
;
1970 if (! (type
= mime_type(filename
))) {
1971 die("Unable to determine MIME type of \"%s\"", filename
);
1975 * Parse the Content-Type. get_ctinfo() parses MIME parameters, but
1976 * since we're just feeding it a MIME type we have to add those ourselves.
1977 * Map that to a valid content-type label and call any initialization
1981 if (get_ctinfo(type
, ct
, 0) == NOTOK
)
1986 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
1987 if (strcasecmp(ct
->c_ctinfo
.ci_type
, s2i
->si_key
) == 0)
1989 if (!s2i
->si_key
&& !uprf(ct
->c_ctinfo
.ci_type
, "X-"))
1993 * Make sure the type isn't incompatible with what we can handle
1996 switch (ct
->c_type
= s2i
->si_val
) {
1998 die("multipart types must be specified by mhbuild directives");
2002 if (strcasecmp(ct
->c_ctinfo
.ci_subtype
, "partial") == 0)
2003 die("Sorry, %s/%s isn't supported", ct
->c_ctinfo
.ci_type
,
2004 ct
->c_ctinfo
.ci_subtype
);
2005 if (strcasecmp(ct
->c_ctinfo
.ci_subtype
, "external-body") == 0)
2006 die("external-body messages must be specified "
2007 "by mhbuild directives");
2012 * This sets the subtype, if it's significant
2014 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
2015 (*ct
->c_ctinitfnx
)(ct
);
2020 * Feed in a few attributes; specifically, the name attribute, the
2021 * content-description, and the content-disposition.
2024 for (pm
= ct
->c_ctinfo
.ci_first_pm
; pm
; pm
= pm
->pm_next
) {
2025 if (strcasecmp(pm
->pm_name
, "name") == 0) {
2027 pm
->pm_value
= mh_xstrdup(simplename
);
2033 add_param(&ct
->c_ctinfo
.ci_first_pm
, &ct
->c_ctinfo
.ci_last_pm
,
2034 "name", simplename
, 0);
2036 ct
->c_descr
= mh_xstrdup(simplename
);
2037 ct
->c_descr
= add("\n", ct
->c_descr
);
2038 ct
->c_cefile
.ce_file
= mh_xstrdup(filename
);
2040 set_disposition (ct
);
2042 add_param(&ct
->c_dispo_first
, &ct
->c_dispo_last
, "filename", simplename
, 0);
2046 * If disposition type hasn't already been set in ct:
2047 * Look for mhbuild-disposition-<type>/<subtype> entry
2048 * that specifies Content-Disposition type. Only
2049 * 'attachment' and 'inline' are allowed. Default to
2053 set_disposition (CT ct
)
2055 if (ct
->c_dispo_type
== NULL
) {
2056 char *cp
= context_find_by_type ("disposition", ct
->c_ctinfo
.ci_type
,
2057 ct
->c_ctinfo
.ci_subtype
);
2059 if (cp
&& strcasecmp (cp
, "attachment") &&
2060 strcasecmp (cp
, "inline")) {
2061 inform("configuration problem: %s-disposition-%s%s%s specifies "
2062 "'%s' but only 'attachment' and 'inline' are allowed, "
2063 "continuing...", invo_name
,
2064 ct
->c_ctinfo
.ci_type
,
2065 ct
->c_ctinfo
.ci_subtype
? "/" : "",
2066 FENDNULL(ct
->c_ctinfo
.ci_subtype
),
2072 ct
->c_dispo_type
= mh_xstrdup(cp
);
2077 * Set text content charset if it was unspecified. contains8bit
2079 * 0: content does not contain 8-bit characters
2080 * 1: content contains 8-bit characters
2081 * -1: ignore content and use user's locale to determine charset
2084 set_charset (CT ct
, int contains8bit
)
2086 if (ct
->c_type
== CT_TEXT
) {
2089 if (ct
->c_ctparams
== NULL
) {
2092 t
->tx_charset
= CHARSET_UNSPECIFIED
;
2094 t
= (struct text
*) ct
->c_ctparams
;
2097 if (t
->tx_charset
== CHARSET_UNSPECIFIED
) {
2098 CI ci
= &ct
->c_ctinfo
;
2099 char *eightbitcharset
= write_charset_8bit();
2100 char *charset
= contains8bit
? eightbitcharset
: "us-ascii";
2102 if (contains8bit
== 1 &&
2103 strcasecmp (eightbitcharset
, "US-ASCII") == 0) {
2104 die("Text content contains 8 bit characters, but "
2105 "character set is US-ASCII");
2108 add_param (&ci
->ci_first_pm
, &ci
->ci_last_pm
, "charset", charset
,
2111 t
->tx_charset
= CHARSET_SPECIFIED
;
2118 * Look at all of the replied-to message parts and expand any that
2119 * are matched by a pseudoheader. Except don't descend into
2123 expand_pseudoheaders (CT ct
, struct multipart
*m
, const char *infile
,
2124 const convert_list
*convert_head
)
2126 /* text_plain_ct is used to concatenate all of the text/plain
2127 replies into one part, instead of having each one in a separate
2129 CT text_plain_ct
= NULL
;
2131 switch (ct
->c_type
) {
2132 case CT_MULTIPART
: {
2133 struct multipart
*mp
= (struct multipart
*) ct
->c_ctparams
;
2136 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
2137 bool matched
= false;
2139 /* The parts are in descending priority order (defined by
2140 RFC 2046 Sec. 5.1.4) because they were reversed by
2141 parse_mime (). So, stop looking for matches with
2142 immediate subparts after the first match of an
2144 for (part
= mp
->mp_parts
; ! matched
&& part
; part
= part
->mp_next
) {
2145 char *type_subtype
=
2146 concat (part
->mp_part
->c_ctinfo
.ci_type
, "/",
2147 part
->mp_part
->c_ctinfo
.ci_subtype
, NULL
);
2149 if (part
->mp_part
->c_type
== CT_MULTIPART
) {
2150 expand_pseudoheaders (part
->mp_part
, m
, infile
,
2153 const convert_list
*c
;
2155 for (c
= convert_head
; c
; c
= c
->next
) {
2156 if (! strcasecmp (type_subtype
, c
->type
)) {
2157 expand_pseudoheader (part
->mp_part
, &text_plain_ct
,
2159 c
->type
, c
->argstring
);
2165 free (type_subtype
);
2168 for (part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
2169 expand_pseudoheaders (part
->mp_part
, m
, infile
, convert_head
);
2176 char *type_subtype
=
2177 concat (ct
->c_ctinfo
.ci_type
, "/", ct
->c_ctinfo
.ci_subtype
,
2179 const convert_list
*c
;
2181 for (c
= convert_head
; c
; c
= c
->next
) {
2182 if (! strcasecmp (type_subtype
, c
->type
)) {
2183 expand_pseudoheader (ct
, &text_plain_ct
, m
, infile
, c
->type
,
2188 free (type_subtype
);
2196 * Expand a single pseudoheader. It's for the specified type.
2199 expand_pseudoheader (CT ct
, CT
*text_plain_ct
, struct multipart
*m
,
2200 const char *infile
, const char *type
,
2201 const char *argstring
)
2204 FILE *reply_fp
= NULL
;
2205 char *convert
, *type_p
, *subtype_p
;
2206 char *convert_command
;
2207 char *charset
= NULL
;
2209 struct str2init
*s2i
;
2214 type_p
= getcpy (type
);
2215 if ((subtype_p
= strchr (type_p
, '/'))) {
2216 *subtype_p
++ = '\0';
2217 convert
= context_find_by_type ("convert", type_p
, subtype_p
);
2220 type_p
= concat ("mhbuild-convert-", type
, NULL
);
2221 convert
= context_find (type_p
);
2226 /* No mhbuild-convert- entry in mhn.defaults or profile for type. */
2229 /* reply_file is used to pass the output of the convert. */
2230 reply_file
= getcpy (m_mktemp2 (NULL
, invo_name
, NULL
, NULL
));
2232 concat (convert
, " ", FENDNULL(argstring
), " >", reply_file
, NULL
);
2234 /* Convert here . . . */
2235 ct
->c_storeproc
= mh_xstrdup(convert_command
);
2236 ct
->c_umask
= ~m_gmprot ();
2238 if ((status
= show_content_aux (ct
, 0, convert_command
, NULL
, NULL
)) !=
2240 inform("store of %s content failed, continuing...", type
);
2242 free (convert_command
);
2244 /* Fill out the the new ct, reply_ct. */
2246 init_decoded_content (reply_ct
, infile
);
2248 if (extract_headers (reply_ct
, reply_file
, &reply_fp
) == NOTOK
) {
2249 inform("failed to extract headers from convert output in %s, "
2250 "continuing...", reply_file
);
2255 /* This sets reply_ct->c_ctparams, and reply_ct->c_termproc if the
2256 charset can't be handled natively. */
2257 for (s2i
= str2cts
; s2i
->si_key
; s2i
++) {
2258 if (strcasecmp(reply_ct
->c_ctinfo
.ci_type
, s2i
->si_key
) == 0) {
2263 if ((reply_ct
->c_ctinitfnx
= s2i
->si_init
)) {
2264 (*reply_ct
->c_ctinitfnx
)(reply_ct
);
2267 if ((cp
= get_param (reply_ct
->c_ctinfo
.ci_first_pm
, "charset", '?', 1))) {
2268 /* The reply Content-Type had the charset. */
2271 set_charset (reply_ct
, -1);
2272 charset
= get_param (reply_ct
->c_ctinfo
.ci_first_pm
, "charset", '?', 1);
2275 /* Concatenate text/plain parts. */
2276 if (reply_ct
->c_type
== CT_TEXT
&& reply_ct
->c_subtype
== TEXT_PLAIN
) {
2277 if (! *text_plain_ct
&& m
->mp_parts
&& m
->mp_parts
->mp_part
&&
2278 m
->mp_parts
->mp_part
->c_type
== CT_TEXT
&&
2279 m
->mp_parts
->mp_part
->c_subtype
== TEXT_PLAIN
) {
2280 *text_plain_ct
= m
->mp_parts
->mp_part
;
2281 /* Make sure that the charset is set in the text/plain part. */
2282 set_charset (*text_plain_ct
, -1);
2285 if (*text_plain_ct
) {
2286 /* Only concatenate if the charsets are identical. */
2287 char *text_plain_ct_charset
=
2288 get_param ((*text_plain_ct
)->c_ctinfo
.ci_first_pm
, "charset",
2291 if (strcasecmp (text_plain_ct_charset
, charset
) == 0) {
2292 /* Append this text/plain reply to the first one.
2293 If there's a problem anywhere along the way,
2294 instead attach it is a separate part. */
2295 int text_plain_reply
=
2296 open ((*text_plain_ct
)->c_cefile
.ce_file
,
2297 O_WRONLY
| O_APPEND
);
2298 int addl_reply
= open (reply_file
, O_RDONLY
);
2300 if (text_plain_reply
!= NOTOK
&& addl_reply
!= NOTOK
) {
2301 /* Insert blank line before each addl part. */
2302 /* It would be nice not to do this for the first one. */
2303 if (write (text_plain_reply
, "\n", 1) == 1) {
2304 /* Copy the text from the new reply and
2305 then free its Content struct. */
2306 cpydata (addl_reply
, text_plain_reply
,
2307 (*text_plain_ct
)->c_cefile
.ce_file
,
2309 if (close (text_plain_reply
) == OK
&&
2310 close (addl_reply
) == OK
) {
2311 /* If appended text needed 8-bit but first text didn't,
2312 propagate the 8-bit indication. */
2313 if ((*text_plain_ct
)->c_reqencoding
== CE_7BIT
&&
2314 reply_ct
->c_reqencoding
== CE_8BIT
) {
2315 (*text_plain_ct
)->c_reqencoding
= CE_8BIT
;
2318 if (reply_fp
) { fclose (reply_fp
); }
2320 free_content (reply_ct
);
2327 *text_plain_ct
= reply_ct
;
2331 reply_ct
->c_cefile
.ce_file
= reply_file
;
2332 reply_ct
->c_cefile
.ce_fp
= reply_fp
;
2333 reply_ct
->c_cefile
.ce_unlink
= 1;
2335 /* Attach the new part to the parent multipart/mixed, "m". */
2337 part
->mp_part
= reply_ct
;
2341 for (p
= m
->mp_parts
; p
&& p
->mp_next
; p
= p
->mp_next
) { continue; }
2349 /* Extract any Content-Type header from beginning of convert output. */
2351 extract_headers (CT ct
, char *reply_file
, FILE **reply_fp
)
2353 char *buffer
= NULL
, *cp
, *end_of_header
;
2354 bool found_header
= false;
2355 struct stat statbuf
;
2357 /* Read the convert reply from the file to memory. */
2358 if (stat (reply_file
, &statbuf
) == NOTOK
) {
2359 admonish (reply_file
, "failed to stat");
2360 goto failed_to_extract_ct
;
2363 buffer
= mh_xmalloc (statbuf
.st_size
+ 1);
2365 if ((*reply_fp
= fopen (reply_file
, "r+")) == NULL
||
2366 fread (buffer
, 1, (size_t) statbuf
.st_size
, *reply_fp
) <
2367 (size_t) statbuf
.st_size
) {
2368 admonish (reply_file
, "failed to read");
2369 goto failed_to_extract_ct
;
2371 buffer
[statbuf
.st_size
] = '\0';
2373 /* Look for a header in the convert reply. */
2374 if (strncasecmp (buffer
, TYPE_FIELD
, LEN(TYPE_FIELD
)) == 0 &&
2375 buffer
[LEN(TYPE_FIELD
)] == ':') {
2376 if ((end_of_header
= strstr (buffer
, "\r\n\r\n"))) {
2378 found_header
= true;
2379 } else if ((end_of_header
= strstr (buffer
, "\n\n"))) {
2381 found_header
= true;
2391 /* Truncate buffer to just the C-T. */
2392 *end_of_header
= '\0';
2393 n
= strlen (buffer
);
2395 if (get_ctinfo (buffer
+ 14, ct
, 0) != OK
) {
2396 inform("unable to get content info for reply, continuing...");
2397 goto failed_to_extract_ct
;
2400 /* Hack. Use parse_mime() to detect the type/subtype of the
2401 reply, which we'll use below. */
2402 tmp_file
= getcpy (m_mktemp2 (NULL
, invo_name
, NULL
, NULL
));
2403 tmp_f
= fopen(tmp_file
, "w");
2405 goto failed_to_extract_ct
;
2406 written
= fwrite(buffer
, 1, n
, tmp_f
);
2409 goto failed_to_extract_ct
;
2411 tmp_ct
= parse_mime (tmp_file
);
2413 /* The type and subtype were detected from the reply
2414 using parse_mime() above. */
2415 ct
->c_type
= tmp_ct
->c_type
;
2416 ct
->c_subtype
= tmp_ct
->c_subtype
;
2417 free_content (tmp_ct
);
2422 /* Rewrite the content without the header. */
2423 cp
= end_of_header
+ 1;
2426 if (fwrite (cp
, 1, statbuf
.st_size
- (cp
- buffer
), *reply_fp
) <
2427 (size_t) (statbuf
.st_size
- (cp
- buffer
))) {
2428 admonish (reply_file
, "failed to write");
2429 goto failed_to_extract_ct
;
2432 if (ftruncate (fileno (*reply_fp
), statbuf
.st_size
- (cp
- buffer
)) !=
2434 advise (reply_file
, "ftruncate");
2435 goto failed_to_extract_ct
;
2438 /* No header section, assume the reply is text/plain. */
2439 ct
->c_type
= CT_TEXT
;
2440 ct
->c_subtype
= TEXT_PLAIN
;
2441 if (get_ctinfo ("text/plain", ct
, 0) == NOTOK
) {
2442 /* This never should fail, but just in case. */
2443 die("unable to get content info for reply");
2447 /* free_encoding() will close reply_fp, which is passed through
2448 ct->c_cefile.ce_fp. */
2452 failed_to_extract_ct
:
2453 if (*reply_fp
) { fclose (*reply_fp
); }