2 ** mhbuild.c -- expand/translate MIME composition files
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.
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.
18 #include <h/signals.h>
23 #include <h/mhparse.h>
26 #ifdef HAVE_SYS_TIME_H
27 # include <sys/time.h>
31 static struct swit switches[] = {
47 ** Directory to place tmp files. This must
48 ** be set before these routines are called.
54 static char prefix[] = "----- =_aaaaaaaaaa";
62 int make_intermediates(char *);
63 void content_error(char *, CT, char *, ...);
66 void free_content(CT);
68 void free_encoding(CT, int);
73 static int init_decoded_content(CT);
74 static char *fgetstr(char *, int, FILE *);
75 static int user_content(FILE *, char *, char *, CT *);
76 static void set_id(CT, int);
77 static int compose_content(CT);
78 static int scan_content(CT);
79 static int build_headers(CT);
80 static CT build_mime(char *);
89 static char infile[BUFSIZ];
90 static int unlink_infile = 0;
92 static char outfile[BUFSIZ];
93 static int unlink_outfile = 0;
95 static void unlink_done(int) NORETURN;
98 int output_message(CT, char *);
99 int output_message_fp(CT, FILE *, char*);
102 void set_endian(void);
105 void free_content(CT);
109 main(int argc, char **argv)
111 char *cp, buf[BUFSIZ];
112 char buffer[BUFSIZ], *compfile = NULL;
113 char **argp, **arguments;
120 setlocale(LC_ALL, "");
121 invo_name = mhbasename(argv[0]);
123 /* read user profile/context */
126 arguments = getarguments(invo_name, argc, argv, 1);
129 while ((cp = *argp++)) {
130 if (cp[0] == '-' && cp[1] == '\0') {
132 adios(NULL, "cannot specify both standard input and a file");
135 verbosw = 0; /* turn off -verbose listings */
139 switch (smatch(++cp, switches)) {
141 ambigsw(cp, switches);
144 adios(NULL, "-%s unknown", cp);
147 snprintf(buf, sizeof(buf), "%s [switches] file", invo_name);
148 print_help(buf, switches, 1);
151 print_version(invo_name);
166 adios(NULL, "only one composition file allowed");
174 ** Check if we've specified an additional profile
176 if ((cp = getenv("MHBUILD"))) {
177 if ((fp = fopen(cp, "r"))) {
178 readconfig((struct node **) 0, fp, cp, 0);
181 admonish("", "unable to read $MHBUILD profile (%s)",
187 ** Read the standard profile setup
189 if ((fp = fopen(cp = etcpath("mhn.defaults"), "r"))) {
190 readconfig((struct node **) 0, fp, cp, 0);
195 ** Check for storage directory. If defined, we
196 ** will store temporary files there. Else we
197 ** store them in standard nmh directory.
199 if ((cp = context_find(nmhstorage)) && *cp)
200 tmp = concat(cp, "/", invo_name, NULL);
202 tmp = getcpy(toabsdir(invo_name));
204 /* Check if we have a file to process */
206 adios(NULL, "need to specify a %s composition file",
210 ** Process the composition file from standard input.
212 if (compfile[0] == '-' && compfile[1] == '\0') {
213 /* copy standard input to temporary file */
214 strncpy(infile, m_mktemp(invo_name, NULL, &fp),
216 while (fgets(buffer, BUFSIZ, stdin))
221 /* build the content structures for MIME message */
222 ct = build_mime(infile);
224 /* output MIME message to this temporary file */
225 strncpy(outfile, m_mktemp(invo_name, NULL, &fp_out),
229 /* output the message */
230 output_message_fp(ct, fp_out, outfile);
233 /* output the temp file to standard output */
234 if ((fp = fopen(outfile, "r")) == NULL)
235 adios(outfile, "unable to open");
236 while (fgets(buffer, BUFSIZ, fp))
237 fputs(buffer, stdout);
251 ** Process the composition file from a file.
254 /* build the content structures for MIME message */
255 ct = build_mime(compfile);
257 /* output MIME message to this temporary file */
258 strncpy(outfile, m_mktemp2(compfile, invo_name, NULL, &fp_out),
262 /* output the message */
263 output_message_fp(ct, fp_out, outfile);
266 /* Rename composition draft */
267 snprintf(buffer, sizeof(buffer), "%s.orig", compfile);
268 if (rename(compfile, buffer) == NOTOK) {
269 adios(buffer, "unable to rename draft %s to", compfile);
272 /* Rename output file to take its place */
273 if (rename(outfile, compfile) == NOTOK) {
274 advise(compfile, "unable to rename output %s to", outfile);
275 rename(buffer, compfile);
287 unlink_done(int status)
290 ** Check if we need to remove stray temporary files.
301 ** Main routine for translating composition file
302 ** into valid MIME message. It translates the draft
303 ** into a content structure (actually a tree of content
304 ** structures). This message then can be manipulated
305 ** in various ways, including being output via
309 build_mime(char *infile)
312 char buf[BUFSIZ], name[NAMESZ];
321 /* open the composition draft */
322 if ((in = fopen(infile, "r")) == NULL)
323 adios(infile, "unable to open for reading");
326 ** Allocate space for primary (outside) content
328 if ((ct = (CT) calloc(1, sizeof(*ct))) == NULL)
329 adios(NULL, "out of memory");
332 ** Allocate structure for handling decoded content
333 ** for this part. We don't really need this, but
334 ** allocate it to remain consistent.
336 init_decoded_content(ct);
339 ** Parse some of the header fields in the composition
340 ** draft into the linked list of header fields for
341 ** the new MIME message.
343 for (compnum = 1, state = FLD;;) {
344 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
350 /* abort if draft has Mime-Version header field */
351 if (!mh_strcasecmp(name, VRSN_FIELD))
352 adios(NULL, "draft shouldn't contain %s: field", VRSN_FIELD);
355 ** abort if draft has Content-Transfer-Encoding
358 if (!mh_strcasecmp(name, ENCODING_FIELD))
359 adios(NULL, "draft shouldn't contain %s: field", ENCODING_FIELD);
361 /* ignore any Content-Type fields in the header */
362 if (!mh_strcasecmp(name, TYPE_FIELD)) {
363 while (state == FLDPLUS)
364 state = m_getfld(state, name, buf,
369 /* get copies of the buffers */
373 /* if necessary, get rest of field */
374 while (state == FLDPLUS) {
375 state = m_getfld(state, name, buf,
377 vp = add(buf, vp); /* add to prev value */
380 /* Now add the header data to the list */
381 add_header(ct, np, vp);
384 /* if this wasn't the last hdr field, then continue */
390 adios(NULL, "draft has empty body -- no directives!");
395 fseek(in, (long) (-strlen(buf)), SEEK_CUR);
400 adios(NULL, "message format error in component #%d",
404 adios(NULL, "getfld() returned %d", state);
410 ** Now add the MIME-Version header field
411 ** to the list of header fields.
413 np = getcpy(VRSN_FIELD);
414 vp = concat(" ", VRSN_VALUE, "\n", NULL);
415 add_header(ct, np, vp);
418 ** We initally 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.
422 if (get_ctinfo("multipart/mixed", ct, 0) == NOTOK)
424 ct->c_type = CT_MULTIPART;
425 ct->c_subtype = MULTI_MIXED;
426 ct->c_file = getcpy(infile);
428 if ((m = (struct multipart *) calloc(1, sizeof(*m))) == NULL)
429 adios(NULL, "out of memory");
430 ct->c_ctparams = (void *) m;
434 ** read and parse the composition file
435 ** and the directives it contains.
437 while (fgetstr(buf, sizeof(buf) - 1, in)) {
441 if (user_content(in, infile, buf, &p) == DONE) {
442 admonish(NULL, "ignoring spurious #end");
448 if ((part = (struct part *) calloc(1, sizeof(*part))) == NULL)
449 adios(NULL, "out of memory");
456 ** close the composition draft since
457 ** it's not needed any longer.
461 /* check if any contents were found */
463 adios(NULL, "no content directives found");
466 ** If only one content was found, then remove and
467 ** free the outer multipart content.
469 if (!m->mp_parts->mp_next) {
472 p = m->mp_parts->mp_part;
473 m->mp_parts->mp_part = NULL;
475 /* move header fields */
476 p->c_first_hf = ct->c_first_hf;
477 p->c_last_hf = ct->c_last_hf;
478 ct->c_first_hf = NULL;
479 ct->c_last_hf = NULL;
488 ** Fill out, or expand directives. Parse and execute
489 ** commands specified by profile composition strings.
493 if ((cp = strchr(prefix, 'a')) == NULL)
494 adios(NULL, "internal error(4)");
497 ** Scan the contents. Choose a transfer encoding, and
498 ** check if prefix for multipart boundary clashes with
499 ** any of the contents.
501 while (scan_content(ct) == NOTOK) {
506 adios(NULL, "giving up trying to find a unique delimiter string");
512 /* Build the rest of the header field structures */
520 ** Set up structures for placing unencoded
521 ** content when building parts.
525 init_decoded_content(CT ct)
529 if ((ce = (CE) calloc(1, sizeof(*ce))) == NULL)
530 adios(NULL, "out of memory");
533 ct->c_ceopenfnx = open7Bit; /* since unencoded */
534 ct->c_ceclosefnx = close_encoding;
535 ct->c_cesizefnx = NULL; /* since unencoded */
542 fgetstr(char *s, int n, FILE *stream)
546 for (ep = (cp = s) + n; cp < ep; ) {
549 if (!fgets(cp, n, stream))
550 return (cp != s ? s : NULL);
551 if (cp == s && *cp != '#')
554 cp += (i = strlen(cp)) - 1;
555 if (i <= 1 || *cp-- != '\n' || *cp != '\\')
566 ** Parse the composition draft for text and directives.
567 ** Do initial setup of Content structure.
571 user_content(FILE *in, char *file, char *buf, CT *ctp)
579 struct str2init *s2i;
584 if (buf[0] == '\n' || strcmp(buf, "#\n") == 0) {
589 /* allocate basic Content structure */
590 if ((ct = (CT) calloc(1, sizeof(*ct))) == NULL)
591 adios(NULL, "out of memory");
594 /* allocate basic structure for handling decoded content */
595 init_decoded_content(ct);
602 ** Handle inline text. Check if line
603 ** is one of the following forms:
605 ** 1) doesn't begin with '#' (implicit directive)
606 ** 2) begins with "##" (implicit directive)
607 ** 3) begins with "#<"
609 if (buf[0] != '#' || buf[1] == '#' || buf[1] == '<') {
613 char content[BUFSIZ];
617 cp = m_mktemp2(NULL, invo_name, NULL, &out);
619 adios("mhbuild", "unable to create temporary file");
621 /* use a temp file to collect the plain text lines */
622 ce->ce_file = getcpy(cp);
625 if (buf[0] == '#' && buf[1] == '<') {
626 strncpy(content, buf + 2, sizeof(content));
633 /* the directive is implicit */
634 strncpy(content, "text/plain", sizeof(content));
636 strncpy(buffer, buf[0] != '#' ? buf : buf + 1, sizeof(buffer));
640 if (headers >= 0 && uprf(buffer, DESCR_FIELD) &&
641 buffer[i=strlen(DESCR_FIELD)] == ':') {
645 ct->c_descr = add(buffer + i + 1, ct->c_descr);
646 if (!fgetstr(buffer, sizeof(buffer) - 1, in))
647 adios(NULL, "end-of-file after %s: field in plaintext", DESCR_FIELD);
655 adios(NULL, "#-directive after %s: field in plaintext", DESCR_FIELD);
663 if (headers >= 0 && uprf(buffer, DISPO_FIELD)
664 && buffer[i = strlen(DISPO_FIELD)] == ':') {
668 ct->c_dispo = add(buffer + i + 1, ct->c_dispo);
669 if (!fgetstr(buffer, sizeof(buffer) - 1, in))
670 adios(NULL, "end-of-file after %s: field in plaintext", DISPO_FIELD);
678 adios(NULL, "#-directive after %s: field in plaintext", DISPO_FIELD);
686 if (headers != 1 || buffer[0] != '\n')
692 if ((cp = fgetstr(buffer, sizeof(buffer) - 1, in))
695 if (buffer[0] == '#') {
698 if (buffer[1] != '#')
700 for (cp = (bp = buffer) + 1; *cp; cp++)
708 /* parse content type */
709 if (get_ctinfo(content, ct, inlineD) == NOTOK)
712 for (s2i = str2cts; s2i->si_key; s2i++)
713 if (!mh_strcasecmp(ci->ci_type, s2i->si_key))
715 if (!s2i->si_key && !uprf(ci->ci_type, "X-"))
719 ** check type specified (possibly implicitly)
721 switch (ct->c_type = s2i->si_val) {
723 if (!mh_strcasecmp(ci->ci_subtype, "rfc822")) {
724 ct->c_encoding = CE_7BIT;
729 adios(NULL, "it doesn't make sense to define an in-line %s content",
730 ct->c_type == CT_MESSAGE ? "message" :
736 if ((ct->c_ctinitfnx = s2i->si_init))
737 (*ct->c_ctinitfnx) (ct);
742 fseek(in, pos, SEEK_SET);
747 ** If we've reached this point, the next line
748 ** must be some type of explicit directive.
752 adios(NULL, "The #@ directive i.e. message/external-body "
753 "is not supported anymore.");
756 /* parse directive */
757 if (get_ctinfo(buf+1, ct, 1) == NOTOK)
760 /* check directive against the list of MIME types */
761 for (s2i = str2cts; s2i->si_key; s2i++)
762 if (!mh_strcasecmp(ci->ci_type, s2i->si_key))
766 ** Check if the directive specified a valid type.
767 ** This will happen if it was one of the following forms:
773 adios(NULL, "missing subtype in \"#%s\"", ci->ci_type);
775 switch (ct->c_type = s2i->si_val) {
777 adios(NULL, "use \"#begin ... #end\" instead of \"#%s/%s\"", ci->ci_type, ci->ci_subtype);
781 if (!mh_strcasecmp(ci->ci_subtype, "partial") ||
782 !mh_strcasecmp(ci->ci_subtype,
784 adios(NULL, "sorry, \"#%s/%s\" isn't supported", ci->ci_type, ci->ci_subtype);
787 admonish(NULL, "use \"#forw [+folder] [msgs]\" instead of \"#%s/%s\"", ci->ci_type, ci->ci_subtype);
791 if ((ct->c_ctinitfnx = s2i->si_init))
792 (*ct->c_ctinitfnx) (ct);
796 /* Handle [file] argument */
798 /* check if specifies command to execute */
799 if (*ci->ci_magic == '|' || *ci->ci_magic == '!') {
800 for (cp = ci->ci_magic + 1; isspace(*cp); cp++)
803 adios(NULL, "empty pipe command for #%s directive", ci->ci_type);
808 /* record filename of decoded contents */
809 ce->ce_file = ci->ci_magic;
810 if (access(ce->ce_file, R_OK) == NOTOK)
811 adios("reading", "unable to access %s for", ce->ce_file);
818 ** No [file] argument, so check profile for
819 ** method to compose content.
821 snprintf(buffer, sizeof(buffer), "%s-compose-%s/%s",
822 invo_name, ci->ci_type, ci->ci_subtype);
823 if ((cp = context_find(buffer)) == NULL || *cp == '\0') {
824 snprintf(buffer, sizeof(buffer), "%s-compose-%s",
825 invo_name, ci->ci_type);
826 if ((cp = context_find(buffer)) == NULL ||
828 content_error(NULL, ct, "don't know how to compose content");
832 ci->ci_magic = getcpy(cp);
838 ** #forw [+folder] [msgs]
840 if (!mh_strcasecmp(ci->ci_type, "forw")) {
842 char *folder, *arguments[MAXARGS];
848 ap = brkstring(ci->ci_magic, " ", "\n");
849 for (i=0; ap[i] && i<MAXARGS-1; i++) {
850 arguments[i] = ap[i];
855 arguments[0] = seq_cur;
860 /* search the arguments for a folder name */
861 for (ap = arguments; *ap; ap++) {
863 if (*cp == '+' || *cp == '@') {
865 adios(NULL, "only one folder per #forw directive");
867 folder = getcpy(expandfol(cp));
871 /* else, use the current folder */
873 folder = getcpy(getcurfol());
875 if (!(mp = folder_read(folder)))
876 adios(NULL, "unable to read folder %s", folder);
877 for (ap = arguments; *ap; ap++) {
879 if (*cp != '+' && *cp != '@')
880 if (!m_convert(mp, cp))
887 ** If there is more than one message to include, make this
888 ** a content of type "multipart/digest" and insert each message
889 ** as a subpart. If there is only one message, then make this
890 ** a content of type "message/rfc822".
892 if (mp->numsel > 1) {
893 /* we are forwarding multiple messages */
894 if (get_ctinfo("multipart/digest", ct, 0) == NOTOK)
896 ct->c_type = CT_MULTIPART;
897 ct->c_subtype = MULTI_DIGEST;
899 if ((m = (struct multipart *)
900 calloc(1, sizeof(*m))) == NULL)
901 adios(NULL, "out of memory");
902 ct->c_ctparams = (void *) m;
905 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
906 if (is_selected(mp, msgnum)) {
911 if ((p = (CT) calloc(1, sizeof(*p)))
913 adios(NULL, "out of memory");
914 init_decoded_content(p);
916 if (get_ctinfo("message/rfc822", p, 0)
919 p->c_type = CT_MESSAGE;
920 p->c_subtype = MESSAGE_RFC822;
922 snprintf(buffer, sizeof(buffer),
923 "%s/%d", mp->foldpath,
925 pe->ce_file = getcpy(buffer);
927 if ((part = (struct part *) calloc(1, sizeof(*part))) == NULL)
928 adios(NULL, "out of memory");
935 /* we are forwarding one message */
936 if (get_ctinfo("message/rfc822", ct, 0) == NOTOK)
938 ct->c_type = CT_MESSAGE;
939 ct->c_subtype = MESSAGE_RFC822;
942 snprintf(buffer, sizeof(buffer), "%s/%d",
943 mp->foldpath, msgnum);
944 ce->ce_file = getcpy(buffer);
947 folder_free(mp); /* free folder/message structure */
954 if (!mh_strcasecmp(ci->ci_type, "end")) {
961 ** #begin [ alternative | parallel ]
963 if (!mh_strcasecmp(ci->ci_type, "begin")) {
966 cp = SubMultiPart[vrsn - 1].kv_key;
967 } else if (!mh_strcasecmp(ci->ci_magic, "alternative")) {
968 vrsn = MULTI_ALTERNATE;
969 cp = SubMultiPart[vrsn - 1].kv_key;
970 } else if (!mh_strcasecmp(ci->ci_magic, "parallel")) {
971 vrsn = MULTI_PARALLEL;
972 cp = SubMultiPart[vrsn - 1].kv_key;
973 } else if (uprf(ci->ci_magic, "digest")) {
976 vrsn = MULTI_UNKNOWN;
981 snprintf(buffer, sizeof(buffer), "multipart/%s", cp);
982 if (get_ctinfo(buffer, ct, 0) == NOTOK)
984 ct->c_type = CT_MULTIPART;
985 ct->c_subtype = vrsn;
987 if ((m = (struct multipart *) calloc(1, sizeof(*m))) == NULL)
988 adios(NULL, "out of memory");
989 ct->c_ctparams = (void *) m;
992 while (fgetstr(buffer, sizeof(buffer) - 1, in)) {
996 if (user_content(in, file, buffer, &p) == DONE) {
998 adios(NULL, "empty \"#begin ... #end\" sequence");
1004 if ((part = (struct part *)
1005 calloc(1, sizeof(*part))) == NULL)
1006 adios(NULL, "out of memory");
1008 pp = &part->mp_next;
1011 admonish(NULL, "premature end-of-file, missing #end");
1016 ** Unknown directive
1018 adios(NULL, "unknown directive \"#%s\"", ci->ci_type);
1019 return NOTOK; /* NOT REACHED */
1024 set_id(CT ct, int top)
1028 static time_t clock = 0;
1029 static char *msgfmt;
1033 snprintf(msgid, sizeof(msgid), "<%d.%ld.%%d@%s>\n",
1034 (int) getpid(), (long) clock, LocalName());
1036 msgfmt = getcpy(msgid);
1038 snprintf(msgid, sizeof(msgid), msgfmt, top ? 0 : ++partno);
1039 ct->c_id = getcpy(msgid);
1044 ** Fill out, or expand the various contents in the composition
1045 ** draft. Read-in any necessary files. Parse and execute any
1046 ** commands specified by profile composition strings.
1050 compose_content(CT ct)
1052 CE ce = ct->c_cefile;
1054 switch (ct->c_type) {
1059 char partnam[BUFSIZ];
1060 struct multipart *m = (struct multipart *) ct->c_ctparams;
1064 snprintf(partnam, sizeof(partnam), "%s.",
1066 pp = partnam + strlen(partnam);
1071 /* first, we call compose_content on all the subparts */
1072 for (part = m->mp_parts, partnum = 1; part;
1073 part = part->mp_next, partnum++) {
1074 CT p = part->mp_part;
1076 sprintf(pp, "%d", partnum);
1077 p->c_partno = getcpy(partnam);
1078 if (compose_content(p) == NOTOK)
1085 /* Nothing to do for type message */
1089 ** Discrete types (text/application/audio/image/video)
1094 int xstdout, len, buflen;
1095 char *bp, **ap, *cp;
1096 char *vec[4], buffer[BUFSIZ];
1098 CI ci = &ct->c_ctinfo;
1101 if (!(cp = ci->ci_magic))
1102 adios(NULL, "internal error(5)");
1104 tfile = m_mktemp2(NULL, invo_name, NULL, NULL);
1105 if (tfile == NULL) {
1106 adios("mhbuild", "unable to create temporary file");
1108 ce->ce_file = getcpy(tfile);
1113 /* Get buffer ready to go */
1116 buflen = sizeof(buffer);
1119 ** Parse composition string into buffer
1121 for ( ; *cp; cp++) {
1127 ** insert parameters from
1133 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
1134 snprintf(bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
1144 /* %f, and stdout is not-redirected */
1150 ** insert temporary filename
1151 ** where content should be
1154 snprintf(bp, buflen, "%s", ce->ce_file);
1158 /* insert content subtype */
1159 strncpy(bp, ci->ci_subtype, buflen);
1163 /* insert character % */
1184 printf("composing content %s/%s from command\n\t%s\n", ci->ci_type, ci->ci_subtype, buffer);
1186 fflush(stdout); /* not sure if need for -noverbose */
1193 if ((out = fopen(ce->ce_file, "w")) == NULL)
1194 adios(ce->ce_file, "unable to open for writing");
1196 switch (child_id = fork()) {
1198 adios("fork", "unable to fork");
1203 dup2(fileno(out), 1);
1205 execvp("/bin/sh", vec);
1206 fprintf(stderr, "unable to exec ");
1213 if (pidXwait(child_id, NULL))
1226 ** Scan the content.
1228 ** 1) choose a transfer encoding.
1229 ** 2) check for clashes with multipart boundary string.
1230 ** 3) for text content, figure out which character set is being used.
1232 ** If there is a clash with one of the contents and the multipart boundary,
1233 ** this function will exit with NOTOK. This will cause the scanning process
1234 ** to be repeated with a different multipart boundary. It is possible
1235 ** (although highly unlikely) that this scan will be repeated multiple times.
1242 int check8bit = 0, contains8bit = 0;
1243 int checklinelen = 0, linelen = 0;
1244 int checkboundary = 0, boundaryclash = 0;
1245 int checklinespace = 0, linespace = 0; /* trailing whitespace */
1246 unsigned char *cp = NULL, buffer[BUFSIZ];
1247 struct text *t = NULL;
1249 CE ce = ct->c_cefile;
1252 ** handle multipart by scanning all subparts
1253 ** and then checking their encoding.
1255 if (ct->c_type == CT_MULTIPART) {
1256 struct multipart *m = (struct multipart *) ct->c_ctparams;
1259 /* initially mark the domain of enclosing multipart as 7bit */
1260 ct->c_encoding = CE_7BIT;
1262 for (part = m->mp_parts; part; part = part->mp_next) {
1263 CT p = part->mp_part;
1265 if (scan_content(p) == NOTOK) {
1266 /* choose encoding for subpart */
1271 ** if necessary, enlarge encoding for enclosing
1274 if (p->c_encoding == CE_BINARY)
1275 ct->c_encoding = CE_BINARY;
1276 if (p->c_encoding == CE_8BIT &&
1277 ct->c_encoding != CE_BINARY)
1278 ct->c_encoding = CE_8BIT;
1285 ** Decide what to check while scanning this content.
1287 switch (ct->c_type) {
1291 if (ct->c_subtype == TEXT_PLAIN) {
1307 case CT_APPLICATION:
1319 ** Scan the unencoded content
1321 if (check8bit || checklinelen || checklinespace || checkboundary) {
1322 if ((in = fopen(ce->ce_file, "r")) == NULL)
1323 adios(ce->ce_file, "unable to open for reading");
1324 len = strlen(prefix);
1326 while (fgets(buffer, sizeof(buffer) - 1, in)) {
1328 for (cp = buffer; *cp; cp++) {
1329 if (!isascii(*cp)) {
1331 /* no need to keep checking */
1337 if (checklinelen && (strlen(buffer) > CPERLIN + 1)) {
1339 checklinelen = 0; /* no need to keep checking */
1342 if (checklinespace &&
1343 (cp = buffer + strlen(buffer) - 2) >
1344 buffer && isspace(*cp)) {
1346 /* no need to keep checking */
1351 ** Check if content contains a line that clashes
1352 ** with our standard boundary for multipart messages.
1354 if (checkboundary && buffer[0] == '-' &&
1356 for (cp = buffer + strlen(buffer) - 1;
1361 if (strncmp(buffer + 2, prefix, len)==0 &&
1362 isdigit(buffer[2 + len])) {
1364 /* no need to keep checking */
1373 ** Decide which transfer encoding to use.
1375 switch (ct->c_type) {
1378 ** If the text content didn't specify a character
1379 ** set, we need to figure out which one was used.
1381 t = (struct text *) ct->c_ctparams;
1382 if (t->tx_charset == CHARSET_UNSPECIFIED) {
1383 CI ci = &ct->c_ctinfo;
1386 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++)
1390 t->tx_charset = CHARSET_UNKNOWN;
1391 *ap = concat("charset=", write_charset_8bit(),
1394 t->tx_charset = CHARSET_USASCII;
1395 *ap = getcpy("charset=us-ascii");
1398 cp = strchr(*ap++, '=');
1404 if (contains8bit || linelen || linespace)
1405 ct->c_encoding = CE_QUOTED;
1407 ct->c_encoding = CE_7BIT;
1411 ct->c_encoding = CE_7BIT;
1414 case CT_APPLICATION:
1419 ** Forcing use of base64, because these types likely
1420 ** contain binary data and NUL bytes. Don't care about
1421 ** files that would be clean.
1423 ct->c_encoding = CE_BASE64;
1427 return (boundaryclash ? NOTOK : OK);
1432 ** Scan the content structures, and build header
1433 ** fields that will need to be output into the
1438 build_headers(CT ct)
1442 char *np, *vp, buffer[BUFSIZ];
1443 CI ci = &ct->c_ctinfo;
1446 ** If message is type multipart, then add the multipart
1447 ** boundary to the list of attribute/value pairs.
1449 if (ct->c_type == CT_MULTIPART) {
1451 static int level = 0; /* store nesting level */
1455 snprintf(buffer, sizeof(buffer), "boundary=%s%d",
1457 cp = strchr(*ap++ = getcpy(buffer), '=');
1464 ** output the content type and subtype
1466 np = getcpy(TYPE_FIELD);
1467 vp = concat(" ", ci->ci_type, "/", ci->ci_subtype, NULL);
1469 /* keep track of length of line */
1470 len = strlen(TYPE_FIELD) + strlen(ci->ci_type) +
1471 strlen(ci->ci_subtype) + 3;
1474 ** Append the attribute/value pairs to
1475 ** the end of the Content-Type line.
1477 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
1481 snprintf(buffer, sizeof(buffer), "%s=\"%s\"", *ap, *ep);
1482 if (len + 1 + (cc = strlen(buffer)) >= CPERLIN) {
1483 vp = add("\n\t", vp);
1489 vp = add(buffer, vp);
1494 ** Append any RFC-822 comment to the end of
1495 ** the Content-Type line.
1497 if (ci->ci_comment) {
1498 snprintf(buffer, sizeof(buffer), "(%s)", ci->ci_comment);
1499 if (len + 1 + (cc = 2 + strlen(ci->ci_comment)) >= CPERLIN) {
1500 vp = add("\n\t", vp);
1506 vp = add(buffer, vp);
1510 add_header(ct, np, vp);
1513 ** output the Content-ID
1516 np = getcpy(ID_FIELD);
1517 vp = concat(" ", ct->c_id, NULL);
1518 add_header(ct, np, vp);
1522 ** output the Content-Description
1525 np = getcpy(DESCR_FIELD);
1526 vp = concat(" ", ct->c_descr, NULL);
1527 add_header(ct, np, vp);
1531 ** output the Content-Disposition
1534 np = getcpy(DISPO_FIELD);
1535 vp = concat(" ", ct->c_dispo, NULL);
1536 add_header(ct, np, vp);
1540 ** output the Content-Transfer-Encoding
1542 switch (ct->c_encoding) {
1544 /* Nothing to output */
1548 if (ct->c_type == CT_MESSAGE)
1549 adios(NULL, "internal error, invalid encoding");
1551 np = getcpy(ENCODING_FIELD);
1552 vp = concat(" ", "8bit", "\n", NULL);
1553 add_header(ct, np, vp);
1557 if (ct->c_type == CT_MESSAGE || ct->c_type == CT_MULTIPART)
1558 adios(NULL, "internal error, invalid encoding");
1560 np = getcpy(ENCODING_FIELD);
1561 vp = concat(" ", "quoted-printable", "\n", NULL);
1562 add_header(ct, np, vp);
1566 if (ct->c_type == CT_MESSAGE || ct->c_type == CT_MULTIPART)
1567 adios(NULL, "internal error, invalid encoding");
1569 np = getcpy(ENCODING_FIELD);
1570 vp = concat(" ", "base64", "\n", NULL);
1571 add_header(ct, np, vp);
1575 if (ct->c_type == CT_MESSAGE)
1576 adios(NULL, "internal error, invalid encoding");
1578 np = getcpy(ENCODING_FIELD);
1579 vp = concat(" ", "binary", "\n", NULL);
1580 add_header(ct, np, vp);
1584 adios(NULL, "unknown transfer encoding in content");
1589 ** Additional content specific header processing
1591 switch (ct->c_type) {
1594 struct multipart *m;
1597 m = (struct multipart *) ct->c_ctparams;
1598 for (part = m->mp_parts; part; part = part->mp_next) {