]>
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>
27 #include <h/mhparse.h>
30 #ifdef HAVE_SYS_TIME_H
31 # include <sys/time.h>
41 extern int contentidsw
;
44 extern int rcachesw
; /* mhcachesbr.c */
45 extern int wcachesw
; /* mhcachesbr.c */
48 * Directory to place tmp files. This must
49 * be set before these routines are called.
55 static char prefix
[] = "----- =_aaaaaaaaaa";
59 void content_error (char *, CT
, char *, ...);
62 int find_cache (CT
, int, int *, char *, char *, int);
65 void free_content (CT
);
66 void free_ctinfo (CT
);
67 void free_encoding (CT
, int);
72 CT
build_mime (char *, int);
77 static int init_decoded_content (CT
);
78 static char *fgetstr (char *, int, FILE *);
79 static int user_content (FILE *, char *, char *, CT
*);
80 static void set_id (CT
, int);
81 static int compose_content (CT
);
82 static int scan_content (CT
);
83 static int build_headers (CT
);
84 static char *calculate_digest (CT
, int);
87 static unsigned char directives_stack
[32];
88 static unsigned int directives_index
;
90 static int do_direct(void)
92 return directives_stack
[directives_index
];
95 static void directive_onoff(int onoff
)
97 if (directives_index
>= sizeof(directives_stack
) - 1) {
98 fprintf(stderr
, "mhbuild: #on/off overflow, continuing\n");
101 directives_stack
[++directives_index
] = onoff
;
104 static void directive_init(int onoff
)
106 directives_index
= 0;
107 directives_stack
[0] = onoff
;
110 static void directive_pop(void)
112 if (directives_index
> 0)
115 fprintf(stderr
, "mhbuild: #pop underflow, continuing\n");
119 * Main routine for translating composition file
120 * into valid MIME message. It translates the draft
121 * into a content structure (actually a tree of content
122 * structures). This message then can be manipulated
123 * in various ways, including being output via
128 build_mime (char *infile
, int directives
)
131 char buf
[BUFSIZ
], name
[NAMESZ
];
138 directive_init(directives
);
140 umask (~m_gmprot ());
142 /* open the composition draft */
143 if ((in
= fopen (infile
, "r")) == NULL
)
144 adios (infile
, "unable to open for reading");
147 * Allocate space for primary (outside) content
149 if ((ct
= (CT
) calloc (1, sizeof(*ct
))) == NULL
)
150 adios (NULL
, "out of memory");
153 * Allocate structure for handling decoded content
154 * for this part. We don't really need this, but
155 * allocate it to remain consistent.
157 init_decoded_content (ct
);
160 * Parse some of the header fields in the composition
161 * draft into the linked list of header fields for
162 * the new MIME message.
164 for (compnum
= 1, state
= FLD
;;) {
165 switch (state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
)) {
171 /* abort if draft has Mime-Version header field */
172 if (!mh_strcasecmp (name
, VRSN_FIELD
))
173 adios (NULL
, "draft shouldn't contain %s: field", VRSN_FIELD
);
175 /* abort if draft has Content-Transfer-Encoding header field */
176 if (!mh_strcasecmp (name
, ENCODING_FIELD
))
177 adios (NULL
, "draft shouldn't contain %s: field", ENCODING_FIELD
);
179 /* ignore any Content-Type fields in the header */
180 if (!mh_strcasecmp (name
, TYPE_FIELD
)) {
181 while (state
== FLDPLUS
)
182 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
186 /* get copies of the buffers */
187 np
= add (name
, NULL
);
188 vp
= add (buf
, NULL
);
190 /* if necessary, get rest of field */
191 while (state
== FLDPLUS
) {
192 state
= m_getfld (state
, name
, buf
, sizeof(buf
), in
);
193 vp
= add (buf
, vp
); /* add to previous value */
196 /* Now add the header data to the list */
197 add_header (ct
, np
, vp
);
200 /* if this wasn't the last header field, then continue */
206 adios (NULL
, "draft has empty body -- no directives!");
211 fseek (in
, (long) (-strlen (buf
)), SEEK_CUR
);
216 adios (NULL
, "message format error in component #%d", compnum
);
219 adios (NULL
, "getfld() returned %d", state
);
225 * Now add the MIME-Version header field
226 * to the list of header fields.
228 np
= add (VRSN_FIELD
, NULL
);
229 vp
= concat (" ", VRSN_VALUE
, "\n", NULL
);
230 add_header (ct
, np
, vp
);
233 * We initally assume we will find multiple contents in the
234 * draft. So create a multipart/mixed content to hold everything.
235 * We can remove this later, if it is not needed.
237 if (get_ctinfo ("multipart/mixed", ct
, 0) == NOTOK
)
239 ct
->c_type
= CT_MULTIPART
;
240 ct
->c_subtype
= MULTI_MIXED
;
241 ct
->c_file
= add (infile
, NULL
);
243 if ((m
= (struct multipart
*) calloc (1, sizeof(*m
))) == NULL
)
244 adios (NULL
, "out of memory");
245 ct
->c_ctparams
= (void *) m
;
249 * read and parse the composition file
250 * and the directives it contains.
252 while (fgetstr (buf
, sizeof(buf
) - 1, in
)) {
256 if (user_content (in
, infile
, buf
, &p
) == DONE
) {
257 admonish (NULL
, "ignoring spurious #end");
263 if ((part
= (struct part
*) calloc (1, sizeof(*part
))) == NULL
)
264 adios (NULL
, "out of memory");
271 * close the composition draft since
272 * it's not needed any longer.
276 /* check if any contents were found */
278 adios (NULL
, "no content directives found");
281 * If only one content was found, then remove and
282 * free the outer multipart content.
284 if (!m
->mp_parts
->mp_next
) {
287 p
= m
->mp_parts
->mp_part
;
288 m
->mp_parts
->mp_part
= NULL
;
290 /* move header fields */
291 p
->c_first_hf
= ct
->c_first_hf
;
292 p
->c_last_hf
= ct
->c_last_hf
;
293 ct
->c_first_hf
= NULL
;
294 ct
->c_last_hf
= NULL
;
303 * Fill out, or expand directives. Parse and execute
304 * commands specified by profile composition strings.
306 compose_content (ct
);
308 if ((cp
= strchr(prefix
, 'a')) == NULL
)
309 adios (NULL
, "internal error(4)");
312 * Scan the contents. Choose a transfer encoding, and
313 * check if prefix for multipart boundary clashes with
314 * any of the contents.
316 while (scan_content (ct
) == NOTOK
) {
321 adios (NULL
, "giving up trying to find a unique delimiter string");
327 /* Build the rest of the header field structures */
335 * Set up structures for placing unencoded
336 * content when building parts.
340 init_decoded_content (CT ct
)
344 if ((ce
= (CE
) calloc (1, sizeof(*ce
))) == NULL
)
345 adios (NULL
, "out of memory");
348 ct
->c_ceopenfnx
= open7Bit
; /* since unencoded */
349 ct
->c_ceclosefnx
= close_encoding
;
350 ct
->c_cesizefnx
= NULL
; /* since unencoded */
357 fgetstr (char *s
, int n
, FILE *stream
)
363 for (ep
= (cp
= s
) + o_n
; cp
< ep
; ) {
366 if (!fgets (cp
, n
, stream
))
367 return (cp
!= s
? s
: NULL
);
369 if (cp
== s
&& *cp
!= '#')
372 cp
+= (i
= strlen (cp
)) - 1;
373 if (i
<= 1 || *cp
-- != '\n' || *cp
!= '\\')
379 if (strcmp(s
, "#on\n") == 0) {
381 } else if (strcmp(s
, "#off\n") == 0) {
383 } else if (strcmp(s
, "#pop\n") == 0) {
395 * Parse the composition draft for text and directives.
396 * Do initial setup of Content structure.
400 user_content (FILE *in
, char *file
, char *buf
, CT
*ctp
)
409 struct str2init
*s2i
;
414 if (buf
[0] == '\n' || (do_direct() && strcmp (buf
, "#\n") == 0)) {
419 /* allocate basic Content structure */
420 if ((ct
= (CT
) calloc (1, sizeof(*ct
))) == NULL
)
421 adios (NULL
, "out of memory");
424 /* allocate basic structure for handling decoded content */
425 init_decoded_content (ct
);
432 * Handle inline text. Check if line
433 * is one of the following forms:
435 * 1) doesn't begin with '#' (implicit directive)
436 * 2) begins with "##" (implicit directive)
437 * 3) begins with "#<"
439 if (!do_direct() || buf
[0] != '#' || buf
[1] == '#' || buf
[1] == '<') {
443 char content
[BUFSIZ
];
447 cp
= m_mktemp2(NULL
, invo_name
, NULL
, &out
);
448 if (cp
== NULL
) adios("mhbuildsbr", "unable to create temporary file");
450 /* use a temp file to collect the plain text lines */
451 ce
->ce_file
= add (cp
, NULL
);
454 if (do_direct() && (buf
[0] == '#' && buf
[1] == '<')) {
455 strncpy (content
, buf
+ 2, sizeof(content
));
462 /* the directive is implicit */
463 strncpy (content
, "text/plain", sizeof(content
));
465 strncpy (buffer
, (!do_direct() || buf
[0] != '#') ? buf
: buf
+ 1, sizeof(buffer
));
469 if (headers
>= 0 && do_direct() && uprf (buffer
, DESCR_FIELD
)
470 && buffer
[i
= strlen (DESCR_FIELD
)] == ':') {
474 ct
->c_descr
= add (buffer
+ i
+ 1, ct
->c_descr
);
475 if (!fgetstr (buffer
, sizeof(buffer
) - 1, in
))
476 adios (NULL
, "end-of-file after %s: field in plaintext", DESCR_FIELD
);
484 adios (NULL
, "#-directive after %s: field in plaintext", DESCR_FIELD
);
492 if (headers
>= 0 && do_direct() && uprf (buffer
, DISPO_FIELD
)
493 && buffer
[i
= strlen (DISPO_FIELD
)] == ':') {
497 ct
->c_dispo
= add (buffer
+ i
+ 1, ct
->c_dispo
);
498 if (!fgetstr (buffer
, sizeof(buffer
) - 1, in
))
499 adios (NULL
, "end-of-file after %s: field in plaintext", DISPO_FIELD
);
507 adios (NULL
, "#-directive after %s: field in plaintext", DISPO_FIELD
);
515 if (headers
!= 1 || buffer
[0] != '\n')
521 if ((cp
= fgetstr (buffer
, sizeof(buffer
) - 1, in
)) == NULL
)
523 if (do_direct() && buffer
[0] == '#') {
526 if (buffer
[1] != '#')
528 for (cp
= (bp
= buffer
) + 1; *cp
; cp
++)
535 ct
->c_end
= ftell (out
);
538 /* parse content type */
539 if (get_ctinfo (content
, ct
, inlineD
) == NOTOK
)
542 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
543 if (!mh_strcasecmp (ci
->ci_type
, s2i
->si_key
))
545 if (!s2i
->si_key
&& !uprf (ci
->ci_type
, "X-"))
549 * check type specified (possibly implicitly)
551 switch (ct
->c_type
= s2i
->si_val
) {
553 if (!mh_strcasecmp (ci
->ci_subtype
, "rfc822")) {
554 ct
->c_encoding
= CE_7BIT
;
559 adios (NULL
, "it doesn't make sense to define an in-line %s content",
560 ct
->c_type
== CT_MESSAGE
? "message" : "multipart");
565 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
566 (*ct
->c_ctinitfnx
) (ct
);
571 fseek (in
, pos
, SEEK_SET
);
576 * If we've reached this point, the next line
577 * must be some type of explicit directive.
580 /* check if directive is external-type */
581 extrnal
= (buf
[1] == '@');
583 /* parse directive */
584 if (get_ctinfo (buf
+ (extrnal
? 2 : 1), ct
, 1) == NOTOK
)
587 /* check directive against the list of MIME types */
588 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
589 if (!mh_strcasecmp (ci
->ci_type
, s2i
->si_key
))
593 * Check if the directive specified a valid type.
594 * This will happen if it was one of the following forms:
601 adios (NULL
, "missing subtype in \"#%s\"", ci
->ci_type
);
603 switch (ct
->c_type
= s2i
->si_val
) {
605 adios (NULL
, "use \"#begin ... #end\" instead of \"#%s/%s\"",
606 ci
->ci_type
, ci
->ci_subtype
);
610 if (!mh_strcasecmp (ci
->ci_subtype
, "partial"))
611 adios (NULL
, "sorry, \"#%s/%s\" isn't supported",
612 ci
->ci_type
, ci
->ci_subtype
);
613 if (!mh_strcasecmp (ci
->ci_subtype
, "external-body"))
614 adios (NULL
, "use \"#@type/subtype ... [] ...\" instead of \"#%s/%s\"",
615 ci
->ci_type
, ci
->ci_subtype
);
618 "use \"#forw [+folder] [msgs]\" instead of \"#%s/%s\"",
619 ci
->ci_type
, ci
->ci_subtype
);
623 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
624 (*ct
->c_ctinitfnx
) (ct
);
629 * #@type/subtype (external types directive)
636 adios (NULL
, "need external information for \"#@%s/%s\"",
637 ci
->ci_type
, ci
->ci_subtype
);
640 snprintf (buffer
, sizeof(buffer
), "message/external-body; %s", ci
->ci_magic
);
645 * Since we are using the current Content structure to
646 * hold information about the type of the external
647 * reference, we need to create another Content structure
648 * for the message/external-body to wrap it in.
650 if ((ct
= (CT
) calloc (1, sizeof(*ct
))) == NULL
)
651 adios (NULL
, "out of memory");
654 if (get_ctinfo (buffer
, ct
, 0) == NOTOK
)
656 ct
->c_type
= CT_MESSAGE
;
657 ct
->c_subtype
= MESSAGE_EXTERNAL
;
659 if ((e
= (struct exbody
*) calloc (1, sizeof(*e
))) == NULL
)
660 adios (NULL
, "out of memory");
661 ct
->c_ctparams
= (void *) e
;
667 if (params_external (ct
, 1) == NOTOK
)
673 /* Handle [file] argument */
675 /* check if specifies command to execute */
676 if (*ci
->ci_magic
== '|' || *ci
->ci_magic
== '!') {
677 for (cp
= ci
->ci_magic
+ 1; isspace (*cp
); cp
++)
680 adios (NULL
, "empty pipe command for #%s directive", ci
->ci_type
);
685 /* record filename of decoded contents */
686 ce
->ce_file
= ci
->ci_magic
;
687 if (access (ce
->ce_file
, R_OK
) == NOTOK
)
688 adios ("reading", "unable to access %s for", ce
->ce_file
);
689 if (listsw
&& stat (ce
->ce_file
, &st
) != NOTOK
)
690 ct
->c_end
= (long) st
.st_size
;
697 * No [file] argument, so check profile for
698 * method to compose content.
700 snprintf (buffer
, sizeof(buffer
), "%s-compose-%s/%s",
701 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
702 if ((cp
= context_find (buffer
)) == NULL
|| *cp
== '\0') {
703 snprintf (buffer
, sizeof(buffer
), "%s-compose-%s", invo_name
, ci
->ci_type
);
704 if ((cp
= context_find (buffer
)) == NULL
|| *cp
== '\0') {
705 content_error (NULL
, ct
, "don't know how to compose content");
709 ci
->ci_magic
= add (cp
, NULL
);
714 adios (NULL
, "external definition not allowed for \"#%s\"", ci
->ci_type
);
718 * #forw [+folder] [msgs]
720 if (!mh_strcasecmp (ci
->ci_type
, "forw")) {
722 char *folder
, *arguments
[MAXARGS
];
726 ap
= brkstring (ci
->ci_magic
, " ", "\n");
727 copyip (ap
, arguments
, MAXARGS
);
729 arguments
[0] = "cur";
734 /* search the arguments for a folder name */
735 for (ap
= arguments
; *ap
; ap
++) {
737 if (*cp
== '+' || *cp
== '@') {
739 adios (NULL
, "only one folder per #forw directive");
741 folder
= pluspath (cp
);
745 /* else, use the current folder */
747 folder
= add (getfolder (1), NULL
);
749 if (!(mp
= folder_read (folder
)))
750 adios (NULL
, "unable to read folder %s", folder
);
751 for (ap
= arguments
; *ap
; ap
++) {
753 if (*cp
!= '+' && *cp
!= '@')
754 if (!m_convert (mp
, cp
))
761 * If there is more than one message to include, make this
762 * a content of type "multipart/digest" and insert each message
763 * as a subpart. If there is only one message, then make this
764 * a content of type "message/rfc822".
766 if (mp
->numsel
> 1) {
767 /* we are forwarding multiple messages */
768 if (get_ctinfo ("multipart/digest", ct
, 0) == NOTOK
)
770 ct
->c_type
= CT_MULTIPART
;
771 ct
->c_subtype
= MULTI_DIGEST
;
773 if ((m
= (struct multipart
*) calloc (1, sizeof(*m
))) == NULL
)
774 adios (NULL
, "out of memory");
775 ct
->c_ctparams
= (void *) m
;
778 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
779 if (is_selected(mp
, msgnum
)) {
784 if ((p
= (CT
) calloc (1, sizeof(*p
))) == NULL
)
785 adios (NULL
, "out of memory");
786 init_decoded_content (p
);
788 if (get_ctinfo ("message/rfc822", p
, 0) == NOTOK
)
790 p
->c_type
= CT_MESSAGE
;
791 p
->c_subtype
= MESSAGE_RFC822
;
793 snprintf (buffer
, sizeof(buffer
), "%s/%d", mp
->foldpath
, msgnum
);
794 pe
->ce_file
= add (buffer
, NULL
);
795 if (listsw
&& stat (pe
->ce_file
, &st
) != NOTOK
)
796 p
->c_end
= (long) st
.st_size
;
798 if ((part
= (struct part
*) calloc (1, sizeof(*part
))) == NULL
)
799 adios (NULL
, "out of memory");
806 /* we are forwarding one message */
807 if (get_ctinfo ("message/rfc822", ct
, 0) == NOTOK
)
809 ct
->c_type
= CT_MESSAGE
;
810 ct
->c_subtype
= MESSAGE_RFC822
;
813 snprintf (buffer
, sizeof(buffer
), "%s/%d", mp
->foldpath
, msgnum
);
814 ce
->ce_file
= add (buffer
, NULL
);
815 if (listsw
&& stat (ce
->ce_file
, &st
) != NOTOK
)
816 ct
->c_end
= (long) st
.st_size
;
819 folder_free (mp
); /* free folder/message structure */
826 if (!mh_strcasecmp (ci
->ci_type
, "end")) {
833 * #begin [ alternative | parallel ]
835 if (!mh_strcasecmp (ci
->ci_type
, "begin")) {
838 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
839 } else if (!mh_strcasecmp (ci
->ci_magic
, "alternative")) {
840 vrsn
= MULTI_ALTERNATE
;
841 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
842 } else if (!mh_strcasecmp (ci
->ci_magic
, "parallel")) {
843 vrsn
= MULTI_PARALLEL
;
844 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
845 } else if (uprf (ci
->ci_magic
, "digest")) {
848 vrsn
= MULTI_UNKNOWN
;
853 snprintf (buffer
, sizeof(buffer
), "multipart/%s", cp
);
854 if (get_ctinfo (buffer
, ct
, 0) == NOTOK
)
856 ct
->c_type
= CT_MULTIPART
;
857 ct
->c_subtype
= vrsn
;
859 if ((m
= (struct multipart
*) calloc (1, sizeof(*m
))) == NULL
)
860 adios (NULL
, "out of memory");
861 ct
->c_ctparams
= (void *) m
;
864 while (fgetstr (buffer
, sizeof(buffer
) - 1, in
)) {
868 if (user_content (in
, file
, buffer
, &p
) == DONE
) {
870 adios (NULL
, "empty \"#begin ... #end\" sequence");
876 if ((part
= (struct part
*) calloc (1, sizeof(*part
))) == NULL
)
877 adios (NULL
, "out of memory");
882 admonish (NULL
, "premature end-of-file, missing #end");
889 adios (NULL
, "unknown directive \"#%s\"", ci
->ci_type
);
890 return NOTOK
; /* NOT REACHED */
895 set_id (CT ct
, int top
)
897 char contentid
[BUFSIZ
];
899 static time_t clock
= 0;
904 snprintf (contentid
, sizeof(contentid
), "%s\n", message_id (clock
, 1));
906 msgfmt
= getcpy(contentid
);
908 snprintf (contentid
, sizeof(contentid
), msgfmt
, top
? 0 : ++partno
);
909 ct
->c_id
= getcpy (contentid
);
914 * Fill out, or expand the various contents in the composition
915 * draft. Read-in any necessary files. Parse and execute any
916 * commands specified by profile composition strings.
920 compose_content (CT ct
)
922 CE ce
= ct
->c_cefile
;
924 switch (ct
->c_type
) {
929 char partnam
[BUFSIZ
];
930 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
934 snprintf (partnam
, sizeof(partnam
), "%s.", ct
->c_partno
);
935 pp
= partnam
+ strlen (partnam
);
940 /* first, we call compose_content on all the subparts */
941 for (part
= m
->mp_parts
, partnum
= 1; part
; part
= part
->mp_next
, partnum
++) {
942 CT p
= part
->mp_part
;
944 sprintf (pp
, "%d", partnum
);
945 p
->c_partno
= add (partnam
, NULL
);
946 if (compose_content (p
) == NOTOK
)
951 * If the -rfc934mode switch is given, then check all
952 * the subparts of a multipart/digest. If they are all
953 * message/rfc822, then mark this content and all
954 * subparts with the rfc934 compatibility mode flag.
956 if (rfc934sw
&& ct
->c_subtype
== MULTI_DIGEST
) {
959 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
960 CT p
= part
->mp_part
;
962 if (p
->c_subtype
!= MESSAGE_RFC822
) {
967 ct
->c_rfc934
= is934
;
968 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
969 CT p
= part
->mp_part
;
971 if ((p
->c_rfc934
= is934
))
977 ct
->c_end
= (partnum
= strlen (prefix
) + 2) + 2;
981 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
)
982 ct
->c_end
+= part
->mp_part
->c_end
+ partnum
;
988 /* Nothing to do for type message */
992 * Discrete types (text/application/audio/image/video)
997 int i
, xstdout
, len
, buflen
;
999 char *vec
[4], buffer
[BUFSIZ
];
1001 CI ci
= &ct
->c_ctinfo
;
1004 if (!(cp
= ci
->ci_magic
))
1005 adios (NULL
, "internal error(5)");
1007 tfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
);
1008 if (tfile
== NULL
) {
1009 adios("mhbuildsbr", "unable to create temporary file");
1011 ce
->ce_file
= add (tfile
, NULL
);
1016 /* Get buffer ready to go */
1019 buflen
= sizeof(buffer
);
1022 * Parse composition string into buffer
1024 for ( ; *cp
; cp
++) {
1029 /* insert parameters from directive */
1033 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
1034 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, *ap
, *ep
);
1044 /* %f, and stdout is not-redirected */
1050 * insert temporary filename where
1051 * content should be written
1053 snprintf (bp
, buflen
, "%s", ce
->ce_file
);
1057 /* insert content subtype */
1058 strncpy (bp
, ci
->ci_subtype
, buflen
);
1062 /* insert character % */
1083 printf ("composing content %s/%s from command\n\t%s\n",
1084 ci
->ci_type
, ci
->ci_subtype
, buffer
);
1086 fflush (stdout
); /* not sure if need for -noverbose */
1093 if ((out
= fopen (ce
->ce_file
, "w")) == NULL
)
1094 adios (ce
->ce_file
, "unable to open for writing");
1096 for (i
= 0; (child_id
= vfork()) == NOTOK
&& i
> 5; i
++)
1100 adios ("fork", "unable to fork");
1105 dup2 (fileno (out
), 1);
1106 close (fileno (out
));
1107 execvp ("/bin/sh", vec
);
1108 fprintf (stderr
, "unable to exec ");
1115 if (pidXwait(child_id
, NULL
))
1121 /* Check size of file */
1122 if (listsw
&& ct
->c_end
== 0L) {
1125 if (stat (ce
->ce_file
, &st
) != NOTOK
)
1126 ct
->c_end
= (long) st
.st_size
;
1138 * 1) choose a transfer encoding.
1139 * 2) check for clashes with multipart boundary string.
1140 * 3) for text content, figure out which character set is being used.
1142 * If there is a clash with one of the contents and the multipart boundary,
1143 * this function will exit with NOTOK. This will cause the scanning process
1144 * to be repeated with a different multipart boundary. It is possible
1145 * (although highly unlikely) that this scan will be repeated multiple times.
1149 scan_content (CT ct
)
1152 int check8bit
= 0, contains8bit
= 0; /* check if contains 8bit data */
1153 int checklinelen
= 0, linelen
= 0; /* check for long lines */
1154 int checkboundary
= 0, boundaryclash
= 0; /* check if clashes with multipart boundary */
1155 int checklinespace
= 0, linespace
= 0; /* check if any line ends with space */
1156 unsigned char *cp
= NULL
, buffer
[BUFSIZ
];
1157 struct text
*t
= NULL
;
1159 CE ce
= ct
->c_cefile
;
1162 * handle multipart by scanning all subparts
1163 * and then checking their encoding.
1165 if (ct
->c_type
== CT_MULTIPART
) {
1166 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1169 /* initially mark the domain of enclosing multipart as 7bit */
1170 ct
->c_encoding
= CE_7BIT
;
1172 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1173 CT p
= part
->mp_part
;
1175 if (scan_content (p
) == NOTOK
) /* choose encoding for subpart */
1178 /* if necessary, enlarge encoding for enclosing multipart */
1179 if (p
->c_encoding
== CE_BINARY
)
1180 ct
->c_encoding
= CE_BINARY
;
1181 if (p
->c_encoding
== CE_8BIT
&& ct
->c_encoding
!= CE_BINARY
)
1182 ct
->c_encoding
= CE_8BIT
;
1189 * Decide what to check while scanning this content.
1191 switch (ct
->c_type
) {
1195 if (ct
->c_subtype
== TEXT_PLAIN
) {
1204 case CT_APPLICATION
:
1216 /* don't check anything for message/external */
1217 if (ct
->c_subtype
== MESSAGE_EXTERNAL
)
1227 * Don't check anything for these types,
1228 * since we are forcing use of base64.
1238 * Scan the unencoded content
1240 if (check8bit
|| checklinelen
|| checklinespace
|| checkboundary
) {
1241 if ((in
= fopen (ce
->ce_file
, "r")) == NULL
)
1242 adios (ce
->ce_file
, "unable to open for reading");
1243 len
= strlen (prefix
);
1245 while (fgets (buffer
, sizeof(buffer
) - 1, in
)) {
1247 * Check for 8bit data.
1250 for (cp
= buffer
; *cp
; cp
++) {
1251 if (!isascii (*cp
)) {
1253 check8bit
= 0; /* no need to keep checking */
1259 * Check line length.
1261 if (checklinelen
&& (strlen (buffer
) > CPERLIN
+ 1)) {
1263 checklinelen
= 0; /* no need to keep checking */
1267 * Check if line ends with a space.
1269 if (checklinespace
&& (cp
= buffer
+ strlen (buffer
) - 2) > buffer
&& isspace (*cp
)) {
1271 checklinespace
= 0; /* no need to keep checking */
1275 * Check if content contains a line that clashes
1276 * with our standard boundary for multipart messages.
1278 if (checkboundary
&& buffer
[0] == '-' && buffer
[1] == '-') {
1279 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1283 if (!strncmp(buffer
+ 2, prefix
, len
) && isdigit(buffer
[2 + len
])) {
1285 checkboundary
= 0; /* no need to keep checking */
1293 * Decide which transfer encoding to use.
1295 switch (ct
->c_type
) {
1298 * If the text content didn't specify a character
1299 * set, we need to figure out which one was used.
1301 t
= (struct text
*) ct
->c_ctparams
;
1302 if (t
->tx_charset
== CHARSET_UNSPECIFIED
) {
1303 CI ci
= &ct
->c_ctinfo
;
1306 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++)
1310 *ap
= concat ("charset=", write_charset_8bit(), NULL
);
1312 *ap
= add ("charset=us-ascii", NULL
);
1314 t
->tx_charset
= CHARSET_SPECIFIED
;
1316 cp
= strchr(*ap
++, '=');
1322 if (contains8bit
|| linelen
|| linespace
|| checksw
)
1323 ct
->c_encoding
= CE_QUOTED
;
1325 ct
->c_encoding
= CE_7BIT
;
1328 case CT_APPLICATION
:
1329 /* For application type, use base64, except when postscript */
1330 if (contains8bit
|| linelen
|| linespace
|| checksw
)
1331 ct
->c_encoding
= (ct
->c_subtype
== APPLICATION_POSTSCRIPT
)
1332 ? CE_QUOTED
: CE_BASE64
;
1334 ct
->c_encoding
= CE_7BIT
;
1338 ct
->c_encoding
= CE_7BIT
;
1344 /* For audio, image, and video contents, just use base64 */
1345 ct
->c_encoding
= CE_BASE64
;
1349 return (boundaryclash
? NOTOK
: OK
);
1354 * Scan the content structures, and build header
1355 * fields that will need to be output into the
1360 build_headers (CT ct
)
1362 int cc
, mailbody
, len
;
1364 char *np
, *vp
, buffer
[BUFSIZ
];
1365 CI ci
= &ct
->c_ctinfo
;
1368 * If message is type multipart, then add the multipart
1369 * boundary to the list of attribute/value pairs.
1371 if (ct
->c_type
== CT_MULTIPART
) {
1373 static int level
= 0; /* store nesting level */
1377 snprintf (buffer
, sizeof(buffer
), "boundary=%s%d", prefix
, level
++);
1378 cp
= strchr(*ap
++ = add (buffer
, NULL
), '=');
1385 * Skip the output of Content-Type, parameters, content
1386 * description and disposition, and Content-ID if the
1387 * content is of type "message" and the rfc934 compatibility
1388 * flag is set (which means we are inside multipart/digest
1389 * and the switch -rfc934mode was given).
1391 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_rfc934
)
1395 * output the content type and subtype
1397 np
= add (TYPE_FIELD
, NULL
);
1398 vp
= concat (" ", ci
->ci_type
, "/", ci
->ci_subtype
, NULL
);
1400 /* keep track of length of line */
1401 len
= strlen (TYPE_FIELD
) + strlen (ci
->ci_type
)
1402 + strlen (ci
->ci_subtype
) + 3;
1404 mailbody
= ct
->c_type
== CT_MESSAGE
1405 && ct
->c_subtype
== MESSAGE_EXTERNAL
1406 && ((struct exbody
*) ct
->c_ctparams
)->eb_body
;
1409 * Append the attribute/value pairs to
1410 * the end of the Content-Type line.
1412 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
1413 if (mailbody
&& !mh_strcasecmp (*ap
, "body"))
1419 snprintf (buffer
, sizeof(buffer
), "%s=\"%s\"", *ap
, *ep
);
1420 if (len
+ 1 + (cc
= strlen (buffer
)) >= CPERLIN
) {
1421 vp
= add ("\n\t", vp
);
1427 vp
= add (buffer
, vp
);
1432 * Append any RFC-822 comment to the end of
1433 * the Content-Type line.
1435 if (ci
->ci_comment
) {
1436 snprintf (buffer
, sizeof(buffer
), "(%s)", ci
->ci_comment
);
1437 if (len
+ 1 + (cc
= 2 + strlen (ci
->ci_comment
)) >= CPERLIN
) {
1438 vp
= add ("\n\t", vp
);
1444 vp
= add (buffer
, vp
);
1447 vp
= add ("\n", vp
);
1448 add_header (ct
, np
, vp
);
1451 * output the Content-ID, unless disabled by -nocontentid
1453 if (contentidsw
&& ct
->c_id
) {
1454 np
= add (ID_FIELD
, NULL
);
1455 vp
= concat (" ", ct
->c_id
, NULL
);
1456 add_header (ct
, np
, vp
);
1460 * output the Content-Description
1463 np
= add (DESCR_FIELD
, NULL
);
1464 vp
= concat (" ", ct
->c_descr
, NULL
);
1465 add_header (ct
, np
, vp
);
1469 * output the Content-Disposition
1472 np
= add (DISPO_FIELD
, NULL
);
1473 vp
= concat (" ", ct
->c_dispo
, NULL
);
1474 add_header (ct
, np
, vp
);
1479 * If this is the internal content structure for a
1480 * "message/external", then we are done with the
1481 * headers (since it has no body).
1487 * output the Content-MD5
1490 np
= add (MD5_FIELD
, NULL
);
1491 vp
= calculate_digest (ct
, (ct
->c_encoding
== CE_QUOTED
) ? 1 : 0);
1492 add_header (ct
, np
, vp
);
1496 * output the Content-Transfer-Encoding
1498 switch (ct
->c_encoding
) {
1500 /* Nothing to output */
1504 if (ct
->c_type
== CT_MESSAGE
)
1505 adios (NULL
, "internal error, invalid encoding");
1507 np
= add (ENCODING_FIELD
, NULL
);
1508 vp
= concat (" ", "8bit", "\n", NULL
);
1509 add_header (ct
, np
, vp
);
1513 if (ct
->c_type
== CT_MESSAGE
|| ct
->c_type
== CT_MULTIPART
)
1514 adios (NULL
, "internal error, invalid encoding");
1516 np
= add (ENCODING_FIELD
, NULL
);
1517 vp
= concat (" ", "quoted-printable", "\n", NULL
);
1518 add_header (ct
, np
, vp
);
1522 if (ct
->c_type
== CT_MESSAGE
|| ct
->c_type
== CT_MULTIPART
)
1523 adios (NULL
, "internal error, invalid encoding");
1525 np
= add (ENCODING_FIELD
, NULL
);
1526 vp
= concat (" ", "base64", "\n", NULL
);
1527 add_header (ct
, np
, vp
);
1531 if (ct
->c_type
== CT_MESSAGE
)
1532 adios (NULL
, "internal error, invalid encoding");
1534 np
= add (ENCODING_FIELD
, NULL
);
1535 vp
= concat (" ", "binary", "\n", NULL
);
1536 add_header (ct
, np
, vp
);
1540 adios (NULL
, "unknown transfer encoding in content");
1545 * Additional content specific header processing
1547 switch (ct
->c_type
) {
1550 struct multipart
*m
;
1553 m
= (struct multipart
*) ct
->c_ctparams
;
1554 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1564 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1567 e
= (struct exbody
*) ct
->c_ctparams
;
1568 build_headers (e
->eb_content
);
1581 static char nib2b64
[0x40+1] =
1582 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1585 calculate_digest (CT ct
, int asciiP
)
1588 char buffer
[BUFSIZ
], *vp
, *op
;
1590 unsigned char digest
[16];
1591 unsigned char outbuf
[25];
1593 CE ce
= ct
->c_cefile
;
1594 char *infilename
= ce
->ce_file
? ce
->ce_file
: ct
->c_file
;
1598 if ((in
= fopen (infilename
, "r")) == NULL
)
1599 adios (infilename
, "unable to open for reading");
1601 /* Initialize md5 context */
1602 MD5Init (&mdContext
);
1604 /* calculate md5 message digest */
1606 while (fgets (buffer
, sizeof(buffer
) - 1, in
)) {
1609 cp
= buffer
+ strlen (buffer
) - 1;
1610 if ((c
= *cp
) == '\n')
1613 MD5Update (&mdContext
, (unsigned char *) buffer
,
1614 (unsigned int) strlen (buffer
));
1617 MD5Update (&mdContext
, (unsigned char *) "\r\n", 2);
1620 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), in
)) > 0)
1621 MD5Update (&mdContext
, (unsigned char *) buffer
, (unsigned int) cc
);
1624 /* md5 finalization. Write digest and zero md5 context */
1625 MD5Final (digest
, &mdContext
);
1630 /* print debugging info */
1634 fprintf (stderr
, "MD5 digest=");
1635 for (ep
= (dp
= digest
) + sizeof(digest
) / sizeof(digest
[0]);
1637 fprintf (stderr
, "%02x", *dp
& 0xff);
1638 fprintf (stderr
, "\n");
1641 /* encode the digest using base64 */
1642 for (dp
= digest
, op
= outbuf
, cc
= sizeof(digest
) / sizeof(digest
[0]);
1643 cc
> 0; cc
-= 3, op
+= 4) {
1647 bits
= (*dp
++ & 0xff) << 16;
1649 bits
|= (*dp
++ & 0xff) << 8;
1651 bits
|= *dp
++ & 0xff;
1654 for (bp
= op
+ 4; bp
> op
; bits
>>= 6)
1655 *--bp
= nib2b64
[bits
& 0x3f];
1663 /* null terminate string */
1666 /* now make copy and return string */
1667 vp
= concat (" ", outbuf
, "\n", NULL
);