]>
diplodocus.org Git - nmh/blob - uip/mhbuildsbr.c
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>
39 extern int contentidsw
;
42 extern int rcachesw
; /* mhcachesbr.c */
43 extern int wcachesw
; /* mhcachesbr.c */
46 * Directory to place tmp files. This must
47 * be set before these routines are called.
53 static char prefix
[] = "----- =_aaaaaaaaaa";
57 struct attach_list
*next
;
61 * Maximum size of URL token in message/external-body
64 #define MAXURLTOKEN 40
68 void content_error (char *, CT
, char *, ...);
71 int find_cache (CT
, int, int *, char *, char *, int);
74 void free_ctinfo (CT
);
75 void free_encoding (CT
, int);
80 static int init_decoded_content (CT
);
81 static void setup_attach_content(CT
, char *);
82 static char *fgetstr (char *, int, FILE *);
83 static int user_content (FILE *, char *, char *, CT
*);
84 static void set_id (CT
, int);
85 static int compose_content (CT
);
86 static int scan_content (CT
);
87 static int build_headers (CT
);
88 static char *calculate_digest (CT
, int);
91 static unsigned char directives_stack
[32];
92 static unsigned int directives_index
;
94 static int do_direct(void)
96 return directives_stack
[directives_index
];
99 static void directive_onoff(int onoff
)
101 if (directives_index
>= sizeof(directives_stack
) - 1) {
102 fprintf(stderr
, "mhbuild: #on/off overflow, continuing\n");
105 directives_stack
[++directives_index
] = onoff
;
108 static void directive_init(int onoff
)
110 directives_index
= 0;
111 directives_stack
[0] = onoff
;
114 static void directive_pop(void)
116 if (directives_index
> 0)
119 fprintf(stderr
, "mhbuild: #pop underflow, continuing\n");
123 * Main routine for translating composition file
124 * into valid MIME message. It translates the draft
125 * into a content structure (actually a tree of content
126 * structures). This message then can be manipulated
127 * in various ways, including being output via
132 build_mime (char *infile
, int autobuild
, int directives
, int header_encoding
)
135 char buf
[BUFSIZ
], name
[NAMESZ
];
142 m_getfld_state_t gstate
= 0;
143 struct attach_list
*attach_head
= NULL
, *attach_tail
= NULL
, *at_entry
;
145 directive_init(directives
);
147 umask (~m_gmprot ());
149 /* open the composition draft */
150 if ((in
= fopen (infile
, "r")) == NULL
)
151 adios (infile
, "unable to open for reading");
154 * Allocate space for primary (outside) content
156 if ((ct
= (CT
) calloc (1, sizeof(*ct
))) == NULL
)
157 adios (NULL
, "out of memory");
160 * Allocate structure for handling decoded content
161 * for this part. We don't really need this, but
162 * allocate it to remain consistent.
164 init_decoded_content (ct
);
167 * Parse some of the header fields in the composition
168 * draft into the linked list of header fields for
169 * the new MIME message.
171 m_getfld_track_filepos (&gstate
, in
);
172 for (compnum
= 1;;) {
173 int bufsz
= sizeof buf
;
174 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
)) {
179 /* abort if draft has Mime-Version or C-T-E header field */
180 if (strcasecmp (name
, VRSN_FIELD
) == 0 ||
181 strcasecmp (name
, ENCODING_FIELD
) == 0) {
186 adios (NULL
, "draft shouldn't contain %s: field", name
);
190 /* ignore any Content-Type fields in the header */
191 if (!strcasecmp (name
, TYPE_FIELD
)) {
192 while (state
== FLDPLUS
) {
194 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
199 /* get copies of the buffers */
200 np
= add (name
, NULL
);
201 vp
= add (buf
, NULL
);
203 /* if necessary, get rest of field */
204 while (state
== FLDPLUS
) {
206 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
207 vp
= add (buf
, vp
); /* add to previous value */
211 * Now add the header data to the list, unless it's an attach
212 * header; in that case, add it to our attach list
215 if (strcasecmp(ATTACH_FIELD
, np
) == 0) {
216 struct attach_list
*entry
;
217 char *s
= vp
, *e
= vp
+ strlen(vp
) - 1;
221 * Make sure we can find the start of this filename.
222 * If it's blank, we skip completely. Otherwise, strip
223 * off any leading spaces and trailing newlines.
226 while (isspace((unsigned char) *s
))
229 while (e
> s
&& *e
== '\n')
237 entry
= mh_xmalloc(sizeof(*entry
));
238 entry
->filename
= getcpy(s
);
241 attach_tail
->next
= entry
;
244 attach_head
= attach_tail
= entry
;
247 add_header (ct
, np
, vp
);
251 /* if this wasn't the last header field, then continue */
255 fseek (in
, (long) (-strlen (buf
)), SEEK_CUR
);
262 adios (NULL
, "message format error in component #%d", compnum
);
265 adios (NULL
, "getfld() returned %d", state
);
269 m_getfld_state_destroy (&gstate
);
272 * Iterate through the list of headers and call the function to MIME-ify
276 for (hp
= ct
->c_first_hf
; hp
!= NULL
; hp
= hp
->next
) {
277 if (encode_rfc2047(hp
->name
, &hp
->value
, header_encoding
, NULL
)) {
278 adios(NULL
, "Unable to encode header \"%s\"", hp
->name
);
283 * Now add the MIME-Version header field
284 * to the list of header fields.
286 np
= add (VRSN_FIELD
, NULL
);
287 vp
= concat (" ", VRSN_VALUE
, "\n", NULL
);
288 add_header (ct
, np
, vp
);
291 * We initally assume we will find multiple contents in the
292 * draft. So create a multipart/mixed content to hold everything.
293 * We can remove this later, if it is not needed.
295 if (get_ctinfo ("multipart/mixed", ct
, 0) == NOTOK
)
297 ct
->c_type
= CT_MULTIPART
;
298 ct
->c_subtype
= MULTI_MIXED
;
299 ct
->c_file
= add (infile
, NULL
);
301 if ((m
= (struct multipart
*) calloc (1, sizeof(*m
))) == NULL
)
302 adios (NULL
, "out of memory");
303 ct
->c_ctparams
= (void *) m
;
307 * read and parse the composition file
308 * and the directives it contains.
310 while (fgetstr (buf
, sizeof(buf
) - 1, in
)) {
314 if (user_content (in
, infile
, buf
, &p
) == DONE
) {
315 admonish (NULL
, "ignoring spurious #end");
321 if ((part
= (struct part
*) calloc (1, sizeof(*part
))) == NULL
)
322 adios (NULL
, "out of memory");
329 * close the composition draft since
330 * it's not needed any longer.
335 * Add any Attach headers to the list of MIME parts at the end of the
339 for (at_entry
= attach_head
; at_entry
; ) {
340 struct attach_list
*at_prev
= at_entry
;
344 if (access(at_entry
->filename
, R_OK
) != 0) {
345 adios("reading", "Unable to open %s for", at_entry
->filename
);
348 if ((p
= (CT
) calloc (1, sizeof(*p
))) == NULL
)
349 adios(NULL
, "out of memory");
351 init_decoded_content(p
);
354 * Initialize our content structure based on the filename,
355 * and fill in all of the relevant fields. Also place MIME
356 * parameters in the attributes array.
359 setup_attach_content(p
, at_entry
->filename
);
361 if ((part
= (struct part
*) calloc (1, sizeof(*part
))) == NULL
)
362 adios (NULL
, "out of memory");
367 at_entry
= at_entry
->next
;
368 free(at_prev
->filename
);
372 /* check if any contents were found */
374 adios (NULL
, "no content directives found");
377 * If only one content was found, then remove and
378 * free the outer multipart content.
380 if (!m
->mp_parts
->mp_next
) {
383 p
= m
->mp_parts
->mp_part
;
384 m
->mp_parts
->mp_part
= NULL
;
386 /* move header fields */
387 p
->c_first_hf
= ct
->c_first_hf
;
388 p
->c_last_hf
= ct
->c_last_hf
;
389 ct
->c_first_hf
= NULL
;
390 ct
->c_last_hf
= NULL
;
399 * Fill out, or expand directives. Parse and execute
400 * commands specified by profile composition strings.
402 compose_content (ct
);
404 if ((cp
= strchr(prefix
, 'a')) == NULL
)
405 adios (NULL
, "internal error(4)");
408 * Scan the contents. Choose a transfer encoding, and
409 * check if prefix for multipart boundary clashes with
410 * any of the contents.
412 while (scan_content (ct
) == NOTOK
) {
417 adios (NULL
, "giving up trying to find a unique delimiter string");
423 /* Build the rest of the header field structures */
431 * Set up structures for placing unencoded
432 * content when building parts.
436 init_decoded_content (CT ct
)
438 ct
->c_ceopenfnx
= open7Bit
; /* since unencoded */
439 ct
->c_ceclosefnx
= close_encoding
;
440 ct
->c_cesizefnx
= NULL
; /* since unencoded */
447 fgetstr (char *s
, int n
, FILE *stream
)
453 for (ep
= (cp
= s
) + o_n
; cp
< ep
; ) {
456 if (!fgets (cp
, n
, stream
))
457 return (cp
!= s
? s
: NULL
);
459 if (cp
== s
&& *cp
!= '#')
462 cp
+= (i
= strlen (cp
)) - 1;
463 if (i
<= 1 || *cp
-- != '\n' || *cp
!= '\\')
469 if (strcmp(s
, "#on\n") == 0) {
471 } else if (strcmp(s
, "#off\n") == 0) {
473 } else if (strcmp(s
, "#pop\n") == 0) {
485 * Parse the composition draft for text and directives.
486 * Do initial setup of Content structure.
490 user_content (FILE *in
, char *file
, char *buf
, CT
*ctp
)
498 struct str2init
*s2i
;
503 if (buf
[0] == '\n' || (do_direct() && strcmp (buf
, "#\n") == 0)) {
508 /* allocate basic Content structure */
509 if ((ct
= (CT
) calloc (1, sizeof(*ct
))) == NULL
)
510 adios (NULL
, "out of memory");
513 /* allocate basic structure for handling decoded content */
514 init_decoded_content (ct
);
521 * Handle inline text. Check if line
522 * is one of the following forms:
524 * 1) doesn't begin with '#' (implicit directive)
525 * 2) begins with "##" (implicit directive)
526 * 3) begins with "#<"
528 if (!do_direct() || buf
[0] != '#' || buf
[1] == '#' || buf
[1] == '<') {
532 char content
[BUFSIZ
];
536 cp
= m_mktemp2(NULL
, invo_name
, NULL
, &out
);
537 if (cp
== NULL
) adios("mhbuildsbr", "unable to create temporary file");
539 /* use a temp file to collect the plain text lines */
540 ce
->ce_file
= add (cp
, NULL
);
543 if (do_direct() && (buf
[0] == '#' && buf
[1] == '<')) {
544 strncpy (content
, buf
+ 2, sizeof(content
));
551 /* the directive is implicit */
552 strncpy (content
, "text/plain", sizeof(content
));
554 strncpy (buffer
, (!do_direct() || buf
[0] != '#') ? buf
: buf
+ 1, sizeof(buffer
));
558 if (headers
>= 0 && do_direct() && uprf (buffer
, DESCR_FIELD
)
559 && buffer
[i
= strlen (DESCR_FIELD
)] == ':') {
563 ct
->c_descr
= add (buffer
+ i
+ 1, ct
->c_descr
);
564 if (!fgetstr (buffer
, sizeof(buffer
) - 1, in
))
565 adios (NULL
, "end-of-file after %s: field in plaintext", DESCR_FIELD
);
573 adios (NULL
, "#-directive after %s: field in plaintext", DESCR_FIELD
);
581 if (headers
>= 0 && do_direct() && uprf (buffer
, DISPO_FIELD
)
582 && buffer
[i
= strlen (DISPO_FIELD
)] == ':') {
586 ct
->c_dispo
= add (buffer
+ i
+ 1, ct
->c_dispo
);
587 if (!fgetstr (buffer
, sizeof(buffer
) - 1, in
))
588 adios (NULL
, "end-of-file after %s: field in plaintext", DISPO_FIELD
);
596 adios (NULL
, "#-directive after %s: field in plaintext", DISPO_FIELD
);
604 if (headers
!= 1 || buffer
[0] != '\n')
610 if ((cp
= fgetstr (buffer
, sizeof(buffer
) - 1, in
)) == NULL
)
612 if (do_direct() && buffer
[0] == '#') {
615 if (buffer
[1] != '#')
617 for (cp
= (bp
= buffer
) + 1; *cp
; cp
++)
624 ct
->c_end
= ftell (out
);
627 /* parse content type */
628 if (get_ctinfo (content
, ct
, inlineD
) == NOTOK
)
631 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
632 if (!strcasecmp (ci
->ci_type
, s2i
->si_key
))
634 if (!s2i
->si_key
&& !uprf (ci
->ci_type
, "X-"))
638 * check type specified (possibly implicitly)
640 switch (ct
->c_type
= s2i
->si_val
) {
642 if (!strcasecmp (ci
->ci_subtype
, "rfc822")) {
643 ct
->c_encoding
= CE_7BIT
;
648 adios (NULL
, "it doesn't make sense to define an in-line %s content",
649 ct
->c_type
== CT_MESSAGE
? "message" : "multipart");
654 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
655 (*ct
->c_ctinitfnx
) (ct
);
660 fseek (in
, pos
, SEEK_SET
);
665 * If we've reached this point, the next line
666 * must be some type of explicit directive.
669 /* check if directive is external-type */
670 extrnal
= (buf
[1] == '@');
672 /* parse directive */
673 if (get_ctinfo (buf
+ (extrnal
? 2 : 1), ct
, 1) == NOTOK
)
676 /* check directive against the list of MIME types */
677 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
678 if (!strcasecmp (ci
->ci_type
, s2i
->si_key
))
682 * Check if the directive specified a valid type.
683 * This will happen if it was one of the following forms:
690 adios (NULL
, "missing subtype in \"#%s\"", ci
->ci_type
);
692 switch (ct
->c_type
= s2i
->si_val
) {
694 adios (NULL
, "use \"#begin ... #end\" instead of \"#%s/%s\"",
695 ci
->ci_type
, ci
->ci_subtype
);
699 if (!strcasecmp (ci
->ci_subtype
, "partial"))
700 adios (NULL
, "sorry, \"#%s/%s\" isn't supported",
701 ci
->ci_type
, ci
->ci_subtype
);
702 if (!strcasecmp (ci
->ci_subtype
, "external-body"))
703 adios (NULL
, "use \"#@type/subtype ... [] ...\" instead of \"#%s/%s\"",
704 ci
->ci_type
, ci
->ci_subtype
);
707 "use \"#forw [+folder] [msgs]\" instead of \"#%s/%s\"",
708 ci
->ci_type
, ci
->ci_subtype
);
712 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
713 (*ct
->c_ctinitfnx
) (ct
);
718 * #@type/subtype (external types directive)
725 adios (NULL
, "need external information for \"#@%s/%s\"",
726 ci
->ci_type
, ci
->ci_subtype
);
729 snprintf (buffer
, sizeof(buffer
), "message/external-body; %s", ci
->ci_magic
);
734 * Since we are using the current Content structure to
735 * hold information about the type of the external
736 * reference, we need to create another Content structure
737 * for the message/external-body to wrap it in.
739 if ((ct
= (CT
) calloc (1, sizeof(*ct
))) == NULL
)
740 adios (NULL
, "out of memory");
743 if (get_ctinfo (buffer
, ct
, 0) == NOTOK
)
745 ct
->c_type
= CT_MESSAGE
;
746 ct
->c_subtype
= MESSAGE_EXTERNAL
;
748 if ((e
= (struct exbody
*) calloc (1, sizeof(*e
))) == NULL
)
749 adios (NULL
, "out of memory");
750 ct
->c_ctparams
= (void *) e
;
756 if (params_external (ct
, 1) == NOTOK
)
762 /* Handle [file] argument */
764 /* check if specifies command to execute */
765 if (*ci
->ci_magic
== '|' || *ci
->ci_magic
== '!') {
766 for (cp
= ci
->ci_magic
+ 1; isspace ((unsigned char) *cp
); cp
++)
769 adios (NULL
, "empty pipe command for #%s directive", ci
->ci_type
);
774 /* record filename of decoded contents */
775 ce
->ce_file
= ci
->ci_magic
;
776 if (access (ce
->ce_file
, R_OK
) == NOTOK
)
777 adios ("reading", "unable to access %s for", ce
->ce_file
);
778 if (listsw
&& stat (ce
->ce_file
, &st
) != NOTOK
)
779 ct
->c_end
= (long) st
.st_size
;
786 * No [file] argument, so check profile for
787 * method to compose content.
789 snprintf (buffer
, sizeof(buffer
), "%s-compose-%s/%s",
790 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
791 if ((cp
= context_find (buffer
)) == NULL
|| *cp
== '\0') {
792 snprintf (buffer
, sizeof(buffer
), "%s-compose-%s", invo_name
, ci
->ci_type
);
793 if ((cp
= context_find (buffer
)) == NULL
|| *cp
== '\0') {
794 content_error (NULL
, ct
, "don't know how to compose content");
798 ci
->ci_magic
= add (cp
, NULL
);
803 adios (NULL
, "external definition not allowed for \"#%s\"", ci
->ci_type
);
807 * #forw [+folder] [msgs]
809 if (!strcasecmp (ci
->ci_type
, "forw")) {
811 char *folder
, *arguments
[MAXARGS
];
815 ap
= brkstring (ci
->ci_magic
, " ", "\n");
816 copyip (ap
, arguments
, MAXARGS
);
818 arguments
[0] = "cur";
823 /* search the arguments for a folder name */
824 for (ap
= arguments
; *ap
; ap
++) {
826 if (*cp
== '+' || *cp
== '@') {
828 adios (NULL
, "only one folder per #forw directive");
830 folder
= pluspath (cp
);
834 /* else, use the current folder */
836 folder
= add (getfolder (1), NULL
);
838 if (!(mp
= folder_read (folder
, 0)))
839 adios (NULL
, "unable to read folder %s", folder
);
840 for (ap
= arguments
; *ap
; ap
++) {
842 if (*cp
!= '+' && *cp
!= '@')
843 if (!m_convert (mp
, cp
))
850 * If there is more than one message to include, make this
851 * a content of type "multipart/digest" and insert each message
852 * as a subpart. If there is only one message, then make this
853 * a content of type "message/rfc822".
855 if (mp
->numsel
> 1) {
856 /* we are forwarding multiple messages */
857 if (get_ctinfo ("multipart/digest", ct
, 0) == NOTOK
)
859 ct
->c_type
= CT_MULTIPART
;
860 ct
->c_subtype
= MULTI_DIGEST
;
862 if ((m
= (struct multipart
*) calloc (1, sizeof(*m
))) == NULL
)
863 adios (NULL
, "out of memory");
864 ct
->c_ctparams
= (void *) m
;
867 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
868 if (is_selected(mp
, msgnum
)) {
873 if ((p
= (CT
) calloc (1, sizeof(*p
))) == NULL
)
874 adios (NULL
, "out of memory");
875 init_decoded_content (p
);
877 if (get_ctinfo ("message/rfc822", p
, 0) == NOTOK
)
879 p
->c_type
= CT_MESSAGE
;
880 p
->c_subtype
= MESSAGE_RFC822
;
882 snprintf (buffer
, sizeof(buffer
), "%s/%d", mp
->foldpath
, msgnum
);
883 pe
->ce_file
= add (buffer
, NULL
);
884 if (listsw
&& stat (pe
->ce_file
, &st
) != NOTOK
)
885 p
->c_end
= (long) st
.st_size
;
887 if ((part
= (struct part
*) calloc (1, sizeof(*part
))) == NULL
)
888 adios (NULL
, "out of memory");
895 /* we are forwarding one message */
896 if (get_ctinfo ("message/rfc822", ct
, 0) == NOTOK
)
898 ct
->c_type
= CT_MESSAGE
;
899 ct
->c_subtype
= MESSAGE_RFC822
;
902 snprintf (buffer
, sizeof(buffer
), "%s/%d", mp
->foldpath
, msgnum
);
903 ce
->ce_file
= add (buffer
, NULL
);
904 if (listsw
&& stat (ce
->ce_file
, &st
) != NOTOK
)
905 ct
->c_end
= (long) st
.st_size
;
908 folder_free (mp
); /* free folder/message structure */
915 if (!strcasecmp (ci
->ci_type
, "end")) {
922 * #begin [ alternative | parallel ]
924 if (!strcasecmp (ci
->ci_type
, "begin")) {
927 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
928 } else if (!strcasecmp (ci
->ci_magic
, "alternative")) {
929 vrsn
= MULTI_ALTERNATE
;
930 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
931 } else if (!strcasecmp (ci
->ci_magic
, "parallel")) {
932 vrsn
= MULTI_PARALLEL
;
933 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
934 } else if (uprf (ci
->ci_magic
, "digest")) {
937 vrsn
= MULTI_UNKNOWN
;
942 snprintf (buffer
, sizeof(buffer
), "multipart/%s", cp
);
943 if (get_ctinfo (buffer
, ct
, 0) == NOTOK
)
945 ct
->c_type
= CT_MULTIPART
;
946 ct
->c_subtype
= vrsn
;
948 if ((m
= (struct multipart
*) calloc (1, sizeof(*m
))) == NULL
)
949 adios (NULL
, "out of memory");
950 ct
->c_ctparams
= (void *) m
;
953 while (fgetstr (buffer
, sizeof(buffer
) - 1, in
)) {
957 if (user_content (in
, file
, buffer
, &p
) == DONE
) {
959 adios (NULL
, "empty \"#begin ... #end\" sequence");
965 if ((part
= (struct part
*) calloc (1, sizeof(*part
))) == NULL
)
966 adios (NULL
, "out of memory");
971 admonish (NULL
, "premature end-of-file, missing #end");
978 adios (NULL
, "unknown directive \"#%s\"", ci
->ci_type
);
979 return NOTOK
; /* NOT REACHED */
984 set_id (CT ct
, int top
)
986 char contentid
[BUFSIZ
];
988 static time_t clock
= 0;
993 snprintf (contentid
, sizeof(contentid
), "%s\n", message_id (clock
, 1));
995 msgfmt
= getcpy(contentid
);
997 snprintf (contentid
, sizeof(contentid
), msgfmt
, top
? 0 : ++partno
);
998 ct
->c_id
= getcpy (contentid
);
1003 * Fill out, or expand the various contents in the composition
1004 * draft. Read-in any necessary files. Parse and execute any
1005 * commands specified by profile composition strings.
1009 compose_content (CT ct
)
1011 CE ce
= &ct
->c_cefile
;
1013 switch (ct
->c_type
) {
1018 char partnam
[BUFSIZ
];
1019 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1023 snprintf (partnam
, sizeof(partnam
), "%s.", ct
->c_partno
);
1024 pp
= partnam
+ strlen (partnam
);
1029 /* first, we call compose_content on all the subparts */
1030 for (part
= m
->mp_parts
, partnum
= 1; part
; part
= part
->mp_next
, partnum
++) {
1031 CT p
= part
->mp_part
;
1033 sprintf (pp
, "%d", partnum
);
1034 p
->c_partno
= add (partnam
, NULL
);
1035 if (compose_content (p
) == NOTOK
)
1040 * If the -rfc934mode switch is given, then check all
1041 * the subparts of a multipart/digest. If they are all
1042 * message/rfc822, then mark this content and all
1043 * subparts with the rfc934 compatibility mode flag.
1045 if (rfc934sw
&& ct
->c_subtype
== MULTI_DIGEST
) {
1048 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1049 CT p
= part
->mp_part
;
1051 if (p
->c_subtype
!= MESSAGE_RFC822
) {
1056 ct
->c_rfc934
= is934
;
1057 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1058 CT p
= part
->mp_part
;
1060 if ((p
->c_rfc934
= is934
))
1066 ct
->c_end
= (partnum
= strlen (prefix
) + 2) + 2;
1070 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
)
1071 ct
->c_end
+= part
->mp_part
->c_end
+ partnum
;
1077 /* Nothing to do for type message */
1081 * Discrete types (text/application/audio/image/video)
1086 int i
, xstdout
, len
, buflen
;
1087 char *bp
, **ap
, *cp
;
1088 char *vec
[4], buffer
[BUFSIZ
];
1090 CI ci
= &ct
->c_ctinfo
;
1093 if (!(cp
= ci
->ci_magic
))
1094 adios (NULL
, "internal error(5)");
1096 tfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
);
1097 if (tfile
== NULL
) {
1098 adios("mhbuildsbr", "unable to create temporary file");
1100 ce
->ce_file
= add (tfile
, NULL
);
1105 /* Get buffer ready to go */
1108 buflen
= sizeof(buffer
);
1111 * Parse composition string into buffer
1113 for ( ; *cp
; cp
++) {
1118 /* insert parameters from directive */
1122 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
1123 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, *ap
, *ep
);
1133 /* %f, and stdout is not-redirected */
1139 * insert temporary filename where
1140 * content should be written
1142 snprintf (bp
, buflen
, "%s", ce
->ce_file
);
1146 /* insert content subtype */
1147 strncpy (bp
, ci
->ci_subtype
, buflen
);
1151 /* insert character % */
1172 printf ("composing content %s/%s from command\n\t%s\n",
1173 ci
->ci_type
, ci
->ci_subtype
, buffer
);
1175 fflush (stdout
); /* not sure if need for -noverbose */
1182 if ((out
= fopen (ce
->ce_file
, "w")) == NULL
)
1183 adios (ce
->ce_file
, "unable to open for writing");
1185 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
> 5; i
++)
1189 adios ("fork", "unable to fork");
1194 dup2 (fileno (out
), 1);
1195 close (fileno (out
));
1196 execvp ("/bin/sh", vec
);
1197 fprintf (stderr
, "unable to exec ");
1204 if (pidXwait(child_id
, NULL
))
1210 /* Check size of file */
1211 if (listsw
&& ct
->c_end
== 0L) {
1214 if (stat (ce
->ce_file
, &st
) != NOTOK
)
1215 ct
->c_end
= (long) st
.st_size
;
1227 * 1) choose a transfer encoding.
1228 * 2) check for clashes with multipart boundary string.
1229 * 3) for text content, figure out which character set is being used.
1231 * If there is a clash with one of the contents and the multipart boundary,
1232 * this function will exit with NOTOK. This will cause the scanning process
1233 * to be repeated with a different multipart boundary. It is possible
1234 * (although highly unlikely) that this scan will be repeated multiple times.
1238 scan_content (CT ct
)
1241 int check8bit
= 0, contains8bit
= 0; /* check if contains 8bit data */
1242 int checklinelen
= 0, linelen
= 0; /* check for long lines */
1243 int checkboundary
= 0, boundaryclash
= 0; /* check if clashes with multipart boundary */
1244 int checklinespace
= 0, linespace
= 0; /* check if any line ends with space */
1245 char *cp
= NULL
, buffer
[BUFSIZ
];
1246 struct text
*t
= NULL
;
1248 CE ce
= &ct
->c_cefile
;
1251 * handle multipart by scanning all subparts
1252 * and then checking their encoding.
1254 if (ct
->c_type
== CT_MULTIPART
) {
1255 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1258 /* initially mark the domain of enclosing multipart as 7bit */
1259 ct
->c_encoding
= CE_7BIT
;
1261 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1262 CT p
= part
->mp_part
;
1264 if (scan_content (p
) == NOTOK
) /* choose encoding for subpart */
1267 /* if necessary, enlarge encoding for enclosing multipart */
1268 if (p
->c_encoding
== CE_BINARY
)
1269 ct
->c_encoding
= CE_BINARY
;
1270 if (p
->c_encoding
== CE_8BIT
&& ct
->c_encoding
!= CE_BINARY
)
1271 ct
->c_encoding
= CE_8BIT
;
1278 * Decide what to check while scanning this content.
1280 switch (ct
->c_type
) {
1284 if (ct
->c_subtype
== TEXT_PLAIN
) {
1293 case CT_APPLICATION
:
1305 /* don't check anything for message/external */
1306 if (ct
->c_subtype
== MESSAGE_EXTERNAL
)
1316 * Don't check anything for these types,
1317 * since we are forcing use of base64.
1327 * Scan the unencoded content
1329 if (check8bit
|| checklinelen
|| checklinespace
|| checkboundary
) {
1330 if ((in
= fopen (ce
->ce_file
, "r")) == NULL
)
1331 adios (ce
->ce_file
, "unable to open for reading");
1332 len
= strlen (prefix
);
1334 while (fgets (buffer
, sizeof(buffer
) - 1, in
)) {
1336 * Check for 8bit data.
1339 for (cp
= buffer
; *cp
; cp
++) {
1340 if (!isascii ((unsigned char) *cp
)) {
1342 check8bit
= 0; /* no need to keep checking */
1348 * Check line length.
1350 if (checklinelen
&& (strlen (buffer
) > CPERLIN
+ 1)) {
1352 checklinelen
= 0; /* no need to keep checking */
1356 * Check if line ends with a space.
1358 if (checklinespace
&& (cp
= buffer
+ strlen (buffer
) - 2) > buffer
&& isspace ((unsigned char) *cp
)) {
1360 checklinespace
= 0; /* no need to keep checking */
1364 * Check if content contains a line that clashes
1365 * with our standard boundary for multipart messages.
1367 if (checkboundary
&& buffer
[0] == '-' && buffer
[1] == '-') {
1368 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1369 if (!isspace ((unsigned char) *cp
))
1372 if (!strncmp(buffer
+ 2, prefix
, len
) && isdigit((unsigned char) buffer
[2 + len
])) {
1374 checkboundary
= 0; /* no need to keep checking */
1382 * Decide which transfer encoding to use.
1384 switch (ct
->c_type
) {
1387 * If the text content didn't specify a character
1388 * set, we need to figure out which one was used.
1390 t
= (struct text
*) ct
->c_ctparams
;
1391 if (t
->tx_charset
== CHARSET_UNSPECIFIED
) {
1392 CI ci
= &ct
->c_ctinfo
;
1395 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++)
1399 *ap
= concat ("charset=", write_charset_8bit(), NULL
);
1401 *ap
= add ("charset=us-ascii", NULL
);
1403 t
->tx_charset
= CHARSET_SPECIFIED
;
1405 cp
= strchr(*ap
++, '=');
1411 if (contains8bit
|| linelen
|| linespace
|| checksw
)
1412 ct
->c_encoding
= CE_QUOTED
;
1414 ct
->c_encoding
= CE_7BIT
;
1417 case CT_APPLICATION
:
1418 /* For application type, use base64, except when postscript */
1419 if (contains8bit
|| linelen
|| linespace
|| checksw
)
1420 ct
->c_encoding
= (ct
->c_subtype
== APPLICATION_POSTSCRIPT
)
1421 ? CE_QUOTED
: CE_BASE64
;
1423 ct
->c_encoding
= CE_7BIT
;
1427 ct
->c_encoding
= CE_7BIT
;
1433 /* For audio, image, and video contents, just use base64 */
1434 ct
->c_encoding
= CE_BASE64
;
1438 return (boundaryclash
? NOTOK
: OK
);
1443 * Scan the content structures, and build header
1444 * fields that will need to be output into the
1449 build_headers (CT ct
)
1451 int cc
, mailbody
, extbody
, len
;
1453 char *np
, *vp
, buffer
[BUFSIZ
];
1454 CI ci
= &ct
->c_ctinfo
;
1457 * If message is type multipart, then add the multipart
1458 * boundary to the list of attribute/value pairs.
1460 if (ct
->c_type
== CT_MULTIPART
) {
1462 static int level
= 0; /* store nesting level */
1466 snprintf (buffer
, sizeof(buffer
), "boundary=%s%d", prefix
, level
++);
1467 cp
= strchr(*ap
++ = add (buffer
, NULL
), '=');
1474 * Skip the output of Content-Type, parameters, content
1475 * description and disposition, and Content-ID if the
1476 * content is of type "message" and the rfc934 compatibility
1477 * flag is set (which means we are inside multipart/digest
1478 * and the switch -rfc934mode was given).
1480 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_rfc934
)
1484 * output the content type and subtype
1486 np
= add (TYPE_FIELD
, NULL
);
1487 vp
= concat (" ", ci
->ci_type
, "/", ci
->ci_subtype
, NULL
);
1489 /* keep track of length of line */
1490 len
= strlen (TYPE_FIELD
) + strlen (ci
->ci_type
)
1491 + strlen (ci
->ci_subtype
) + 3;
1493 extbody
= ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_EXTERNAL
;
1494 mailbody
= extbody
&& ((struct exbody
*) ct
->c_ctparams
)->eb_body
;
1497 * Append the attribute/value pairs to
1498 * the end of the Content-Type line.
1500 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
1501 if (mailbody
&& !strcasecmp (*ap
, "body"))
1508 * According to RFC 2017, if we have a URL longer than 40 characters
1509 * we have to break it across multiple lines
1512 if (extbody
&& strcasecmp (*ap
, "url") == 0) {
1515 /* 7 here refers to " url=\"\"" */
1516 if (len
+ 1 + (cc
= (min(MAXURLTOKEN
, strlen(value
)) + 7)) >=
1518 vp
= add ("\n\t", vp
);
1525 vp
= add ("url=\"", vp
);
1528 while (strlen(value
) > MAXURLTOKEN
) {
1529 strncpy(buffer
, value
, MAXURLTOKEN
);
1530 buffer
[MAXURLTOKEN
] = '\0';
1531 vp
= add (buffer
, vp
);
1532 vp
= add ("\n\t", vp
);
1533 value
+= MAXURLTOKEN
;
1537 vp
= add (value
, vp
);
1538 vp
= add ("\"", vp
);
1539 len
+= strlen(value
) + 1;
1543 snprintf (buffer
, sizeof(buffer
), "%s=\"%s\"", *ap
, *ep
);
1544 if (len
+ 1 + (cc
= strlen (buffer
)) >= CPERLIN
) {
1545 vp
= add ("\n\t", vp
);
1551 vp
= add (buffer
, vp
);
1556 * Append any RFC-822 comment to the end of
1557 * the Content-Type line.
1559 if (ci
->ci_comment
) {
1560 snprintf (buffer
, sizeof(buffer
), "(%s)", ci
->ci_comment
);
1561 if (len
+ 1 + (cc
= 2 + strlen (ci
->ci_comment
)) >= CPERLIN
) {
1562 vp
= add ("\n\t", vp
);
1568 vp
= add (buffer
, vp
);
1571 vp
= add ("\n", vp
);
1572 add_header (ct
, np
, vp
);
1575 * output the Content-ID, unless disabled by -nocontentid
1577 if (contentidsw
&& ct
->c_id
) {
1578 np
= add (ID_FIELD
, NULL
);
1579 vp
= concat (" ", ct
->c_id
, NULL
);
1580 add_header (ct
, np
, vp
);
1584 * output the Content-Description
1587 np
= add (DESCR_FIELD
, NULL
);
1588 vp
= concat (" ", ct
->c_descr
, NULL
);
1589 add_header (ct
, np
, vp
);
1593 * output the Content-Disposition
1596 np
= add (DISPO_FIELD
, NULL
);
1597 vp
= concat (" ", ct
->c_dispo
, NULL
);
1598 add_header (ct
, np
, vp
);
1603 * If this is the internal content structure for a
1604 * "message/external", then we are done with the
1605 * headers (since it has no body).
1611 * output the Content-MD5
1614 np
= add (MD5_FIELD
, NULL
);
1615 vp
= calculate_digest (ct
, (ct
->c_encoding
== CE_QUOTED
) ? 1 : 0);
1616 add_header (ct
, np
, vp
);
1620 * output the Content-Transfer-Encoding
1622 switch (ct
->c_encoding
) {
1624 /* Nothing to output */
1628 if (ct
->c_type
== CT_MESSAGE
)
1629 adios (NULL
, "internal error, invalid encoding");
1631 np
= add (ENCODING_FIELD
, NULL
);
1632 vp
= concat (" ", "8bit", "\n", NULL
);
1633 add_header (ct
, np
, vp
);
1637 if (ct
->c_type
== CT_MESSAGE
|| ct
->c_type
== CT_MULTIPART
)
1638 adios (NULL
, "internal error, invalid encoding");
1640 np
= add (ENCODING_FIELD
, NULL
);
1641 vp
= concat (" ", "quoted-printable", "\n", NULL
);
1642 add_header (ct
, np
, vp
);
1646 if (ct
->c_type
== CT_MESSAGE
|| ct
->c_type
== CT_MULTIPART
)
1647 adios (NULL
, "internal error, invalid encoding");
1649 np
= add (ENCODING_FIELD
, NULL
);
1650 vp
= concat (" ", "base64", "\n", NULL
);
1651 add_header (ct
, np
, vp
);
1655 if (ct
->c_type
== CT_MESSAGE
)
1656 adios (NULL
, "internal error, invalid encoding");
1658 np
= add (ENCODING_FIELD
, NULL
);
1659 vp
= concat (" ", "binary", "\n", NULL
);
1660 add_header (ct
, np
, vp
);
1664 adios (NULL
, "unknown transfer encoding in content");
1669 * Additional content specific header processing
1671 switch (ct
->c_type
) {
1674 struct multipart
*m
;
1677 m
= (struct multipart
*) ct
->c_ctparams
;
1678 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1688 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1691 e
= (struct exbody
*) ct
->c_ctparams
;
1692 build_headers (e
->eb_content
);
1705 static char nib2b64
[0x40+1] =
1706 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1709 calculate_digest (CT ct
, int asciiP
)
1712 char buffer
[BUFSIZ
], *vp
, *op
;
1714 unsigned char digest
[16];
1715 unsigned char outbuf
[25];
1717 CE ce
= &ct
->c_cefile
;
1718 char *infilename
= ce
->ce_file
? ce
->ce_file
: ct
->c_file
;
1722 if ((in
= fopen (infilename
, "r")) == NULL
)
1723 adios (infilename
, "unable to open for reading");
1725 /* Initialize md5 context */
1726 MD5Init (&mdContext
);
1728 /* calculate md5 message digest */
1730 while (fgets (buffer
, sizeof(buffer
) - 1, in
)) {
1733 cp
= buffer
+ strlen (buffer
) - 1;
1734 if ((c
= *cp
) == '\n')
1737 MD5Update (&mdContext
, (unsigned char *) buffer
,
1738 (unsigned int) strlen (buffer
));
1741 MD5Update (&mdContext
, (unsigned char *) "\r\n", 2);
1744 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), in
)) > 0)
1745 MD5Update (&mdContext
, (unsigned char *) buffer
, (unsigned int) cc
);
1748 /* md5 finalization. Write digest and zero md5 context */
1749 MD5Final (digest
, &mdContext
);
1754 /* print debugging info */
1758 fprintf (stderr
, "MD5 digest=");
1759 for (ep
= (dp
= digest
) + sizeof(digest
) / sizeof(digest
[0]);
1761 fprintf (stderr
, "%02x", *dp
& 0xff);
1762 fprintf (stderr
, "\n");
1765 /* encode the digest using base64 */
1766 for (dp
= digest
, op
= (char *) outbuf
,
1767 cc
= sizeof(digest
) / sizeof(digest
[0]);
1768 cc
> 0; cc
-= 3, op
+= 4) {
1772 bits
= (*dp
++ & 0xff) << 16;
1774 bits
|= (*dp
++ & 0xff) << 8;
1776 bits
|= *dp
++ & 0xff;
1779 for (bp
= op
+ 4; bp
> op
; bits
>>= 6)
1780 *--bp
= nib2b64
[bits
& 0x3f];
1788 /* null terminate string */
1791 /* now make copy and return string */
1792 vp
= concat (" ", outbuf
, "\n", NULL
);
1797 * Set things up for the content structure for file "filename" that
1802 setup_attach_content(CT ct
, char *filename
)
1804 char *type
, **ap
, **ep
, *simplename
= r1bindex(filename
, '/');
1805 struct str2init
*s2i
;
1807 if (! (type
= mime_type(filename
))) {
1808 adios(NULL
, "Unable to determine MIME type of \"%s\"", filename
);
1812 * Parse the Content-Type. get_ctinfo() parses MIME parameters, but
1813 * since we're just feeding it a MIME type we have to add those ourself.
1814 * Map that to a valid content-type label and call any initialization
1818 if (get_ctinfo(type
, ct
, 0) == NOTOK
)
1823 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
1824 if (strcasecmp(ct
->c_ctinfo
.ci_type
, s2i
->si_key
) == 0)
1826 if (!s2i
->si_key
&& !uprf(ct
->c_ctinfo
.ci_type
, "X-"))
1830 * Make sure the type isn't incompatible with what we can handle
1833 switch (ct
->c_type
= s2i
->si_val
) {
1835 adios (NULL
, "multipart types must be specified by mhbuild directives");
1839 if (strcasecmp(ct
->c_ctinfo
.ci_subtype
, "partial") == 0)
1840 adios(NULL
, "Sorry, %s/%s isn't supported", ct
->c_ctinfo
.ci_type
,
1841 ct
->c_ctinfo
.ci_subtype
);
1842 if (strcasecmp(ct
->c_ctinfo
.ci_subtype
, "external-body") == 0)
1843 adios(NULL
, "external-body messages must be specified "
1844 "by mhbuild directives");
1849 * This sets the subtype, if it's significant
1851 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
1852 (*ct
->c_ctinitfnx
)(ct
);
1857 * Feed in a few attributes; specifically, the name attribute, the
1858 * content-description, and the content-disposition.
1861 for (ap
= ct
->c_ctinfo
.ci_attrs
, ep
= ct
->c_ctinfo
.ci_values
; *ap
;
1863 if (strcasecmp(*ap
, "name") == 0) {
1866 *ep
= getcpy(simplename
);
1872 *ap
= getcpy("name");
1873 *ep
= getcpy(simplename
);
1876 ct
->c_descr
= getcpy(simplename
);
1877 ct
->c_descr
= add("\n", ct
->c_descr
);
1878 ct
->c_cefile
.ce_file
= getcpy(filename
);
1881 * If it's a text/calendar, we need to make sure it's an inline,
1882 * otherwise it won't work with some calendar programs. Otherwise
1886 if (strcasecmp(ct
->c_ctinfo
.ci_type
, "text") == 0 &&
1887 strcasecmp(ct
->c_ctinfo
.ci_subtype
, "calendar") == 0) {
1888 ct
->c_dispo
= getcpy("inline; filename=\"");
1890 ct
->c_dispo
= getcpy("attachment; filename=\"");
1893 ct
->c_dispo
= add(simplename
, ct
->c_dispo
);
1894 ct
->c_dispo
= add("\"\n", ct
->c_dispo
);