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