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"
40 #include "h/fmt_scan.h"
42 #include "h/mhparse.h"
46 #include "sbr/m_mktemp.h"
47 #include "sbr/message_id.h"
48 #include "sbr/mime_type.h"
50 #include "mhshowsbr.h"
52 #ifdef HAVE_SYS_TIME_H
53 # include <sys/time.h>
62 extern bool contentidsw
;
64 static char prefix
[] = "----- =_aaaaaaaaaa";
68 struct attach_list
*next
;
71 typedef struct convert_list
{
75 struct convert_list
*next
;
82 static int init_decoded_content (CT
, const char *);
83 static void setup_attach_content(CT
, char *);
84 static void set_disposition (CT
);
85 static void set_charset (CT
, int);
86 static void expand_pseudoheaders (CT
, struct multipart
*, const char *,
87 const convert_list
*);
88 static void expand_pseudoheader (CT
, CT
*, struct multipart
*, const char *,
89 const char *, const char *);
90 static char *fgetstr (char *, int, FILE *);
91 static int user_content (FILE *, char *, CT
*, const char *infilename
);
92 static void set_id (CT
, int);
93 static int compose_content (CT
, int);
94 static int scan_content (CT
, size_t);
95 static int build_headers (CT
, int);
96 static int extract_headers (CT
, char *, FILE **);
99 static unsigned char directives_stack
[32];
100 static unsigned int directives_index
;
105 return directives_stack
[directives_index
];
109 directive_onoff(int onoff
)
111 if (directives_index
>= sizeof(directives_stack
) - 1) {
112 fprintf(stderr
, "mhbuild: #on/off overflow, continuing\n");
115 directives_stack
[++directives_index
] = onoff
;
119 directive_init(int onoff
)
121 directives_index
= 0;
122 directives_stack
[0] = onoff
;
128 if (directives_index
> 0)
131 fprintf(stderr
, "mhbuild: #pop underflow, continuing\n");
135 * Main routine for translating composition file
136 * into valid MIME message. It translates the draft
137 * into a content structure (actually a tree of content
138 * structures). This message then can be manipulated
139 * in various ways, including being output via
144 build_mime (char *infile
, int autobuild
, int dist
, int directives
,
145 int header_encoding
, size_t maxunencoded
, int verbose
)
148 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
155 m_getfld_state_t gstate
;
156 struct attach_list
*attach_head
= NULL
, *attach_tail
= NULL
, *at_entry
;
157 convert_list
*convert_head
= NULL
, *convert_tail
= NULL
, *convert
;
159 directive_init(directives
);
161 umask (~m_gmprot ());
163 /* open the composition draft */
164 if ((in
= fopen (infile
, "r")) == NULL
)
165 adios (infile
, "unable to open for reading");
168 * Allocate space for primary (outside) content
173 * Allocate structure for handling decoded content
174 * for this part. We don't really need this, but
175 * allocate it to remain consistent.
177 init_decoded_content (ct
, infile
);
180 * Parse some of the header fields in the composition
181 * draft into the linked list of header fields for
182 * the new MIME message.
184 gstate
= m_getfld_state_init(in
);
185 m_getfld_track_filepos2(&gstate
);
186 for (compnum
= 1;;) {
187 int bufsz
= sizeof buf
;
188 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
194 * If we are running with autobuild set, then silently
195 * exit if we find a MIME-Version header. For any other MIME
196 * header, return an error.
198 * RFC 2045, Section 9 says that any valid MIME header should
199 * start with "Content-", so we will match on that rather than
200 * enumerate all current MIME headers.
202 * Because the headers could be in any order, just check for
203 * MIME-Version here; check for Content-* later.
206 if (strcasecmp (name
, VRSN_FIELD
) == 0) {
212 die("draft shouldn't contain %s: field", name
);
215 /* get copies of the buffers */
216 np
= mh_xstrdup(name
);
217 vp
= mh_xstrdup(buf
);
219 /* if necessary, get rest of field */
220 while (state
== FLDPLUS
) {
222 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
223 vp
= add (buf
, vp
); /* add to previous value */
227 * Now add the header data to the list, unless it's an attach
228 * header; in that case, add it to our attach list
231 if (strcasecmp(ATTACH_FIELD
, np
) == 0 ||
232 strcasecmp(ATTACH_FIELD_ALT
, np
) == 0) {
233 struct attach_list
*entry
;
234 char *s
= vp
, *e
= vp
+ strlen(vp
) - 1;
238 * Make sure we can find the start of this filename.
239 * If it's blank, we skip completely. Otherwise, strip
240 * off any leading spaces and trailing newlines.
243 while (isspace((unsigned char) *s
))
246 while (e
> s
&& *e
== '\n')
255 entry
->filename
= mh_xstrdup(s
);
260 attach_tail
->next
= entry
;
263 attach_head
= attach_tail
= entry
;
265 } else if (strncasecmp(MHBUILD_FILE_PSEUDOHEADER
, np
,
266 LEN(MHBUILD_FILE_PSEUDOHEADER
)) == 0) {
268 * Nmh-mhbuild-file-text/calendar: /home/user/Mail/inbox/9
270 char *type
= np
+ LEN(MHBUILD_FILE_PSEUDOHEADER
);
273 /* vp should begin with a space because m_getfld2()
274 includes the space after the colon in buf. */
275 while (isspace((unsigned char) *filename
)) { ++filename
; }
276 /* Trim trailing newline and any other whitespace. */
279 for (convert
= convert_head
; convert
; convert
= convert
->next
) {
280 if (strcasecmp (convert
->type
, type
) == 0) { break; }
283 if (convert
->filename
&&
284 strcasecmp (convert
->filename
, filename
)) {
285 die("Multiple %s headers with different files"
286 " not allowed", type
);
288 convert
->filename
= mh_xstrdup(filename
);
292 convert
->filename
= mh_xstrdup(filename
);
293 convert
->type
= mh_xstrdup(type
);
296 convert_tail
->next
= convert
;
298 convert_head
= convert
;
300 convert_tail
= convert
;
305 } else if (strncasecmp(MHBUILD_ARGS_PSEUDOHEADER
, np
,
306 LEN(MHBUILD_ARGS_PSEUDOHEADER
)) == 0) {
308 * Nmh-mhbuild-args-text/calendar: -reply accept
310 char *type
= np
+ LEN(MHBUILD_ARGS_PSEUDOHEADER
);
311 char *argstring
= vp
;
313 /* vp should begin with a space because m_getfld2()
314 includes the space after the colon in buf. */
315 while (isspace((unsigned char) *argstring
)) { ++argstring
; }
316 /* Trim trailing newline and any other whitespace. */
319 for (convert
= convert_head
; convert
; convert
= convert
->next
) {
320 if (strcasecmp (convert
->type
, type
) == 0) { break; }
323 if (convert
->argstring
&&
324 strcasecmp (convert
->argstring
, argstring
)) {
325 die("Multiple %s headers with different "
326 "argstrings not allowed", type
);
328 convert
->argstring
= mh_xstrdup(argstring
);
332 convert
->type
= mh_xstrdup(type
);
333 convert
->argstring
= mh_xstrdup(argstring
);
336 convert_tail
->next
= convert
;
338 convert_head
= convert
;
340 convert_tail
= convert
;
346 add_header (ct
, np
, vp
);
350 /* if this wasn't the last header field, then continue */
354 fseek (in
, (long) (-strlen (buf
)), SEEK_CUR
);
361 die("message format error in component #%d", compnum
);
364 die("getfld() returned %d", state
);
368 m_getfld_state_destroy (&gstate
);
371 * If we see any Content-* headers at this point, it is an error.
374 for (hp
= ct
->c_first_hf
; hp
!= NULL
; hp
= hp
->next
) {
375 if (uprf (hp
->name
, "Content-")) {
376 die ("draft shouldn't contain %s: field", hp
->name
);
380 if (header_encoding
!= CE_8BIT
) {
382 * Iterate through the list of headers and call the function to MIME-ify
386 for (hp
= ct
->c_first_hf
; hp
!= NULL
; hp
= hp
->next
) {
387 if (encode_rfc2047(hp
->name
, &hp
->value
, header_encoding
, NULL
)) {
388 die("Unable to encode header \"%s\"", hp
->name
);
394 * Now add the MIME-Version header field
395 * to the list of header fields.
399 np
= mh_xstrdup(VRSN_FIELD
);
400 vp
= concat (" ", VRSN_VALUE
, "\n", NULL
);
401 add_header (ct
, np
, vp
);
405 * We initially assume we will find multiple contents in the
406 * draft. So create a multipart/mixed content to hold everything.
407 * We can remove this later, if it is not needed.
409 if (get_ctinfo ("multipart/mixed", ct
, 0) == NOTOK
)
411 ct
->c_type
= CT_MULTIPART
;
412 ct
->c_subtype
= MULTI_MIXED
;
415 ct
->c_ctparams
= (void *) m
;
419 * read and parse the composition file
420 * and the directives it contains.
422 while (fgetstr (buf
, sizeof(buf
) - 1, in
)) {
426 if (user_content (in
, buf
, &p
, infile
) == DONE
) {
427 inform("ignoring spurious #end, continuing...");
440 * Add any Attach headers to the list of MIME parts at the end of the
444 for (at_entry
= attach_head
; at_entry
; ) {
445 struct attach_list
*at_prev
= at_entry
;
449 if (access(at_entry
->filename
, R_OK
) != 0) {
450 adios("reading", "Unable to open %s for", at_entry
->filename
);
454 init_decoded_content(p
, infile
);
457 * Initialize our content structure based on the filename,
458 * and fill in all of the relevant fields. Also place MIME
459 * parameters in the attributes array.
462 setup_attach_content(p
, at_entry
->filename
);
469 at_entry
= at_entry
->next
;
470 free(at_prev
->filename
);
475 * Handle the mhbuild pseudoheaders, which deal with specific
482 set_done(freects_done
);
484 /* In case there are multiple calls that land here, prevent leak. */
485 for (ctp
= cts
; ctp
&& *ctp
; ++ctp
) { free_content (*ctp
); }
488 /* Extract the type part (as a CT) from filename. */
489 cts
= mh_xcalloc(2, sizeof *cts
);
490 if (! (cts
[0] = parse_mime (convert_head
->filename
))) {
491 die("failed to parse %s", convert_head
->filename
);
494 expand_pseudoheaders (cts
[0], m
, infile
, convert_head
);
496 /* Free the convert list. */
497 for (convert
= convert_head
; convert
; convert
= next
) {
498 next
= convert
->next
;
499 free (convert
->type
);
500 free (convert
->filename
);
501 free (convert
->argstring
);
508 * To allow for empty message bodies, if we've found NO content at all
509 * yet cook up an empty text/plain part.
518 init_decoded_content(p
, infile
);
520 if (get_ctinfo ("text/plain", p
, 0) == NOTOK
)
524 p
->c_subtype
= TEXT_PLAIN
;
525 p
->c_encoding
= CE_7BIT
;
527 * Sigh. ce_file contains the "decoded" contents of this part.
528 * So this seems like the best option available since we're going
529 * to call scan_content() on this.
531 p
->c_cefile
.ce_file
= mh_xstrdup("/dev/null");
532 p
->c_begin
= ftell(in
);
533 p
->c_end
= ftell(in
);
536 t
->tx_charset
= CHARSET_SPECIFIED
;
545 * close the composition draft since
546 * it's not needed any longer.
551 * If only one content was found, then remove and
552 * free the outer multipart content.
554 if (!m
->mp_parts
->mp_next
) {
557 p
= m
->mp_parts
->mp_part
;
558 m
->mp_parts
->mp_part
= NULL
;
560 /* move header fields */
561 p
->c_first_hf
= ct
->c_first_hf
;
562 p
->c_last_hf
= ct
->c_last_hf
;
563 ct
->c_first_hf
= NULL
;
564 ct
->c_last_hf
= NULL
;
573 * Fill out, or expand directives. Parse and execute
574 * commands specified by profile composition strings.
576 compose_content (ct
, verbose
);
578 if ((cp
= strchr(prefix
, 'a')) == NULL
)
579 die("internal error(4)");
582 * If using EAI, force 8-bit charset.
584 if (header_encoding
== CE_8BIT
) {
589 * Scan the contents. Choose a transfer encoding, and
590 * check if prefix for multipart boundary clashes with
591 * any of the contents.
593 while (scan_content (ct
, maxunencoded
) == NOTOK
) {
598 die("giving up trying to find a unique delimiter string");
603 /* Build the rest of the header field structures */
605 build_headers (ct
, header_encoding
);
612 * Set up structures for placing unencoded
613 * content when building parts.
617 init_decoded_content (CT ct
, const char *filename
)
619 ct
->c_ceopenfnx
= open7Bit
; /* since unencoded */
620 ct
->c_ceclosefnx
= close_encoding
;
621 ct
->c_cesizefnx
= NULL
; /* since unencoded */
622 ct
->c_encoding
= CE_7BIT
; /* Seems like a reasonable default */
623 ct
->c_file
= mh_xstrdup(FENDNULL(filename
));
630 fgetstr (char *s
, int n
, FILE *stream
)
636 for (cp
= s
; cp
< ep
;) {
639 if (!fgets (cp
, n
, stream
))
640 return cp
== s
? NULL
: s
; /* "\\\nEOF" ignored. */
642 if (! do_direct() || (cp
== s
&& *cp
!= '#'))
643 return s
; /* Plaintext line. */
647 break; /* Can't contain "\\\n". */
648 cp
+= len
- 1; /* Just before NUL. */
649 if (*cp
-- != '\n' || *cp
!= '\\')
651 *cp
= '\0'; /* Erase the trailing "\\\n". */
655 if (strcmp(s
, "#on\n") == 0) {
657 } else if (strcmp(s
, "#off\n") == 0) {
659 } else if (strcmp(s
, "#pop\n") == 0) {
669 * Parse the composition draft for text and directives.
670 * Do initial setup of Content structure.
674 user_content (FILE *in
, char *buf
, CT
*ctp
, const char *infilename
)
678 char buffer
[NMH_BUFSIZ
];
682 struct str2init
*s2i
;
687 if (buf
[0] == '\n' || (do_direct() && strcmp (buf
, "#\n") == 0)) {
692 /* allocate basic Content structure */
696 /* allocate basic structure for handling decoded content */
697 init_decoded_content (ct
, infilename
);
704 * Handle inline text. Check if line
705 * is one of the following forms:
707 * 1) doesn't begin with '#' (implicit directive)
708 * 2) begins with "##" (implicit directive)
709 * 3) begins with "#<"
711 if (!do_direct() || buf
[0] != '#' || buf
[1] == '#' || buf
[1] == '<') {
715 char content
[BUFSIZ
];
719 if ((cp
= m_mktemp2(NULL
, invo_name
, NULL
, &out
)) == NULL
) {
720 adios("mhbuildsbr", "unable to create temporary file in %s",
724 /* use a temp file to collect the plain text lines */
725 ce
->ce_file
= mh_xstrdup(cp
);
728 if (do_direct() && (buf
[0] == '#' && buf
[1] == '<')) {
729 strncpy (content
, buf
+ 2, sizeof(content
) - 1);
735 /* the directive is implicit */
736 strncpy (content
, "text/plain", sizeof(content
));
738 strncpy (buffer
, (!do_direct() || buf
[0] != '#') ? buf
: buf
+ 1,
743 if (headers
>= 0 && do_direct() && uprf (buffer
, DESCR_FIELD
)
744 && buffer
[i
= LEN(DESCR_FIELD
)] == ':') {
748 ct
->c_descr
= add (buffer
+ i
+ 1, ct
->c_descr
);
749 if (!fgetstr (buffer
, sizeof(buffer
) - 1, in
))
750 die("end-of-file after %s: field in plaintext", DESCR_FIELD
);
758 die("#-directive after %s: field in plaintext", DESCR_FIELD
);
766 if (headers
>= 0 && do_direct() && uprf (buffer
, DISPO_FIELD
)
767 && buffer
[i
= LEN(DISPO_FIELD
)] == ':') {
771 ct
->c_dispo
= add (buffer
+ i
+ 1, ct
->c_dispo
);
772 if (!fgetstr (buffer
, sizeof(buffer
) - 1, in
))
773 die("end-of-file after %s: field in plaintext", DISPO_FIELD
);
781 die("#-directive after %s: field in plaintext", DISPO_FIELD
);
789 if (headers
!= 1 || buffer
[0] != '\n')
795 if ((cp
= fgetstr (buffer
, sizeof(buffer
) - 1, in
)) == NULL
)
797 if (do_direct() && buffer
[0] == '#') {
800 if (buffer
[1] != '#')
802 for (cp
= (bp
= buffer
) + 1; *cp
; cp
++)
809 ct
->c_end
= ftell (out
);
812 /* parse content type */
813 if (get_ctinfo (content
, ct
, inlineD
) == NOTOK
)
816 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
817 if (!strcasecmp (ci
->ci_type
, s2i
->si_key
))
819 if (!s2i
->si_key
&& !uprf (ci
->ci_type
, "X-"))
823 * check type specified (possibly implicitly)
825 switch (ct
->c_type
= s2i
->si_val
) {
827 if (!strcasecmp (ci
->ci_subtype
, "rfc822")) {
828 ct
->c_encoding
= CE_7BIT
;
833 die("it doesn't make sense to define an in-line %s content",
834 ct
->c_type
== CT_MESSAGE
? "message" : "multipart");
839 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
840 (*ct
->c_ctinitfnx
) (ct
);
845 fseek (in
, pos
, SEEK_SET
);
850 * If we've reached this point, the next line
851 * must be some type of explicit directive.
854 /* check if directive is external-type */
855 extrnal
= (buf
[1] == '@');
857 /* parse directive */
858 if (get_ctinfo (buf
+ (extrnal
? 2 : 1), ct
, 1) == NOTOK
)
861 /* check directive against the list of MIME types */
862 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
863 if (!strcasecmp (ci
->ci_type
, s2i
->si_key
))
867 * Check if the directive specified a valid type.
868 * This will happen if it was one of the following forms:
875 die("missing subtype in \"#%s\"", ci
->ci_type
);
877 switch (ct
->c_type
= s2i
->si_val
) {
879 die("use \"#begin ... #end\" instead of \"#%s/%s\"",
880 ci
->ci_type
, ci
->ci_subtype
);
884 if (!strcasecmp (ci
->ci_subtype
, "partial"))
885 die("sorry, \"#%s/%s\" isn't supported",
886 ci
->ci_type
, ci
->ci_subtype
);
887 if (!strcasecmp (ci
->ci_subtype
, "external-body"))
888 die("use \"#@type/subtype ... [] ...\" instead of \"#%s/%s\"",
889 ci
->ci_type
, ci
->ci_subtype
);
891 die( "use \"#forw [+folder] [msgs]\" instead of \"#%s/%s\"",
892 ci
->ci_type
, ci
->ci_subtype
);
896 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
897 (*ct
->c_ctinitfnx
) (ct
);
902 * #@type/subtype (external types directive)
909 die("need external information for \"#@%s/%s\"",
910 ci
->ci_type
, ci
->ci_subtype
);
913 snprintf (buffer
, sizeof(buffer
), "message/external-body; %s", ci
->ci_magic
);
918 * Since we are using the current Content structure to
919 * hold information about the type of the external
920 * reference, we need to create another Content structure
921 * for the message/external-body to wrap it in.
924 init_decoded_content(ct
, infilename
);
926 if (get_ctinfo (buffer
, ct
, 0) == NOTOK
)
928 ct
->c_type
= CT_MESSAGE
;
929 ct
->c_subtype
= MESSAGE_EXTERNAL
;
932 ct
->c_ctparams
= (void *) e
;
938 if (params_external (ct
, 1) == NOTOK
)
944 /* Handle [file] argument */
946 /* check if specifies command to execute */
947 if (*ci
->ci_magic
== '|' || *ci
->ci_magic
== '!') {
948 for (cp
= ci
->ci_magic
+ 1; isspace ((unsigned char) *cp
); cp
++)
951 die("empty pipe command for #%s directive", ci
->ci_type
);
956 /* record filename of decoded contents */
957 ce
->ce_file
= ci
->ci_magic
;
958 if (access (ce
->ce_file
, R_OK
) == NOTOK
)
959 adios ("reading", "unable to access %s for", ce
->ce_file
);
960 if (listsw
&& stat (ce
->ce_file
, &st
) != NOTOK
)
961 ct
->c_end
= (long) st
.st_size
;
968 * No [file] argument, so check profile for
969 * method to compose content.
971 cp
= context_find_by_type ("compose", ci
->ci_type
, ci
->ci_subtype
);
973 content_error (NULL
, ct
, "don't know how to compose content");
976 ci
->ci_magic
= mh_xstrdup(cp
);
981 die("external definition not allowed for \"#%s\"", ci
->ci_type
);
985 * #forw [+folder] [msgs]
987 if (!strcasecmp (ci
->ci_type
, "forw")) {
989 char *folder
, *arguments
[MAXARGS
];
993 ap
= brkstring (ci
->ci_magic
, " ", "\n");
994 copyip (ap
, arguments
, MAXARGS
);
996 arguments
[0] = "cur";
1001 /* search the arguments for a folder name */
1002 for (ap
= arguments
; *ap
; ap
++) {
1004 if (*cp
== '+' || *cp
== '@') {
1006 die("only one folder per #forw directive");
1007 folder
= pluspath (cp
);
1011 /* else, use the current folder */
1013 folder
= mh_xstrdup(getfolder(1));
1015 if (!(mp
= folder_read (folder
, 0)))
1016 die("unable to read folder %s", folder
);
1017 for (ap
= arguments
; *ap
; ap
++) {
1019 if (*cp
!= '+' && *cp
!= '@')
1020 if (!m_convert (mp
, cp
))
1027 * If there is more than one message to include, make this
1028 * a content of type "multipart/digest" and insert each message
1029 * as a subpart. If there is only one message, then make this
1030 * a content of type "message/rfc822".
1032 if (mp
->numsel
> 1) {
1033 /* we are forwarding multiple messages */
1034 if (get_ctinfo ("multipart/digest", ct
, 0) == NOTOK
)
1036 ct
->c_type
= CT_MULTIPART
;
1037 ct
->c_subtype
= MULTI_DIGEST
;
1040 ct
->c_ctparams
= (void *) m
;
1043 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
1044 if (is_selected(mp
, msgnum
)) {
1050 init_decoded_content (p
, infilename
);
1052 if (get_ctinfo ("message/rfc822", p
, 0) == NOTOK
)
1054 p
->c_type
= CT_MESSAGE
;
1055 p
->c_subtype
= MESSAGE_RFC822
;
1057 snprintf (buffer
, sizeof(buffer
), "%s/%d", mp
->foldpath
, msgnum
);
1058 pe
->ce_file
= mh_xstrdup(buffer
);
1059 if (listsw
&& stat (pe
->ce_file
, &st
) != NOTOK
)
1060 p
->c_end
= (long) st
.st_size
;
1064 pp
= &part
->mp_next
;
1069 /* we are forwarding one message */
1070 if (get_ctinfo ("message/rfc822", ct
, 0) == NOTOK
)
1072 ct
->c_type
= CT_MESSAGE
;
1073 ct
->c_subtype
= MESSAGE_RFC822
;
1075 msgnum
= mp
->lowsel
;
1076 snprintf (buffer
, sizeof(buffer
), "%s/%d", mp
->foldpath
, msgnum
);
1077 ce
->ce_file
= mh_xstrdup(buffer
);
1078 if (listsw
&& stat (ce
->ce_file
, &st
) != NOTOK
)
1079 ct
->c_end
= (long) st
.st_size
;
1082 folder_free (mp
); /* free folder/message structure */
1089 if (!strcasecmp (ci
->ci_type
, "end")) {
1096 * #begin [ alternative | parallel ]
1098 if (!strcasecmp (ci
->ci_type
, "begin")) {
1099 if (!ci
->ci_magic
) {
1101 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
1102 } else if (!strcasecmp (ci
->ci_magic
, "alternative")) {
1103 vrsn
= MULTI_ALTERNATE
;
1104 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
1105 } else if (!strcasecmp (ci
->ci_magic
, "parallel")) {
1106 vrsn
= MULTI_PARALLEL
;
1107 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
1108 } else if (uprf (ci
->ci_magic
, "digest")) {
1111 vrsn
= MULTI_UNKNOWN
;
1116 snprintf (buffer
, sizeof(buffer
), "multipart/%s", cp
);
1117 if (get_ctinfo (buffer
, ct
, 0) == NOTOK
)
1119 ct
->c_type
= CT_MULTIPART
;
1120 ct
->c_subtype
= vrsn
;
1123 ct
->c_ctparams
= (void *) m
;
1126 while (fgetstr (buffer
, sizeof(buffer
) - 1, in
)) {
1130 if (user_content (in
, buffer
, &p
, infilename
) == DONE
) {
1132 die("empty \"#begin ... #end\" sequence");
1140 pp
= &part
->mp_next
;
1143 inform("premature end-of-file, missing #end, continuing...");
1150 die("unknown directive \"#%s\"", ci
->ci_type
);
1151 return NOTOK
; /* NOT REACHED */
1156 set_id (CT ct
, int top
)
1158 char contentid
[BUFSIZ
];
1160 static time_t clock
= 0;
1161 static char *msgfmt
;
1165 snprintf (contentid
, sizeof(contentid
), "%s\n", message_id (clock
, 1));
1167 msgfmt
= mh_xstrdup(contentid
);
1169 snprintf (contentid
, sizeof(contentid
), msgfmt
, top
? 0 : ++partno
);
1170 ct
->c_id
= mh_xstrdup(contentid
);
1175 * Fill out, or expand the various contents in the composition
1176 * draft. Read-in any necessary files. Parse and execute any
1177 * commands specified by profile composition strings.
1181 compose_content (CT ct
, int verbose
)
1183 CE ce
= &ct
->c_cefile
;
1185 switch (ct
->c_type
) {
1190 char partnam
[BUFSIZ
];
1191 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1195 snprintf (partnam
, sizeof(partnam
), "%s.", ct
->c_partno
);
1196 pp
= partnam
+ strlen (partnam
);
1201 /* first, we call compose_content on all the subparts */
1202 for (part
= m
->mp_parts
, partnum
= 1; part
; part
= part
->mp_next
, partnum
++) {
1203 CT p
= part
->mp_part
;
1205 sprintf (pp
, "%d", partnum
);
1206 p
->c_partno
= mh_xstrdup(partnam
);
1207 if (compose_content (p
, verbose
) == NOTOK
)
1212 * If the -rfc934mode switch is given, then check all
1213 * the subparts of a multipart/digest. If they are all
1214 * message/rfc822, then mark this content and all
1215 * subparts with the rfc934 compatibility mode flag.
1217 if (rfc934sw
&& ct
->c_subtype
== MULTI_DIGEST
) {
1220 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1221 CT p
= part
->mp_part
;
1223 if (p
->c_subtype
!= MESSAGE_RFC822
) {
1228 ct
->c_rfc934
= is934
;
1229 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1230 CT p
= part
->mp_part
;
1232 if ((p
->c_rfc934
= is934
))
1238 ct
->c_end
= (partnum
= strlen (prefix
) + 2) + 2;
1242 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
)
1243 ct
->c_end
+= part
->mp_part
->c_end
+ partnum
;
1249 /* Nothing to do for type message */
1253 * Discrete types (text/application/audio/image/video)
1261 char *vec
[4], buffer
[BUFSIZ
];
1263 CI ci
= &ct
->c_ctinfo
;
1266 if (!(cp
= ci
->ci_magic
))
1267 die("internal error(5)");
1269 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1270 adios("mhbuildsbr", "unable to create temporary file in %s",
1273 ce
->ce_file
= mh_xstrdup(tfile
);
1278 /* Get buffer ready to go */
1281 buflen
= sizeof(buffer
);
1284 * Parse composition string into buffer
1286 for ( ; *cp
; cp
++) {
1291 /* insert parameters from directive */
1295 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
1296 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
,
1297 pm
->pm_name
, get_param_value(pm
, '?'));
1307 /* %f, and stdout is not-redirected */
1313 * insert temporary filename where
1314 * content should be written
1316 snprintf (bp
, buflen
, "%s", ce
->ce_file
);
1320 /* insert content subtype */
1321 strncpy (bp
, ci
->ci_subtype
, buflen
);
1325 /* insert character % */
1346 printf ("composing content %s/%s from command\n\t%s\n",
1347 ci
->ci_type
, ci
->ci_subtype
, buffer
);
1349 fflush (stdout
); /* not sure if need for -noverbose */
1356 if ((out
= fopen (ce
->ce_file
, "w")) == NULL
)
1357 adios (ce
->ce_file
, "unable to open for writing");
1362 adios ("fork", "unable to fork");
1367 dup2 (fileno (out
), 1);
1368 close (fileno (out
));
1369 execvp ("/bin/sh", vec
);
1370 fprintf (stderr
, "unable to exec ");
1377 if (pidXwait(child_id
, NULL
))
1383 /* Check size of file */
1384 if (listsw
&& ct
->c_end
== 0L) {
1387 if (stat (ce
->ce_file
, &st
) != NOTOK
)
1388 ct
->c_end
= (long) st
.st_size
;
1400 * 1) choose a transfer encoding.
1401 * 2) check for clashes with multipart boundary string.
1402 * 3) for text content, figure out which character set is being used.
1404 * If there is a clash with one of the contents and the multipart boundary,
1405 * this function will exit with NOTOK. This will cause the scanning process
1406 * to be repeated with a different multipart boundary. It is possible
1407 * (although highly unlikely) that this scan will be repeated multiple times.
1411 scan_content (CT ct
, size_t maxunencoded
)
1414 bool check8bit
= false, contains8bit
= false; /* check if contains 8bit data */
1415 bool checknul
= false, containsnul
= false; /* check if contains NULs */
1416 bool checklinelen
= false, linelen
= false; /* check for long lines */
1417 bool checkllinelen
= false; /* check for extra-long lines */
1418 bool checkboundary
= false, boundaryclash
= false; /* check if clashes with multipart boundary */
1419 bool checklinespace
= false, linespace
= false; /* check if any line ends with space */
1424 struct text
*t
= NULL
;
1426 CE ce
= &ct
->c_cefile
;
1429 * handle multipart by scanning all subparts
1430 * and then checking their encoding.
1432 if (ct
->c_type
== CT_MULTIPART
) {
1433 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1436 /* initially mark the domain of enclosing multipart as 7bit */
1437 ct
->c_encoding
= CE_7BIT
;
1439 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1440 CT p
= part
->mp_part
;
1442 if (scan_content (p
, maxunencoded
) == NOTOK
) /* choose encoding for subpart */
1445 /* if necessary, enlarge encoding for enclosing multipart */
1446 if (p
->c_encoding
== CE_BINARY
)
1447 ct
->c_encoding
= CE_BINARY
;
1448 if (p
->c_encoding
== CE_8BIT
&& ct
->c_encoding
!= CE_BINARY
)
1449 ct
->c_encoding
= CE_8BIT
;
1456 * Decide what to check while scanning this content. Note that
1457 * for text content we always check for 8bit characters if the
1458 * charset is unspecified, because that controls whether or not the
1459 * character set is us-ascii or retrieved from the locale. And
1460 * we check even if the charset is specified, to allow setting
1461 * the proper Content-Transfer-Encoding.
1464 if (ct
->c_type
== CT_TEXT
) {
1465 t
= (struct text
*) ct
->c_ctparams
;
1466 if (t
->tx_charset
== CHARSET_UNSPECIFIED
) {
1472 switch (ct
->c_reqencoding
) {
1474 checkllinelen
= true;
1475 checkboundary
= true;
1478 checkboundary
= true;
1483 /* Use the default rules based on content-type */
1484 switch (ct
->c_type
) {
1486 checkboundary
= true;
1487 checklinelen
= true;
1488 if (ct
->c_subtype
== TEXT_PLAIN
) {
1489 checklinespace
= false;
1491 checklinespace
= true;
1495 case CT_APPLICATION
:
1498 checklinelen
= true;
1499 checklinespace
= true;
1500 checkboundary
= true;
1504 checklinelen
= false;
1505 checklinespace
= false;
1507 /* don't check anything for message/external */
1508 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1509 checkboundary
= false;
1512 checkboundary
= true;
1521 * Don't check anything for these types,
1522 * since we are forcing use of base64, unless
1523 * the content-type was specified by a mhbuild directive.
1526 checklinelen
= false;
1527 checklinespace
= false;
1528 checkboundary
= false;
1534 * Scan the unencoded content
1536 if (check8bit
|| checklinelen
|| checklinespace
|| checkboundary
||
1537 checkllinelen
|| checknul
) {
1538 if ((in
= fopen (ce
->ce_file
, "r")) == NULL
)
1539 adios (ce
->ce_file
, "unable to open for reading");
1540 prefix_len
= strlen (prefix
);
1542 while ((gotlen
= getline(&bufp
, &buflen
, in
)) != -1) {
1544 * Check for 8bit and NUL data.
1546 for (cp
= bufp
; (check8bit
|| checknul
) &&
1547 cp
< bufp
+ gotlen
; cp
++) {
1548 if (!isascii ((unsigned char) *cp
)) {
1549 contains8bit
= true;
1550 check8bit
= false; /* no need to keep checking */
1554 checknul
= false; /* no need to keep checking */
1559 * Check line length.
1561 if (checklinelen
&& ((size_t)gotlen
> maxunencoded
+ 1)) {
1563 checklinelen
= false; /* no need to keep checking */
1567 * RFC 5322 specifies that a message cannot contain a line
1568 * greater than 998 characters (excluding the CRLF). If we
1569 * get one of those lines and linelen is NOT set, then abort.
1572 if (checkllinelen
&& !linelen
&&
1573 (gotlen
> MAXLONGLINE
+ 1)) {
1574 die("Line in content exceeds maximum line limit (%d)",
1579 * Check if line ends with a space.
1581 if (checklinespace
&& (cp
= bufp
+ gotlen
- 2) > bufp
&&
1582 isspace ((unsigned char) *cp
)) {
1584 checklinespace
= false; /* no need to keep checking */
1588 * Check if content contains a line that clashes
1589 * with our standard boundary for multipart messages.
1591 if (checkboundary
&& bufp
[0] == '-' && bufp
[1] == '-') {
1592 for (cp
= bufp
+ gotlen
- 1; cp
>= bufp
; cp
--)
1593 if (!isspace ((unsigned char) *cp
))
1596 if (!strncmp(bufp
+ 2, prefix
, prefix_len
) &&
1597 isdigit((unsigned char) bufp
[2 + prefix_len
])) {
1598 boundaryclash
= true;
1599 checkboundary
= false; /* no need to keep checking */
1608 * If the content is text and didn't specify a character set,
1609 * we need to figure out which one was used.
1611 set_charset (ct
, contains8bit
);
1614 * Decide which transfer encoding to use.
1617 if (ct
->c_reqencoding
!= CE_UNKNOWN
)
1618 ct
->c_encoding
= ct
->c_reqencoding
;
1620 int wants_q_p
= (containsnul
|| linelen
|| linespace
);
1622 switch (ct
->c_type
) {
1625 ct
->c_encoding
= CE_QUOTED
;
1626 else if (contains8bit
)
1627 ct
->c_encoding
= CE_8BIT
;
1629 ct
->c_encoding
= CE_7BIT
;
1633 case CT_APPLICATION
:
1634 /* For application type, use base64, except when postscript */
1635 if (wants_q_p
|| contains8bit
) {
1636 if (ct
->c_subtype
== APPLICATION_POSTSCRIPT
)
1637 ct
->c_encoding
= CE_QUOTED
; /* historical */
1639 ct
->c_encoding
= CE_BASE64
;
1641 ct
->c_encoding
= CE_7BIT
;
1646 ct
->c_encoding
= contains8bit
? CE_8BIT
: CE_7BIT
;
1652 /* For audio, image, and video contents, just use base64 */
1653 ct
->c_encoding
= CE_BASE64
;
1658 return boundaryclash
? NOTOK
: OK
;
1663 * Scan the content structures, and build header
1664 * fields that will need to be output into the
1669 build_headers (CT ct
, int header_encoding
)
1671 int cc
, mailbody
, extbody
, len
;
1672 char *np
, *vp
, buffer
[BUFSIZ
];
1673 CI ci
= &ct
->c_ctinfo
;
1676 * If message is type multipart, then add the multipart
1677 * boundary to the list of attribute/value pairs.
1679 if (ct
->c_type
== CT_MULTIPART
) {
1680 static int level
= 0; /* store nesting level */
1682 snprintf (buffer
, sizeof(buffer
), "%s%d", prefix
, level
++);
1683 add_param(&ci
->ci_first_pm
, &ci
->ci_last_pm
, "boundary", buffer
, 0);
1687 * Skip the output of Content-Type, parameters, content
1688 * description and disposition, and Content-ID if the
1689 * content is of type "message" and the rfc934 compatibility
1690 * flag is set (which means we are inside multipart/digest
1691 * and the switch -rfc934mode was given).
1693 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_rfc934
)
1697 * output the content type and subtype
1699 np
= mh_xstrdup(TYPE_FIELD
);
1700 vp
= concat (" ", ci
->ci_type
, "/", ci
->ci_subtype
, NULL
);
1702 /* keep track of length of line */
1703 len
= LEN(TYPE_FIELD
) + strlen (ci
->ci_type
)
1704 + strlen (ci
->ci_subtype
) + 3;
1706 extbody
= ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_EXTERNAL
;
1707 mailbody
= extbody
&& ((struct exbody
*) ct
->c_ctparams
)->eb_body
;
1710 * Append the attribute/value pairs to
1711 * the end of the Content-Type line.
1714 if (ci
->ci_first_pm
) {
1715 char *s
= output_params(len
, ci
->ci_first_pm
, &len
, mailbody
);
1718 die("Internal error: failed outputting Content-Type "
1726 * Append any RFC-822 comment to the end of
1727 * the Content-Type line.
1729 if (ci
->ci_comment
) {
1730 snprintf (buffer
, sizeof(buffer
), "(%s)", ci
->ci_comment
);
1731 if (len
+ 1 + (cc
= 2 + strlen (ci
->ci_comment
)) >= CPERLIN
) {
1732 vp
= add ("\n\t", vp
);
1738 vp
= add (buffer
, vp
);
1741 vp
= add ("\n", vp
);
1742 add_header (ct
, np
, vp
);
1745 * output the Content-ID, unless disabled by -nocontentid. Note that
1746 * RFC 2045 always requires a Content-ID header for message/external-body
1749 if ((contentidsw
|| ct
->c_ctexbody
) && ct
->c_id
) {
1750 np
= mh_xstrdup(ID_FIELD
);
1751 vp
= concat (" ", ct
->c_id
, NULL
);
1752 add_header (ct
, np
, vp
);
1755 * output the Content-Description
1758 np
= mh_xstrdup(DESCR_FIELD
);
1759 vp
= concat (" ", ct
->c_descr
, NULL
);
1760 if (header_encoding
!= CE_8BIT
) {
1761 if (encode_rfc2047(DESCR_FIELD
, &vp
, header_encoding
, NULL
)) {
1762 die("Unable to encode %s header", DESCR_FIELD
);
1765 add_header (ct
, np
, vp
);
1769 * output the Content-Disposition. If it's NULL but c_dispo_type is
1770 * set, then we need to build it.
1773 np
= mh_xstrdup(DISPO_FIELD
);
1774 vp
= concat (" ", ct
->c_dispo
, NULL
);
1775 add_header (ct
, np
, vp
);
1776 } else if (ct
->c_dispo_type
) {
1777 vp
= concat (" ", ct
->c_dispo_type
, NULL
);
1778 len
= LEN(DISPO_FIELD
) + strlen(vp
) + 1;
1779 np
= output_params(len
, ct
->c_dispo_first
, NULL
, 0);
1783 add_header (ct
, mh_xstrdup(DISPO_FIELD
), vp
);
1788 * If this is the internal content structure for a
1789 * "message/external", then we are done with the
1790 * headers (since it has no body).
1796 * output the Content-Transfer-Encoding
1797 * If using EAI and message body is 7-bit, force 8-bit C-T-E.
1799 if (header_encoding
== CE_8BIT
&& ct
->c_encoding
== CE_7BIT
) {
1800 ct
->c_encoding
= CE_8BIT
;
1803 switch (ct
->c_encoding
) {
1805 /* Nothing to output */
1809 np
= mh_xstrdup(ENCODING_FIELD
);
1810 vp
= concat (" ", "8bit", "\n", NULL
);
1811 add_header (ct
, np
, vp
);
1815 if (ct
->c_type
== CT_MESSAGE
|| ct
->c_type
== CT_MULTIPART
)
1816 die("internal error, invalid encoding");
1818 np
= mh_xstrdup(ENCODING_FIELD
);
1819 vp
= concat (" ", "quoted-printable", "\n", NULL
);
1820 add_header (ct
, np
, vp
);
1824 if (ct
->c_type
== CT_MESSAGE
|| ct
->c_type
== CT_MULTIPART
)
1825 die("internal error, invalid encoding");
1827 np
= mh_xstrdup(ENCODING_FIELD
);
1828 vp
= concat (" ", "base64", "\n", NULL
);
1829 add_header (ct
, np
, vp
);
1833 if (ct
->c_type
== CT_MESSAGE
)
1834 die("internal error, invalid encoding");
1836 np
= mh_xstrdup(ENCODING_FIELD
);
1837 vp
= concat (" ", "binary", "\n", NULL
);
1838 add_header (ct
, np
, vp
);
1842 die("unknown transfer encoding in content");
1847 * Additional content specific header processing
1849 switch (ct
->c_type
) {
1852 struct multipart
*m
;
1855 m
= (struct multipart
*) ct
->c_ctparams
;
1856 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1860 build_headers (p
, header_encoding
);
1866 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1869 e
= (struct exbody
*) ct
->c_ctparams
;
1870 build_headers (e
->eb_content
, header_encoding
);
1884 * Set things up for the content structure for file "filename" that
1889 setup_attach_content(CT ct
, char *filename
)
1891 char *type
, *simplename
= r1bindex(filename
, '/');
1892 struct str2init
*s2i
;
1895 if (! (type
= mime_type(filename
))) {
1896 die("Unable to determine MIME type of \"%s\"", filename
);
1900 * Parse the Content-Type. get_ctinfo() parses MIME parameters, but
1901 * since we're just feeding it a MIME type we have to add those ourselves.
1902 * Map that to a valid content-type label and call any initialization
1906 if (get_ctinfo(type
, ct
, 0) == NOTOK
)
1911 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
1912 if (strcasecmp(ct
->c_ctinfo
.ci_type
, s2i
->si_key
) == 0)
1914 if (!s2i
->si_key
&& !uprf(ct
->c_ctinfo
.ci_type
, "X-"))
1918 * Make sure the type isn't incompatible with what we can handle
1921 switch (ct
->c_type
= s2i
->si_val
) {
1923 die("multipart types must be specified by mhbuild directives");
1927 if (strcasecmp(ct
->c_ctinfo
.ci_subtype
, "partial") == 0)
1928 die("Sorry, %s/%s isn't supported", ct
->c_ctinfo
.ci_type
,
1929 ct
->c_ctinfo
.ci_subtype
);
1930 if (strcasecmp(ct
->c_ctinfo
.ci_subtype
, "external-body") == 0)
1931 die("external-body messages must be specified "
1932 "by mhbuild directives");
1937 * This sets the subtype, if it's significant
1939 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
1940 (*ct
->c_ctinitfnx
)(ct
);
1945 * Feed in a few attributes; specifically, the name attribute, the
1946 * content-description, and the content-disposition.
1949 for (pm
= ct
->c_ctinfo
.ci_first_pm
; pm
; pm
= pm
->pm_next
) {
1950 if (strcasecmp(pm
->pm_name
, "name") == 0) {
1952 pm
->pm_value
= mh_xstrdup(simplename
);
1958 add_param(&ct
->c_ctinfo
.ci_first_pm
, &ct
->c_ctinfo
.ci_last_pm
,
1959 "name", simplename
, 0);
1961 ct
->c_descr
= mh_xstrdup(simplename
);
1962 ct
->c_descr
= add("\n", ct
->c_descr
);
1963 ct
->c_cefile
.ce_file
= mh_xstrdup(filename
);
1965 set_disposition (ct
);
1967 add_param(&ct
->c_dispo_first
, &ct
->c_dispo_last
, "filename", simplename
, 0);
1971 * If disposition type hasn't already been set in ct:
1972 * Look for mhbuild-disposition-<type>/<subtype> entry
1973 * that specifies Content-Disposition type. Only
1974 * 'attachment' and 'inline' are allowed. Default to
1978 set_disposition (CT ct
)
1980 if (ct
->c_dispo_type
== NULL
) {
1981 char *cp
= context_find_by_type ("disposition", ct
->c_ctinfo
.ci_type
,
1982 ct
->c_ctinfo
.ci_subtype
);
1984 if (cp
&& strcasecmp (cp
, "attachment") &&
1985 strcasecmp (cp
, "inline")) {
1986 inform("configuration problem: %s-disposition-%s%s%s specifies "
1987 "'%s' but only 'attachment' and 'inline' are allowed, "
1988 "continuing...", invo_name
,
1989 ct
->c_ctinfo
.ci_type
,
1990 ct
->c_ctinfo
.ci_subtype
? "/" : "",
1991 FENDNULL(ct
->c_ctinfo
.ci_subtype
),
1997 ct
->c_dispo_type
= mh_xstrdup(cp
);
2002 * Set text content charset if it was unspecified. contains8bit
2004 * 0: content does not contain 8-bit characters
2005 * 1: content contains 8-bit characters
2006 * -1: ignore content and use user's locale to determine charset
2009 set_charset (CT ct
, int contains8bit
)
2011 if (ct
->c_type
== CT_TEXT
) {
2014 if (ct
->c_ctparams
== NULL
) {
2017 t
->tx_charset
= CHARSET_UNSPECIFIED
;
2019 t
= (struct text
*) ct
->c_ctparams
;
2022 if (t
->tx_charset
== CHARSET_UNSPECIFIED
) {
2023 CI ci
= &ct
->c_ctinfo
;
2024 char *eightbitcharset
= write_charset_8bit();
2025 char *charset
= contains8bit
? eightbitcharset
: "us-ascii";
2027 if (contains8bit
== 1 &&
2028 strcasecmp (eightbitcharset
, "US-ASCII") == 0) {
2029 die("Text content contains 8 bit characters, but "
2030 "character set is US-ASCII");
2033 add_param (&ci
->ci_first_pm
, &ci
->ci_last_pm
, "charset", charset
,
2036 t
->tx_charset
= CHARSET_SPECIFIED
;
2043 * Look at all of the replied-to message parts and expand any that
2044 * are matched by a pseudoheader. Except don't descend into
2048 expand_pseudoheaders (CT ct
, struct multipart
*m
, const char *infile
,
2049 const convert_list
*convert_head
)
2051 /* text_plain_ct is used to concatenate all of the text/plain
2052 replies into one part, instead of having each one in a separate
2054 CT text_plain_ct
= NULL
;
2056 switch (ct
->c_type
) {
2057 case CT_MULTIPART
: {
2058 struct multipart
*mp
= (struct multipart
*) ct
->c_ctparams
;
2061 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
2062 bool matched
= false;
2064 /* The parts are in descending priority order (defined by
2065 RFC 2046 Sec. 5.1.4) because they were reversed by
2066 parse_mime (). So, stop looking for matches with
2067 immediate subparts after the first match of an
2069 for (part
= mp
->mp_parts
; ! matched
&& part
; part
= part
->mp_next
) {
2070 char *type_subtype
=
2071 concat (part
->mp_part
->c_ctinfo
.ci_type
, "/",
2072 part
->mp_part
->c_ctinfo
.ci_subtype
, NULL
);
2074 if (part
->mp_part
->c_type
== CT_MULTIPART
) {
2075 expand_pseudoheaders (part
->mp_part
, m
, infile
,
2078 const convert_list
*c
;
2080 for (c
= convert_head
; c
; c
= c
->next
) {
2081 if (! strcasecmp (type_subtype
, c
->type
)) {
2082 expand_pseudoheader (part
->mp_part
, &text_plain_ct
,
2084 c
->type
, c
->argstring
);
2090 free (type_subtype
);
2093 for (part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
2094 expand_pseudoheaders (part
->mp_part
, m
, infile
, convert_head
);
2101 char *type_subtype
=
2102 concat (ct
->c_ctinfo
.ci_type
, "/", ct
->c_ctinfo
.ci_subtype
,
2104 const convert_list
*c
;
2106 for (c
= convert_head
; c
; c
= c
->next
) {
2107 if (! strcasecmp (type_subtype
, c
->type
)) {
2108 expand_pseudoheader (ct
, &text_plain_ct
, m
, infile
, c
->type
,
2113 free (type_subtype
);
2121 * Expand a single pseudoheader. It's for the specified type.
2124 expand_pseudoheader (CT ct
, CT
*text_plain_ct
, struct multipart
*m
,
2125 const char *infile
, const char *type
,
2126 const char *argstring
)
2129 FILE *reply_fp
= NULL
;
2130 char *convert
, *type_p
, *subtype_p
;
2131 char *convert_command
;
2132 char *charset
= NULL
;
2134 struct str2init
*s2i
;
2139 type_p
= getcpy (type
);
2140 if ((subtype_p
= strchr (type_p
, '/'))) {
2141 *subtype_p
++ = '\0';
2142 convert
= context_find_by_type ("convert", type_p
, subtype_p
);
2145 type_p
= concat ("mhbuild-convert-", type
, NULL
);
2146 convert
= context_find (type_p
);
2151 /* No mhbuild-convert- entry in mhn.defaults or profile for type. */
2154 /* reply_file is used to pass the output of the convert. */
2155 reply_file
= getcpy (m_mktemp2 (NULL
, invo_name
, NULL
, NULL
));
2157 concat (convert
, " ", FENDNULL(argstring
), " >", reply_file
, NULL
);
2159 /* Convert here . . . */
2160 ct
->c_storeproc
= mh_xstrdup(convert_command
);
2161 ct
->c_umask
= ~m_gmprot ();
2163 if ((status
= show_content_aux (ct
, 0, convert_command
, NULL
, NULL
)) !=
2165 inform("store of %s content failed, continuing...", type
);
2167 free (convert_command
);
2169 /* Fill out the the new ct, reply_ct. */
2171 init_decoded_content (reply_ct
, infile
);
2173 if (extract_headers (reply_ct
, reply_file
, &reply_fp
) == NOTOK
) {
2174 inform("failed to extract headers from convert output in %s, "
2175 "continuing...", reply_file
);
2180 /* This sets reply_ct->c_ctparams, and reply_ct->c_termproc if the
2181 charset can't be handled natively. */
2182 for (s2i
= str2cts
; s2i
->si_key
; s2i
++) {
2183 if (strcasecmp(reply_ct
->c_ctinfo
.ci_type
, s2i
->si_key
) == 0) {
2188 if ((reply_ct
->c_ctinitfnx
= s2i
->si_init
)) {
2189 (*reply_ct
->c_ctinitfnx
)(reply_ct
);
2192 if ((cp
= get_param (reply_ct
->c_ctinfo
.ci_first_pm
, "charset", '?', 1))) {
2193 /* The reply Content-Type had the charset. */
2196 set_charset (reply_ct
, -1);
2197 charset
= get_param (reply_ct
->c_ctinfo
.ci_first_pm
, "charset", '?', 1);
2200 /* Concatenate text/plain parts. */
2201 if (reply_ct
->c_type
== CT_TEXT
&& reply_ct
->c_subtype
== TEXT_PLAIN
) {
2202 if (! *text_plain_ct
&& m
->mp_parts
&& m
->mp_parts
->mp_part
&&
2203 m
->mp_parts
->mp_part
->c_type
== CT_TEXT
&&
2204 m
->mp_parts
->mp_part
->c_subtype
== TEXT_PLAIN
) {
2205 *text_plain_ct
= m
->mp_parts
->mp_part
;
2206 /* Make sure that the charset is set in the text/plain part. */
2207 set_charset (*text_plain_ct
, -1);
2210 if (*text_plain_ct
) {
2211 /* Only concatenate if the charsets are identical. */
2212 char *text_plain_ct_charset
=
2213 get_param ((*text_plain_ct
)->c_ctinfo
.ci_first_pm
, "charset",
2216 if (strcasecmp (text_plain_ct_charset
, charset
) == 0) {
2217 /* Append this text/plain reply to the first one.
2218 If there's a problem anywhere along the way,
2219 instead attach it is a separate part. */
2220 int text_plain_reply
=
2221 open ((*text_plain_ct
)->c_cefile
.ce_file
,
2222 O_WRONLY
| O_APPEND
);
2223 int addl_reply
= open (reply_file
, O_RDONLY
);
2225 if (text_plain_reply
!= NOTOK
&& addl_reply
!= NOTOK
) {
2226 /* Insert blank line before each addl part. */
2227 /* It would be nice not to do this for the first one. */
2228 if (write (text_plain_reply
, "\n", 1) == 1) {
2229 /* Copy the text from the new reply and
2230 then free its Content struct. */
2231 cpydata (addl_reply
, text_plain_reply
,
2232 (*text_plain_ct
)->c_cefile
.ce_file
,
2234 if (close (text_plain_reply
) == OK
&&
2235 close (addl_reply
) == OK
) {
2236 /* If appended text needed 8-bit but first text didn't,
2237 propagate the 8-bit indication. */
2238 if ((*text_plain_ct
)->c_reqencoding
== CE_7BIT
&&
2239 reply_ct
->c_reqencoding
== CE_8BIT
) {
2240 (*text_plain_ct
)->c_reqencoding
= CE_8BIT
;
2243 if (reply_fp
) { fclose (reply_fp
); }
2245 free_content (reply_ct
);
2252 *text_plain_ct
= reply_ct
;
2256 reply_ct
->c_cefile
.ce_file
= reply_file
;
2257 reply_ct
->c_cefile
.ce_fp
= reply_fp
;
2258 reply_ct
->c_cefile
.ce_unlink
= 1;
2260 /* Attach the new part to the parent multipart/mixed, "m". */
2262 part
->mp_part
= reply_ct
;
2266 for (p
= m
->mp_parts
; p
&& p
->mp_next
; p
= p
->mp_next
) { continue; }
2274 /* Extract any Content-Type header from beginning of convert output. */
2276 extract_headers (CT ct
, char *reply_file
, FILE **reply_fp
)
2278 char *buffer
= NULL
, *cp
, *end_of_header
;
2279 bool found_header
= false;
2280 struct stat statbuf
;
2282 /* Read the convert reply from the file to memory. */
2283 if (stat (reply_file
, &statbuf
) == NOTOK
) {
2284 admonish (reply_file
, "failed to stat");
2285 goto failed_to_extract_ct
;
2288 buffer
= mh_xmalloc (statbuf
.st_size
+ 1);
2290 if ((*reply_fp
= fopen (reply_file
, "r+")) == NULL
||
2291 fread (buffer
, 1, (size_t) statbuf
.st_size
, *reply_fp
) <
2292 (size_t) statbuf
.st_size
) {
2293 admonish (reply_file
, "failed to read");
2294 goto failed_to_extract_ct
;
2296 buffer
[statbuf
.st_size
] = '\0';
2298 /* Look for a header in the convert reply. */
2299 if (strncasecmp (buffer
, TYPE_FIELD
, LEN(TYPE_FIELD
)) == 0 &&
2300 buffer
[LEN(TYPE_FIELD
)] == ':') {
2301 if ((end_of_header
= strstr (buffer
, "\r\n\r\n"))) {
2303 found_header
= true;
2304 } else if ((end_of_header
= strstr (buffer
, "\n\n"))) {
2306 found_header
= true;
2316 /* Truncate buffer to just the C-T. */
2317 *end_of_header
= '\0';
2318 n
= strlen (buffer
);
2320 if (get_ctinfo (buffer
+ 14, ct
, 0) != OK
) {
2321 inform("unable to get content info for reply, continuing...");
2322 goto failed_to_extract_ct
;
2325 /* Hack. Use parse_mime() to detect the type/subtype of the
2326 reply, which we'll use below. */
2327 tmp_file
= getcpy (m_mktemp2 (NULL
, invo_name
, NULL
, NULL
));
2328 tmp_f
= fopen(tmp_file
, "w");
2330 goto failed_to_extract_ct
;
2331 written
= fwrite(buffer
, 1, n
, tmp_f
);
2334 goto failed_to_extract_ct
;
2336 tmp_ct
= parse_mime (tmp_file
);
2338 /* The type and subtype were detected from the reply
2339 using parse_mime() above. */
2340 ct
->c_type
= tmp_ct
->c_type
;
2341 ct
->c_subtype
= tmp_ct
->c_subtype
;
2342 free_content (tmp_ct
);
2347 /* Rewrite the content without the header. */
2348 cp
= end_of_header
+ 1;
2351 if (fwrite (cp
, 1, statbuf
.st_size
- (cp
- buffer
), *reply_fp
) <
2352 (size_t) (statbuf
.st_size
- (cp
- buffer
))) {
2353 admonish (reply_file
, "failed to write");
2354 goto failed_to_extract_ct
;
2357 if (ftruncate (fileno (*reply_fp
), statbuf
.st_size
- (cp
- buffer
)) !=
2359 advise (reply_file
, "ftruncate");
2360 goto failed_to_extract_ct
;
2363 /* No header section, assume the reply is text/plain. */
2364 ct
->c_type
= CT_TEXT
;
2365 ct
->c_subtype
= TEXT_PLAIN
;
2366 if (get_ctinfo ("text/plain", ct
, 0) == NOTOK
) {
2367 /* This never should fail, but just in case. */
2368 die("unable to get content info for reply");
2372 /* free_encoding() will close reply_fp, which is passed through
2373 ct->c_cefile.ce_fp. */
2377 failed_to_extract_ct
:
2378 if (*reply_fp
) { fclose (*reply_fp
); }