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/m_gmprot.h"
18 #include "sbr/m_getfld.h"
19 #include "sbr/concat.h"
20 #include "sbr/r1bindex.h"
21 #include "sbr/encode_rfc2047.h"
22 #include "sbr/copyip.h"
23 #include "sbr/cpydata.h"
24 #include "sbr/trimcpy.h"
26 #include "sbr/check_charset.h"
27 #include "sbr/getcpy.h"
28 #include "sbr/m_convert.h"
29 #include "sbr/getfolder.h"
30 #include "sbr/folder_read.h"
31 #include "sbr/folder_free.h"
32 #include "sbr/context_find.h"
33 #include "sbr/brkstring.h"
34 #include "sbr/pidstatus.h"
36 #include "sbr/error.h"
41 #include "h/fmt_scan.h"
43 #include "h/mhparse.h"
46 #include "h/mhcachesbr.h"
48 #include "sbr/m_mktemp.h"
49 #include "sbr/message_id.h"
50 #include "sbr/mime_type.h"
52 #include "mhshowsbr.h"
54 #ifdef HAVE_SYS_TIME_H
55 # include <sys/time.h>
64 extern bool contentidsw
;
66 static char prefix
[] = "----- =_aaaaaaaaaa";
70 struct attach_list
*next
;
73 typedef struct convert_list
{
77 struct convert_list
*next
;
84 static int init_decoded_content (CT
, const char *);
85 static void setup_attach_content(CT
, char *);
86 static void set_disposition (CT
);
87 static void set_charset (CT
, int);
88 static void expand_pseudoheaders (CT
, struct multipart
*, const char *,
89 const convert_list
*);
90 static void expand_pseudoheader (CT
, CT
*, struct multipart
*, const char *,
91 const char *, const char *);
92 static char *fgetstr (char *, int, FILE *);
93 static int user_content (FILE *, char *, CT
*, const char *infilename
);
94 static void set_id (CT
, int);
95 static int compose_content (CT
, int);
96 static int scan_content (CT
, size_t);
97 static int build_headers (CT
, int);
98 static char *calculate_digest (CT
, int);
99 static int extract_headers (CT
, char *, FILE **);
102 static unsigned char directives_stack
[32];
103 static unsigned int directives_index
;
108 return directives_stack
[directives_index
];
112 directive_onoff(int onoff
)
114 if (directives_index
>= sizeof(directives_stack
) - 1) {
115 fprintf(stderr
, "mhbuild: #on/off overflow, continuing\n");
118 directives_stack
[++directives_index
] = onoff
;
122 directive_init(int onoff
)
124 directives_index
= 0;
125 directives_stack
[0] = onoff
;
131 if (directives_index
> 0)
134 fprintf(stderr
, "mhbuild: #pop underflow, continuing\n");
138 * Main routine for translating composition file
139 * into valid MIME message. It translates the draft
140 * into a content structure (actually a tree of content
141 * structures). This message then can be manipulated
142 * in various ways, including being output via
147 build_mime (char *infile
, int autobuild
, int dist
, int directives
,
148 int header_encoding
, size_t maxunencoded
, int verbose
)
151 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
158 m_getfld_state_t gstate
;
159 struct attach_list
*attach_head
= NULL
, *attach_tail
= NULL
, *at_entry
;
160 convert_list
*convert_head
= NULL
, *convert_tail
= NULL
, *convert
;
162 directive_init(directives
);
164 umask (~m_gmprot ());
166 /* open the composition draft */
167 if ((in
= fopen (infile
, "r")) == NULL
)
168 adios (infile
, "unable to open for reading");
171 * Allocate space for primary (outside) content
176 * Allocate structure for handling decoded content
177 * for this part. We don't really need this, but
178 * allocate it to remain consistent.
180 init_decoded_content (ct
, infile
);
183 * Parse some of the header fields in the composition
184 * draft into the linked list of header fields for
185 * the new MIME message.
187 gstate
= m_getfld_state_init(in
);
188 m_getfld_track_filepos2(&gstate
);
189 for (compnum
= 1;;) {
190 int bufsz
= sizeof buf
;
191 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
196 /* abort if draft has Mime-Version or C-T-E header field */
197 if (strcasecmp (name
, VRSN_FIELD
) == 0 ||
198 strcasecmp (name
, ENCODING_FIELD
) == 0) {
204 die("draft shouldn't contain %s: field", name
);
207 /* ignore any Content-Type fields in the header */
208 if (!strcasecmp (name
, TYPE_FIELD
)) {
209 while (state
== FLDPLUS
) {
211 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
216 /* get copies of the buffers */
217 np
= mh_xstrdup(name
);
218 vp
= mh_xstrdup(buf
);
220 /* if necessary, get rest of field */
221 while (state
== FLDPLUS
) {
223 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
224 vp
= add (buf
, vp
); /* add to previous value */
228 * Now add the header data to the list, unless it's an attach
229 * header; in that case, add it to our attach list
232 if (strcasecmp(ATTACH_FIELD
, np
) == 0 ||
233 strcasecmp(ATTACH_FIELD_ALT
, np
) == 0) {
234 struct attach_list
*entry
;
235 char *s
= vp
, *e
= vp
+ strlen(vp
) - 1;
239 * Make sure we can find the start of this filename.
240 * If it's blank, we skip completely. Otherwise, strip
241 * off any leading spaces and trailing newlines.
244 while (isspace((unsigned char) *s
))
247 while (e
> s
&& *e
== '\n')
256 entry
->filename
= mh_xstrdup(s
);
261 attach_tail
->next
= entry
;
264 attach_head
= attach_tail
= entry
;
266 } else if (strncasecmp(MHBUILD_FILE_PSEUDOHEADER
, np
,
267 LEN(MHBUILD_FILE_PSEUDOHEADER
)) == 0) {
269 * Nmh-mhbuild-file-text/calendar: /home/user/Mail/inbox/9
271 char *type
= np
+ LEN(MHBUILD_FILE_PSEUDOHEADER
);
274 /* vp should begin with a space because m_getfld2()
275 includes the space after the colon in buf. */
276 while (isspace((unsigned char) *filename
)) { ++filename
; }
277 /* Trim trailing newline and any other whitespace. */
280 for (convert
= convert_head
; convert
; convert
= convert
->next
) {
281 if (strcasecmp (convert
->type
, type
) == 0) { break; }
284 if (convert
->filename
&&
285 strcasecmp (convert
->filename
, filename
)) {
286 die("Multiple %s headers with different files"
287 " not allowed", type
);
289 convert
->filename
= mh_xstrdup(filename
);
293 convert
->filename
= mh_xstrdup(filename
);
294 convert
->type
= mh_xstrdup(type
);
297 convert_tail
->next
= convert
;
299 convert_head
= convert
;
301 convert_tail
= convert
;
306 } else if (strncasecmp(MHBUILD_ARGS_PSEUDOHEADER
, np
,
307 LEN(MHBUILD_ARGS_PSEUDOHEADER
)) == 0) {
309 * Nmh-mhbuild-args-text/calendar: -reply accept
311 char *type
= np
+ LEN(MHBUILD_ARGS_PSEUDOHEADER
);
312 char *argstring
= vp
;
314 /* vp should begin with a space because m_getfld2()
315 includes the space after the colon in buf. */
316 while (isspace((unsigned char) *argstring
)) { ++argstring
; }
317 /* Trim trailing newline and any other whitespace. */
320 for (convert
= convert_head
; convert
; convert
= convert
->next
) {
321 if (strcasecmp (convert
->type
, type
) == 0) { break; }
324 if (convert
->argstring
&&
325 strcasecmp (convert
->argstring
, argstring
)) {
326 die("Multiple %s headers with different "
327 "argstrings not allowed", type
);
329 convert
->argstring
= mh_xstrdup(argstring
);
333 convert
->type
= mh_xstrdup(type
);
334 convert
->argstring
= mh_xstrdup(argstring
);
337 convert_tail
->next
= convert
;
339 convert_head
= convert
;
341 convert_tail
= convert
;
347 add_header (ct
, np
, vp
);
351 /* if this wasn't the last header field, then continue */
355 fseek (in
, (long) (-strlen (buf
)), SEEK_CUR
);
362 die("message format error in component #%d", compnum
);
365 die("getfld() returned %d", state
);
369 m_getfld_state_destroy (&gstate
);
371 if (header_encoding
!= CE_8BIT
) {
373 * Iterate through the list of headers and call the function to MIME-ify
377 for (hp
= ct
->c_first_hf
; hp
!= NULL
; hp
= hp
->next
) {
378 if (encode_rfc2047(hp
->name
, &hp
->value
, header_encoding
, NULL
)) {
379 die("Unable to encode header \"%s\"", hp
->name
);
385 * Now add the MIME-Version header field
386 * to the list of header fields.
390 np
= mh_xstrdup(VRSN_FIELD
);
391 vp
= concat (" ", VRSN_VALUE
, "\n", NULL
);
392 add_header (ct
, np
, vp
);
396 * We initially assume we will find multiple contents in the
397 * draft. So create a multipart/mixed content to hold everything.
398 * We can remove this later, if it is not needed.
400 if (get_ctinfo ("multipart/mixed", ct
, 0) == NOTOK
)
402 ct
->c_type
= CT_MULTIPART
;
403 ct
->c_subtype
= MULTI_MIXED
;
406 ct
->c_ctparams
= (void *) m
;
410 * read and parse the composition file
411 * and the directives it contains.
413 while (fgetstr (buf
, sizeof(buf
) - 1, in
)) {
417 if (user_content (in
, buf
, &p
, infile
) == DONE
) {
418 inform("ignoring spurious #end, continuing...");
431 * Add any Attach headers to the list of MIME parts at the end of the
435 for (at_entry
= attach_head
; at_entry
; ) {
436 struct attach_list
*at_prev
= at_entry
;
440 if (access(at_entry
->filename
, R_OK
) != 0) {
441 adios("reading", "Unable to open %s for", at_entry
->filename
);
445 init_decoded_content(p
, infile
);
448 * Initialize our content structure based on the filename,
449 * and fill in all of the relevant fields. Also place MIME
450 * parameters in the attributes array.
453 setup_attach_content(p
, at_entry
->filename
);
460 at_entry
= at_entry
->next
;
461 free(at_prev
->filename
);
466 * Handle the mhbuild pseudoheaders, which deal with specific
473 set_done(freects_done
);
475 /* In case there are multiple calls that land here, prevent leak. */
476 for (ctp
= cts
; ctp
&& *ctp
; ++ctp
) { free_content (*ctp
); }
479 /* Extract the type part (as a CT) from filename. */
480 cts
= mh_xcalloc(2, sizeof *cts
);
481 if (! (cts
[0] = parse_mime (convert_head
->filename
))) {
482 die("failed to parse %s", convert_head
->filename
);
485 expand_pseudoheaders (cts
[0], m
, infile
, convert_head
);
487 /* Free the convert list. */
488 for (convert
= convert_head
; convert
; convert
= next
) {
489 next
= convert
->next
;
490 free (convert
->type
);
491 free (convert
->filename
);
492 free (convert
->argstring
);
499 * To allow for empty message bodies, if we've found NO content at all
500 * yet cook up an empty text/plain part.
509 init_decoded_content(p
, infile
);
511 if (get_ctinfo ("text/plain", p
, 0) == NOTOK
)
515 p
->c_subtype
= TEXT_PLAIN
;
516 p
->c_encoding
= CE_7BIT
;
518 * Sigh. ce_file contains the "decoded" contents of this part.
519 * So this seems like the best option available since we're going
520 * to call scan_content() on this.
522 p
->c_cefile
.ce_file
= mh_xstrdup("/dev/null");
523 p
->c_begin
= ftell(in
);
524 p
->c_end
= ftell(in
);
527 t
->tx_charset
= CHARSET_SPECIFIED
;
536 * close the composition draft since
537 * it's not needed any longer.
542 * If only one content was found, then remove and
543 * free the outer multipart content.
545 if (!m
->mp_parts
->mp_next
) {
548 p
= m
->mp_parts
->mp_part
;
549 m
->mp_parts
->mp_part
= NULL
;
551 /* move header fields */
552 p
->c_first_hf
= ct
->c_first_hf
;
553 p
->c_last_hf
= ct
->c_last_hf
;
554 ct
->c_first_hf
= NULL
;
555 ct
->c_last_hf
= NULL
;
564 * Fill out, or expand directives. Parse and execute
565 * commands specified by profile composition strings.
567 compose_content (ct
, verbose
);
569 if ((cp
= strchr(prefix
, 'a')) == NULL
)
570 die("internal error(4)");
573 * If using EAI, force 8-bit charset.
575 if (header_encoding
== CE_8BIT
) {
580 * Scan the contents. Choose a transfer encoding, and
581 * check if prefix for multipart boundary clashes with
582 * any of the contents.
584 while (scan_content (ct
, maxunencoded
) == NOTOK
) {
589 die("giving up trying to find a unique delimiter string");
594 /* Build the rest of the header field structures */
596 build_headers (ct
, header_encoding
);
603 * Set up structures for placing unencoded
604 * content when building parts.
608 init_decoded_content (CT ct
, const char *filename
)
610 ct
->c_ceopenfnx
= open7Bit
; /* since unencoded */
611 ct
->c_ceclosefnx
= close_encoding
;
612 ct
->c_cesizefnx
= NULL
; /* since unencoded */
613 ct
->c_encoding
= CE_7BIT
; /* Seems like a reasonable default */
614 ct
->c_file
= mh_xstrdup(FENDNULL(filename
));
621 fgetstr (char *s
, int n
, FILE *stream
)
627 for (cp
= s
; cp
< ep
;) {
630 if (!fgets (cp
, n
, stream
))
631 return cp
== s
? NULL
: s
; /* "\\\nEOF" ignored. */
633 if (! do_direct() || (cp
== s
&& *cp
!= '#'))
634 return s
; /* Plaintext line. */
638 break; /* Can't contain "\\\n". */
639 cp
+= len
- 1; /* Just before NUL. */
640 if (*cp
-- != '\n' || *cp
!= '\\')
642 *cp
= '\0'; /* Erase the trailing "\\\n". */
646 if (strcmp(s
, "#on\n") == 0) {
648 } else if (strcmp(s
, "#off\n") == 0) {
650 } else if (strcmp(s
, "#pop\n") == 0) {
660 * Parse the composition draft for text and directives.
661 * Do initial setup of Content structure.
665 user_content (FILE *in
, char *buf
, CT
*ctp
, const char *infilename
)
669 char buffer
[NMH_BUFSIZ
];
673 struct str2init
*s2i
;
678 if (buf
[0] == '\n' || (do_direct() && strcmp (buf
, "#\n") == 0)) {
683 /* allocate basic Content structure */
687 /* allocate basic structure for handling decoded content */
688 init_decoded_content (ct
, infilename
);
695 * Handle inline text. Check if line
696 * is one of the following forms:
698 * 1) doesn't begin with '#' (implicit directive)
699 * 2) begins with "##" (implicit directive)
700 * 3) begins with "#<"
702 if (!do_direct() || buf
[0] != '#' || buf
[1] == '#' || buf
[1] == '<') {
706 char content
[BUFSIZ
];
710 if ((cp
= m_mktemp2(NULL
, invo_name
, NULL
, &out
)) == NULL
) {
711 adios("mhbuildsbr", "unable to create temporary file in %s",
715 /* use a temp file to collect the plain text lines */
716 ce
->ce_file
= mh_xstrdup(cp
);
719 if (do_direct() && (buf
[0] == '#' && buf
[1] == '<')) {
720 strncpy (content
, buf
+ 2, sizeof(content
));
726 /* the directive is implicit */
727 strncpy (content
, "text/plain", sizeof(content
));
729 strncpy (buffer
, (!do_direct() || buf
[0] != '#') ? buf
: buf
+ 1, sizeof(buffer
));
733 if (headers
>= 0 && do_direct() && uprf (buffer
, DESCR_FIELD
)
734 && buffer
[i
= LEN(DESCR_FIELD
)] == ':') {
738 ct
->c_descr
= add (buffer
+ i
+ 1, ct
->c_descr
);
739 if (!fgetstr (buffer
, sizeof(buffer
) - 1, in
))
740 die("end-of-file after %s: field in plaintext", DESCR_FIELD
);
748 die("#-directive after %s: field in plaintext", DESCR_FIELD
);
756 if (headers
>= 0 && do_direct() && uprf (buffer
, DISPO_FIELD
)
757 && buffer
[i
= LEN(DISPO_FIELD
)] == ':') {
761 ct
->c_dispo
= add (buffer
+ i
+ 1, ct
->c_dispo
);
762 if (!fgetstr (buffer
, sizeof(buffer
) - 1, in
))
763 die("end-of-file after %s: field in plaintext", DISPO_FIELD
);
771 die("#-directive after %s: field in plaintext", DISPO_FIELD
);
779 if (headers
!= 1 || buffer
[0] != '\n')
785 if ((cp
= fgetstr (buffer
, sizeof(buffer
) - 1, in
)) == NULL
)
787 if (do_direct() && buffer
[0] == '#') {
790 if (buffer
[1] != '#')
792 for (cp
= (bp
= buffer
) + 1; *cp
; cp
++)
799 ct
->c_end
= ftell (out
);
802 /* parse content type */
803 if (get_ctinfo (content
, ct
, inlineD
) == NOTOK
)
806 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
807 if (!strcasecmp (ci
->ci_type
, s2i
->si_key
))
809 if (!s2i
->si_key
&& !uprf (ci
->ci_type
, "X-"))
813 * check type specified (possibly implicitly)
815 switch (ct
->c_type
= s2i
->si_val
) {
817 if (!strcasecmp (ci
->ci_subtype
, "rfc822")) {
818 ct
->c_encoding
= CE_7BIT
;
823 die("it doesn't make sense to define an in-line %s content",
824 ct
->c_type
== CT_MESSAGE
? "message" : "multipart");
829 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
830 (*ct
->c_ctinitfnx
) (ct
);
835 fseek (in
, pos
, SEEK_SET
);
840 * If we've reached this point, the next line
841 * must be some type of explicit directive.
844 /* check if directive is external-type */
845 extrnal
= (buf
[1] == '@');
847 /* parse directive */
848 if (get_ctinfo (buf
+ (extrnal
? 2 : 1), ct
, 1) == NOTOK
)
851 /* check directive against the list of MIME types */
852 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
853 if (!strcasecmp (ci
->ci_type
, s2i
->si_key
))
857 * Check if the directive specified a valid type.
858 * This will happen if it was one of the following forms:
865 die("missing subtype in \"#%s\"", ci
->ci_type
);
867 switch (ct
->c_type
= s2i
->si_val
) {
869 die("use \"#begin ... #end\" instead of \"#%s/%s\"",
870 ci
->ci_type
, ci
->ci_subtype
);
874 if (!strcasecmp (ci
->ci_subtype
, "partial"))
875 die("sorry, \"#%s/%s\" isn't supported",
876 ci
->ci_type
, ci
->ci_subtype
);
877 if (!strcasecmp (ci
->ci_subtype
, "external-body"))
878 die("use \"#@type/subtype ... [] ...\" instead of \"#%s/%s\"",
879 ci
->ci_type
, ci
->ci_subtype
);
881 die( "use \"#forw [+folder] [msgs]\" instead of \"#%s/%s\"",
882 ci
->ci_type
, ci
->ci_subtype
);
886 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
887 (*ct
->c_ctinitfnx
) (ct
);
892 * #@type/subtype (external types directive)
899 die("need external information for \"#@%s/%s\"",
900 ci
->ci_type
, ci
->ci_subtype
);
903 snprintf (buffer
, sizeof(buffer
), "message/external-body; %s", ci
->ci_magic
);
908 * Since we are using the current Content structure to
909 * hold information about the type of the external
910 * reference, we need to create another Content structure
911 * for the message/external-body to wrap it in.
914 init_decoded_content(ct
, infilename
);
916 if (get_ctinfo (buffer
, ct
, 0) == NOTOK
)
918 ct
->c_type
= CT_MESSAGE
;
919 ct
->c_subtype
= MESSAGE_EXTERNAL
;
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 die("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
= mh_xstrdup(cp
);
971 die("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 die("only one folder per #forw directive");
997 folder
= pluspath (cp
);
1001 /* else, use the current folder */
1003 folder
= mh_xstrdup(getfolder(1));
1005 if (!(mp
= folder_read (folder
, 0)))
1006 die("unable to read folder %s", folder
);
1007 for (ap
= arguments
; *ap
; ap
++) {
1009 if (*cp
!= '+' && *cp
!= '@')
1010 if (!m_convert (mp
, cp
))
1017 * If there is more than one message to include, make this
1018 * a content of type "multipart/digest" and insert each message
1019 * as a subpart. If there is only one message, then make this
1020 * a content of type "message/rfc822".
1022 if (mp
->numsel
> 1) {
1023 /* we are forwarding multiple messages */
1024 if (get_ctinfo ("multipart/digest", ct
, 0) == NOTOK
)
1026 ct
->c_type
= CT_MULTIPART
;
1027 ct
->c_subtype
= MULTI_DIGEST
;
1030 ct
->c_ctparams
= (void *) m
;
1033 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
1034 if (is_selected(mp
, msgnum
)) {
1040 init_decoded_content (p
, infilename
);
1042 if (get_ctinfo ("message/rfc822", p
, 0) == NOTOK
)
1044 p
->c_type
= CT_MESSAGE
;
1045 p
->c_subtype
= MESSAGE_RFC822
;
1047 snprintf (buffer
, sizeof(buffer
), "%s/%d", mp
->foldpath
, msgnum
);
1048 pe
->ce_file
= mh_xstrdup(buffer
);
1049 if (listsw
&& stat (pe
->ce_file
, &st
) != NOTOK
)
1050 p
->c_end
= (long) st
.st_size
;
1054 pp
= &part
->mp_next
;
1059 /* we are forwarding one message */
1060 if (get_ctinfo ("message/rfc822", ct
, 0) == NOTOK
)
1062 ct
->c_type
= CT_MESSAGE
;
1063 ct
->c_subtype
= MESSAGE_RFC822
;
1065 msgnum
= mp
->lowsel
;
1066 snprintf (buffer
, sizeof(buffer
), "%s/%d", mp
->foldpath
, msgnum
);
1067 ce
->ce_file
= mh_xstrdup(buffer
);
1068 if (listsw
&& stat (ce
->ce_file
, &st
) != NOTOK
)
1069 ct
->c_end
= (long) st
.st_size
;
1072 folder_free (mp
); /* free folder/message structure */
1079 if (!strcasecmp (ci
->ci_type
, "end")) {
1086 * #begin [ alternative | parallel ]
1088 if (!strcasecmp (ci
->ci_type
, "begin")) {
1089 if (!ci
->ci_magic
) {
1091 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
1092 } else if (!strcasecmp (ci
->ci_magic
, "alternative")) {
1093 vrsn
= MULTI_ALTERNATE
;
1094 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
1095 } else if (!strcasecmp (ci
->ci_magic
, "parallel")) {
1096 vrsn
= MULTI_PARALLEL
;
1097 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
1098 } else if (uprf (ci
->ci_magic
, "digest")) {
1101 vrsn
= MULTI_UNKNOWN
;
1106 snprintf (buffer
, sizeof(buffer
), "multipart/%s", cp
);
1107 if (get_ctinfo (buffer
, ct
, 0) == NOTOK
)
1109 ct
->c_type
= CT_MULTIPART
;
1110 ct
->c_subtype
= vrsn
;
1113 ct
->c_ctparams
= (void *) m
;
1116 while (fgetstr (buffer
, sizeof(buffer
) - 1, in
)) {
1120 if (user_content (in
, buffer
, &p
, infilename
) == DONE
) {
1122 die("empty \"#begin ... #end\" sequence");
1130 pp
= &part
->mp_next
;
1133 inform("premature end-of-file, missing #end, continuing...");
1140 die("unknown directive \"#%s\"", ci
->ci_type
);
1141 return NOTOK
; /* NOT REACHED */
1146 set_id (CT ct
, int top
)
1148 char contentid
[BUFSIZ
];
1150 static time_t clock
= 0;
1151 static char *msgfmt
;
1155 snprintf (contentid
, sizeof(contentid
), "%s\n", message_id (clock
, 1));
1157 msgfmt
= mh_xstrdup(contentid
);
1159 snprintf (contentid
, sizeof(contentid
), msgfmt
, top
? 0 : ++partno
);
1160 ct
->c_id
= mh_xstrdup(contentid
);
1165 * Fill out, or expand the various contents in the composition
1166 * draft. Read-in any necessary files. Parse and execute any
1167 * commands specified by profile composition strings.
1171 compose_content (CT ct
, int verbose
)
1173 CE ce
= &ct
->c_cefile
;
1175 switch (ct
->c_type
) {
1180 char partnam
[BUFSIZ
];
1181 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1185 snprintf (partnam
, sizeof(partnam
), "%s.", ct
->c_partno
);
1186 pp
= partnam
+ strlen (partnam
);
1191 /* first, we call compose_content on all the subparts */
1192 for (part
= m
->mp_parts
, partnum
= 1; part
; part
= part
->mp_next
, partnum
++) {
1193 CT p
= part
->mp_part
;
1195 sprintf (pp
, "%d", partnum
);
1196 p
->c_partno
= mh_xstrdup(partnam
);
1197 if (compose_content (p
, verbose
) == NOTOK
)
1202 * If the -rfc934mode switch is given, then check all
1203 * the subparts of a multipart/digest. If they are all
1204 * message/rfc822, then mark this content and all
1205 * subparts with the rfc934 compatibility mode flag.
1207 if (rfc934sw
&& ct
->c_subtype
== MULTI_DIGEST
) {
1210 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1211 CT p
= part
->mp_part
;
1213 if (p
->c_subtype
!= MESSAGE_RFC822
) {
1218 ct
->c_rfc934
= is934
;
1219 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1220 CT p
= part
->mp_part
;
1222 if ((p
->c_rfc934
= is934
))
1228 ct
->c_end
= (partnum
= strlen (prefix
) + 2) + 2;
1232 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
)
1233 ct
->c_end
+= part
->mp_part
->c_end
+ partnum
;
1239 /* Nothing to do for type message */
1243 * Discrete types (text/application/audio/image/video)
1251 char *vec
[4], buffer
[BUFSIZ
];
1253 CI ci
= &ct
->c_ctinfo
;
1256 if (!(cp
= ci
->ci_magic
))
1257 die("internal error(5)");
1259 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1260 adios("mhbuildsbr", "unable to create temporary file in %s",
1263 ce
->ce_file
= mh_xstrdup(tfile
);
1268 /* Get buffer ready to go */
1271 buflen
= sizeof(buffer
);
1274 * Parse composition string into buffer
1276 for ( ; *cp
; cp
++) {
1281 /* insert parameters from directive */
1285 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
1286 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
,
1287 pm
->pm_name
, get_param_value(pm
, '?'));
1297 /* %f, and stdout is not-redirected */
1303 * insert temporary filename where
1304 * content should be written
1306 snprintf (bp
, buflen
, "%s", ce
->ce_file
);
1310 /* insert content subtype */
1311 strncpy (bp
, ci
->ci_subtype
, buflen
);
1315 /* insert character % */
1336 printf ("composing content %s/%s from command\n\t%s\n",
1337 ci
->ci_type
, ci
->ci_subtype
, buffer
);
1339 fflush (stdout
); /* not sure if need for -noverbose */
1346 if ((out
= fopen (ce
->ce_file
, "w")) == NULL
)
1347 adios (ce
->ce_file
, "unable to open for writing");
1352 adios ("fork", "unable to fork");
1357 dup2 (fileno (out
), 1);
1358 close (fileno (out
));
1359 execvp ("/bin/sh", vec
);
1360 fprintf (stderr
, "unable to exec ");
1367 if (pidXwait(child_id
, NULL
))
1373 /* Check size of file */
1374 if (listsw
&& ct
->c_end
== 0L) {
1377 if (stat (ce
->ce_file
, &st
) != NOTOK
)
1378 ct
->c_end
= (long) st
.st_size
;
1390 * 1) choose a transfer encoding.
1391 * 2) check for clashes with multipart boundary string.
1392 * 3) for text content, figure out which character set is being used.
1394 * If there is a clash with one of the contents and the multipart boundary,
1395 * this function will exit with NOTOK. This will cause the scanning process
1396 * to be repeated with a different multipart boundary. It is possible
1397 * (although highly unlikely) that this scan will be repeated multiple times.
1401 scan_content (CT ct
, size_t maxunencoded
)
1404 bool check8bit
= false, contains8bit
= false; /* check if contains 8bit data */
1405 bool checknul
= false, containsnul
= false; /* check if contains NULs */
1406 bool checklinelen
= false, linelen
= false; /* check for long lines */
1407 bool checkllinelen
= false; /* check for extra-long lines */
1408 bool checkboundary
= false, boundaryclash
= false; /* check if clashes with multipart boundary */
1409 bool checklinespace
= false, linespace
= false; /* check if any line ends with space */
1414 struct text
*t
= NULL
;
1416 CE ce
= &ct
->c_cefile
;
1419 * handle multipart by scanning all subparts
1420 * and then checking their encoding.
1422 if (ct
->c_type
== CT_MULTIPART
) {
1423 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1426 /* initially mark the domain of enclosing multipart as 7bit */
1427 ct
->c_encoding
= CE_7BIT
;
1429 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1430 CT p
= part
->mp_part
;
1432 if (scan_content (p
, maxunencoded
) == NOTOK
) /* choose encoding for subpart */
1435 /* if necessary, enlarge encoding for enclosing multipart */
1436 if (p
->c_encoding
== CE_BINARY
)
1437 ct
->c_encoding
= CE_BINARY
;
1438 if (p
->c_encoding
== CE_8BIT
&& ct
->c_encoding
!= CE_BINARY
)
1439 ct
->c_encoding
= CE_8BIT
;
1446 * Decide what to check while scanning this content. Note that
1447 * for text content we always check for 8bit characters if the
1448 * charset is unspecified, because that controls whether or not the
1449 * character set is us-ascii or retrieved from the locale. And
1450 * we check even if the charset is specified, to allow setting
1451 * the proper Content-Transfer-Encoding.
1454 if (ct
->c_type
== CT_TEXT
) {
1455 t
= (struct text
*) ct
->c_ctparams
;
1456 if (t
->tx_charset
== CHARSET_UNSPECIFIED
) {
1462 switch (ct
->c_reqencoding
) {
1464 checkllinelen
= true;
1465 checkboundary
= true;
1468 checkboundary
= true;
1473 /* Use the default rules based on content-type */
1474 switch (ct
->c_type
) {
1476 checkboundary
= true;
1477 checklinelen
= true;
1478 if (ct
->c_subtype
== TEXT_PLAIN
) {
1479 checklinespace
= false;
1481 checklinespace
= true;
1485 case CT_APPLICATION
:
1488 checklinelen
= true;
1489 checklinespace
= true;
1490 checkboundary
= true;
1494 checklinelen
= false;
1495 checklinespace
= false;
1497 /* don't check anything for message/external */
1498 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1499 checkboundary
= false;
1502 checkboundary
= true;
1511 * Don't check anything for these types,
1512 * since we are forcing use of base64, unless
1513 * the content-type was specified by a mhbuild directive.
1516 checklinelen
= false;
1517 checklinespace
= false;
1518 checkboundary
= false;
1524 * Scan the unencoded content
1526 if (check8bit
|| checklinelen
|| checklinespace
|| checkboundary
||
1527 checkllinelen
|| checknul
) {
1528 if ((in
= fopen (ce
->ce_file
, "r")) == NULL
)
1529 adios (ce
->ce_file
, "unable to open for reading");
1530 prefix_len
= strlen (prefix
);
1532 while ((gotlen
= getline(&bufp
, &buflen
, in
)) != -1) {
1534 * Check for 8bit and NUL data.
1536 for (cp
= bufp
; (check8bit
|| checknul
) &&
1537 cp
< bufp
+ gotlen
; cp
++) {
1538 if (!isascii ((unsigned char) *cp
)) {
1539 contains8bit
= true;
1540 check8bit
= false; /* no need to keep checking */
1544 checknul
= false; /* no need to keep checking */
1549 * Check line length.
1551 if (checklinelen
&& ((size_t)gotlen
> maxunencoded
+ 1)) {
1553 checklinelen
= false; /* no need to keep checking */
1557 * RFC 5322 specifies that a message cannot contain a line
1558 * greater than 998 characters (excluding the CRLF). If we
1559 * get one of those lines and linelen is NOT set, then abort.
1562 if (checkllinelen
&& !linelen
&&
1563 (gotlen
> MAXLONGLINE
+ 1)) {
1564 die("Line in content exceeds maximum line limit (%d)",
1569 * Check if line ends with a space.
1571 if (checklinespace
&& (cp
= bufp
+ gotlen
- 2) > bufp
&&
1572 isspace ((unsigned char) *cp
)) {
1574 checklinespace
= false; /* no need to keep checking */
1578 * Check if content contains a line that clashes
1579 * with our standard boundary for multipart messages.
1581 if (checkboundary
&& bufp
[0] == '-' && bufp
[1] == '-') {
1582 for (cp
= bufp
+ gotlen
- 1; cp
>= bufp
; cp
--)
1583 if (!isspace ((unsigned char) *cp
))
1586 if (!strncmp(bufp
+ 2, prefix
, prefix_len
) &&
1587 isdigit((unsigned char) bufp
[2 + prefix_len
])) {
1588 boundaryclash
= true;
1589 checkboundary
= false; /* no need to keep checking */
1598 * If the content is text and didn't specify a character set,
1599 * we need to figure out which one was used.
1601 set_charset (ct
, contains8bit
);
1604 * Decide which transfer encoding to use.
1607 if (ct
->c_reqencoding
!= CE_UNKNOWN
)
1608 ct
->c_encoding
= ct
->c_reqencoding
;
1610 int wants_q_p
= (containsnul
|| linelen
|| linespace
|| checksw
);
1612 switch (ct
->c_type
) {
1615 ct
->c_encoding
= CE_QUOTED
;
1616 else if (contains8bit
)
1617 ct
->c_encoding
= CE_8BIT
;
1619 ct
->c_encoding
= CE_7BIT
;
1623 case CT_APPLICATION
:
1624 /* For application type, use base64, except when postscript */
1625 if (wants_q_p
|| contains8bit
) {
1626 if (ct
->c_subtype
== APPLICATION_POSTSCRIPT
)
1627 ct
->c_encoding
= CE_QUOTED
; /* historical */
1629 ct
->c_encoding
= CE_BASE64
;
1631 ct
->c_encoding
= CE_7BIT
;
1636 ct
->c_encoding
= contains8bit
? CE_8BIT
: CE_7BIT
;
1642 /* For audio, image, and video contents, just use base64 */
1643 ct
->c_encoding
= CE_BASE64
;
1648 return boundaryclash
? NOTOK
: OK
;
1653 * Scan the content structures, and build header
1654 * fields that will need to be output into the
1659 build_headers (CT ct
, int header_encoding
)
1661 int cc
, mailbody
, extbody
, len
;
1662 char *np
, *vp
, buffer
[BUFSIZ
];
1663 CI ci
= &ct
->c_ctinfo
;
1666 * If message is type multipart, then add the multipart
1667 * boundary to the list of attribute/value pairs.
1669 if (ct
->c_type
== CT_MULTIPART
) {
1670 static int level
= 0; /* store nesting level */
1672 snprintf (buffer
, sizeof(buffer
), "%s%d", prefix
, level
++);
1673 add_param(&ci
->ci_first_pm
, &ci
->ci_last_pm
, "boundary", buffer
, 0);
1677 * Skip the output of Content-Type, parameters, content
1678 * description and disposition, and Content-ID if the
1679 * content is of type "message" and the rfc934 compatibility
1680 * flag is set (which means we are inside multipart/digest
1681 * and the switch -rfc934mode was given).
1683 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_rfc934
)
1687 * output the content type and subtype
1689 np
= mh_xstrdup(TYPE_FIELD
);
1690 vp
= concat (" ", ci
->ci_type
, "/", ci
->ci_subtype
, NULL
);
1692 /* keep track of length of line */
1693 len
= LEN(TYPE_FIELD
) + strlen (ci
->ci_type
)
1694 + strlen (ci
->ci_subtype
) + 3;
1696 extbody
= ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_EXTERNAL
;
1697 mailbody
= extbody
&& ((struct exbody
*) ct
->c_ctparams
)->eb_body
;
1700 * Append the attribute/value pairs to
1701 * the end of the Content-Type line.
1704 if (ci
->ci_first_pm
) {
1705 char *s
= output_params(len
, ci
->ci_first_pm
, &len
, mailbody
);
1708 die("Internal error: failed outputting Content-Type "
1716 * Append any RFC-822 comment to the end of
1717 * the Content-Type line.
1719 if (ci
->ci_comment
) {
1720 snprintf (buffer
, sizeof(buffer
), "(%s)", ci
->ci_comment
);
1721 if (len
+ 1 + (cc
= 2 + strlen (ci
->ci_comment
)) >= CPERLIN
) {
1722 vp
= add ("\n\t", vp
);
1728 vp
= add (buffer
, vp
);
1731 vp
= add ("\n", vp
);
1732 add_header (ct
, np
, vp
);
1735 * output the Content-ID, unless disabled by -nocontentid. Note that
1736 * RFC 2045 always requires a Content-ID header for message/external-body
1739 if ((contentidsw
|| ct
->c_ctexbody
) && ct
->c_id
) {
1740 np
= mh_xstrdup(ID_FIELD
);
1741 vp
= concat (" ", ct
->c_id
, NULL
);
1742 add_header (ct
, np
, vp
);
1745 * output the Content-Description
1748 np
= mh_xstrdup(DESCR_FIELD
);
1749 vp
= concat (" ", ct
->c_descr
, NULL
);
1750 if (header_encoding
!= CE_8BIT
) {
1751 if (encode_rfc2047(DESCR_FIELD
, &vp
, header_encoding
, NULL
)) {
1752 die("Unable to encode %s header", DESCR_FIELD
);
1755 add_header (ct
, np
, vp
);
1759 * output the Content-Disposition. If it's NULL but c_dispo_type is
1760 * set, then we need to build it.
1763 np
= mh_xstrdup(DISPO_FIELD
);
1764 vp
= concat (" ", ct
->c_dispo
, NULL
);
1765 add_header (ct
, np
, vp
);
1766 } else if (ct
->c_dispo_type
) {
1767 vp
= concat (" ", ct
->c_dispo_type
, NULL
);
1768 len
= LEN(DISPO_FIELD
) + strlen(vp
) + 1;
1769 np
= output_params(len
, ct
->c_dispo_first
, NULL
, 0);
1773 add_header (ct
, mh_xstrdup(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
= mh_xstrdup(MD5_FIELD
);
1790 vp
= calculate_digest (ct
, ct
->c_encoding
== CE_QUOTED
);
1791 add_header (ct
, np
, vp
);
1795 * output the Content-Transfer-Encoding
1796 * If using EAI and message body is 7-bit, force 8-bit C-T-E.
1798 if (header_encoding
== CE_8BIT
&& ct
->c_encoding
== CE_7BIT
) {
1799 ct
->c_encoding
= CE_8BIT
;
1802 switch (ct
->c_encoding
) {
1804 /* Nothing to output */
1808 np
= mh_xstrdup(ENCODING_FIELD
);
1809 vp
= concat (" ", "8bit", "\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 (" ", "quoted-printable", "\n", NULL
);
1819 add_header (ct
, np
, vp
);
1823 if (ct
->c_type
== CT_MESSAGE
|| ct
->c_type
== CT_MULTIPART
)
1824 die("internal error, invalid encoding");
1826 np
= mh_xstrdup(ENCODING_FIELD
);
1827 vp
= concat (" ", "base64", "\n", NULL
);
1828 add_header (ct
, np
, vp
);
1832 if (ct
->c_type
== CT_MESSAGE
)
1833 die("internal error, invalid encoding");
1835 np
= mh_xstrdup(ENCODING_FIELD
);
1836 vp
= concat (" ", "binary", "\n", NULL
);
1837 add_header (ct
, np
, vp
);
1841 die("unknown transfer encoding in content");
1846 * Additional content specific header processing
1848 switch (ct
->c_type
) {
1851 struct multipart
*m
;
1854 m
= (struct multipart
*) ct
->c_ctparams
;
1855 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1859 build_headers (p
, header_encoding
);
1865 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1868 e
= (struct exbody
*) ct
->c_ctparams
;
1869 build_headers (e
->eb_content
, header_encoding
);
1882 static char nib2b64
[0x40+1] =
1883 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1886 calculate_digest (CT ct
, int asciiP
)
1891 unsigned char digest
[16];
1892 unsigned char outbuf
[25];
1894 CE ce
= &ct
->c_cefile
;
1895 char *infilename
= ce
->ce_file
? ce
->ce_file
: ct
->c_file
;
1899 if ((in
= fopen (infilename
, "r")) == NULL
)
1900 adios (infilename
, "unable to open for reading");
1902 /* Initialize md5 context */
1903 MD5Init (&mdContext
);
1905 /* calculate md5 message digest */
1910 while ((gotlen
= getline(&bufp
, &buflen
, in
)) != -1) {
1913 cp
= bufp
+ gotlen
- 1;
1914 if ((c
= *cp
) == '\n')
1917 MD5Update (&mdContext
, (unsigned char *) bufp
,
1918 (unsigned int) gotlen
);
1921 MD5Update (&mdContext
, (unsigned char *) "\r\n", 2);
1924 char buffer
[BUFSIZ
];
1925 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), in
)) > 0)
1926 MD5Update (&mdContext
, (unsigned char *) buffer
, (unsigned int) cc
);
1929 /* md5 finalization. Write digest and zero md5 context */
1930 MD5Final (digest
, &mdContext
);
1935 /* print debugging info */
1939 fprintf (stderr
, "MD5 digest=");
1940 for (ep
= (dp
= digest
) + sizeof digest
;
1942 fprintf (stderr
, "%02x", *dp
& 0xff);
1943 fprintf (stderr
, "\n");
1946 /* encode the digest using base64 */
1947 for (dp
= digest
, op
= (char *) outbuf
,
1949 cc
> 0; cc
-= 3, op
+= 4) {
1953 bits
= (*dp
++ & 0xff) << 16;
1955 bits
|= (*dp
++ & 0xff) << 8;
1957 bits
|= *dp
++ & 0xff;
1960 for (bp
= op
+ 4; bp
> op
; bits
>>= 6)
1961 *--bp
= nib2b64
[bits
& 0x3f];
1969 /* null terminate string */
1972 /* now make copy and return string */
1973 vp
= concat (" ", outbuf
, "\n", NULL
);
1978 * Set things up for the content structure for file "filename" that
1983 setup_attach_content(CT ct
, char *filename
)
1985 char *type
, *simplename
= r1bindex(filename
, '/');
1986 struct str2init
*s2i
;
1989 if (! (type
= mime_type(filename
))) {
1990 die("Unable to determine MIME type of \"%s\"", filename
);
1994 * Parse the Content-Type. get_ctinfo() parses MIME parameters, but
1995 * since we're just feeding it a MIME type we have to add those ourselves.
1996 * Map that to a valid content-type label and call any initialization
2000 if (get_ctinfo(type
, ct
, 0) == NOTOK
)
2005 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
2006 if (strcasecmp(ct
->c_ctinfo
.ci_type
, s2i
->si_key
) == 0)
2008 if (!s2i
->si_key
&& !uprf(ct
->c_ctinfo
.ci_type
, "X-"))
2012 * Make sure the type isn't incompatible with what we can handle
2015 switch (ct
->c_type
= s2i
->si_val
) {
2017 die("multipart types must be specified by mhbuild directives");
2021 if (strcasecmp(ct
->c_ctinfo
.ci_subtype
, "partial") == 0)
2022 die("Sorry, %s/%s isn't supported", ct
->c_ctinfo
.ci_type
,
2023 ct
->c_ctinfo
.ci_subtype
);
2024 if (strcasecmp(ct
->c_ctinfo
.ci_subtype
, "external-body") == 0)
2025 die("external-body messages must be specified "
2026 "by mhbuild directives");
2031 * This sets the subtype, if it's significant
2033 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
2034 (*ct
->c_ctinitfnx
)(ct
);
2039 * Feed in a few attributes; specifically, the name attribute, the
2040 * content-description, and the content-disposition.
2043 for (pm
= ct
->c_ctinfo
.ci_first_pm
; pm
; pm
= pm
->pm_next
) {
2044 if (strcasecmp(pm
->pm_name
, "name") == 0) {
2046 pm
->pm_value
= mh_xstrdup(simplename
);
2052 add_param(&ct
->c_ctinfo
.ci_first_pm
, &ct
->c_ctinfo
.ci_last_pm
,
2053 "name", simplename
, 0);
2055 ct
->c_descr
= mh_xstrdup(simplename
);
2056 ct
->c_descr
= add("\n", ct
->c_descr
);
2057 ct
->c_cefile
.ce_file
= mh_xstrdup(filename
);
2059 set_disposition (ct
);
2061 add_param(&ct
->c_dispo_first
, &ct
->c_dispo_last
, "filename", simplename
, 0);
2065 * If disposition type hasn't already been set in ct:
2066 * Look for mhbuild-disposition-<type>/<subtype> entry
2067 * that specifies Content-Disposition type. Only
2068 * 'attachment' and 'inline' are allowed. Default to
2072 set_disposition (CT ct
)
2074 if (ct
->c_dispo_type
== NULL
) {
2075 char *cp
= context_find_by_type ("disposition", ct
->c_ctinfo
.ci_type
,
2076 ct
->c_ctinfo
.ci_subtype
);
2078 if (cp
&& strcasecmp (cp
, "attachment") &&
2079 strcasecmp (cp
, "inline")) {
2080 inform("configuration problem: %s-disposition-%s%s%s specifies "
2081 "'%s' but only 'attachment' and 'inline' are allowed, "
2082 "continuing...", invo_name
,
2083 ct
->c_ctinfo
.ci_type
,
2084 ct
->c_ctinfo
.ci_subtype
? "/" : "",
2085 FENDNULL(ct
->c_ctinfo
.ci_subtype
),
2091 ct
->c_dispo_type
= mh_xstrdup(cp
);
2096 * Set text content charset if it was unspecified. contains8bit
2098 * 0: content does not contain 8-bit characters
2099 * 1: content contains 8-bit characters
2100 * -1: ignore content and use user's locale to determine charset
2103 set_charset (CT ct
, int contains8bit
)
2105 if (ct
->c_type
== CT_TEXT
) {
2108 if (ct
->c_ctparams
== NULL
) {
2111 t
->tx_charset
= CHARSET_UNSPECIFIED
;
2113 t
= (struct text
*) ct
->c_ctparams
;
2116 if (t
->tx_charset
== CHARSET_UNSPECIFIED
) {
2117 CI ci
= &ct
->c_ctinfo
;
2118 char *eightbitcharset
= write_charset_8bit();
2119 char *charset
= contains8bit
? eightbitcharset
: "us-ascii";
2121 if (contains8bit
== 1 &&
2122 strcasecmp (eightbitcharset
, "US-ASCII") == 0) {
2123 die("Text content contains 8 bit characters, but "
2124 "character set is US-ASCII");
2127 add_param (&ci
->ci_first_pm
, &ci
->ci_last_pm
, "charset", charset
,
2130 t
->tx_charset
= CHARSET_SPECIFIED
;
2137 * Look at all of the replied-to message parts and expand any that
2138 * are matched by a pseudoheader. Except don't descend into
2142 expand_pseudoheaders (CT ct
, struct multipart
*m
, const char *infile
,
2143 const convert_list
*convert_head
)
2145 /* text_plain_ct is used to concatenate all of the text/plain
2146 replies into one part, instead of having each one in a separate
2148 CT text_plain_ct
= NULL
;
2150 switch (ct
->c_type
) {
2151 case CT_MULTIPART
: {
2152 struct multipart
*mp
= (struct multipart
*) ct
->c_ctparams
;
2155 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
2156 bool matched
= false;
2158 /* The parts are in descending priority order (defined by
2159 RFC 2046 Sec. 5.1.4) because they were reversed by
2160 parse_mime (). So, stop looking for matches with
2161 immediate subparts after the first match of an
2163 for (part
= mp
->mp_parts
; ! matched
&& part
; part
= part
->mp_next
) {
2164 char *type_subtype
=
2165 concat (part
->mp_part
->c_ctinfo
.ci_type
, "/",
2166 part
->mp_part
->c_ctinfo
.ci_subtype
, NULL
);
2168 if (part
->mp_part
->c_type
== CT_MULTIPART
) {
2169 expand_pseudoheaders (part
->mp_part
, m
, infile
,
2172 const convert_list
*c
;
2174 for (c
= convert_head
; c
; c
= c
->next
) {
2175 if (! strcasecmp (type_subtype
, c
->type
)) {
2176 expand_pseudoheader (part
->mp_part
, &text_plain_ct
,
2178 c
->type
, c
->argstring
);
2184 free (type_subtype
);
2187 for (part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
2188 expand_pseudoheaders (part
->mp_part
, m
, infile
, convert_head
);
2195 char *type_subtype
=
2196 concat (ct
->c_ctinfo
.ci_type
, "/", ct
->c_ctinfo
.ci_subtype
,
2198 const convert_list
*c
;
2200 for (c
= convert_head
; c
; c
= c
->next
) {
2201 if (! strcasecmp (type_subtype
, c
->type
)) {
2202 expand_pseudoheader (ct
, &text_plain_ct
, m
, infile
, c
->type
,
2207 free (type_subtype
);
2215 * Expand a single pseudoheader. It's for the specified type.
2218 expand_pseudoheader (CT ct
, CT
*text_plain_ct
, struct multipart
*m
,
2219 const char *infile
, const char *type
,
2220 const char *argstring
)
2223 FILE *reply_fp
= NULL
;
2224 char *convert
, *type_p
, *subtype_p
;
2225 char *convert_command
;
2226 char *charset
= NULL
;
2228 struct str2init
*s2i
;
2233 type_p
= getcpy (type
);
2234 if ((subtype_p
= strchr (type_p
, '/'))) {
2235 *subtype_p
++ = '\0';
2236 convert
= context_find_by_type ("convert", type_p
, subtype_p
);
2239 type_p
= concat ("mhbuild-convert-", type
, NULL
);
2240 convert
= context_find (type_p
);
2245 /* No mhbuild-convert- entry in mhn.defaults or profile for type. */
2248 /* reply_file is used to pass the output of the convert. */
2249 reply_file
= getcpy (m_mktemp2 (NULL
, invo_name
, NULL
, NULL
));
2251 concat (convert
, " ", FENDNULL(argstring
), " >", reply_file
, NULL
);
2253 /* Convert here . . . */
2254 ct
->c_storeproc
= mh_xstrdup(convert_command
);
2255 ct
->c_umask
= ~m_gmprot ();
2257 if ((status
= show_content_aux (ct
, 0, convert_command
, NULL
, NULL
)) !=
2259 inform("store of %s content failed, continuing...", type
);
2261 free (convert_command
);
2263 /* Fill out the the new ct, reply_ct. */
2265 init_decoded_content (reply_ct
, infile
);
2267 if (extract_headers (reply_ct
, reply_file
, &reply_fp
) == NOTOK
) {
2268 inform("failed to extract headers from convert output in %s, "
2269 "continuing...", reply_file
);
2274 /* This sets reply_ct->c_ctparams, and reply_ct->c_termproc if the
2275 charset can't be handled natively. */
2276 for (s2i
= str2cts
; s2i
->si_key
; s2i
++) {
2277 if (strcasecmp(reply_ct
->c_ctinfo
.ci_type
, s2i
->si_key
) == 0) {
2282 if ((reply_ct
->c_ctinitfnx
= s2i
->si_init
)) {
2283 (*reply_ct
->c_ctinitfnx
)(reply_ct
);
2286 if ((cp
= get_param (reply_ct
->c_ctinfo
.ci_first_pm
, "charset", '?', 1))) {
2287 /* The reply Content-Type had the charset. */
2290 set_charset (reply_ct
, -1);
2291 charset
= get_param (reply_ct
->c_ctinfo
.ci_first_pm
, "charset", '?', 1);
2294 /* Concatenate text/plain parts. */
2295 if (reply_ct
->c_type
== CT_TEXT
&& 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 part. */
2301 set_charset (*text_plain_ct
, -1);
2304 if (*text_plain_ct
) {
2305 /* Only concatenate if the charsets are identical. */
2306 char *text_plain_ct_charset
=
2307 get_param ((*text_plain_ct
)->c_ctinfo
.ci_first_pm
, "charset",
2310 if (strcasecmp (text_plain_ct_charset
, charset
) == 0) {
2311 /* Append this text/plain reply to the first one.
2312 If there's a problem anywhere along the way,
2313 instead attach it is a separate part. */
2314 int text_plain_reply
=
2315 open ((*text_plain_ct
)->c_cefile
.ce_file
,
2316 O_WRONLY
| O_APPEND
);
2317 int addl_reply
= open (reply_file
, O_RDONLY
);
2319 if (text_plain_reply
!= NOTOK
&& addl_reply
!= NOTOK
) {
2320 /* Insert blank line before each addl part. */
2321 /* It would be nice not to do this for the first one. */
2322 if (write (text_plain_reply
, "\n", 1) == 1) {
2323 /* Copy the text from the new reply and
2324 then free its Content struct. */
2325 cpydata (addl_reply
, text_plain_reply
,
2326 (*text_plain_ct
)->c_cefile
.ce_file
,
2328 if (close (text_plain_reply
) == OK
&&
2329 close (addl_reply
) == OK
) {
2330 /* If appended text needed 8-bit but first text didn't,
2331 propagate the 8-bit indication. */
2332 if ((*text_plain_ct
)->c_reqencoding
== CE_7BIT
&&
2333 reply_ct
->c_reqencoding
== CE_8BIT
) {
2334 (*text_plain_ct
)->c_reqencoding
= CE_8BIT
;
2337 if (reply_fp
) { fclose (reply_fp
); }
2339 free_content (reply_ct
);
2346 *text_plain_ct
= reply_ct
;
2350 reply_ct
->c_cefile
.ce_file
= reply_file
;
2351 reply_ct
->c_cefile
.ce_fp
= reply_fp
;
2352 reply_ct
->c_cefile
.ce_unlink
= 1;
2354 /* Attach the new part to the parent multipart/mixed, "m". */
2356 part
->mp_part
= reply_ct
;
2360 for (p
= m
->mp_parts
; p
&& p
->mp_next
; p
= p
->mp_next
) { continue; }
2368 /* Extract any Content-Type header from beginning of convert output. */
2370 extract_headers (CT ct
, char *reply_file
, FILE **reply_fp
)
2372 char *buffer
= NULL
, *cp
, *end_of_header
;
2373 bool found_header
= false;
2374 struct stat statbuf
;
2376 /* Read the convert reply from the file to memory. */
2377 if (stat (reply_file
, &statbuf
) == NOTOK
) {
2378 admonish (reply_file
, "failed to stat");
2379 goto failed_to_extract_ct
;
2382 buffer
= mh_xmalloc (statbuf
.st_size
+ 1);
2384 if ((*reply_fp
= fopen (reply_file
, "r+")) == NULL
||
2385 fread (buffer
, 1, (size_t) statbuf
.st_size
, *reply_fp
) <
2386 (size_t) statbuf
.st_size
) {
2387 admonish (reply_file
, "failed to read");
2388 goto failed_to_extract_ct
;
2390 buffer
[statbuf
.st_size
] = '\0';
2392 /* Look for a header in the convert reply. */
2393 if (strncasecmp (buffer
, TYPE_FIELD
, LEN(TYPE_FIELD
)) == 0 &&
2394 buffer
[LEN(TYPE_FIELD
)] == ':') {
2395 if ((end_of_header
= strstr (buffer
, "\r\n\r\n"))) {
2397 found_header
= true;
2398 } else if ((end_of_header
= strstr (buffer
, "\n\n"))) {
2400 found_header
= true;
2410 /* Truncate buffer to just the C-T. */
2411 *end_of_header
= '\0';
2412 n
= strlen (buffer
);
2414 if (get_ctinfo (buffer
+ 14, ct
, 0) != OK
) {
2415 inform("unable to get content info for reply, continuing...");
2416 goto failed_to_extract_ct
;
2419 /* Hack. Use parse_mime() to detect the type/subtype of the
2420 reply, which we'll use below. */
2421 tmp_file
= getcpy (m_mktemp2 (NULL
, invo_name
, NULL
, NULL
));
2422 tmp_f
= fopen(tmp_file
, "w");
2424 goto failed_to_extract_ct
;
2425 written
= fwrite(buffer
, 1, n
, tmp_f
);
2428 goto failed_to_extract_ct
;
2430 tmp_ct
= parse_mime (tmp_file
);
2432 /* The type and subtype were detected from the reply
2433 using parse_mime() above. */
2434 ct
->c_type
= tmp_ct
->c_type
;
2435 ct
->c_subtype
= tmp_ct
->c_subtype
;
2436 free_content (tmp_ct
);
2441 /* Rewrite the content without the header. */
2442 cp
= end_of_header
+ 1;
2445 if (fwrite (cp
, 1, statbuf
.st_size
- (cp
- buffer
), *reply_fp
) <
2446 (size_t) (statbuf
.st_size
- (cp
- buffer
))) {
2447 admonish (reply_file
, "failed to write");
2448 goto failed_to_extract_ct
;
2451 if (ftruncate (fileno (*reply_fp
), statbuf
.st_size
- (cp
- buffer
)) !=
2453 advise (reply_file
, "ftruncate");
2454 goto failed_to_extract_ct
;
2457 /* No header section, assume the reply is text/plain. */
2458 ct
->c_type
= CT_TEXT
;
2459 ct
->c_subtype
= TEXT_PLAIN
;
2460 if (get_ctinfo ("text/plain", ct
, 0) == NOTOK
) {
2461 /* This never should fail, but just in case. */
2462 die("unable to get content info for reply");
2466 /* free_encoding() will close reply_fp, which is passed through
2467 ct->c_cefile.ce_fp. */
2471 failed_to_extract_ct
:
2472 if (*reply_fp
) { fclose (*reply_fp
); }