]>
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>
38 extern int contentidsw
;
41 extern int rcachesw
; /* mhcachesbr.c */
42 extern int wcachesw
; /* mhcachesbr.c */
44 static char prefix
[] = "----- =_aaaaaaaaaa";
48 struct attach_list
*next
;
52 * Maximum size of URL token in message/external-body
55 #define MAXURLTOKEN 40
59 void content_error (char *, CT
, char *, ...);
62 int find_cache (CT
, int, int *, char *, char *, int);
65 void free_ctinfo (CT
);
66 void free_encoding (CT
, int);
71 static int init_decoded_content (CT
, const char *);
72 static void setup_attach_content(CT
, char *);
73 static char *fgetstr (char *, int, FILE *);
74 static int user_content (FILE *, char *, CT
*, const char *infilename
);
75 static void set_id (CT
, int);
76 static int compose_content (CT
, int);
77 static int scan_content (CT
, size_t);
78 static int build_headers (CT
, int);
79 static char *calculate_digest (CT
, int);
82 static unsigned char directives_stack
[32];
83 static unsigned int directives_index
;
85 static int do_direct(void)
87 return directives_stack
[directives_index
];
90 static void directive_onoff(int onoff
)
92 if (directives_index
>= sizeof(directives_stack
) - 1) {
93 fprintf(stderr
, "mhbuild: #on/off overflow, continuing\n");
96 directives_stack
[++directives_index
] = onoff
;
99 static void directive_init(int onoff
)
101 directives_index
= 0;
102 directives_stack
[0] = onoff
;
105 static void directive_pop(void)
107 if (directives_index
> 0)
110 fprintf(stderr
, "mhbuild: #pop underflow, continuing\n");
114 * Main routine for translating composition file
115 * into valid MIME message. It translates the draft
116 * into a content structure (actually a tree of content
117 * structures). This message then can be manipulated
118 * in various ways, including being output via
123 build_mime (char *infile
, int autobuild
, int dist
, int directives
,
124 int header_encoding
, size_t maxunencoded
, int verbose
)
127 char buf
[BUFSIZ
], name
[NAMESZ
];
134 m_getfld_state_t gstate
= 0;
135 struct attach_list
*attach_head
= NULL
, *attach_tail
= NULL
, *at_entry
;
137 directive_init(directives
);
139 umask (~m_gmprot ());
141 /* open the composition draft */
142 if ((in
= fopen (infile
, "r")) == NULL
)
143 adios (infile
, "unable to open for reading");
146 * Allocate space for primary (outside) content
148 if ((ct
= (CT
) calloc (1, sizeof(*ct
))) == NULL
)
149 adios (NULL
, "out of memory");
152 * Allocate structure for handling decoded content
153 * for this part. We don't really need this, but
154 * allocate it to remain consistent.
156 init_decoded_content (ct
, infile
);
159 * Parse some of the header fields in the composition
160 * draft into the linked list of header fields for
161 * the new MIME message.
163 m_getfld_track_filepos (&gstate
, in
);
164 for (compnum
= 1;;) {
165 int bufsz
= sizeof buf
;
166 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
)) {
171 /* abort if draft has Mime-Version or C-T-E header field */
172 if (strcasecmp (name
, VRSN_FIELD
) == 0 ||
173 strcasecmp (name
, ENCODING_FIELD
) == 0) {
179 adios (NULL
, "draft shouldn't contain %s: field", name
);
183 /* ignore any Content-Type fields in the header */
184 if (!strcasecmp (name
, TYPE_FIELD
)) {
185 while (state
== FLDPLUS
) {
187 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
192 /* get copies of the buffers */
193 np
= add (name
, NULL
);
194 vp
= add (buf
, NULL
);
196 /* if necessary, get rest of field */
197 while (state
== FLDPLUS
) {
199 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
200 vp
= add (buf
, vp
); /* add to previous value */
204 * Now add the header data to the list, unless it's an attach
205 * header; in that case, add it to our attach list
208 if (strcasecmp(ATTACH_FIELD
, np
) == 0) {
209 struct attach_list
*entry
;
210 char *s
= vp
, *e
= vp
+ strlen(vp
) - 1;
214 * Make sure we can find the start of this filename.
215 * If it's blank, we skip completely. Otherwise, strip
216 * off any leading spaces and trailing newlines.
219 while (isspace((unsigned char) *s
))
222 while (e
> s
&& *e
== '\n')
230 entry
= mh_xmalloc(sizeof(*entry
));
231 entry
->filename
= getcpy(s
);
236 attach_tail
->next
= entry
;
239 attach_head
= attach_tail
= entry
;
242 add_header (ct
, np
, vp
);
246 /* if this wasn't the last header field, then continue */
250 fseek (in
, (long) (-strlen (buf
)), SEEK_CUR
);
257 adios (NULL
, "message format error in component #%d", compnum
);
260 adios (NULL
, "getfld() returned %d", state
);
264 m_getfld_state_destroy (&gstate
);
267 * Iterate through the list of headers and call the function to MIME-ify
271 for (hp
= ct
->c_first_hf
; hp
!= NULL
; hp
= hp
->next
) {
272 if (encode_rfc2047(hp
->name
, &hp
->value
, header_encoding
, NULL
)) {
273 adios(NULL
, "Unable to encode header \"%s\"", hp
->name
);
278 * Now add the MIME-Version header field
279 * to the list of header fields.
283 np
= add (VRSN_FIELD
, NULL
);
284 vp
= concat (" ", VRSN_VALUE
, "\n", NULL
);
285 add_header (ct
, np
, vp
);
289 * We initally assume we will find multiple contents in the
290 * draft. So create a multipart/mixed content to hold everything.
291 * We can remove this later, if it is not needed.
293 if (get_ctinfo ("multipart/mixed", ct
, 0) == NOTOK
)
295 ct
->c_type
= CT_MULTIPART
;
296 ct
->c_subtype
= MULTI_MIXED
;
298 if ((m
= (struct multipart
*) calloc (1, sizeof(*m
))) == NULL
)
299 adios (NULL
, "out of memory");
300 ct
->c_ctparams
= (void *) m
;
304 * read and parse the composition file
305 * and the directives it contains.
307 while (fgetstr (buf
, sizeof(buf
) - 1, in
)) {
311 if (user_content (in
, buf
, &p
, infile
) == DONE
) {
312 admonish (NULL
, "ignoring spurious #end");
318 if ((part
= (struct part
*) calloc (1, sizeof(*part
))) == NULL
)
319 adios (NULL
, "out of memory");
326 * Add any Attach headers to the list of MIME parts at the end of the
330 for (at_entry
= attach_head
; at_entry
; ) {
331 struct attach_list
*at_prev
= at_entry
;
335 if (access(at_entry
->filename
, R_OK
) != 0) {
336 adios("reading", "Unable to open %s for", at_entry
->filename
);
339 if ((p
= (CT
) calloc (1, sizeof(*p
))) == NULL
)
340 adios(NULL
, "out of memory");
342 init_decoded_content(p
, infile
);
345 * Initialize our content structure based on the filename,
346 * and fill in all of the relevant fields. Also place MIME
347 * parameters in the attributes array.
350 setup_attach_content(p
, at_entry
->filename
);
352 if ((part
= (struct part
*) calloc (1, sizeof(*part
))) == NULL
)
353 adios (NULL
, "out of memory");
358 at_entry
= at_entry
->next
;
359 free(at_prev
->filename
);
364 * To allow for empty message bodies, if we've found NO content at all
365 * yet cook up an empty text/plain part.
373 if ((p
= (CT
) calloc (1, sizeof(*p
))) == NULL
)
374 adios(NULL
, "out of memory");
376 init_decoded_content(p
, infile
);
378 if (get_ctinfo ("text/plain", p
, 0) == NOTOK
)
382 p
->c_subtype
= TEXT_PLAIN
;
383 p
->c_encoding
= CE_7BIT
;
385 * Sigh. ce_file contains the "decoded" contents of this part.
386 * So this seems like the best option available since we're going
387 * to call scan_content() on this.
389 p
->c_cefile
.ce_file
= getcpy("/dev/null");
390 p
->c_begin
= ftell(in
);
391 p
->c_end
= ftell(in
);
393 if ((t
= (struct text
*) calloc (1, sizeof (*t
))) == NULL
)
394 adios (NULL
, "out of memory");
396 t
->tx_charset
= CHARSET_SPECIFIED
;
399 if ((part
= (struct part
*) calloc (1, sizeof(*part
))) == NULL
)
400 adios (NULL
, "out of memory");
406 * close the composition draft since
407 * it's not needed any longer.
412 * If only one content was found, then remove and
413 * free the outer multipart content.
415 if (!m
->mp_parts
->mp_next
) {
418 p
= m
->mp_parts
->mp_part
;
419 m
->mp_parts
->mp_part
= NULL
;
421 /* move header fields */
422 p
->c_first_hf
= ct
->c_first_hf
;
423 p
->c_last_hf
= ct
->c_last_hf
;
424 ct
->c_first_hf
= NULL
;
425 ct
->c_last_hf
= NULL
;
434 * Fill out, or expand directives. Parse and execute
435 * commands specified by profile composition strings.
437 compose_content (ct
, verbose
);
439 if ((cp
= strchr(prefix
, 'a')) == NULL
)
440 adios (NULL
, "internal error(4)");
443 * Scan the contents. Choose a transfer encoding, and
444 * check if prefix for multipart boundary clashes with
445 * any of the contents.
447 while (scan_content (ct
, maxunencoded
) == NOTOK
) {
452 adios (NULL
, "giving up trying to find a unique delimiter string");
458 /* Build the rest of the header field structures */
460 build_headers (ct
, header_encoding
);
467 * Set up structures for placing unencoded
468 * content when building parts.
472 init_decoded_content (CT ct
, const char *filename
)
474 ct
->c_ceopenfnx
= open7Bit
; /* since unencoded */
475 ct
->c_ceclosefnx
= close_encoding
;
476 ct
->c_cesizefnx
= NULL
; /* since unencoded */
477 ct
->c_encoding
= CE_7BIT
; /* Seems like a reasonable default */
478 ct
->c_file
= add(filename
, NULL
);
485 fgetstr (char *s
, int n
, FILE *stream
)
491 for (ep
= (cp
= s
) + o_n
; cp
< ep
; ) {
494 if (!fgets (cp
, n
, stream
))
495 return (cp
!= s
? s
: NULL
);
497 if (cp
== s
&& *cp
!= '#')
500 cp
+= (i
= strlen (cp
)) - 1;
501 if (i
<= 1 || *cp
-- != '\n' || *cp
!= '\\')
507 if (strcmp(s
, "#on\n") == 0) {
509 } else if (strcmp(s
, "#off\n") == 0) {
511 } else if (strcmp(s
, "#pop\n") == 0) {
523 * Parse the composition draft for text and directives.
524 * Do initial setup of Content structure.
528 user_content (FILE *in
, char *buf
, CT
*ctp
, const char *infilename
)
536 struct str2init
*s2i
;
541 if (buf
[0] == '\n' || (do_direct() && strcmp (buf
, "#\n") == 0)) {
546 /* allocate basic Content structure */
547 if ((ct
= (CT
) calloc (1, sizeof(*ct
))) == NULL
)
548 adios (NULL
, "out of memory");
551 /* allocate basic structure for handling decoded content */
552 init_decoded_content (ct
, infilename
);
559 * Handle inline text. Check if line
560 * is one of the following forms:
562 * 1) doesn't begin with '#' (implicit directive)
563 * 2) begins with "##" (implicit directive)
564 * 3) begins with "#<"
566 if (!do_direct() || buf
[0] != '#' || buf
[1] == '#' || buf
[1] == '<') {
570 char content
[BUFSIZ
];
574 if ((cp
= m_mktemp2(NULL
, invo_name
, NULL
, &out
)) == NULL
) {
575 adios("mhbuildsbr", "unable to create temporary file in %s",
579 /* use a temp file to collect the plain text lines */
580 ce
->ce_file
= add (cp
, NULL
);
583 if (do_direct() && (buf
[0] == '#' && buf
[1] == '<')) {
584 strncpy (content
, buf
+ 2, sizeof(content
));
591 /* the directive is implicit */
592 strncpy (content
, "text/plain", sizeof(content
));
594 strncpy (buffer
, (!do_direct() || buf
[0] != '#') ? buf
: buf
+ 1, sizeof(buffer
));
598 if (headers
>= 0 && do_direct() && uprf (buffer
, DESCR_FIELD
)
599 && buffer
[i
= strlen (DESCR_FIELD
)] == ':') {
603 ct
->c_descr
= add (buffer
+ i
+ 1, ct
->c_descr
);
604 if (!fgetstr (buffer
, sizeof(buffer
) - 1, in
))
605 adios (NULL
, "end-of-file after %s: field in plaintext", DESCR_FIELD
);
613 adios (NULL
, "#-directive after %s: field in plaintext", DESCR_FIELD
);
621 if (headers
>= 0 && do_direct() && uprf (buffer
, DISPO_FIELD
)
622 && buffer
[i
= strlen (DISPO_FIELD
)] == ':') {
626 ct
->c_dispo
= add (buffer
+ i
+ 1, ct
->c_dispo
);
627 if (!fgetstr (buffer
, sizeof(buffer
) - 1, in
))
628 adios (NULL
, "end-of-file after %s: field in plaintext", DISPO_FIELD
);
636 adios (NULL
, "#-directive after %s: field in plaintext", DISPO_FIELD
);
644 if (headers
!= 1 || buffer
[0] != '\n')
650 if ((cp
= fgetstr (buffer
, sizeof(buffer
) - 1, in
)) == NULL
)
652 if (do_direct() && buffer
[0] == '#') {
655 if (buffer
[1] != '#')
657 for (cp
= (bp
= buffer
) + 1; *cp
; cp
++)
664 ct
->c_end
= ftell (out
);
667 /* parse content type */
668 if (get_ctinfo (content
, ct
, inlineD
) == NOTOK
)
671 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
672 if (!strcasecmp (ci
->ci_type
, s2i
->si_key
))
674 if (!s2i
->si_key
&& !uprf (ci
->ci_type
, "X-"))
678 * check type specified (possibly implicitly)
680 switch (ct
->c_type
= s2i
->si_val
) {
682 if (!strcasecmp (ci
->ci_subtype
, "rfc822")) {
683 ct
->c_encoding
= CE_7BIT
;
688 adios (NULL
, "it doesn't make sense to define an in-line %s content",
689 ct
->c_type
== CT_MESSAGE
? "message" : "multipart");
694 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
695 (*ct
->c_ctinitfnx
) (ct
);
700 fseek (in
, pos
, SEEK_SET
);
705 * If we've reached this point, the next line
706 * must be some type of explicit directive.
709 /* check if directive is external-type */
710 extrnal
= (buf
[1] == '@');
712 /* parse directive */
713 if (get_ctinfo (buf
+ (extrnal
? 2 : 1), ct
, 1) == NOTOK
)
716 /* check directive against the list of MIME types */
717 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
718 if (!strcasecmp (ci
->ci_type
, s2i
->si_key
))
722 * Check if the directive specified a valid type.
723 * This will happen if it was one of the following forms:
730 adios (NULL
, "missing subtype in \"#%s\"", ci
->ci_type
);
732 switch (ct
->c_type
= s2i
->si_val
) {
734 adios (NULL
, "use \"#begin ... #end\" instead of \"#%s/%s\"",
735 ci
->ci_type
, ci
->ci_subtype
);
739 if (!strcasecmp (ci
->ci_subtype
, "partial"))
740 adios (NULL
, "sorry, \"#%s/%s\" isn't supported",
741 ci
->ci_type
, ci
->ci_subtype
);
742 if (!strcasecmp (ci
->ci_subtype
, "external-body"))
743 adios (NULL
, "use \"#@type/subtype ... [] ...\" instead of \"#%s/%s\"",
744 ci
->ci_type
, ci
->ci_subtype
);
747 "use \"#forw [+folder] [msgs]\" instead of \"#%s/%s\"",
748 ci
->ci_type
, ci
->ci_subtype
);
752 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
753 (*ct
->c_ctinitfnx
) (ct
);
758 * #@type/subtype (external types directive)
765 adios (NULL
, "need external information for \"#@%s/%s\"",
766 ci
->ci_type
, ci
->ci_subtype
);
769 snprintf (buffer
, sizeof(buffer
), "message/external-body; %s", ci
->ci_magic
);
774 * Since we are using the current Content structure to
775 * hold information about the type of the external
776 * reference, we need to create another Content structure
777 * for the message/external-body to wrap it in.
779 if ((ct
= (CT
) calloc (1, sizeof(*ct
))) == NULL
)
780 adios (NULL
, "out of memory");
781 init_decoded_content(ct
, infilename
);
783 if (get_ctinfo (buffer
, ct
, 0) == NOTOK
)
785 ct
->c_type
= CT_MESSAGE
;
786 ct
->c_subtype
= MESSAGE_EXTERNAL
;
788 if ((e
= (struct exbody
*) calloc (1, sizeof(*e
))) == NULL
)
789 adios (NULL
, "out of memory");
790 ct
->c_ctparams
= (void *) e
;
796 if (params_external (ct
, 1) == NOTOK
)
802 /* Handle [file] argument */
804 /* check if specifies command to execute */
805 if (*ci
->ci_magic
== '|' || *ci
->ci_magic
== '!') {
806 for (cp
= ci
->ci_magic
+ 1; isspace ((unsigned char) *cp
); cp
++)
809 adios (NULL
, "empty pipe command for #%s directive", ci
->ci_type
);
814 /* record filename of decoded contents */
815 ce
->ce_file
= ci
->ci_magic
;
816 if (access (ce
->ce_file
, R_OK
) == NOTOK
)
817 adios ("reading", "unable to access %s for", ce
->ce_file
);
818 if (listsw
&& stat (ce
->ce_file
, &st
) != NOTOK
)
819 ct
->c_end
= (long) st
.st_size
;
826 * No [file] argument, so check profile for
827 * method to compose content.
829 cp
= context_find_by_type ("compose", ci
->ci_type
, ci
->ci_subtype
);
831 content_error (NULL
, ct
, "don't know how to compose content");
834 ci
->ci_magic
= add (cp
, NULL
);
839 adios (NULL
, "external definition not allowed for \"#%s\"", ci
->ci_type
);
843 * #forw [+folder] [msgs]
845 if (!strcasecmp (ci
->ci_type
, "forw")) {
847 char *folder
, *arguments
[MAXARGS
];
851 ap
= brkstring (ci
->ci_magic
, " ", "\n");
852 copyip (ap
, arguments
, MAXARGS
);
854 arguments
[0] = "cur";
859 /* search the arguments for a folder name */
860 for (ap
= arguments
; *ap
; ap
++) {
862 if (*cp
== '+' || *cp
== '@') {
864 adios (NULL
, "only one folder per #forw directive");
866 folder
= pluspath (cp
);
870 /* else, use the current folder */
872 folder
= add (getfolder (1), NULL
);
874 if (!(mp
= folder_read (folder
, 0)))
875 adios (NULL
, "unable to read folder %s", folder
);
876 for (ap
= arguments
; *ap
; ap
++) {
878 if (*cp
!= '+' && *cp
!= '@')
879 if (!m_convert (mp
, cp
))
886 * If there is more than one message to include, make this
887 * a content of type "multipart/digest" and insert each message
888 * as a subpart. If there is only one message, then make this
889 * a content of type "message/rfc822".
891 if (mp
->numsel
> 1) {
892 /* we are forwarding multiple messages */
893 if (get_ctinfo ("multipart/digest", ct
, 0) == NOTOK
)
895 ct
->c_type
= CT_MULTIPART
;
896 ct
->c_subtype
= MULTI_DIGEST
;
898 if ((m
= (struct multipart
*) calloc (1, sizeof(*m
))) == NULL
)
899 adios (NULL
, "out of memory");
900 ct
->c_ctparams
= (void *) m
;
903 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
904 if (is_selected(mp
, msgnum
)) {
909 if ((p
= (CT
) calloc (1, sizeof(*p
))) == NULL
)
910 adios (NULL
, "out of memory");
911 init_decoded_content (p
, infilename
);
913 if (get_ctinfo ("message/rfc822", p
, 0) == NOTOK
)
915 p
->c_type
= CT_MESSAGE
;
916 p
->c_subtype
= MESSAGE_RFC822
;
918 snprintf (buffer
, sizeof(buffer
), "%s/%d", mp
->foldpath
, msgnum
);
919 pe
->ce_file
= add (buffer
, NULL
);
920 if (listsw
&& stat (pe
->ce_file
, &st
) != NOTOK
)
921 p
->c_end
= (long) st
.st_size
;
923 if ((part
= (struct part
*) calloc (1, sizeof(*part
))) == NULL
)
924 adios (NULL
, "out of memory");
931 /* we are forwarding one message */
932 if (get_ctinfo ("message/rfc822", ct
, 0) == NOTOK
)
934 ct
->c_type
= CT_MESSAGE
;
935 ct
->c_subtype
= MESSAGE_RFC822
;
938 snprintf (buffer
, sizeof(buffer
), "%s/%d", mp
->foldpath
, msgnum
);
939 ce
->ce_file
= add (buffer
, NULL
);
940 if (listsw
&& stat (ce
->ce_file
, &st
) != NOTOK
)
941 ct
->c_end
= (long) st
.st_size
;
944 folder_free (mp
); /* free folder/message structure */
951 if (!strcasecmp (ci
->ci_type
, "end")) {
958 * #begin [ alternative | parallel ]
960 if (!strcasecmp (ci
->ci_type
, "begin")) {
963 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
964 } else if (!strcasecmp (ci
->ci_magic
, "alternative")) {
965 vrsn
= MULTI_ALTERNATE
;
966 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
967 } else if (!strcasecmp (ci
->ci_magic
, "parallel")) {
968 vrsn
= MULTI_PARALLEL
;
969 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
970 } else if (uprf (ci
->ci_magic
, "digest")) {
973 vrsn
= MULTI_UNKNOWN
;
978 snprintf (buffer
, sizeof(buffer
), "multipart/%s", cp
);
979 if (get_ctinfo (buffer
, ct
, 0) == NOTOK
)
981 ct
->c_type
= CT_MULTIPART
;
982 ct
->c_subtype
= vrsn
;
984 if ((m
= (struct multipart
*) calloc (1, sizeof(*m
))) == NULL
)
985 adios (NULL
, "out of memory");
986 ct
->c_ctparams
= (void *) m
;
989 while (fgetstr (buffer
, sizeof(buffer
) - 1, in
)) {
993 if (user_content (in
, buffer
, &p
, infilename
) == DONE
) {
995 adios (NULL
, "empty \"#begin ... #end\" sequence");
1001 if ((part
= (struct part
*) calloc (1, sizeof(*part
))) == NULL
)
1002 adios (NULL
, "out of memory");
1004 pp
= &part
->mp_next
;
1007 admonish (NULL
, "premature end-of-file, missing #end");
1014 adios (NULL
, "unknown directive \"#%s\"", ci
->ci_type
);
1015 return NOTOK
; /* NOT REACHED */
1020 set_id (CT ct
, int top
)
1022 char contentid
[BUFSIZ
];
1024 static time_t clock
= 0;
1025 static char *msgfmt
;
1029 snprintf (contentid
, sizeof(contentid
), "%s\n", message_id (clock
, 1));
1031 msgfmt
= getcpy(contentid
);
1033 snprintf (contentid
, sizeof(contentid
), msgfmt
, top
? 0 : ++partno
);
1034 ct
->c_id
= getcpy (contentid
);
1039 * Fill out, or expand the various contents in the composition
1040 * draft. Read-in any necessary files. Parse and execute any
1041 * commands specified by profile composition strings.
1045 compose_content (CT ct
, int verbose
)
1047 CE ce
= &ct
->c_cefile
;
1049 switch (ct
->c_type
) {
1054 char partnam
[BUFSIZ
];
1055 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1059 snprintf (partnam
, sizeof(partnam
), "%s.", ct
->c_partno
);
1060 pp
= partnam
+ strlen (partnam
);
1065 /* first, we call compose_content on all the subparts */
1066 for (part
= m
->mp_parts
, partnum
= 1; part
; part
= part
->mp_next
, partnum
++) {
1067 CT p
= part
->mp_part
;
1069 sprintf (pp
, "%d", partnum
);
1070 p
->c_partno
= add (partnam
, NULL
);
1071 if (compose_content (p
, verbose
) == NOTOK
)
1076 * If the -rfc934mode switch is given, then check all
1077 * the subparts of a multipart/digest. If they are all
1078 * message/rfc822, then mark this content and all
1079 * subparts with the rfc934 compatibility mode flag.
1081 if (rfc934sw
&& ct
->c_subtype
== MULTI_DIGEST
) {
1084 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1085 CT p
= part
->mp_part
;
1087 if (p
->c_subtype
!= MESSAGE_RFC822
) {
1092 ct
->c_rfc934
= is934
;
1093 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1094 CT p
= part
->mp_part
;
1096 if ((p
->c_rfc934
= is934
))
1102 ct
->c_end
= (partnum
= strlen (prefix
) + 2) + 2;
1106 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
)
1107 ct
->c_end
+= part
->mp_part
->c_end
+ partnum
;
1113 /* Nothing to do for type message */
1117 * Discrete types (text/application/audio/image/video)
1122 int i
, xstdout
, len
, buflen
;
1124 char *vec
[4], buffer
[BUFSIZ
];
1126 CI ci
= &ct
->c_ctinfo
;
1129 if (!(cp
= ci
->ci_magic
))
1130 adios (NULL
, "internal error(5)");
1132 if ((tfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1133 adios("mhbuildsbr", "unable to create temporary file in %s",
1136 ce
->ce_file
= add (tfile
, NULL
);
1141 /* Get buffer ready to go */
1144 buflen
= sizeof(buffer
);
1147 * Parse composition string into buffer
1149 for ( ; *cp
; cp
++) {
1154 /* insert parameters from directive */
1158 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
1159 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
,
1160 pm
->pm_name
, get_param_value(pm
, '?'));
1170 /* %f, and stdout is not-redirected */
1176 * insert temporary filename where
1177 * content should be written
1179 snprintf (bp
, buflen
, "%s", ce
->ce_file
);
1183 /* insert content subtype */
1184 strncpy (bp
, ci
->ci_subtype
, buflen
);
1188 /* insert character % */
1209 printf ("composing content %s/%s from command\n\t%s\n",
1210 ci
->ci_type
, ci
->ci_subtype
, buffer
);
1212 fflush (stdout
); /* not sure if need for -noverbose */
1219 if ((out
= fopen (ce
->ce_file
, "w")) == NULL
)
1220 adios (ce
->ce_file
, "unable to open for writing");
1222 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
> 5; i
++)
1226 adios ("fork", "unable to fork");
1231 dup2 (fileno (out
), 1);
1232 close (fileno (out
));
1233 execvp ("/bin/sh", vec
);
1234 fprintf (stderr
, "unable to exec ");
1241 if (pidXwait(child_id
, NULL
))
1247 /* Check size of file */
1248 if (listsw
&& ct
->c_end
== 0L) {
1251 if (stat (ce
->ce_file
, &st
) != NOTOK
)
1252 ct
->c_end
= (long) st
.st_size
;
1264 * 1) choose a transfer encoding.
1265 * 2) check for clashes with multipart boundary string.
1266 * 3) for text content, figure out which character set is being used.
1268 * If there is a clash with one of the contents and the multipart boundary,
1269 * this function will exit with NOTOK. This will cause the scanning process
1270 * to be repeated with a different multipart boundary. It is possible
1271 * (although highly unlikely) that this scan will be repeated multiple times.
1275 scan_content (CT ct
, size_t maxunencoded
)
1278 int check8bit
= 0, contains8bit
= 0; /* check if contains 8bit data */
1279 int checknul
= 0, containsnul
= 0; /* check if contains NULs */
1280 int checklinelen
= 0, linelen
= 0; /* check for long lines */
1281 int checkllinelen
= 0; /* check for extra-long lines */
1282 int checkboundary
= 0, boundaryclash
= 0; /* check if clashes with multipart boundary */
1283 int checklinespace
= 0, linespace
= 0; /* check if any line ends with space */
1288 struct text
*t
= NULL
;
1290 CE ce
= &ct
->c_cefile
;
1293 * handle multipart by scanning all subparts
1294 * and then checking their encoding.
1296 if (ct
->c_type
== CT_MULTIPART
) {
1297 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1300 /* initially mark the domain of enclosing multipart as 7bit */
1301 ct
->c_encoding
= CE_7BIT
;
1303 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1304 CT p
= part
->mp_part
;
1306 if (scan_content (p
, maxunencoded
) == NOTOK
) /* choose encoding for subpart */
1309 /* if necessary, enlarge encoding for enclosing multipart */
1310 if (p
->c_encoding
== CE_BINARY
)
1311 ct
->c_encoding
= CE_BINARY
;
1312 if (p
->c_encoding
== CE_8BIT
&& ct
->c_encoding
!= CE_BINARY
)
1313 ct
->c_encoding
= CE_8BIT
;
1320 * Decide what to check while scanning this content. Note that
1321 * for text content we always check for 8bit characters if the
1322 * charset is unspecified, because that controls whether or not the
1323 * character set is us-ascii or retrieved from the locale.
1326 if (ct
->c_type
== CT_TEXT
) {
1327 t
= (struct text
*) ct
->c_ctparams
;
1328 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
) {
1383 * Don't check anything for these types,
1384 * since we are forcing use of base64, unless
1385 * the content-type was specified by a mhbuild directive.
1396 * Scan the unencoded content
1398 if (check8bit
|| checklinelen
|| checklinespace
|| checkboundary
||
1399 checkllinelen
|| checknul
) {
1400 if ((in
= fopen (ce
->ce_file
, "r")) == NULL
)
1401 adios (ce
->ce_file
, "unable to open for reading");
1402 prefix_len
= strlen (prefix
);
1404 while ((gotlen
= getline(&bufp
, &buflen
, in
)) != -1) {
1406 * Check for 8bit and NUL data.
1408 for (cp
= bufp
; (check8bit
|| checknul
) &&
1409 cp
< bufp
+ gotlen
; cp
++) {
1410 if (!isascii ((unsigned char) *cp
)) {
1412 check8bit
= 0; /* no need to keep checking */
1416 checknul
= 0; /* no need to keep checking */
1421 * Check line length.
1423 if (checklinelen
&& ((size_t)gotlen
> maxunencoded
+ 1)) {
1425 checklinelen
= 0; /* no need to keep checking */
1429 * RFC 5322 specifies that a message cannot contain a line
1430 * greater than 998 characters (excluding the CRLF). If we
1431 * get one of those lines and linelen is NOT set, then abort.
1434 if (checkllinelen
&& !linelen
&&
1435 (gotlen
> MAXLONGLINE
+ 1)) {
1436 adios(NULL
, "Line in content exceeds maximum line limit (%d)",
1441 * Check if line ends with a space.
1443 if (checklinespace
&& (cp
= bufp
+ gotlen
- 2) > bufp
&&
1444 isspace ((unsigned char) *cp
)) {
1446 checklinespace
= 0; /* no need to keep checking */
1450 * Check if content contains a line that clashes
1451 * with our standard boundary for multipart messages.
1453 if (checkboundary
&& bufp
[0] == '-' && bufp
[1] == '-') {
1454 for (cp
= bufp
+ gotlen
- 1; cp
>= bufp
; cp
--)
1455 if (!isspace ((unsigned char) *cp
))
1458 if (!strncmp(bufp
+ 2, prefix
, prefix_len
) &&
1459 isdigit((unsigned char) bufp
[2 + prefix_len
])) {
1461 checkboundary
= 0; /* no need to keep checking */
1470 * If the content is text and didn't specify a character set,
1471 * we need to figure out which one was used.
1474 if (ct
->c_type
== CT_TEXT
) {
1475 t
= (struct text
*) ct
->c_ctparams
;
1476 if (t
->tx_charset
== CHARSET_UNSPECIFIED
) {
1477 CI ci
= &ct
->c_ctinfo
;
1478 char *eightbitcharset
= write_charset_8bit();
1480 if (contains8bit
&& strcasecmp(eightbitcharset
, "US-ASCII") == 0) {
1481 adios(NULL
, "Text content contains 8 bit characters, but "
1482 "character set is US-ASCII");
1485 add_param(&ci
->ci_first_pm
, &ci
->ci_last_pm
, "charset",
1486 contains8bit
? eightbitcharset
: "us-ascii", 0);
1488 t
->tx_charset
= CHARSET_SPECIFIED
;
1493 * Decide which transfer encoding to use.
1496 if (ct
->c_reqencoding
!= CE_UNKNOWN
)
1497 ct
->c_encoding
= ct
->c_reqencoding
;
1499 int wants_q_p
= (containsnul
|| linelen
|| linespace
|| checksw
);
1501 switch (ct
->c_type
) {
1504 ct
->c_encoding
= CE_QUOTED
;
1505 else if (contains8bit
)
1506 ct
->c_encoding
= CE_8BIT
;
1508 ct
->c_encoding
= CE_7BIT
;
1512 case CT_APPLICATION
:
1513 /* For application type, use base64, except when postscript */
1514 if (wants_q_p
|| contains8bit
) {
1515 if (ct
->c_subtype
== APPLICATION_POSTSCRIPT
)
1516 ct
->c_encoding
= CE_QUOTED
; /* historical */
1518 ct
->c_encoding
= CE_BASE64
;
1520 ct
->c_encoding
= CE_7BIT
;
1525 ct
->c_encoding
= contains8bit
? CE_8BIT
: CE_7BIT
;
1531 /* For audio, image, and video contents, just use base64 */
1532 ct
->c_encoding
= CE_BASE64
;
1537 return (boundaryclash
? NOTOK
: OK
);
1542 * Scan the content structures, and build header
1543 * fields that will need to be output into the
1548 build_headers (CT ct
, int header_encoding
)
1550 int cc
, mailbody
, extbody
, len
;
1551 char *np
, *vp
, buffer
[BUFSIZ
];
1552 CI ci
= &ct
->c_ctinfo
;
1555 * If message is type multipart, then add the multipart
1556 * boundary to the list of attribute/value pairs.
1558 if (ct
->c_type
== CT_MULTIPART
) {
1559 static int level
= 0; /* store nesting level */
1561 snprintf (buffer
, sizeof(buffer
), "%s%d", prefix
, level
++);
1562 add_param(&ci
->ci_first_pm
, &ci
->ci_last_pm
, "boundary", buffer
, 0);
1566 * Skip the output of Content-Type, parameters, content
1567 * description and disposition, and Content-ID if the
1568 * content is of type "message" and the rfc934 compatibility
1569 * flag is set (which means we are inside multipart/digest
1570 * and the switch -rfc934mode was given).
1572 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_rfc934
)
1576 * output the content type and subtype
1578 np
= add (TYPE_FIELD
, NULL
);
1579 vp
= concat (" ", ci
->ci_type
, "/", ci
->ci_subtype
, NULL
);
1581 /* keep track of length of line */
1582 len
= strlen (TYPE_FIELD
) + strlen (ci
->ci_type
)
1583 + strlen (ci
->ci_subtype
) + 3;
1585 extbody
= ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_EXTERNAL
;
1586 mailbody
= extbody
&& ((struct exbody
*) ct
->c_ctparams
)->eb_body
;
1589 * Append the attribute/value pairs to
1590 * the end of the Content-Type line.
1593 if (ci
->ci_first_pm
) {
1594 char *s
= output_params(len
, ci
->ci_first_pm
, &len
, mailbody
);
1597 adios(NULL
, "Internal error: failed outputting Content-Type "
1605 * Append any RFC-822 comment to the end of
1606 * the Content-Type line.
1608 if (ci
->ci_comment
) {
1609 snprintf (buffer
, sizeof(buffer
), "(%s)", ci
->ci_comment
);
1610 if (len
+ 1 + (cc
= 2 + strlen (ci
->ci_comment
)) >= CPERLIN
) {
1611 vp
= add ("\n\t", vp
);
1617 vp
= add (buffer
, vp
);
1620 vp
= add ("\n", vp
);
1621 add_header (ct
, np
, vp
);
1624 * output the Content-ID, unless disabled by -nocontentid
1626 if (contentidsw
&& ct
->c_id
) {
1627 np
= add (ID_FIELD
, NULL
);
1628 vp
= concat (" ", ct
->c_id
, NULL
);
1629 add_header (ct
, np
, vp
);
1632 * output the Content-Description
1635 np
= add (DESCR_FIELD
, NULL
);
1636 vp
= concat (" ", ct
->c_descr
, NULL
);
1637 if (encode_rfc2047(DESCR_FIELD
, &vp
, header_encoding
, NULL
))
1638 adios(NULL
, "Unable to encode %s header", DESCR_FIELD
);
1639 add_header (ct
, np
, vp
);
1643 * output the Content-Disposition. If it's NULL but c_dispo_type is
1644 * set, then we need to build it.
1647 np
= add (DISPO_FIELD
, NULL
);
1648 vp
= concat (" ", ct
->c_dispo
, NULL
);
1649 add_header (ct
, np
, vp
);
1650 } else if (ct
->c_dispo_type
) {
1651 vp
= concat (" ", ct
->c_dispo_type
, NULL
);
1652 len
= strlen(DISPO_FIELD
) + strlen(vp
) + 1;
1653 np
= output_params(len
, ct
->c_dispo_first
, NULL
, 0);
1658 add_header (ct
, getcpy(DISPO_FIELD
), vp
);
1663 * If this is the internal content structure for a
1664 * "message/external", then we are done with the
1665 * headers (since it has no body).
1671 * output the Content-MD5
1674 np
= add (MD5_FIELD
, NULL
);
1675 vp
= calculate_digest (ct
, (ct
->c_encoding
== CE_QUOTED
) ? 1 : 0);
1676 add_header (ct
, np
, vp
);
1680 * output the Content-Transfer-Encoding
1682 switch (ct
->c_encoding
) {
1684 /* Nothing to output */
1688 np
= add (ENCODING_FIELD
, NULL
);
1689 vp
= concat (" ", "8bit", "\n", NULL
);
1690 add_header (ct
, np
, vp
);
1694 if (ct
->c_type
== CT_MESSAGE
|| ct
->c_type
== CT_MULTIPART
)
1695 adios (NULL
, "internal error, invalid encoding");
1697 np
= add (ENCODING_FIELD
, NULL
);
1698 vp
= concat (" ", "quoted-printable", "\n", NULL
);
1699 add_header (ct
, np
, vp
);
1703 if (ct
->c_type
== CT_MESSAGE
|| ct
->c_type
== CT_MULTIPART
)
1704 adios (NULL
, "internal error, invalid encoding");
1706 np
= add (ENCODING_FIELD
, NULL
);
1707 vp
= concat (" ", "base64", "\n", NULL
);
1708 add_header (ct
, np
, vp
);
1712 if (ct
->c_type
== CT_MESSAGE
)
1713 adios (NULL
, "internal error, invalid encoding");
1715 np
= add (ENCODING_FIELD
, NULL
);
1716 vp
= concat (" ", "binary", "\n", NULL
);
1717 add_header (ct
, np
, vp
);
1721 adios (NULL
, "unknown transfer encoding in content");
1726 * Additional content specific header processing
1728 switch (ct
->c_type
) {
1731 struct multipart
*m
;
1734 m
= (struct multipart
*) ct
->c_ctparams
;
1735 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1739 build_headers (p
, header_encoding
);
1745 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1748 e
= (struct exbody
*) ct
->c_ctparams
;
1749 build_headers (e
->eb_content
, header_encoding
);
1762 static char nib2b64
[0x40+1] =
1763 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1766 calculate_digest (CT ct
, int asciiP
)
1771 unsigned char digest
[16];
1772 unsigned char outbuf
[25];
1774 CE ce
= &ct
->c_cefile
;
1775 char *infilename
= ce
->ce_file
? ce
->ce_file
: ct
->c_file
;
1779 if ((in
= fopen (infilename
, "r")) == NULL
)
1780 adios (infilename
, "unable to open for reading");
1782 /* Initialize md5 context */
1783 MD5Init (&mdContext
);
1785 /* calculate md5 message digest */
1790 while ((gotlen
= getline(&bufp
, &buflen
, in
)) != -1) {
1793 cp
= bufp
+ gotlen
- 1;
1794 if ((c
= *cp
) == '\n')
1797 MD5Update (&mdContext
, (unsigned char *) bufp
,
1798 (unsigned int) gotlen
);
1801 MD5Update (&mdContext
, (unsigned char *) "\r\n", 2);
1804 char buffer
[BUFSIZ
];
1805 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), in
)) > 0)
1806 MD5Update (&mdContext
, (unsigned char *) buffer
, (unsigned int) cc
);
1809 /* md5 finalization. Write digest and zero md5 context */
1810 MD5Final (digest
, &mdContext
);
1815 /* print debugging info */
1819 fprintf (stderr
, "MD5 digest=");
1820 for (ep
= (dp
= digest
) + sizeof(digest
) / sizeof(digest
[0]);
1822 fprintf (stderr
, "%02x", *dp
& 0xff);
1823 fprintf (stderr
, "\n");
1826 /* encode the digest using base64 */
1827 for (dp
= digest
, op
= (char *) outbuf
,
1828 cc
= sizeof(digest
) / sizeof(digest
[0]);
1829 cc
> 0; cc
-= 3, op
+= 4) {
1833 bits
= (*dp
++ & 0xff) << 16;
1835 bits
|= (*dp
++ & 0xff) << 8;
1837 bits
|= *dp
++ & 0xff;
1840 for (bp
= op
+ 4; bp
> op
; bits
>>= 6)
1841 *--bp
= nib2b64
[bits
& 0x3f];
1849 /* null terminate string */
1852 /* now make copy and return string */
1853 vp
= concat (" ", outbuf
, "\n", NULL
);
1858 * Set things up for the content structure for file "filename" that
1863 setup_attach_content(CT ct
, char *filename
)
1865 char *type
, *simplename
= r1bindex(filename
, '/');
1866 struct str2init
*s2i
;
1870 if (! (type
= mime_type(filename
))) {
1871 adios(NULL
, "Unable to determine MIME type of \"%s\"", filename
);
1875 * Parse the Content-Type. get_ctinfo() parses MIME parameters, but
1876 * since we're just feeding it a MIME type we have to add those ourself.
1877 * Map that to a valid content-type label and call any initialization
1881 if (get_ctinfo(type
, ct
, 0) == NOTOK
)
1886 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
1887 if (strcasecmp(ct
->c_ctinfo
.ci_type
, s2i
->si_key
) == 0)
1889 if (!s2i
->si_key
&& !uprf(ct
->c_ctinfo
.ci_type
, "X-"))
1893 * Make sure the type isn't incompatible with what we can handle
1896 switch (ct
->c_type
= s2i
->si_val
) {
1898 adios (NULL
, "multipart types must be specified by mhbuild directives");
1902 if (strcasecmp(ct
->c_ctinfo
.ci_subtype
, "partial") == 0)
1903 adios(NULL
, "Sorry, %s/%s isn't supported", ct
->c_ctinfo
.ci_type
,
1904 ct
->c_ctinfo
.ci_subtype
);
1905 if (strcasecmp(ct
->c_ctinfo
.ci_subtype
, "external-body") == 0)
1906 adios(NULL
, "external-body messages must be specified "
1907 "by mhbuild directives");
1912 * This sets the subtype, if it's significant
1914 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
1915 (*ct
->c_ctinitfnx
)(ct
);
1920 * Feed in a few attributes; specifically, the name attribute, the
1921 * content-description, and the content-disposition.
1924 for (pm
= ct
->c_ctinfo
.ci_first_pm
; pm
; pm
= pm
->pm_next
) {
1925 if (strcasecmp(pm
->pm_name
, "name") == 0) {
1928 pm
->pm_value
= getcpy(simplename
);
1934 add_param(&ct
->c_ctinfo
.ci_first_pm
, &ct
->c_ctinfo
.ci_last_pm
,
1935 "name", simplename
, 0);
1937 ct
->c_descr
= getcpy(simplename
);
1938 ct
->c_descr
= add("\n", ct
->c_descr
);
1939 ct
->c_cefile
.ce_file
= getcpy(filename
);
1942 * Look for mhbuild-disposition-<type>/<subtype> entry
1943 * that specifies Content-Disposition type. Only
1944 * 'attachment' and 'inline' are allowed. Default to
1948 cp
= context_find_by_type ("disposition", ct
->c_ctinfo
.ci_type
,
1949 ct
->c_ctinfo
.ci_subtype
);
1951 if (strcasecmp (cp
, "attachment") && strcasecmp (cp
, "inline")) {
1952 admonish (NULL
, "configuration problem: %s-disposition-%s%s%s "
1953 "specifies '%s' but only 'attachment' and 'inline' are "
1954 "allowed", invo_name
,
1955 ct
->c_ctinfo
.ci_type
,
1956 ct
->c_ctinfo
.ci_subtype
? "/" : "",
1957 ct
->c_ctinfo
.ci_subtype
? ct
->c_ctinfo
.ci_subtype
: "",
1963 ct
->c_dispo_type
= getcpy(cp
);
1965 ct
->c_dispo_type
= getcpy("attachment");
1968 add_param(&ct
->c_dispo_first
, &ct
->c_dispo_last
, "filename", simplename
, 0);