]>
diplodocus.org Git - nmh/blob - uip/mhbuildsbr.c
e5909229ca85fbd8500ad402be66eaf7a0468323
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 *, 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 dist
, int directives
,
136 char buf
[BUFSIZ
], name
[NAMESZ
];
143 m_getfld_state_t gstate
= 0;
144 struct attach_list
*attach_head
= NULL
, *attach_tail
= NULL
, *at_entry
;
146 directive_init(directives
);
148 umask (~m_gmprot ());
150 /* open the composition draft */
151 if ((in
= fopen (infile
, "r")) == NULL
)
152 adios (infile
, "unable to open for reading");
155 * Allocate space for primary (outside) content
157 if ((ct
= (CT
) calloc (1, sizeof(*ct
))) == NULL
)
158 adios (NULL
, "out of memory");
161 * Allocate structure for handling decoded content
162 * for this part. We don't really need this, but
163 * allocate it to remain consistent.
165 init_decoded_content (ct
);
168 * Parse some of the header fields in the composition
169 * draft into the linked list of header fields for
170 * the new MIME message.
172 m_getfld_track_filepos (&gstate
, in
);
173 for (compnum
= 1;;) {
174 int bufsz
= sizeof buf
;
175 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
)) {
180 /* abort if draft has Mime-Version or C-T-E header field */
181 if (strcasecmp (name
, VRSN_FIELD
) == 0 ||
182 strcasecmp (name
, ENCODING_FIELD
) == 0) {
187 adios (NULL
, "draft shouldn't contain %s: field", name
);
191 /* ignore any Content-Type fields in the header */
192 if (!strcasecmp (name
, TYPE_FIELD
)) {
193 while (state
== FLDPLUS
) {
195 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
200 /* get copies of the buffers */
201 np
= add (name
, NULL
);
202 vp
= add (buf
, NULL
);
204 /* if necessary, get rest of field */
205 while (state
== FLDPLUS
) {
207 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
208 vp
= add (buf
, vp
); /* add to previous value */
212 * Now add the header data to the list, unless it's an attach
213 * header; in that case, add it to our attach list
216 if (strcasecmp(ATTACH_FIELD
, np
) == 0) {
217 struct attach_list
*entry
;
218 char *s
= vp
, *e
= vp
+ strlen(vp
) - 1;
222 * Make sure we can find the start of this filename.
223 * If it's blank, we skip completely. Otherwise, strip
224 * off any leading spaces and trailing newlines.
227 while (isspace((unsigned char) *s
))
230 while (e
> s
&& *e
== '\n')
238 entry
= mh_xmalloc(sizeof(*entry
));
239 entry
->filename
= getcpy(s
);
244 attach_tail
->next
= entry
;
247 attach_head
= attach_tail
= entry
;
250 add_header (ct
, np
, vp
);
254 /* if this wasn't the last header field, then continue */
258 fseek (in
, (long) (-strlen (buf
)), SEEK_CUR
);
265 adios (NULL
, "message format error in component #%d", compnum
);
268 adios (NULL
, "getfld() returned %d", state
);
272 m_getfld_state_destroy (&gstate
);
275 * Iterate through the list of headers and call the function to MIME-ify
279 for (hp
= ct
->c_first_hf
; hp
!= NULL
; hp
= hp
->next
) {
280 if (encode_rfc2047(hp
->name
, &hp
->value
, header_encoding
, NULL
)) {
281 adios(NULL
, "Unable to encode header \"%s\"", hp
->name
);
286 * Now add the MIME-Version header field
287 * to the list of header fields.
291 np
= add (VRSN_FIELD
, NULL
);
292 vp
= concat (" ", VRSN_VALUE
, "\n", NULL
);
293 add_header (ct
, np
, vp
);
297 * We initally assume we will find multiple contents in the
298 * draft. So create a multipart/mixed content to hold everything.
299 * We can remove this later, if it is not needed.
301 if (get_ctinfo ("multipart/mixed", ct
, 0) == NOTOK
)
303 ct
->c_type
= CT_MULTIPART
;
304 ct
->c_subtype
= MULTI_MIXED
;
305 ct
->c_file
= add (infile
, NULL
);
307 if ((m
= (struct multipart
*) calloc (1, sizeof(*m
))) == NULL
)
308 adios (NULL
, "out of memory");
309 ct
->c_ctparams
= (void *) m
;
313 * read and parse the composition file
314 * and the directives it contains.
316 while (fgetstr (buf
, sizeof(buf
) - 1, in
)) {
320 if (user_content (in
, buf
, &p
) == DONE
) {
321 admonish (NULL
, "ignoring spurious #end");
327 if ((part
= (struct part
*) calloc (1, sizeof(*part
))) == NULL
)
328 adios (NULL
, "out of memory");
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
);
373 * To allow for empty message bodies, if we've found NO content at all
374 * yet cook up an empty text/plain part.
382 if ((p
= (CT
) calloc (1, sizeof(*p
))) == NULL
)
383 adios(NULL
, "out of memory");
385 init_decoded_content(p
);
387 if (get_ctinfo ("text/plain", p
, 0) == NOTOK
)
391 p
->c_subtype
= TEXT_PLAIN
;
392 p
->c_encoding
= CE_7BIT
;
393 p
->c_file
= getcpy(infile
);
395 * Sigh. ce_file contains the "decoded" contents of this part.
396 * So this seems like the best option available since we're going
397 * to call scan_content() on this.
399 p
->c_cefile
.ce_file
= getcpy("/dev/null");
400 p
->c_begin
= ftell(in
);
401 p
->c_end
= ftell(in
);
403 if ((t
= (struct text
*) calloc (1, sizeof (*t
))) == NULL
)
404 adios (NULL
, "out of memory");
406 t
->tx_charset
= CHARSET_SPECIFIED
;
409 if ((part
= (struct part
*) calloc (1, sizeof(*part
))) == NULL
)
410 adios (NULL
, "out of memory");
417 * close the composition draft since
418 * it's not needed any longer.
423 * If only one content was found, then remove and
424 * free the outer multipart content.
426 if (!m
->mp_parts
->mp_next
) {
429 p
= m
->mp_parts
->mp_part
;
430 m
->mp_parts
->mp_part
= NULL
;
432 /* move header fields */
433 p
->c_first_hf
= ct
->c_first_hf
;
434 p
->c_last_hf
= ct
->c_last_hf
;
435 ct
->c_first_hf
= NULL
;
436 ct
->c_last_hf
= NULL
;
445 * Fill out, or expand directives. Parse and execute
446 * commands specified by profile composition strings.
448 compose_content (ct
);
450 if ((cp
= strchr(prefix
, 'a')) == NULL
)
451 adios (NULL
, "internal error(4)");
454 * Scan the contents. Choose a transfer encoding, and
455 * check if prefix for multipart boundary clashes with
456 * any of the contents.
458 while (scan_content (ct
) == NOTOK
) {
463 adios (NULL
, "giving up trying to find a unique delimiter string");
469 /* Build the rest of the header field structures */
478 * Set up structures for placing unencoded
479 * content when building parts.
483 init_decoded_content (CT ct
)
485 ct
->c_ceopenfnx
= open7Bit
; /* since unencoded */
486 ct
->c_ceclosefnx
= close_encoding
;
487 ct
->c_cesizefnx
= NULL
; /* since unencoded */
494 fgetstr (char *s
, int n
, FILE *stream
)
500 for (ep
= (cp
= s
) + o_n
; cp
< ep
; ) {
503 if (!fgets (cp
, n
, stream
))
504 return (cp
!= s
? s
: NULL
);
506 if (cp
== s
&& *cp
!= '#')
509 cp
+= (i
= strlen (cp
)) - 1;
510 if (i
<= 1 || *cp
-- != '\n' || *cp
!= '\\')
516 if (strcmp(s
, "#on\n") == 0) {
518 } else if (strcmp(s
, "#off\n") == 0) {
520 } else if (strcmp(s
, "#pop\n") == 0) {
532 * Parse the composition draft for text and directives.
533 * Do initial setup of Content structure.
537 user_content (FILE *in
, char *buf
, CT
*ctp
)
545 struct str2init
*s2i
;
550 if (buf
[0] == '\n' || (do_direct() && strcmp (buf
, "#\n") == 0)) {
555 /* allocate basic Content structure */
556 if ((ct
= (CT
) calloc (1, sizeof(*ct
))) == NULL
)
557 adios (NULL
, "out of memory");
560 /* allocate basic structure for handling decoded content */
561 init_decoded_content (ct
);
568 * Handle inline text. Check if line
569 * is one of the following forms:
571 * 1) doesn't begin with '#' (implicit directive)
572 * 2) begins with "##" (implicit directive)
573 * 3) begins with "#<"
575 if (!do_direct() || buf
[0] != '#' || buf
[1] == '#' || buf
[1] == '<') {
579 char content
[BUFSIZ
];
583 cp
= m_mktemp2(NULL
, invo_name
, NULL
, &out
);
584 if (cp
== NULL
) adios("mhbuildsbr", "unable to create temporary file");
586 /* use a temp file to collect the plain text lines */
587 ce
->ce_file
= add (cp
, NULL
);
590 if (do_direct() && (buf
[0] == '#' && buf
[1] == '<')) {
591 strncpy (content
, buf
+ 2, sizeof(content
));
598 /* the directive is implicit */
599 strncpy (content
, "text/plain", sizeof(content
));
601 strncpy (buffer
, (!do_direct() || buf
[0] != '#') ? buf
: buf
+ 1, sizeof(buffer
));
605 if (headers
>= 0 && do_direct() && uprf (buffer
, DESCR_FIELD
)
606 && buffer
[i
= strlen (DESCR_FIELD
)] == ':') {
610 ct
->c_descr
= add (buffer
+ i
+ 1, ct
->c_descr
);
611 if (!fgetstr (buffer
, sizeof(buffer
) - 1, in
))
612 adios (NULL
, "end-of-file after %s: field in plaintext", DESCR_FIELD
);
620 adios (NULL
, "#-directive after %s: field in plaintext", DESCR_FIELD
);
628 if (headers
>= 0 && do_direct() && uprf (buffer
, DISPO_FIELD
)
629 && buffer
[i
= strlen (DISPO_FIELD
)] == ':') {
633 ct
->c_dispo
= add (buffer
+ i
+ 1, ct
->c_dispo
);
634 if (!fgetstr (buffer
, sizeof(buffer
) - 1, in
))
635 adios (NULL
, "end-of-file after %s: field in plaintext", DISPO_FIELD
);
643 adios (NULL
, "#-directive after %s: field in plaintext", DISPO_FIELD
);
651 if (headers
!= 1 || buffer
[0] != '\n')
657 if ((cp
= fgetstr (buffer
, sizeof(buffer
) - 1, in
)) == NULL
)
659 if (do_direct() && buffer
[0] == '#') {
662 if (buffer
[1] != '#')
664 for (cp
= (bp
= buffer
) + 1; *cp
; cp
++)
671 ct
->c_end
= ftell (out
);
674 /* parse content type */
675 if (get_ctinfo (content
, ct
, inlineD
) == NOTOK
)
678 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
679 if (!strcasecmp (ci
->ci_type
, s2i
->si_key
))
681 if (!s2i
->si_key
&& !uprf (ci
->ci_type
, "X-"))
685 * check type specified (possibly implicitly)
687 switch (ct
->c_type
= s2i
->si_val
) {
689 if (!strcasecmp (ci
->ci_subtype
, "rfc822")) {
690 ct
->c_encoding
= CE_7BIT
;
695 adios (NULL
, "it doesn't make sense to define an in-line %s content",
696 ct
->c_type
== CT_MESSAGE
? "message" : "multipart");
701 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
702 (*ct
->c_ctinitfnx
) (ct
);
707 fseek (in
, pos
, SEEK_SET
);
712 * If we've reached this point, the next line
713 * must be some type of explicit directive.
716 /* check if directive is external-type */
717 extrnal
= (buf
[1] == '@');
719 /* parse directive */
720 if (get_ctinfo (buf
+ (extrnal
? 2 : 1), ct
, 1) == NOTOK
)
723 /* check directive against the list of MIME types */
724 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
725 if (!strcasecmp (ci
->ci_type
, s2i
->si_key
))
729 * Check if the directive specified a valid type.
730 * This will happen if it was one of the following forms:
737 adios (NULL
, "missing subtype in \"#%s\"", ci
->ci_type
);
739 switch (ct
->c_type
= s2i
->si_val
) {
741 adios (NULL
, "use \"#begin ... #end\" instead of \"#%s/%s\"",
742 ci
->ci_type
, ci
->ci_subtype
);
746 if (!strcasecmp (ci
->ci_subtype
, "partial"))
747 adios (NULL
, "sorry, \"#%s/%s\" isn't supported",
748 ci
->ci_type
, ci
->ci_subtype
);
749 if (!strcasecmp (ci
->ci_subtype
, "external-body"))
750 adios (NULL
, "use \"#@type/subtype ... [] ...\" instead of \"#%s/%s\"",
751 ci
->ci_type
, ci
->ci_subtype
);
754 "use \"#forw [+folder] [msgs]\" instead of \"#%s/%s\"",
755 ci
->ci_type
, ci
->ci_subtype
);
759 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
760 (*ct
->c_ctinitfnx
) (ct
);
765 * #@type/subtype (external types directive)
772 adios (NULL
, "need external information for \"#@%s/%s\"",
773 ci
->ci_type
, ci
->ci_subtype
);
776 snprintf (buffer
, sizeof(buffer
), "message/external-body; %s", ci
->ci_magic
);
781 * Since we are using the current Content structure to
782 * hold information about the type of the external
783 * reference, we need to create another Content structure
784 * for the message/external-body to wrap it in.
786 if ((ct
= (CT
) calloc (1, sizeof(*ct
))) == NULL
)
787 adios (NULL
, "out of memory");
790 if (get_ctinfo (buffer
, ct
, 0) == NOTOK
)
792 ct
->c_type
= CT_MESSAGE
;
793 ct
->c_subtype
= MESSAGE_EXTERNAL
;
795 if ((e
= (struct exbody
*) calloc (1, sizeof(*e
))) == NULL
)
796 adios (NULL
, "out of memory");
797 ct
->c_ctparams
= (void *) e
;
803 if (params_external (ct
, 1) == NOTOK
)
809 /* Handle [file] argument */
811 /* check if specifies command to execute */
812 if (*ci
->ci_magic
== '|' || *ci
->ci_magic
== '!') {
813 for (cp
= ci
->ci_magic
+ 1; isspace ((unsigned char) *cp
); cp
++)
816 adios (NULL
, "empty pipe command for #%s directive", ci
->ci_type
);
821 /* record filename of decoded contents */
822 ce
->ce_file
= ci
->ci_magic
;
823 if (access (ce
->ce_file
, R_OK
) == NOTOK
)
824 adios ("reading", "unable to access %s for", ce
->ce_file
);
825 if (listsw
&& stat (ce
->ce_file
, &st
) != NOTOK
)
826 ct
->c_end
= (long) st
.st_size
;
833 * No [file] argument, so check profile for
834 * method to compose content.
836 snprintf (buffer
, sizeof(buffer
), "%s-compose-%s/%s",
837 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
838 if ((cp
= context_find (buffer
)) == NULL
|| *cp
== '\0') {
839 snprintf (buffer
, sizeof(buffer
), "%s-compose-%s", invo_name
, ci
->ci_type
);
840 if ((cp
= context_find (buffer
)) == NULL
|| *cp
== '\0') {
841 content_error (NULL
, ct
, "don't know how to compose content");
845 ci
->ci_magic
= add (cp
, NULL
);
850 adios (NULL
, "external definition not allowed for \"#%s\"", ci
->ci_type
);
854 * #forw [+folder] [msgs]
856 if (!strcasecmp (ci
->ci_type
, "forw")) {
858 char *folder
, *arguments
[MAXARGS
];
862 ap
= brkstring (ci
->ci_magic
, " ", "\n");
863 copyip (ap
, arguments
, MAXARGS
);
865 arguments
[0] = "cur";
870 /* search the arguments for a folder name */
871 for (ap
= arguments
; *ap
; ap
++) {
873 if (*cp
== '+' || *cp
== '@') {
875 adios (NULL
, "only one folder per #forw directive");
877 folder
= pluspath (cp
);
881 /* else, use the current folder */
883 folder
= add (getfolder (1), NULL
);
885 if (!(mp
= folder_read (folder
, 0)))
886 adios (NULL
, "unable to read folder %s", folder
);
887 for (ap
= arguments
; *ap
; ap
++) {
889 if (*cp
!= '+' && *cp
!= '@')
890 if (!m_convert (mp
, cp
))
897 * If there is more than one message to include, make this
898 * a content of type "multipart/digest" and insert each message
899 * as a subpart. If there is only one message, then make this
900 * a content of type "message/rfc822".
902 if (mp
->numsel
> 1) {
903 /* we are forwarding multiple messages */
904 if (get_ctinfo ("multipart/digest", ct
, 0) == NOTOK
)
906 ct
->c_type
= CT_MULTIPART
;
907 ct
->c_subtype
= MULTI_DIGEST
;
909 if ((m
= (struct multipart
*) calloc (1, sizeof(*m
))) == NULL
)
910 adios (NULL
, "out of memory");
911 ct
->c_ctparams
= (void *) m
;
914 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
915 if (is_selected(mp
, msgnum
)) {
920 if ((p
= (CT
) calloc (1, sizeof(*p
))) == NULL
)
921 adios (NULL
, "out of memory");
922 init_decoded_content (p
);
924 if (get_ctinfo ("message/rfc822", p
, 0) == NOTOK
)
926 p
->c_type
= CT_MESSAGE
;
927 p
->c_subtype
= MESSAGE_RFC822
;
929 snprintf (buffer
, sizeof(buffer
), "%s/%d", mp
->foldpath
, msgnum
);
930 pe
->ce_file
= add (buffer
, NULL
);
931 if (listsw
&& stat (pe
->ce_file
, &st
) != NOTOK
)
932 p
->c_end
= (long) st
.st_size
;
934 if ((part
= (struct part
*) calloc (1, sizeof(*part
))) == NULL
)
935 adios (NULL
, "out of memory");
942 /* we are forwarding one message */
943 if (get_ctinfo ("message/rfc822", ct
, 0) == NOTOK
)
945 ct
->c_type
= CT_MESSAGE
;
946 ct
->c_subtype
= MESSAGE_RFC822
;
949 snprintf (buffer
, sizeof(buffer
), "%s/%d", mp
->foldpath
, msgnum
);
950 ce
->ce_file
= add (buffer
, NULL
);
951 if (listsw
&& stat (ce
->ce_file
, &st
) != NOTOK
)
952 ct
->c_end
= (long) st
.st_size
;
955 folder_free (mp
); /* free folder/message structure */
962 if (!strcasecmp (ci
->ci_type
, "end")) {
969 * #begin [ alternative | parallel ]
971 if (!strcasecmp (ci
->ci_type
, "begin")) {
974 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
975 } else if (!strcasecmp (ci
->ci_magic
, "alternative")) {
976 vrsn
= MULTI_ALTERNATE
;
977 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
978 } else if (!strcasecmp (ci
->ci_magic
, "parallel")) {
979 vrsn
= MULTI_PARALLEL
;
980 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
981 } else if (uprf (ci
->ci_magic
, "digest")) {
984 vrsn
= MULTI_UNKNOWN
;
989 snprintf (buffer
, sizeof(buffer
), "multipart/%s", cp
);
990 if (get_ctinfo (buffer
, ct
, 0) == NOTOK
)
992 ct
->c_type
= CT_MULTIPART
;
993 ct
->c_subtype
= vrsn
;
995 if ((m
= (struct multipart
*) calloc (1, sizeof(*m
))) == NULL
)
996 adios (NULL
, "out of memory");
997 ct
->c_ctparams
= (void *) m
;
1000 while (fgetstr (buffer
, sizeof(buffer
) - 1, in
)) {
1004 if (user_content (in
, buffer
, &p
) == DONE
) {
1006 adios (NULL
, "empty \"#begin ... #end\" sequence");
1012 if ((part
= (struct part
*) calloc (1, sizeof(*part
))) == NULL
)
1013 adios (NULL
, "out of memory");
1015 pp
= &part
->mp_next
;
1018 admonish (NULL
, "premature end-of-file, missing #end");
1025 adios (NULL
, "unknown directive \"#%s\"", ci
->ci_type
);
1026 return NOTOK
; /* NOT REACHED */
1031 set_id (CT ct
, int top
)
1033 char contentid
[BUFSIZ
];
1035 static time_t clock
= 0;
1036 static char *msgfmt
;
1040 snprintf (contentid
, sizeof(contentid
), "%s\n", message_id (clock
, 1));
1042 msgfmt
= getcpy(contentid
);
1044 snprintf (contentid
, sizeof(contentid
), msgfmt
, top
? 0 : ++partno
);
1045 ct
->c_id
= getcpy (contentid
);
1050 * Fill out, or expand the various contents in the composition
1051 * draft. Read-in any necessary files. Parse and execute any
1052 * commands specified by profile composition strings.
1056 compose_content (CT ct
)
1058 CE ce
= &ct
->c_cefile
;
1060 switch (ct
->c_type
) {
1065 char partnam
[BUFSIZ
];
1066 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1070 snprintf (partnam
, sizeof(partnam
), "%s.", ct
->c_partno
);
1071 pp
= partnam
+ strlen (partnam
);
1076 /* first, we call compose_content on all the subparts */
1077 for (part
= m
->mp_parts
, partnum
= 1; part
; part
= part
->mp_next
, partnum
++) {
1078 CT p
= part
->mp_part
;
1080 sprintf (pp
, "%d", partnum
);
1081 p
->c_partno
= add (partnam
, NULL
);
1082 if (compose_content (p
) == NOTOK
)
1087 * If the -rfc934mode switch is given, then check all
1088 * the subparts of a multipart/digest. If they are all
1089 * message/rfc822, then mark this content and all
1090 * subparts with the rfc934 compatibility mode flag.
1092 if (rfc934sw
&& ct
->c_subtype
== MULTI_DIGEST
) {
1095 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1096 CT p
= part
->mp_part
;
1098 if (p
->c_subtype
!= MESSAGE_RFC822
) {
1103 ct
->c_rfc934
= is934
;
1104 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1105 CT p
= part
->mp_part
;
1107 if ((p
->c_rfc934
= is934
))
1113 ct
->c_end
= (partnum
= strlen (prefix
) + 2) + 2;
1117 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
)
1118 ct
->c_end
+= part
->mp_part
->c_end
+ partnum
;
1124 /* Nothing to do for type message */
1128 * Discrete types (text/application/audio/image/video)
1133 int i
, xstdout
, len
, buflen
;
1134 char *bp
, **ap
, *cp
;
1135 char *vec
[4], buffer
[BUFSIZ
];
1137 CI ci
= &ct
->c_ctinfo
;
1140 if (!(cp
= ci
->ci_magic
))
1141 adios (NULL
, "internal error(5)");
1143 tfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
);
1144 if (tfile
== NULL
) {
1145 adios("mhbuildsbr", "unable to create temporary file");
1147 ce
->ce_file
= add (tfile
, NULL
);
1152 /* Get buffer ready to go */
1155 buflen
= sizeof(buffer
);
1158 * Parse composition string into buffer
1160 for ( ; *cp
; cp
++) {
1165 /* insert parameters from directive */
1169 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
1170 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, *ap
, *ep
);
1180 /* %f, and stdout is not-redirected */
1186 * insert temporary filename where
1187 * content should be written
1189 snprintf (bp
, buflen
, "%s", ce
->ce_file
);
1193 /* insert content subtype */
1194 strncpy (bp
, ci
->ci_subtype
, buflen
);
1198 /* insert character % */
1219 printf ("composing content %s/%s from command\n\t%s\n",
1220 ci
->ci_type
, ci
->ci_subtype
, buffer
);
1222 fflush (stdout
); /* not sure if need for -noverbose */
1229 if ((out
= fopen (ce
->ce_file
, "w")) == NULL
)
1230 adios (ce
->ce_file
, "unable to open for writing");
1232 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
> 5; i
++)
1236 adios ("fork", "unable to fork");
1241 dup2 (fileno (out
), 1);
1242 close (fileno (out
));
1243 execvp ("/bin/sh", vec
);
1244 fprintf (stderr
, "unable to exec ");
1251 if (pidXwait(child_id
, NULL
))
1257 /* Check size of file */
1258 if (listsw
&& ct
->c_end
== 0L) {
1261 if (stat (ce
->ce_file
, &st
) != NOTOK
)
1262 ct
->c_end
= (long) st
.st_size
;
1274 * 1) choose a transfer encoding.
1275 * 2) check for clashes with multipart boundary string.
1276 * 3) for text content, figure out which character set is being used.
1278 * If there is a clash with one of the contents and the multipart boundary,
1279 * this function will exit with NOTOK. This will cause the scanning process
1280 * to be repeated with a different multipart boundary. It is possible
1281 * (although highly unlikely) that this scan will be repeated multiple times.
1285 scan_content (CT ct
)
1288 int check8bit
= 0, contains8bit
= 0; /* check if contains 8bit data */
1289 int checklinelen
= 0, linelen
= 0; /* check for long lines */
1290 int checkboundary
= 0, boundaryclash
= 0; /* check if clashes with multipart boundary */
1291 int checklinespace
= 0, linespace
= 0; /* check if any line ends with space */
1292 char *cp
= NULL
, buffer
[BUFSIZ
];
1293 struct text
*t
= NULL
;
1295 CE ce
= &ct
->c_cefile
;
1298 * handle multipart by scanning all subparts
1299 * and then checking their encoding.
1301 if (ct
->c_type
== CT_MULTIPART
) {
1302 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1305 /* initially mark the domain of enclosing multipart as 7bit */
1306 ct
->c_encoding
= CE_7BIT
;
1308 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1309 CT p
= part
->mp_part
;
1311 if (scan_content (p
) == NOTOK
) /* choose encoding for subpart */
1314 /* if necessary, enlarge encoding for enclosing multipart */
1315 if (p
->c_encoding
== CE_BINARY
)
1316 ct
->c_encoding
= CE_BINARY
;
1317 if (p
->c_encoding
== CE_8BIT
&& ct
->c_encoding
!= CE_BINARY
)
1318 ct
->c_encoding
= CE_8BIT
;
1325 * Decide what to check while scanning this content.
1327 switch (ct
->c_type
) {
1331 if (ct
->c_subtype
== TEXT_PLAIN
) {
1340 case CT_APPLICATION
:
1352 /* don't check anything for message/external */
1353 if (ct
->c_subtype
== MESSAGE_EXTERNAL
)
1363 * Don't check anything for these types,
1364 * since we are forcing use of base64.
1374 * Scan the unencoded content
1376 if (check8bit
|| checklinelen
|| checklinespace
|| checkboundary
) {
1377 if ((in
= fopen (ce
->ce_file
, "r")) == NULL
)
1378 adios (ce
->ce_file
, "unable to open for reading");
1379 len
= strlen (prefix
);
1381 while (fgets (buffer
, sizeof(buffer
) - 1, in
)) {
1383 * Check for 8bit data.
1386 for (cp
= buffer
; *cp
; cp
++) {
1387 if (!isascii ((unsigned char) *cp
)) {
1389 check8bit
= 0; /* no need to keep checking */
1395 * Check line length.
1397 if (checklinelen
&& (strlen (buffer
) > CPERLIN
+ 1)) {
1399 checklinelen
= 0; /* no need to keep checking */
1403 * Check if line ends with a space.
1405 if (checklinespace
&& (cp
= buffer
+ strlen (buffer
) - 2) > buffer
&& isspace ((unsigned char) *cp
)) {
1407 checklinespace
= 0; /* no need to keep checking */
1411 * Check if content contains a line that clashes
1412 * with our standard boundary for multipart messages.
1414 if (checkboundary
&& buffer
[0] == '-' && buffer
[1] == '-') {
1415 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1416 if (!isspace ((unsigned char) *cp
))
1419 if (!strncmp(buffer
+ 2, prefix
, len
) && isdigit((unsigned char) buffer
[2 + len
])) {
1421 checkboundary
= 0; /* no need to keep checking */
1429 * Decide which transfer encoding to use.
1431 switch (ct
->c_type
) {
1434 * If the text content didn't specify a character
1435 * set, we need to figure out which one was used.
1437 t
= (struct text
*) ct
->c_ctparams
;
1438 if (t
->tx_charset
== CHARSET_UNSPECIFIED
) {
1439 CI ci
= &ct
->c_ctinfo
;
1442 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++)
1446 *ap
= concat ("charset=", write_charset_8bit(), NULL
);
1448 *ap
= add ("charset=us-ascii", NULL
);
1450 t
->tx_charset
= CHARSET_SPECIFIED
;
1452 cp
= strchr(*ap
++, '=');
1458 if (contains8bit
|| linelen
|| linespace
|| checksw
)
1459 ct
->c_encoding
= CE_QUOTED
;
1461 ct
->c_encoding
= CE_7BIT
;
1464 case CT_APPLICATION
:
1465 /* For application type, use base64, except when postscript */
1466 if (contains8bit
|| linelen
|| linespace
|| checksw
)
1467 ct
->c_encoding
= (ct
->c_subtype
== APPLICATION_POSTSCRIPT
)
1468 ? CE_QUOTED
: CE_BASE64
;
1470 ct
->c_encoding
= CE_7BIT
;
1474 ct
->c_encoding
= CE_7BIT
;
1480 /* For audio, image, and video contents, just use base64 */
1481 ct
->c_encoding
= CE_BASE64
;
1485 return (boundaryclash
? NOTOK
: OK
);
1490 * Scan the content structures, and build header
1491 * fields that will need to be output into the
1496 build_headers (CT ct
)
1498 int cc
, mailbody
, extbody
, len
;
1500 char *np
, *vp
, buffer
[BUFSIZ
];
1501 CI ci
= &ct
->c_ctinfo
;
1504 * If message is type multipart, then add the multipart
1505 * boundary to the list of attribute/value pairs.
1507 if (ct
->c_type
== CT_MULTIPART
) {
1509 static int level
= 0; /* store nesting level */
1513 snprintf (buffer
, sizeof(buffer
), "boundary=%s%d", prefix
, level
++);
1514 cp
= strchr(*ap
++ = add (buffer
, NULL
), '=');
1521 * Skip the output of Content-Type, parameters, content
1522 * description and disposition, and Content-ID if the
1523 * content is of type "message" and the rfc934 compatibility
1524 * flag is set (which means we are inside multipart/digest
1525 * and the switch -rfc934mode was given).
1527 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_rfc934
)
1531 * output the content type and subtype
1533 np
= add (TYPE_FIELD
, NULL
);
1534 vp
= concat (" ", ci
->ci_type
, "/", ci
->ci_subtype
, NULL
);
1536 /* keep track of length of line */
1537 len
= strlen (TYPE_FIELD
) + strlen (ci
->ci_type
)
1538 + strlen (ci
->ci_subtype
) + 3;
1540 extbody
= ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_EXTERNAL
;
1541 mailbody
= extbody
&& ((struct exbody
*) ct
->c_ctparams
)->eb_body
;
1544 * Append the attribute/value pairs to
1545 * the end of the Content-Type line.
1547 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
1548 if (mailbody
&& !strcasecmp (*ap
, "body"))
1555 * According to RFC 2017, if we have a URL longer than 40 characters
1556 * we have to break it across multiple lines
1559 if (extbody
&& strcasecmp (*ap
, "url") == 0) {
1562 /* 7 here refers to " url=\"\"" */
1563 if (len
+ 1 + (cc
= (min(MAXURLTOKEN
, strlen(value
)) + 7)) >=
1565 vp
= add ("\n\t", vp
);
1572 vp
= add ("url=\"", vp
);
1575 while (strlen(value
) > MAXURLTOKEN
) {
1576 strncpy(buffer
, value
, MAXURLTOKEN
);
1577 buffer
[MAXURLTOKEN
] = '\0';
1578 vp
= add (buffer
, vp
);
1579 vp
= add ("\n\t", vp
);
1580 value
+= MAXURLTOKEN
;
1584 vp
= add (value
, vp
);
1585 vp
= add ("\"", vp
);
1586 len
+= strlen(value
) + 1;
1590 snprintf (buffer
, sizeof(buffer
), "%s=\"%s\"", *ap
, *ep
);
1591 if (len
+ 1 + (cc
= strlen (buffer
)) >= CPERLIN
) {
1592 vp
= add ("\n\t", vp
);
1598 vp
= add (buffer
, vp
);
1603 * Append any RFC-822 comment to the end of
1604 * the Content-Type line.
1606 if (ci
->ci_comment
) {
1607 snprintf (buffer
, sizeof(buffer
), "(%s)", ci
->ci_comment
);
1608 if (len
+ 1 + (cc
= 2 + strlen (ci
->ci_comment
)) >= CPERLIN
) {
1609 vp
= add ("\n\t", vp
);
1615 vp
= add (buffer
, vp
);
1618 vp
= add ("\n", vp
);
1619 add_header (ct
, np
, vp
);
1622 * output the Content-ID, unless disabled by -nocontentid
1624 if (contentidsw
&& ct
->c_id
) {
1625 np
= add (ID_FIELD
, NULL
);
1626 vp
= concat (" ", ct
->c_id
, NULL
);
1627 add_header (ct
, np
, vp
);
1631 * output the Content-Description
1634 np
= add (DESCR_FIELD
, NULL
);
1635 vp
= concat (" ", ct
->c_descr
, NULL
);
1636 add_header (ct
, np
, vp
);
1640 * output the Content-Disposition
1643 np
= add (DISPO_FIELD
, NULL
);
1644 vp
= concat (" ", ct
->c_dispo
, NULL
);
1645 add_header (ct
, np
, vp
);
1650 * If this is the internal content structure for a
1651 * "message/external", then we are done with the
1652 * headers (since it has no body).
1658 * output the Content-MD5
1661 np
= add (MD5_FIELD
, NULL
);
1662 vp
= calculate_digest (ct
, (ct
->c_encoding
== CE_QUOTED
) ? 1 : 0);
1663 add_header (ct
, np
, vp
);
1667 * output the Content-Transfer-Encoding
1669 switch (ct
->c_encoding
) {
1671 /* Nothing to output */
1675 if (ct
->c_type
== CT_MESSAGE
)
1676 adios (NULL
, "internal error, invalid encoding");
1678 np
= add (ENCODING_FIELD
, NULL
);
1679 vp
= concat (" ", "8bit", "\n", NULL
);
1680 add_header (ct
, np
, vp
);
1684 if (ct
->c_type
== CT_MESSAGE
|| ct
->c_type
== CT_MULTIPART
)
1685 adios (NULL
, "internal error, invalid encoding");
1687 np
= add (ENCODING_FIELD
, NULL
);
1688 vp
= concat (" ", "quoted-printable", "\n", NULL
);
1689 add_header (ct
, np
, vp
);
1693 if (ct
->c_type
== CT_MESSAGE
|| ct
->c_type
== CT_MULTIPART
)
1694 adios (NULL
, "internal error, invalid encoding");
1696 np
= add (ENCODING_FIELD
, NULL
);
1697 vp
= concat (" ", "base64", "\n", NULL
);
1698 add_header (ct
, np
, vp
);
1702 if (ct
->c_type
== CT_MESSAGE
)
1703 adios (NULL
, "internal error, invalid encoding");
1705 np
= add (ENCODING_FIELD
, NULL
);
1706 vp
= concat (" ", "binary", "\n", NULL
);
1707 add_header (ct
, np
, vp
);
1711 adios (NULL
, "unknown transfer encoding in content");
1716 * Additional content specific header processing
1718 switch (ct
->c_type
) {
1721 struct multipart
*m
;
1724 m
= (struct multipart
*) ct
->c_ctparams
;
1725 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1735 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1738 e
= (struct exbody
*) ct
->c_ctparams
;
1739 build_headers (e
->eb_content
);
1752 static char nib2b64
[0x40+1] =
1753 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1756 calculate_digest (CT ct
, int asciiP
)
1759 char buffer
[BUFSIZ
], *vp
, *op
;
1761 unsigned char digest
[16];
1762 unsigned char outbuf
[25];
1764 CE ce
= &ct
->c_cefile
;
1765 char *infilename
= ce
->ce_file
? ce
->ce_file
: ct
->c_file
;
1769 if ((in
= fopen (infilename
, "r")) == NULL
)
1770 adios (infilename
, "unable to open for reading");
1772 /* Initialize md5 context */
1773 MD5Init (&mdContext
);
1775 /* calculate md5 message digest */
1777 while (fgets (buffer
, sizeof(buffer
) - 1, in
)) {
1780 cp
= buffer
+ strlen (buffer
) - 1;
1781 if ((c
= *cp
) == '\n')
1784 MD5Update (&mdContext
, (unsigned char *) buffer
,
1785 (unsigned int) strlen (buffer
));
1788 MD5Update (&mdContext
, (unsigned char *) "\r\n", 2);
1791 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), in
)) > 0)
1792 MD5Update (&mdContext
, (unsigned char *) buffer
, (unsigned int) cc
);
1795 /* md5 finalization. Write digest and zero md5 context */
1796 MD5Final (digest
, &mdContext
);
1801 /* print debugging info */
1805 fprintf (stderr
, "MD5 digest=");
1806 for (ep
= (dp
= digest
) + sizeof(digest
) / sizeof(digest
[0]);
1808 fprintf (stderr
, "%02x", *dp
& 0xff);
1809 fprintf (stderr
, "\n");
1812 /* encode the digest using base64 */
1813 for (dp
= digest
, op
= (char *) outbuf
,
1814 cc
= sizeof(digest
) / sizeof(digest
[0]);
1815 cc
> 0; cc
-= 3, op
+= 4) {
1819 bits
= (*dp
++ & 0xff) << 16;
1821 bits
|= (*dp
++ & 0xff) << 8;
1823 bits
|= *dp
++ & 0xff;
1826 for (bp
= op
+ 4; bp
> op
; bits
>>= 6)
1827 *--bp
= nib2b64
[bits
& 0x3f];
1835 /* null terminate string */
1838 /* now make copy and return string */
1839 vp
= concat (" ", outbuf
, "\n", NULL
);
1844 * Set things up for the content structure for file "filename" that
1849 setup_attach_content(CT ct
, char *filename
)
1851 char *type
, **ap
, **ep
, *simplename
= r1bindex(filename
, '/');
1852 struct str2init
*s2i
;
1854 if (! (type
= mime_type(filename
))) {
1855 adios(NULL
, "Unable to determine MIME type of \"%s\"", filename
);
1859 * Parse the Content-Type. get_ctinfo() parses MIME parameters, but
1860 * since we're just feeding it a MIME type we have to add those ourself.
1861 * Map that to a valid content-type label and call any initialization
1865 if (get_ctinfo(type
, ct
, 0) == NOTOK
)
1870 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
1871 if (strcasecmp(ct
->c_ctinfo
.ci_type
, s2i
->si_key
) == 0)
1873 if (!s2i
->si_key
&& !uprf(ct
->c_ctinfo
.ci_type
, "X-"))
1877 * Make sure the type isn't incompatible with what we can handle
1880 switch (ct
->c_type
= s2i
->si_val
) {
1882 adios (NULL
, "multipart types must be specified by mhbuild directives");
1886 if (strcasecmp(ct
->c_ctinfo
.ci_subtype
, "partial") == 0)
1887 adios(NULL
, "Sorry, %s/%s isn't supported", ct
->c_ctinfo
.ci_type
,
1888 ct
->c_ctinfo
.ci_subtype
);
1889 if (strcasecmp(ct
->c_ctinfo
.ci_subtype
, "external-body") == 0)
1890 adios(NULL
, "external-body messages must be specified "
1891 "by mhbuild directives");
1896 * This sets the subtype, if it's significant
1898 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
1899 (*ct
->c_ctinitfnx
)(ct
);
1904 * Feed in a few attributes; specifically, the name attribute, the
1905 * content-description, and the content-disposition.
1908 for (ap
= ct
->c_ctinfo
.ci_attrs
, ep
= ct
->c_ctinfo
.ci_values
; *ap
;
1910 if (strcasecmp(*ap
, "name") == 0) {
1913 *ep
= getcpy(simplename
);
1919 *ap
= getcpy("name");
1920 *ep
= getcpy(simplename
);
1923 ct
->c_descr
= getcpy(simplename
);
1924 ct
->c_descr
= add("\n", ct
->c_descr
);
1925 ct
->c_cefile
.ce_file
= getcpy(filename
);
1928 * If it's a text/calendar, we need to make sure it's an inline,
1929 * otherwise it won't work with some calendar programs. Otherwise
1933 if (strcasecmp(ct
->c_ctinfo
.ci_type
, "text") == 0 &&
1934 strcasecmp(ct
->c_ctinfo
.ci_subtype
, "calendar") == 0) {
1935 ct
->c_dispo
= getcpy("inline; filename=\"");
1937 ct
->c_dispo
= getcpy("attachment; filename=\"");
1940 ct
->c_dispo
= add(simplename
, ct
->c_dispo
);
1941 ct
->c_dispo
= add("\"\n", ct
->c_dispo
);