add irc channel to README
[mmh] / uip / mhbuild.c
1 /*
2 ** mhbuild.c -- expand/translate MIME composition files
3 **
4 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
7 */
8
9 /*
10 ** This code was originally part of mhn.c.  I split it into
11 ** a separate program (mhbuild.c). But the code still has some of
12 ** the mhn.c code in it.  This program needs additional
13 ** streamlining and removal of unneeded code.
14 */
15
16 #include <h/mh.h>
17 #include <fcntl.h>
18 #include <errno.h>
19 #include <h/tws.h>
20 #include <h/mime.h>
21 #include <h/mhparse.h>
22 #include <h/utils.h>
23 #include <unistd.h>
24 #include <ctype.h>
25 #include <sys/stat.h>
26 #include <locale.h>
27 #include <sysexits.h>
28
29 #ifdef HAVE_SYS_TIME_H
30 # include <sys/time.h>
31 #endif
32 #include <time.h>
33
34 static struct swit switches[] = {
35 #define VERBSW  0
36         { "verbose", 0 },
37 #define NVERBSW  1
38         { "noverbose", 2 },
39 #define VERSIONSW  2
40         { "Version", 0 },
41 #define HELPSW  3
42         { "help", 0 },
43 #define DEBUGSW  4
44         { "debug", -5 },
45         { NULL, 0 }
46 };
47
48 char *version=VERSION;
49
50 /*
51 ** Directory to place tmp files.  This must
52 ** be set before these routines are called.
53 */
54 char *tmp;
55
56 pid_t xpid = 0;
57
58 static char prefix[] = "----- =_aaaaaaaaaa";
59
60
61 /*
62 ** prototypes
63 */
64
65 /* mhmisc.c */
66 int make_intermediates(char *);
67 void content_error(char *, CT, char *, ...);
68
69 /* mhfree.c */
70 void free_content(CT);
71 void free_ctinfo(CT);
72 void free_encoding(CT, int);
73
74 /*
75 ** static prototypes
76 */
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 CT build_mime(char *);
85
86
87 int debugsw = 0;
88 int verbosw = 0;
89
90 /*
91 ** Temporary files
92 */
93 static char infile[BUFSIZ];
94 static int unlink_infile  = 0;
95
96 static char outfile[BUFSIZ];
97 static int unlink_outfile = 0;
98
99 void unlink_done();
100
101 /* mhoutsbr.c */
102 int output_message(CT, char *);
103 int output_message_fp(CT, FILE *, char*);
104
105 /* mhmisc.c */
106 void set_endian(void);
107
108 /* mhfree.c */
109 void free_content(CT);
110
111
112 int
113 main(int argc, char **argv)
114 {
115         char *cp, buf[BUFSIZ];
116         char buffer[BUFSIZ], *compfile = NULL;
117         char **argp, **arguments;
118         CT ct;
119         FILE *fp = NULL;
120         FILE *fp_out = NULL;
121
122         if (atexit(unlink_done) != 0) {
123                 adios(EX_OSERR, NULL, "atexit failed");
124         }
125
126         setlocale(LC_ALL, "");
127         invo_name = mhbasename(argv[0]);
128
129         /* read user profile/context */
130         context_read();
131
132         arguments = getarguments(invo_name, argc, argv, 1);
133         argp = arguments;
134
135         while ((cp = *argp++)) {
136                 if (cp[0] == '-' && cp[1] == '\0') {
137                         if (compfile)
138                                 adios(EX_USAGE, NULL, "cannot specify both standard input and a file");
139                         else
140                                 compfile = cp;
141                         verbosw = 0;  /* turn off -verbose listings */
142                         break;
143                 }
144                 if (*cp == '-') {
145                         switch (smatch(++cp, switches)) {
146                         case AMBIGSW:
147                                 ambigsw(cp, switches);
148                                 exit(EX_USAGE);
149                         case UNKWNSW:
150                                 adios(EX_USAGE, NULL, "-%s unknown", cp);
151
152                         case HELPSW:
153                                 snprintf(buf, sizeof(buf), "%s [switches] file", invo_name);
154                                 print_help(buf, switches, 1);
155                                 exit(argc == 2 ? EX_OK : EX_USAGE);
156                         case VERSIONSW:
157                                 print_version(invo_name);
158                                 exit(argc == 2 ? EX_OK : EX_USAGE);
159
160                         case VERBSW:
161                                 verbosw++;
162                                 continue;
163                         case NVERBSW:
164                                 verbosw = 0;
165                                 continue;
166                         case DEBUGSW:
167                                 debugsw = 1;
168                                 continue;
169                         }
170                 }
171                 if (compfile)
172                         adios(EX_USAGE, NULL, "only one composition file allowed");
173                 else
174                         compfile = cp;
175         }
176
177         set_endian();
178
179         /*
180         ** Check if we've specified an additional profile
181         */
182         if ((cp = getenv("MHBUILD"))) {
183                 if ((fp = fopen(cp, "r"))) {
184                         readconfig((struct node **) 0, fp, cp, 0);
185                         fclose(fp);
186                 } else {
187                         admonish("", "unable to read $MHBUILD profile (%s)",
188                                         cp);
189                 }
190         }
191
192         /*
193         ** Read the standard profile setup
194         */
195         if ((fp = fopen(cp = etcpath("mhn.defaults"), "r"))) {
196                 readconfig((struct node **) 0, fp, cp, 0);
197                 fclose(fp);
198         }
199
200         /*
201         ** Check for storage directory.  If defined, we
202         ** will store temporary files there.  Else we
203         ** store them in standard nmh directory.
204         */
205         if ((cp = context_find(nmhstorage)) && *cp)
206                 tmp = concat(cp, "/", invo_name, NULL);
207         else
208                 tmp = mh_xstrdup(toabsdir(invo_name));
209
210         /* Check if we have a file to process */
211         if (!compfile)
212                 adios(EX_USAGE, NULL, "need to specify a %s composition file",
213                                 invo_name);
214
215         /*
216         ** Process the composition file from standard input.
217         */
218         if (compfile[0] == '-' && compfile[1] == '\0') {
219                 /* copy standard input to temporary file */
220                 strncpy(infile, m_mktemp(invo_name, NULL, &fp),
221                                 sizeof(infile));
222                 while (fgets(buffer, BUFSIZ, stdin))
223                         fputs(buffer, fp);
224                 fclose(fp);
225                 unlink_infile = 1;
226
227                 /* build the content structures for MIME message */
228                 ct = build_mime(infile);
229
230                 /* output MIME message to this temporary file */
231                 strncpy(outfile, m_mktemp(invo_name, NULL, &fp_out),
232                                 sizeof(outfile));
233                 unlink_outfile = 1;
234
235                 /* output the message */
236                 output_message_fp(ct, fp_out, outfile);
237                 fclose(fp_out);
238
239                 /* output the temp file to standard output */
240                 if ((fp = fopen(outfile, "r")) == NULL)
241                         adios(EX_IOERR, outfile, "unable to open");
242                 while (fgets(buffer, BUFSIZ, fp))
243                         fputs(buffer, stdout);
244                 fclose(fp);
245
246                 unlink(infile);
247                 unlink_infile = 0;
248
249                 unlink(outfile);
250                 unlink_outfile = 0;
251
252                 free_content(ct);
253                 exit(EX_OK);
254         }
255
256         /*
257         ** Process the composition file from a file.
258         */
259
260         /* build the content structures for MIME message */
261         ct = build_mime(compfile);
262
263         /* output MIME message to this temporary file */
264         strncpy(outfile, m_mktemp2(compfile, invo_name, NULL, &fp_out),
265                         sizeof(outfile));
266         unlink_outfile = 1;
267
268         /* output the message */
269         output_message_fp(ct, fp_out, outfile);
270         fclose(fp_out);
271
272         /* Rename composition draft */
273         snprintf(buffer, sizeof(buffer), "%s.orig", compfile);
274         if (rename(compfile, buffer) == NOTOK) {
275                 adios(EX_IOERR, buffer, "unable to rename draft %s to", compfile);
276         }
277
278         /* Rename output file to take its place */
279         if (rename(outfile, compfile) == NOTOK) {
280                 advise(compfile, "unable to rename output %s to", outfile);
281                 rename(buffer, compfile);
282                 exit(EX_IOERR);
283         }
284         unlink_outfile = 0;
285
286         free_content(ct);
287         return 0;
288 }
289
290
291 void
292 unlink_done()
293 {
294         /*
295         ** Check if we need to remove stray temporary files.
296         */
297         if (unlink_infile) {
298                 unlink(infile);
299         }
300         if (unlink_outfile) {
301                 unlink(outfile);
302         }
303 }
304
305 /*
306 ** Main routine for translating composition file
307 ** into valid MIME message.  It translates the draft
308 ** into a content structure (actually a tree of content
309 ** structures).  This message then can be manipulated
310 ** in various ways, including being output via
311 ** output_message().
312 */
313 static CT
314 build_mime(char *infile)
315 {
316         enum state state;
317         struct field f = {{0}};
318         int compnum;
319         char buf[BUFSIZ];
320         char *cp, *np, *vp;
321         struct multipart *m;
322         struct part **pp;
323         CT ct;
324         FILE *in;
325         HF hp;
326
327         umask(~m_gmprot());
328
329         /* open the composition draft */
330         if ((in = fopen(infile, "r")) == NULL) {
331                 adios(EX_IOERR, infile, "unable to open for reading");
332         }
333
334         /*
335         ** Allocate space for primary (outside) content
336         */
337         ct = mh_xcalloc(1, sizeof(*ct));
338
339         /*
340         ** Allocate structure for handling decoded content
341         ** for this part.  We don't really need this, but
342         ** allocate it to remain consistent.
343         */
344         init_decoded_content(ct);
345
346         /*
347         ** Parse some of the header fields in the composition
348         ** draft into the linked list of header fields for
349         ** the new MIME message.
350         */
351         for (compnum = 1, state = FLD2;;) {
352                 switch (state = m_getfld2(state, &f, in)) {
353                 case FLD2:
354                         compnum++;
355
356                         /* abort if draft has Mime-Version header field */
357                         if (!mh_strcasecmp(f.name, VRSN_FIELD)) {
358                                 adios(EX_CONFIG, NULL, "draft shouldn't contain %s: field", VRSN_FIELD);
359                         }
360
361                         /*
362                         ** abort if draft has Content-Transfer-Encoding
363                         ** header field
364                         */
365                         if (!mh_strcasecmp(f.name, ENCODING_FIELD)) {
366                                 adios(EX_CONFIG, NULL, "draft shouldn't contain %s: field", ENCODING_FIELD);
367                         }
368
369                         /* ignore any Content-Type fields in the header */
370                         if (!mh_strcasecmp(f.name, TYPE_FIELD)) {
371                                 continue;
372                         }
373
374                         /* add the header data to the list */
375                         add_header(ct, mh_xstrdup(f.name), mh_xstrdup(f.value));
376
377                         continue;
378
379                 case BODY2:
380                         fseek(in, (long) (-strlen(f.value)), SEEK_CUR);
381                         break;
382
383                 case FILEEOF2:
384                         adios(EX_CONFIG, NULL, "draft has empty body -- no directives!");
385                         /* NOTREACHED */
386
387                 case LENERR2:
388                 case FMTERR2:
389                 case IOERR2:
390                         adios(EX_CONFIG, NULL, "message format error in component #%d",
391                                         compnum);
392
393                 default:
394                         adios(EX_SOFTWARE, NULL, "getfld() returned %d", state);
395                 }
396                 break;
397         }
398
399         /*
400         ** Iterate through the list of headers and call the function to
401         ** MIME-ify them if required.
402         */
403         for (hp = ct->c_first_hf; hp != NULL; hp = hp->next) {
404                 if (encode_rfc2047(hp->name, &hp->value, NULL)) {
405                         adios(EX_DATAERR, NULL, "Unable to encode header \"%s\"", hp->name);
406                 }
407         }
408
409         /*
410         ** Now add the MIME-Version header field
411         ** to the list of header fields.
412         */
413         np = mh_xstrdup(VRSN_FIELD);
414         vp = concat(" ", VRSN_VALUE, "\n", NULL);
415         add_header(ct, np, vp);
416
417         /*
418         ** We initially assume we will find multiple contents in the
419         ** draft.  So create a multipart/mixed content to hold everything.
420         ** We can remove this later, if it is not needed.
421         */
422         if (get_ctinfo("multipart/mixed", ct, 0) == NOTOK) {
423                 exit(EX_DATAERR);
424         }
425         ct->c_type = CT_MULTIPART;
426         ct->c_subtype = MULTI_MIXED;
427         ct->c_file = mh_xstrdup(infile);
428
429         m = (struct multipart *) mh_xcalloc(1, sizeof(*m));
430         ct->c_ctparams = (void *) m;
431         pp = &m->mp_parts;
432
433         /*
434         ** read and parse the composition file
435         ** and the directives it contains.
436         */
437         while (fgetstr(buf, sizeof(buf) - 1, in)) {
438                 struct part *part;
439                 CT p;
440
441                 if (user_content(in, infile, buf, &p) == DONE) {
442                         admonish(NULL, "ignoring spurious #end");
443                         continue;
444                 }
445                 if (!p)
446                         continue;
447
448                 part = mh_xcalloc(1, sizeof(*part));
449                 *pp = part;
450                 pp = &part->mp_next;
451                 part->mp_part = p;
452         }
453
454         /*
455         ** close the composition draft since
456         ** it's not needed any longer.
457         */
458         fclose(in);
459
460         /* check if any contents were found */
461         if (!m->mp_parts)
462                 adios(EX_OSERR, NULL, "no content directives found");
463
464         /*
465         ** If only one content was found, then remove and
466         ** free the outer multipart content.
467         */
468         if (!m->mp_parts->mp_next) {
469                 CT p;
470
471                 p = m->mp_parts->mp_part;
472                 m->mp_parts->mp_part = NULL;
473
474                 /* move header fields */
475                 p->c_first_hf = ct->c_first_hf;
476                 p->c_last_hf = ct->c_last_hf;
477                 ct->c_first_hf = NULL;
478                 ct->c_last_hf = NULL;
479
480                 free_content(ct);
481                 ct = p;
482         } else {
483                 set_id(ct, 1);
484         }
485
486         /*
487         ** Fill out, or expand directives.  Parse and execute
488         ** commands specified by profile composition strings.
489         */
490         compose_content(ct);
491
492         if ((cp = strchr(prefix, 'a')) == NULL)
493                 adios(EX_SOFTWARE, NULL, "internal error(4)");
494
495         /*
496         ** Scan the contents.  Choose a transfer encoding, and
497         ** check if prefix for multipart boundary clashes with
498         ** any of the contents.
499         */
500         while (scan_content(ct) == NOTOK) {
501                 if (*cp < 'z') {
502                         (*cp)++;
503                 } else {
504                         if (*++cp == 0)
505                                 adios(EX_SOFTWARE, NULL, "giving up trying to find a unique delimiter string");
506                                 else
507                                 (*cp)++;
508                 }
509         }
510
511         /* Build the rest of the header field structures */
512         build_headers(ct);
513
514         return ct;
515 }
516
517
518 /*
519 ** Set up structures for placing unencoded
520 ** content when building parts.
521 */
522
523 static int
524 init_decoded_content(CT ct)
525 {
526         CE ce;
527
528         ce = mh_xcalloc(1, sizeof(*ce));
529
530         ct->c_cefile     = ce;
531         ct->c_ceopenfnx  = open7Bit;  /* since unencoded */
532         ct->c_ceclosefnx = close_encoding;
533         ct->c_cesizefnx  = NULL;  /* since unencoded */
534
535         return OK;
536 }
537
538
539 static char *
540 fgetstr(char *s, int n, FILE *stream)
541 {
542         char *cp, *ep;
543
544         for (ep = (cp = s) + n; cp < ep; ) {
545                 int i;
546
547                 if (!fgets(cp, n, stream))
548                         return (cp != s ? s : NULL);
549                 if (cp == s && *cp != '#')
550                         return s;
551
552                 cp += (i = strlen(cp)) - 1;
553                 if (i <= 1 || *cp-- != '\n' || *cp != '\\')
554                         break;
555                 *cp = '\0';
556                 n -= (i - 2);
557         }
558
559         return s;
560 }
561
562
563 /*
564 ** Parse the composition draft for text and directives.
565 ** Do initial setup of Content structure.
566 */
567
568 static int
569 user_content(FILE *in, char *file, char *buf, CT *ctp)
570 {
571         int vrsn;
572         unsigned char *cp;
573         char **ap;
574         char buffer[BUFSIZ];
575         struct multipart *m;
576         struct part **pp;
577         struct str2init *s2i;
578         CI ci;
579         CT ct;
580         CE ce;
581
582         if (buf[0] == '\n' || strcmp(buf, "#\n") == 0) {
583                 *ctp = NULL;
584                 return OK;
585         }
586
587         /* allocate basic Content structure */
588         ct = mh_xcalloc(1, sizeof(*ct));
589         *ctp = ct;
590
591         /* allocate basic structure for handling decoded content */
592         init_decoded_content(ct);
593         ce = ct->c_cefile;
594
595         ci = &ct->c_ctinfo;
596         set_id(ct, 0);
597
598         /*
599         ** Handle inline text.  Check if line
600         ** is one of the following forms:
601         **
602         ** 1) doesn't begin with '#'  (implicit directive)
603         ** 2) begins with "##"        (implicit directive)
604         ** 3) begins with "#<"
605         */
606         if (buf[0] != '#' || buf[1] == '#' || buf[1] == '<') {
607                 int headers;
608                 int inlineD;
609                 long pos;
610                 char content[BUFSIZ];
611                 FILE *out;
612                 char *cp;
613
614                 cp = m_mktemp2(NULL, invo_name, NULL, &out);
615                 if (cp == NULL)
616                         adios(EX_CANTCREAT, "mhbuild", "unable to create temporary file");
617
618                 /* use a temp file to collect the plain text lines */
619                 ce->ce_file = mh_xstrdup(cp);
620                 ce->ce_unlink = 1;
621
622                 if (buf[0] == '#' && buf[1] == '<') {
623                         strncpy(content, buf + 2, sizeof(content));
624                         inlineD = 1;
625                         goto rock_and_roll;
626                 } else {
627                         inlineD = 0;
628                 }
629
630                 /* the directive is implicit */
631                 strncpy(content, "text/plain", sizeof(content));
632                 headers = 0;
633                 strncpy(buffer, buf[0] != '#' ? buf : buf + 1, sizeof(buffer));
634                 for (;;) {
635                         int i;
636
637                         if (headers >= 0 && uprf(buffer, DESCR_FIELD) &&
638                                         buffer[i=strlen(DESCR_FIELD)] == ':') {
639                                 headers = 1;
640
641 again_descr:
642                                 ct->c_descr = add(buffer + i + 1, ct->c_descr);
643                                 if (!fgetstr(buffer, sizeof(buffer) - 1, in))
644                                         adios(EX_DATAERR, NULL, "end-of-file after %s: field in plaintext", DESCR_FIELD);
645                                 switch (buffer[0]) {
646                                 case ' ':
647                                 case '\t':
648                                         i = -1;
649                                         goto again_descr;
650
651                                 case '#':
652                                         adios(EX_DATAERR, NULL, "#-directive after %s: field in plaintext", DESCR_FIELD);
653                                         /* NOTREACHED */
654
655                                 default:
656                                         break;
657                                 }
658                         }
659
660                         if (headers >= 0 && uprf(buffer, DISPO_FIELD)
661                                 && buffer[i = strlen(DISPO_FIELD)] == ':') {
662                                 headers = 1;
663
664 again_dispo:
665                                 ct->c_dispo = add(buffer + i + 1, ct->c_dispo);
666                                 if (!fgetstr(buffer, sizeof(buffer) - 1, in))
667                                         adios(EX_DATAERR, NULL, "end-of-file after %s: field in plaintext", DISPO_FIELD);
668                                 switch (buffer[0]) {
669                                 case ' ':
670                                 case '\t':
671                                         i = -1;
672                                         goto again_dispo;
673
674                                 case '#':
675                                         adios(EX_DATAERR, NULL, "#-directive after %s: field in plaintext", DISPO_FIELD);
676                                         /* NOTREACHED */
677
678                                 default:
679                                         break;
680                                 }
681                         }
682
683                         if (headers != 1 || buffer[0] != '\n')
684                                 fputs(buffer, out);
685
686 rock_and_roll:
687                         headers = -1;
688                         pos = ftell(in);
689                         if ((cp = fgetstr(buffer, sizeof(buffer) - 1, in))
690                                         == NULL)
691                                 break;
692                         if (buffer[0] == '#') {
693                                 char *bp;
694
695                                 if (buffer[1] != '#')
696                                         break;
697                                 for (cp = (bp = buffer) + 1; *cp; cp++)
698                                         *bp++ = *cp;
699                                 *bp = '\0';
700                         }
701                 }
702
703                 fclose(out);
704
705                 /* parse content type */
706                 if (get_ctinfo(content, ct, inlineD) == NOTOK)
707                         exit(EX_DATAERR);
708
709                 for (s2i = str2cts; s2i->si_key; s2i++)
710                         if (!mh_strcasecmp(ci->ci_type, s2i->si_key))
711                                 break;
712                 if (!s2i->si_key && !uprf(ci->ci_type, "X-"))
713                         s2i++;
714
715                 /*
716                 ** check type specified (possibly implicitly)
717                 */
718                 switch (ct->c_type = s2i->si_val) {
719                 case CT_MESSAGE:
720                         if (!mh_strcasecmp(ci->ci_subtype, "rfc822")) {
721                                 ct->c_encoding = CE_7BIT;
722                                 goto call_init;
723                         }
724                         /* else fall... */
725                 case CT_MULTIPART:
726                         adios(EX_DATAERR, NULL, "it doesn't make sense to define an in-line %s content",
727                                         ct->c_type == CT_MESSAGE ? "message" :
728                                         "multipart");
729                         /* NOTREACHED */
730
731                 default:
732 call_init:
733                         if ((ct->c_ctinitfnx = s2i->si_init))
734                                 (*ct->c_ctinitfnx) (ct);
735                         break;
736                 }
737
738                 if (cp)
739                         fseek(in, pos, SEEK_SET);
740                 return OK;
741         }
742
743         /*
744         ** If we've reached this point, the next line
745         ** must be some type of explicit directive.
746         */
747
748         if (buf[1] == '@') {
749                 adios(EX_DATAERR, NULL, "The #@ directive i.e. message/external-body "
750                                 "is not supported anymore.");
751         }
752
753         /* parse directive */
754         if (get_ctinfo(buf+1, ct, 1) == NOTOK)
755                 exit(EX_DATAERR);
756
757         /* check directive against the list of MIME types */
758         for (s2i = str2cts; s2i->si_key; s2i++)
759                 if (!mh_strcasecmp(ci->ci_type, s2i->si_key))
760                         break;
761
762         /*
763         ** Check if the directive specified a valid type.
764         ** This will happen if it was one of the following forms:
765         **
766         **    #type/subtype
767         */
768         if (s2i->si_key) {
769                 if (!ci->ci_subtype)
770                         adios(EX_DATAERR, NULL, "missing subtype in \"#%s\"", ci->ci_type);
771
772                 switch (ct->c_type = s2i->si_val) {
773                 case CT_MULTIPART:
774                         adios(EX_DATAERR, NULL, "use \"#begin ... #end\" instead of \"#%s/%s\"", ci->ci_type, ci->ci_subtype);
775                         /* NOTREACHED */
776
777                 case CT_MESSAGE:
778                         if (!mh_strcasecmp(ci->ci_subtype, "partial") ||
779                                         !mh_strcasecmp(ci->ci_subtype,
780                                         "external-body")) {
781                                 adios(EX_DATAERR, NULL, "sorry, \"#%s/%s\" isn't supported", ci->ci_type, ci->ci_subtype);
782                         }
783 use_forw:
784                         admonish(NULL, "use \"#forw [+folder] [msgs]\" instead of \"#%s/%s\"", ci->ci_type, ci->ci_subtype);
785                         /* FALL */
786
787                 default:
788                         if ((ct->c_ctinitfnx = s2i->si_init))
789                                 (*ct->c_ctinitfnx) (ct);
790                         break;
791                 }
792
793                 /* Handle [file] argument */
794                 if (ci->ci_magic) {
795                         /* check if specifies command to execute */
796                         if (*ci->ci_magic == '|' || *ci->ci_magic == '!') {
797                                 for (cp = ci->ci_magic + 1; isspace(*cp); cp++)
798                                         continue;
799                                 if (!*cp)
800                                         adios(EX_DATAERR, NULL, "empty pipe command for #%s directive", ci->ci_type);
801                                 cp = mh_xstrdup(cp);
802                                 mh_free0(&(ci->ci_magic));
803                                 ci->ci_magic = cp;
804                         } else {
805                                 /* record filename of decoded contents */
806                                 ce->ce_file = ci->ci_magic;
807                                 if (access(ce->ce_file, R_OK) == NOTOK)
808                                         adios(EX_IOERR, "reading", "unable to access %s for", ce->ce_file);
809                                 ci->ci_magic = NULL;
810                         }
811                         return OK;
812                 }
813
814                 /*
815                 ** No [file] argument, so check profile for
816                 ** method to compose content.
817                 */
818                 snprintf(buffer, sizeof(buffer), "%s-compose-%s/%s",
819                                 invo_name, ci->ci_type, ci->ci_subtype);
820                 if ((cp = context_find(buffer)) == NULL || *cp == '\0') {
821                         snprintf(buffer, sizeof(buffer), "%s-compose-%s",
822                                         invo_name, ci->ci_type);
823                         if ((cp = context_find(buffer)) == NULL ||
824                                         *cp == '\0') {
825                                 content_error(NULL, ct, "don't know how to compose content");
826                                 exit(EX_CONFIG);
827                         }
828                 }
829                 ci->ci_magic = mh_xstrdup(cp);
830                 return OK;
831         }
832
833         /*
834         ** Message directive
835         ** #forw [+folder] [msgs]
836         */
837         if (!mh_strcasecmp(ci->ci_type, "forw")) {
838                 int msgnum;
839                 char *folder, *arguments[MAXARGS];
840                 struct msgs *mp;
841
842                 if (ci->ci_magic) {
843                         int i;
844
845                         ap = brkstring(ci->ci_magic, " ", "\n");
846                         for (i=0; ap[i] && i<MAXARGS-1; i++) {
847                                 arguments[i] = ap[i];
848                         }
849                         arguments[i] = NULL;
850
851                 } else {
852                         arguments[0] = seq_cur;
853                         arguments[1] = NULL;
854                 }
855                 folder = NULL;
856
857                 /* search the arguments for a folder name */
858                 for (ap = arguments; *ap; ap++) {
859                         cp = *ap;
860                         if (*cp == '+' || *cp == '@') {
861                                 if (folder)
862                                         adios(EX_USAGE, NULL, "only one folder per #forw directive");
863                                 else
864                                         folder = mh_xstrdup(expandfol(cp));
865                         }
866                 }
867
868                 /* else, use the current folder */
869                 if (!folder)
870                         folder = mh_xstrdup(getcurfol());
871
872                 if (!(mp = folder_read(folder)))
873                         adios(EX_IOERR, NULL, "unable to read folder %s", folder);
874                 for (ap = arguments; *ap; ap++) {
875                         cp = *ap;
876                         if (*cp != '+' && *cp != '@')
877                                 if (!m_convert(mp, cp))
878                                         exit(EX_USAGE);
879                 }
880                 mh_free0(&folder);
881                 free_ctinfo(ct);
882
883                 /*
884                 ** If there is more than one message to include, make this
885                 ** a content of type "multipart/digest" and insert each message
886                 ** as a subpart.  If there is only one message, then make this
887                 ** a content of type "message/rfc822".
888                 */
889                 if (mp->numsel > 1) {
890                         /* we are forwarding multiple messages */
891                         if (get_ctinfo("multipart/digest", ct, 0) == NOTOK)
892                                 exit(EX_DATAERR);
893                         ct->c_type = CT_MULTIPART;
894                         ct->c_subtype = MULTI_DIGEST;
895
896                         m = mh_xcalloc(1, sizeof(*m));
897                         ct->c_ctparams = (void *) m;
898                         pp = &m->mp_parts;
899
900                         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
901                                 if (is_selected(mp, msgnum)) {
902                                         struct part *part;
903                                         CT p;
904                                         CE pe;
905
906                                         p = mh_xcalloc(1, sizeof(*p));
907                                         init_decoded_content(p);
908                                         pe = p->c_cefile;
909                                         if (get_ctinfo("message/rfc822", p, 0)
910                                                         == NOTOK)
911                                                 exit(EX_DATAERR);
912                                         p->c_type = CT_MESSAGE;
913                                         p->c_subtype = MESSAGE_RFC822;
914
915                                         snprintf(buffer, sizeof(buffer),
916                                                         "%s/%d", mp->foldpath,
917                                                         msgnum);
918                                         pe->ce_file = mh_xstrdup(buffer);
919
920                                         part = mh_xcalloc(1, sizeof(*part));
921                                         *pp = part;
922                                         pp = &part->mp_next;
923                                         part->mp_part = p;
924                                 }
925                         }
926                 } else {
927                         /* we are forwarding one message */
928                         if (get_ctinfo("message/rfc822", ct, 0) == NOTOK)
929                                 exit(EX_DATAERR);
930                         ct->c_type = CT_MESSAGE;
931                         ct->c_subtype = MESSAGE_RFC822;
932
933                         msgnum = mp->lowsel;
934                         snprintf(buffer, sizeof(buffer), "%s/%d",
935                                         mp->foldpath, msgnum);
936                         ce->ce_file = mh_xstrdup(buffer);
937                 }
938
939                 folder_free(mp);  /* free folder/message structure */
940                 return OK;
941         }
942
943         /*
944         ** #end
945         */
946         if (!mh_strcasecmp(ci->ci_type, "end")) {
947                 free_content(ct);
948                 *ctp = NULL;
949                 return DONE;
950         }
951
952         /*
953         ** #begin [ alternative | parallel ]
954         */
955         if (!mh_strcasecmp(ci->ci_type, "begin")) {
956                 if (!ci->ci_magic) {
957                         vrsn = MULTI_MIXED;
958                         cp = SubMultiPart[vrsn - 1].kv_key;
959                 } else if (!mh_strcasecmp(ci->ci_magic, "alternative")) {
960                         vrsn = MULTI_ALTERNATE;
961                         cp = SubMultiPart[vrsn - 1].kv_key;
962                 } else if (!mh_strcasecmp(ci->ci_magic, "parallel")) {
963                         vrsn = MULTI_PARALLEL;
964                         cp = SubMultiPart[vrsn - 1].kv_key;
965                 } else if (uprf(ci->ci_magic, "digest")) {
966                         goto use_forw;
967                 } else {
968                         vrsn = MULTI_UNKNOWN;
969                         cp = ci->ci_magic;
970                 }
971
972                 free_ctinfo(ct);
973                 snprintf(buffer, sizeof(buffer), "multipart/%s", cp);
974                 if (get_ctinfo(buffer, ct, 0) == NOTOK)
975                         exit(EX_DATAERR);
976                 ct->c_type = CT_MULTIPART;
977                 ct->c_subtype = vrsn;
978
979                 m = mh_xcalloc(1, sizeof(*m));
980                 ct->c_ctparams = (void *) m;
981
982                 pp = &m->mp_parts;
983                 while (fgetstr(buffer, sizeof(buffer) - 1, in)) {
984                         struct part *part;
985                         CT p;
986
987                         if (user_content(in, file, buffer, &p) == DONE) {
988                                 if (!m->mp_parts)
989                                         adios(EX_DATAERR, NULL, "empty \"#begin ... #end\" sequence");
990                                 return OK;
991                         }
992                         if (!p)
993                                 continue;
994
995                         part = mh_xcalloc(1, sizeof(*part));
996                         *pp = part;
997                         pp = &part->mp_next;
998                         part->mp_part = p;
999                 }
1000                 admonish(NULL, "premature end-of-file, missing #end");
1001                 return OK;
1002         }
1003
1004         /*
1005         ** Unknown directive
1006         */
1007         adios(EX_DATAERR, NULL, "unknown directive \"#%s\"", ci->ci_type);
1008         return NOTOK;  /* NOT REACHED */
1009 }
1010
1011
1012 static void
1013 set_id(CT ct, int top)
1014 {
1015         char msgid[BUFSIZ];
1016         static int partno;
1017         static time_t clock = 0;
1018         static char *msgfmt;
1019
1020         if (clock == 0) {
1021                 time(&clock);
1022                 snprintf(msgid, sizeof(msgid), "<%d.%ld.%%d@%s>\n",
1023                                 (int) getpid(), (long) clock, LocalName());
1024                 partno = 0;
1025                 msgfmt = mh_xstrdup(msgid);
1026         }
1027         snprintf(msgid, sizeof(msgid), msgfmt, top ? 0 : ++partno);
1028         ct->c_id = mh_xstrdup(msgid);
1029 }
1030
1031
1032 /*
1033 ** Fill out, or expand the various contents in the composition
1034 ** draft.  Read-in any necessary files.  Parse and execute any
1035 ** commands specified by profile composition strings.
1036 */
1037
1038 static int
1039 compose_content(CT ct)
1040 {
1041         CE ce = ct->c_cefile;
1042
1043         switch (ct->c_type) {
1044         case CT_MULTIPART:
1045         {
1046                 int partnum;
1047                 char *pp;
1048                 char partnam[BUFSIZ];
1049                 struct multipart *m = (struct multipart *) ct->c_ctparams;
1050                 struct part *part;
1051
1052                 if (ct->c_partno) {
1053                         snprintf(partnam, sizeof(partnam), "%s.",
1054                                         ct->c_partno);
1055                         pp = partnam + strlen(partnam);
1056                 } else {
1057                         pp = partnam;
1058                 }
1059
1060                 /* first, we call compose_content on all the subparts */
1061                 for (part = m->mp_parts, partnum = 1; part;
1062                                 part = part->mp_next, partnum++) {
1063                         CT p = part->mp_part;
1064
1065                         sprintf(pp, "%d", partnum);
1066                         p->c_partno = mh_xstrdup(partnam);
1067                         if (compose_content(p) == NOTOK)
1068                                 return NOTOK;
1069                 }
1070         }
1071         break;
1072
1073         case CT_MESSAGE:
1074                 /* Nothing to do for type message */
1075                 break;
1076
1077         /*
1078         ** Discrete types (text/application/audio/image/video)
1079         */
1080         default:
1081                 if (!ce->ce_file) {
1082                         pid_t child_id;
1083                         int xstdout, len, buflen;
1084                         char *bp, **ap, *cp;
1085                         char *vec[4], buffer[BUFSIZ];
1086                         FILE *out;
1087                         CI ci = &ct->c_ctinfo;
1088                         char *tfile = NULL;
1089
1090                         if (!(cp = ci->ci_magic))
1091                                 adios(EX_SOFTWARE, NULL, "internal error(5)");
1092
1093                         tfile = m_mktemp2(NULL, invo_name, NULL, NULL);
1094                         if (tfile == NULL) {
1095                                 adios(EX_CANTCREAT, "mhbuild", "unable to create temporary file");
1096                         }
1097                         ce->ce_file = mh_xstrdup(tfile);
1098                         ce->ce_unlink = 1;
1099
1100                         xstdout = 0;
1101
1102                         /* Get buffer ready to go */
1103                         bp = buffer;
1104                         bp[0] = '\0';
1105                         buflen = sizeof(buffer);
1106
1107                         /*
1108                         ** Parse composition string into buffer
1109                         */
1110                         for ( ; *cp; cp++) {
1111                                 if (*cp == '%') {
1112                                         switch (*++cp) {
1113                                         case 'a':
1114                                         {
1115                                                 /*
1116                                                 ** insert parameters from
1117                                                 ** directive
1118                                                 */
1119                                                 char **ep;
1120                                                 char *s = "";
1121
1122                                                 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
1123                                                         snprintf(bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
1124                                                         len = strlen(bp);
1125                                                         bp += len;
1126                                                         buflen -= len;
1127                                                         s = " ";
1128                                                 }
1129                                         }
1130                                         break;
1131
1132                                         case 'F':
1133                                                 /* %f, and stdout is not-redirected */
1134                                                 xstdout = 1;
1135                                                 /* and fall... */
1136
1137                                         case 'f':
1138                                                 /*
1139                                                 ** insert temporary filename
1140                                                 ** where content should be
1141                                                 ** written
1142                                                 */
1143                                                 snprintf(bp, buflen, "%s", ce->ce_file);
1144                                                 break;
1145
1146                                         case 's':
1147                                                 /* insert content subtype */
1148                                                 strncpy(bp, ci->ci_subtype, buflen);
1149                                                 break;
1150
1151                                         case '%':
1152                                                 /* insert character % */
1153                                                 goto raw;
1154
1155                                         default:
1156                                                 *bp++ = *--cp;
1157                                                 *bp = '\0';
1158                                                 buflen--;
1159                                                 continue;
1160                                         }
1161                                         len = strlen(bp);
1162                                         bp += len;
1163                                         buflen -= len;
1164                                 } else {
1165 raw:
1166                                         *bp++ = *cp;
1167                                         *bp = '\0';
1168                                         buflen--;
1169                                 }
1170                         }
1171
1172                         if (verbosw)
1173                                 printf("composing content %s/%s from command\n\t%s\n", ci->ci_type, ci->ci_subtype, buffer);
1174
1175                         fflush(stdout);  /* not sure if need for -noverbose */
1176
1177                         vec[0] = "/bin/sh";
1178                         vec[1] = "-c";
1179                         vec[2] = buffer;
1180                         vec[3] = NULL;
1181
1182                         if ((out = fopen(ce->ce_file, "w")) == NULL)
1183                                 adios(EX_IOERR, ce->ce_file, "unable to open for writing");
1184
1185                         switch (child_id = fork()) {
1186                         case NOTOK:
1187                                 adios(EX_OSERR, "fork", "unable to fork");
1188                                 /* NOTREACHED */
1189
1190                         case OK:
1191                                 if (!xstdout)
1192                                         dup2(fileno(out), 1);
1193                                 close(fileno(out));
1194                                 execvp("/bin/sh", vec);
1195                                 fprintf(stderr, "unable to exec ");
1196                                 perror("/bin/sh");
1197                                 _exit(EX_OSERR);
1198                                 /* NOTREACHED */
1199
1200                         default:
1201                                 fclose(out);
1202                                 if (pidXwait(child_id, NULL))
1203                                         exit(EX_SOFTWARE);
1204                                 break;
1205                         }
1206                 }
1207                 break;
1208         }
1209
1210         return OK;
1211 }
1212
1213
1214 /*
1215 ** Scan the content.
1216 **
1217 **    1) choose a transfer encoding.
1218 **    2) check for clashes with multipart boundary string.
1219 **    3) for text content, figure out which character set is being used.
1220 **
1221 ** If there is a clash with one of the contents and the multipart boundary,
1222 ** this function will exit with NOTOK.  This will cause the scanning process
1223 ** to be repeated with a different multipart boundary.  It is possible
1224 ** (although highly unlikely) that this scan will be repeated multiple times.
1225 */
1226
1227 static int
1228 scan_content(CT ct)
1229 {
1230         int len;
1231         int check8bit = 0, contains8bit = 0;
1232         int checklinelen = 0, linelen = 0;
1233         int checkboundary = 0, boundaryclash = 0;
1234         int checklinespace = 0, linespace = 0;  /* trailing whitespace */
1235         unsigned char *cp = NULL, buffer[BUFSIZ];
1236         struct text *t = NULL;
1237         FILE *in = NULL;
1238         CE ce = ct->c_cefile;
1239
1240         /*
1241         ** handle multipart by scanning all subparts
1242         ** and then checking their encoding.
1243         */
1244         if (ct->c_type == CT_MULTIPART) {
1245                 struct multipart *m = (struct multipart *) ct->c_ctparams;
1246                 struct part *part;
1247
1248                 /* initially mark the domain of enclosing multipart as 7bit */
1249                 ct->c_encoding = CE_7BIT;
1250
1251                 for (part = m->mp_parts; part; part = part->mp_next) {
1252                         CT p = part->mp_part;
1253
1254                         if (scan_content(p) == NOTOK) {
1255                                 /* choose encoding for subpart */
1256                                 return NOTOK;
1257                         }
1258
1259                         /*
1260                         ** if necessary, enlarge encoding for enclosing
1261                         ** multipart
1262                         */
1263                         if (p->c_encoding == CE_BINARY)
1264                                 ct->c_encoding = CE_BINARY;
1265                         if (p->c_encoding == CE_8BIT &&
1266                                         ct->c_encoding != CE_BINARY)
1267                                 ct->c_encoding = CE_8BIT;
1268                 }
1269
1270                 return OK;
1271         }
1272
1273         /*
1274         ** Decide what to check while scanning this content.
1275         */
1276         switch (ct->c_type) {
1277         case CT_TEXT:
1278                 check8bit = 1;
1279                 checkboundary = 1;
1280                 checklinelen = 1;
1281                 checklinespace = 1;
1282                 break;
1283
1284         case CT_MESSAGE:
1285                 check8bit = 0;
1286                 checklinelen = 0;
1287                 checklinespace = 0;
1288                 checkboundary = 1;
1289                 break;
1290
1291         case CT_APPLICATION:
1292         case CT_AUDIO:
1293         case CT_IMAGE:
1294         case CT_VIDEO:
1295                 check8bit = 0;
1296                 checklinelen = 0;
1297                 checklinespace = 0;
1298                 checkboundary = 0;
1299                 break;
1300         }
1301
1302         /*
1303         ** Scan the unencoded content
1304         */
1305         if (check8bit || checklinelen || checklinespace || checkboundary) {
1306                 if ((in = fopen(ce->ce_file, "r")) == NULL)
1307                         adios(EX_IOERR, ce->ce_file, "unable to open for reading");
1308                 len = strlen(prefix);
1309
1310                 while (fgets(buffer, sizeof(buffer) - 1, in)) {
1311                         if (check8bit) {
1312                                 for (cp = buffer; *cp; cp++) {
1313                                         if (!isascii(*cp)) {
1314                                                 contains8bit = 1;
1315                                                 /* no need to keep checking */
1316                                                 check8bit = 0;
1317                                         }
1318                                 }
1319                         }
1320
1321                         if (checklinelen && (strlen(buffer) > CPERLIN + 1)) {
1322                                 linelen = 1;
1323                                 checklinelen = 0;  /* no need to keep checking */
1324                         }
1325
1326                         if (checklinespace &&
1327                                         (cp = buffer + strlen(buffer) - 2) >
1328                                         buffer && isspace(*cp)) {
1329                                 linespace = 1;
1330                                 /* no need to keep checking */
1331                                 checklinespace = 0;
1332                         }
1333
1334                         /*
1335                         ** Check if content contains a line that clashes
1336                         ** with our standard boundary for multipart messages.
1337                         */
1338                         if (checkboundary && buffer[0] == '-' &&
1339                                         buffer[1] == '-') {
1340                                 for (cp = buffer + strlen(buffer) - 1;
1341                                                 cp >= buffer; cp--)
1342                                         if (!isspace(*cp))
1343                                                 break;
1344                                 *++cp = '\0';
1345                                 if (strncmp(buffer + 2, prefix, len)==0 &&
1346                                                 isdigit(buffer[2 + len])) {
1347                                         boundaryclash = 1;
1348                                         /* no need to keep checking */
1349                                         checkboundary = 0;
1350                                 }
1351                         }
1352                 }
1353                 fclose(in);
1354         }
1355
1356         /*
1357         ** Decide which transfer encoding to use.
1358         */
1359         switch (ct->c_type) {
1360         case CT_TEXT:
1361                 /*
1362                 ** If the text content didn't specify a character
1363                 ** set, we need to figure out which one was used.
1364                 */
1365                 t = (struct text *) ct->c_ctparams;
1366                 if (t->tx_charset == CHARSET_UNSPECIFIED) {
1367                         CI ci = &ct->c_ctinfo;
1368                         char **ap, **ep;
1369
1370                         for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++)
1371                                 continue;
1372
1373                         if (contains8bit) {
1374                                 t->tx_charset = CHARSET_UNKNOWN;
1375                                 *ap = concat("charset=", write_charset_8bit(),
1376                                                 NULL);
1377                         } else {
1378                                 t->tx_charset = CHARSET_USASCII;
1379                                 *ap = mh_xstrdup("charset=us-ascii");
1380                         }
1381
1382                         cp = strchr(*ap++, '=');
1383                         *ap = NULL;
1384                         *cp++ = '\0';
1385                         *ep = cp;
1386                 }
1387
1388                 if (contains8bit || linelen || linespace)
1389                         ct->c_encoding = CE_QUOTED;
1390                 else
1391                         ct->c_encoding = CE_7BIT;
1392                 break;
1393
1394         case CT_MESSAGE:
1395                 ct->c_encoding = CE_7BIT;
1396                 break;
1397
1398         case CT_APPLICATION:
1399         case CT_AUDIO:
1400         case CT_IMAGE:
1401         case CT_VIDEO:
1402                 /*
1403                 ** Forcing use of base64, because these types likely
1404                 ** contain binary data and NUL bytes. Don't care about
1405                 ** files that would be clean.
1406                 */
1407                 ct->c_encoding = CE_BASE64;
1408                 break;
1409         }
1410
1411         return (boundaryclash ? NOTOK : OK);
1412 }
1413
1414
1415 /*
1416 ** Scan the content structures, and build header
1417 ** fields that will need to be output into the
1418 ** message.
1419 */
1420
1421 static int
1422 build_headers(CT ct)
1423 {
1424         int cc, len;
1425         char **ap, **ep;
1426         char *np, *vp, buffer[BUFSIZ];
1427         CI ci = &ct->c_ctinfo;
1428
1429         /*
1430         ** If message is type multipart, then add the multipart
1431         ** boundary to the list of attribute/value pairs.
1432         */
1433         if (ct->c_type == CT_MULTIPART) {
1434                 char *cp;
1435                 static int level = 0;  /* store nesting level */
1436
1437                 ap = ci->ci_attrs;
1438                 ep = ci->ci_values;
1439                 snprintf(buffer, sizeof(buffer), "boundary=%s%d",
1440                                 prefix, level++);
1441                 cp = strchr(*ap++ = mh_xstrdup(buffer), '=');
1442                 *ap = NULL;
1443                 *cp++ = '\0';
1444                 *ep = cp;
1445         }
1446
1447         /*
1448         ** output the content type and subtype
1449         */
1450         np = mh_xstrdup(TYPE_FIELD);
1451         vp = concat(" ", ci->ci_type, "/", ci->ci_subtype, NULL);
1452
1453         /* keep track of length of line */
1454         len = strlen(TYPE_FIELD) + strlen(ci->ci_type) +
1455                         strlen(ci->ci_subtype) + 3;
1456
1457         /*
1458         ** Append the attribute/value pairs to
1459         ** the end of the Content-Type line.
1460         */
1461         for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
1462                 vp = add(";", vp);
1463                 len++;
1464
1465                 snprintf(buffer, sizeof(buffer), "%s=\"%s\"", *ap, *ep);
1466                 if (len + 1 + (cc = strlen(buffer)) >= CPERLIN) {
1467                         vp = add("\n\t", vp);
1468                         len = 8;
1469                 } else {
1470                         vp = add(" ", vp);
1471                         len++;
1472                 }
1473                 vp = add(buffer, vp);
1474                 len += cc;
1475         }
1476
1477         /*
1478         ** Append any RFC-822 comment to the end of
1479         ** the Content-Type line.
1480         */
1481         if (ci->ci_comment) {
1482                 snprintf(buffer, sizeof(buffer), "(%s)", ci->ci_comment);
1483                 if (len + 1 + (cc = 2 + strlen(ci->ci_comment)) >= CPERLIN) {
1484                         vp = add("\n\t", vp);
1485                         len = 8;
1486                 } else {
1487                         vp = add(" ", vp);
1488                         len++;
1489                 }
1490                 vp = add(buffer, vp);
1491                 len += cc;
1492         }
1493         vp = add("\n", vp);
1494         add_header(ct, np, vp);
1495
1496         /*
1497         ** output the Content-ID
1498         */
1499         if (ct->c_id) {
1500                 np = mh_xstrdup(ID_FIELD);
1501                 vp = concat(" ", ct->c_id, NULL);
1502                 add_header(ct, np, vp);
1503         }
1504
1505         /*
1506         ** output the Content-Description
1507         */
1508         if (ct->c_descr) {
1509                 np = mh_xstrdup(DESCR_FIELD);
1510                 vp = concat(" ", ct->c_descr, NULL);
1511                 if (encode_rfc2047(DESCR_FIELD, &vp, NULL)) {
1512                         adios(EX_DATAERR, NULL, "Unable to encode %s header", DESCR_FIELD);
1513                 }
1514                 add_header(ct, np, vp);
1515         }
1516
1517         /*
1518         ** output the Content-Disposition
1519         */
1520         if (ct->c_dispo) {
1521                 np = mh_xstrdup(DISPO_FIELD);
1522                 vp = concat(" ", ct->c_dispo, NULL);
1523                 add_header(ct, np, vp);
1524         }
1525
1526         /*
1527         ** output the Content-Transfer-Encoding
1528         */
1529         switch (ct->c_encoding) {
1530         case CE_7BIT:
1531                 /* Nothing to output */
1532                 break;
1533
1534         case CE_8BIT:
1535                 if (ct->c_type == CT_MESSAGE)
1536                         adios(EX_DATAERR, NULL, "internal error, invalid encoding");
1537
1538                 np = mh_xstrdup(ENCODING_FIELD);
1539                 vp = concat(" ", "8bit", "\n", NULL);
1540                 add_header(ct, np, vp);
1541                 break;
1542
1543         case CE_QUOTED:
1544                 if (ct->c_type == CT_MESSAGE || ct->c_type == CT_MULTIPART)
1545                         adios(EX_DATAERR, NULL, "internal error, invalid encoding");
1546
1547                 np = mh_xstrdup(ENCODING_FIELD);
1548                 vp = concat(" ", "quoted-printable", "\n", NULL);
1549                 add_header(ct, np, vp);
1550                 break;
1551
1552         case CE_BASE64:
1553                 if (ct->c_type == CT_MESSAGE || ct->c_type == CT_MULTIPART)
1554                         adios(EX_DATAERR, NULL, "internal error, invalid encoding");
1555
1556                 np = mh_xstrdup(ENCODING_FIELD);
1557                 vp = concat(" ", "base64", "\n", NULL);
1558                 add_header(ct, np, vp);
1559                 break;
1560
1561         case CE_BINARY:
1562                 if (ct->c_type == CT_MESSAGE)
1563                         adios(EX_DATAERR, NULL, "internal error, invalid encoding");
1564
1565                 np = mh_xstrdup(ENCODING_FIELD);
1566                 vp = concat(" ", "binary", "\n", NULL);
1567                 add_header(ct, np, vp);
1568                 break;
1569
1570         default:
1571                 adios(EX_DATAERR, NULL, "unknown transfer encoding in content");
1572                 break;
1573         }
1574
1575         /*
1576         ** Additional content specific header processing
1577         */
1578         switch (ct->c_type) {
1579         case CT_MULTIPART:
1580         {
1581                 struct multipart *m;
1582                 struct part *part;
1583
1584                 m = (struct multipart *) ct->c_ctparams;
1585                 for (part = m->mp_parts; part; part = part->mp_next) {
1586                         CT p;
1587
1588                         p = part->mp_part;
1589                         build_headers(p);
1590                 }
1591         }
1592                 break;
1593
1594         default:
1595                 /* Nothing to do */
1596                 break;
1597         }
1598
1599         return OK;
1600 }