]>
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 */
47 static char prefix
[] = "----- =_aaaaaaaaaa";
51 struct attach_list
*next
;
55 * Maximum size of URL token in message/external-body
58 #define MAXURLTOKEN 40
62 void content_error (char *, CT
, char *, ...);
65 int find_cache (CT
, int, int *, char *, char *, int);
68 void free_ctinfo (CT
);
69 void free_encoding (CT
, int);
74 static int init_decoded_content (CT
);
75 static void setup_attach_content(CT
, char *);
76 static char *fgetstr (char *, int, FILE *);
77 static int user_content (FILE *, char *, CT
*);
78 static void set_id (CT
, int);
79 static int compose_content (CT
);
80 static int scan_content (CT
, size_t);
81 static int build_headers (CT
);
82 static char *calculate_digest (CT
, int);
85 static unsigned char directives_stack
[32];
86 static unsigned int directives_index
;
88 static int do_direct(void)
90 return directives_stack
[directives_index
];
93 static void directive_onoff(int onoff
)
95 if (directives_index
>= sizeof(directives_stack
) - 1) {
96 fprintf(stderr
, "mhbuild: #on/off overflow, continuing\n");
99 directives_stack
[++directives_index
] = onoff
;
102 static void directive_init(int onoff
)
104 directives_index
= 0;
105 directives_stack
[0] = onoff
;
108 static void directive_pop(void)
110 if (directives_index
> 0)
113 fprintf(stderr
, "mhbuild: #pop underflow, continuing\n");
117 * Main routine for translating composition file
118 * into valid MIME message. It translates the draft
119 * into a content structure (actually a tree of content
120 * structures). This message then can be manipulated
121 * in various ways, including being output via
126 build_mime (char *infile
, int autobuild
, int dist
, int directives
,
127 int header_encoding
, size_t maxunencoded
)
130 char buf
[BUFSIZ
], name
[NAMESZ
];
137 m_getfld_state_t gstate
= 0;
138 struct attach_list
*attach_head
= NULL
, *attach_tail
= NULL
, *at_entry
;
140 directive_init(directives
);
142 umask (~m_gmprot ());
144 /* open the composition draft */
145 if ((in
= fopen (infile
, "r")) == NULL
)
146 adios (infile
, "unable to open for reading");
149 * Allocate space for primary (outside) content
151 if ((ct
= (CT
) calloc (1, sizeof(*ct
))) == NULL
)
152 adios (NULL
, "out of memory");
155 * Allocate structure for handling decoded content
156 * for this part. We don't really need this, but
157 * allocate it to remain consistent.
159 init_decoded_content (ct
);
162 * Parse some of the header fields in the composition
163 * draft into the linked list of header fields for
164 * the new MIME message.
166 m_getfld_track_filepos (&gstate
, in
);
167 for (compnum
= 1;;) {
168 int bufsz
= sizeof buf
;
169 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
)) {
174 /* abort if draft has Mime-Version or C-T-E header field */
175 if (strcasecmp (name
, VRSN_FIELD
) == 0 ||
176 strcasecmp (name
, ENCODING_FIELD
) == 0) {
181 adios (NULL
, "draft shouldn't contain %s: field", name
);
185 /* ignore any Content-Type fields in the header */
186 if (!strcasecmp (name
, TYPE_FIELD
)) {
187 while (state
== FLDPLUS
) {
189 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
194 /* get copies of the buffers */
195 np
= add (name
, NULL
);
196 vp
= add (buf
, NULL
);
198 /* if necessary, get rest of field */
199 while (state
== FLDPLUS
) {
201 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
202 vp
= add (buf
, vp
); /* add to previous value */
206 * Now add the header data to the list, unless it's an attach
207 * header; in that case, add it to our attach list
210 if (strcasecmp(ATTACH_FIELD
, np
) == 0) {
211 struct attach_list
*entry
;
212 char *s
= vp
, *e
= vp
+ strlen(vp
) - 1;
216 * Make sure we can find the start of this filename.
217 * If it's blank, we skip completely. Otherwise, strip
218 * off any leading spaces and trailing newlines.
221 while (isspace((unsigned char) *s
))
224 while (e
> s
&& *e
== '\n')
232 entry
= mh_xmalloc(sizeof(*entry
));
233 entry
->filename
= getcpy(s
);
238 attach_tail
->next
= entry
;
241 attach_head
= attach_tail
= entry
;
244 add_header (ct
, np
, vp
);
248 /* if this wasn't the last header field, then continue */
252 fseek (in
, (long) (-strlen (buf
)), SEEK_CUR
);
259 adios (NULL
, "message format error in component #%d", compnum
);
262 adios (NULL
, "getfld() returned %d", state
);
266 m_getfld_state_destroy (&gstate
);
269 * Iterate through the list of headers and call the function to MIME-ify
273 for (hp
= ct
->c_first_hf
; hp
!= NULL
; hp
= hp
->next
) {
274 if (encode_rfc2047(hp
->name
, &hp
->value
, header_encoding
, NULL
)) {
275 adios(NULL
, "Unable to encode header \"%s\"", hp
->name
);
280 * Now add the MIME-Version header field
281 * to the list of header fields.
285 np
= add (VRSN_FIELD
, NULL
);
286 vp
= concat (" ", VRSN_VALUE
, "\n", NULL
);
287 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
, 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 * Add any Attach headers to the list of MIME parts at the end of the
333 for (at_entry
= attach_head
; at_entry
; ) {
334 struct attach_list
*at_prev
= at_entry
;
338 if (access(at_entry
->filename
, R_OK
) != 0) {
339 adios("reading", "Unable to open %s for", at_entry
->filename
);
342 if ((p
= (CT
) calloc (1, sizeof(*p
))) == NULL
)
343 adios(NULL
, "out of memory");
345 init_decoded_content(p
);
348 * Initialize our content structure based on the filename,
349 * and fill in all of the relevant fields. Also place MIME
350 * parameters in the attributes array.
353 setup_attach_content(p
, at_entry
->filename
);
355 if ((part
= (struct part
*) calloc (1, sizeof(*part
))) == NULL
)
356 adios (NULL
, "out of memory");
361 at_entry
= at_entry
->next
;
362 free(at_prev
->filename
);
367 * To allow for empty message bodies, if we've found NO content at all
368 * yet cook up an empty text/plain part.
376 if ((p
= (CT
) calloc (1, sizeof(*p
))) == NULL
)
377 adios(NULL
, "out of memory");
379 init_decoded_content(p
);
381 if (get_ctinfo ("text/plain", p
, 0) == NOTOK
)
385 p
->c_subtype
= TEXT_PLAIN
;
386 p
->c_encoding
= CE_7BIT
;
387 p
->c_file
= getcpy(infile
);
389 * Sigh. ce_file contains the "decoded" contents of this part.
390 * So this seems like the best option available since we're going
391 * to call scan_content() on this.
393 p
->c_cefile
.ce_file
= getcpy("/dev/null");
394 p
->c_begin
= ftell(in
);
395 p
->c_end
= ftell(in
);
397 if ((t
= (struct text
*) calloc (1, sizeof (*t
))) == NULL
)
398 adios (NULL
, "out of memory");
400 t
->tx_charset
= CHARSET_SPECIFIED
;
403 if ((part
= (struct part
*) calloc (1, sizeof(*part
))) == NULL
)
404 adios (NULL
, "out of memory");
411 * close the composition draft since
412 * it's not needed any longer.
417 * If only one content was found, then remove and
418 * free the outer multipart content.
420 if (!m
->mp_parts
->mp_next
) {
423 p
= m
->mp_parts
->mp_part
;
424 m
->mp_parts
->mp_part
= NULL
;
426 /* move header fields */
427 p
->c_first_hf
= ct
->c_first_hf
;
428 p
->c_last_hf
= ct
->c_last_hf
;
429 ct
->c_first_hf
= NULL
;
430 ct
->c_last_hf
= NULL
;
439 * Fill out, or expand directives. Parse and execute
440 * commands specified by profile composition strings.
442 compose_content (ct
);
444 if ((cp
= strchr(prefix
, 'a')) == NULL
)
445 adios (NULL
, "internal error(4)");
448 * Scan the contents. Choose a transfer encoding, and
449 * check if prefix for multipart boundary clashes with
450 * any of the contents.
452 while (scan_content (ct
, maxunencoded
) == NOTOK
) {
457 adios (NULL
, "giving up trying to find a unique delimiter string");
463 /* Build the rest of the header field structures */
472 * Set up structures for placing unencoded
473 * content when building parts.
477 init_decoded_content (CT ct
)
479 ct
->c_ceopenfnx
= open7Bit
; /* since unencoded */
480 ct
->c_ceclosefnx
= close_encoding
;
481 ct
->c_cesizefnx
= NULL
; /* since unencoded */
488 fgetstr (char *s
, int n
, FILE *stream
)
494 for (ep
= (cp
= s
) + o_n
; cp
< ep
; ) {
497 if (!fgets (cp
, n
, stream
))
498 return (cp
!= s
? s
: NULL
);
500 if (cp
== s
&& *cp
!= '#')
503 cp
+= (i
= strlen (cp
)) - 1;
504 if (i
<= 1 || *cp
-- != '\n' || *cp
!= '\\')
510 if (strcmp(s
, "#on\n") == 0) {
512 } else if (strcmp(s
, "#off\n") == 0) {
514 } else if (strcmp(s
, "#pop\n") == 0) {
526 * Parse the composition draft for text and directives.
527 * Do initial setup of Content structure.
531 user_content (FILE *in
, char *buf
, CT
*ctp
)
539 struct str2init
*s2i
;
544 if (buf
[0] == '\n' || (do_direct() && strcmp (buf
, "#\n") == 0)) {
549 /* allocate basic Content structure */
550 if ((ct
= (CT
) calloc (1, sizeof(*ct
))) == NULL
)
551 adios (NULL
, "out of memory");
554 /* allocate basic structure for handling decoded content */
555 init_decoded_content (ct
);
562 * Handle inline text. Check if line
563 * is one of the following forms:
565 * 1) doesn't begin with '#' (implicit directive)
566 * 2) begins with "##" (implicit directive)
567 * 3) begins with "#<"
569 if (!do_direct() || buf
[0] != '#' || buf
[1] == '#' || buf
[1] == '<') {
573 char content
[BUFSIZ
];
577 if ((cp
= m_mktemp2(NULL
, invo_name
, NULL
, &out
)) == NULL
) {
578 adios("mhbuildsbr", "unable to create temporary file in %s",
582 /* use a temp file to collect the plain text lines */
583 ce
->ce_file
= add (cp
, NULL
);
586 if (do_direct() && (buf
[0] == '#' && buf
[1] == '<')) {
587 strncpy (content
, buf
+ 2, sizeof(content
));
594 /* the directive is implicit */
595 strncpy (content
, "text/plain", sizeof(content
));
597 strncpy (buffer
, (!do_direct() || buf
[0] != '#') ? buf
: buf
+ 1, sizeof(buffer
));
601 if (headers
>= 0 && do_direct() && uprf (buffer
, DESCR_FIELD
)
602 && buffer
[i
= strlen (DESCR_FIELD
)] == ':') {
606 ct
->c_descr
= add (buffer
+ i
+ 1, ct
->c_descr
);
607 if (!fgetstr (buffer
, sizeof(buffer
) - 1, in
))
608 adios (NULL
, "end-of-file after %s: field in plaintext", DESCR_FIELD
);
616 adios (NULL
, "#-directive after %s: field in plaintext", DESCR_FIELD
);
624 if (headers
>= 0 && do_direct() && uprf (buffer
, DISPO_FIELD
)
625 && buffer
[i
= strlen (DISPO_FIELD
)] == ':') {
629 ct
->c_dispo
= add (buffer
+ i
+ 1, ct
->c_dispo
);
630 if (!fgetstr (buffer
, sizeof(buffer
) - 1, in
))
631 adios (NULL
, "end-of-file after %s: field in plaintext", DISPO_FIELD
);
639 adios (NULL
, "#-directive after %s: field in plaintext", DISPO_FIELD
);
647 if (headers
!= 1 || buffer
[0] != '\n')
653 if ((cp
= fgetstr (buffer
, sizeof(buffer
) - 1, in
)) == NULL
)
655 if (do_direct() && buffer
[0] == '#') {
658 if (buffer
[1] != '#')
660 for (cp
= (bp
= buffer
) + 1; *cp
; cp
++)
667 ct
->c_end
= ftell (out
);
670 /* parse content type */
671 if (get_ctinfo (content
, ct
, inlineD
) == NOTOK
)
674 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
675 if (!strcasecmp (ci
->ci_type
, s2i
->si_key
))
677 if (!s2i
->si_key
&& !uprf (ci
->ci_type
, "X-"))
681 * check type specified (possibly implicitly)
683 switch (ct
->c_type
= s2i
->si_val
) {
685 if (!strcasecmp (ci
->ci_subtype
, "rfc822")) {
686 ct
->c_encoding
= CE_7BIT
;
691 adios (NULL
, "it doesn't make sense to define an in-line %s content",
692 ct
->c_type
== CT_MESSAGE
? "message" : "multipart");
697 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
698 (*ct
->c_ctinitfnx
) (ct
);
703 fseek (in
, pos
, SEEK_SET
);
708 * If we've reached this point, the next line
709 * must be some type of explicit directive.
712 /* check if directive is external-type */
713 extrnal
= (buf
[1] == '@');
715 /* parse directive */
716 if (get_ctinfo (buf
+ (extrnal
? 2 : 1), ct
, 1) == NOTOK
)
719 /* check directive against the list of MIME types */
720 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
721 if (!strcasecmp (ci
->ci_type
, s2i
->si_key
))
725 * Check if the directive specified a valid type.
726 * This will happen if it was one of the following forms:
733 adios (NULL
, "missing subtype in \"#%s\"", ci
->ci_type
);
735 switch (ct
->c_type
= s2i
->si_val
) {
737 adios (NULL
, "use \"#begin ... #end\" instead of \"#%s/%s\"",
738 ci
->ci_type
, ci
->ci_subtype
);
742 if (!strcasecmp (ci
->ci_subtype
, "partial"))
743 adios (NULL
, "sorry, \"#%s/%s\" isn't supported",
744 ci
->ci_type
, ci
->ci_subtype
);
745 if (!strcasecmp (ci
->ci_subtype
, "external-body"))
746 adios (NULL
, "use \"#@type/subtype ... [] ...\" instead of \"#%s/%s\"",
747 ci
->ci_type
, ci
->ci_subtype
);
750 "use \"#forw [+folder] [msgs]\" instead of \"#%s/%s\"",
751 ci
->ci_type
, ci
->ci_subtype
);
755 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
756 (*ct
->c_ctinitfnx
) (ct
);
761 * #@type/subtype (external types directive)
768 adios (NULL
, "need external information for \"#@%s/%s\"",
769 ci
->ci_type
, ci
->ci_subtype
);
772 snprintf (buffer
, sizeof(buffer
), "message/external-body; %s", ci
->ci_magic
);
777 * Since we are using the current Content structure to
778 * hold information about the type of the external
779 * reference, we need to create another Content structure
780 * for the message/external-body to wrap it in.
782 if ((ct
= (CT
) calloc (1, sizeof(*ct
))) == NULL
)
783 adios (NULL
, "out of memory");
786 if (get_ctinfo (buffer
, ct
, 0) == NOTOK
)
788 ct
->c_type
= CT_MESSAGE
;
789 ct
->c_subtype
= MESSAGE_EXTERNAL
;
791 if ((e
= (struct exbody
*) calloc (1, sizeof(*e
))) == NULL
)
792 adios (NULL
, "out of memory");
793 ct
->c_ctparams
= (void *) e
;
799 if (params_external (ct
, 1) == NOTOK
)
805 /* Handle [file] argument */
807 /* check if specifies command to execute */
808 if (*ci
->ci_magic
== '|' || *ci
->ci_magic
== '!') {
809 for (cp
= ci
->ci_magic
+ 1; isspace ((unsigned char) *cp
); cp
++)
812 adios (NULL
, "empty pipe command for #%s directive", ci
->ci_type
);
817 /* record filename of decoded contents */
818 ce
->ce_file
= ci
->ci_magic
;
819 if (access (ce
->ce_file
, R_OK
) == NOTOK
)
820 adios ("reading", "unable to access %s for", ce
->ce_file
);
821 if (listsw
&& stat (ce
->ce_file
, &st
) != NOTOK
)
822 ct
->c_end
= (long) st
.st_size
;
829 * No [file] argument, so check profile for
830 * method to compose content.
832 snprintf (buffer
, sizeof(buffer
), "%s-compose-%s/%s",
833 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
834 if ((cp
= context_find (buffer
)) == NULL
|| *cp
== '\0') {
835 snprintf (buffer
, sizeof(buffer
), "%s-compose-%s", invo_name
, ci
->ci_type
);
836 if ((cp
= context_find (buffer
)) == NULL
|| *cp
== '\0') {
837 content_error (NULL
, ct
, "don't know how to compose content");
841 ci
->ci_magic
= add (cp
, NULL
);
846 adios (NULL
, "external definition not allowed for \"#%s\"", ci
->ci_type
);
850 * #forw [+folder] [msgs]
852 if (!strcasecmp (ci
->ci_type
, "forw")) {
854 char *folder
, *arguments
[MAXARGS
];
858 ap
= brkstring (ci
->ci_magic
, " ", "\n");
859 copyip (ap
, arguments
, MAXARGS
);
861 arguments
[0] = "cur";
866 /* search the arguments for a folder name */
867 for (ap
= arguments
; *ap
; ap
++) {
869 if (*cp
== '+' || *cp
== '@') {
871 adios (NULL
, "only one folder per #forw directive");
873 folder
= pluspath (cp
);
877 /* else, use the current folder */
879 folder
= add (getfolder (1), NULL
);
881 if (!(mp
= folder_read (folder
, 0)))
882 adios (NULL
, "unable to read folder %s", folder
);
883 for (ap
= arguments
; *ap
; ap
++) {
885 if (*cp
!= '+' && *cp
!= '@')
886 if (!m_convert (mp
, cp
))
893 * If there is more than one message to include, make this
894 * a content of type "multipart/digest" and insert each message
895 * as a subpart. If there is only one message, then make this
896 * a content of type "message/rfc822".
898 if (mp
->numsel
> 1) {
899 /* we are forwarding multiple messages */
900 if (get_ctinfo ("multipart/digest", ct
, 0) == NOTOK
)
902 ct
->c_type
= CT_MULTIPART
;
903 ct
->c_subtype
= MULTI_DIGEST
;
905 if ((m
= (struct multipart
*) calloc (1, sizeof(*m
))) == NULL
)
906 adios (NULL
, "out of memory");
907 ct
->c_ctparams
= (void *) m
;
910 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
911 if (is_selected(mp
, msgnum
)) {
916 if ((p
= (CT
) calloc (1, sizeof(*p
))) == NULL
)
917 adios (NULL
, "out of memory");
918 init_decoded_content (p
);
920 if (get_ctinfo ("message/rfc822", p
, 0) == NOTOK
)
922 p
->c_type
= CT_MESSAGE
;
923 p
->c_subtype
= MESSAGE_RFC822
;
925 snprintf (buffer
, sizeof(buffer
), "%s/%d", mp
->foldpath
, msgnum
);
926 pe
->ce_file
= add (buffer
, NULL
);
927 if (listsw
&& stat (pe
->ce_file
, &st
) != NOTOK
)
928 p
->c_end
= (long) st
.st_size
;
930 if ((part
= (struct part
*) calloc (1, sizeof(*part
))) == NULL
)
931 adios (NULL
, "out of memory");
938 /* we are forwarding one message */
939 if (get_ctinfo ("message/rfc822", ct
, 0) == NOTOK
)
941 ct
->c_type
= CT_MESSAGE
;
942 ct
->c_subtype
= MESSAGE_RFC822
;
945 snprintf (buffer
, sizeof(buffer
), "%s/%d", mp
->foldpath
, msgnum
);
946 ce
->ce_file
= add (buffer
, NULL
);
947 if (listsw
&& stat (ce
->ce_file
, &st
) != NOTOK
)
948 ct
->c_end
= (long) st
.st_size
;
951 folder_free (mp
); /* free folder/message structure */
958 if (!strcasecmp (ci
->ci_type
, "end")) {
965 * #begin [ alternative | parallel ]
967 if (!strcasecmp (ci
->ci_type
, "begin")) {
970 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
971 } else if (!strcasecmp (ci
->ci_magic
, "alternative")) {
972 vrsn
= MULTI_ALTERNATE
;
973 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
974 } else if (!strcasecmp (ci
->ci_magic
, "parallel")) {
975 vrsn
= MULTI_PARALLEL
;
976 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
977 } else if (uprf (ci
->ci_magic
, "digest")) {
980 vrsn
= MULTI_UNKNOWN
;
985 snprintf (buffer
, sizeof(buffer
), "multipart/%s", cp
);
986 if (get_ctinfo (buffer
, ct
, 0) == NOTOK
)
988 ct
->c_type
= CT_MULTIPART
;
989 ct
->c_subtype
= vrsn
;
991 if ((m
= (struct multipart
*) calloc (1, sizeof(*m
))) == NULL
)
992 adios (NULL
, "out of memory");
993 ct
->c_ctparams
= (void *) m
;
996 while (fgetstr (buffer
, sizeof(buffer
) - 1, in
)) {
1000 if (user_content (in
, buffer
, &p
) == DONE
) {
1002 adios (NULL
, "empty \"#begin ... #end\" sequence");
1008 if ((part
= (struct part
*) calloc (1, sizeof(*part
))) == NULL
)
1009 adios (NULL
, "out of memory");
1011 pp
= &part
->mp_next
;
1014 admonish (NULL
, "premature end-of-file, missing #end");
1021 adios (NULL
, "unknown directive \"#%s\"", ci
->ci_type
);
1022 return NOTOK
; /* NOT REACHED */
1027 set_id (CT ct
, int top
)
1029 char contentid
[BUFSIZ
];
1031 static time_t clock
= 0;
1032 static char *msgfmt
;
1036 snprintf (contentid
, sizeof(contentid
), "%s\n", message_id (clock
, 1));
1038 msgfmt
= getcpy(contentid
);
1040 snprintf (contentid
, sizeof(contentid
), msgfmt
, top
? 0 : ++partno
);
1041 ct
->c_id
= getcpy (contentid
);
1046 * Fill out, or expand the various contents in the composition
1047 * draft. Read-in any necessary files. Parse and execute any
1048 * commands specified by profile composition strings.
1052 compose_content (CT ct
)
1054 CE ce
= &ct
->c_cefile
;
1056 switch (ct
->c_type
) {
1061 char partnam
[BUFSIZ
];
1062 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1066 snprintf (partnam
, sizeof(partnam
), "%s.", ct
->c_partno
);
1067 pp
= partnam
+ strlen (partnam
);
1072 /* first, we call compose_content on all the subparts */
1073 for (part
= m
->mp_parts
, partnum
= 1; part
; part
= part
->mp_next
, partnum
++) {
1074 CT p
= part
->mp_part
;
1076 sprintf (pp
, "%d", partnum
);
1077 p
->c_partno
= add (partnam
, NULL
);
1078 if (compose_content (p
) == NOTOK
)
1083 * If the -rfc934mode switch is given, then check all
1084 * the subparts of a multipart/digest. If they are all
1085 * message/rfc822, then mark this content and all
1086 * subparts with the rfc934 compatibility mode flag.
1088 if (rfc934sw
&& ct
->c_subtype
== MULTI_DIGEST
) {
1091 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1092 CT p
= part
->mp_part
;
1094 if (p
->c_subtype
!= MESSAGE_RFC822
) {
1099 ct
->c_rfc934
= is934
;
1100 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1101 CT p
= part
->mp_part
;
1103 if ((p
->c_rfc934
= is934
))
1109 ct
->c_end
= (partnum
= strlen (prefix
) + 2) + 2;
1113 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
)
1114 ct
->c_end
+= part
->mp_part
->c_end
+ partnum
;
1120 /* Nothing to do for type message */
1124 * Discrete types (text/application/audio/image/video)
1129 int i
, xstdout
, len
, buflen
;
1130 char *bp
, **ap
, *cp
;
1131 char *vec
[4], buffer
[BUFSIZ
];
1133 CI ci
= &ct
->c_ctinfo
;
1136 if (!(cp
= ci
->ci_magic
))
1137 adios (NULL
, "internal error(5)");
1139 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1140 adios("mhbuildsbr", "unable to create temporary file in %s",
1143 ce
->ce_file
= add (tfile
, NULL
);
1148 /* Get buffer ready to go */
1151 buflen
= sizeof(buffer
);
1154 * Parse composition string into buffer
1156 for ( ; *cp
; cp
++) {
1161 /* insert parameters from directive */
1165 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
1166 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, *ap
, *ep
);
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
;
1471 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++)
1475 *ap
= concat ("charset=", write_charset_8bit(), NULL
);
1477 *ap
= add ("charset=us-ascii", NULL
);
1479 t
->tx_charset
= CHARSET_SPECIFIED
;
1481 cp
= strchr(*ap
++, '=');
1489 * Decide which transfer encoding to use.
1492 if (ct
->c_reqencoding
!= CE_UNKNOWN
)
1493 ct
->c_encoding
= ct
->c_reqencoding
;
1495 switch (ct
->c_type
) {
1497 if (contains8bit
&& !linelen
&& !linespace
&& !checksw
)
1498 ct
->c_encoding
= CE_8BIT
;
1499 else if (contains8bit
|| linelen
|| linespace
|| checksw
)
1500 ct
->c_encoding
= CE_QUOTED
;
1502 ct
->c_encoding
= CE_7BIT
;
1505 case CT_APPLICATION
:
1506 /* For application type, use base64, except when postscript */
1507 if (contains8bit
|| linelen
|| linespace
|| checksw
)
1508 ct
->c_encoding
= (ct
->c_subtype
== APPLICATION_POSTSCRIPT
)
1509 ? CE_QUOTED
: CE_BASE64
;
1511 ct
->c_encoding
= CE_7BIT
;
1515 ct
->c_encoding
= CE_7BIT
;
1521 /* For audio, image, and video contents, just use base64 */
1522 ct
->c_encoding
= CE_BASE64
;
1526 return (boundaryclash
? NOTOK
: OK
);
1531 * Scan the content structures, and build header
1532 * fields that will need to be output into the
1537 build_headers (CT ct
)
1539 int cc
, mailbody
, extbody
, len
;
1541 char *np
, *vp
, buffer
[BUFSIZ
];
1542 CI ci
= &ct
->c_ctinfo
;
1545 * If message is type multipart, then add the multipart
1546 * boundary to the list of attribute/value pairs.
1548 if (ct
->c_type
== CT_MULTIPART
) {
1550 static int level
= 0; /* store nesting level */
1554 snprintf (buffer
, sizeof(buffer
), "boundary=%s%d", prefix
, level
++);
1555 cp
= strchr(*ap
++ = add (buffer
, NULL
), '=');
1562 * Skip the output of Content-Type, parameters, content
1563 * description and disposition, and Content-ID if the
1564 * content is of type "message" and the rfc934 compatibility
1565 * flag is set (which means we are inside multipart/digest
1566 * and the switch -rfc934mode was given).
1568 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_rfc934
)
1572 * output the content type and subtype
1574 np
= add (TYPE_FIELD
, NULL
);
1575 vp
= concat (" ", ci
->ci_type
, "/", ci
->ci_subtype
, NULL
);
1577 /* keep track of length of line */
1578 len
= strlen (TYPE_FIELD
) + strlen (ci
->ci_type
)
1579 + strlen (ci
->ci_subtype
) + 3;
1581 extbody
= ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_EXTERNAL
;
1582 mailbody
= extbody
&& ((struct exbody
*) ct
->c_ctparams
)->eb_body
;
1585 * Append the attribute/value pairs to
1586 * the end of the Content-Type line.
1588 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
1589 if (mailbody
&& !strcasecmp (*ap
, "body"))
1596 * According to RFC 2017, if we have a URL longer than 40 characters
1597 * we have to break it across multiple lines
1600 if (extbody
&& strcasecmp (*ap
, "url") == 0) {
1603 /* 7 here refers to " url=\"\"" */
1604 if (len
+ 1 + (cc
= (min(MAXURLTOKEN
, strlen(value
)) + 7)) >=
1606 vp
= add ("\n\t", vp
);
1613 vp
= add ("url=\"", vp
);
1616 while (strlen(value
) > MAXURLTOKEN
) {
1617 strncpy(buffer
, value
, MAXURLTOKEN
);
1618 buffer
[MAXURLTOKEN
] = '\0';
1619 vp
= add (buffer
, vp
);
1620 vp
= add ("\n\t", vp
);
1621 value
+= MAXURLTOKEN
;
1625 vp
= add (value
, vp
);
1626 vp
= add ("\"", vp
);
1627 len
+= strlen(value
) + 1;
1631 snprintf (buffer
, sizeof(buffer
), "%s=\"%s\"", *ap
, *ep
);
1632 if (len
+ 1 + (cc
= strlen (buffer
)) >= CPERLIN
) {
1633 vp
= add ("\n\t", vp
);
1639 vp
= add (buffer
, vp
);
1644 * Append any RFC-822 comment to the end of
1645 * the Content-Type line.
1647 if (ci
->ci_comment
) {
1648 snprintf (buffer
, sizeof(buffer
), "(%s)", ci
->ci_comment
);
1649 if (len
+ 1 + (cc
= 2 + strlen (ci
->ci_comment
)) >= CPERLIN
) {
1650 vp
= add ("\n\t", vp
);
1656 vp
= add (buffer
, vp
);
1659 vp
= add ("\n", vp
);
1660 add_header (ct
, np
, vp
);
1663 * output the Content-ID, unless disabled by -nocontentid
1665 if (contentidsw
&& ct
->c_id
) {
1666 np
= add (ID_FIELD
, NULL
);
1667 vp
= concat (" ", ct
->c_id
, NULL
);
1668 add_header (ct
, np
, vp
);
1672 * output the Content-Description
1675 np
= add (DESCR_FIELD
, NULL
);
1676 vp
= concat (" ", ct
->c_descr
, NULL
);
1677 add_header (ct
, np
, vp
);
1681 * output the Content-Disposition
1684 np
= add (DISPO_FIELD
, NULL
);
1685 vp
= concat (" ", ct
->c_dispo
, NULL
);
1686 add_header (ct
, np
, vp
);
1691 * If this is the internal content structure for a
1692 * "message/external", then we are done with the
1693 * headers (since it has no body).
1699 * output the Content-MD5
1702 np
= add (MD5_FIELD
, NULL
);
1703 vp
= calculate_digest (ct
, (ct
->c_encoding
== CE_QUOTED
) ? 1 : 0);
1704 add_header (ct
, np
, vp
);
1708 * output the Content-Transfer-Encoding
1710 switch (ct
->c_encoding
) {
1712 /* Nothing to output */
1716 if (ct
->c_type
== CT_MESSAGE
)
1717 adios (NULL
, "internal error, invalid encoding");
1719 np
= add (ENCODING_FIELD
, NULL
);
1720 vp
= concat (" ", "8bit", "\n", NULL
);
1721 add_header (ct
, np
, vp
);
1725 if (ct
->c_type
== CT_MESSAGE
|| ct
->c_type
== CT_MULTIPART
)
1726 adios (NULL
, "internal error, invalid encoding");
1728 np
= add (ENCODING_FIELD
, NULL
);
1729 vp
= concat (" ", "quoted-printable", "\n", NULL
);
1730 add_header (ct
, np
, vp
);
1734 if (ct
->c_type
== CT_MESSAGE
|| ct
->c_type
== CT_MULTIPART
)
1735 adios (NULL
, "internal error, invalid encoding");
1737 np
= add (ENCODING_FIELD
, NULL
);
1738 vp
= concat (" ", "base64", "\n", NULL
);
1739 add_header (ct
, np
, vp
);
1743 if (ct
->c_type
== CT_MESSAGE
)
1744 adios (NULL
, "internal error, invalid encoding");
1746 np
= add (ENCODING_FIELD
, NULL
);
1747 vp
= concat (" ", "binary", "\n", NULL
);
1748 add_header (ct
, np
, vp
);
1752 adios (NULL
, "unknown transfer encoding in content");
1757 * Additional content specific header processing
1759 switch (ct
->c_type
) {
1762 struct multipart
*m
;
1765 m
= (struct multipart
*) ct
->c_ctparams
;
1766 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1776 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1779 e
= (struct exbody
*) ct
->c_ctparams
;
1780 build_headers (e
->eb_content
);
1793 static char nib2b64
[0x40+1] =
1794 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1797 calculate_digest (CT ct
, int asciiP
)
1800 char buffer
[BUFSIZ
], *vp
, *op
;
1802 unsigned char digest
[16];
1803 unsigned char outbuf
[25];
1805 CE ce
= &ct
->c_cefile
;
1806 char *infilename
= ce
->ce_file
? ce
->ce_file
: ct
->c_file
;
1810 if ((in
= fopen (infilename
, "r")) == NULL
)
1811 adios (infilename
, "unable to open for reading");
1813 /* Initialize md5 context */
1814 MD5Init (&mdContext
);
1816 /* calculate md5 message digest */
1818 while (fgets (buffer
, sizeof(buffer
) - 1, in
)) {
1821 cp
= buffer
+ strlen (buffer
) - 1;
1822 if ((c
= *cp
) == '\n')
1825 MD5Update (&mdContext
, (unsigned char *) buffer
,
1826 (unsigned int) strlen (buffer
));
1829 MD5Update (&mdContext
, (unsigned char *) "\r\n", 2);
1832 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), in
)) > 0)
1833 MD5Update (&mdContext
, (unsigned char *) buffer
, (unsigned int) cc
);
1836 /* md5 finalization. Write digest and zero md5 context */
1837 MD5Final (digest
, &mdContext
);
1842 /* print debugging info */
1846 fprintf (stderr
, "MD5 digest=");
1847 for (ep
= (dp
= digest
) + sizeof(digest
) / sizeof(digest
[0]);
1849 fprintf (stderr
, "%02x", *dp
& 0xff);
1850 fprintf (stderr
, "\n");
1853 /* encode the digest using base64 */
1854 for (dp
= digest
, op
= (char *) outbuf
,
1855 cc
= sizeof(digest
) / sizeof(digest
[0]);
1856 cc
> 0; cc
-= 3, op
+= 4) {
1860 bits
= (*dp
++ & 0xff) << 16;
1862 bits
|= (*dp
++ & 0xff) << 8;
1864 bits
|= *dp
++ & 0xff;
1867 for (bp
= op
+ 4; bp
> op
; bits
>>= 6)
1868 *--bp
= nib2b64
[bits
& 0x3f];
1876 /* null terminate string */
1879 /* now make copy and return string */
1880 vp
= concat (" ", outbuf
, "\n", NULL
);
1885 * Set things up for the content structure for file "filename" that
1890 setup_attach_content(CT ct
, char *filename
)
1892 char *type
, **ap
, **ep
, *simplename
= r1bindex(filename
, '/');
1893 struct str2init
*s2i
;
1895 if (! (type
= mime_type(filename
))) {
1896 adios(NULL
, "Unable to determine MIME type of \"%s\"", filename
);
1900 * Parse the Content-Type. get_ctinfo() parses MIME parameters, but
1901 * since we're just feeding it a MIME type we have to add those ourself.
1902 * Map that to a valid content-type label and call any initialization
1906 if (get_ctinfo(type
, ct
, 0) == NOTOK
)
1911 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
1912 if (strcasecmp(ct
->c_ctinfo
.ci_type
, s2i
->si_key
) == 0)
1914 if (!s2i
->si_key
&& !uprf(ct
->c_ctinfo
.ci_type
, "X-"))
1918 * Make sure the type isn't incompatible with what we can handle
1921 switch (ct
->c_type
= s2i
->si_val
) {
1923 adios (NULL
, "multipart types must be specified by mhbuild directives");
1927 if (strcasecmp(ct
->c_ctinfo
.ci_subtype
, "partial") == 0)
1928 adios(NULL
, "Sorry, %s/%s isn't supported", ct
->c_ctinfo
.ci_type
,
1929 ct
->c_ctinfo
.ci_subtype
);
1930 if (strcasecmp(ct
->c_ctinfo
.ci_subtype
, "external-body") == 0)
1931 adios(NULL
, "external-body messages must be specified "
1932 "by mhbuild directives");
1937 * This sets the subtype, if it's significant
1939 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
1940 (*ct
->c_ctinitfnx
)(ct
);
1945 * Feed in a few attributes; specifically, the name attribute, the
1946 * content-description, and the content-disposition.
1949 for (ap
= ct
->c_ctinfo
.ci_attrs
, ep
= ct
->c_ctinfo
.ci_values
; *ap
;
1951 if (strcasecmp(*ap
, "name") == 0) {
1954 *ep
= getcpy(simplename
);
1960 *ap
= getcpy("name");
1961 *ep
= getcpy(simplename
);
1964 ct
->c_descr
= getcpy(simplename
);
1965 ct
->c_descr
= add("\n", ct
->c_descr
);
1966 ct
->c_cefile
.ce_file
= getcpy(filename
);
1969 * If it's a text/calendar, we need to make sure it's an inline,
1970 * otherwise it won't work with some calendar programs. Otherwise
1974 if (strcasecmp(ct
->c_ctinfo
.ci_type
, "text") == 0 &&
1975 strcasecmp(ct
->c_ctinfo
.ci_subtype
, "calendar") == 0) {
1976 ct
->c_dispo
= getcpy("inline; filename=\"");
1978 ct
->c_dispo
= getcpy("attachment; filename=\"");
1981 ct
->c_dispo
= add(simplename
, ct
->c_dispo
);
1982 ct
->c_dispo
= add("\"\n", ct
->c_dispo
);