]>
diplodocus.org Git - nmh/blob - uip/mhbuildsbr.c
3 * mhbuildsbr.c -- routines to expand/translate MIME composition files
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
11 * This code was originally part of mhn.c. I split it into
12 * a separate program (mhbuild.c) and then later split it
13 * again (mhbuildsbr.c). But the code still has some of
14 * the mhn.c code in it. This program needs additional
15 * streamlining and removal of unneeded code.
20 #include <h/signals.h>
25 #include <h/mhparse.h>
28 #ifdef HAVE_SYS_TIME_H
29 # include <sys/time.h>
39 extern int contentidsw
;
42 extern int rcachesw
; /* mhcachesbr.c */
43 extern int wcachesw
; /* mhcachesbr.c */
46 * Directory to place tmp files. This must
47 * be set before these routines are called.
53 static char prefix
[] = "----- =_aaaaaaaaaa";
56 * Maximum size of URL token in message/external-body
59 #define MAXURLTOKEN 40
63 void content_error (char *, CT
, char *, ...);
66 int find_cache (CT
, int, int *, char *, char *, int);
69 void free_ctinfo (CT
);
70 void free_encoding (CT
, int);
75 CT
build_mime (char *, int);
80 static int init_decoded_content (CT
);
81 static char *fgetstr (char *, int, FILE *);
82 static int user_content (FILE *, char *, char *, CT
*);
83 static void set_id (CT
, int);
84 static int compose_content (CT
);
85 static int scan_content (CT
);
86 static int build_headers (CT
);
87 static char *calculate_digest (CT
, int);
90 static unsigned char directives_stack
[32];
91 static unsigned int directives_index
;
93 static int do_direct(void)
95 return directives_stack
[directives_index
];
98 static void directive_onoff(int onoff
)
100 if (directives_index
>= sizeof(directives_stack
) - 1) {
101 fprintf(stderr
, "mhbuild: #on/off overflow, continuing\n");
104 directives_stack
[++directives_index
] = onoff
;
107 static void directive_init(int onoff
)
109 directives_index
= 0;
110 directives_stack
[0] = onoff
;
113 static void directive_pop(void)
115 if (directives_index
> 0)
118 fprintf(stderr
, "mhbuild: #pop underflow, continuing\n");
122 * Main routine for translating composition file
123 * into valid MIME message. It translates the draft
124 * into a content structure (actually a tree of content
125 * structures). This message then can be manipulated
126 * in various ways, including being output via
131 build_mime (char *infile
, int directives
)
134 char buf
[BUFSIZ
], name
[NAMESZ
];
140 m_getfld_state_t gstate
= 0;
142 directive_init(directives
);
144 umask (~m_gmprot ());
146 /* open the composition draft */
147 if ((in
= fopen (infile
, "r")) == NULL
)
148 adios (infile
, "unable to open for reading");
151 * Allocate space for primary (outside) content
153 if ((ct
= (CT
) calloc (1, sizeof(*ct
))) == NULL
)
154 adios (NULL
, "out of memory");
157 * Allocate structure for handling decoded content
158 * for this part. We don't really need this, but
159 * allocate it to remain consistent.
161 init_decoded_content (ct
);
164 * Parse some of the header fields in the composition
165 * draft into the linked list of header fields for
166 * the new MIME message.
168 m_getfld_track_filepos (&gstate
, in
);
169 for (compnum
= 1;;) {
170 int bufsz
= sizeof buf
;
171 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
)) {
176 /* abort if draft has Mime-Version header field */
177 if (!strcasecmp (name
, VRSN_FIELD
))
178 adios (NULL
, "draft shouldn't contain %s: field", VRSN_FIELD
);
180 /* abort if draft has Content-Transfer-Encoding header field */
181 if (!strcasecmp (name
, ENCODING_FIELD
))
182 adios (NULL
, "draft shouldn't contain %s: field", ENCODING_FIELD
);
184 /* ignore any Content-Type fields in the header */
185 if (!strcasecmp (name
, TYPE_FIELD
)) {
186 while (state
== FLDPLUS
) {
188 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
193 /* get copies of the buffers */
194 np
= add (name
, NULL
);
195 vp
= add (buf
, NULL
);
197 /* if necessary, get rest of field */
198 while (state
== FLDPLUS
) {
200 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, in
);
201 vp
= add (buf
, vp
); /* add to previous value */
204 /* Now add the header data to the list */
205 add_header (ct
, np
, vp
);
208 /* if this wasn't the last header field, then continue */
212 adios (NULL
, "draft has empty body -- no directives!");
216 fseek (in
, (long) (-strlen (buf
)), SEEK_CUR
);
221 adios (NULL
, "message format error in component #%d", compnum
);
224 adios (NULL
, "getfld() returned %d", state
);
228 m_getfld_state_destroy (&gstate
);
231 * Now add the MIME-Version header field
232 * to the list of header fields.
234 np
= add (VRSN_FIELD
, NULL
);
235 vp
= concat (" ", VRSN_VALUE
, "\n", NULL
);
236 add_header (ct
, np
, vp
);
239 * We initally assume we will find multiple contents in the
240 * draft. So create a multipart/mixed content to hold everything.
241 * We can remove this later, if it is not needed.
243 if (get_ctinfo ("multipart/mixed", ct
, 0) == NOTOK
)
245 ct
->c_type
= CT_MULTIPART
;
246 ct
->c_subtype
= MULTI_MIXED
;
247 ct
->c_file
= add (infile
, NULL
);
249 if ((m
= (struct multipart
*) calloc (1, sizeof(*m
))) == NULL
)
250 adios (NULL
, "out of memory");
251 ct
->c_ctparams
= (void *) m
;
255 * read and parse the composition file
256 * and the directives it contains.
258 while (fgetstr (buf
, sizeof(buf
) - 1, in
)) {
262 if (user_content (in
, infile
, buf
, &p
) == DONE
) {
263 admonish (NULL
, "ignoring spurious #end");
269 if ((part
= (struct part
*) calloc (1, sizeof(*part
))) == NULL
)
270 adios (NULL
, "out of memory");
277 * close the composition draft since
278 * it's not needed any longer.
282 /* check if any contents were found */
284 adios (NULL
, "no content directives found");
287 * If only one content was found, then remove and
288 * free the outer multipart content.
290 if (!m
->mp_parts
->mp_next
) {
293 p
= m
->mp_parts
->mp_part
;
294 m
->mp_parts
->mp_part
= NULL
;
296 /* move header fields */
297 p
->c_first_hf
= ct
->c_first_hf
;
298 p
->c_last_hf
= ct
->c_last_hf
;
299 ct
->c_first_hf
= NULL
;
300 ct
->c_last_hf
= NULL
;
309 * Fill out, or expand directives. Parse and execute
310 * commands specified by profile composition strings.
312 compose_content (ct
);
314 if ((cp
= strchr(prefix
, 'a')) == NULL
)
315 adios (NULL
, "internal error(4)");
318 * Scan the contents. Choose a transfer encoding, and
319 * check if prefix for multipart boundary clashes with
320 * any of the contents.
322 while (scan_content (ct
) == NOTOK
) {
327 adios (NULL
, "giving up trying to find a unique delimiter string");
333 /* Build the rest of the header field structures */
341 * Set up structures for placing unencoded
342 * content when building parts.
346 init_decoded_content (CT ct
)
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
)
408 struct str2init
*s2i
;
413 if (buf
[0] == '\n' || (do_direct() && strcmp (buf
, "#\n") == 0)) {
418 /* allocate basic Content structure */
419 if ((ct
= (CT
) calloc (1, sizeof(*ct
))) == NULL
)
420 adios (NULL
, "out of memory");
423 /* allocate basic structure for handling decoded content */
424 init_decoded_content (ct
);
431 * Handle inline text. Check if line
432 * is one of the following forms:
434 * 1) doesn't begin with '#' (implicit directive)
435 * 2) begins with "##" (implicit directive)
436 * 3) begins with "#<"
438 if (!do_direct() || buf
[0] != '#' || buf
[1] == '#' || buf
[1] == '<') {
442 char content
[BUFSIZ
];
446 cp
= m_mktemp2(NULL
, invo_name
, NULL
, &out
);
447 if (cp
== NULL
) adios("mhbuildsbr", "unable to create temporary file");
449 /* use a temp file to collect the plain text lines */
450 ce
->ce_file
= add (cp
, NULL
);
453 if (do_direct() && (buf
[0] == '#' && buf
[1] == '<')) {
454 strncpy (content
, buf
+ 2, sizeof(content
));
461 /* the directive is implicit */
462 strncpy (content
, "text/plain", sizeof(content
));
464 strncpy (buffer
, (!do_direct() || buf
[0] != '#') ? buf
: buf
+ 1, sizeof(buffer
));
468 if (headers
>= 0 && do_direct() && uprf (buffer
, DESCR_FIELD
)
469 && buffer
[i
= strlen (DESCR_FIELD
)] == ':') {
473 ct
->c_descr
= add (buffer
+ i
+ 1, ct
->c_descr
);
474 if (!fgetstr (buffer
, sizeof(buffer
) - 1, in
))
475 adios (NULL
, "end-of-file after %s: field in plaintext", DESCR_FIELD
);
483 adios (NULL
, "#-directive after %s: field in plaintext", DESCR_FIELD
);
491 if (headers
>= 0 && do_direct() && uprf (buffer
, DISPO_FIELD
)
492 && buffer
[i
= strlen (DISPO_FIELD
)] == ':') {
496 ct
->c_dispo
= add (buffer
+ i
+ 1, ct
->c_dispo
);
497 if (!fgetstr (buffer
, sizeof(buffer
) - 1, in
))
498 adios (NULL
, "end-of-file after %s: field in plaintext", DISPO_FIELD
);
506 adios (NULL
, "#-directive after %s: field in plaintext", DISPO_FIELD
);
514 if (headers
!= 1 || buffer
[0] != '\n')
520 if ((cp
= fgetstr (buffer
, sizeof(buffer
) - 1, in
)) == NULL
)
522 if (do_direct() && buffer
[0] == '#') {
525 if (buffer
[1] != '#')
527 for (cp
= (bp
= buffer
) + 1; *cp
; cp
++)
534 ct
->c_end
= ftell (out
);
537 /* parse content type */
538 if (get_ctinfo (content
, ct
, inlineD
) == NOTOK
)
541 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
542 if (!strcasecmp (ci
->ci_type
, s2i
->si_key
))
544 if (!s2i
->si_key
&& !uprf (ci
->ci_type
, "X-"))
548 * check type specified (possibly implicitly)
550 switch (ct
->c_type
= s2i
->si_val
) {
552 if (!strcasecmp (ci
->ci_subtype
, "rfc822")) {
553 ct
->c_encoding
= CE_7BIT
;
558 adios (NULL
, "it doesn't make sense to define an in-line %s content",
559 ct
->c_type
== CT_MESSAGE
? "message" : "multipart");
564 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
565 (*ct
->c_ctinitfnx
) (ct
);
570 fseek (in
, pos
, SEEK_SET
);
575 * If we've reached this point, the next line
576 * must be some type of explicit directive.
579 /* check if directive is external-type */
580 extrnal
= (buf
[1] == '@');
582 /* parse directive */
583 if (get_ctinfo (buf
+ (extrnal
? 2 : 1), ct
, 1) == NOTOK
)
586 /* check directive against the list of MIME types */
587 for (s2i
= str2cts
; s2i
->si_key
; s2i
++)
588 if (!strcasecmp (ci
->ci_type
, s2i
->si_key
))
592 * Check if the directive specified a valid type.
593 * This will happen if it was one of the following forms:
600 adios (NULL
, "missing subtype in \"#%s\"", ci
->ci_type
);
602 switch (ct
->c_type
= s2i
->si_val
) {
604 adios (NULL
, "use \"#begin ... #end\" instead of \"#%s/%s\"",
605 ci
->ci_type
, ci
->ci_subtype
);
609 if (!strcasecmp (ci
->ci_subtype
, "partial"))
610 adios (NULL
, "sorry, \"#%s/%s\" isn't supported",
611 ci
->ci_type
, ci
->ci_subtype
);
612 if (!strcasecmp (ci
->ci_subtype
, "external-body"))
613 adios (NULL
, "use \"#@type/subtype ... [] ...\" instead of \"#%s/%s\"",
614 ci
->ci_type
, ci
->ci_subtype
);
617 "use \"#forw [+folder] [msgs]\" instead of \"#%s/%s\"",
618 ci
->ci_type
, ci
->ci_subtype
);
622 if ((ct
->c_ctinitfnx
= s2i
->si_init
))
623 (*ct
->c_ctinitfnx
) (ct
);
628 * #@type/subtype (external types directive)
635 adios (NULL
, "need external information for \"#@%s/%s\"",
636 ci
->ci_type
, ci
->ci_subtype
);
639 snprintf (buffer
, sizeof(buffer
), "message/external-body; %s", ci
->ci_magic
);
644 * Since we are using the current Content structure to
645 * hold information about the type of the external
646 * reference, we need to create another Content structure
647 * for the message/external-body to wrap it in.
649 if ((ct
= (CT
) calloc (1, sizeof(*ct
))) == NULL
)
650 adios (NULL
, "out of memory");
653 if (get_ctinfo (buffer
, ct
, 0) == NOTOK
)
655 ct
->c_type
= CT_MESSAGE
;
656 ct
->c_subtype
= MESSAGE_EXTERNAL
;
658 if ((e
= (struct exbody
*) calloc (1, sizeof(*e
))) == NULL
)
659 adios (NULL
, "out of memory");
660 ct
->c_ctparams
= (void *) e
;
666 if (params_external (ct
, 1) == NOTOK
)
672 /* Handle [file] argument */
674 /* check if specifies command to execute */
675 if (*ci
->ci_magic
== '|' || *ci
->ci_magic
== '!') {
676 for (cp
= ci
->ci_magic
+ 1; isspace ((unsigned char) *cp
); cp
++)
679 adios (NULL
, "empty pipe command for #%s directive", ci
->ci_type
);
684 /* record filename of decoded contents */
685 ce
->ce_file
= ci
->ci_magic
;
686 if (access (ce
->ce_file
, R_OK
) == NOTOK
)
687 adios ("reading", "unable to access %s for", ce
->ce_file
);
688 if (listsw
&& stat (ce
->ce_file
, &st
) != NOTOK
)
689 ct
->c_end
= (long) st
.st_size
;
696 * No [file] argument, so check profile for
697 * method to compose content.
699 snprintf (buffer
, sizeof(buffer
), "%s-compose-%s/%s",
700 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
701 if ((cp
= context_find (buffer
)) == NULL
|| *cp
== '\0') {
702 snprintf (buffer
, sizeof(buffer
), "%s-compose-%s", invo_name
, ci
->ci_type
);
703 if ((cp
= context_find (buffer
)) == NULL
|| *cp
== '\0') {
704 content_error (NULL
, ct
, "don't know how to compose content");
708 ci
->ci_magic
= add (cp
, NULL
);
713 adios (NULL
, "external definition not allowed for \"#%s\"", ci
->ci_type
);
717 * #forw [+folder] [msgs]
719 if (!strcasecmp (ci
->ci_type
, "forw")) {
721 char *folder
, *arguments
[MAXARGS
];
725 ap
= brkstring (ci
->ci_magic
, " ", "\n");
726 copyip (ap
, arguments
, MAXARGS
);
728 arguments
[0] = "cur";
733 /* search the arguments for a folder name */
734 for (ap
= arguments
; *ap
; ap
++) {
736 if (*cp
== '+' || *cp
== '@') {
738 adios (NULL
, "only one folder per #forw directive");
740 folder
= pluspath (cp
);
744 /* else, use the current folder */
746 folder
= add (getfolder (1), NULL
);
748 if (!(mp
= folder_read (folder
, 0)))
749 adios (NULL
, "unable to read folder %s", folder
);
750 for (ap
= arguments
; *ap
; ap
++) {
752 if (*cp
!= '+' && *cp
!= '@')
753 if (!m_convert (mp
, cp
))
760 * If there is more than one message to include, make this
761 * a content of type "multipart/digest" and insert each message
762 * as a subpart. If there is only one message, then make this
763 * a content of type "message/rfc822".
765 if (mp
->numsel
> 1) {
766 /* we are forwarding multiple messages */
767 if (get_ctinfo ("multipart/digest", ct
, 0) == NOTOK
)
769 ct
->c_type
= CT_MULTIPART
;
770 ct
->c_subtype
= MULTI_DIGEST
;
772 if ((m
= (struct multipart
*) calloc (1, sizeof(*m
))) == NULL
)
773 adios (NULL
, "out of memory");
774 ct
->c_ctparams
= (void *) m
;
777 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
778 if (is_selected(mp
, msgnum
)) {
783 if ((p
= (CT
) calloc (1, sizeof(*p
))) == NULL
)
784 adios (NULL
, "out of memory");
785 init_decoded_content (p
);
787 if (get_ctinfo ("message/rfc822", p
, 0) == NOTOK
)
789 p
->c_type
= CT_MESSAGE
;
790 p
->c_subtype
= MESSAGE_RFC822
;
792 snprintf (buffer
, sizeof(buffer
), "%s/%d", mp
->foldpath
, msgnum
);
793 pe
->ce_file
= add (buffer
, NULL
);
794 if (listsw
&& stat (pe
->ce_file
, &st
) != NOTOK
)
795 p
->c_end
= (long) st
.st_size
;
797 if ((part
= (struct part
*) calloc (1, sizeof(*part
))) == NULL
)
798 adios (NULL
, "out of memory");
805 /* we are forwarding one message */
806 if (get_ctinfo ("message/rfc822", ct
, 0) == NOTOK
)
808 ct
->c_type
= CT_MESSAGE
;
809 ct
->c_subtype
= MESSAGE_RFC822
;
812 snprintf (buffer
, sizeof(buffer
), "%s/%d", mp
->foldpath
, msgnum
);
813 ce
->ce_file
= add (buffer
, NULL
);
814 if (listsw
&& stat (ce
->ce_file
, &st
) != NOTOK
)
815 ct
->c_end
= (long) st
.st_size
;
818 folder_free (mp
); /* free folder/message structure */
825 if (!strcasecmp (ci
->ci_type
, "end")) {
832 * #begin [ alternative | parallel ]
834 if (!strcasecmp (ci
->ci_type
, "begin")) {
837 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
838 } else if (!strcasecmp (ci
->ci_magic
, "alternative")) {
839 vrsn
= MULTI_ALTERNATE
;
840 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
841 } else if (!strcasecmp (ci
->ci_magic
, "parallel")) {
842 vrsn
= MULTI_PARALLEL
;
843 cp
= SubMultiPart
[vrsn
- 1].kv_key
;
844 } else if (uprf (ci
->ci_magic
, "digest")) {
847 vrsn
= MULTI_UNKNOWN
;
852 snprintf (buffer
, sizeof(buffer
), "multipart/%s", cp
);
853 if (get_ctinfo (buffer
, ct
, 0) == NOTOK
)
855 ct
->c_type
= CT_MULTIPART
;
856 ct
->c_subtype
= vrsn
;
858 if ((m
= (struct multipart
*) calloc (1, sizeof(*m
))) == NULL
)
859 adios (NULL
, "out of memory");
860 ct
->c_ctparams
= (void *) m
;
863 while (fgetstr (buffer
, sizeof(buffer
) - 1, in
)) {
867 if (user_content (in
, file
, buffer
, &p
) == DONE
) {
869 adios (NULL
, "empty \"#begin ... #end\" sequence");
875 if ((part
= (struct part
*) calloc (1, sizeof(*part
))) == NULL
)
876 adios (NULL
, "out of memory");
881 admonish (NULL
, "premature end-of-file, missing #end");
888 adios (NULL
, "unknown directive \"#%s\"", ci
->ci_type
);
889 return NOTOK
; /* NOT REACHED */
894 set_id (CT ct
, int top
)
896 char contentid
[BUFSIZ
];
898 static time_t clock
= 0;
903 snprintf (contentid
, sizeof(contentid
), "%s\n", message_id (clock
, 1));
905 msgfmt
= getcpy(contentid
);
907 snprintf (contentid
, sizeof(contentid
), msgfmt
, top
? 0 : ++partno
);
908 ct
->c_id
= getcpy (contentid
);
913 * Fill out, or expand the various contents in the composition
914 * draft. Read-in any necessary files. Parse and execute any
915 * commands specified by profile composition strings.
919 compose_content (CT ct
)
921 CE ce
= &ct
->c_cefile
;
923 switch (ct
->c_type
) {
928 char partnam
[BUFSIZ
];
929 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
933 snprintf (partnam
, sizeof(partnam
), "%s.", ct
->c_partno
);
934 pp
= partnam
+ strlen (partnam
);
939 /* first, we call compose_content on all the subparts */
940 for (part
= m
->mp_parts
, partnum
= 1; part
; part
= part
->mp_next
, partnum
++) {
941 CT p
= part
->mp_part
;
943 sprintf (pp
, "%d", partnum
);
944 p
->c_partno
= add (partnam
, NULL
);
945 if (compose_content (p
) == NOTOK
)
950 * If the -rfc934mode switch is given, then check all
951 * the subparts of a multipart/digest. If they are all
952 * message/rfc822, then mark this content and all
953 * subparts with the rfc934 compatibility mode flag.
955 if (rfc934sw
&& ct
->c_subtype
== MULTI_DIGEST
) {
958 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
959 CT p
= part
->mp_part
;
961 if (p
->c_subtype
!= MESSAGE_RFC822
) {
966 ct
->c_rfc934
= is934
;
967 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
968 CT p
= part
->mp_part
;
970 if ((p
->c_rfc934
= is934
))
976 ct
->c_end
= (partnum
= strlen (prefix
) + 2) + 2;
980 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
)
981 ct
->c_end
+= part
->mp_part
->c_end
+ partnum
;
987 /* Nothing to do for type message */
991 * Discrete types (text/application/audio/image/video)
996 int i
, xstdout
, len
, buflen
;
998 char *vec
[4], buffer
[BUFSIZ
];
1000 CI ci
= &ct
->c_ctinfo
;
1003 if (!(cp
= ci
->ci_magic
))
1004 adios (NULL
, "internal error(5)");
1006 tfile
= m_mktemp2(NULL
, invo_name
, NULL
, NULL
);
1007 if (tfile
== NULL
) {
1008 adios("mhbuildsbr", "unable to create temporary file");
1010 ce
->ce_file
= add (tfile
, NULL
);
1015 /* Get buffer ready to go */
1018 buflen
= sizeof(buffer
);
1021 * Parse composition string into buffer
1023 for ( ; *cp
; cp
++) {
1028 /* insert parameters from directive */
1032 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
1033 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, *ap
, *ep
);
1043 /* %f, and stdout is not-redirected */
1049 * insert temporary filename where
1050 * content should be written
1052 snprintf (bp
, buflen
, "%s", ce
->ce_file
);
1056 /* insert content subtype */
1057 strncpy (bp
, ci
->ci_subtype
, buflen
);
1061 /* insert character % */
1082 printf ("composing content %s/%s from command\n\t%s\n",
1083 ci
->ci_type
, ci
->ci_subtype
, buffer
);
1085 fflush (stdout
); /* not sure if need for -noverbose */
1092 if ((out
= fopen (ce
->ce_file
, "w")) == NULL
)
1093 adios (ce
->ce_file
, "unable to open for writing");
1095 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
> 5; i
++)
1099 adios ("fork", "unable to fork");
1104 dup2 (fileno (out
), 1);
1105 close (fileno (out
));
1106 execvp ("/bin/sh", vec
);
1107 fprintf (stderr
, "unable to exec ");
1114 if (pidXwait(child_id
, NULL
))
1120 /* Check size of file */
1121 if (listsw
&& ct
->c_end
== 0L) {
1124 if (stat (ce
->ce_file
, &st
) != NOTOK
)
1125 ct
->c_end
= (long) st
.st_size
;
1137 * 1) choose a transfer encoding.
1138 * 2) check for clashes with multipart boundary string.
1139 * 3) for text content, figure out which character set is being used.
1141 * If there is a clash with one of the contents and the multipart boundary,
1142 * this function will exit with NOTOK. This will cause the scanning process
1143 * to be repeated with a different multipart boundary. It is possible
1144 * (although highly unlikely) that this scan will be repeated multiple times.
1148 scan_content (CT ct
)
1151 int check8bit
= 0, contains8bit
= 0; /* check if contains 8bit data */
1152 int checklinelen
= 0, linelen
= 0; /* check for long lines */
1153 int checkboundary
= 0, boundaryclash
= 0; /* check if clashes with multipart boundary */
1154 int checklinespace
= 0, linespace
= 0; /* check if any line ends with space */
1155 char *cp
= NULL
, buffer
[BUFSIZ
];
1156 struct text
*t
= NULL
;
1158 CE ce
= &ct
->c_cefile
;
1161 * handle multipart by scanning all subparts
1162 * and then checking their encoding.
1164 if (ct
->c_type
== CT_MULTIPART
) {
1165 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1168 /* initially mark the domain of enclosing multipart as 7bit */
1169 ct
->c_encoding
= CE_7BIT
;
1171 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1172 CT p
= part
->mp_part
;
1174 if (scan_content (p
) == NOTOK
) /* choose encoding for subpart */
1177 /* if necessary, enlarge encoding for enclosing multipart */
1178 if (p
->c_encoding
== CE_BINARY
)
1179 ct
->c_encoding
= CE_BINARY
;
1180 if (p
->c_encoding
== CE_8BIT
&& ct
->c_encoding
!= CE_BINARY
)
1181 ct
->c_encoding
= CE_8BIT
;
1188 * Decide what to check while scanning this content.
1190 switch (ct
->c_type
) {
1194 if (ct
->c_subtype
== TEXT_PLAIN
) {
1203 case CT_APPLICATION
:
1215 /* don't check anything for message/external */
1216 if (ct
->c_subtype
== MESSAGE_EXTERNAL
)
1226 * Don't check anything for these types,
1227 * since we are forcing use of base64.
1237 * Scan the unencoded content
1239 if (check8bit
|| checklinelen
|| checklinespace
|| checkboundary
) {
1240 if ((in
= fopen (ce
->ce_file
, "r")) == NULL
)
1241 adios (ce
->ce_file
, "unable to open for reading");
1242 len
= strlen (prefix
);
1244 while (fgets (buffer
, sizeof(buffer
) - 1, in
)) {
1246 * Check for 8bit data.
1249 for (cp
= buffer
; *cp
; cp
++) {
1250 if (!isascii ((unsigned char) *cp
)) {
1252 check8bit
= 0; /* no need to keep checking */
1258 * Check line length.
1260 if (checklinelen
&& (strlen (buffer
) > CPERLIN
+ 1)) {
1262 checklinelen
= 0; /* no need to keep checking */
1266 * Check if line ends with a space.
1268 if (checklinespace
&& (cp
= buffer
+ strlen (buffer
) - 2) > buffer
&& isspace ((unsigned char) *cp
)) {
1270 checklinespace
= 0; /* no need to keep checking */
1274 * Check if content contains a line that clashes
1275 * with our standard boundary for multipart messages.
1277 if (checkboundary
&& buffer
[0] == '-' && buffer
[1] == '-') {
1278 for (cp
= buffer
+ strlen (buffer
) - 1; cp
>= buffer
; cp
--)
1279 if (!isspace ((unsigned char) *cp
))
1282 if (!strncmp(buffer
+ 2, prefix
, len
) && isdigit((unsigned char) buffer
[2 + len
])) {
1284 checkboundary
= 0; /* no need to keep checking */
1292 * Decide which transfer encoding to use.
1294 switch (ct
->c_type
) {
1297 * If the text content didn't specify a character
1298 * set, we need to figure out which one was used.
1300 t
= (struct text
*) ct
->c_ctparams
;
1301 if (t
->tx_charset
== CHARSET_UNSPECIFIED
) {
1302 CI ci
= &ct
->c_ctinfo
;
1305 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++)
1309 *ap
= concat ("charset=", write_charset_8bit(), NULL
);
1311 *ap
= add ("charset=us-ascii", NULL
);
1313 t
->tx_charset
= CHARSET_SPECIFIED
;
1315 cp
= strchr(*ap
++, '=');
1321 if (contains8bit
|| linelen
|| linespace
|| checksw
)
1322 ct
->c_encoding
= CE_QUOTED
;
1324 ct
->c_encoding
= CE_7BIT
;
1327 case CT_APPLICATION
:
1328 /* For application type, use base64, except when postscript */
1329 if (contains8bit
|| linelen
|| linespace
|| checksw
)
1330 ct
->c_encoding
= (ct
->c_subtype
== APPLICATION_POSTSCRIPT
)
1331 ? CE_QUOTED
: CE_BASE64
;
1333 ct
->c_encoding
= CE_7BIT
;
1337 ct
->c_encoding
= CE_7BIT
;
1343 /* For audio, image, and video contents, just use base64 */
1344 ct
->c_encoding
= CE_BASE64
;
1348 return (boundaryclash
? NOTOK
: OK
);
1353 * Scan the content structures, and build header
1354 * fields that will need to be output into the
1359 build_headers (CT ct
)
1361 int cc
, mailbody
, extbody
, len
;
1363 char *np
, *vp
, buffer
[BUFSIZ
];
1364 CI ci
= &ct
->c_ctinfo
;
1367 * If message is type multipart, then add the multipart
1368 * boundary to the list of attribute/value pairs.
1370 if (ct
->c_type
== CT_MULTIPART
) {
1372 static int level
= 0; /* store nesting level */
1376 snprintf (buffer
, sizeof(buffer
), "boundary=%s%d", prefix
, level
++);
1377 cp
= strchr(*ap
++ = add (buffer
, NULL
), '=');
1384 * Skip the output of Content-Type, parameters, content
1385 * description and disposition, and Content-ID if the
1386 * content is of type "message" and the rfc934 compatibility
1387 * flag is set (which means we are inside multipart/digest
1388 * and the switch -rfc934mode was given).
1390 if (ct
->c_type
== CT_MESSAGE
&& ct
->c_rfc934
)
1394 * output the content type and subtype
1396 np
= add (TYPE_FIELD
, NULL
);
1397 vp
= concat (" ", ci
->ci_type
, "/", ci
->ci_subtype
, NULL
);
1399 /* keep track of length of line */
1400 len
= strlen (TYPE_FIELD
) + strlen (ci
->ci_type
)
1401 + strlen (ci
->ci_subtype
) + 3;
1403 extbody
= ct
->c_type
== CT_MESSAGE
&& ct
->c_subtype
== MESSAGE_EXTERNAL
;
1404 mailbody
= extbody
&& ((struct exbody
*) ct
->c_ctparams
)->eb_body
;
1407 * Append the attribute/value pairs to
1408 * the end of the Content-Type line.
1410 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
1411 if (mailbody
&& !strcasecmp (*ap
, "body"))
1418 * According to RFC 2017, if we have a URL longer than 40 characters
1419 * we have to break it across multiple lines
1422 if (extbody
&& strcasecmp (*ap
, "url") == 0) {
1425 /* 7 here refers to " url=\"\"" */
1426 if (len
+ 1 + (cc
= (min(MAXURLTOKEN
, strlen(value
)) + 7)) >=
1428 vp
= add ("\n\t", vp
);
1435 vp
= add ("url=\"", vp
);
1438 while (strlen(value
) > MAXURLTOKEN
) {
1439 strncpy(buffer
, value
, MAXURLTOKEN
);
1440 buffer
[MAXURLTOKEN
] = '\0';
1441 vp
= add (buffer
, vp
);
1442 vp
= add ("\n\t", vp
);
1443 value
+= MAXURLTOKEN
;
1447 vp
= add (value
, vp
);
1448 vp
= add ("\"", vp
);
1449 len
+= strlen(value
) + 1;
1453 snprintf (buffer
, sizeof(buffer
), "%s=\"%s\"", *ap
, *ep
);
1454 if (len
+ 1 + (cc
= strlen (buffer
)) >= CPERLIN
) {
1455 vp
= add ("\n\t", vp
);
1461 vp
= add (buffer
, vp
);
1466 * Append any RFC-822 comment to the end of
1467 * the Content-Type line.
1469 if (ci
->ci_comment
) {
1470 snprintf (buffer
, sizeof(buffer
), "(%s)", ci
->ci_comment
);
1471 if (len
+ 1 + (cc
= 2 + strlen (ci
->ci_comment
)) >= CPERLIN
) {
1472 vp
= add ("\n\t", vp
);
1478 vp
= add (buffer
, vp
);
1481 vp
= add ("\n", vp
);
1482 add_header (ct
, np
, vp
);
1485 * output the Content-ID, unless disabled by -nocontentid
1487 if (contentidsw
&& ct
->c_id
) {
1488 np
= add (ID_FIELD
, NULL
);
1489 vp
= concat (" ", ct
->c_id
, NULL
);
1490 add_header (ct
, np
, vp
);
1494 * output the Content-Description
1497 np
= add (DESCR_FIELD
, NULL
);
1498 vp
= concat (" ", ct
->c_descr
, NULL
);
1499 add_header (ct
, np
, vp
);
1503 * output the Content-Disposition
1506 np
= add (DISPO_FIELD
, NULL
);
1507 vp
= concat (" ", ct
->c_dispo
, NULL
);
1508 add_header (ct
, np
, vp
);
1513 * If this is the internal content structure for a
1514 * "message/external", then we are done with the
1515 * headers (since it has no body).
1521 * output the Content-MD5
1524 np
= add (MD5_FIELD
, NULL
);
1525 vp
= calculate_digest (ct
, (ct
->c_encoding
== CE_QUOTED
) ? 1 : 0);
1526 add_header (ct
, np
, vp
);
1530 * output the Content-Transfer-Encoding
1532 switch (ct
->c_encoding
) {
1534 /* Nothing to output */
1538 if (ct
->c_type
== CT_MESSAGE
)
1539 adios (NULL
, "internal error, invalid encoding");
1541 np
= add (ENCODING_FIELD
, NULL
);
1542 vp
= concat (" ", "8bit", "\n", NULL
);
1543 add_header (ct
, np
, vp
);
1547 if (ct
->c_type
== CT_MESSAGE
|| ct
->c_type
== CT_MULTIPART
)
1548 adios (NULL
, "internal error, invalid encoding");
1550 np
= add (ENCODING_FIELD
, NULL
);
1551 vp
= concat (" ", "quoted-printable", "\n", NULL
);
1552 add_header (ct
, np
, vp
);
1556 if (ct
->c_type
== CT_MESSAGE
|| ct
->c_type
== CT_MULTIPART
)
1557 adios (NULL
, "internal error, invalid encoding");
1559 np
= add (ENCODING_FIELD
, NULL
);
1560 vp
= concat (" ", "base64", "\n", NULL
);
1561 add_header (ct
, np
, vp
);
1565 if (ct
->c_type
== CT_MESSAGE
)
1566 adios (NULL
, "internal error, invalid encoding");
1568 np
= add (ENCODING_FIELD
, NULL
);
1569 vp
= concat (" ", "binary", "\n", NULL
);
1570 add_header (ct
, np
, vp
);
1574 adios (NULL
, "unknown transfer encoding in content");
1579 * Additional content specific header processing
1581 switch (ct
->c_type
) {
1584 struct multipart
*m
;
1587 m
= (struct multipart
*) ct
->c_ctparams
;
1588 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1598 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1601 e
= (struct exbody
*) ct
->c_ctparams
;
1602 build_headers (e
->eb_content
);
1615 static char nib2b64
[0x40+1] =
1616 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1619 calculate_digest (CT ct
, int asciiP
)
1622 char buffer
[BUFSIZ
], *vp
, *op
;
1624 unsigned char digest
[16];
1625 unsigned char outbuf
[25];
1627 CE ce
= &ct
->c_cefile
;
1628 char *infilename
= ce
->ce_file
? ce
->ce_file
: ct
->c_file
;
1632 if ((in
= fopen (infilename
, "r")) == NULL
)
1633 adios (infilename
, "unable to open for reading");
1635 /* Initialize md5 context */
1636 MD5Init (&mdContext
);
1638 /* calculate md5 message digest */
1640 while (fgets (buffer
, sizeof(buffer
) - 1, in
)) {
1643 cp
= buffer
+ strlen (buffer
) - 1;
1644 if ((c
= *cp
) == '\n')
1647 MD5Update (&mdContext
, (unsigned char *) buffer
,
1648 (unsigned int) strlen (buffer
));
1651 MD5Update (&mdContext
, (unsigned char *) "\r\n", 2);
1654 while ((cc
= fread (buffer
, sizeof(*buffer
), sizeof(buffer
), in
)) > 0)
1655 MD5Update (&mdContext
, (unsigned char *) buffer
, (unsigned int) cc
);
1658 /* md5 finalization. Write digest and zero md5 context */
1659 MD5Final (digest
, &mdContext
);
1664 /* print debugging info */
1668 fprintf (stderr
, "MD5 digest=");
1669 for (ep
= (dp
= digest
) + sizeof(digest
) / sizeof(digest
[0]);
1671 fprintf (stderr
, "%02x", *dp
& 0xff);
1672 fprintf (stderr
, "\n");
1675 /* encode the digest using base64 */
1676 for (dp
= digest
, op
= (char *) outbuf
,
1677 cc
= sizeof(digest
) / sizeof(digest
[0]);
1678 cc
> 0; cc
-= 3, op
+= 4) {
1682 bits
= (*dp
++ & 0xff) << 16;
1684 bits
|= (*dp
++ & 0xff) << 8;
1686 bits
|= *dp
++ & 0xff;
1689 for (bp
= op
+ 4; bp
> op
; bits
>>= 6)
1690 *--bp
= nib2b64
[bits
& 0x3f];
1698 /* null terminate string */
1701 /* now make copy and return string */
1702 vp
= concat (" ", outbuf
, "\n", NULL
);