3 * mhbuildsbr.c -- routines to expand/translate MIME composition files
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
11 * This code was originally part of mhn.c. I split it into
12 * a separate program (mhbuild.c) and then later split it
13 * again (mhbuildsbr.c). But the code still has some of
14 * the mhn.c code in it. This program needs additional
15 * streamlining and removal of unneeded code.
20 #include <h/signals.h>
25 #include <h/mhparse.h>
28 #ifdef HAVE_SYS_TIME_H
29 # include <sys/time.h>
38 extern int contentidsw
;
41 extern int rcachesw
; /* mhcachesbr.c */
42 extern int wcachesw
; /* mhcachesbr.c */
44 static char prefix
[] = "----- =_aaaaaaaaaa";
48 struct attach_list
*next
;
51 typedef struct convert_list
{
55 struct convert_list
*next
;
59 * Maximum size of URL token in message/external-body
62 #define MAXURLTOKEN 40
66 void content_error (char *, CT
, char *, ...);
69 int find_cache (CT
, int, int *, char *, char *, int);
73 void freects_done (int) NORETURN
;
74 void free_ctinfo (CT
);
75 void free_encoding (CT
, int);
80 static int init_decoded_content (CT
, const char *);
81 static void setup_attach_content(CT
, char *);
82 static void set_disposition (CT
);
83 static void set_charset (CT
, int);
84 static void expand_pseudoheaders (CT
, struct multipart
*, const char *,
85 const convert_list
*);
86 static void expand_pseudoheader (CT
, CT
*, struct multipart
*, const char *,
87 const char *, const char *);
88 static char *fgetstr (char *, int, FILE *);
89 static int user_content (FILE *, char *, CT
*, const char *infilename
);
90 static void set_id (CT
, int);
91 static int compose_content (CT
, int);
92 static int scan_content (CT
, size_t);
93 static int build_headers (CT
, int);
94 static char *calculate_digest (CT
, int);
95 static int extract_headers (CT
, char *, FILE **);
98 static unsigned char directives_stack
[32];
99 static unsigned int directives_index
;
101 static int do_direct(void)
103 return directives_stack
[directives_index
];
106 static void directive_onoff(int onoff
)
108 if (directives_index
>= sizeof(directives_stack
) - 1) {
109 fprintf(stderr
, "mhbuild: #on/off overflow, continuing\n");
112 directives_stack
[++directives_index
] = onoff
;
115 static void directive_init(int onoff
)
117 directives_index
= 0;
118 directives_stack
[0] = onoff
;
121 static void directive_pop(void)
123 if (directives_index
> 0)
126 fprintf(stderr
, "mhbuild: #pop underflow, continuing\n");
130 * Main routine for translating composition file
131 * into valid MIME message. It translates the draft
132 * into a content structure (actually a tree of content
133 * structures). This message then can be manipulated
134 * in various ways, including being output via
139 build_mime (char *infile
, int autobuild
, int dist
, int directives
,
140 int header_encoding
, size_t maxunencoded
, int verbose
)
143 char buf
[BUFSIZ
], name
[NAMESZ
];
150 m_getfld_state_t gstate
= 0;
151 struct attach_list
*attach_head
= NULL
, *attach_tail
= NULL
, *at_entry
;
152 convert_list
*convert_head
= NULL
, *convert_tail
= NULL
, *convert
;
154 directive_init(directives
);
156 umask (~m_gmprot ());
158 /* open the composition draft */
159 if ((in
= fopen (infile
, "r")) == NULL
)
160 adios (infile
, "unable to open for reading");
163 * Allocate space for primary (outside) content
165 if ((ct
= (CT
) mh_xcalloc (1, sizeof(*ct
))) == NULL
)
166 adios (NULL
, "out of memory");
169 * Allocate structure for handling decoded content
170 * for this part. We don't really need this, but
171 * allocate it to remain consistent.
173 init_decoded_content (ct
, infile
);
176 * Parse some of the header fields in the composition
177 * draft into the linked list of header fields for
178 * the new MIME message.
180 m_getfld_track_filepos (&gstate
, in
);
181 for (compnum
= 1;;) {
182 int bufsz
= sizeof buf
;
183 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
)) {
188 /* abort if draft has Mime-Version or C-T-E header field */
189 if (strcasecmp (name
, VRSN_FIELD
) == 0 ||
190 strcasecmp (name
, ENCODING_FIELD
) == 0) {
196 adios (NULL
, "draft shouldn't contain %s: field", name
);
200 /* ignore any Content-Type fields in the header */
201 if (!strcasecmp (name
, TYPE_FIELD
)) {
202 while (state
== FLDPLUS
) {
204 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
209 /* get copies of the buffers */
210 np
= add (name
, NULL
);
211 vp
= add (buf
, NULL
);
213 /* if necessary, get rest of field */
214 while (state
== FLDPLUS
) {
216 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
217 vp
= add (buf
, vp
); /* add to previous value */
221 * Now add the header data to the list, unless it's an attach
222 * header; in that case, add it to our attach list
225 if (strcasecmp(ATTACH_FIELD
, np
) == 0 ||
226 strcasecmp(ATTACH_FIELD_ALT
, np
) == 0) {
227 struct attach_list
*entry
;
228 char *s
= vp
, *e
= vp
+ strlen(vp
) - 1;
232 * Make sure we can find the start of this filename.
233 * If it's blank, we skip completely. Otherwise, strip
234 * off any leading spaces and trailing newlines.
237 while (isspace((unsigned char) *s
))
240 while (e
> s
&& *e
== '\n')
248 entry
= mh_xmalloc(sizeof(*entry
));
249 entry
->filename
= getcpy(s
);
254 attach_tail
->next
= entry
;
257 attach_head
= attach_tail
= entry
;
259 } else if (strncasecmp(MHBUILD_FILE_PSEUDOHEADER
, np
,
260 strlen (MHBUILD_FILE_PSEUDOHEADER
)) == 0) {
262 * Nmh-mhbuild-file-text/calendar: /home/user/Mail/inbox/9
264 char *type
= np
+ strlen (MHBUILD_FILE_PSEUDOHEADER
);
267 /* vp should begin with a space because m_getfld()
268 includes the space after the colon in buf. */
269 while (isspace((unsigned char) *filename
)) { ++filename
; }
270 /* Trim trailing newline and any other whitespace. */
273 for (convert
= convert_head
; convert
; convert
= convert
->next
) {
274 if (strcasecmp (convert
->type
, type
) == 0) { break; }
277 if (convert
->filename
&&
278 strcasecmp (convert
->filename
, filename
)) {
279 adios (NULL
, "Multiple %s headers with different files"
280 " not allowed", type
);
282 convert
->filename
= getcpy (filename
);
285 convert
= mh_xcalloc (sizeof *convert
, 1);
286 convert
->filename
= getcpy (filename
);
287 convert
->type
= getcpy (type
);
290 convert_tail
->next
= convert
;
292 convert_head
= convert
;
294 convert_tail
= convert
;
299 } else if (strncasecmp(MHBUILD_ARGS_PSEUDOHEADER
, np
,
300 strlen (MHBUILD_ARGS_PSEUDOHEADER
)) == 0) {
302 * Nmh-mhbuild-args-text/calendar: -reply accept
304 char *type
= np
+ strlen (MHBUILD_ARGS_PSEUDOHEADER
);
305 char *argstring
= vp
;
307 /* vp should begin with a space because m_getfld()
308 includes the space after the colon in buf. */
309 while (isspace((unsigned char) *argstring
)) { ++argstring
; }
310 /* Trim trailing newline and any other whitespace. */
313 for (convert
= convert_head
; convert
; convert
= convert
->next
) {
314 if (strcasecmp (convert
->type
, type
) == 0) { break; }
317 if (convert
->argstring
&&
318 strcasecmp (convert
->argstring
, argstring
)) {
319 adios (NULL
, "Multiple %s headers with different "
320 "argstrings not allowed", type
);
322 convert
->argstring
= getcpy (argstring
);
325 convert
= mh_xcalloc (sizeof *convert
, 1);
326 convert
->type
= getcpy (type
);
327 convert
->argstring
= getcpy (argstring
);
330 convert_tail
->next
= convert
;
332 convert_head
= convert
;
334 convert_tail
= convert
;
340 add_header (ct
, np
, vp
);
344 /* if this wasn't the last header field, then continue */
348 fseek (in
, (long) (-strlen (buf
)), SEEK_CUR
);
355 adios (NULL
, "message format error in component #%d", compnum
);
358 adios (NULL
, "getfld() returned %d", state
);
362 m_getfld_state_destroy (&gstate
);
365 * Iterate through the list of headers and call the function to MIME-ify
369 for (hp
= ct
->c_first_hf
; hp
!= NULL
; hp
= hp
->next
) {
370 if (encode_rfc2047(hp
->name
, &hp
->value
, header_encoding
, NULL
)) {
371 adios(NULL
, "Unable to encode header \"%s\"", hp
->name
);
376 * Now add the MIME-Version header field
377 * to the list of header fields.
381 np
= add (VRSN_FIELD
, NULL
);
382 vp
= concat (" ", VRSN_VALUE
, "\n", NULL
);
383 add_header (ct
, np
, vp
);
387 * We initally assume we will find multiple contents in the
388 * draft. So create a multipart/mixed content to hold everything.
389 * We can remove this later, if it is not needed.
391 if (get_ctinfo ("multipart/mixed", ct
, 0) == NOTOK
)
393 ct
->c_type
= CT_MULTIPART
;
394 ct
->c_subtype
= MULTI_MIXED
;
396 if ((m
= (struct multipart
*) mh_xcalloc (1, sizeof(*m
))) == NULL
)
397 adios (NULL
, "out of memory");
398 ct
->c_ctparams
= (void *) m
;
402 * read and parse the composition file
403 * and the directives it contains.
405 while (fgetstr (buf
, sizeof(buf
) - 1, in
)) {
409 if (user_content (in
, buf
, &p
, infile
) == DONE
) {
410 admonish (NULL
, "ignoring spurious #end");
416 if ((part
= (struct part
*) mh_xcalloc (1, sizeof(*part
))) == NULL
)
417 adios (NULL
, "out of memory");
424 * Add any Attach headers to the list of MIME parts at the end of the
428 for (at_entry
= attach_head
; at_entry
; ) {
429 struct attach_list
*at_prev
= at_entry
;
433 if (access(at_entry
->filename
, R_OK
) != 0) {
434 adios("reading", "Unable to open %s for", at_entry
->filename
);
437 if ((p
= (CT
) mh_xcalloc (1, sizeof(*p
))) == NULL
)
438 adios(NULL
, "out of memory");
440 init_decoded_content(p
, infile
);
443 * Initialize our content structure based on the filename,
444 * and fill in all of the relevant fields. Also place MIME
445 * parameters in the attributes array.
448 setup_attach_content(p
, at_entry
->filename
);
450 if ((part
= (struct part
*) mh_xcalloc (1, sizeof(*part
))) == NULL
)
451 adios (NULL
, "out of memory");
456 at_entry
= at_entry
->next
;
457 free(at_prev
->filename
);
462 * Handle the mhbuild pseudoheaders, which deal with specific
471 /* In case there are multiple calls that land here, prevent leak. */
472 for (ctp
= cts
; ctp
&& *ctp
; ++ctp
) { free_content (*ctp
); }
475 /* Extract the type part (as a CT) from filename. */
476 if (! (cts
= (CT
*) mh_xcalloc ((size_t) 2, sizeof *cts
))) {
477 adios (NULL
, "out of memory");
478 } else if (! (cts
[0] = parse_mime (convert_head
->filename
))) {
479 adios (NULL
, "failed to parse %s", convert_head
->filename
);
482 expand_pseudoheaders (cts
[0], m
, infile
, convert_head
);
484 /* Free the convert list. */
485 for (convert
= convert_head
; convert
; convert
= next
) {
486 next
= convert
->next
;
487 free (convert
->type
);
488 free (convert
->filename
);
489 free (convert
->argstring
);
496 * To allow for empty message bodies, if we've found NO content at all
497 * yet cook up an empty text/plain part.
505 if ((p
= (CT
) mh_xcalloc (1, sizeof(*p
))) == NULL
)
506 adios(NULL
, "out of memory");
508 init_decoded_content(p
, infile
);
510 if (get_ctinfo ("text/plain", p
, 0) == NOTOK
)
514 p
->c_subtype
= TEXT_PLAIN
;
515 p
->c_encoding
= CE_7BIT
;
517 * Sigh. ce_file contains the "decoded" contents of this part.
518 * So this seems like the best option available since we're going
519 * to call scan_content() on this.
521 p
->c_cefile
.ce_file
= getcpy("/dev/null");
522 p
->c_begin
= ftell(in
);
523 p
->c_end
= ftell(in
);
525 if ((t
= (struct text
*) mh_xcalloc (1, sizeof (*t
))) == NULL
)
526 adios (NULL
, "out of memory");
528 t
->tx_charset
= CHARSET_SPECIFIED
;
531 if ((part
= (struct part
*) mh_xcalloc (1, sizeof(*part
))) == NULL
)
532 adios (NULL
, "out of memory");
538 * close the composition draft since
539 * it's not needed any longer.
544 * If only one content was found, then remove and
545 * free the outer multipart content.
547 if (!m
->mp_parts
->mp_next
) {
550 p
= m
->mp_parts
->mp_part
;
551 m
->mp_parts
->mp_part
= NULL
;
553 /* move header fields */
554 p
->c_first_hf
= ct
->c_first_hf
;
555 p
->c_last_hf
= ct
->c_last_hf
;
556 ct
->c_first_hf
= NULL
;
557 ct
->c_last_hf
= NULL
;
566 * Fill out, or expand directives. Parse and execute
567 * commands specified by profile composition strings.
569 compose_content (ct
, verbose
);
571 if ((cp
= strchr(prefix
, 'a')) == NULL
)
572 adios (NULL
, "internal error(4)");
575 * Scan the contents. Choose a transfer encoding, and
576 * check if prefix for multipart boundary clashes with
577 * any of the contents.
579 while (scan_content (ct
, maxunencoded
) == NOTOK
) {
584 adios (NULL
, "giving up trying to find a unique delimiter string");
590 /* Build the rest of the header field structures */
592 build_headers (ct
, header_encoding
);
599 * Set up structures for placing unencoded
600 * content when building parts.
604 init_decoded_content (CT ct
, const char *filename
)
606 ct
->c_ceopenfnx
= open7Bit
; /* since unencoded */
607 ct
->c_ceclosefnx
= close_encoding
;
608 ct
->c_cesizefnx
= NULL
; /* since unencoded */
609 ct
->c_encoding
= CE_7BIT
; /* Seems like a reasonable default */
610 ct
->c_file
= add(filename
, NULL
);
617 fgetstr (char *s
, int n
, FILE *stream
)
623 for (ep
= (cp
= s
) + o_n
; cp
< ep
; ) {
626 if (!fgets (cp
, n
, stream
))
627 return (cp
!= s
? s
: NULL
);
629 if (cp
== s
&& *cp
!= '#')
632 cp
+= (i
= strlen (cp
)) - 1;
633 if (i
<= 1 || *cp
-- != '\n' || *cp
!= '\\')
639 if (strcmp(s
, "#on\n") == 0) {
641 } else if (strcmp(s
, "#off\n") == 0) {
643 } else if (strcmp(s
, "#pop\n") == 0) {
655 * Parse the composition draft for text and directives.
656 * Do initial setup of Content structure.
660 user_content (FILE *in
, char *buf
, CT
*ctp
, const char *infilename
)
668 struct str2init
*s2i
;
673 if (buf
[0] == '\n' || (do_direct() && strcmp (buf
, "#\n") == 0)) {
678 /* allocate basic Content structure */
679 if ((ct
= (CT
) mh_xcalloc (1, sizeof(*ct
))) == NULL
)
680 adios (NULL
, "out of memory");
683 /* allocate basic structure for handling decoded content */
684 init_decoded_content (ct
, infilename
);
691 * Handle inline text. Check if line
692 * is one of the following forms:
694 * 1) doesn't begin with '#' (implicit directive)
695 * 2) begins with "##" (implicit directive)
696 * 3) begins with "#<"
698 if (!do_direct() || buf
[0] != '#' || buf
[1] == '#' || buf
[1] == '<') {
702 char content
[BUFSIZ
];
706 if ((cp
= m_mktemp2(NULL
, invo_name
, NULL
, &out
)) == NULL
) {
707 adios("mhbuildsbr", "unable to create temporary file in %s",
711 /* use a temp file to collect the plain text lines */
712 ce
->ce_file
= add (cp
, NULL
);
715 if (do_direct() && (buf
[0] == '#' && buf
[1] == '<')) {
716 strncpy (content
, buf
+ 2, sizeof(content
));
723 /* the directive is implicit */
724 strncpy (content
, "text/plain", sizeof(content
));
726 strncpy (buffer
, (!do_direct() || buf
[0] != '#') ? buf
: buf
+ 1, sizeof(buffer
));
730 if (headers
>= 0 && do_direct() && uprf (buffer
, DESCR_FIELD
)
731 && buffer
[i
= strlen (DESCR_FIELD
)] == ':') {
735 ct
->c_descr
= add (buffer
+ i
+ 1, ct
->c_descr
);
736 if (!fgetstr (buffer
, sizeof(buffer
) - 1, in
))
737 adios (NULL
, "end-of-file after %s: field in plaintext", DESCR_FIELD
);
745 adios (NULL
, "#-directive after %s: field in plaintext", DESCR_FIELD
);
753 if (headers
>= 0 && do_direct() && uprf (buffer
, DISPO_FIELD
)
754 && buffer
[i
= strlen (DISPO_FIELD
)] == ':') {
758 ct
->c_dispo
= add (buffer
+ i
+ 1, ct
->c_dispo
);
759 if (!fgetstr (buffer
, sizeof(buffer
) - 1, in
))
760 adios (NULL
, "end-of-file after %s: field in plaintext", DISPO_FIELD
);
768 adios (NULL
, "#-directive after %s: field in plaintext", DISPO_FIELD
);
776 if (headers
!= 1 || buffer
[0] != '\n')
782 if ((cp
= fgetstr (buffer
, sizeof(buffer
) - 1, in
)) == NULL
)
784 if (do_direct() && buffer
[0] == '#') {
787 if (buffer
[1] != '#')
789 for (cp
= (bp
= buffer
) + 1; *cp
; cp
++)
796 ct
->c_end
= ftell (out
);
799 /* parse content type */
800 if (get_ctinfo (content
, ct
, inlineD
) == NOTOK
)
803 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
804 if (!strcasecmp (ci
->ci_type
, s2i
->si_key
))
806 if (!s2i
->si_key
&& !uprf (ci
->ci_type
, "X-"))
810 * check type specified (possibly implicitly)
812 switch (ct
->c_type
= s2i
->si_val
) {
814 if (!strcasecmp (ci
->ci_subtype
, "rfc822")) {
815 ct
->c_encoding
= CE_7BIT
;
820 adios (NULL
, "it doesn't make sense to define an in-line %s content",
821 ct
->c_type
== CT_MESSAGE
? "message" : "multipart");
826 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
827 (*ct
->c_ctinitfnx
) (ct
);
832 fseek (in
, pos
, SEEK_SET
);
837 * If we've reached this point, the next line
838 * must be some type of explicit directive.
841 /* check if directive is external-type */
842 extrnal
= (buf
[1] == '@');
844 /* parse directive */
845 if (get_ctinfo (buf
+ (extrnal
? 2 : 1), ct
, 1) == NOTOK
)
848 /* check directive against the list of MIME types */
849 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
850 if (!strcasecmp (ci
->ci_type
, s2i
->si_key
))
854 * Check if the directive specified a valid type.
855 * This will happen if it was one of the following forms:
862 adios (NULL
, "missing subtype in \"#%s\"", ci
->ci_type
);
864 switch (ct
->c_type
= s2i
->si_val
) {
866 adios (NULL
, "use \"#begin ... #end\" instead of \"#%s/%s\"",
867 ci
->ci_type
, ci
->ci_subtype
);
871 if (!strcasecmp (ci
->ci_subtype
, "partial"))
872 adios (NULL
, "sorry, \"#%s/%s\" isn't supported",
873 ci
->ci_type
, ci
->ci_subtype
);
874 if (!strcasecmp (ci
->ci_subtype
, "external-body"))
875 adios (NULL
, "use \"#@type/subtype ... [] ...\" instead of \"#%s/%s\"",
876 ci
->ci_type
, ci
->ci_subtype
);
879 "use \"#forw [+folder] [msgs]\" instead of \"#%s/%s\"",
880 ci
->ci_type
, ci
->ci_subtype
);
884 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
885 (*ct
->c_ctinitfnx
) (ct
);
890 * #@type/subtype (external types directive)
897 adios (NULL
, "need external information for \"#@%s/%s\"",
898 ci
->ci_type
, ci
->ci_subtype
);
901 snprintf (buffer
, sizeof(buffer
), "message/external-body; %s", ci
->ci_magic
);
906 * Since we are using the current Content structure to
907 * hold information about the type of the external
908 * reference, we need to create another Content structure
909 * for the message/external-body to wrap it in.
911 if ((ct
= (CT
) mh_xcalloc (1, sizeof(*ct
))) == NULL
)
912 adios (NULL
, "out of memory");
913 init_decoded_content(ct
, infilename
);
915 if (get_ctinfo (buffer
, ct
, 0) == NOTOK
)
917 ct
->c_type
= CT_MESSAGE
;
918 ct
->c_subtype
= MESSAGE_EXTERNAL
;
920 if ((e
= (struct exbody
*) mh_xcalloc (1, sizeof(*e
))) == NULL
)
921 adios (NULL
, "out of memory");
922 ct
->c_ctparams
= (void *) e
;
928 if (params_external (ct
, 1) == NOTOK
)
934 /* Handle [file] argument */
936 /* check if specifies command to execute */
937 if (*ci
->ci_magic
== '|' || *ci
->ci_magic
== '!') {
938 for (cp
= ci
->ci_magic
+ 1; isspace ((unsigned char) *cp
); cp
++)
941 adios (NULL
, "empty pipe command for #%s directive", ci
->ci_type
);
946 /* record filename of decoded contents */
947 ce
->ce_file
= ci
->ci_magic
;
948 if (access (ce
->ce_file
, R_OK
) == NOTOK
)
949 adios ("reading", "unable to access %s for", ce
->ce_file
);
950 if (listsw
&& stat (ce
->ce_file
, &st
) != NOTOK
)
951 ct
->c_end
= (long) st
.st_size
;
958 * No [file] argument, so check profile for
959 * method to compose content.
961 cp
= context_find_by_type ("compose", ci
->ci_type
, ci
->ci_subtype
);
963 content_error (NULL
, ct
, "don't know how to compose content");
966 ci
->ci_magic
= add (cp
, NULL
);
971 adios (NULL
, "external definition not allowed for \"#%s\"", ci
->ci_type
);
975 * #forw [+folder] [msgs]
977 if (!strcasecmp (ci
->ci_type
, "forw")) {
979 char *folder
, *arguments
[MAXARGS
];
983 ap
= brkstring (ci
->ci_magic
, " ", "\n");
984 copyip (ap
, arguments
, MAXARGS
);
986 arguments
[0] = "cur";
991 /* search the arguments for a folder name */
992 for (ap
= arguments
; *ap
; ap
++) {
994 if (*cp
== '+' || *cp
== '@') {
996 adios (NULL
, "only one folder per #forw directive");
998 folder
= pluspath (cp
);
1002 /* else, use the current folder */
1004 folder
= add (getfolder (1), NULL
);
1006 if (!(mp
= folder_read (folder
, 0)))
1007 adios (NULL
, "unable to read folder %s", folder
);
1008 for (ap
= arguments
; *ap
; ap
++) {
1010 if (*cp
!= '+' && *cp
!= '@')
1011 if (!m_convert (mp
, cp
))
1018 * If there is more than one message to include, make this
1019 * a content of type "multipart/digest" and insert each message
1020 * as a subpart. If there is only one message, then make this
1021 * a content of type "message/rfc822".
1023 if (mp
->numsel
> 1) {
1024 /* we are forwarding multiple messages */
1025 if (get_ctinfo ("multipart/digest", ct
, 0) == NOTOK
)
1027 ct
->c_type
= CT_MULTIPART
;
1028 ct
->c_subtype
= MULTI_DIGEST
;
1030 if ((m
= (struct multipart
*) mh_xcalloc (1, sizeof(*m
))) == NULL
)
1031 adios (NULL
, "out of memory");
1032 ct
->c_ctparams
= (void *) m
;
1035 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
1036 if (is_selected(mp
, msgnum
)) {
1041 if ((p
= (CT
) mh_xcalloc (1, sizeof(*p
))) == NULL
)
1042 adios (NULL
, "out of memory");
1043 init_decoded_content (p
, infilename
);
1045 if (get_ctinfo ("message/rfc822", p
, 0) == NOTOK
)
1047 p
->c_type
= CT_MESSAGE
;
1048 p
->c_subtype
= MESSAGE_RFC822
;
1050 snprintf (buffer
, sizeof(buffer
), "%s/%d", mp
->foldpath
, msgnum
);
1051 pe
->ce_file
= add (buffer
, NULL
);
1052 if (listsw
&& stat (pe
->ce_file
, &st
) != NOTOK
)
1053 p
->c_end
= (long) st
.st_size
;
1055 if ((part
= (struct part
*) mh_xcalloc (1, sizeof(*part
))) == NULL
)
1056 adios (NULL
, "out of memory");
1058 pp
= &part
->mp_next
;
1063 /* we are forwarding one message */
1064 if (get_ctinfo ("message/rfc822", ct
, 0) == NOTOK
)
1066 ct
->c_type
= CT_MESSAGE
;
1067 ct
->c_subtype
= MESSAGE_RFC822
;
1069 msgnum
= mp
->lowsel
;
1070 snprintf (buffer
, sizeof(buffer
), "%s/%d", mp
->foldpath
, msgnum
);
1071 ce
->ce_file
= add (buffer
, NULL
);
1072 if (listsw
&& stat (ce
->ce_file
, &st
) != NOTOK
)
1073 ct
->c_end
= (long) st
.st_size
;
1076 folder_free (mp
); /* free folder/message structure */
1083 if (!strcasecmp (ci
->ci_type
, "end")) {
1090 * #begin [ alternative | parallel ]
1092 if (!strcasecmp (ci
->ci_type
, "begin")) {
1093 if (!ci
->ci_magic
) {
1095 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
1096 } else if (!strcasecmp (ci
->ci_magic
, "alternative")) {
1097 vrsn
= MULTI_ALTERNATE
;
1098 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
1099 } else if (!strcasecmp (ci
->ci_magic
, "parallel")) {
1100 vrsn
= MULTI_PARALLEL
;
1101 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
1102 } else if (uprf (ci
->ci_magic
, "digest")) {
1105 vrsn
= MULTI_UNKNOWN
;
1110 snprintf (buffer
, sizeof(buffer
), "multipart/%s", cp
);
1111 if (get_ctinfo (buffer
, ct
, 0) == NOTOK
)
1113 ct
->c_type
= CT_MULTIPART
;
1114 ct
->c_subtype
= vrsn
;
1116 if ((m
= (struct multipart
*) mh_xcalloc (1, sizeof(*m
))) == NULL
)
1117 adios (NULL
, "out of memory");
1118 ct
->c_ctparams
= (void *) m
;
1121 while (fgetstr (buffer
, sizeof(buffer
) - 1, in
)) {
1125 if (user_content (in
, buffer
, &p
, infilename
) == DONE
) {
1127 adios (NULL
, "empty \"#begin ... #end\" sequence");
1133 if ((part
= (struct part
*) mh_xcalloc (1, sizeof(*part
))) == NULL
)
1134 adios (NULL
, "out of memory");
1136 pp
= &part
->mp_next
;
1139 admonish (NULL
, "premature end-of-file, missing #end");
1146 adios (NULL
, "unknown directive \"#%s\"", ci
->ci_type
);
1147 return NOTOK
; /* NOT REACHED */
1152 set_id (CT ct
, int top
)
1154 char contentid
[BUFSIZ
];
1156 static time_t clock
= 0;
1157 static char *msgfmt
;
1161 snprintf (contentid
, sizeof(contentid
), "%s\n", message_id (clock
, 1));
1163 msgfmt
= getcpy(contentid
);
1165 snprintf (contentid
, sizeof(contentid
), msgfmt
, top
? 0 : ++partno
);
1166 ct
->c_id
= getcpy (contentid
);
1171 * Fill out, or expand the various contents in the composition
1172 * draft. Read-in any necessary files. Parse and execute any
1173 * commands specified by profile composition strings.
1177 compose_content (CT ct
, int verbose
)
1179 CE ce
= &ct
->c_cefile
;
1181 switch (ct
->c_type
) {
1186 char partnam
[BUFSIZ
];
1187 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1191 snprintf (partnam
, sizeof(partnam
), "%s.", ct
->c_partno
);
1192 pp
= partnam
+ strlen (partnam
);
1197 /* first, we call compose_content on all the subparts */
1198 for (part
= m
->mp_parts
, partnum
= 1; part
; part
= part
->mp_next
, partnum
++) {
1199 CT p
= part
->mp_part
;
1201 sprintf (pp
, "%d", partnum
);
1202 p
->c_partno
= add (partnam
, NULL
);
1203 if (compose_content (p
, verbose
) == NOTOK
)
1208 * If the -rfc934mode switch is given, then check all
1209 * the subparts of a multipart/digest. If they are all
1210 * message/rfc822, then mark this content and all
1211 * subparts with the rfc934 compatibility mode flag.
1213 if (rfc934sw
&& ct
->c_subtype
== MULTI_DIGEST
) {
1216 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1217 CT p
= part
->mp_part
;
1219 if (p
->c_subtype
!= MESSAGE_RFC822
) {
1224 ct
->c_rfc934
= is934
;
1225 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1226 CT p
= part
->mp_part
;
1228 if ((p
->c_rfc934
= is934
))
1234 ct
->c_end
= (partnum
= strlen (prefix
) + 2) + 2;
1238 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
)
1239 ct
->c_end
+= part
->mp_part
->c_end
+ partnum
;
1245 /* Nothing to do for type message */
1249 * Discrete types (text/application/audio/image/video)
1254 int i
, xstdout
, len
, buflen
;
1256 char *vec
[4], buffer
[BUFSIZ
];
1258 CI ci
= &ct
->c_ctinfo
;
1261 if (!(cp
= ci
->ci_magic
))
1262 adios (NULL
, "internal error(5)");
1264 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1265 adios("mhbuildsbr", "unable to create temporary file in %s",
1268 ce
->ce_file
= add (tfile
, NULL
);
1273 /* Get buffer ready to go */
1276 buflen
= sizeof(buffer
);
1279 * Parse composition string into buffer
1281 for ( ; *cp
; cp
++) {
1286 /* insert parameters from directive */
1290 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
1291 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
,
1292 pm
->pm_name
, get_param_value(pm
, '?'));
1302 /* %f, and stdout is not-redirected */
1308 * insert temporary filename where
1309 * content should be written
1311 snprintf (bp
, buflen
, "%s", ce
->ce_file
);
1315 /* insert content subtype */
1316 strncpy (bp
, ci
->ci_subtype
, buflen
);
1320 /* insert character % */
1341 printf ("composing content %s/%s from command\n\t%s\n",
1342 ci
->ci_type
, ci
->ci_subtype
, buffer
);
1344 fflush (stdout
); /* not sure if need for -noverbose */
1351 if ((out
= fopen (ce
->ce_file
, "w")) == NULL
)
1352 adios (ce
->ce_file
, "unable to open for writing");
1354 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
> 5; i
++)
1358 adios ("fork", "unable to fork");
1363 dup2 (fileno (out
), 1);
1364 close (fileno (out
));
1365 execvp ("/bin/sh", vec
);
1366 fprintf (stderr
, "unable to exec ");
1373 if (pidXwait(child_id
, NULL
))
1379 /* Check size of file */
1380 if (listsw
&& ct
->c_end
== 0L) {
1383 if (stat (ce
->ce_file
, &st
) != NOTOK
)
1384 ct
->c_end
= (long) st
.st_size
;
1396 * 1) choose a transfer encoding.
1397 * 2) check for clashes with multipart boundary string.
1398 * 3) for text content, figure out which character set is being used.
1400 * If there is a clash with one of the contents and the multipart boundary,
1401 * this function will exit with NOTOK. This will cause the scanning process
1402 * to be repeated with a different multipart boundary. It is possible
1403 * (although highly unlikely) that this scan will be repeated multiple times.
1407 scan_content (CT ct
, size_t maxunencoded
)
1410 int check8bit
= 0, contains8bit
= 0; /* check if contains 8bit data */
1411 int checknul
= 0, containsnul
= 0; /* check if contains NULs */
1412 int checklinelen
= 0, linelen
= 0; /* check for long lines */
1413 int checkllinelen
= 0; /* check for extra-long lines */
1414 int checkboundary
= 0, boundaryclash
= 0; /* check if clashes with multipart boundary */
1415 int checklinespace
= 0, linespace
= 0; /* check if any line ends with space */
1420 struct text
*t
= NULL
;
1422 CE ce
= &ct
->c_cefile
;
1425 * handle multipart by scanning all subparts
1426 * and then checking their encoding.
1428 if (ct
->c_type
== CT_MULTIPART
) {
1429 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1432 /* initially mark the domain of enclosing multipart as 7bit */
1433 ct
->c_encoding
= CE_7BIT
;
1435 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1436 CT p
= part
->mp_part
;
1438 if (scan_content (p
, maxunencoded
) == NOTOK
) /* choose encoding for subpart */
1441 /* if necessary, enlarge encoding for enclosing multipart */
1442 if (p
->c_encoding
== CE_BINARY
)
1443 ct
->c_encoding
= CE_BINARY
;
1444 if (p
->c_encoding
== CE_8BIT
&& ct
->c_encoding
!= CE_BINARY
)
1445 ct
->c_encoding
= CE_8BIT
;
1452 * Decide what to check while scanning this content. Note that
1453 * for text content we always check for 8bit characters if the
1454 * charset is unspecified, because that controls whether or not the
1455 * character set is us-ascii or retrieved from the locale.
1458 if (ct
->c_type
== CT_TEXT
) {
1459 t
= (struct text
*) ct
->c_ctparams
;
1460 if (t
->tx_charset
== CHARSET_UNSPECIFIED
) {
1466 switch (ct
->c_reqencoding
) {
1477 /* Use the default rules based on content-type */
1478 switch (ct
->c_type
) {
1482 if (ct
->c_subtype
== TEXT_PLAIN
) {
1489 case CT_APPLICATION
:
1501 /* don't check anything for message/external */
1502 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1515 * Don't check anything for these types,
1516 * since we are forcing use of base64, unless
1517 * the content-type was specified by a mhbuild directive.
1528 * Scan the unencoded content
1530 if (check8bit
|| checklinelen
|| checklinespace
|| checkboundary
||
1531 checkllinelen
|| checknul
) {
1532 if ((in
= fopen (ce
->ce_file
, "r")) == NULL
)
1533 adios (ce
->ce_file
, "unable to open for reading");
1534 prefix_len
= strlen (prefix
);
1536 while ((gotlen
= getline(&bufp
, &buflen
, in
)) != -1) {
1538 * Check for 8bit and NUL data.
1540 for (cp
= bufp
; (check8bit
|| checknul
) &&
1541 cp
< bufp
+ gotlen
; cp
++) {
1542 if (!isascii ((unsigned char) *cp
)) {
1544 check8bit
= 0; /* no need to keep checking */
1548 checknul
= 0; /* no need to keep checking */
1553 * Check line length.
1555 if (checklinelen
&& ((size_t)gotlen
> maxunencoded
+ 1)) {
1557 checklinelen
= 0; /* no need to keep checking */
1561 * RFC 5322 specifies that a message cannot contain a line
1562 * greater than 998 characters (excluding the CRLF). If we
1563 * get one of those lines and linelen is NOT set, then abort.
1566 if (checkllinelen
&& !linelen
&&
1567 (gotlen
> MAXLONGLINE
+ 1)) {
1568 adios(NULL
, "Line in content exceeds maximum line limit (%d)",
1573 * Check if line ends with a space.
1575 if (checklinespace
&& (cp
= bufp
+ gotlen
- 2) > bufp
&&
1576 isspace ((unsigned char) *cp
)) {
1578 checklinespace
= 0; /* no need to keep checking */
1582 * Check if content contains a line that clashes
1583 * with our standard boundary for multipart messages.
1585 if (checkboundary
&& bufp
[0] == '-' && bufp
[1] == '-') {
1586 for (cp
= bufp
+ gotlen
- 1; cp
>= bufp
; cp
--)
1587 if (!isspace ((unsigned char) *cp
))
1590 if (!strncmp(bufp
+ 2, prefix
, prefix_len
) &&
1591 isdigit((unsigned char) bufp
[2 + prefix_len
])) {
1593 checkboundary
= 0; /* no need to keep checking */
1602 * If the content is text and didn't specify a character set,
1603 * we need to figure out which one was used.
1605 set_charset (ct
, contains8bit
);
1608 * Decide which transfer encoding to use.
1611 if (ct
->c_reqencoding
!= CE_UNKNOWN
)
1612 ct
->c_encoding
= ct
->c_reqencoding
;
1614 int wants_q_p
= (containsnul
|| linelen
|| linespace
|| checksw
);
1616 switch (ct
->c_type
) {
1619 ct
->c_encoding
= CE_QUOTED
;
1620 else if (contains8bit
)
1621 ct
->c_encoding
= CE_8BIT
;
1623 ct
->c_encoding
= CE_7BIT
;
1627 case CT_APPLICATION
:
1628 /* For application type, use base64, except when postscript */
1629 if (wants_q_p
|| contains8bit
) {
1630 if (ct
->c_subtype
== APPLICATION_POSTSCRIPT
)
1631 ct
->c_encoding
= CE_QUOTED
; /* historical */
1633 ct
->c_encoding
= CE_BASE64
;
1635 ct
->c_encoding
= CE_7BIT
;
1640 ct
->c_encoding
= contains8bit
? CE_8BIT
: CE_7BIT
;
1646 /* For audio, image, and video contents, just use base64 */
1647 ct
->c_encoding
= CE_BASE64
;
1652 return (boundaryclash
? NOTOK
: OK
);
1657 * Scan the content structures, and build header
1658 * fields that will need to be output into the
1663 build_headers (CT ct
, int header_encoding
)
1665 int cc
, mailbody
, extbody
, len
;
1666 char *np
, *vp
, buffer
[BUFSIZ
];
1667 CI ci
= &ct
->c_ctinfo
;
1670 * If message is type multipart, then add the multipart
1671 * boundary to the list of attribute/value pairs.
1673 if (ct
->c_type
== CT_MULTIPART
) {
1674 static int level
= 0; /* store nesting level */
1676 snprintf (buffer
, sizeof(buffer
), "%s%d", prefix
, level
++);
1677 add_param(&ci
->ci_first_pm
, &ci
->ci_last_pm
, "boundary", buffer
, 0);
1681 * Skip the output of Content-Type, parameters, content
1682 * description and disposition, and Content-ID if the
1683 * content is of type "message" and the rfc934 compatibility
1684 * flag is set (which means we are inside multipart/digest
1685 * and the switch -rfc934mode was given).
1687 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_rfc934
)
1691 * output the content type and subtype
1693 np
= add (TYPE_FIELD
, NULL
);
1694 vp
= concat (" ", ci
->ci_type
, "/", ci
->ci_subtype
, NULL
);
1696 /* keep track of length of line */
1697 len
= strlen (TYPE_FIELD
) + strlen (ci
->ci_type
)
1698 + strlen (ci
->ci_subtype
) + 3;
1700 extbody
= ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_EXTERNAL
;
1701 mailbody
= extbody
&& ((struct exbody
*) ct
->c_ctparams
)->eb_body
;
1704 * Append the attribute/value pairs to
1705 * the end of the Content-Type line.
1708 if (ci
->ci_first_pm
) {
1709 char *s
= output_params(len
, ci
->ci_first_pm
, &len
, mailbody
);
1712 adios(NULL
, "Internal error: failed outputting Content-Type "
1720 * Append any RFC-822 comment to the end of
1721 * the Content-Type line.
1723 if (ci
->ci_comment
) {
1724 snprintf (buffer
, sizeof(buffer
), "(%s)", ci
->ci_comment
);
1725 if (len
+ 1 + (cc
= 2 + strlen (ci
->ci_comment
)) >= CPERLIN
) {
1726 vp
= add ("\n\t", vp
);
1732 vp
= add (buffer
, vp
);
1735 vp
= add ("\n", vp
);
1736 add_header (ct
, np
, vp
);
1739 * output the Content-ID, unless disabled by -nocontentid
1741 if (contentidsw
&& ct
->c_id
) {
1742 np
= add (ID_FIELD
, NULL
);
1743 vp
= concat (" ", ct
->c_id
, NULL
);
1744 add_header (ct
, np
, vp
);
1747 * output the Content-Description
1750 np
= add (DESCR_FIELD
, NULL
);
1751 vp
= concat (" ", ct
->c_descr
, NULL
);
1752 if (encode_rfc2047(DESCR_FIELD
, &vp
, header_encoding
, NULL
))
1753 adios(NULL
, "Unable to encode %s header", DESCR_FIELD
);
1754 add_header (ct
, np
, vp
);
1758 * output the Content-Disposition. If it's NULL but c_dispo_type is
1759 * set, then we need to build it.
1762 np
= add (DISPO_FIELD
, NULL
);
1763 vp
= concat (" ", ct
->c_dispo
, NULL
);
1764 add_header (ct
, np
, vp
);
1765 } else if (ct
->c_dispo_type
) {
1766 vp
= concat (" ", ct
->c_dispo_type
, NULL
);
1767 len
= strlen(DISPO_FIELD
) + strlen(vp
) + 1;
1768 np
= output_params(len
, ct
->c_dispo_first
, NULL
, 0);
1773 add_header (ct
, getcpy(DISPO_FIELD
), vp
);
1778 * If this is the internal content structure for a
1779 * "message/external", then we are done with the
1780 * headers (since it has no body).
1786 * output the Content-MD5
1789 np
= add (MD5_FIELD
, NULL
);
1790 vp
= calculate_digest (ct
, (ct
->c_encoding
== CE_QUOTED
) ? 1 : 0);
1791 add_header (ct
, np
, vp
);
1795 * output the Content-Transfer-Encoding
1797 switch (ct
->c_encoding
) {
1799 /* Nothing to output */
1803 np
= add (ENCODING_FIELD
, NULL
);
1804 vp
= concat (" ", "8bit", "\n", NULL
);
1805 add_header (ct
, np
, vp
);
1809 if (ct
->c_type
== CT_MESSAGE
|| ct
->c_type
== CT_MULTIPART
)
1810 adios (NULL
, "internal error, invalid encoding");
1812 np
= add (ENCODING_FIELD
, NULL
);
1813 vp
= concat (" ", "quoted-printable", "\n", NULL
);
1814 add_header (ct
, np
, vp
);
1818 if (ct
->c_type
== CT_MESSAGE
|| ct
->c_type
== CT_MULTIPART
)
1819 adios (NULL
, "internal error, invalid encoding");
1821 np
= add (ENCODING_FIELD
, NULL
);
1822 vp
= concat (" ", "base64", "\n", NULL
);
1823 add_header (ct
, np
, vp
);
1827 if (ct
->c_type
== CT_MESSAGE
)
1828 adios (NULL
, "internal error, invalid encoding");
1830 np
= add (ENCODING_FIELD
, NULL
);
1831 vp
= concat (" ", "binary", "\n", NULL
);
1832 add_header (ct
, np
, vp
);
1836 adios (NULL
, "unknown transfer encoding in content");
1841 * Additional content specific header processing
1843 switch (ct
->c_type
) {
1846 struct multipart
*m
;
1849 m
= (struct multipart
*) ct
->c_ctparams
;
1850 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1854 build_headers (p
, header_encoding
);
1860 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1863 e
= (struct exbody
*) ct
->c_ctparams
;
1864 build_headers (e
->eb_content
, header_encoding
);
1877 static char nib2b64
[0x40+1] =
1878 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1881 calculate_digest (CT ct
, int asciiP
)
1886 unsigned char digest
[16];
1887 unsigned char outbuf
[25];
1889 CE ce
= &ct
->c_cefile
;
1890 char *infilename
= ce
->ce_file
? ce
->ce_file
: ct
->c_file
;
1894 if ((in
= fopen (infilename
, "r")) == NULL
)
1895 adios (infilename
, "unable to open for reading");
1897 /* Initialize md5 context */
1898 MD5Init (&mdContext
);
1900 /* calculate md5 message digest */
1905 while ((gotlen
= getline(&bufp
, &buflen
, in
)) != -1) {
1908 cp
= bufp
+ gotlen
- 1;
1909 if ((c
= *cp
) == '\n')
1912 MD5Update (&mdContext
, (unsigned char *) bufp
,
1913 (unsigned int) gotlen
);
1916 MD5Update (&mdContext
, (unsigned char *) "\r\n", 2);
1919 char buffer
[BUFSIZ
];
1920 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), in
)) > 0)
1921 MD5Update (&mdContext
, (unsigned char *) buffer
, (unsigned int) cc
);
1924 /* md5 finalization. Write digest and zero md5 context */
1925 MD5Final (digest
, &mdContext
);
1930 /* print debugging info */
1934 fprintf (stderr
, "MD5 digest=");
1935 for (ep
= (dp
= digest
) + sizeof(digest
) / sizeof(digest
[0]);
1937 fprintf (stderr
, "%02x", *dp
& 0xff);
1938 fprintf (stderr
, "\n");
1941 /* encode the digest using base64 */
1942 for (dp
= digest
, op
= (char *) outbuf
,
1943 cc
= sizeof(digest
) / sizeof(digest
[0]);
1944 cc
> 0; cc
-= 3, op
+= 4) {
1948 bits
= (*dp
++ & 0xff) << 16;
1950 bits
|= (*dp
++ & 0xff) << 8;
1952 bits
|= *dp
++ & 0xff;
1955 for (bp
= op
+ 4; bp
> op
; bits
>>= 6)
1956 *--bp
= nib2b64
[bits
& 0x3f];
1964 /* null terminate string */
1967 /* now make copy and return string */
1968 vp
= concat (" ", outbuf
, "\n", NULL
);
1973 * Set things up for the content structure for file "filename" that
1978 setup_attach_content(CT ct
, char *filename
)
1980 char *type
, *simplename
= r1bindex(filename
, '/');
1981 struct str2init
*s2i
;
1984 if (! (type
= mime_type(filename
))) {
1985 adios(NULL
, "Unable to determine MIME type of \"%s\"", filename
);
1989 * Parse the Content-Type. get_ctinfo() parses MIME parameters, but
1990 * since we're just feeding it a MIME type we have to add those ourself.
1991 * Map that to a valid content-type label and call any initialization
1995 if (get_ctinfo(type
, ct
, 0) == NOTOK
)
2000 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
2001 if (strcasecmp(ct
->c_ctinfo
.ci_type
, s2i
->si_key
) == 0)
2003 if (!s2i
->si_key
&& !uprf(ct
->c_ctinfo
.ci_type
, "X-"))
2007 * Make sure the type isn't incompatible with what we can handle
2010 switch (ct
->c_type
= s2i
->si_val
) {
2012 adios (NULL
, "multipart types must be specified by mhbuild directives");
2016 if (strcasecmp(ct
->c_ctinfo
.ci_subtype
, "partial") == 0)
2017 adios(NULL
, "Sorry, %s/%s isn't supported", ct
->c_ctinfo
.ci_type
,
2018 ct
->c_ctinfo
.ci_subtype
);
2019 if (strcasecmp(ct
->c_ctinfo
.ci_subtype
, "external-body") == 0)
2020 adios(NULL
, "external-body messages must be specified "
2021 "by mhbuild directives");
2026 * This sets the subtype, if it's significant
2028 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
2029 (*ct
->c_ctinitfnx
)(ct
);
2034 * Feed in a few attributes; specifically, the name attribute, the
2035 * content-description, and the content-disposition.
2038 for (pm
= ct
->c_ctinfo
.ci_first_pm
; pm
; pm
= pm
->pm_next
) {
2039 if (strcasecmp(pm
->pm_name
, "name") == 0) {
2042 pm
->pm_value
= getcpy(simplename
);
2048 add_param(&ct
->c_ctinfo
.ci_first_pm
, &ct
->c_ctinfo
.ci_last_pm
,
2049 "name", simplename
, 0);
2051 ct
->c_descr
= getcpy(simplename
);
2052 ct
->c_descr
= add("\n", ct
->c_descr
);
2053 ct
->c_cefile
.ce_file
= getcpy(filename
);
2055 set_disposition (ct
);
2057 add_param(&ct
->c_dispo_first
, &ct
->c_dispo_last
, "filename", simplename
, 0);
2061 * If disposition type hasn't already been set in ct:
2062 * Look for mhbuild-disposition-<type>/<subtype> entry
2063 * that specifies Content-Disposition type. Only
2064 * 'attachment' and 'inline' are allowed. Default to
2068 set_disposition (CT ct
) {
2069 if (ct
->c_dispo_type
== NULL
) {
2070 char *cp
= context_find_by_type ("disposition", ct
->c_ctinfo
.ci_type
,
2071 ct
->c_ctinfo
.ci_subtype
);
2073 if (cp
&& strcasecmp (cp
, "attachment") &&
2074 strcasecmp (cp
, "inline")) {
2075 admonish (NULL
, "configuration problem: %s-disposition-%s%s%s "
2076 "specifies '%s' but only 'attachment' and 'inline' are "
2077 "allowed", invo_name
,
2078 ct
->c_ctinfo
.ci_type
,
2079 ct
->c_ctinfo
.ci_subtype
? "/" : "",
2080 ct
->c_ctinfo
.ci_subtype
? ct
->c_ctinfo
.ci_subtype
: "",
2084 ct
->c_dispo_type
= cp
? getcpy (cp
) : getcpy ("attachment");
2089 * Set text content charset if it was unspecified. contains8bit
2091 * 0: content does not contain 8-bit characters
2092 * 1: content contains 8-bit characters
2093 * -1: ignore content and use user's locale to determine charset
2096 set_charset (CT ct
, int contains8bit
) {
2097 if (ct
->c_type
== CT_TEXT
) {
2100 if (ct
->c_ctparams
== NULL
) {
2101 if ((t
= ct
->c_ctparams
=
2102 (struct text
*) mh_xcalloc (1, sizeof (struct text
))) ==
2104 adios (NULL
, "out of memory");
2106 t
->tx_charset
= CHARSET_UNSPECIFIED
;
2108 t
= (struct text
*) ct
->c_ctparams
;
2111 if (t
->tx_charset
== CHARSET_UNSPECIFIED
) {
2112 CI ci
= &ct
->c_ctinfo
;
2113 char *eightbitcharset
= write_charset_8bit();
2114 char *charset
= contains8bit
? eightbitcharset
: "us-ascii";
2116 if (contains8bit
== 1 &&
2117 strcasecmp (eightbitcharset
, "US-ASCII") == 0) {
2118 adios (NULL
, "Text content contains 8 bit characters, but "
2119 "character set is US-ASCII");
2122 add_param (&ci
->ci_first_pm
, &ci
->ci_last_pm
, "charset", charset
,
2125 t
->tx_charset
= CHARSET_SPECIFIED
;
2132 * Look at all of the replied-to message parts and expand any that
2133 * are matched by a pseudoheader. Except don't descend into
2137 expand_pseudoheaders (CT ct
, struct multipart
*m
, const char *infile
,
2138 const convert_list
*convert_head
) {
2139 /* text_plain_ct is used to concatenate all of the text/plain
2140 replies into one part, instead of having each one in a separate
2142 CT text_plain_ct
= NULL
;
2144 switch (ct
->c_type
) {
2145 case CT_MULTIPART
: {
2146 struct multipart
*mp
= (struct multipart
*) ct
->c_ctparams
;
2149 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
2152 /* The parts are in descending priority order (defined by
2153 RFC 2046 Sec. 5.1.4) because they were reversed by
2154 parse_mime (). So, stop looking for matches with
2155 immediate subparts after the first match of an
2157 for (part
= mp
->mp_parts
; ! matched
&& part
; part
= part
->mp_next
) {
2158 char *type_subtype
=
2159 concat (part
->mp_part
->c_ctinfo
.ci_type
, "/",
2160 part
->mp_part
->c_ctinfo
.ci_subtype
, NULL
);
2162 if (part
->mp_part
->c_type
== CT_MULTIPART
) {
2163 expand_pseudoheaders (part
->mp_part
, m
, infile
,
2166 const convert_list
*c
;
2168 for (c
= convert_head
; c
; c
= c
->next
) {
2169 if (! strcasecmp (type_subtype
, c
->type
)) {
2170 expand_pseudoheader (part
->mp_part
, &text_plain_ct
,
2172 c
->type
, c
->argstring
);
2178 free (type_subtype
);
2181 for (part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
2182 expand_pseudoheaders (part
->mp_part
, m
, infile
, convert_head
);
2189 char *type_subtype
=
2190 concat (ct
->c_ctinfo
.ci_type
, "/", ct
->c_ctinfo
.ci_subtype
,
2192 const convert_list
*c
;
2194 for (c
= convert_head
; c
; c
= c
->next
) {
2195 if (! strcasecmp (type_subtype
, c
->type
)) {
2196 expand_pseudoheader (ct
, &text_plain_ct
, m
, infile
, c
->type
,
2201 free (type_subtype
);
2209 * Expand a single pseudoheader. It's for the specified type.
2212 expand_pseudoheader (CT ct
, CT
*text_plain_ct
, struct multipart
*m
,
2213 const char *infile
, const char *type
,
2214 const char *argstring
) {
2216 FILE *reply_fp
= NULL
;
2217 char *convert
, *type_p
, *subtype_p
;
2218 char *convert_command
;
2219 char *charset
= NULL
;
2221 struct str2init
*s2i
;
2225 type_p
= getcpy (type
);
2226 if ((subtype_p
= strchr (type_p
, '/'))) {
2227 *subtype_p
++ = '\0';
2228 convert
= context_find_by_type ("convert", type_p
, subtype_p
);
2231 type_p
= concat ("mhbuild-convert-", type
, NULL
);
2232 convert
= context_find (type_p
);
2237 /* No mhbuild-convert- entry in mhn.defaults or profile
2241 /* reply_file is used to pass the output of the convert. */
2242 reply_file
= getcpy (m_mktemp2 (NULL
, invo_name
, NULL
, NULL
));
2244 concat (convert
, " ", argstring
? argstring
: "", " >", reply_file
,
2247 /* Convert here . . . */
2248 ct
->c_storeproc
= getcpy (convert_command
);
2249 ct
->c_umask
= ~m_gmprot ();
2250 if (show_content_aux (ct
, 0, convert_command
, NULL
, NULL
) == NOTOK
) {
2251 admonish (NULL
, "store of %s content failed", type
);
2253 free (convert_command
);
2255 /* Fill out the the new ct, reply_ct. */
2256 reply_ct
= (CT
) mh_xcalloc (1, sizeof *reply_ct
);
2257 init_decoded_content (reply_ct
, infile
);
2259 if (extract_headers (reply_ct
, reply_file
, &reply_fp
) == NOTOK
) {
2262 "failed to extract headers from convert output in %s",
2267 /* This sets reply_ct->c_ctparams, and reply_ct->c_termproc if the
2268 charset can't be handled natively. */
2269 for (s2i
= str2cts
; s2i
->si_key
; s2i
++) {
2270 if (strcasecmp(reply_ct
->c_ctinfo
.ci_type
, s2i
->si_key
) == 0) {
2275 if ((reply_ct
->c_ctinitfnx
= s2i
->si_init
)) {
2276 (*reply_ct
->c_ctinitfnx
)(reply_ct
);
2280 get_param (reply_ct
->c_ctinfo
.ci_first_pm
, "charset", '?', 1))) {
2281 /* The reply Content-Type had the charset. */
2284 set_charset (reply_ct
, -1);
2285 charset
= get_param (reply_ct
->c_ctinfo
.ci_first_pm
, "charset", '?', 1);
2286 if (reply_ct
->c_reqencoding
== CE_UNKNOWN
) {
2287 /* Assume that 8bit is sufficient (for text). */
2288 reply_ct
->c_reqencoding
=
2289 strcasecmp (charset
, "US-ASCII") ? CE_8BIT
: CE_7BIT
;
2293 /* Concatenate text/plain parts. */
2294 if (reply_ct
->c_type
== CT_TEXT
&&
2295 reply_ct
->c_subtype
== TEXT_PLAIN
) {
2296 if (! *text_plain_ct
&& m
->mp_parts
&& m
->mp_parts
->mp_part
&&
2297 m
->mp_parts
->mp_part
->c_type
== CT_TEXT
&&
2298 m
->mp_parts
->mp_part
->c_subtype
== TEXT_PLAIN
) {
2299 *text_plain_ct
= m
->mp_parts
->mp_part
;
2300 /* Make sure that the charset is set in the text/plain
2302 set_charset (*text_plain_ct
, -1);
2303 if ((*text_plain_ct
)->c_reqencoding
== CE_UNKNOWN
) {
2304 /* Assume that 8bit is sufficient (for text). */
2305 (*text_plain_ct
)->c_reqencoding
=
2306 strcasecmp (charset
, "US-ASCII") ? CE_8BIT
: CE_7BIT
;
2310 if (*text_plain_ct
) {
2311 /* Only concatenate if the charsets are identical. */
2312 char *text_plain_ct_charset
=
2313 get_param ((*text_plain_ct
)->c_ctinfo
.ci_first_pm
, "charset",
2316 if (strcasecmp (text_plain_ct_charset
, charset
) == 0) {
2317 /* Append this text/plain reply to the first one.
2318 If there's a problem anywhere along the way,
2319 instead attach it is a separate part. */
2320 int text_plain_reply
=
2321 open ((*text_plain_ct
)->c_cefile
.ce_file
,
2322 O_WRONLY
| O_APPEND
);
2323 int addl_reply
= open (reply_file
, O_RDONLY
);
2325 if (text_plain_reply
!= NOTOK
&& addl_reply
!= NOTOK
) {
2326 /* Insert blank line before each addl part. */
2327 /* It would be nice not to do this for the first one. */
2328 if (write (text_plain_reply
, "\n", 1) == 1) {
2329 /* Copy the text from the new reply and
2330 then free its Content struct. */
2331 cpydata (addl_reply
, text_plain_reply
,
2332 (*text_plain_ct
)->c_cefile
.ce_file
,
2334 if (close (text_plain_reply
) == OK
&&
2335 close (addl_reply
) == OK
) {
2336 if (reply_fp
) { fclose (reply_fp
); }
2338 free_content (reply_ct
);
2345 *text_plain_ct
= reply_ct
;
2349 reply_ct
->c_cefile
.ce_file
= reply_file
;
2350 reply_ct
->c_cefile
.ce_fp
= reply_fp
;
2351 reply_ct
->c_cefile
.ce_unlink
= 1;
2353 /* Attach the new part to the parent mulitpart/mixed, "m". */
2354 part
= (struct part
*) mh_xcalloc (1, sizeof *part
);
2355 part
->mp_part
= reply_ct
;
2359 for (p
= m
->mp_parts
; p
&& p
->mp_next
; p
= p
->mp_next
) { continue; }
2367 /* Extract any Content-Type header from beginning of convert output. */
2369 extract_headers (CT ct
, char *reply_file
, FILE **reply_fp
) {
2370 char *buffer
= NULL
, *cp
, *end_of_header
;
2371 int found_header
= 0;
2372 struct stat statbuf
;
2374 /* Read the convert reply from the file to memory. */
2375 if (stat (reply_file
, &statbuf
) == NOTOK
) {
2376 admonish (reply_file
, "failed to stat");
2377 goto failed_to_extract_ct
;
2380 buffer
= mh_xmalloc (statbuf
.st_size
+ 1);
2382 if ((*reply_fp
= fopen (reply_file
, "r+")) == NULL
||
2383 fread (buffer
, 1, (size_t) statbuf
.st_size
, *reply_fp
) <
2384 (size_t) statbuf
.st_size
) {
2385 admonish (reply_file
, "failed to read");
2386 goto failed_to_extract_ct
;
2388 buffer
[statbuf
.st_size
] = '\0';
2390 /* Look for a header in the convert reply. */
2391 if (strncasecmp (buffer
, TYPE_FIELD
, strlen (TYPE_FIELD
)) == 0 &&
2392 buffer
[strlen (TYPE_FIELD
)] == ':') {
2393 if ((end_of_header
= strstr (buffer
, "\r\n\r\n"))) {
2396 } else if ((end_of_header
= strstr (buffer
, "\n\n"))) {
2408 /* Truncate buffer to just the C-T. */
2409 *end_of_header
= '\0';
2410 n
= strlen (buffer
);
2412 if (get_ctinfo (buffer
+ 14, ct
, 0) != OK
) {
2413 admonish (NULL
, "unable to get content info for reply");
2414 goto failed_to_extract_ct
;
2417 /* Hack. Use parse_mime() to detect the type/subtype of the
2418 reply, which we'll use below. */
2419 tmp_file
= getcpy (m_mktemp2 (NULL
, invo_name
, NULL
, NULL
));
2420 if ((tmp_f
= fopen (tmp_file
, "w")) &&
2421 fwrite (buffer
, 1, n
, tmp_f
) == n
) {
2424 goto failed_to_extract_ct
;
2426 tmp_ct
= parse_mime (tmp_file
);
2429 /* The type and subtype were detected from the reply
2430 using parse_mime() above. */
2431 ct
->c_type
= tmp_ct
->c_type
;
2432 ct
->c_subtype
= tmp_ct
->c_subtype
;
2433 free_content (tmp_ct
);
2438 /* Rewrite the content without the header. */
2439 cp
= end_of_header
+ 1;
2442 if (fwrite (cp
, 1, statbuf
.st_size
- (cp
- buffer
), *reply_fp
) <
2443 (size_t) (statbuf
.st_size
- (cp
- buffer
))) {
2444 admonish (reply_file
, "failed to write");
2445 goto failed_to_extract_ct
;
2448 if (ftruncate (fileno (*reply_fp
), statbuf
.st_size
- (cp
- buffer
)) !=
2450 advise (reply_file
, "ftruncate");
2451 goto failed_to_extract_ct
;
2454 /* No header section, assume the reply is text/plain. */
2455 ct
->c_type
= CT_TEXT
;
2456 ct
->c_subtype
= TEXT_PLAIN
;
2457 if (get_ctinfo ("text/plain", ct
, 0) == NOTOK
) {
2458 /* This never should fail, but just in case. */
2459 adios (NULL
, "unable to get content info for reply");
2463 /* free_encoding() will close reply_fp, which is passed through
2464 ct->c_cefile.ce_fp. */
2468 failed_to_extract_ct
:
2469 if (*reply_fp
) { fclose (*reply_fp
); }