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.
21 #include <h/mhparse.h>
28 #ifdef HAVE_SYS_TIME_H
29 # include <sys/time.h>
33 static struct swit switches[] = {
49 ** Directory to place tmp files. This must
50 ** be set before these routines are called.
56 static char prefix[] = "----- =_aaaaaaaaaa";
64 int make_intermediates(char *);
65 void content_error(char *, CT, char *, ...);
68 void free_content(CT);
70 void free_encoding(CT, int);
75 static int init_decoded_content(CT);
76 static char *fgetstr(char *, int, FILE *);
77 static int user_content(FILE *, char *, char *, CT *);
78 static void set_id(CT, int);
79 static int compose_content(CT);
80 static int scan_content(CT);
81 static int build_headers(CT);
82 static CT build_mime(char *);
91 static char infile[BUFSIZ];
92 static int unlink_infile = 0;
94 static char outfile[BUFSIZ];
95 static int unlink_outfile = 0;
100 int output_message(CT, char *);
101 int output_message_fp(CT, FILE *, char*);
104 void set_endian(void);
107 void free_content(CT);
111 main(int argc, char **argv)
113 char *cp, buf[BUFSIZ];
114 char buffer[BUFSIZ], *compfile = NULL;
115 char **argp, **arguments;
120 if (atexit(unlink_done) != 0) {
121 adios(NULL, "atexit failed");
124 setlocale(LC_ALL, "");
125 invo_name = mhbasename(argv[0]);
127 /* read user profile/context */
130 arguments = getarguments(invo_name, argc, argv, 1);
133 while ((cp = *argp++)) {
134 if (cp[0] == '-' && cp[1] == '\0') {
136 adios(NULL, "cannot specify both standard input and a file");
139 verbosw = 0; /* turn off -verbose listings */
143 switch (smatch(++cp, switches)) {
145 ambigsw(cp, switches);
146 /* sysexits.h EX_USAGE */
149 adios(NULL, "-%s unknown", cp);
152 snprintf(buf, sizeof(buf), "%s [switches] file", invo_name);
153 print_help(buf, switches, 1);
156 print_version(invo_name);
171 adios(NULL, "only one composition file allowed");
179 ** Check if we've specified an additional profile
181 if ((cp = getenv("MHBUILD"))) {
182 if ((fp = fopen(cp, "r"))) {
183 readconfig((struct node **) 0, fp, cp, 0);
186 admonish("", "unable to read $MHBUILD profile (%s)",
192 ** Read the standard profile setup
194 if ((fp = fopen(cp = etcpath("mhn.defaults"), "r"))) {
195 readconfig((struct node **) 0, fp, cp, 0);
200 ** Check for storage directory. If defined, we
201 ** will store temporary files there. Else we
202 ** store them in standard nmh directory.
204 if ((cp = context_find(nmhstorage)) && *cp)
205 tmp = concat(cp, "/", invo_name, NULL);
207 tmp = getcpy(toabsdir(invo_name));
209 /* Check if we have a file to process */
211 adios(NULL, "need to specify a %s composition file",
215 ** Process the composition file from standard input.
217 if (compfile[0] == '-' && compfile[1] == '\0') {
218 /* copy standard input to temporary file */
219 strncpy(infile, m_mktemp(invo_name, NULL, &fp),
221 while (fgets(buffer, BUFSIZ, stdin))
226 /* build the content structures for MIME message */
227 ct = build_mime(infile);
229 /* output MIME message to this temporary file */
230 strncpy(outfile, m_mktemp(invo_name, NULL, &fp_out),
234 /* output the message */
235 output_message_fp(ct, fp_out, outfile);
238 /* output the temp file to standard output */
239 if ((fp = fopen(outfile, "r")) == NULL)
240 adios(outfile, "unable to open");
241 while (fgets(buffer, BUFSIZ, fp))
242 fputs(buffer, stdout);
256 ** Process the composition file from a file.
259 /* build the content structures for MIME message */
260 ct = build_mime(compfile);
262 /* output MIME message to this temporary file */
263 strncpy(outfile, m_mktemp2(compfile, invo_name, NULL, &fp_out),
267 /* output the message */
268 output_message_fp(ct, fp_out, outfile);
271 /* Rename composition draft */
272 snprintf(buffer, sizeof(buffer), "%s.orig", compfile);
273 if (rename(compfile, buffer) == NOTOK) {
274 adios(buffer, "unable to rename draft %s to", compfile);
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 /* sysexits.h EX_IOERR */
295 ** Check if we need to remove stray temporary files.
300 if (unlink_outfile) {
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
314 build_mime(char *infile)
317 char buf[BUFSIZ], name[NAMESZ];
326 /* open the composition draft */
327 if ((in = fopen(infile, "r")) == NULL)
328 adios(infile, "unable to open for reading");
331 ** Allocate space for primary (outside) content
333 if ((ct = (CT) calloc(1, sizeof(*ct))) == NULL)
334 adios(NULL, "out of memory");
337 ** Allocate structure for handling decoded content
338 ** for this part. We don't really need this, but
339 ** allocate it to remain consistent.
341 init_decoded_content(ct);
344 ** Parse some of the header fields in the composition
345 ** draft into the linked list of header fields for
346 ** the new MIME message.
348 for (compnum = 1, state = FLD;;) {
349 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
355 /* abort if draft has Mime-Version header field */
356 if (!mh_strcasecmp(name, VRSN_FIELD))
357 adios(NULL, "draft shouldn't contain %s: field", VRSN_FIELD);
360 ** abort if draft has Content-Transfer-Encoding
363 if (!mh_strcasecmp(name, ENCODING_FIELD))
364 adios(NULL, "draft shouldn't contain %s: field", ENCODING_FIELD);
366 /* ignore any Content-Type fields in the header */
367 if (!mh_strcasecmp(name, TYPE_FIELD)) {
368 while (state == FLDPLUS)
369 state = m_getfld(state, name, buf,
374 /* get copies of the buffers */
378 /* if necessary, get rest of field */
379 while (state == FLDPLUS) {
380 state = m_getfld(state, name, buf,
382 vp = add(buf, vp); /* add to prev value */
385 /* Now add the header data to the list */
386 add_header(ct, np, vp);
389 /* if this wasn't the last hdr field, then continue */
395 adios(NULL, "draft has empty body -- no directives!");
400 fseek(in, (long) (-strlen(buf)), SEEK_CUR);
405 adios(NULL, "message format error in component #%d",
409 adios(NULL, "getfld() returned %d", state);
415 ** Now add the MIME-Version header field
416 ** to the list of header fields.
418 np = getcpy(VRSN_FIELD);
419 vp = concat(" ", VRSN_VALUE, "\n", NULL);
420 add_header(ct, np, vp);
423 ** We initally assume we will find multiple contents in the
424 ** draft. So create a multipart/mixed content to hold everything.
425 ** We can remove this later, if it is not needed.
427 if (get_ctinfo("multipart/mixed", ct, 0) == NOTOK) {
428 /* sysexits.h EX_DATAERR */
431 ct->c_type = CT_MULTIPART;
432 ct->c_subtype = MULTI_MIXED;
433 ct->c_file = getcpy(infile);
435 if ((m = (struct multipart *) calloc(1, sizeof(*m))) == NULL)
436 adios(NULL, "out of memory");
437 ct->c_ctparams = (void *) m;
441 ** read and parse the composition file
442 ** and the directives it contains.
444 while (fgetstr(buf, sizeof(buf) - 1, in)) {
448 if (user_content(in, infile, buf, &p) == DONE) {
449 admonish(NULL, "ignoring spurious #end");
455 if ((part = (struct part *) calloc(1, sizeof(*part))) == NULL)
456 adios(NULL, "out of memory");
463 ** close the composition draft since
464 ** it's not needed any longer.
468 /* check if any contents were found */
470 adios(NULL, "no content directives found");
473 ** If only one content was found, then remove and
474 ** free the outer multipart content.
476 if (!m->mp_parts->mp_next) {
479 p = m->mp_parts->mp_part;
480 m->mp_parts->mp_part = NULL;
482 /* move header fields */
483 p->c_first_hf = ct->c_first_hf;
484 p->c_last_hf = ct->c_last_hf;
485 ct->c_first_hf = NULL;
486 ct->c_last_hf = NULL;
495 ** Fill out, or expand directives. Parse and execute
496 ** commands specified by profile composition strings.
500 if ((cp = strchr(prefix, 'a')) == NULL)
501 adios(NULL, "internal error(4)");
504 ** Scan the contents. Choose a transfer encoding, and
505 ** check if prefix for multipart boundary clashes with
506 ** any of the contents.
508 while (scan_content(ct) == NOTOK) {
513 adios(NULL, "giving up trying to find a unique delimiter string");
519 /* Build the rest of the header field structures */
527 ** Set up structures for placing unencoded
528 ** content when building parts.
532 init_decoded_content(CT ct)
536 if ((ce = (CE) calloc(1, sizeof(*ce))) == NULL)
537 adios(NULL, "out of memory");
540 ct->c_ceopenfnx = open7Bit; /* since unencoded */
541 ct->c_ceclosefnx = close_encoding;
542 ct->c_cesizefnx = NULL; /* since unencoded */
549 fgetstr(char *s, int n, FILE *stream)
553 for (ep = (cp = s) + n; cp < ep; ) {
556 if (!fgets(cp, n, stream))
557 return (cp != s ? s : NULL);
558 if (cp == s && *cp != '#')
561 cp += (i = strlen(cp)) - 1;
562 if (i <= 1 || *cp-- != '\n' || *cp != '\\')
573 ** Parse the composition draft for text and directives.
574 ** Do initial setup of Content structure.
578 user_content(FILE *in, char *file, char *buf, CT *ctp)
586 struct str2init *s2i;
591 if (buf[0] == '\n' || strcmp(buf, "#\n") == 0) {
596 /* allocate basic Content structure */
597 if ((ct = (CT) calloc(1, sizeof(*ct))) == NULL)
598 adios(NULL, "out of memory");
601 /* allocate basic structure for handling decoded content */
602 init_decoded_content(ct);
609 ** Handle inline text. Check if line
610 ** is one of the following forms:
612 ** 1) doesn't begin with '#' (implicit directive)
613 ** 2) begins with "##" (implicit directive)
614 ** 3) begins with "#<"
616 if (buf[0] != '#' || buf[1] == '#' || buf[1] == '<') {
620 char content[BUFSIZ];
624 cp = m_mktemp2(NULL, invo_name, NULL, &out);
626 adios("mhbuild", "unable to create temporary file");
628 /* use a temp file to collect the plain text lines */
629 ce->ce_file = getcpy(cp);
632 if (buf[0] == '#' && buf[1] == '<') {
633 strncpy(content, buf + 2, sizeof(content));
640 /* the directive is implicit */
641 strncpy(content, "text/plain", sizeof(content));
643 strncpy(buffer, buf[0] != '#' ? buf : buf + 1, sizeof(buffer));
647 if (headers >= 0 && uprf(buffer, DESCR_FIELD) &&
648 buffer[i=strlen(DESCR_FIELD)] == ':') {
652 ct->c_descr = add(buffer + i + 1, ct->c_descr);
653 if (!fgetstr(buffer, sizeof(buffer) - 1, in))
654 adios(NULL, "end-of-file after %s: field in plaintext", DESCR_FIELD);
662 adios(NULL, "#-directive after %s: field in plaintext", DESCR_FIELD);
670 if (headers >= 0 && uprf(buffer, DISPO_FIELD)
671 && buffer[i = strlen(DISPO_FIELD)] == ':') {
675 ct->c_dispo = add(buffer + i + 1, ct->c_dispo);
676 if (!fgetstr(buffer, sizeof(buffer) - 1, in))
677 adios(NULL, "end-of-file after %s: field in plaintext", DISPO_FIELD);
685 adios(NULL, "#-directive after %s: field in plaintext", DISPO_FIELD);
693 if (headers != 1 || buffer[0] != '\n')
699 if ((cp = fgetstr(buffer, sizeof(buffer) - 1, in))
702 if (buffer[0] == '#') {
705 if (buffer[1] != '#')
707 for (cp = (bp = buffer) + 1; *cp; cp++)
715 /* parse content type */
716 if (get_ctinfo(content, ct, inlineD) == NOTOK)
717 /* sysexits.h EX_USAGE */
720 for (s2i = str2cts; s2i->si_key; s2i++)
721 if (!mh_strcasecmp(ci->ci_type, s2i->si_key))
723 if (!s2i->si_key && !uprf(ci->ci_type, "X-"))
727 ** check type specified (possibly implicitly)
729 switch (ct->c_type = s2i->si_val) {
731 if (!mh_strcasecmp(ci->ci_subtype, "rfc822")) {
732 ct->c_encoding = CE_7BIT;
737 adios(NULL, "it doesn't make sense to define an in-line %s content",
738 ct->c_type == CT_MESSAGE ? "message" :
744 if ((ct->c_ctinitfnx = s2i->si_init))
745 (*ct->c_ctinitfnx) (ct);
750 fseek(in, pos, SEEK_SET);
755 ** If we've reached this point, the next line
756 ** must be some type of explicit directive.
760 adios(NULL, "The #@ directive i.e. message/external-body "
761 "is not supported anymore.");
764 /* parse directive */
765 if (get_ctinfo(buf+1, ct, 1) == NOTOK)
766 /* sysexits.h EX_DATAERR */
769 /* check directive against the list of MIME types */
770 for (s2i = str2cts; s2i->si_key; s2i++)
771 if (!mh_strcasecmp(ci->ci_type, s2i->si_key))
775 ** Check if the directive specified a valid type.
776 ** This will happen if it was one of the following forms:
782 adios(NULL, "missing subtype in \"#%s\"", ci->ci_type);
784 switch (ct->c_type = s2i->si_val) {
786 adios(NULL, "use \"#begin ... #end\" instead of \"#%s/%s\"", ci->ci_type, ci->ci_subtype);
790 if (!mh_strcasecmp(ci->ci_subtype, "partial") ||
791 !mh_strcasecmp(ci->ci_subtype,
793 adios(NULL, "sorry, \"#%s/%s\" isn't supported", ci->ci_type, ci->ci_subtype);
796 admonish(NULL, "use \"#forw [+folder] [msgs]\" instead of \"#%s/%s\"", ci->ci_type, ci->ci_subtype);
800 if ((ct->c_ctinitfnx = s2i->si_init))
801 (*ct->c_ctinitfnx) (ct);
805 /* Handle [file] argument */
807 /* check if specifies command to execute */
808 if (*ci->ci_magic == '|' || *ci->ci_magic == '!') {
809 for (cp = ci->ci_magic + 1; isspace(*cp); cp++)
812 adios(NULL, "empty pipe command for #%s directive", ci->ci_type);
817 /* record filename of decoded contents */
818 ce->ce_file = ci->ci_magic;
819 if (access(ce->ce_file, R_OK) == NOTOK)
820 adios("reading", "unable to access %s for", ce->ce_file);
827 ** No [file] argument, so check profile for
828 ** method to compose content.
830 snprintf(buffer, sizeof(buffer), "%s-compose-%s/%s",
831 invo_name, ci->ci_type, ci->ci_subtype);
832 if ((cp = context_find(buffer)) == NULL || *cp == '\0') {
833 snprintf(buffer, sizeof(buffer), "%s-compose-%s",
834 invo_name, ci->ci_type);
835 if ((cp = context_find(buffer)) == NULL ||
837 content_error(NULL, ct, "don't know how to compose content");
838 /* sysexits.h EX_USAGE */
842 ci->ci_magic = getcpy(cp);
848 ** #forw [+folder] [msgs]
850 if (!mh_strcasecmp(ci->ci_type, "forw")) {
852 char *folder, *arguments[MAXARGS];
858 ap = brkstring(ci->ci_magic, " ", "\n");
859 for (i=0; ap[i] && i<MAXARGS-1; i++) {
860 arguments[i] = ap[i];
865 arguments[0] = seq_cur;
870 /* search the arguments for a folder name */
871 for (ap = arguments; *ap; ap++) {
873 if (*cp == '+' || *cp == '@') {
875 adios(NULL, "only one folder per #forw directive");
877 folder = getcpy(expandfol(cp));
881 /* else, use the current folder */
883 folder = getcpy(getcurfol());
885 if (!(mp = folder_read(folder)))
886 adios(NULL, "unable to read folder %s", folder);
887 for (ap = arguments; *ap; ap++) {
889 if (*cp != '+' && *cp != '@')
890 if (!m_convert(mp, cp))
891 /* sysexits.h EX_USAGE */
898 ** If there is more than one message to include, make this
899 ** a content of type "multipart/digest" and insert each message
900 ** as a subpart. If there is only one message, then make this
901 ** a content of type "message/rfc822".
903 if (mp->numsel > 1) {
904 /* we are forwarding multiple messages */
905 if (get_ctinfo("multipart/digest", ct, 0) == NOTOK)
906 /* sysexits.h EX_DATAERR */
908 ct->c_type = CT_MULTIPART;
909 ct->c_subtype = MULTI_DIGEST;
911 if ((m = (struct multipart *)
912 calloc(1, sizeof(*m))) == NULL)
913 adios(NULL, "out of memory");
914 ct->c_ctparams = (void *) m;
917 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
918 if (is_selected(mp, msgnum)) {
923 if ((p = (CT) calloc(1, sizeof(*p)))
925 adios(NULL, "out of memory");
926 init_decoded_content(p);
928 if (get_ctinfo("message/rfc822", p, 0)
930 /* sysexits.h EX_DATAERR */
932 p->c_type = CT_MESSAGE;
933 p->c_subtype = MESSAGE_RFC822;
935 snprintf(buffer, sizeof(buffer),
936 "%s/%d", mp->foldpath,
938 pe->ce_file = getcpy(buffer);
940 if ((part = (struct part *) calloc(1, sizeof(*part))) == NULL)
941 adios(NULL, "out of memory");
948 /* we are forwarding one message */
949 if (get_ctinfo("message/rfc822", ct, 0) == NOTOK)
950 /* sysexits.h EX_DATAERR */
952 ct->c_type = CT_MESSAGE;
953 ct->c_subtype = MESSAGE_RFC822;
956 snprintf(buffer, sizeof(buffer), "%s/%d",
957 mp->foldpath, msgnum);
958 ce->ce_file = getcpy(buffer);
961 folder_free(mp); /* free folder/message structure */
968 if (!mh_strcasecmp(ci->ci_type, "end")) {
975 ** #begin [ alternative | parallel ]
977 if (!mh_strcasecmp(ci->ci_type, "begin")) {
980 cp = SubMultiPart[vrsn - 1].kv_key;
981 } else if (!mh_strcasecmp(ci->ci_magic, "alternative")) {
982 vrsn = MULTI_ALTERNATE;
983 cp = SubMultiPart[vrsn - 1].kv_key;
984 } else if (!mh_strcasecmp(ci->ci_magic, "parallel")) {
985 vrsn = MULTI_PARALLEL;
986 cp = SubMultiPart[vrsn - 1].kv_key;
987 } else if (uprf(ci->ci_magic, "digest")) {
990 vrsn = MULTI_UNKNOWN;
995 snprintf(buffer, sizeof(buffer), "multipart/%s", cp);
996 if (get_ctinfo(buffer, ct, 0) == NOTOK)
997 /* sysexits.h EX_DATAERR */
999 ct->c_type = CT_MULTIPART;
1000 ct->c_subtype = vrsn;
1002 if ((m = (struct multipart *) calloc(1, sizeof(*m))) == NULL)
1003 adios(NULL, "out of memory");
1004 ct->c_ctparams = (void *) m;
1007 while (fgetstr(buffer, sizeof(buffer) - 1, in)) {
1011 if (user_content(in, file, buffer, &p) == DONE) {
1013 adios(NULL, "empty \"#begin ... #end\" sequence");
1019 if ((part = (struct part *)
1020 calloc(1, sizeof(*part))) == NULL)
1021 adios(NULL, "out of memory");
1023 pp = &part->mp_next;
1026 admonish(NULL, "premature end-of-file, missing #end");
1031 ** Unknown directive
1033 adios(NULL, "unknown directive \"#%s\"", ci->ci_type);
1034 return NOTOK; /* NOT REACHED */
1039 set_id(CT ct, int top)
1043 static time_t clock = 0;
1044 static char *msgfmt;
1048 snprintf(msgid, sizeof(msgid), "<%d.%ld.%%d@%s>\n",
1049 (int) getpid(), (long) clock, LocalName());
1051 msgfmt = getcpy(msgid);
1053 snprintf(msgid, sizeof(msgid), msgfmt, top ? 0 : ++partno);
1054 ct->c_id = getcpy(msgid);
1059 ** Fill out, or expand the various contents in the composition
1060 ** draft. Read-in any necessary files. Parse and execute any
1061 ** commands specified by profile composition strings.
1065 compose_content(CT ct)
1067 CE ce = ct->c_cefile;
1069 switch (ct->c_type) {
1074 char partnam[BUFSIZ];
1075 struct multipart *m = (struct multipart *) ct->c_ctparams;
1079 snprintf(partnam, sizeof(partnam), "%s.",
1081 pp = partnam + strlen(partnam);
1086 /* first, we call compose_content on all the subparts */
1087 for (part = m->mp_parts, partnum = 1; part;
1088 part = part->mp_next, partnum++) {
1089 CT p = part->mp_part;
1091 sprintf(pp, "%d", partnum);
1092 p->c_partno = getcpy(partnam);
1093 if (compose_content(p) == NOTOK)
1100 /* Nothing to do for type message */
1104 ** Discrete types (text/application/audio/image/video)
1109 int xstdout, len, buflen;
1110 char *bp, **ap, *cp;
1111 char *vec[4], buffer[BUFSIZ];
1113 CI ci = &ct->c_ctinfo;
1116 if (!(cp = ci->ci_magic))
1117 adios(NULL, "internal error(5)");
1119 tfile = m_mktemp2(NULL, invo_name, NULL, NULL);
1120 if (tfile == NULL) {
1121 adios("mhbuild", "unable to create temporary file");
1123 ce->ce_file = getcpy(tfile);
1128 /* Get buffer ready to go */
1131 buflen = sizeof(buffer);
1134 ** Parse composition string into buffer
1136 for ( ; *cp; cp++) {
1142 ** insert parameters from
1148 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
1149 snprintf(bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
1159 /* %f, and stdout is not-redirected */
1165 ** insert temporary filename
1166 ** where content should be
1169 snprintf(bp, buflen, "%s", ce->ce_file);
1173 /* insert content subtype */
1174 strncpy(bp, ci->ci_subtype, buflen);
1178 /* insert character % */
1199 printf("composing content %s/%s from command\n\t%s\n", ci->ci_type, ci->ci_subtype, buffer);
1201 fflush(stdout); /* not sure if need for -noverbose */
1208 if ((out = fopen(ce->ce_file, "w")) == NULL)
1209 adios(ce->ce_file, "unable to open for writing");
1211 switch (child_id = fork()) {
1213 adios("fork", "unable to fork");
1218 dup2(fileno(out), 1);
1220 execvp("/bin/sh", vec);
1221 fprintf(stderr, "unable to exec ");
1228 if (pidXwait(child_id, NULL))
1229 /* sysexits.h EX_SOFTWARE */
1242 ** Scan the content.
1244 ** 1) choose a transfer encoding.
1245 ** 2) check for clashes with multipart boundary string.
1246 ** 3) for text content, figure out which character set is being used.
1248 ** If there is a clash with one of the contents and the multipart boundary,
1249 ** this function will exit with NOTOK. This will cause the scanning process
1250 ** to be repeated with a different multipart boundary. It is possible
1251 ** (although highly unlikely) that this scan will be repeated multiple times.
1258 int check8bit = 0, contains8bit = 0;
1259 int checklinelen = 0, linelen = 0;
1260 int checkboundary = 0, boundaryclash = 0;
1261 int checklinespace = 0, linespace = 0; /* trailing whitespace */
1262 unsigned char *cp = NULL, buffer[BUFSIZ];
1263 struct text *t = NULL;
1265 CE ce = ct->c_cefile;
1268 ** handle multipart by scanning all subparts
1269 ** and then checking their encoding.
1271 if (ct->c_type == CT_MULTIPART) {
1272 struct multipart *m = (struct multipart *) ct->c_ctparams;
1275 /* initially mark the domain of enclosing multipart as 7bit */
1276 ct->c_encoding = CE_7BIT;
1278 for (part = m->mp_parts; part; part = part->mp_next) {
1279 CT p = part->mp_part;
1281 if (scan_content(p) == NOTOK) {
1282 /* choose encoding for subpart */
1287 ** if necessary, enlarge encoding for enclosing
1290 if (p->c_encoding == CE_BINARY)
1291 ct->c_encoding = CE_BINARY;
1292 if (p->c_encoding == CE_8BIT &&
1293 ct->c_encoding != CE_BINARY)
1294 ct->c_encoding = CE_8BIT;
1301 ** Decide what to check while scanning this content.
1303 switch (ct->c_type) {
1307 if (ct->c_subtype == TEXT_PLAIN) {
1323 case CT_APPLICATION:
1335 ** Scan the unencoded content
1337 if (check8bit || checklinelen || checklinespace || checkboundary) {
1338 if ((in = fopen(ce->ce_file, "r")) == NULL)
1339 adios(ce->ce_file, "unable to open for reading");
1340 len = strlen(prefix);
1342 while (fgets(buffer, sizeof(buffer) - 1, in)) {
1344 for (cp = buffer; *cp; cp++) {
1345 if (!isascii(*cp)) {
1347 /* no need to keep checking */
1353 if (checklinelen && (strlen(buffer) > CPERLIN + 1)) {
1355 checklinelen = 0; /* no need to keep checking */
1358 if (checklinespace &&
1359 (cp = buffer + strlen(buffer) - 2) >
1360 buffer && isspace(*cp)) {
1362 /* no need to keep checking */
1367 ** Check if content contains a line that clashes
1368 ** with our standard boundary for multipart messages.
1370 if (checkboundary && buffer[0] == '-' &&
1372 for (cp = buffer + strlen(buffer) - 1;
1377 if (strncmp(buffer + 2, prefix, len)==0 &&
1378 isdigit(buffer[2 + len])) {
1380 /* no need to keep checking */
1389 ** Decide which transfer encoding to use.
1391 switch (ct->c_type) {
1394 ** If the text content didn't specify a character
1395 ** set, we need to figure out which one was used.
1397 t = (struct text *) ct->c_ctparams;
1398 if (t->tx_charset == CHARSET_UNSPECIFIED) {
1399 CI ci = &ct->c_ctinfo;
1402 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++)
1406 t->tx_charset = CHARSET_UNKNOWN;
1407 *ap = concat("charset=", write_charset_8bit(),
1410 t->tx_charset = CHARSET_USASCII;
1411 *ap = getcpy("charset=us-ascii");
1414 cp = strchr(*ap++, '=');
1420 if (contains8bit || linelen || linespace)
1421 ct->c_encoding = CE_QUOTED;
1423 ct->c_encoding = CE_7BIT;
1427 ct->c_encoding = CE_7BIT;
1430 case CT_APPLICATION:
1435 ** Forcing use of base64, because these types likely
1436 ** contain binary data and NUL bytes. Don't care about
1437 ** files that would be clean.
1439 ct->c_encoding = CE_BASE64;
1443 return (boundaryclash ? NOTOK : OK);
1448 ** Scan the content structures, and build header
1449 ** fields that will need to be output into the
1454 build_headers(CT ct)
1458 char *np, *vp, buffer[BUFSIZ];
1459 CI ci = &ct->c_ctinfo;
1462 ** If message is type multipart, then add the multipart
1463 ** boundary to the list of attribute/value pairs.
1465 if (ct->c_type == CT_MULTIPART) {
1467 static int level = 0; /* store nesting level */
1471 snprintf(buffer, sizeof(buffer), "boundary=%s%d",
1473 cp = strchr(*ap++ = getcpy(buffer), '=');
1480 ** output the content type and subtype
1482 np = getcpy(TYPE_FIELD);
1483 vp = concat(" ", ci->ci_type, "/", ci->ci_subtype, NULL);
1485 /* keep track of length of line */
1486 len = strlen(TYPE_FIELD) + strlen(ci->ci_type) +
1487 strlen(ci->ci_subtype) + 3;
1490 ** Append the attribute/value pairs to
1491 ** the end of the Content-Type line.
1493 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
1497 snprintf(buffer, sizeof(buffer), "%s=\"%s\"", *ap, *ep);
1498 if (len + 1 + (cc = strlen(buffer)) >= CPERLIN) {
1499 vp = add("\n\t", vp);
1505 vp = add(buffer, vp);
1510 ** Append any RFC-822 comment to the end of
1511 ** the Content-Type line.
1513 if (ci->ci_comment) {
1514 snprintf(buffer, sizeof(buffer), "(%s)", ci->ci_comment);
1515 if (len + 1 + (cc = 2 + strlen(ci->ci_comment)) >= CPERLIN) {
1516 vp = add("\n\t", vp);
1522 vp = add(buffer, vp);
1526 add_header(ct, np, vp);
1529 ** output the Content-ID
1532 np = getcpy(ID_FIELD);
1533 vp = concat(" ", ct->c_id, NULL);
1534 add_header(ct, np, vp);
1538 ** output the Content-Description
1541 np = getcpy(DESCR_FIELD);
1542 vp = concat(" ", ct->c_descr, NULL);
1543 add_header(ct, np, vp);
1547 ** output the Content-Disposition
1550 np = getcpy(DISPO_FIELD);
1551 vp = concat(" ", ct->c_dispo, NULL);
1552 add_header(ct, np, vp);
1556 ** output the Content-Transfer-Encoding
1558 switch (ct->c_encoding) {
1560 /* Nothing to output */
1564 if (ct->c_type == CT_MESSAGE)
1565 adios(NULL, "internal error, invalid encoding");
1567 np = getcpy(ENCODING_FIELD);
1568 vp = concat(" ", "8bit", "\n", NULL);
1569 add_header(ct, np, vp);
1573 if (ct->c_type == CT_MESSAGE || ct->c_type == CT_MULTIPART)
1574 adios(NULL, "internal error, invalid encoding");
1576 np = getcpy(ENCODING_FIELD);
1577 vp = concat(" ", "quoted-printable", "\n", NULL);
1578 add_header(ct, np, vp);
1582 if (ct->c_type == CT_MESSAGE || ct->c_type == CT_MULTIPART)
1583 adios(NULL, "internal error, invalid encoding");
1585 np = getcpy(ENCODING_FIELD);
1586 vp = concat(" ", "base64", "\n", NULL);
1587 add_header(ct, np, vp);
1591 if (ct->c_type == CT_MESSAGE)
1592 adios(NULL, "internal error, invalid encoding");
1594 np = getcpy(ENCODING_FIELD);
1595 vp = concat(" ", "binary", "\n", NULL);
1596 add_header(ct, np, vp);
1600 adios(NULL, "unknown transfer encoding in content");
1605 ** Additional content specific header processing
1607 switch (ct->c_type) {
1610 struct multipart *m;
1613 m = (struct multipart *) ct->c_ctparams;
1614 for (part = m->mp_parts; part; part = part->mp_next) {