]>
diplodocus.org Git - nmh/blob - uip/mhbuildsbr.c
555e3b5c673d9fe70cac83acd493a09c5f71badb
3 * mhbuildsbr.c -- routines to expand/translate MIME composition files
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
11 * This code was originally part of mhn.c. I split it into
12 * a separate program (mhbuild.c) and then later split it
13 * again (mhbuildsbr.c). But the code still has some of
14 * the mhn.c code in it. This program needs additional
15 * streamlining and removal of unneeded code.
20 #include <h/signals.h>
25 #include <h/mhparse.h>
28 #ifdef HAVE_SYS_TIME_H
29 # include <sys/time.h>
38 extern int contentidsw
;
41 extern int rcachesw
; /* mhcachesbr.c */
42 extern int wcachesw
; /* mhcachesbr.c */
46 static char prefix
[] = "----- =_aaaaaaaaaa";
50 struct attach_list
*next
;
54 * Maximum size of URL token in message/external-body
57 #define MAXURLTOKEN 40
61 void content_error (char *, CT
, char *, ...);
64 int find_cache (CT
, int, int *, char *, char *, int);
67 void free_ctinfo (CT
);
68 void free_encoding (CT
, int);
73 static int init_decoded_content (CT
);
74 static void setup_attach_content(CT
, char *);
75 static char *fgetstr (char *, int, FILE *);
76 static int user_content (FILE *, char *, CT
*);
77 static void set_id (CT
, int);
78 static int compose_content (CT
, int);
79 static int scan_content (CT
, size_t);
80 static int build_headers (CT
, int);
81 static char *calculate_digest (CT
, int);
84 static unsigned char directives_stack
[32];
85 static unsigned int directives_index
;
87 static int do_direct(void)
89 return directives_stack
[directives_index
];
92 static void directive_onoff(int onoff
)
94 if (directives_index
>= sizeof(directives_stack
) - 1) {
95 fprintf(stderr
, "mhbuild: #on/off overflow, continuing\n");
98 directives_stack
[++directives_index
] = onoff
;
101 static void directive_init(int onoff
)
103 directives_index
= 0;
104 directives_stack
[0] = onoff
;
107 static void directive_pop(void)
109 if (directives_index
> 0)
112 fprintf(stderr
, "mhbuild: #pop underflow, continuing\n");
116 * Main routine for translating composition file
117 * into valid MIME message. It translates the draft
118 * into a content structure (actually a tree of content
119 * structures). This message then can be manipulated
120 * in various ways, including being output via
125 build_mime (char *infile
, int autobuild
, int dist
, int directives
,
126 int header_encoding
, size_t maxunencoded
, int verbose
)
129 char buf
[BUFSIZ
], name
[NAMESZ
];
136 m_getfld_state_t gstate
= 0;
137 struct attach_list
*attach_head
= NULL
, *attach_tail
= NULL
, *at_entry
;
139 directive_init(directives
);
141 umask (~m_gmprot ());
143 /* open the composition draft */
144 if ((in
= fopen (infile
, "r")) == NULL
)
145 adios (infile
, "unable to open for reading");
148 * Allocate space for primary (outside) content
150 if ((ct
= (CT
) calloc (1, sizeof(*ct
))) == NULL
)
151 adios (NULL
, "out of memory");
154 * Allocate structure for handling decoded content
155 * for this part. We don't really need this, but
156 * allocate it to remain consistent.
158 init_decoded_content (ct
);
161 * Parse some of the header fields in the composition
162 * draft into the linked list of header fields for
163 * the new MIME message.
165 m_getfld_track_filepos (&gstate
, in
);
166 for (compnum
= 1;;) {
167 int bufsz
= sizeof buf
;
168 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
)) {
173 /* abort if draft has Mime-Version or C-T-E header field */
174 if (strcasecmp (name
, VRSN_FIELD
) == 0 ||
175 strcasecmp (name
, ENCODING_FIELD
) == 0) {
180 adios (NULL
, "draft shouldn't contain %s: field", name
);
184 /* ignore any Content-Type fields in the header */
185 if (!strcasecmp (name
, TYPE_FIELD
)) {
186 while (state
== FLDPLUS
) {
188 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
193 /* get copies of the buffers */
194 np
= add (name
, NULL
);
195 vp
= add (buf
, NULL
);
197 /* if necessary, get rest of field */
198 while (state
== FLDPLUS
) {
200 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
201 vp
= add (buf
, vp
); /* add to previous value */
205 * Now add the header data to the list, unless it's an attach
206 * header; in that case, add it to our attach list
209 if (strcasecmp(ATTACH_FIELD
, np
) == 0) {
210 struct attach_list
*entry
;
211 char *s
= vp
, *e
= vp
+ strlen(vp
) - 1;
215 * Make sure we can find the start of this filename.
216 * If it's blank, we skip completely. Otherwise, strip
217 * off any leading spaces and trailing newlines.
220 while (isspace((unsigned char) *s
))
223 while (e
> s
&& *e
== '\n')
231 entry
= mh_xmalloc(sizeof(*entry
));
232 entry
->filename
= getcpy(s
);
237 attach_tail
->next
= entry
;
240 attach_head
= attach_tail
= entry
;
243 add_header (ct
, np
, vp
);
247 /* if this wasn't the last header field, then continue */
251 fseek (in
, (long) (-strlen (buf
)), SEEK_CUR
);
258 adios (NULL
, "message format error in component #%d", compnum
);
261 adios (NULL
, "getfld() returned %d", state
);
265 m_getfld_state_destroy (&gstate
);
268 * Iterate through the list of headers and call the function to MIME-ify
272 for (hp
= ct
->c_first_hf
; hp
!= NULL
; hp
= hp
->next
) {
273 if (encode_rfc2047(hp
->name
, &hp
->value
, header_encoding
, NULL
)) {
274 adios(NULL
, "Unable to encode header \"%s\"", hp
->name
);
279 * Now add the MIME-Version header field
280 * to the list of header fields.
284 np
= add (VRSN_FIELD
, NULL
);
285 vp
= concat (" ", VRSN_VALUE
, "\n", NULL
);
286 add_header (ct
, np
, vp
);
290 * We initally assume we will find multiple contents in the
291 * draft. So create a multipart/mixed content to hold everything.
292 * We can remove this later, if it is not needed.
294 if (get_ctinfo ("multipart/mixed", ct
, 0) == NOTOK
)
296 ct
->c_type
= CT_MULTIPART
;
297 ct
->c_subtype
= MULTI_MIXED
;
298 ct
->c_file
= add (infile
, NULL
);
300 if ((m
= (struct multipart
*) calloc (1, sizeof(*m
))) == NULL
)
301 adios (NULL
, "out of memory");
302 ct
->c_ctparams
= (void *) m
;
306 * read and parse the composition file
307 * and the directives it contains.
309 while (fgetstr (buf
, sizeof(buf
) - 1, in
)) {
313 if (user_content (in
, buf
, &p
) == DONE
) {
314 admonish (NULL
, "ignoring spurious #end");
320 if ((part
= (struct part
*) calloc (1, sizeof(*part
))) == NULL
)
321 adios (NULL
, "out of memory");
328 * Add any Attach headers to the list of MIME parts at the end of the
332 for (at_entry
= attach_head
; at_entry
; ) {
333 struct attach_list
*at_prev
= at_entry
;
337 if (access(at_entry
->filename
, R_OK
) != 0) {
338 adios("reading", "Unable to open %s for", at_entry
->filename
);
341 if ((p
= (CT
) calloc (1, sizeof(*p
))) == NULL
)
342 adios(NULL
, "out of memory");
344 init_decoded_content(p
);
347 * Initialize our content structure based on the filename,
348 * and fill in all of the relevant fields. Also place MIME
349 * parameters in the attributes array.
352 setup_attach_content(p
, at_entry
->filename
);
354 if ((part
= (struct part
*) calloc (1, sizeof(*part
))) == NULL
)
355 adios (NULL
, "out of memory");
360 at_entry
= at_entry
->next
;
361 free(at_prev
->filename
);
366 * To allow for empty message bodies, if we've found NO content at all
367 * yet cook up an empty text/plain part.
375 if ((p
= (CT
) calloc (1, sizeof(*p
))) == NULL
)
376 adios(NULL
, "out of memory");
378 init_decoded_content(p
);
380 if (get_ctinfo ("text/plain", p
, 0) == NOTOK
)
384 p
->c_subtype
= TEXT_PLAIN
;
385 p
->c_encoding
= CE_7BIT
;
386 p
->c_file
= getcpy(infile
);
388 * Sigh. ce_file contains the "decoded" contents of this part.
389 * So this seems like the best option available since we're going
390 * to call scan_content() on this.
392 p
->c_cefile
.ce_file
= getcpy("/dev/null");
393 p
->c_begin
= ftell(in
);
394 p
->c_end
= ftell(in
);
396 if ((t
= (struct text
*) calloc (1, sizeof (*t
))) == NULL
)
397 adios (NULL
, "out of memory");
399 t
->tx_charset
= CHARSET_SPECIFIED
;
402 if ((part
= (struct part
*) calloc (1, sizeof(*part
))) == NULL
)
403 adios (NULL
, "out of memory");
410 * close the composition draft since
411 * it's not needed any longer.
416 * If only one content was found, then remove and
417 * free the outer multipart content.
419 if (!m
->mp_parts
->mp_next
) {
422 p
= m
->mp_parts
->mp_part
;
423 m
->mp_parts
->mp_part
= NULL
;
425 /* move header fields */
426 p
->c_first_hf
= ct
->c_first_hf
;
427 p
->c_last_hf
= ct
->c_last_hf
;
428 ct
->c_first_hf
= NULL
;
429 ct
->c_last_hf
= NULL
;
438 * Fill out, or expand directives. Parse and execute
439 * commands specified by profile composition strings.
441 compose_content (ct
, verbose
);
443 if ((cp
= strchr(prefix
, 'a')) == NULL
)
444 adios (NULL
, "internal error(4)");
447 * Scan the contents. Choose a transfer encoding, and
448 * check if prefix for multipart boundary clashes with
449 * any of the contents.
451 while (scan_content (ct
, maxunencoded
) == NOTOK
) {
456 adios (NULL
, "giving up trying to find a unique delimiter string");
462 /* Build the rest of the header field structures */
464 build_headers (ct
, header_encoding
);
471 * Set up structures for placing unencoded
472 * content when building parts.
476 init_decoded_content (CT ct
)
478 ct
->c_ceopenfnx
= open7Bit
; /* since unencoded */
479 ct
->c_ceclosefnx
= close_encoding
;
480 ct
->c_cesizefnx
= NULL
; /* since unencoded */
487 fgetstr (char *s
, int n
, FILE *stream
)
493 for (ep
= (cp
= s
) + o_n
; cp
< ep
; ) {
496 if (!fgets (cp
, n
, stream
))
497 return (cp
!= s
? s
: NULL
);
499 if (cp
== s
&& *cp
!= '#')
502 cp
+= (i
= strlen (cp
)) - 1;
503 if (i
<= 1 || *cp
-- != '\n' || *cp
!= '\\')
509 if (strcmp(s
, "#on\n") == 0) {
511 } else if (strcmp(s
, "#off\n") == 0) {
513 } else if (strcmp(s
, "#pop\n") == 0) {
525 * Parse the composition draft for text and directives.
526 * Do initial setup of Content structure.
530 user_content (FILE *in
, char *buf
, CT
*ctp
)
538 struct str2init
*s2i
;
543 if (buf
[0] == '\n' || (do_direct() && strcmp (buf
, "#\n") == 0)) {
548 /* allocate basic Content structure */
549 if ((ct
= (CT
) calloc (1, sizeof(*ct
))) == NULL
)
550 adios (NULL
, "out of memory");
553 /* allocate basic structure for handling decoded content */
554 init_decoded_content (ct
);
561 * Handle inline text. Check if line
562 * is one of the following forms:
564 * 1) doesn't begin with '#' (implicit directive)
565 * 2) begins with "##" (implicit directive)
566 * 3) begins with "#<"
568 if (!do_direct() || buf
[0] != '#' || buf
[1] == '#' || buf
[1] == '<') {
572 char content
[BUFSIZ
];
576 if ((cp
= m_mktemp2(NULL
, invo_name
, NULL
, &out
)) == NULL
) {
577 adios("mhbuildsbr", "unable to create temporary file in %s",
581 /* use a temp file to collect the plain text lines */
582 ce
->ce_file
= add (cp
, NULL
);
585 if (do_direct() && (buf
[0] == '#' && buf
[1] == '<')) {
586 strncpy (content
, buf
+ 2, sizeof(content
));
593 /* the directive is implicit */
594 strncpy (content
, "text/plain", sizeof(content
));
596 strncpy (buffer
, (!do_direct() || buf
[0] != '#') ? buf
: buf
+ 1, sizeof(buffer
));
600 if (headers
>= 0 && do_direct() && uprf (buffer
, DESCR_FIELD
)
601 && buffer
[i
= strlen (DESCR_FIELD
)] == ':') {
605 ct
->c_descr
= add (buffer
+ i
+ 1, ct
->c_descr
);
606 if (!fgetstr (buffer
, sizeof(buffer
) - 1, in
))
607 adios (NULL
, "end-of-file after %s: field in plaintext", DESCR_FIELD
);
615 adios (NULL
, "#-directive after %s: field in plaintext", DESCR_FIELD
);
623 if (headers
>= 0 && do_direct() && uprf (buffer
, DISPO_FIELD
)
624 && buffer
[i
= strlen (DISPO_FIELD
)] == ':') {
628 ct
->c_dispo
= add (buffer
+ i
+ 1, ct
->c_dispo
);
629 if (!fgetstr (buffer
, sizeof(buffer
) - 1, in
))
630 adios (NULL
, "end-of-file after %s: field in plaintext", DISPO_FIELD
);
638 adios (NULL
, "#-directive after %s: field in plaintext", DISPO_FIELD
);
646 if (headers
!= 1 || buffer
[0] != '\n')
652 if ((cp
= fgetstr (buffer
, sizeof(buffer
) - 1, in
)) == NULL
)
654 if (do_direct() && buffer
[0] == '#') {
657 if (buffer
[1] != '#')
659 for (cp
= (bp
= buffer
) + 1; *cp
; cp
++)
666 ct
->c_end
= ftell (out
);
669 /* parse content type */
670 if (get_ctinfo (content
, ct
, inlineD
) == NOTOK
)
673 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
674 if (!strcasecmp (ci
->ci_type
, s2i
->si_key
))
676 if (!s2i
->si_key
&& !uprf (ci
->ci_type
, "X-"))
680 * check type specified (possibly implicitly)
682 switch (ct
->c_type
= s2i
->si_val
) {
684 if (!strcasecmp (ci
->ci_subtype
, "rfc822")) {
685 ct
->c_encoding
= CE_7BIT
;
690 adios (NULL
, "it doesn't make sense to define an in-line %s content",
691 ct
->c_type
== CT_MESSAGE
? "message" : "multipart");
696 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
697 (*ct
->c_ctinitfnx
) (ct
);
702 fseek (in
, pos
, SEEK_SET
);
707 * If we've reached this point, the next line
708 * must be some type of explicit directive.
711 /* check if directive is external-type */
712 extrnal
= (buf
[1] == '@');
714 /* parse directive */
715 if (get_ctinfo (buf
+ (extrnal
? 2 : 1), ct
, 1) == NOTOK
)
718 /* check directive against the list of MIME types */
719 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
720 if (!strcasecmp (ci
->ci_type
, s2i
->si_key
))
724 * Check if the directive specified a valid type.
725 * This will happen if it was one of the following forms:
732 adios (NULL
, "missing subtype in \"#%s\"", ci
->ci_type
);
734 switch (ct
->c_type
= s2i
->si_val
) {
736 adios (NULL
, "use \"#begin ... #end\" instead of \"#%s/%s\"",
737 ci
->ci_type
, ci
->ci_subtype
);
741 if (!strcasecmp (ci
->ci_subtype
, "partial"))
742 adios (NULL
, "sorry, \"#%s/%s\" isn't supported",
743 ci
->ci_type
, ci
->ci_subtype
);
744 if (!strcasecmp (ci
->ci_subtype
, "external-body"))
745 adios (NULL
, "use \"#@type/subtype ... [] ...\" instead of \"#%s/%s\"",
746 ci
->ci_type
, ci
->ci_subtype
);
749 "use \"#forw [+folder] [msgs]\" instead of \"#%s/%s\"",
750 ci
->ci_type
, ci
->ci_subtype
);
754 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
755 (*ct
->c_ctinitfnx
) (ct
);
760 * #@type/subtype (external types directive)
767 adios (NULL
, "need external information for \"#@%s/%s\"",
768 ci
->ci_type
, ci
->ci_subtype
);
771 snprintf (buffer
, sizeof(buffer
), "message/external-body; %s", ci
->ci_magic
);
776 * Since we are using the current Content structure to
777 * hold information about the type of the external
778 * reference, we need to create another Content structure
779 * for the message/external-body to wrap it in.
781 if ((ct
= (CT
) calloc (1, sizeof(*ct
))) == NULL
)
782 adios (NULL
, "out of memory");
785 if (get_ctinfo (buffer
, ct
, 0) == NOTOK
)
787 ct
->c_type
= CT_MESSAGE
;
788 ct
->c_subtype
= MESSAGE_EXTERNAL
;
790 if ((e
= (struct exbody
*) calloc (1, sizeof(*e
))) == NULL
)
791 adios (NULL
, "out of memory");
792 ct
->c_ctparams
= (void *) e
;
798 if (params_external (ct
, 1) == NOTOK
)
804 /* Handle [file] argument */
806 /* check if specifies command to execute */
807 if (*ci
->ci_magic
== '|' || *ci
->ci_magic
== '!') {
808 for (cp
= ci
->ci_magic
+ 1; isspace ((unsigned char) *cp
); cp
++)
811 adios (NULL
, "empty pipe command for #%s directive", ci
->ci_type
);
816 /* record filename of decoded contents */
817 ce
->ce_file
= ci
->ci_magic
;
818 if (access (ce
->ce_file
, R_OK
) == NOTOK
)
819 adios ("reading", "unable to access %s for", ce
->ce_file
);
820 if (listsw
&& stat (ce
->ce_file
, &st
) != NOTOK
)
821 ct
->c_end
= (long) st
.st_size
;
828 * No [file] argument, so check profile for
829 * method to compose content.
831 snprintf (buffer
, sizeof(buffer
), "%s-compose-%s/%s",
832 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
833 if ((cp
= context_find (buffer
)) == NULL
|| *cp
== '\0') {
834 snprintf (buffer
, sizeof(buffer
), "%s-compose-%s", invo_name
, ci
->ci_type
);
835 if ((cp
= context_find (buffer
)) == NULL
|| *cp
== '\0') {
836 content_error (NULL
, ct
, "don't know how to compose content");
840 ci
->ci_magic
= add (cp
, NULL
);
845 adios (NULL
, "external definition not allowed for \"#%s\"", ci
->ci_type
);
849 * #forw [+folder] [msgs]
851 if (!strcasecmp (ci
->ci_type
, "forw")) {
853 char *folder
, *arguments
[MAXARGS
];
857 ap
= brkstring (ci
->ci_magic
, " ", "\n");
858 copyip (ap
, arguments
, MAXARGS
);
860 arguments
[0] = "cur";
865 /* search the arguments for a folder name */
866 for (ap
= arguments
; *ap
; ap
++) {
868 if (*cp
== '+' || *cp
== '@') {
870 adios (NULL
, "only one folder per #forw directive");
872 folder
= pluspath (cp
);
876 /* else, use the current folder */
878 folder
= add (getfolder (1), NULL
);
880 if (!(mp
= folder_read (folder
, 0)))
881 adios (NULL
, "unable to read folder %s", folder
);
882 for (ap
= arguments
; *ap
; ap
++) {
884 if (*cp
!= '+' && *cp
!= '@')
885 if (!m_convert (mp
, cp
))
892 * If there is more than one message to include, make this
893 * a content of type "multipart/digest" and insert each message
894 * as a subpart. If there is only one message, then make this
895 * a content of type "message/rfc822".
897 if (mp
->numsel
> 1) {
898 /* we are forwarding multiple messages */
899 if (get_ctinfo ("multipart/digest", ct
, 0) == NOTOK
)
901 ct
->c_type
= CT_MULTIPART
;
902 ct
->c_subtype
= MULTI_DIGEST
;
904 if ((m
= (struct multipart
*) calloc (1, sizeof(*m
))) == NULL
)
905 adios (NULL
, "out of memory");
906 ct
->c_ctparams
= (void *) m
;
909 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
910 if (is_selected(mp
, msgnum
)) {
915 if ((p
= (CT
) calloc (1, sizeof(*p
))) == NULL
)
916 adios (NULL
, "out of memory");
917 init_decoded_content (p
);
919 if (get_ctinfo ("message/rfc822", p
, 0) == NOTOK
)
921 p
->c_type
= CT_MESSAGE
;
922 p
->c_subtype
= MESSAGE_RFC822
;
924 snprintf (buffer
, sizeof(buffer
), "%s/%d", mp
->foldpath
, msgnum
);
925 pe
->ce_file
= add (buffer
, NULL
);
926 if (listsw
&& stat (pe
->ce_file
, &st
) != NOTOK
)
927 p
->c_end
= (long) st
.st_size
;
929 if ((part
= (struct part
*) calloc (1, sizeof(*part
))) == NULL
)
930 adios (NULL
, "out of memory");
937 /* we are forwarding one message */
938 if (get_ctinfo ("message/rfc822", ct
, 0) == NOTOK
)
940 ct
->c_type
= CT_MESSAGE
;
941 ct
->c_subtype
= MESSAGE_RFC822
;
944 snprintf (buffer
, sizeof(buffer
), "%s/%d", mp
->foldpath
, msgnum
);
945 ce
->ce_file
= add (buffer
, NULL
);
946 if (listsw
&& stat (ce
->ce_file
, &st
) != NOTOK
)
947 ct
->c_end
= (long) st
.st_size
;
950 folder_free (mp
); /* free folder/message structure */
957 if (!strcasecmp (ci
->ci_type
, "end")) {
964 * #begin [ alternative | parallel ]
966 if (!strcasecmp (ci
->ci_type
, "begin")) {
969 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
970 } else if (!strcasecmp (ci
->ci_magic
, "alternative")) {
971 vrsn
= MULTI_ALTERNATE
;
972 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
973 } else if (!strcasecmp (ci
->ci_magic
, "parallel")) {
974 vrsn
= MULTI_PARALLEL
;
975 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
976 } else if (uprf (ci
->ci_magic
, "digest")) {
979 vrsn
= MULTI_UNKNOWN
;
984 snprintf (buffer
, sizeof(buffer
), "multipart/%s", cp
);
985 if (get_ctinfo (buffer
, ct
, 0) == NOTOK
)
987 ct
->c_type
= CT_MULTIPART
;
988 ct
->c_subtype
= vrsn
;
990 if ((m
= (struct multipart
*) calloc (1, sizeof(*m
))) == NULL
)
991 adios (NULL
, "out of memory");
992 ct
->c_ctparams
= (void *) m
;
995 while (fgetstr (buffer
, sizeof(buffer
) - 1, in
)) {
999 if (user_content (in
, buffer
, &p
) == DONE
) {
1001 adios (NULL
, "empty \"#begin ... #end\" sequence");
1007 if ((part
= (struct part
*) calloc (1, sizeof(*part
))) == NULL
)
1008 adios (NULL
, "out of memory");
1010 pp
= &part
->mp_next
;
1013 admonish (NULL
, "premature end-of-file, missing #end");
1020 adios (NULL
, "unknown directive \"#%s\"", ci
->ci_type
);
1021 return NOTOK
; /* NOT REACHED */
1026 set_id (CT ct
, int top
)
1028 char contentid
[BUFSIZ
];
1030 static time_t clock
= 0;
1031 static char *msgfmt
;
1035 snprintf (contentid
, sizeof(contentid
), "%s\n", message_id (clock
, 1));
1037 msgfmt
= getcpy(contentid
);
1039 snprintf (contentid
, sizeof(contentid
), msgfmt
, top
? 0 : ++partno
);
1040 ct
->c_id
= getcpy (contentid
);
1045 * Fill out, or expand the various contents in the composition
1046 * draft. Read-in any necessary files. Parse and execute any
1047 * commands specified by profile composition strings.
1051 compose_content (CT ct
, int verbose
)
1053 CE ce
= &ct
->c_cefile
;
1055 switch (ct
->c_type
) {
1060 char partnam
[BUFSIZ
];
1061 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1065 snprintf (partnam
, sizeof(partnam
), "%s.", ct
->c_partno
);
1066 pp
= partnam
+ strlen (partnam
);
1071 /* first, we call compose_content on all the subparts */
1072 for (part
= m
->mp_parts
, partnum
= 1; part
; part
= part
->mp_next
, partnum
++) {
1073 CT p
= part
->mp_part
;
1075 sprintf (pp
, "%d", partnum
);
1076 p
->c_partno
= add (partnam
, NULL
);
1077 if (compose_content (p
, verbose
) == NOTOK
)
1082 * If the -rfc934mode switch is given, then check all
1083 * the subparts of a multipart/digest. If they are all
1084 * message/rfc822, then mark this content and all
1085 * subparts with the rfc934 compatibility mode flag.
1087 if (rfc934sw
&& ct
->c_subtype
== MULTI_DIGEST
) {
1090 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1091 CT p
= part
->mp_part
;
1093 if (p
->c_subtype
!= MESSAGE_RFC822
) {
1098 ct
->c_rfc934
= is934
;
1099 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1100 CT p
= part
->mp_part
;
1102 if ((p
->c_rfc934
= is934
))
1108 ct
->c_end
= (partnum
= strlen (prefix
) + 2) + 2;
1112 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
)
1113 ct
->c_end
+= part
->mp_part
->c_end
+ partnum
;
1119 /* Nothing to do for type message */
1123 * Discrete types (text/application/audio/image/video)
1128 int i
, xstdout
, len
, buflen
;
1130 char *vec
[4], buffer
[BUFSIZ
];
1132 CI ci
= &ct
->c_ctinfo
;
1135 if (!(cp
= ci
->ci_magic
))
1136 adios (NULL
, "internal error(5)");
1138 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1139 adios("mhbuildsbr", "unable to create temporary file in %s",
1142 ce
->ce_file
= add (tfile
, NULL
);
1147 /* Get buffer ready to go */
1150 buflen
= sizeof(buffer
);
1153 * Parse composition string into buffer
1155 for ( ; *cp
; cp
++) {
1160 /* insert parameters from directive */
1164 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
1165 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
,
1166 pm
->pm_name
, get_param_value(pm
, '?'));
1176 /* %f, and stdout is not-redirected */
1182 * insert temporary filename where
1183 * content should be written
1185 snprintf (bp
, buflen
, "%s", ce
->ce_file
);
1189 /* insert content subtype */
1190 strncpy (bp
, ci
->ci_subtype
, buflen
);
1194 /* insert character % */
1215 printf ("composing content %s/%s from command\n\t%s\n",
1216 ci
->ci_type
, ci
->ci_subtype
, buffer
);
1218 fflush (stdout
); /* not sure if need for -noverbose */
1225 if ((out
= fopen (ce
->ce_file
, "w")) == NULL
)
1226 adios (ce
->ce_file
, "unable to open for writing");
1228 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
> 5; i
++)
1232 adios ("fork", "unable to fork");
1237 dup2 (fileno (out
), 1);
1238 close (fileno (out
));
1239 execvp ("/bin/sh", vec
);
1240 fprintf (stderr
, "unable to exec ");
1247 if (pidXwait(child_id
, NULL
))
1253 /* Check size of file */
1254 if (listsw
&& ct
->c_end
== 0L) {
1257 if (stat (ce
->ce_file
, &st
) != NOTOK
)
1258 ct
->c_end
= (long) st
.st_size
;
1270 * 1) choose a transfer encoding.
1271 * 2) check for clashes with multipart boundary string.
1272 * 3) for text content, figure out which character set is being used.
1274 * If there is a clash with one of the contents and the multipart boundary,
1275 * this function will exit with NOTOK. This will cause the scanning process
1276 * to be repeated with a different multipart boundary. It is possible
1277 * (although highly unlikely) that this scan will be repeated multiple times.
1281 scan_content (CT ct
, size_t maxunencoded
)
1284 int check8bit
= 0, contains8bit
= 0; /* check if contains 8bit data */
1285 int checklinelen
= 0, linelen
= 0; /* check for long lines */
1286 int checkllinelen
= 0; /* check for extra-long lines */
1287 int checkboundary
= 0, boundaryclash
= 0; /* check if clashes with multipart boundary */
1288 int checklinespace
= 0, linespace
= 0; /* check if any line ends with space */
1289 char *cp
= NULL
, buffer
[BUFSIZ
];
1290 struct text
*t
= NULL
;
1292 CE ce
= &ct
->c_cefile
;
1295 * handle multipart by scanning all subparts
1296 * and then checking their encoding.
1298 if (ct
->c_type
== CT_MULTIPART
) {
1299 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1302 /* initially mark the domain of enclosing multipart as 7bit */
1303 ct
->c_encoding
= CE_7BIT
;
1305 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1306 CT p
= part
->mp_part
;
1308 if (scan_content (p
, maxunencoded
) == NOTOK
) /* choose encoding for subpart */
1311 /* if necessary, enlarge encoding for enclosing multipart */
1312 if (p
->c_encoding
== CE_BINARY
)
1313 ct
->c_encoding
= CE_BINARY
;
1314 if (p
->c_encoding
== CE_8BIT
&& ct
->c_encoding
!= CE_BINARY
)
1315 ct
->c_encoding
= CE_8BIT
;
1322 * Decide what to check while scanning this content. Note that
1323 * for text content we always check for 8bit characters if the
1324 * charset is unspecified, because that controls whether or not the
1325 * character set is us-ascii or retrieved from the locale.
1328 if (ct
->c_type
== CT_TEXT
) {
1329 t
= (struct text
*) ct
->c_ctparams
;
1330 if (t
->tx_charset
== CHARSET_UNSPECIFIED
)
1334 switch (ct
->c_reqencoding
) {
1345 /* Use the default rules based on content-type */
1346 switch (ct
->c_type
) {
1350 if (ct
->c_subtype
== TEXT_PLAIN
) {
1357 case CT_APPLICATION
:
1369 /* don't check anything for message/external */
1370 if (ct
->c_subtype
== MESSAGE_EXTERNAL
)
1380 * Don't check anything for these types,
1381 * since we are forcing use of base64, unless
1382 * the content-type was specified by a mhbuild directive.
1393 * Scan the unencoded content
1395 if (check8bit
|| checklinelen
|| checklinespace
|| checkboundary
||
1397 if ((in
= fopen (ce
->ce_file
, "r")) == NULL
)
1398 adios (ce
->ce_file
, "unable to open for reading");
1399 len
= strlen (prefix
);
1401 while (fgets (buffer
, sizeof(buffer
) - 1, in
)) {
1403 * Check for 8bit data.
1406 for (cp
= buffer
; *cp
; cp
++) {
1407 if (!isascii ((unsigned char) *cp
)) {
1409 check8bit
= 0; /* no need to keep checking */
1415 * Check line length.
1417 if (checklinelen
&& (strlen (buffer
) > maxunencoded
+ 1)) {
1419 checklinelen
= 0; /* no need to keep checking */
1423 * RFC 5322 specifies that a message cannot contain a line
1424 * greater than 998 characters (excluding the CRLF). If we
1425 * get one of those lines and linelen is NOT set, then abort.
1428 if (checkllinelen
&& !linelen
&&
1429 (strlen(buffer
) > MAXLONGLINE
+ 1)) {
1430 adios(NULL
, "Line in content exceeds maximum line limit (%d)",
1435 * Check if line ends with a space.
1437 if (checklinespace
&& (cp
= buffer
+ strlen (buffer
) - 2) > buffer
&& isspace ((unsigned char) *cp
)) {
1439 checklinespace
= 0; /* no need to keep checking */
1443 * Check if content contains a line that clashes
1444 * with our standard boundary for multipart messages.
1446 if (checkboundary
&& buffer
[0] == '-' && buffer
[1] == '-') {
1447 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1448 if (!isspace ((unsigned char) *cp
))
1451 if (!strncmp(buffer
+ 2, prefix
, len
) && isdigit((unsigned char) buffer
[2 + len
])) {
1453 checkboundary
= 0; /* no need to keep checking */
1461 * If the content is text and didn't specify a character set,
1462 * we need to figure out which one was used.
1465 if (ct
->c_type
== CT_TEXT
) {
1466 t
= (struct text
*) ct
->c_ctparams
;
1467 if (t
->tx_charset
== CHARSET_UNSPECIFIED
) {
1468 CI ci
= &ct
->c_ctinfo
;
1470 add_param(&ci
->ci_first_pm
, &ci
->ci_last_pm
, "charset",
1471 contains8bit
? write_charset_8bit() : "us-ascii", 0);
1472 t
->tx_charset
= CHARSET_SPECIFIED
;
1477 * Decide which transfer encoding to use.
1480 if (ct
->c_reqencoding
!= CE_UNKNOWN
)
1481 ct
->c_encoding
= ct
->c_reqencoding
;
1483 switch (ct
->c_type
) {
1485 if (contains8bit
&& !linelen
&& !linespace
&& !checksw
)
1486 ct
->c_encoding
= CE_8BIT
;
1487 else if (contains8bit
|| linelen
|| linespace
|| checksw
)
1488 ct
->c_encoding
= CE_QUOTED
;
1490 ct
->c_encoding
= CE_7BIT
;
1493 case CT_APPLICATION
:
1494 /* For application type, use base64, except when postscript */
1495 if (contains8bit
|| linelen
|| linespace
|| checksw
)
1496 ct
->c_encoding
= (ct
->c_subtype
== APPLICATION_POSTSCRIPT
)
1497 ? CE_QUOTED
: CE_BASE64
;
1499 ct
->c_encoding
= CE_7BIT
;
1503 ct
->c_encoding
= CE_7BIT
;
1509 /* For audio, image, and video contents, just use base64 */
1510 ct
->c_encoding
= CE_BASE64
;
1514 return (boundaryclash
? NOTOK
: OK
);
1519 * Scan the content structures, and build header
1520 * fields that will need to be output into the
1525 build_headers (CT ct
, int header_encoding
)
1527 int cc
, mailbody
, extbody
, len
;
1528 char *np
, *vp
, buffer
[BUFSIZ
];
1529 CI ci
= &ct
->c_ctinfo
;
1532 * If message is type multipart, then add the multipart
1533 * boundary to the list of attribute/value pairs.
1535 if (ct
->c_type
== CT_MULTIPART
) {
1536 static int level
= 0; /* store nesting level */
1538 snprintf (buffer
, sizeof(buffer
), "%s%d", prefix
, level
++);
1539 add_param(&ci
->ci_first_pm
, &ci
->ci_last_pm
, "boundary", buffer
, 0);
1543 * Skip the output of Content-Type, parameters, content
1544 * description and disposition, and Content-ID if the
1545 * content is of type "message" and the rfc934 compatibility
1546 * flag is set (which means we are inside multipart/digest
1547 * and the switch -rfc934mode was given).
1549 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_rfc934
)
1553 * output the content type and subtype
1555 np
= add (TYPE_FIELD
, NULL
);
1556 vp
= concat (" ", ci
->ci_type
, "/", ci
->ci_subtype
, NULL
);
1558 /* keep track of length of line */
1559 len
= strlen (TYPE_FIELD
) + strlen (ci
->ci_type
)
1560 + strlen (ci
->ci_subtype
) + 3;
1562 extbody
= ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_EXTERNAL
;
1563 mailbody
= extbody
&& ((struct exbody
*) ct
->c_ctparams
)->eb_body
;
1566 * Append the attribute/value pairs to
1567 * the end of the Content-Type line.
1570 if (ci
->ci_first_pm
) {
1571 char *s
= output_params(len
, ci
->ci_first_pm
, &len
, mailbody
);
1574 adios(NULL
, "Internal error: failed outputting Content-Type "
1582 * Append any RFC-822 comment to the end of
1583 * the Content-Type line.
1585 if (ci
->ci_comment
) {
1586 snprintf (buffer
, sizeof(buffer
), "(%s)", ci
->ci_comment
);
1587 if (len
+ 1 + (cc
= 2 + strlen (ci
->ci_comment
)) >= CPERLIN
) {
1588 vp
= add ("\n\t", vp
);
1594 vp
= add (buffer
, vp
);
1597 vp
= add ("\n", vp
);
1598 add_header (ct
, np
, vp
);
1601 * output the Content-ID, unless disabled by -nocontentid
1603 if (contentidsw
&& ct
->c_id
) {
1604 np
= add (ID_FIELD
, NULL
);
1605 vp
= concat (" ", ct
->c_id
, NULL
);
1606 add_header (ct
, np
, vp
);
1609 * output the Content-Description
1612 np
= add (DESCR_FIELD
, NULL
);
1613 vp
= concat (" ", ct
->c_descr
, NULL
);
1614 if (encode_rfc2047(DESCR_FIELD
, &vp
, header_encoding
, NULL
))
1615 adios(NULL
, "Unable to encode %s header", DESCR_FIELD
);
1616 add_header (ct
, np
, vp
);
1620 * output the Content-Disposition. If it's NULL but c_dispo_type is
1621 * set, then we need to build it.
1624 np
= add (DISPO_FIELD
, NULL
);
1625 vp
= concat (" ", ct
->c_dispo
, NULL
);
1626 add_header (ct
, np
, vp
);
1627 } else if (ct
->c_dispo_type
) {
1628 vp
= concat (" ", ct
->c_dispo_type
, NULL
);
1629 len
= strlen(DISPO_FIELD
) + strlen(vp
) + 1;
1630 np
= output_params(len
, ct
->c_dispo_first
, NULL
, 0);
1635 add_header (ct
, getcpy(DISPO_FIELD
), vp
);
1640 * If this is the internal content structure for a
1641 * "message/external", then we are done with the
1642 * headers (since it has no body).
1648 * output the Content-MD5
1651 np
= add (MD5_FIELD
, NULL
);
1652 vp
= calculate_digest (ct
, (ct
->c_encoding
== CE_QUOTED
) ? 1 : 0);
1653 add_header (ct
, np
, vp
);
1657 * output the Content-Transfer-Encoding
1659 switch (ct
->c_encoding
) {
1661 /* Nothing to output */
1665 if (ct
->c_type
== CT_MESSAGE
)
1666 adios (NULL
, "internal error, invalid encoding");
1668 np
= add (ENCODING_FIELD
, NULL
);
1669 vp
= concat (" ", "8bit", "\n", NULL
);
1670 add_header (ct
, np
, vp
);
1674 if (ct
->c_type
== CT_MESSAGE
|| ct
->c_type
== CT_MULTIPART
)
1675 adios (NULL
, "internal error, invalid encoding");
1677 np
= add (ENCODING_FIELD
, NULL
);
1678 vp
= concat (" ", "quoted-printable", "\n", NULL
);
1679 add_header (ct
, np
, vp
);
1683 if (ct
->c_type
== CT_MESSAGE
|| ct
->c_type
== CT_MULTIPART
)
1684 adios (NULL
, "internal error, invalid encoding");
1686 np
= add (ENCODING_FIELD
, NULL
);
1687 vp
= concat (" ", "base64", "\n", NULL
);
1688 add_header (ct
, np
, vp
);
1692 if (ct
->c_type
== CT_MESSAGE
)
1693 adios (NULL
, "internal error, invalid encoding");
1695 np
= add (ENCODING_FIELD
, NULL
);
1696 vp
= concat (" ", "binary", "\n", NULL
);
1697 add_header (ct
, np
, vp
);
1701 adios (NULL
, "unknown transfer encoding in content");
1706 * Additional content specific header processing
1708 switch (ct
->c_type
) {
1711 struct multipart
*m
;
1714 m
= (struct multipart
*) ct
->c_ctparams
;
1715 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1719 build_headers (p
, header_encoding
);
1725 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1728 e
= (struct exbody
*) ct
->c_ctparams
;
1729 build_headers (e
->eb_content
, header_encoding
);
1742 static char nib2b64
[0x40+1] =
1743 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1746 calculate_digest (CT ct
, int asciiP
)
1749 char buffer
[BUFSIZ
], *vp
, *op
;
1751 unsigned char digest
[16];
1752 unsigned char outbuf
[25];
1754 CE ce
= &ct
->c_cefile
;
1755 char *infilename
= ce
->ce_file
? ce
->ce_file
: ct
->c_file
;
1759 if ((in
= fopen (infilename
, "r")) == NULL
)
1760 adios (infilename
, "unable to open for reading");
1762 /* Initialize md5 context */
1763 MD5Init (&mdContext
);
1765 /* calculate md5 message digest */
1767 while (fgets (buffer
, sizeof(buffer
) - 1, in
)) {
1770 cp
= buffer
+ strlen (buffer
) - 1;
1771 if ((c
= *cp
) == '\n')
1774 MD5Update (&mdContext
, (unsigned char *) buffer
,
1775 (unsigned int) strlen (buffer
));
1778 MD5Update (&mdContext
, (unsigned char *) "\r\n", 2);
1781 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), in
)) > 0)
1782 MD5Update (&mdContext
, (unsigned char *) buffer
, (unsigned int) cc
);
1785 /* md5 finalization. Write digest and zero md5 context */
1786 MD5Final (digest
, &mdContext
);
1791 /* print debugging info */
1795 fprintf (stderr
, "MD5 digest=");
1796 for (ep
= (dp
= digest
) + sizeof(digest
) / sizeof(digest
[0]);
1798 fprintf (stderr
, "%02x", *dp
& 0xff);
1799 fprintf (stderr
, "\n");
1802 /* encode the digest using base64 */
1803 for (dp
= digest
, op
= (char *) outbuf
,
1804 cc
= sizeof(digest
) / sizeof(digest
[0]);
1805 cc
> 0; cc
-= 3, op
+= 4) {
1809 bits
= (*dp
++ & 0xff) << 16;
1811 bits
|= (*dp
++ & 0xff) << 8;
1813 bits
|= *dp
++ & 0xff;
1816 for (bp
= op
+ 4; bp
> op
; bits
>>= 6)
1817 *--bp
= nib2b64
[bits
& 0x3f];
1825 /* null terminate string */
1828 /* now make copy and return string */
1829 vp
= concat (" ", outbuf
, "\n", NULL
);
1834 * Set things up for the content structure for file "filename" that
1839 setup_attach_content(CT ct
, char *filename
)
1841 char *type
, *simplename
= r1bindex(filename
, '/');
1842 struct str2init
*s2i
;
1845 if (! (type
= mime_type(filename
))) {
1846 adios(NULL
, "Unable to determine MIME type of \"%s\"", filename
);
1850 * Parse the Content-Type. get_ctinfo() parses MIME parameters, but
1851 * since we're just feeding it a MIME type we have to add those ourself.
1852 * Map that to a valid content-type label and call any initialization
1856 if (get_ctinfo(type
, ct
, 0) == NOTOK
)
1861 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
1862 if (strcasecmp(ct
->c_ctinfo
.ci_type
, s2i
->si_key
) == 0)
1864 if (!s2i
->si_key
&& !uprf(ct
->c_ctinfo
.ci_type
, "X-"))
1868 * Make sure the type isn't incompatible with what we can handle
1871 switch (ct
->c_type
= s2i
->si_val
) {
1873 adios (NULL
, "multipart types must be specified by mhbuild directives");
1877 if (strcasecmp(ct
->c_ctinfo
.ci_subtype
, "partial") == 0)
1878 adios(NULL
, "Sorry, %s/%s isn't supported", ct
->c_ctinfo
.ci_type
,
1879 ct
->c_ctinfo
.ci_subtype
);
1880 if (strcasecmp(ct
->c_ctinfo
.ci_subtype
, "external-body") == 0)
1881 adios(NULL
, "external-body messages must be specified "
1882 "by mhbuild directives");
1887 * This sets the subtype, if it's significant
1889 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
1890 (*ct
->c_ctinitfnx
)(ct
);
1895 * Feed in a few attributes; specifically, the name attribute, the
1896 * content-description, and the content-disposition.
1899 for (pm
= ct
->c_ctinfo
.ci_first_pm
; pm
; pm
= pm
->pm_next
) {
1900 if (strcasecmp(pm
->pm_name
, "name") == 0) {
1903 pm
->pm_value
= getcpy(simplename
);
1909 add_param(&ct
->c_ctinfo
.ci_first_pm
, &ct
->c_ctinfo
.ci_last_pm
,
1910 "name", simplename
, 0);
1912 ct
->c_descr
= getcpy(simplename
);
1913 ct
->c_descr
= add("\n", ct
->c_descr
);
1914 ct
->c_cefile
.ce_file
= getcpy(filename
);
1917 * If it's a text/calendar, we need to make sure it's an inline,
1918 * otherwise it won't work with some calendar programs. Otherwise
1922 if (strcasecmp(ct
->c_ctinfo
.ci_type
, "text") == 0 &&
1923 strcasecmp(ct
->c_ctinfo
.ci_subtype
, "calendar") == 0) {
1924 ct
->c_dispo_type
= getcpy("inline");
1926 ct
->c_dispo_type
= getcpy("attachment");
1929 add_param(&ct
->c_dispo_first
, &ct
->c_dispo_last
, "filename", simplename
, 0);