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/check_charset.h"
18 #include "sbr/getcpy.h"
19 #include "sbr/m_convert.h"
20 #include "sbr/getfolder.h"
21 #include "sbr/folder_read.h"
22 #include "sbr/folder_free.h"
23 #include "sbr/context_find.h"
24 #include "sbr/brkstring.h"
25 #include "sbr/pidstatus.h"
27 #include "sbr/error.h"
32 #include "h/fmt_scan.h"
34 #include "h/mhparse.h"
37 #include "h/mhcachesbr.h"
39 #include "sbr/m_mktemp.h"
40 #include "sbr/message_id.h"
41 #include "sbr/mime_type.h"
43 #include "mhshowsbr.h"
45 #ifdef HAVE_SYS_TIME_H
46 # include <sys/time.h>
55 extern bool contentidsw
;
57 static char prefix
[] = "----- =_aaaaaaaaaa";
61 struct attach_list
*next
;
64 typedef struct convert_list
{
68 struct convert_list
*next
;
75 static int init_decoded_content (CT
, const char *);
76 static void setup_attach_content(CT
, char *);
77 static void set_disposition (CT
);
78 static void set_charset (CT
, int);
79 static void expand_pseudoheaders (CT
, struct multipart
*, const char *,
80 const convert_list
*);
81 static void expand_pseudoheader (CT
, CT
*, struct multipart
*, const char *,
82 const char *, const char *);
83 static char *fgetstr (char *, int, FILE *);
84 static int user_content (FILE *, char *, CT
*, const char *infilename
);
85 static void set_id (CT
, int);
86 static int compose_content (CT
, int);
87 static int scan_content (CT
, size_t);
88 static int build_headers (CT
, int);
89 static char *calculate_digest (CT
, int);
90 static int extract_headers (CT
, char *, FILE **);
93 static unsigned char directives_stack
[32];
94 static unsigned int directives_index
;
99 return directives_stack
[directives_index
];
103 directive_onoff(int onoff
)
105 if (directives_index
>= sizeof(directives_stack
) - 1) {
106 fprintf(stderr
, "mhbuild: #on/off overflow, continuing\n");
109 directives_stack
[++directives_index
] = onoff
;
113 directive_init(int onoff
)
115 directives_index
= 0;
116 directives_stack
[0] = onoff
;
122 if (directives_index
> 0)
125 fprintf(stderr
, "mhbuild: #pop underflow, continuing\n");
129 * Main routine for translating composition file
130 * into valid MIME message. It translates the draft
131 * into a content structure (actually a tree of content
132 * structures). This message then can be manipulated
133 * in various ways, including being output via
138 build_mime (char *infile
, int autobuild
, int dist
, int directives
,
139 int header_encoding
, size_t maxunencoded
, int verbose
)
142 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
149 m_getfld_state_t gstate
;
150 struct attach_list
*attach_head
= NULL
, *attach_tail
= NULL
, *at_entry
;
151 convert_list
*convert_head
= NULL
, *convert_tail
= NULL
, *convert
;
153 directive_init(directives
);
155 umask (~m_gmprot ());
157 /* open the composition draft */
158 if ((in
= fopen (infile
, "r")) == NULL
)
159 adios (infile
, "unable to open for reading");
162 * Allocate space for primary (outside) content
167 * Allocate structure for handling decoded content
168 * for this part. We don't really need this, but
169 * allocate it to remain consistent.
171 init_decoded_content (ct
, infile
);
174 * Parse some of the header fields in the composition
175 * draft into the linked list of header fields for
176 * the new MIME message.
178 gstate
= m_getfld_state_init(in
);
179 m_getfld_track_filepos2(&gstate
);
180 for (compnum
= 1;;) {
181 int bufsz
= sizeof buf
;
182 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
187 /* abort if draft has Mime-Version or C-T-E header field */
188 if (strcasecmp (name
, VRSN_FIELD
) == 0 ||
189 strcasecmp (name
, ENCODING_FIELD
) == 0) {
195 die("draft shouldn't contain %s: field", name
);
198 /* ignore any Content-Type fields in the header */
199 if (!strcasecmp (name
, TYPE_FIELD
)) {
200 while (state
== FLDPLUS
) {
202 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
207 /* get copies of the buffers */
208 np
= mh_xstrdup(name
);
209 vp
= mh_xstrdup(buf
);
211 /* if necessary, get rest of field */
212 while (state
== FLDPLUS
) {
214 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
215 vp
= add (buf
, vp
); /* add to previous value */
219 * Now add the header data to the list, unless it's an attach
220 * header; in that case, add it to our attach list
223 if (strcasecmp(ATTACH_FIELD
, np
) == 0 ||
224 strcasecmp(ATTACH_FIELD_ALT
, np
) == 0) {
225 struct attach_list
*entry
;
226 char *s
= vp
, *e
= vp
+ strlen(vp
) - 1;
230 * Make sure we can find the start of this filename.
231 * If it's blank, we skip completely. Otherwise, strip
232 * off any leading spaces and trailing newlines.
235 while (isspace((unsigned char) *s
))
238 while (e
> s
&& *e
== '\n')
247 entry
->filename
= mh_xstrdup(s
);
252 attach_tail
->next
= entry
;
255 attach_head
= attach_tail
= entry
;
257 } else if (strncasecmp(MHBUILD_FILE_PSEUDOHEADER
, np
,
258 LEN(MHBUILD_FILE_PSEUDOHEADER
)) == 0) {
260 * Nmh-mhbuild-file-text/calendar: /home/user/Mail/inbox/9
262 char *type
= np
+ LEN(MHBUILD_FILE_PSEUDOHEADER
);
265 /* vp should begin with a space because m_getfld2()
266 includes the space after the colon in buf. */
267 while (isspace((unsigned char) *filename
)) { ++filename
; }
268 /* Trim trailing newline and any other whitespace. */
271 for (convert
= convert_head
; convert
; convert
= convert
->next
) {
272 if (strcasecmp (convert
->type
, type
) == 0) { break; }
275 if (convert
->filename
&&
276 strcasecmp (convert
->filename
, filename
)) {
277 die("Multiple %s headers with different files"
278 " not allowed", type
);
280 convert
->filename
= mh_xstrdup(filename
);
284 convert
->filename
= mh_xstrdup(filename
);
285 convert
->type
= mh_xstrdup(type
);
288 convert_tail
->next
= convert
;
290 convert_head
= convert
;
292 convert_tail
= convert
;
297 } else if (strncasecmp(MHBUILD_ARGS_PSEUDOHEADER
, np
,
298 LEN(MHBUILD_ARGS_PSEUDOHEADER
)) == 0) {
300 * Nmh-mhbuild-args-text/calendar: -reply accept
302 char *type
= np
+ LEN(MHBUILD_ARGS_PSEUDOHEADER
);
303 char *argstring
= vp
;
305 /* vp should begin with a space because m_getfld2()
306 includes the space after the colon in buf. */
307 while (isspace((unsigned char) *argstring
)) { ++argstring
; }
308 /* Trim trailing newline and any other whitespace. */
311 for (convert
= convert_head
; convert
; convert
= convert
->next
) {
312 if (strcasecmp (convert
->type
, type
) == 0) { break; }
315 if (convert
->argstring
&&
316 strcasecmp (convert
->argstring
, argstring
)) {
317 die("Multiple %s headers with different "
318 "argstrings not allowed", type
);
320 convert
->argstring
= mh_xstrdup(argstring
);
324 convert
->type
= mh_xstrdup(type
);
325 convert
->argstring
= mh_xstrdup(argstring
);
328 convert_tail
->next
= convert
;
330 convert_head
= convert
;
332 convert_tail
= convert
;
338 add_header (ct
, np
, vp
);
342 /* if this wasn't the last header field, then continue */
346 fseek (in
, (long) (-strlen (buf
)), SEEK_CUR
);
353 die("message format error in component #%d", compnum
);
356 die("getfld() returned %d", state
);
360 m_getfld_state_destroy (&gstate
);
362 if (header_encoding
!= CE_8BIT
) {
364 * Iterate through the list of headers and call the function to MIME-ify
368 for (hp
= ct
->c_first_hf
; hp
!= NULL
; hp
= hp
->next
) {
369 if (encode_rfc2047(hp
->name
, &hp
->value
, header_encoding
, NULL
)) {
370 die("Unable to encode header \"%s\"", hp
->name
);
376 * Now add the MIME-Version header field
377 * to the list of header fields.
381 np
= mh_xstrdup(VRSN_FIELD
);
382 vp
= concat (" ", VRSN_VALUE
, "\n", NULL
);
383 add_header (ct
, np
, vp
);
387 * We initially 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
;
397 ct
->c_ctparams
= (void *) m
;
401 * read and parse the composition file
402 * and the directives it contains.
404 while (fgetstr (buf
, sizeof(buf
) - 1, in
)) {
408 if (user_content (in
, buf
, &p
, infile
) == DONE
) {
409 inform("ignoring spurious #end, continuing...");
422 * Add any Attach headers to the list of MIME parts at the end of the
426 for (at_entry
= attach_head
; at_entry
; ) {
427 struct attach_list
*at_prev
= at_entry
;
431 if (access(at_entry
->filename
, R_OK
) != 0) {
432 adios("reading", "Unable to open %s for", at_entry
->filename
);
436 init_decoded_content(p
, infile
);
439 * Initialize our content structure based on the filename,
440 * and fill in all of the relevant fields. Also place MIME
441 * parameters in the attributes array.
444 setup_attach_content(p
, at_entry
->filename
);
451 at_entry
= at_entry
->next
;
452 free(at_prev
->filename
);
457 * Handle the mhbuild pseudoheaders, which deal with specific
464 set_done(freects_done
);
466 /* In case there are multiple calls that land here, prevent leak. */
467 for (ctp
= cts
; ctp
&& *ctp
; ++ctp
) { free_content (*ctp
); }
470 /* Extract the type part (as a CT) from filename. */
471 cts
= mh_xcalloc(2, sizeof *cts
);
472 if (! (cts
[0] = parse_mime (convert_head
->filename
))) {
473 die("failed to parse %s", convert_head
->filename
);
476 expand_pseudoheaders (cts
[0], m
, infile
, convert_head
);
478 /* Free the convert list. */
479 for (convert
= convert_head
; convert
; convert
= next
) {
480 next
= convert
->next
;
481 free (convert
->type
);
482 free (convert
->filename
);
483 free (convert
->argstring
);
490 * To allow for empty message bodies, if we've found NO content at all
491 * yet cook up an empty text/plain part.
500 init_decoded_content(p
, infile
);
502 if (get_ctinfo ("text/plain", p
, 0) == NOTOK
)
506 p
->c_subtype
= TEXT_PLAIN
;
507 p
->c_encoding
= CE_7BIT
;
509 * Sigh. ce_file contains the "decoded" contents of this part.
510 * So this seems like the best option available since we're going
511 * to call scan_content() on this.
513 p
->c_cefile
.ce_file
= mh_xstrdup("/dev/null");
514 p
->c_begin
= ftell(in
);
515 p
->c_end
= ftell(in
);
518 t
->tx_charset
= CHARSET_SPECIFIED
;
527 * close the composition draft since
528 * it's not needed any longer.
533 * If only one content was found, then remove and
534 * free the outer multipart content.
536 if (!m
->mp_parts
->mp_next
) {
539 p
= m
->mp_parts
->mp_part
;
540 m
->mp_parts
->mp_part
= NULL
;
542 /* move header fields */
543 p
->c_first_hf
= ct
->c_first_hf
;
544 p
->c_last_hf
= ct
->c_last_hf
;
545 ct
->c_first_hf
= NULL
;
546 ct
->c_last_hf
= NULL
;
555 * Fill out, or expand directives. Parse and execute
556 * commands specified by profile composition strings.
558 compose_content (ct
, verbose
);
560 if ((cp
= strchr(prefix
, 'a')) == NULL
)
561 die("internal error(4)");
564 * If using EAI, force 8-bit charset.
566 if (header_encoding
== CE_8BIT
) {
571 * Scan the contents. Choose a transfer encoding, and
572 * check if prefix for multipart boundary clashes with
573 * any of the contents.
575 while (scan_content (ct
, maxunencoded
) == NOTOK
) {
580 die("giving up trying to find a unique delimiter string");
585 /* Build the rest of the header field structures */
587 build_headers (ct
, header_encoding
);
594 * Set up structures for placing unencoded
595 * content when building parts.
599 init_decoded_content (CT ct
, const char *filename
)
601 ct
->c_ceopenfnx
= open7Bit
; /* since unencoded */
602 ct
->c_ceclosefnx
= close_encoding
;
603 ct
->c_cesizefnx
= NULL
; /* since unencoded */
604 ct
->c_encoding
= CE_7BIT
; /* Seems like a reasonable default */
605 ct
->c_file
= mh_xstrdup(FENDNULL(filename
));
612 fgetstr (char *s
, int n
, FILE *stream
)
618 for (cp
= s
; cp
< ep
;) {
621 if (!fgets (cp
, n
, stream
))
622 return cp
== s
? NULL
: s
; /* "\\\nEOF" ignored. */
624 if (! do_direct() || (cp
== s
&& *cp
!= '#'))
625 return s
; /* Plaintext line. */
629 break; /* Can't contain "\\\n". */
630 cp
+= len
- 1; /* Just before NUL. */
631 if (*cp
-- != '\n' || *cp
!= '\\')
633 *cp
= '\0'; /* Erase the trailing "\\\n". */
637 if (strcmp(s
, "#on\n") == 0) {
639 } else if (strcmp(s
, "#off\n") == 0) {
641 } else if (strcmp(s
, "#pop\n") == 0) {
651 * Parse the composition draft for text and directives.
652 * Do initial setup of Content structure.
656 user_content (FILE *in
, char *buf
, CT
*ctp
, const char *infilename
)
660 char buffer
[NMH_BUFSIZ
];
664 struct str2init
*s2i
;
669 if (buf
[0] == '\n' || (do_direct() && strcmp (buf
, "#\n") == 0)) {
674 /* allocate basic Content structure */
678 /* allocate basic structure for handling decoded content */
679 init_decoded_content (ct
, infilename
);
686 * Handle inline text. Check if line
687 * is one of the following forms:
689 * 1) doesn't begin with '#' (implicit directive)
690 * 2) begins with "##" (implicit directive)
691 * 3) begins with "#<"
693 if (!do_direct() || buf
[0] != '#' || buf
[1] == '#' || buf
[1] == '<') {
697 char content
[BUFSIZ
];
701 if ((cp
= m_mktemp2(NULL
, invo_name
, NULL
, &out
)) == NULL
) {
702 adios("mhbuildsbr", "unable to create temporary file in %s",
706 /* use a temp file to collect the plain text lines */
707 ce
->ce_file
= mh_xstrdup(cp
);
710 if (do_direct() && (buf
[0] == '#' && buf
[1] == '<')) {
711 strncpy (content
, buf
+ 2, sizeof(content
));
717 /* the directive is implicit */
718 strncpy (content
, "text/plain", sizeof(content
));
720 strncpy (buffer
, (!do_direct() || buf
[0] != '#') ? buf
: buf
+ 1, sizeof(buffer
));
724 if (headers
>= 0 && do_direct() && uprf (buffer
, DESCR_FIELD
)
725 && buffer
[i
= LEN(DESCR_FIELD
)] == ':') {
729 ct
->c_descr
= add (buffer
+ i
+ 1, ct
->c_descr
);
730 if (!fgetstr (buffer
, sizeof(buffer
) - 1, in
))
731 die("end-of-file after %s: field in plaintext", DESCR_FIELD
);
739 die("#-directive after %s: field in plaintext", DESCR_FIELD
);
747 if (headers
>= 0 && do_direct() && uprf (buffer
, DISPO_FIELD
)
748 && buffer
[i
= LEN(DISPO_FIELD
)] == ':') {
752 ct
->c_dispo
= add (buffer
+ i
+ 1, ct
->c_dispo
);
753 if (!fgetstr (buffer
, sizeof(buffer
) - 1, in
))
754 die("end-of-file after %s: field in plaintext", DISPO_FIELD
);
762 die("#-directive after %s: field in plaintext", DISPO_FIELD
);
770 if (headers
!= 1 || buffer
[0] != '\n')
776 if ((cp
= fgetstr (buffer
, sizeof(buffer
) - 1, in
)) == NULL
)
778 if (do_direct() && buffer
[0] == '#') {
781 if (buffer
[1] != '#')
783 for (cp
= (bp
= buffer
) + 1; *cp
; cp
++)
790 ct
->c_end
= ftell (out
);
793 /* parse content type */
794 if (get_ctinfo (content
, ct
, inlineD
) == NOTOK
)
797 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
798 if (!strcasecmp (ci
->ci_type
, s2i
->si_key
))
800 if (!s2i
->si_key
&& !uprf (ci
->ci_type
, "X-"))
804 * check type specified (possibly implicitly)
806 switch (ct
->c_type
= s2i
->si_val
) {
808 if (!strcasecmp (ci
->ci_subtype
, "rfc822")) {
809 ct
->c_encoding
= CE_7BIT
;
814 die("it doesn't make sense to define an in-line %s content",
815 ct
->c_type
== CT_MESSAGE
? "message" : "multipart");
820 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
821 (*ct
->c_ctinitfnx
) (ct
);
826 fseek (in
, pos
, SEEK_SET
);
831 * If we've reached this point, the next line
832 * must be some type of explicit directive.
835 /* check if directive is external-type */
836 extrnal
= (buf
[1] == '@');
838 /* parse directive */
839 if (get_ctinfo (buf
+ (extrnal
? 2 : 1), ct
, 1) == NOTOK
)
842 /* check directive against the list of MIME types */
843 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
844 if (!strcasecmp (ci
->ci_type
, s2i
->si_key
))
848 * Check if the directive specified a valid type.
849 * This will happen if it was one of the following forms:
856 die("missing subtype in \"#%s\"", ci
->ci_type
);
858 switch (ct
->c_type
= s2i
->si_val
) {
860 die("use \"#begin ... #end\" instead of \"#%s/%s\"",
861 ci
->ci_type
, ci
->ci_subtype
);
865 if (!strcasecmp (ci
->ci_subtype
, "partial"))
866 die("sorry, \"#%s/%s\" isn't supported",
867 ci
->ci_type
, ci
->ci_subtype
);
868 if (!strcasecmp (ci
->ci_subtype
, "external-body"))
869 die("use \"#@type/subtype ... [] ...\" instead of \"#%s/%s\"",
870 ci
->ci_type
, ci
->ci_subtype
);
872 die( "use \"#forw [+folder] [msgs]\" instead of \"#%s/%s\"",
873 ci
->ci_type
, ci
->ci_subtype
);
877 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
878 (*ct
->c_ctinitfnx
) (ct
);
883 * #@type/subtype (external types directive)
890 die("need external information for \"#@%s/%s\"",
891 ci
->ci_type
, ci
->ci_subtype
);
894 snprintf (buffer
, sizeof(buffer
), "message/external-body; %s", ci
->ci_magic
);
899 * Since we are using the current Content structure to
900 * hold information about the type of the external
901 * reference, we need to create another Content structure
902 * for the message/external-body to wrap it in.
905 init_decoded_content(ct
, infilename
);
907 if (get_ctinfo (buffer
, ct
, 0) == NOTOK
)
909 ct
->c_type
= CT_MESSAGE
;
910 ct
->c_subtype
= MESSAGE_EXTERNAL
;
913 ct
->c_ctparams
= (void *) e
;
919 if (params_external (ct
, 1) == NOTOK
)
925 /* Handle [file] argument */
927 /* check if specifies command to execute */
928 if (*ci
->ci_magic
== '|' || *ci
->ci_magic
== '!') {
929 for (cp
= ci
->ci_magic
+ 1; isspace ((unsigned char) *cp
); cp
++)
932 die("empty pipe command for #%s directive", ci
->ci_type
);
937 /* record filename of decoded contents */
938 ce
->ce_file
= ci
->ci_magic
;
939 if (access (ce
->ce_file
, R_OK
) == NOTOK
)
940 adios ("reading", "unable to access %s for", ce
->ce_file
);
941 if (listsw
&& stat (ce
->ce_file
, &st
) != NOTOK
)
942 ct
->c_end
= (long) st
.st_size
;
949 * No [file] argument, so check profile for
950 * method to compose content.
952 cp
= context_find_by_type ("compose", ci
->ci_type
, ci
->ci_subtype
);
954 content_error (NULL
, ct
, "don't know how to compose content");
957 ci
->ci_magic
= mh_xstrdup(cp
);
962 die("external definition not allowed for \"#%s\"", ci
->ci_type
);
966 * #forw [+folder] [msgs]
968 if (!strcasecmp (ci
->ci_type
, "forw")) {
970 char *folder
, *arguments
[MAXARGS
];
974 ap
= brkstring (ci
->ci_magic
, " ", "\n");
975 copyip (ap
, arguments
, MAXARGS
);
977 arguments
[0] = "cur";
982 /* search the arguments for a folder name */
983 for (ap
= arguments
; *ap
; ap
++) {
985 if (*cp
== '+' || *cp
== '@') {
987 die("only one folder per #forw directive");
988 folder
= pluspath (cp
);
992 /* else, use the current folder */
994 folder
= mh_xstrdup(getfolder(1));
996 if (!(mp
= folder_read (folder
, 0)))
997 die("unable to read folder %s", folder
);
998 for (ap
= arguments
; *ap
; ap
++) {
1000 if (*cp
!= '+' && *cp
!= '@')
1001 if (!m_convert (mp
, cp
))
1008 * If there is more than one message to include, make this
1009 * a content of type "multipart/digest" and insert each message
1010 * as a subpart. If there is only one message, then make this
1011 * a content of type "message/rfc822".
1013 if (mp
->numsel
> 1) {
1014 /* we are forwarding multiple messages */
1015 if (get_ctinfo ("multipart/digest", ct
, 0) == NOTOK
)
1017 ct
->c_type
= CT_MULTIPART
;
1018 ct
->c_subtype
= MULTI_DIGEST
;
1021 ct
->c_ctparams
= (void *) m
;
1024 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
1025 if (is_selected(mp
, msgnum
)) {
1031 init_decoded_content (p
, infilename
);
1033 if (get_ctinfo ("message/rfc822", p
, 0) == NOTOK
)
1035 p
->c_type
= CT_MESSAGE
;
1036 p
->c_subtype
= MESSAGE_RFC822
;
1038 snprintf (buffer
, sizeof(buffer
), "%s/%d", mp
->foldpath
, msgnum
);
1039 pe
->ce_file
= mh_xstrdup(buffer
);
1040 if (listsw
&& stat (pe
->ce_file
, &st
) != NOTOK
)
1041 p
->c_end
= (long) st
.st_size
;
1045 pp
= &part
->mp_next
;
1050 /* we are forwarding one message */
1051 if (get_ctinfo ("message/rfc822", ct
, 0) == NOTOK
)
1053 ct
->c_type
= CT_MESSAGE
;
1054 ct
->c_subtype
= MESSAGE_RFC822
;
1056 msgnum
= mp
->lowsel
;
1057 snprintf (buffer
, sizeof(buffer
), "%s/%d", mp
->foldpath
, msgnum
);
1058 ce
->ce_file
= mh_xstrdup(buffer
);
1059 if (listsw
&& stat (ce
->ce_file
, &st
) != NOTOK
)
1060 ct
->c_end
= (long) st
.st_size
;
1063 folder_free (mp
); /* free folder/message structure */
1070 if (!strcasecmp (ci
->ci_type
, "end")) {
1077 * #begin [ alternative | parallel ]
1079 if (!strcasecmp (ci
->ci_type
, "begin")) {
1080 if (!ci
->ci_magic
) {
1082 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
1083 } else if (!strcasecmp (ci
->ci_magic
, "alternative")) {
1084 vrsn
= MULTI_ALTERNATE
;
1085 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
1086 } else if (!strcasecmp (ci
->ci_magic
, "parallel")) {
1087 vrsn
= MULTI_PARALLEL
;
1088 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
1089 } else if (uprf (ci
->ci_magic
, "digest")) {
1092 vrsn
= MULTI_UNKNOWN
;
1097 snprintf (buffer
, sizeof(buffer
), "multipart/%s", cp
);
1098 if (get_ctinfo (buffer
, ct
, 0) == NOTOK
)
1100 ct
->c_type
= CT_MULTIPART
;
1101 ct
->c_subtype
= vrsn
;
1104 ct
->c_ctparams
= (void *) m
;
1107 while (fgetstr (buffer
, sizeof(buffer
) - 1, in
)) {
1111 if (user_content (in
, buffer
, &p
, infilename
) == DONE
) {
1113 die("empty \"#begin ... #end\" sequence");
1121 pp
= &part
->mp_next
;
1124 inform("premature end-of-file, missing #end, continuing...");
1131 die("unknown directive \"#%s\"", ci
->ci_type
);
1132 return NOTOK
; /* NOT REACHED */
1137 set_id (CT ct
, int top
)
1139 char contentid
[BUFSIZ
];
1141 static time_t clock
= 0;
1142 static char *msgfmt
;
1146 snprintf (contentid
, sizeof(contentid
), "%s\n", message_id (clock
, 1));
1148 msgfmt
= mh_xstrdup(contentid
);
1150 snprintf (contentid
, sizeof(contentid
), msgfmt
, top
? 0 : ++partno
);
1151 ct
->c_id
= mh_xstrdup(contentid
);
1156 * Fill out, or expand the various contents in the composition
1157 * draft. Read-in any necessary files. Parse and execute any
1158 * commands specified by profile composition strings.
1162 compose_content (CT ct
, int verbose
)
1164 CE ce
= &ct
->c_cefile
;
1166 switch (ct
->c_type
) {
1171 char partnam
[BUFSIZ
];
1172 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1176 snprintf (partnam
, sizeof(partnam
), "%s.", ct
->c_partno
);
1177 pp
= partnam
+ strlen (partnam
);
1182 /* first, we call compose_content on all the subparts */
1183 for (part
= m
->mp_parts
, partnum
= 1; part
; part
= part
->mp_next
, partnum
++) {
1184 CT p
= part
->mp_part
;
1186 sprintf (pp
, "%d", partnum
);
1187 p
->c_partno
= mh_xstrdup(partnam
);
1188 if (compose_content (p
, verbose
) == NOTOK
)
1193 * If the -rfc934mode switch is given, then check all
1194 * the subparts of a multipart/digest. If they are all
1195 * message/rfc822, then mark this content and all
1196 * subparts with the rfc934 compatibility mode flag.
1198 if (rfc934sw
&& ct
->c_subtype
== MULTI_DIGEST
) {
1201 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1202 CT p
= part
->mp_part
;
1204 if (p
->c_subtype
!= MESSAGE_RFC822
) {
1209 ct
->c_rfc934
= is934
;
1210 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1211 CT p
= part
->mp_part
;
1213 if ((p
->c_rfc934
= is934
))
1219 ct
->c_end
= (partnum
= strlen (prefix
) + 2) + 2;
1223 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
)
1224 ct
->c_end
+= part
->mp_part
->c_end
+ partnum
;
1230 /* Nothing to do for type message */
1234 * Discrete types (text/application/audio/image/video)
1242 char *vec
[4], buffer
[BUFSIZ
];
1244 CI ci
= &ct
->c_ctinfo
;
1247 if (!(cp
= ci
->ci_magic
))
1248 die("internal error(5)");
1250 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1251 adios("mhbuildsbr", "unable to create temporary file in %s",
1254 ce
->ce_file
= mh_xstrdup(tfile
);
1259 /* Get buffer ready to go */
1262 buflen
= sizeof(buffer
);
1265 * Parse composition string into buffer
1267 for ( ; *cp
; cp
++) {
1272 /* insert parameters from directive */
1276 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
1277 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
,
1278 pm
->pm_name
, get_param_value(pm
, '?'));
1288 /* %f, and stdout is not-redirected */
1294 * insert temporary filename where
1295 * content should be written
1297 snprintf (bp
, buflen
, "%s", ce
->ce_file
);
1301 /* insert content subtype */
1302 strncpy (bp
, ci
->ci_subtype
, buflen
);
1306 /* insert character % */
1327 printf ("composing content %s/%s from command\n\t%s\n",
1328 ci
->ci_type
, ci
->ci_subtype
, buffer
);
1330 fflush (stdout
); /* not sure if need for -noverbose */
1337 if ((out
= fopen (ce
->ce_file
, "w")) == NULL
)
1338 adios (ce
->ce_file
, "unable to open for writing");
1343 adios ("fork", "unable to fork");
1348 dup2 (fileno (out
), 1);
1349 close (fileno (out
));
1350 execvp ("/bin/sh", vec
);
1351 fprintf (stderr
, "unable to exec ");
1358 if (pidXwait(child_id
, NULL
))
1364 /* Check size of file */
1365 if (listsw
&& ct
->c_end
== 0L) {
1368 if (stat (ce
->ce_file
, &st
) != NOTOK
)
1369 ct
->c_end
= (long) st
.st_size
;
1381 * 1) choose a transfer encoding.
1382 * 2) check for clashes with multipart boundary string.
1383 * 3) for text content, figure out which character set is being used.
1385 * If there is a clash with one of the contents and the multipart boundary,
1386 * this function will exit with NOTOK. This will cause the scanning process
1387 * to be repeated with a different multipart boundary. It is possible
1388 * (although highly unlikely) that this scan will be repeated multiple times.
1392 scan_content (CT ct
, size_t maxunencoded
)
1395 bool check8bit
= false, contains8bit
= false; /* check if contains 8bit data */
1396 bool checknul
= false, containsnul
= false; /* check if contains NULs */
1397 bool checklinelen
= false, linelen
= false; /* check for long lines */
1398 bool checkllinelen
= false; /* check for extra-long lines */
1399 bool checkboundary
= false, boundaryclash
= false; /* check if clashes with multipart boundary */
1400 bool checklinespace
= false, linespace
= false; /* check if any line ends with space */
1405 struct text
*t
= NULL
;
1407 CE ce
= &ct
->c_cefile
;
1410 * handle multipart by scanning all subparts
1411 * and then checking their encoding.
1413 if (ct
->c_type
== CT_MULTIPART
) {
1414 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1417 /* initially mark the domain of enclosing multipart as 7bit */
1418 ct
->c_encoding
= CE_7BIT
;
1420 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1421 CT p
= part
->mp_part
;
1423 if (scan_content (p
, maxunencoded
) == NOTOK
) /* choose encoding for subpart */
1426 /* if necessary, enlarge encoding for enclosing multipart */
1427 if (p
->c_encoding
== CE_BINARY
)
1428 ct
->c_encoding
= CE_BINARY
;
1429 if (p
->c_encoding
== CE_8BIT
&& ct
->c_encoding
!= CE_BINARY
)
1430 ct
->c_encoding
= CE_8BIT
;
1437 * Decide what to check while scanning this content. Note that
1438 * for text content we always check for 8bit characters if the
1439 * charset is unspecified, because that controls whether or not the
1440 * character set is us-ascii or retrieved from the locale. And
1441 * we check even if the charset is specified, to allow setting
1442 * the proper Content-Transfer-Encoding.
1445 if (ct
->c_type
== CT_TEXT
) {
1446 t
= (struct text
*) ct
->c_ctparams
;
1447 if (t
->tx_charset
== CHARSET_UNSPECIFIED
) {
1453 switch (ct
->c_reqencoding
) {
1455 checkllinelen
= true;
1456 checkboundary
= true;
1459 checkboundary
= true;
1464 /* Use the default rules based on content-type */
1465 switch (ct
->c_type
) {
1467 checkboundary
= true;
1468 checklinelen
= true;
1469 if (ct
->c_subtype
== TEXT_PLAIN
) {
1470 checklinespace
= false;
1472 checklinespace
= true;
1476 case CT_APPLICATION
:
1479 checklinelen
= true;
1480 checklinespace
= true;
1481 checkboundary
= true;
1485 checklinelen
= false;
1486 checklinespace
= false;
1488 /* don't check anything for message/external */
1489 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1490 checkboundary
= false;
1493 checkboundary
= true;
1502 * Don't check anything for these types,
1503 * since we are forcing use of base64, unless
1504 * the content-type was specified by a mhbuild directive.
1507 checklinelen
= false;
1508 checklinespace
= false;
1509 checkboundary
= false;
1515 * Scan the unencoded content
1517 if (check8bit
|| checklinelen
|| checklinespace
|| checkboundary
||
1518 checkllinelen
|| checknul
) {
1519 if ((in
= fopen (ce
->ce_file
, "r")) == NULL
)
1520 adios (ce
->ce_file
, "unable to open for reading");
1521 prefix_len
= strlen (prefix
);
1523 while ((gotlen
= getline(&bufp
, &buflen
, in
)) != -1) {
1525 * Check for 8bit and NUL data.
1527 for (cp
= bufp
; (check8bit
|| checknul
) &&
1528 cp
< bufp
+ gotlen
; cp
++) {
1529 if (!isascii ((unsigned char) *cp
)) {
1530 contains8bit
= true;
1531 check8bit
= false; /* no need to keep checking */
1535 checknul
= false; /* no need to keep checking */
1540 * Check line length.
1542 if (checklinelen
&& ((size_t)gotlen
> maxunencoded
+ 1)) {
1544 checklinelen
= false; /* no need to keep checking */
1548 * RFC 5322 specifies that a message cannot contain a line
1549 * greater than 998 characters (excluding the CRLF). If we
1550 * get one of those lines and linelen is NOT set, then abort.
1553 if (checkllinelen
&& !linelen
&&
1554 (gotlen
> MAXLONGLINE
+ 1)) {
1555 die("Line in content exceeds maximum line limit (%d)",
1560 * Check if line ends with a space.
1562 if (checklinespace
&& (cp
= bufp
+ gotlen
- 2) > bufp
&&
1563 isspace ((unsigned char) *cp
)) {
1565 checklinespace
= false; /* no need to keep checking */
1569 * Check if content contains a line that clashes
1570 * with our standard boundary for multipart messages.
1572 if (checkboundary
&& bufp
[0] == '-' && bufp
[1] == '-') {
1573 for (cp
= bufp
+ gotlen
- 1; cp
>= bufp
; cp
--)
1574 if (!isspace ((unsigned char) *cp
))
1577 if (!strncmp(bufp
+ 2, prefix
, prefix_len
) &&
1578 isdigit((unsigned char) bufp
[2 + prefix_len
])) {
1579 boundaryclash
= true;
1580 checkboundary
= false; /* no need to keep checking */
1589 * If the content is text and didn't specify a character set,
1590 * we need to figure out which one was used.
1592 set_charset (ct
, contains8bit
);
1595 * Decide which transfer encoding to use.
1598 if (ct
->c_reqencoding
!= CE_UNKNOWN
)
1599 ct
->c_encoding
= ct
->c_reqencoding
;
1601 int wants_q_p
= (containsnul
|| linelen
|| linespace
|| checksw
);
1603 switch (ct
->c_type
) {
1606 ct
->c_encoding
= CE_QUOTED
;
1607 else if (contains8bit
)
1608 ct
->c_encoding
= CE_8BIT
;
1610 ct
->c_encoding
= CE_7BIT
;
1614 case CT_APPLICATION
:
1615 /* For application type, use base64, except when postscript */
1616 if (wants_q_p
|| contains8bit
) {
1617 if (ct
->c_subtype
== APPLICATION_POSTSCRIPT
)
1618 ct
->c_encoding
= CE_QUOTED
; /* historical */
1620 ct
->c_encoding
= CE_BASE64
;
1622 ct
->c_encoding
= CE_7BIT
;
1627 ct
->c_encoding
= contains8bit
? CE_8BIT
: CE_7BIT
;
1633 /* For audio, image, and video contents, just use base64 */
1634 ct
->c_encoding
= CE_BASE64
;
1639 return boundaryclash
? NOTOK
: OK
;
1644 * Scan the content structures, and build header
1645 * fields that will need to be output into the
1650 build_headers (CT ct
, int header_encoding
)
1652 int cc
, mailbody
, extbody
, len
;
1653 char *np
, *vp
, buffer
[BUFSIZ
];
1654 CI ci
= &ct
->c_ctinfo
;
1657 * If message is type multipart, then add the multipart
1658 * boundary to the list of attribute/value pairs.
1660 if (ct
->c_type
== CT_MULTIPART
) {
1661 static int level
= 0; /* store nesting level */
1663 snprintf (buffer
, sizeof(buffer
), "%s%d", prefix
, level
++);
1664 add_param(&ci
->ci_first_pm
, &ci
->ci_last_pm
, "boundary", buffer
, 0);
1668 * Skip the output of Content-Type, parameters, content
1669 * description and disposition, and Content-ID if the
1670 * content is of type "message" and the rfc934 compatibility
1671 * flag is set (which means we are inside multipart/digest
1672 * and the switch -rfc934mode was given).
1674 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_rfc934
)
1678 * output the content type and subtype
1680 np
= mh_xstrdup(TYPE_FIELD
);
1681 vp
= concat (" ", ci
->ci_type
, "/", ci
->ci_subtype
, NULL
);
1683 /* keep track of length of line */
1684 len
= LEN(TYPE_FIELD
) + strlen (ci
->ci_type
)
1685 + strlen (ci
->ci_subtype
) + 3;
1687 extbody
= ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_EXTERNAL
;
1688 mailbody
= extbody
&& ((struct exbody
*) ct
->c_ctparams
)->eb_body
;
1691 * Append the attribute/value pairs to
1692 * the end of the Content-Type line.
1695 if (ci
->ci_first_pm
) {
1696 char *s
= output_params(len
, ci
->ci_first_pm
, &len
, mailbody
);
1699 die("Internal error: failed outputting Content-Type "
1707 * Append any RFC-822 comment to the end of
1708 * the Content-Type line.
1710 if (ci
->ci_comment
) {
1711 snprintf (buffer
, sizeof(buffer
), "(%s)", ci
->ci_comment
);
1712 if (len
+ 1 + (cc
= 2 + strlen (ci
->ci_comment
)) >= CPERLIN
) {
1713 vp
= add ("\n\t", vp
);
1719 vp
= add (buffer
, vp
);
1722 vp
= add ("\n", vp
);
1723 add_header (ct
, np
, vp
);
1726 * output the Content-ID, unless disabled by -nocontentid. Note that
1727 * RFC 2045 always requires a Content-ID header for message/external-body
1730 if ((contentidsw
|| ct
->c_ctexbody
) && ct
->c_id
) {
1731 np
= mh_xstrdup(ID_FIELD
);
1732 vp
= concat (" ", ct
->c_id
, NULL
);
1733 add_header (ct
, np
, vp
);
1736 * output the Content-Description
1739 np
= mh_xstrdup(DESCR_FIELD
);
1740 vp
= concat (" ", ct
->c_descr
, NULL
);
1741 if (header_encoding
!= CE_8BIT
) {
1742 if (encode_rfc2047(DESCR_FIELD
, &vp
, header_encoding
, NULL
)) {
1743 die("Unable to encode %s header", DESCR_FIELD
);
1746 add_header (ct
, np
, vp
);
1750 * output the Content-Disposition. If it's NULL but c_dispo_type is
1751 * set, then we need to build it.
1754 np
= mh_xstrdup(DISPO_FIELD
);
1755 vp
= concat (" ", ct
->c_dispo
, NULL
);
1756 add_header (ct
, np
, vp
);
1757 } else if (ct
->c_dispo_type
) {
1758 vp
= concat (" ", ct
->c_dispo_type
, NULL
);
1759 len
= LEN(DISPO_FIELD
) + strlen(vp
) + 1;
1760 np
= output_params(len
, ct
->c_dispo_first
, NULL
, 0);
1764 add_header (ct
, mh_xstrdup(DISPO_FIELD
), vp
);
1769 * If this is the internal content structure for a
1770 * "message/external", then we are done with the
1771 * headers (since it has no body).
1777 * output the Content-MD5
1780 np
= mh_xstrdup(MD5_FIELD
);
1781 vp
= calculate_digest (ct
, ct
->c_encoding
== CE_QUOTED
);
1782 add_header (ct
, np
, vp
);
1786 * output the Content-Transfer-Encoding
1787 * If using EAI and message body is 7-bit, force 8-bit C-T-E.
1789 if (header_encoding
== CE_8BIT
&& ct
->c_encoding
== CE_7BIT
) {
1790 ct
->c_encoding
= CE_8BIT
;
1793 switch (ct
->c_encoding
) {
1795 /* Nothing to output */
1799 np
= mh_xstrdup(ENCODING_FIELD
);
1800 vp
= concat (" ", "8bit", "\n", NULL
);
1801 add_header (ct
, np
, vp
);
1805 if (ct
->c_type
== CT_MESSAGE
|| ct
->c_type
== CT_MULTIPART
)
1806 die("internal error, invalid encoding");
1808 np
= mh_xstrdup(ENCODING_FIELD
);
1809 vp
= concat (" ", "quoted-printable", "\n", NULL
);
1810 add_header (ct
, np
, vp
);
1814 if (ct
->c_type
== CT_MESSAGE
|| ct
->c_type
== CT_MULTIPART
)
1815 die("internal error, invalid encoding");
1817 np
= mh_xstrdup(ENCODING_FIELD
);
1818 vp
= concat (" ", "base64", "\n", NULL
);
1819 add_header (ct
, np
, vp
);
1823 if (ct
->c_type
== CT_MESSAGE
)
1824 die("internal error, invalid encoding");
1826 np
= mh_xstrdup(ENCODING_FIELD
);
1827 vp
= concat (" ", "binary", "\n", NULL
);
1828 add_header (ct
, np
, vp
);
1832 die("unknown transfer encoding in content");
1837 * Additional content specific header processing
1839 switch (ct
->c_type
) {
1842 struct multipart
*m
;
1845 m
= (struct multipart
*) ct
->c_ctparams
;
1846 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1850 build_headers (p
, header_encoding
);
1856 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1859 e
= (struct exbody
*) ct
->c_ctparams
;
1860 build_headers (e
->eb_content
, header_encoding
);
1873 static char nib2b64
[0x40+1] =
1874 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1877 calculate_digest (CT ct
, int asciiP
)
1882 unsigned char digest
[16];
1883 unsigned char outbuf
[25];
1885 CE ce
= &ct
->c_cefile
;
1886 char *infilename
= ce
->ce_file
? ce
->ce_file
: ct
->c_file
;
1890 if ((in
= fopen (infilename
, "r")) == NULL
)
1891 adios (infilename
, "unable to open for reading");
1893 /* Initialize md5 context */
1894 MD5Init (&mdContext
);
1896 /* calculate md5 message digest */
1901 while ((gotlen
= getline(&bufp
, &buflen
, in
)) != -1) {
1904 cp
= bufp
+ gotlen
- 1;
1905 if ((c
= *cp
) == '\n')
1908 MD5Update (&mdContext
, (unsigned char *) bufp
,
1909 (unsigned int) gotlen
);
1912 MD5Update (&mdContext
, (unsigned char *) "\r\n", 2);
1915 char buffer
[BUFSIZ
];
1916 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), in
)) > 0)
1917 MD5Update (&mdContext
, (unsigned char *) buffer
, (unsigned int) cc
);
1920 /* md5 finalization. Write digest and zero md5 context */
1921 MD5Final (digest
, &mdContext
);
1926 /* print debugging info */
1930 fprintf (stderr
, "MD5 digest=");
1931 for (ep
= (dp
= digest
) + sizeof digest
;
1933 fprintf (stderr
, "%02x", *dp
& 0xff);
1934 fprintf (stderr
, "\n");
1937 /* encode the digest using base64 */
1938 for (dp
= digest
, op
= (char *) outbuf
,
1940 cc
> 0; cc
-= 3, op
+= 4) {
1944 bits
= (*dp
++ & 0xff) << 16;
1946 bits
|= (*dp
++ & 0xff) << 8;
1948 bits
|= *dp
++ & 0xff;
1951 for (bp
= op
+ 4; bp
> op
; bits
>>= 6)
1952 *--bp
= nib2b64
[bits
& 0x3f];
1960 /* null terminate string */
1963 /* now make copy and return string */
1964 vp
= concat (" ", outbuf
, "\n", NULL
);
1969 * Set things up for the content structure for file "filename" that
1974 setup_attach_content(CT ct
, char *filename
)
1976 char *type
, *simplename
= r1bindex(filename
, '/');
1977 struct str2init
*s2i
;
1980 if (! (type
= mime_type(filename
))) {
1981 die("Unable to determine MIME type of \"%s\"", filename
);
1985 * Parse the Content-Type. get_ctinfo() parses MIME parameters, but
1986 * since we're just feeding it a MIME type we have to add those ourselves.
1987 * Map that to a valid content-type label and call any initialization
1991 if (get_ctinfo(type
, ct
, 0) == NOTOK
)
1996 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
1997 if (strcasecmp(ct
->c_ctinfo
.ci_type
, s2i
->si_key
) == 0)
1999 if (!s2i
->si_key
&& !uprf(ct
->c_ctinfo
.ci_type
, "X-"))
2003 * Make sure the type isn't incompatible with what we can handle
2006 switch (ct
->c_type
= s2i
->si_val
) {
2008 die("multipart types must be specified by mhbuild directives");
2012 if (strcasecmp(ct
->c_ctinfo
.ci_subtype
, "partial") == 0)
2013 die("Sorry, %s/%s isn't supported", ct
->c_ctinfo
.ci_type
,
2014 ct
->c_ctinfo
.ci_subtype
);
2015 if (strcasecmp(ct
->c_ctinfo
.ci_subtype
, "external-body") == 0)
2016 die("external-body messages must be specified "
2017 "by mhbuild directives");
2022 * This sets the subtype, if it's significant
2024 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
2025 (*ct
->c_ctinitfnx
)(ct
);
2030 * Feed in a few attributes; specifically, the name attribute, the
2031 * content-description, and the content-disposition.
2034 for (pm
= ct
->c_ctinfo
.ci_first_pm
; pm
; pm
= pm
->pm_next
) {
2035 if (strcasecmp(pm
->pm_name
, "name") == 0) {
2037 pm
->pm_value
= mh_xstrdup(simplename
);
2043 add_param(&ct
->c_ctinfo
.ci_first_pm
, &ct
->c_ctinfo
.ci_last_pm
,
2044 "name", simplename
, 0);
2046 ct
->c_descr
= mh_xstrdup(simplename
);
2047 ct
->c_descr
= add("\n", ct
->c_descr
);
2048 ct
->c_cefile
.ce_file
= mh_xstrdup(filename
);
2050 set_disposition (ct
);
2052 add_param(&ct
->c_dispo_first
, &ct
->c_dispo_last
, "filename", simplename
, 0);
2056 * If disposition type hasn't already been set in ct:
2057 * Look for mhbuild-disposition-<type>/<subtype> entry
2058 * that specifies Content-Disposition type. Only
2059 * 'attachment' and 'inline' are allowed. Default to
2063 set_disposition (CT ct
)
2065 if (ct
->c_dispo_type
== NULL
) {
2066 char *cp
= context_find_by_type ("disposition", ct
->c_ctinfo
.ci_type
,
2067 ct
->c_ctinfo
.ci_subtype
);
2069 if (cp
&& strcasecmp (cp
, "attachment") &&
2070 strcasecmp (cp
, "inline")) {
2071 inform("configuration problem: %s-disposition-%s%s%s specifies "
2072 "'%s' but only 'attachment' and 'inline' are allowed, "
2073 "continuing...", invo_name
,
2074 ct
->c_ctinfo
.ci_type
,
2075 ct
->c_ctinfo
.ci_subtype
? "/" : "",
2076 FENDNULL(ct
->c_ctinfo
.ci_subtype
),
2082 ct
->c_dispo_type
= mh_xstrdup(cp
);
2087 * Set text content charset if it was unspecified. contains8bit
2089 * 0: content does not contain 8-bit characters
2090 * 1: content contains 8-bit characters
2091 * -1: ignore content and use user's locale to determine charset
2094 set_charset (CT ct
, int contains8bit
)
2096 if (ct
->c_type
== CT_TEXT
) {
2099 if (ct
->c_ctparams
== NULL
) {
2102 t
->tx_charset
= CHARSET_UNSPECIFIED
;
2104 t
= (struct text
*) ct
->c_ctparams
;
2107 if (t
->tx_charset
== CHARSET_UNSPECIFIED
) {
2108 CI ci
= &ct
->c_ctinfo
;
2109 char *eightbitcharset
= write_charset_8bit();
2110 char *charset
= contains8bit
? eightbitcharset
: "us-ascii";
2112 if (contains8bit
== 1 &&
2113 strcasecmp (eightbitcharset
, "US-ASCII") == 0) {
2114 die("Text content contains 8 bit characters, but "
2115 "character set is US-ASCII");
2118 add_param (&ci
->ci_first_pm
, &ci
->ci_last_pm
, "charset", charset
,
2121 t
->tx_charset
= CHARSET_SPECIFIED
;
2128 * Look at all of the replied-to message parts and expand any that
2129 * are matched by a pseudoheader. Except don't descend into
2133 expand_pseudoheaders (CT ct
, struct multipart
*m
, const char *infile
,
2134 const convert_list
*convert_head
)
2136 /* text_plain_ct is used to concatenate all of the text/plain
2137 replies into one part, instead of having each one in a separate
2139 CT text_plain_ct
= NULL
;
2141 switch (ct
->c_type
) {
2142 case CT_MULTIPART
: {
2143 struct multipart
*mp
= (struct multipart
*) ct
->c_ctparams
;
2146 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
2147 bool matched
= false;
2149 /* The parts are in descending priority order (defined by
2150 RFC 2046 Sec. 5.1.4) because they were reversed by
2151 parse_mime (). So, stop looking for matches with
2152 immediate subparts after the first match of an
2154 for (part
= mp
->mp_parts
; ! matched
&& part
; part
= part
->mp_next
) {
2155 char *type_subtype
=
2156 concat (part
->mp_part
->c_ctinfo
.ci_type
, "/",
2157 part
->mp_part
->c_ctinfo
.ci_subtype
, NULL
);
2159 if (part
->mp_part
->c_type
== CT_MULTIPART
) {
2160 expand_pseudoheaders (part
->mp_part
, m
, infile
,
2163 const convert_list
*c
;
2165 for (c
= convert_head
; c
; c
= c
->next
) {
2166 if (! strcasecmp (type_subtype
, c
->type
)) {
2167 expand_pseudoheader (part
->mp_part
, &text_plain_ct
,
2169 c
->type
, c
->argstring
);
2175 free (type_subtype
);
2178 for (part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
2179 expand_pseudoheaders (part
->mp_part
, m
, infile
, convert_head
);
2186 char *type_subtype
=
2187 concat (ct
->c_ctinfo
.ci_type
, "/", ct
->c_ctinfo
.ci_subtype
,
2189 const convert_list
*c
;
2191 for (c
= convert_head
; c
; c
= c
->next
) {
2192 if (! strcasecmp (type_subtype
, c
->type
)) {
2193 expand_pseudoheader (ct
, &text_plain_ct
, m
, infile
, c
->type
,
2198 free (type_subtype
);
2206 * Expand a single pseudoheader. It's for the specified type.
2209 expand_pseudoheader (CT ct
, CT
*text_plain_ct
, struct multipart
*m
,
2210 const char *infile
, const char *type
,
2211 const char *argstring
)
2214 FILE *reply_fp
= NULL
;
2215 char *convert
, *type_p
, *subtype_p
;
2216 char *convert_command
;
2217 char *charset
= NULL
;
2219 struct str2init
*s2i
;
2224 type_p
= getcpy (type
);
2225 if ((subtype_p
= strchr (type_p
, '/'))) {
2226 *subtype_p
++ = '\0';
2227 convert
= context_find_by_type ("convert", type_p
, subtype_p
);
2230 type_p
= concat ("mhbuild-convert-", type
, NULL
);
2231 convert
= context_find (type_p
);
2236 /* No mhbuild-convert- entry in mhn.defaults or profile for type. */
2239 /* reply_file is used to pass the output of the convert. */
2240 reply_file
= getcpy (m_mktemp2 (NULL
, invo_name
, NULL
, NULL
));
2242 concat (convert
, " ", FENDNULL(argstring
), " >", reply_file
, NULL
);
2244 /* Convert here . . . */
2245 ct
->c_storeproc
= mh_xstrdup(convert_command
);
2246 ct
->c_umask
= ~m_gmprot ();
2248 if ((status
= show_content_aux (ct
, 0, convert_command
, NULL
, NULL
)) !=
2250 inform("store of %s content failed, continuing...", type
);
2252 free (convert_command
);
2254 /* Fill out the the new ct, reply_ct. */
2256 init_decoded_content (reply_ct
, infile
);
2258 if (extract_headers (reply_ct
, reply_file
, &reply_fp
) == NOTOK
) {
2259 inform("failed to extract headers from convert output in %s, "
2260 "continuing...", reply_file
);
2265 /* This sets reply_ct->c_ctparams, and reply_ct->c_termproc if the
2266 charset can't be handled natively. */
2267 for (s2i
= str2cts
; s2i
->si_key
; s2i
++) {
2268 if (strcasecmp(reply_ct
->c_ctinfo
.ci_type
, s2i
->si_key
) == 0) {
2273 if ((reply_ct
->c_ctinitfnx
= s2i
->si_init
)) {
2274 (*reply_ct
->c_ctinitfnx
)(reply_ct
);
2277 if ((cp
= get_param (reply_ct
->c_ctinfo
.ci_first_pm
, "charset", '?', 1))) {
2278 /* The reply Content-Type had the charset. */
2281 set_charset (reply_ct
, -1);
2282 charset
= get_param (reply_ct
->c_ctinfo
.ci_first_pm
, "charset", '?', 1);
2285 /* Concatenate text/plain parts. */
2286 if (reply_ct
->c_type
== CT_TEXT
&& reply_ct
->c_subtype
== TEXT_PLAIN
) {
2287 if (! *text_plain_ct
&& m
->mp_parts
&& m
->mp_parts
->mp_part
&&
2288 m
->mp_parts
->mp_part
->c_type
== CT_TEXT
&&
2289 m
->mp_parts
->mp_part
->c_subtype
== TEXT_PLAIN
) {
2290 *text_plain_ct
= m
->mp_parts
->mp_part
;
2291 /* Make sure that the charset is set in the text/plain part. */
2292 set_charset (*text_plain_ct
, -1);
2295 if (*text_plain_ct
) {
2296 /* Only concatenate if the charsets are identical. */
2297 char *text_plain_ct_charset
=
2298 get_param ((*text_plain_ct
)->c_ctinfo
.ci_first_pm
, "charset",
2301 if (strcasecmp (text_plain_ct_charset
, charset
) == 0) {
2302 /* Append this text/plain reply to the first one.
2303 If there's a problem anywhere along the way,
2304 instead attach it is a separate part. */
2305 int text_plain_reply
=
2306 open ((*text_plain_ct
)->c_cefile
.ce_file
,
2307 O_WRONLY
| O_APPEND
);
2308 int addl_reply
= open (reply_file
, O_RDONLY
);
2310 if (text_plain_reply
!= NOTOK
&& addl_reply
!= NOTOK
) {
2311 /* Insert blank line before each addl part. */
2312 /* It would be nice not to do this for the first one. */
2313 if (write (text_plain_reply
, "\n", 1) == 1) {
2314 /* Copy the text from the new reply and
2315 then free its Content struct. */
2316 cpydata (addl_reply
, text_plain_reply
,
2317 (*text_plain_ct
)->c_cefile
.ce_file
,
2319 if (close (text_plain_reply
) == OK
&&
2320 close (addl_reply
) == OK
) {
2321 /* If appended text needed 8-bit but first text didn't,
2322 propagate the 8-bit indication. */
2323 if ((*text_plain_ct
)->c_reqencoding
== CE_7BIT
&&
2324 reply_ct
->c_reqencoding
== CE_8BIT
) {
2325 (*text_plain_ct
)->c_reqencoding
= CE_8BIT
;
2328 if (reply_fp
) { fclose (reply_fp
); }
2330 free_content (reply_ct
);
2337 *text_plain_ct
= reply_ct
;
2341 reply_ct
->c_cefile
.ce_file
= reply_file
;
2342 reply_ct
->c_cefile
.ce_fp
= reply_fp
;
2343 reply_ct
->c_cefile
.ce_unlink
= 1;
2345 /* Attach the new part to the parent multipart/mixed, "m". */
2347 part
->mp_part
= reply_ct
;
2351 for (p
= m
->mp_parts
; p
&& p
->mp_next
; p
= p
->mp_next
) { continue; }
2359 /* Extract any Content-Type header from beginning of convert output. */
2361 extract_headers (CT ct
, char *reply_file
, FILE **reply_fp
)
2363 char *buffer
= NULL
, *cp
, *end_of_header
;
2364 bool found_header
= false;
2365 struct stat statbuf
;
2367 /* Read the convert reply from the file to memory. */
2368 if (stat (reply_file
, &statbuf
) == NOTOK
) {
2369 admonish (reply_file
, "failed to stat");
2370 goto failed_to_extract_ct
;
2373 buffer
= mh_xmalloc (statbuf
.st_size
+ 1);
2375 if ((*reply_fp
= fopen (reply_file
, "r+")) == NULL
||
2376 fread (buffer
, 1, (size_t) statbuf
.st_size
, *reply_fp
) <
2377 (size_t) statbuf
.st_size
) {
2378 admonish (reply_file
, "failed to read");
2379 goto failed_to_extract_ct
;
2381 buffer
[statbuf
.st_size
] = '\0';
2383 /* Look for a header in the convert reply. */
2384 if (strncasecmp (buffer
, TYPE_FIELD
, LEN(TYPE_FIELD
)) == 0 &&
2385 buffer
[LEN(TYPE_FIELD
)] == ':') {
2386 if ((end_of_header
= strstr (buffer
, "\r\n\r\n"))) {
2388 found_header
= true;
2389 } else if ((end_of_header
= strstr (buffer
, "\n\n"))) {
2391 found_header
= true;
2401 /* Truncate buffer to just the C-T. */
2402 *end_of_header
= '\0';
2403 n
= strlen (buffer
);
2405 if (get_ctinfo (buffer
+ 14, ct
, 0) != OK
) {
2406 inform("unable to get content info for reply, continuing...");
2407 goto failed_to_extract_ct
;
2410 /* Hack. Use parse_mime() to detect the type/subtype of the
2411 reply, which we'll use below. */
2412 tmp_file
= getcpy (m_mktemp2 (NULL
, invo_name
, NULL
, NULL
));
2413 tmp_f
= fopen(tmp_file
, "w");
2415 goto failed_to_extract_ct
;
2416 written
= fwrite(buffer
, 1, n
, tmp_f
);
2419 goto failed_to_extract_ct
;
2421 tmp_ct
= parse_mime (tmp_file
);
2423 /* The type and subtype were detected from the reply
2424 using parse_mime() above. */
2425 ct
->c_type
= tmp_ct
->c_type
;
2426 ct
->c_subtype
= tmp_ct
->c_subtype
;
2427 free_content (tmp_ct
);
2432 /* Rewrite the content without the header. */
2433 cp
= end_of_header
+ 1;
2436 if (fwrite (cp
, 1, statbuf
.st_size
- (cp
- buffer
), *reply_fp
) <
2437 (size_t) (statbuf
.st_size
- (cp
- buffer
))) {
2438 admonish (reply_file
, "failed to write");
2439 goto failed_to_extract_ct
;
2442 if (ftruncate (fileno (*reply_fp
), statbuf
.st_size
- (cp
- buffer
)) !=
2444 advise (reply_file
, "ftruncate");
2445 goto failed_to_extract_ct
;
2448 /* No header section, assume the reply is text/plain. */
2449 ct
->c_type
= CT_TEXT
;
2450 ct
->c_subtype
= TEXT_PLAIN
;
2451 if (get_ctinfo ("text/plain", ct
, 0) == NOTOK
) {
2452 /* This never should fail, but just in case. */
2453 die("unable to get content info for reply");
2457 /* free_encoding() will close reply_fp, which is passed through
2458 ct->c_cefile.ce_fp. */
2462 failed_to_extract_ct
:
2463 if (*reply_fp
) { fclose (*reply_fp
); }