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
));
735 /* the directive is implicit */
736 strncpy (content
, "text/plain", sizeof(content
));
738 strncpy (buffer
, (!do_direct() || buf
[0] != '#') ? buf
: buf
+ 1, sizeof(buffer
));
742 if (headers
>= 0 && do_direct() && uprf (buffer
, DESCR_FIELD
)
743 && buffer
[i
= LEN(DESCR_FIELD
)] == ':') {
747 ct
->c_descr
= add (buffer
+ i
+ 1, ct
->c_descr
);
748 if (!fgetstr (buffer
, sizeof(buffer
) - 1, in
))
749 die("end-of-file after %s: field in plaintext", DESCR_FIELD
);
757 die("#-directive after %s: field in plaintext", DESCR_FIELD
);
765 if (headers
>= 0 && do_direct() && uprf (buffer
, DISPO_FIELD
)
766 && buffer
[i
= LEN(DISPO_FIELD
)] == ':') {
770 ct
->c_dispo
= add (buffer
+ i
+ 1, ct
->c_dispo
);
771 if (!fgetstr (buffer
, sizeof(buffer
) - 1, in
))
772 die("end-of-file after %s: field in plaintext", DISPO_FIELD
);
780 die("#-directive after %s: field in plaintext", DISPO_FIELD
);
788 if (headers
!= 1 || buffer
[0] != '\n')
794 if ((cp
= fgetstr (buffer
, sizeof(buffer
) - 1, in
)) == NULL
)
796 if (do_direct() && buffer
[0] == '#') {
799 if (buffer
[1] != '#')
801 for (cp
= (bp
= buffer
) + 1; *cp
; cp
++)
808 ct
->c_end
= ftell (out
);
811 /* parse content type */
812 if (get_ctinfo (content
, ct
, inlineD
) == NOTOK
)
815 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
816 if (!strcasecmp (ci
->ci_type
, s2i
->si_key
))
818 if (!s2i
->si_key
&& !uprf (ci
->ci_type
, "X-"))
822 * check type specified (possibly implicitly)
824 switch (ct
->c_type
= s2i
->si_val
) {
826 if (!strcasecmp (ci
->ci_subtype
, "rfc822")) {
827 ct
->c_encoding
= CE_7BIT
;
832 die("it doesn't make sense to define an in-line %s content",
833 ct
->c_type
== CT_MESSAGE
? "message" : "multipart");
838 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
839 (*ct
->c_ctinitfnx
) (ct
);
844 fseek (in
, pos
, SEEK_SET
);
849 * If we've reached this point, the next line
850 * must be some type of explicit directive.
853 /* check if directive is external-type */
854 extrnal
= (buf
[1] == '@');
856 /* parse directive */
857 if (get_ctinfo (buf
+ (extrnal
? 2 : 1), ct
, 1) == NOTOK
)
860 /* check directive against the list of MIME types */
861 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
862 if (!strcasecmp (ci
->ci_type
, s2i
->si_key
))
866 * Check if the directive specified a valid type.
867 * This will happen if it was one of the following forms:
874 die("missing subtype in \"#%s\"", ci
->ci_type
);
876 switch (ct
->c_type
= s2i
->si_val
) {
878 die("use \"#begin ... #end\" instead of \"#%s/%s\"",
879 ci
->ci_type
, ci
->ci_subtype
);
883 if (!strcasecmp (ci
->ci_subtype
, "partial"))
884 die("sorry, \"#%s/%s\" isn't supported",
885 ci
->ci_type
, ci
->ci_subtype
);
886 if (!strcasecmp (ci
->ci_subtype
, "external-body"))
887 die("use \"#@type/subtype ... [] ...\" instead of \"#%s/%s\"",
888 ci
->ci_type
, ci
->ci_subtype
);
890 die( "use \"#forw [+folder] [msgs]\" instead of \"#%s/%s\"",
891 ci
->ci_type
, ci
->ci_subtype
);
895 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
896 (*ct
->c_ctinitfnx
) (ct
);
901 * #@type/subtype (external types directive)
908 die("need external information for \"#@%s/%s\"",
909 ci
->ci_type
, ci
->ci_subtype
);
912 snprintf (buffer
, sizeof(buffer
), "message/external-body; %s", ci
->ci_magic
);
917 * Since we are using the current Content structure to
918 * hold information about the type of the external
919 * reference, we need to create another Content structure
920 * for the message/external-body to wrap it in.
923 init_decoded_content(ct
, infilename
);
925 if (get_ctinfo (buffer
, ct
, 0) == NOTOK
)
927 ct
->c_type
= CT_MESSAGE
;
928 ct
->c_subtype
= MESSAGE_EXTERNAL
;
931 ct
->c_ctparams
= (void *) e
;
937 if (params_external (ct
, 1) == NOTOK
)
943 /* Handle [file] argument */
945 /* check if specifies command to execute */
946 if (*ci
->ci_magic
== '|' || *ci
->ci_magic
== '!') {
947 for (cp
= ci
->ci_magic
+ 1; isspace ((unsigned char) *cp
); cp
++)
950 die("empty pipe command for #%s directive", ci
->ci_type
);
955 /* record filename of decoded contents */
956 ce
->ce_file
= ci
->ci_magic
;
957 if (access (ce
->ce_file
, R_OK
) == NOTOK
)
958 adios ("reading", "unable to access %s for", ce
->ce_file
);
959 if (listsw
&& stat (ce
->ce_file
, &st
) != NOTOK
)
960 ct
->c_end
= (long) st
.st_size
;
967 * No [file] argument, so check profile for
968 * method to compose content.
970 cp
= context_find_by_type ("compose", ci
->ci_type
, ci
->ci_subtype
);
972 content_error (NULL
, ct
, "don't know how to compose content");
975 ci
->ci_magic
= mh_xstrdup(cp
);
980 die("external definition not allowed for \"#%s\"", ci
->ci_type
);
984 * #forw [+folder] [msgs]
986 if (!strcasecmp (ci
->ci_type
, "forw")) {
988 char *folder
, *arguments
[MAXARGS
];
992 ap
= brkstring (ci
->ci_magic
, " ", "\n");
993 copyip (ap
, arguments
, MAXARGS
);
995 arguments
[0] = "cur";
1000 /* search the arguments for a folder name */
1001 for (ap
= arguments
; *ap
; ap
++) {
1003 if (*cp
== '+' || *cp
== '@') {
1005 die("only one folder per #forw directive");
1006 folder
= pluspath (cp
);
1010 /* else, use the current folder */
1012 folder
= mh_xstrdup(getfolder(1));
1014 if (!(mp
= folder_read (folder
, 0)))
1015 die("unable to read folder %s", folder
);
1016 for (ap
= arguments
; *ap
; ap
++) {
1018 if (*cp
!= '+' && *cp
!= '@')
1019 if (!m_convert (mp
, cp
))
1026 * If there is more than one message to include, make this
1027 * a content of type "multipart/digest" and insert each message
1028 * as a subpart. If there is only one message, then make this
1029 * a content of type "message/rfc822".
1031 if (mp
->numsel
> 1) {
1032 /* we are forwarding multiple messages */
1033 if (get_ctinfo ("multipart/digest", ct
, 0) == NOTOK
)
1035 ct
->c_type
= CT_MULTIPART
;
1036 ct
->c_subtype
= MULTI_DIGEST
;
1039 ct
->c_ctparams
= (void *) m
;
1042 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
1043 if (is_selected(mp
, msgnum
)) {
1049 init_decoded_content (p
, infilename
);
1051 if (get_ctinfo ("message/rfc822", p
, 0) == NOTOK
)
1053 p
->c_type
= CT_MESSAGE
;
1054 p
->c_subtype
= MESSAGE_RFC822
;
1056 snprintf (buffer
, sizeof(buffer
), "%s/%d", mp
->foldpath
, msgnum
);
1057 pe
->ce_file
= mh_xstrdup(buffer
);
1058 if (listsw
&& stat (pe
->ce_file
, &st
) != NOTOK
)
1059 p
->c_end
= (long) st
.st_size
;
1063 pp
= &part
->mp_next
;
1068 /* we are forwarding one message */
1069 if (get_ctinfo ("message/rfc822", ct
, 0) == NOTOK
)
1071 ct
->c_type
= CT_MESSAGE
;
1072 ct
->c_subtype
= MESSAGE_RFC822
;
1074 msgnum
= mp
->lowsel
;
1075 snprintf (buffer
, sizeof(buffer
), "%s/%d", mp
->foldpath
, msgnum
);
1076 ce
->ce_file
= mh_xstrdup(buffer
);
1077 if (listsw
&& stat (ce
->ce_file
, &st
) != NOTOK
)
1078 ct
->c_end
= (long) st
.st_size
;
1081 folder_free (mp
); /* free folder/message structure */
1088 if (!strcasecmp (ci
->ci_type
, "end")) {
1095 * #begin [ alternative | parallel ]
1097 if (!strcasecmp (ci
->ci_type
, "begin")) {
1098 if (!ci
->ci_magic
) {
1100 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
1101 } else if (!strcasecmp (ci
->ci_magic
, "alternative")) {
1102 vrsn
= MULTI_ALTERNATE
;
1103 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
1104 } else if (!strcasecmp (ci
->ci_magic
, "parallel")) {
1105 vrsn
= MULTI_PARALLEL
;
1106 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
1107 } else if (uprf (ci
->ci_magic
, "digest")) {
1110 vrsn
= MULTI_UNKNOWN
;
1115 snprintf (buffer
, sizeof(buffer
), "multipart/%s", cp
);
1116 if (get_ctinfo (buffer
, ct
, 0) == NOTOK
)
1118 ct
->c_type
= CT_MULTIPART
;
1119 ct
->c_subtype
= vrsn
;
1122 ct
->c_ctparams
= (void *) m
;
1125 while (fgetstr (buffer
, sizeof(buffer
) - 1, in
)) {
1129 if (user_content (in
, buffer
, &p
, infilename
) == DONE
) {
1131 die("empty \"#begin ... #end\" sequence");
1139 pp
= &part
->mp_next
;
1142 inform("premature end-of-file, missing #end, continuing...");
1149 die("unknown directive \"#%s\"", ci
->ci_type
);
1150 return NOTOK
; /* NOT REACHED */
1155 set_id (CT ct
, int top
)
1157 char contentid
[BUFSIZ
];
1159 static time_t clock
= 0;
1160 static char *msgfmt
;
1164 snprintf (contentid
, sizeof(contentid
), "%s\n", message_id (clock
, 1));
1166 msgfmt
= mh_xstrdup(contentid
);
1168 snprintf (contentid
, sizeof(contentid
), msgfmt
, top
? 0 : ++partno
);
1169 ct
->c_id
= mh_xstrdup(contentid
);
1174 * Fill out, or expand the various contents in the composition
1175 * draft. Read-in any necessary files. Parse and execute any
1176 * commands specified by profile composition strings.
1180 compose_content (CT ct
, int verbose
)
1182 CE ce
= &ct
->c_cefile
;
1184 switch (ct
->c_type
) {
1189 char partnam
[BUFSIZ
];
1190 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1194 snprintf (partnam
, sizeof(partnam
), "%s.", ct
->c_partno
);
1195 pp
= partnam
+ strlen (partnam
);
1200 /* first, we call compose_content on all the subparts */
1201 for (part
= m
->mp_parts
, partnum
= 1; part
; part
= part
->mp_next
, partnum
++) {
1202 CT p
= part
->mp_part
;
1204 sprintf (pp
, "%d", partnum
);
1205 p
->c_partno
= mh_xstrdup(partnam
);
1206 if (compose_content (p
, verbose
) == NOTOK
)
1211 * If the -rfc934mode switch is given, then check all
1212 * the subparts of a multipart/digest. If they are all
1213 * message/rfc822, then mark this content and all
1214 * subparts with the rfc934 compatibility mode flag.
1216 if (rfc934sw
&& ct
->c_subtype
== MULTI_DIGEST
) {
1219 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1220 CT p
= part
->mp_part
;
1222 if (p
->c_subtype
!= MESSAGE_RFC822
) {
1227 ct
->c_rfc934
= is934
;
1228 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1229 CT p
= part
->mp_part
;
1231 if ((p
->c_rfc934
= is934
))
1237 ct
->c_end
= (partnum
= strlen (prefix
) + 2) + 2;
1241 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
)
1242 ct
->c_end
+= part
->mp_part
->c_end
+ partnum
;
1248 /* Nothing to do for type message */
1252 * Discrete types (text/application/audio/image/video)
1260 char *vec
[4], buffer
[BUFSIZ
];
1262 CI ci
= &ct
->c_ctinfo
;
1265 if (!(cp
= ci
->ci_magic
))
1266 die("internal error(5)");
1268 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1269 adios("mhbuildsbr", "unable to create temporary file in %s",
1272 ce
->ce_file
= mh_xstrdup(tfile
);
1277 /* Get buffer ready to go */
1280 buflen
= sizeof(buffer
);
1283 * Parse composition string into buffer
1285 for ( ; *cp
; cp
++) {
1290 /* insert parameters from directive */
1294 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
1295 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
,
1296 pm
->pm_name
, get_param_value(pm
, '?'));
1306 /* %f, and stdout is not-redirected */
1312 * insert temporary filename where
1313 * content should be written
1315 snprintf (bp
, buflen
, "%s", ce
->ce_file
);
1319 /* insert content subtype */
1320 strncpy (bp
, ci
->ci_subtype
, buflen
);
1324 /* insert character % */
1345 printf ("composing content %s/%s from command\n\t%s\n",
1346 ci
->ci_type
, ci
->ci_subtype
, buffer
);
1348 fflush (stdout
); /* not sure if need for -noverbose */
1355 if ((out
= fopen (ce
->ce_file
, "w")) == NULL
)
1356 adios (ce
->ce_file
, "unable to open for writing");
1361 adios ("fork", "unable to fork");
1366 dup2 (fileno (out
), 1);
1367 close (fileno (out
));
1368 execvp ("/bin/sh", vec
);
1369 fprintf (stderr
, "unable to exec ");
1376 if (pidXwait(child_id
, NULL
))
1382 /* Check size of file */
1383 if (listsw
&& ct
->c_end
== 0L) {
1386 if (stat (ce
->ce_file
, &st
) != NOTOK
)
1387 ct
->c_end
= (long) st
.st_size
;
1399 * 1) choose a transfer encoding.
1400 * 2) check for clashes with multipart boundary string.
1401 * 3) for text content, figure out which character set is being used.
1403 * If there is a clash with one of the contents and the multipart boundary,
1404 * this function will exit with NOTOK. This will cause the scanning process
1405 * to be repeated with a different multipart boundary. It is possible
1406 * (although highly unlikely) that this scan will be repeated multiple times.
1410 scan_content (CT ct
, size_t maxunencoded
)
1413 bool check8bit
= false, contains8bit
= false; /* check if contains 8bit data */
1414 bool checknul
= false, containsnul
= false; /* check if contains NULs */
1415 bool checklinelen
= false, linelen
= false; /* check for long lines */
1416 bool checkllinelen
= false; /* check for extra-long lines */
1417 bool checkboundary
= false, boundaryclash
= false; /* check if clashes with multipart boundary */
1418 bool checklinespace
= false, linespace
= false; /* check if any line ends with space */
1423 struct text
*t
= NULL
;
1425 CE ce
= &ct
->c_cefile
;
1428 * handle multipart by scanning all subparts
1429 * and then checking their encoding.
1431 if (ct
->c_type
== CT_MULTIPART
) {
1432 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1435 /* initially mark the domain of enclosing multipart as 7bit */
1436 ct
->c_encoding
= CE_7BIT
;
1438 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1439 CT p
= part
->mp_part
;
1441 if (scan_content (p
, maxunencoded
) == NOTOK
) /* choose encoding for subpart */
1444 /* if necessary, enlarge encoding for enclosing multipart */
1445 if (p
->c_encoding
== CE_BINARY
)
1446 ct
->c_encoding
= CE_BINARY
;
1447 if (p
->c_encoding
== CE_8BIT
&& ct
->c_encoding
!= CE_BINARY
)
1448 ct
->c_encoding
= CE_8BIT
;
1455 * Decide what to check while scanning this content. Note that
1456 * for text content we always check for 8bit characters if the
1457 * charset is unspecified, because that controls whether or not the
1458 * character set is us-ascii or retrieved from the locale. And
1459 * we check even if the charset is specified, to allow setting
1460 * the proper Content-Transfer-Encoding.
1463 if (ct
->c_type
== CT_TEXT
) {
1464 t
= (struct text
*) ct
->c_ctparams
;
1465 if (t
->tx_charset
== CHARSET_UNSPECIFIED
) {
1471 switch (ct
->c_reqencoding
) {
1473 checkllinelen
= true;
1474 checkboundary
= true;
1477 checkboundary
= true;
1482 /* Use the default rules based on content-type */
1483 switch (ct
->c_type
) {
1485 checkboundary
= true;
1486 checklinelen
= true;
1487 if (ct
->c_subtype
== TEXT_PLAIN
) {
1488 checklinespace
= false;
1490 checklinespace
= true;
1494 case CT_APPLICATION
:
1497 checklinelen
= true;
1498 checklinespace
= true;
1499 checkboundary
= true;
1503 checklinelen
= false;
1504 checklinespace
= false;
1506 /* don't check anything for message/external */
1507 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1508 checkboundary
= false;
1511 checkboundary
= true;
1520 * Don't check anything for these types,
1521 * since we are forcing use of base64, unless
1522 * the content-type was specified by a mhbuild directive.
1525 checklinelen
= false;
1526 checklinespace
= false;
1527 checkboundary
= false;
1533 * Scan the unencoded content
1535 if (check8bit
|| checklinelen
|| checklinespace
|| checkboundary
||
1536 checkllinelen
|| checknul
) {
1537 if ((in
= fopen (ce
->ce_file
, "r")) == NULL
)
1538 adios (ce
->ce_file
, "unable to open for reading");
1539 prefix_len
= strlen (prefix
);
1541 while ((gotlen
= getline(&bufp
, &buflen
, in
)) != -1) {
1543 * Check for 8bit and NUL data.
1545 for (cp
= bufp
; (check8bit
|| checknul
) &&
1546 cp
< bufp
+ gotlen
; cp
++) {
1547 if (!isascii ((unsigned char) *cp
)) {
1548 contains8bit
= true;
1549 check8bit
= false; /* no need to keep checking */
1553 checknul
= false; /* no need to keep checking */
1558 * Check line length.
1560 if (checklinelen
&& ((size_t)gotlen
> maxunencoded
+ 1)) {
1562 checklinelen
= false; /* no need to keep checking */
1566 * RFC 5322 specifies that a message cannot contain a line
1567 * greater than 998 characters (excluding the CRLF). If we
1568 * get one of those lines and linelen is NOT set, then abort.
1571 if (checkllinelen
&& !linelen
&&
1572 (gotlen
> MAXLONGLINE
+ 1)) {
1573 die("Line in content exceeds maximum line limit (%d)",
1578 * Check if line ends with a space.
1580 if (checklinespace
&& (cp
= bufp
+ gotlen
- 2) > bufp
&&
1581 isspace ((unsigned char) *cp
)) {
1583 checklinespace
= false; /* no need to keep checking */
1587 * Check if content contains a line that clashes
1588 * with our standard boundary for multipart messages.
1590 if (checkboundary
&& bufp
[0] == '-' && bufp
[1] == '-') {
1591 for (cp
= bufp
+ gotlen
- 1; cp
>= bufp
; cp
--)
1592 if (!isspace ((unsigned char) *cp
))
1595 if (!strncmp(bufp
+ 2, prefix
, prefix_len
) &&
1596 isdigit((unsigned char) bufp
[2 + prefix_len
])) {
1597 boundaryclash
= true;
1598 checkboundary
= false; /* no need to keep checking */
1607 * If the content is text and didn't specify a character set,
1608 * we need to figure out which one was used.
1610 set_charset (ct
, contains8bit
);
1613 * Decide which transfer encoding to use.
1616 if (ct
->c_reqencoding
!= CE_UNKNOWN
)
1617 ct
->c_encoding
= ct
->c_reqencoding
;
1619 int wants_q_p
= (containsnul
|| linelen
|| linespace
);
1621 switch (ct
->c_type
) {
1624 ct
->c_encoding
= CE_QUOTED
;
1625 else if (contains8bit
)
1626 ct
->c_encoding
= CE_8BIT
;
1628 ct
->c_encoding
= CE_7BIT
;
1632 case CT_APPLICATION
:
1633 /* For application type, use base64, except when postscript */
1634 if (wants_q_p
|| contains8bit
) {
1635 if (ct
->c_subtype
== APPLICATION_POSTSCRIPT
)
1636 ct
->c_encoding
= CE_QUOTED
; /* historical */
1638 ct
->c_encoding
= CE_BASE64
;
1640 ct
->c_encoding
= CE_7BIT
;
1645 ct
->c_encoding
= contains8bit
? CE_8BIT
: CE_7BIT
;
1651 /* For audio, image, and video contents, just use base64 */
1652 ct
->c_encoding
= CE_BASE64
;
1657 return boundaryclash
? NOTOK
: OK
;
1662 * Scan the content structures, and build header
1663 * fields that will need to be output into the
1668 build_headers (CT ct
, int header_encoding
)
1670 int cc
, mailbody
, extbody
, len
;
1671 char *np
, *vp
, buffer
[BUFSIZ
];
1672 CI ci
= &ct
->c_ctinfo
;
1675 * If message is type multipart, then add the multipart
1676 * boundary to the list of attribute/value pairs.
1678 if (ct
->c_type
== CT_MULTIPART
) {
1679 static int level
= 0; /* store nesting level */
1681 snprintf (buffer
, sizeof(buffer
), "%s%d", prefix
, level
++);
1682 add_param(&ci
->ci_first_pm
, &ci
->ci_last_pm
, "boundary", buffer
, 0);
1686 * Skip the output of Content-Type, parameters, content
1687 * description and disposition, and Content-ID if the
1688 * content is of type "message" and the rfc934 compatibility
1689 * flag is set (which means we are inside multipart/digest
1690 * and the switch -rfc934mode was given).
1692 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_rfc934
)
1696 * output the content type and subtype
1698 np
= mh_xstrdup(TYPE_FIELD
);
1699 vp
= concat (" ", ci
->ci_type
, "/", ci
->ci_subtype
, NULL
);
1701 /* keep track of length of line */
1702 len
= LEN(TYPE_FIELD
) + strlen (ci
->ci_type
)
1703 + strlen (ci
->ci_subtype
) + 3;
1705 extbody
= ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_EXTERNAL
;
1706 mailbody
= extbody
&& ((struct exbody
*) ct
->c_ctparams
)->eb_body
;
1709 * Append the attribute/value pairs to
1710 * the end of the Content-Type line.
1713 if (ci
->ci_first_pm
) {
1714 char *s
= output_params(len
, ci
->ci_first_pm
, &len
, mailbody
);
1717 die("Internal error: failed outputting Content-Type "
1725 * Append any RFC-822 comment to the end of
1726 * the Content-Type line.
1728 if (ci
->ci_comment
) {
1729 snprintf (buffer
, sizeof(buffer
), "(%s)", ci
->ci_comment
);
1730 if (len
+ 1 + (cc
= 2 + strlen (ci
->ci_comment
)) >= CPERLIN
) {
1731 vp
= add ("\n\t", vp
);
1737 vp
= add (buffer
, vp
);
1740 vp
= add ("\n", vp
);
1741 add_header (ct
, np
, vp
);
1744 * output the Content-ID, unless disabled by -nocontentid. Note that
1745 * RFC 2045 always requires a Content-ID header for message/external-body
1748 if ((contentidsw
|| ct
->c_ctexbody
) && ct
->c_id
) {
1749 np
= mh_xstrdup(ID_FIELD
);
1750 vp
= concat (" ", ct
->c_id
, NULL
);
1751 add_header (ct
, np
, vp
);
1754 * output the Content-Description
1757 np
= mh_xstrdup(DESCR_FIELD
);
1758 vp
= concat (" ", ct
->c_descr
, NULL
);
1759 if (header_encoding
!= CE_8BIT
) {
1760 if (encode_rfc2047(DESCR_FIELD
, &vp
, header_encoding
, NULL
)) {
1761 die("Unable to encode %s header", DESCR_FIELD
);
1764 add_header (ct
, np
, vp
);
1768 * output the Content-Disposition. If it's NULL but c_dispo_type is
1769 * set, then we need to build it.
1772 np
= mh_xstrdup(DISPO_FIELD
);
1773 vp
= concat (" ", ct
->c_dispo
, NULL
);
1774 add_header (ct
, np
, vp
);
1775 } else if (ct
->c_dispo_type
) {
1776 vp
= concat (" ", ct
->c_dispo_type
, NULL
);
1777 len
= LEN(DISPO_FIELD
) + strlen(vp
) + 1;
1778 np
= output_params(len
, ct
->c_dispo_first
, NULL
, 0);
1782 add_header (ct
, mh_xstrdup(DISPO_FIELD
), vp
);
1787 * If this is the internal content structure for a
1788 * "message/external", then we are done with the
1789 * headers (since it has no body).
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
);
1883 * Set things up for the content structure for file "filename" that
1888 setup_attach_content(CT ct
, char *filename
)
1890 char *type
, *simplename
= r1bindex(filename
, '/');
1891 struct str2init
*s2i
;
1894 if (! (type
= mime_type(filename
))) {
1895 die("Unable to determine MIME type of \"%s\"", filename
);
1899 * Parse the Content-Type. get_ctinfo() parses MIME parameters, but
1900 * since we're just feeding it a MIME type we have to add those ourselves.
1901 * Map that to a valid content-type label and call any initialization
1905 if (get_ctinfo(type
, ct
, 0) == NOTOK
)
1910 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
1911 if (strcasecmp(ct
->c_ctinfo
.ci_type
, s2i
->si_key
) == 0)
1913 if (!s2i
->si_key
&& !uprf(ct
->c_ctinfo
.ci_type
, "X-"))
1917 * Make sure the type isn't incompatible with what we can handle
1920 switch (ct
->c_type
= s2i
->si_val
) {
1922 die("multipart types must be specified by mhbuild directives");
1926 if (strcasecmp(ct
->c_ctinfo
.ci_subtype
, "partial") == 0)
1927 die("Sorry, %s/%s isn't supported", ct
->c_ctinfo
.ci_type
,
1928 ct
->c_ctinfo
.ci_subtype
);
1929 if (strcasecmp(ct
->c_ctinfo
.ci_subtype
, "external-body") == 0)
1930 die("external-body messages must be specified "
1931 "by mhbuild directives");
1936 * This sets the subtype, if it's significant
1938 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
1939 (*ct
->c_ctinitfnx
)(ct
);
1944 * Feed in a few attributes; specifically, the name attribute, the
1945 * content-description, and the content-disposition.
1948 for (pm
= ct
->c_ctinfo
.ci_first_pm
; pm
; pm
= pm
->pm_next
) {
1949 if (strcasecmp(pm
->pm_name
, "name") == 0) {
1951 pm
->pm_value
= mh_xstrdup(simplename
);
1957 add_param(&ct
->c_ctinfo
.ci_first_pm
, &ct
->c_ctinfo
.ci_last_pm
,
1958 "name", simplename
, 0);
1960 ct
->c_descr
= mh_xstrdup(simplename
);
1961 ct
->c_descr
= add("\n", ct
->c_descr
);
1962 ct
->c_cefile
.ce_file
= mh_xstrdup(filename
);
1964 set_disposition (ct
);
1966 add_param(&ct
->c_dispo_first
, &ct
->c_dispo_last
, "filename", simplename
, 0);
1970 * If disposition type hasn't already been set in ct:
1971 * Look for mhbuild-disposition-<type>/<subtype> entry
1972 * that specifies Content-Disposition type. Only
1973 * 'attachment' and 'inline' are allowed. Default to
1977 set_disposition (CT ct
)
1979 if (ct
->c_dispo_type
== NULL
) {
1980 char *cp
= context_find_by_type ("disposition", ct
->c_ctinfo
.ci_type
,
1981 ct
->c_ctinfo
.ci_subtype
);
1983 if (cp
&& strcasecmp (cp
, "attachment") &&
1984 strcasecmp (cp
, "inline")) {
1985 inform("configuration problem: %s-disposition-%s%s%s specifies "
1986 "'%s' but only 'attachment' and 'inline' are allowed, "
1987 "continuing...", invo_name
,
1988 ct
->c_ctinfo
.ci_type
,
1989 ct
->c_ctinfo
.ci_subtype
? "/" : "",
1990 FENDNULL(ct
->c_ctinfo
.ci_subtype
),
1996 ct
->c_dispo_type
= mh_xstrdup(cp
);
2001 * Set text content charset if it was unspecified. contains8bit
2003 * 0: content does not contain 8-bit characters
2004 * 1: content contains 8-bit characters
2005 * -1: ignore content and use user's locale to determine charset
2008 set_charset (CT ct
, int contains8bit
)
2010 if (ct
->c_type
== CT_TEXT
) {
2013 if (ct
->c_ctparams
== NULL
) {
2016 t
->tx_charset
= CHARSET_UNSPECIFIED
;
2018 t
= (struct text
*) ct
->c_ctparams
;
2021 if (t
->tx_charset
== CHARSET_UNSPECIFIED
) {
2022 CI ci
= &ct
->c_ctinfo
;
2023 char *eightbitcharset
= write_charset_8bit();
2024 char *charset
= contains8bit
? eightbitcharset
: "us-ascii";
2026 if (contains8bit
== 1 &&
2027 strcasecmp (eightbitcharset
, "US-ASCII") == 0) {
2028 die("Text content contains 8 bit characters, but "
2029 "character set is US-ASCII");
2032 add_param (&ci
->ci_first_pm
, &ci
->ci_last_pm
, "charset", charset
,
2035 t
->tx_charset
= CHARSET_SPECIFIED
;
2042 * Look at all of the replied-to message parts and expand any that
2043 * are matched by a pseudoheader. Except don't descend into
2047 expand_pseudoheaders (CT ct
, struct multipart
*m
, const char *infile
,
2048 const convert_list
*convert_head
)
2050 /* text_plain_ct is used to concatenate all of the text/plain
2051 replies into one part, instead of having each one in a separate
2053 CT text_plain_ct
= NULL
;
2055 switch (ct
->c_type
) {
2056 case CT_MULTIPART
: {
2057 struct multipart
*mp
= (struct multipart
*) ct
->c_ctparams
;
2060 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
2061 bool matched
= false;
2063 /* The parts are in descending priority order (defined by
2064 RFC 2046 Sec. 5.1.4) because they were reversed by
2065 parse_mime (). So, stop looking for matches with
2066 immediate subparts after the first match of an
2068 for (part
= mp
->mp_parts
; ! matched
&& part
; part
= part
->mp_next
) {
2069 char *type_subtype
=
2070 concat (part
->mp_part
->c_ctinfo
.ci_type
, "/",
2071 part
->mp_part
->c_ctinfo
.ci_subtype
, NULL
);
2073 if (part
->mp_part
->c_type
== CT_MULTIPART
) {
2074 expand_pseudoheaders (part
->mp_part
, m
, infile
,
2077 const convert_list
*c
;
2079 for (c
= convert_head
; c
; c
= c
->next
) {
2080 if (! strcasecmp (type_subtype
, c
->type
)) {
2081 expand_pseudoheader (part
->mp_part
, &text_plain_ct
,
2083 c
->type
, c
->argstring
);
2089 free (type_subtype
);
2092 for (part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
2093 expand_pseudoheaders (part
->mp_part
, m
, infile
, convert_head
);
2100 char *type_subtype
=
2101 concat (ct
->c_ctinfo
.ci_type
, "/", ct
->c_ctinfo
.ci_subtype
,
2103 const convert_list
*c
;
2105 for (c
= convert_head
; c
; c
= c
->next
) {
2106 if (! strcasecmp (type_subtype
, c
->type
)) {
2107 expand_pseudoheader (ct
, &text_plain_ct
, m
, infile
, c
->type
,
2112 free (type_subtype
);
2120 * Expand a single pseudoheader. It's for the specified type.
2123 expand_pseudoheader (CT ct
, CT
*text_plain_ct
, struct multipart
*m
,
2124 const char *infile
, const char *type
,
2125 const char *argstring
)
2128 FILE *reply_fp
= NULL
;
2129 char *convert
, *type_p
, *subtype_p
;
2130 char *convert_command
;
2131 char *charset
= NULL
;
2133 struct str2init
*s2i
;
2138 type_p
= getcpy (type
);
2139 if ((subtype_p
= strchr (type_p
, '/'))) {
2140 *subtype_p
++ = '\0';
2141 convert
= context_find_by_type ("convert", type_p
, subtype_p
);
2144 type_p
= concat ("mhbuild-convert-", type
, NULL
);
2145 convert
= context_find (type_p
);
2150 /* No mhbuild-convert- entry in mhn.defaults or profile for type. */
2153 /* reply_file is used to pass the output of the convert. */
2154 reply_file
= getcpy (m_mktemp2 (NULL
, invo_name
, NULL
, NULL
));
2156 concat (convert
, " ", FENDNULL(argstring
), " >", reply_file
, NULL
);
2158 /* Convert here . . . */
2159 ct
->c_storeproc
= mh_xstrdup(convert_command
);
2160 ct
->c_umask
= ~m_gmprot ();
2162 if ((status
= show_content_aux (ct
, 0, convert_command
, NULL
, NULL
)) !=
2164 inform("store of %s content failed, continuing...", type
);
2166 free (convert_command
);
2168 /* Fill out the the new ct, reply_ct. */
2170 init_decoded_content (reply_ct
, infile
);
2172 if (extract_headers (reply_ct
, reply_file
, &reply_fp
) == NOTOK
) {
2173 inform("failed to extract headers from convert output in %s, "
2174 "continuing...", reply_file
);
2179 /* This sets reply_ct->c_ctparams, and reply_ct->c_termproc if the
2180 charset can't be handled natively. */
2181 for (s2i
= str2cts
; s2i
->si_key
; s2i
++) {
2182 if (strcasecmp(reply_ct
->c_ctinfo
.ci_type
, s2i
->si_key
) == 0) {
2187 if ((reply_ct
->c_ctinitfnx
= s2i
->si_init
)) {
2188 (*reply_ct
->c_ctinitfnx
)(reply_ct
);
2191 if ((cp
= get_param (reply_ct
->c_ctinfo
.ci_first_pm
, "charset", '?', 1))) {
2192 /* The reply Content-Type had the charset. */
2195 set_charset (reply_ct
, -1);
2196 charset
= get_param (reply_ct
->c_ctinfo
.ci_first_pm
, "charset", '?', 1);
2199 /* Concatenate text/plain parts. */
2200 if (reply_ct
->c_type
== CT_TEXT
&& reply_ct
->c_subtype
== TEXT_PLAIN
) {
2201 if (! *text_plain_ct
&& m
->mp_parts
&& m
->mp_parts
->mp_part
&&
2202 m
->mp_parts
->mp_part
->c_type
== CT_TEXT
&&
2203 m
->mp_parts
->mp_part
->c_subtype
== TEXT_PLAIN
) {
2204 *text_plain_ct
= m
->mp_parts
->mp_part
;
2205 /* Make sure that the charset is set in the text/plain part. */
2206 set_charset (*text_plain_ct
, -1);
2209 if (*text_plain_ct
) {
2210 /* Only concatenate if the charsets are identical. */
2211 char *text_plain_ct_charset
=
2212 get_param ((*text_plain_ct
)->c_ctinfo
.ci_first_pm
, "charset",
2215 if (strcasecmp (text_plain_ct_charset
, charset
) == 0) {
2216 /* Append this text/plain reply to the first one.
2217 If there's a problem anywhere along the way,
2218 instead attach it is a separate part. */
2219 int text_plain_reply
=
2220 open ((*text_plain_ct
)->c_cefile
.ce_file
,
2221 O_WRONLY
| O_APPEND
);
2222 int addl_reply
= open (reply_file
, O_RDONLY
);
2224 if (text_plain_reply
!= NOTOK
&& addl_reply
!= NOTOK
) {
2225 /* Insert blank line before each addl part. */
2226 /* It would be nice not to do this for the first one. */
2227 if (write (text_plain_reply
, "\n", 1) == 1) {
2228 /* Copy the text from the new reply and
2229 then free its Content struct. */
2230 cpydata (addl_reply
, text_plain_reply
,
2231 (*text_plain_ct
)->c_cefile
.ce_file
,
2233 if (close (text_plain_reply
) == OK
&&
2234 close (addl_reply
) == OK
) {
2235 /* If appended text needed 8-bit but first text didn't,
2236 propagate the 8-bit indication. */
2237 if ((*text_plain_ct
)->c_reqencoding
== CE_7BIT
&&
2238 reply_ct
->c_reqencoding
== CE_8BIT
) {
2239 (*text_plain_ct
)->c_reqencoding
= CE_8BIT
;
2242 if (reply_fp
) { fclose (reply_fp
); }
2244 free_content (reply_ct
);
2251 *text_plain_ct
= reply_ct
;
2255 reply_ct
->c_cefile
.ce_file
= reply_file
;
2256 reply_ct
->c_cefile
.ce_fp
= reply_fp
;
2257 reply_ct
->c_cefile
.ce_unlink
= 1;
2259 /* Attach the new part to the parent multipart/mixed, "m". */
2261 part
->mp_part
= reply_ct
;
2265 for (p
= m
->mp_parts
; p
&& p
->mp_next
; p
= p
->mp_next
) { continue; }
2273 /* Extract any Content-Type header from beginning of convert output. */
2275 extract_headers (CT ct
, char *reply_file
, FILE **reply_fp
)
2277 char *buffer
= NULL
, *cp
, *end_of_header
;
2278 bool found_header
= false;
2279 struct stat statbuf
;
2281 /* Read the convert reply from the file to memory. */
2282 if (stat (reply_file
, &statbuf
) == NOTOK
) {
2283 admonish (reply_file
, "failed to stat");
2284 goto failed_to_extract_ct
;
2287 buffer
= mh_xmalloc (statbuf
.st_size
+ 1);
2289 if ((*reply_fp
= fopen (reply_file
, "r+")) == NULL
||
2290 fread (buffer
, 1, (size_t) statbuf
.st_size
, *reply_fp
) <
2291 (size_t) statbuf
.st_size
) {
2292 admonish (reply_file
, "failed to read");
2293 goto failed_to_extract_ct
;
2295 buffer
[statbuf
.st_size
] = '\0';
2297 /* Look for a header in the convert reply. */
2298 if (strncasecmp (buffer
, TYPE_FIELD
, LEN(TYPE_FIELD
)) == 0 &&
2299 buffer
[LEN(TYPE_FIELD
)] == ':') {
2300 if ((end_of_header
= strstr (buffer
, "\r\n\r\n"))) {
2302 found_header
= true;
2303 } else if ((end_of_header
= strstr (buffer
, "\n\n"))) {
2305 found_header
= true;
2315 /* Truncate buffer to just the C-T. */
2316 *end_of_header
= '\0';
2317 n
= strlen (buffer
);
2319 if (get_ctinfo (buffer
+ 14, ct
, 0) != OK
) {
2320 inform("unable to get content info for reply, continuing...");
2321 goto failed_to_extract_ct
;
2324 /* Hack. Use parse_mime() to detect the type/subtype of the
2325 reply, which we'll use below. */
2326 tmp_file
= getcpy (m_mktemp2 (NULL
, invo_name
, NULL
, NULL
));
2327 tmp_f
= fopen(tmp_file
, "w");
2329 goto failed_to_extract_ct
;
2330 written
= fwrite(buffer
, 1, n
, tmp_f
);
2333 goto failed_to_extract_ct
;
2335 tmp_ct
= parse_mime (tmp_file
);
2337 /* The type and subtype were detected from the reply
2338 using parse_mime() above. */
2339 ct
->c_type
= tmp_ct
->c_type
;
2340 ct
->c_subtype
= tmp_ct
->c_subtype
;
2341 free_content (tmp_ct
);
2346 /* Rewrite the content without the header. */
2347 cp
= end_of_header
+ 1;
2350 if (fwrite (cp
, 1, statbuf
.st_size
- (cp
- buffer
), *reply_fp
) <
2351 (size_t) (statbuf
.st_size
- (cp
- buffer
))) {
2352 admonish (reply_file
, "failed to write");
2353 goto failed_to_extract_ct
;
2356 if (ftruncate (fileno (*reply_fp
), statbuf
.st_size
- (cp
- buffer
)) !=
2358 advise (reply_file
, "ftruncate");
2359 goto failed_to_extract_ct
;
2362 /* No header section, assume the reply is text/plain. */
2363 ct
->c_type
= CT_TEXT
;
2364 ct
->c_subtype
= TEXT_PLAIN
;
2365 if (get_ctinfo ("text/plain", ct
, 0) == NOTOK
) {
2366 /* This never should fail, but just in case. */
2367 die("unable to get content info for reply");
2371 /* free_encoding() will close reply_fp, which is passed through
2372 ct->c_cefile.ce_fp. */
2376 failed_to_extract_ct
:
2377 if (*reply_fp
) { fclose (*reply_fp
); }