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);
226 /* output MIME message to this temporary file */
227 strncpy(outfile, m_mktemp(invo_name, NULL, &fp_out),
231 /* output the message */
232 output_message_fp(ct, fp_out, outfile);
235 /* output the temp file to standard output */
236 if ((fp = fopen(outfile, "r")) == NULL)
237 adios(outfile, "unable to open");
238 while (fgets(buffer, BUFSIZ, fp))
239 fputs(buffer, stdout);
253 ** Process the composition file from a file.
256 /* build the content structures for MIME message */
257 ct = build_mime(compfile);
261 /* output MIME message to this temporary file */
262 strncpy(outfile, m_mktemp2(compfile, invo_name, NULL, &fp_out),
266 /* output the message */
267 output_message_fp(ct, fp_out, outfile);
270 /* Rename composition draft */
271 snprintf(buffer, sizeof(buffer), "%s.orig", m_backup(compfile));
272 if (rename(compfile, buffer) == NOTOK) {
273 adios(compfile, "unable to rename comp draft %s to", buffer);
276 /* Rename output file to take its place */
277 if (rename(outfile, compfile) == NOTOK) {
278 advise(outfile, "unable to rename output %s to", compfile);
279 rename(buffer, compfile);
291 unlink_done(int status)
294 ** Check if we need to remove stray temporary files.
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
313 build_mime(char *infile)
316 char buf[BUFSIZ], name[NAMESZ];
325 /* open the composition draft */
326 if ((in = fopen(infile, "r")) == NULL)
327 adios(infile, "unable to open for reading");
330 ** Allocate space for primary (outside) content
332 if ((ct = (CT) calloc(1, sizeof(*ct))) == NULL)
333 adios(NULL, "out of memory");
336 ** Allocate structure for handling decoded content
337 ** for this part. We don't really need this, but
338 ** allocate it to remain consistent.
340 init_decoded_content(ct);
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.
347 for (compnum = 1, state = FLD;;) {
348 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
354 /* abort if draft has Mime-Version header field */
355 if (!mh_strcasecmp(name, VRSN_FIELD))
356 adios(NULL, "draft shouldn't contain %s: field", VRSN_FIELD);
359 ** abort if draft has Content-Transfer-Encoding
362 if (!mh_strcasecmp(name, ENCODING_FIELD))
363 adios(NULL, "draft shouldn't contain %s: field", ENCODING_FIELD);
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,
373 /* get copies of the buffers */
377 /* if necessary, get rest of field */
378 while (state == FLDPLUS) {
379 state = m_getfld(state, name, buf,
381 vp = add(buf, vp); /* add to prev value */
384 /* Now add the header data to the list */
385 add_header(ct, np, vp);
388 /* if this wasn't the last hdr field, then continue */
394 adios(NULL, "draft has empty body -- no directives!");
399 fseek(in, (long) (-strlen(buf)), SEEK_CUR);
404 adios(NULL, "message format error in component #%d",
408 adios(NULL, "getfld() returned %d", state);
414 ** Now add the MIME-Version header field
415 ** to the list of header fields.
417 np = getcpy(VRSN_FIELD);
418 vp = concat(" ", VRSN_VALUE, "\n", NULL);
419 add_header(ct, np, vp);
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.
426 if (get_ctinfo("multipart/mixed", ct, 0) == NOTOK)
428 ct->c_type = CT_MULTIPART;
429 ct->c_subtype = MULTI_MIXED;
430 ct->c_file = getcpy(infile);
432 if ((m = (struct multipart *) calloc(1, sizeof(*m))) == NULL)
433 adios(NULL, "out of memory");
434 ct->c_ctparams = (void *) m;
438 ** read and parse the composition file
439 ** and the directives it contains.
441 while (fgetstr(buf, sizeof(buf) - 1, in)) {
445 if (user_content(in, infile, buf, &p) == DONE) {
446 admonish(NULL, "ignoring spurious #end");
452 if ((part = (struct part *) calloc(1, sizeof(*part))) == NULL)
453 adios(NULL, "out of memory");
460 ** close the composition draft since
461 ** it's not needed any longer.
465 /* check if any contents were found */
467 adios(NULL, "no content directives found");
470 ** If only one content was found, then remove and
471 ** free the outer multipart content.
473 if (!m->mp_parts->mp_next) {
476 p = m->mp_parts->mp_part;
477 m->mp_parts->mp_part = NULL;
479 /* move header fields */
480 p->c_first_hf = ct->c_first_hf;
481 p->c_last_hf = ct->c_last_hf;
482 ct->c_first_hf = NULL;
483 ct->c_last_hf = NULL;
492 ** Fill out, or expand directives. Parse and execute
493 ** commands specified by profile composition strings.
497 if ((cp = strchr(prefix, 'a')) == NULL)
498 adios(NULL, "internal error(4)");
501 ** Scan the contents. Choose a transfer encoding, and
502 ** check if prefix for multipart boundary clashes with
503 ** any of the contents.
505 while (scan_content(ct) == NOTOK) {
510 adios(NULL, "giving up trying to find a unique delimiter string");
516 /* Build the rest of the header field structures */
524 ** Set up structures for placing unencoded
525 ** content when building parts.
529 init_decoded_content(CT ct)
533 if ((ce = (CE) calloc(1, sizeof(*ce))) == NULL)
534 adios(NULL, "out of memory");
537 ct->c_ceopenfnx = open7Bit; /* since unencoded */
538 ct->c_ceclosefnx = close_encoding;
539 ct->c_cesizefnx = NULL; /* since unencoded */
546 fgetstr(char *s, int n, FILE *stream)
550 for (ep = (cp = s) + n; cp < ep; ) {
553 if (!fgets(cp, n, stream))
554 return (cp != s ? s : NULL);
555 if (cp == s && *cp != '#')
558 cp += (i = strlen(cp)) - 1;
559 if (i <= 1 || *cp-- != '\n' || *cp != '\\')
570 ** Parse the composition draft for text and directives.
571 ** Do initial setup of Content structure.
575 user_content(FILE *in, char *file, char *buf, CT *ctp)
583 struct str2init *s2i;
588 if (buf[0] == '\n' || strcmp(buf, "#\n") == 0) {
593 /* allocate basic Content structure */
594 if ((ct = (CT) calloc(1, sizeof(*ct))) == NULL)
595 adios(NULL, "out of memory");
598 /* allocate basic structure for handling decoded content */
599 init_decoded_content(ct);
606 ** Handle inline text. Check if line
607 ** is one of the following forms:
609 ** 1) doesn't begin with '#' (implicit directive)
610 ** 2) begins with "##" (implicit directive)
611 ** 3) begins with "#<"
613 if (buf[0] != '#' || buf[1] == '#' || buf[1] == '<') {
617 char content[BUFSIZ];
621 cp = m_mktemp2(NULL, invo_name, NULL, &out);
623 adios("mhbuild", "unable to create temporary file");
625 /* use a temp file to collect the plain text lines */
626 ce->ce_file = getcpy(cp);
629 if (buf[0] == '#' && buf[1] == '<') {
630 strncpy(content, buf + 2, sizeof(content));
637 /* the directive is implicit */
638 strncpy(content, "text/plain", sizeof(content));
640 strncpy(buffer, buf[0] != '#' ? buf : buf + 1, sizeof(buffer));
644 if (headers >= 0 && uprf(buffer, DESCR_FIELD) &&
645 buffer[i=strlen(DESCR_FIELD)] == ':') {
649 ct->c_descr = add(buffer + i + 1, ct->c_descr);
650 if (!fgetstr(buffer, sizeof(buffer) - 1, in))
651 adios(NULL, "end-of-file after %s: field in plaintext", DESCR_FIELD);
659 adios(NULL, "#-directive after %s: field in plaintext", DESCR_FIELD);
667 if (headers >= 0 && uprf(buffer, DISPO_FIELD)
668 && buffer[i = strlen(DISPO_FIELD)] == ':') {
672 ct->c_dispo = add(buffer + i + 1, ct->c_dispo);
673 if (!fgetstr(buffer, sizeof(buffer) - 1, in))
674 adios(NULL, "end-of-file after %s: field in plaintext", DISPO_FIELD);
682 adios(NULL, "#-directive after %s: field in plaintext", DISPO_FIELD);
690 if (headers != 1 || buffer[0] != '\n')
696 if ((cp = fgetstr(buffer, sizeof(buffer) - 1, in))
699 if (buffer[0] == '#') {
702 if (buffer[1] != '#')
704 for (cp = (bp = buffer) + 1; *cp; cp++)
712 /* parse content type */
713 if (get_ctinfo(content, ct, inlineD) == NOTOK)
716 for (s2i = str2cts; s2i->si_key; s2i++)
717 if (!mh_strcasecmp(ci->ci_type, s2i->si_key))
719 if (!s2i->si_key && !uprf(ci->ci_type, "X-"))
723 ** check type specified (possibly implicitly)
725 switch (ct->c_type = s2i->si_val) {
727 if (!mh_strcasecmp(ci->ci_subtype, "rfc822")) {
728 ct->c_encoding = CE_7BIT;
733 adios(NULL, "it doesn't make sense to define an in-line %s content",
734 ct->c_type == CT_MESSAGE ? "message" :
740 if ((ct->c_ctinitfnx = s2i->si_init))
741 (*ct->c_ctinitfnx) (ct);
746 fseek(in, pos, SEEK_SET);
751 ** If we've reached this point, the next line
752 ** must be some type of explicit directive.
756 adios(NULL, "The #@ directive i.e. message/external-body "
757 "is not supported anymore.");
760 /* parse directive */
761 if (get_ctinfo(buf+1, ct, 1) == NOTOK)
764 /* check directive against the list of MIME types */
765 for (s2i = str2cts; s2i->si_key; s2i++)
766 if (!mh_strcasecmp(ci->ci_type, s2i->si_key))
770 ** Check if the directive specified a valid type.
771 ** This will happen if it was one of the following forms:
777 adios(NULL, "missing subtype in \"#%s\"", ci->ci_type);
779 switch (ct->c_type = s2i->si_val) {
781 adios(NULL, "use \"#begin ... #end\" instead of \"#%s/%s\"", ci->ci_type, ci->ci_subtype);
785 if (!mh_strcasecmp(ci->ci_subtype, "partial") ||
786 !mh_strcasecmp(ci->ci_subtype,
788 adios(NULL, "sorry, \"#%s/%s\" isn't supported", ci->ci_type, ci->ci_subtype);
791 adios(NULL, "use \"#forw [+folder] [msgs]\" instead of \"#%s/%s\"", ci->ci_type, ci->ci_subtype);
795 if ((ct->c_ctinitfnx = s2i->si_init))
796 (*ct->c_ctinitfnx) (ct);
800 /* Handle [file] argument */
802 /* check if specifies command to execute */
803 if (*ci->ci_magic == '|' || *ci->ci_magic == '!') {
804 for (cp = ci->ci_magic + 1; isspace(*cp); cp++)
807 adios(NULL, "empty pipe command for #%s directive", ci->ci_type);
812 /* record filename of decoded contents */
813 ce->ce_file = ci->ci_magic;
814 if (access(ce->ce_file, R_OK) == NOTOK)
815 adios("reading", "unable to access %s for", ce->ce_file);
822 ** No [file] argument, so check profile for
823 ** method to compose content.
825 snprintf(buffer, sizeof(buffer), "%s-compose-%s/%s",
826 invo_name, ci->ci_type, ci->ci_subtype);
827 if ((cp = context_find(buffer)) == NULL || *cp == '\0') {
828 snprintf(buffer, sizeof(buffer), "%s-compose-%s",
829 invo_name, ci->ci_type);
830 if ((cp = context_find(buffer)) == NULL ||
832 content_error(NULL, ct, "don't know how to compose content");
836 ci->ci_magic = getcpy(cp);
842 ** #forw [+folder] [msgs]
844 if (!mh_strcasecmp(ci->ci_type, "forw")) {
846 char *folder, *arguments[MAXARGS];
852 ap = brkstring(ci->ci_magic, " ", "\n");
853 for (i=0; ap[i] && i<MAXARGS-1; i++) {
854 arguments[i] = ap[i];
859 arguments[0] = seq_cur;
864 /* search the arguments for a folder name */
865 for (ap = arguments; *ap; ap++) {
867 if (*cp == '+' || *cp == '@') {
869 adios(NULL, "only one folder per #forw directive");
871 folder = getcpy(expandfol(cp));
875 /* else, use the current folder */
877 folder = getcpy(getcurfol());
879 if (!(mp = folder_read(folder)))
880 adios(NULL, "unable to read folder %s", folder);
881 for (ap = arguments; *ap; ap++) {
883 if (*cp != '+' && *cp != '@')
884 if (!m_convert(mp, cp))
891 ** If there is more than one message to include, make this
892 ** a content of type "multipart/digest" and insert each message
893 ** as a subpart. If there is only one message, then make this
894 ** a content of type "message/rfc822".
896 if (mp->numsel > 1) {
897 /* we are forwarding multiple messages */
898 if (get_ctinfo("multipart/digest", ct, 0) == NOTOK)
900 ct->c_type = CT_MULTIPART;
901 ct->c_subtype = MULTI_DIGEST;
903 if ((m = (struct multipart *)
904 calloc(1, sizeof(*m))) == NULL)
905 adios(NULL, "out of memory");
906 ct->c_ctparams = (void *) m;
909 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
910 if (is_selected(mp, msgnum)) {
915 if ((p = (CT) calloc(1, sizeof(*p)))
917 adios(NULL, "out of memory");
918 init_decoded_content(p);
920 if (get_ctinfo("message/rfc822", p, 0)
923 p->c_type = CT_MESSAGE;
924 p->c_subtype = MESSAGE_RFC822;
926 snprintf(buffer, sizeof(buffer),
927 "%s/%d", mp->foldpath,
929 pe->ce_file = getcpy(buffer);
931 if ((part = (struct part *) calloc(1, sizeof(*part))) == NULL)
932 adios(NULL, "out of memory");
939 /* we are forwarding one message */
940 if (get_ctinfo("message/rfc822", ct, 0) == NOTOK)
942 ct->c_type = CT_MESSAGE;
943 ct->c_subtype = MESSAGE_RFC822;
946 snprintf(buffer, sizeof(buffer), "%s/%d",
947 mp->foldpath, msgnum);
948 ce->ce_file = getcpy(buffer);
951 folder_free(mp); /* free folder/message structure */
958 if (!mh_strcasecmp(ci->ci_type, "end")) {
965 ** #begin [ alternative | parallel ]
967 if (!mh_strcasecmp(ci->ci_type, "begin")) {
970 cp = SubMultiPart[vrsn - 1].kv_key;
971 } else if (!mh_strcasecmp(ci->ci_magic, "alternative")) {
972 vrsn = MULTI_ALTERNATE;
973 cp = SubMultiPart[vrsn - 1].kv_key;
974 } else if (!mh_strcasecmp(ci->ci_magic, "parallel")) {
975 vrsn = MULTI_PARALLEL;
976 cp = SubMultiPart[vrsn - 1].kv_key;
977 } else if (uprf(ci->ci_magic, "digest")) {
980 vrsn = MULTI_UNKNOWN;
985 snprintf(buffer, sizeof(buffer), "multipart/%s", cp);
986 if (get_ctinfo(buffer, ct, 0) == NOTOK)
988 ct->c_type = CT_MULTIPART;
989 ct->c_subtype = vrsn;
991 if ((m = (struct multipart *) calloc(1, sizeof(*m))) == NULL)
992 adios(NULL, "out of memory");
993 ct->c_ctparams = (void *) m;
996 while (fgetstr(buffer, sizeof(buffer) - 1, in)) {
1000 if (user_content(in, file, buffer, &p) == DONE) {
1002 adios(NULL, "empty \"#begin ... #end\" sequence");
1008 if ((part = (struct part *)
1009 calloc(1, sizeof(*part))) == NULL)
1010 adios(NULL, "out of memory");
1012 pp = &part->mp_next;
1015 admonish(NULL, "premature end-of-file, missing #end");
1020 ** Unknown directive
1022 adios(NULL, "unknown directive \"#%s\"", ci->ci_type);
1023 return NOTOK; /* NOT REACHED */
1028 set_id(CT ct, int top)
1032 static time_t clock = 0;
1033 static char *msgfmt;
1037 snprintf(msgid, sizeof(msgid), "<%d.%ld.%%d@%s>\n",
1038 (int) getpid(), (long) clock, LocalName());
1040 msgfmt = getcpy(msgid);
1042 snprintf(msgid, sizeof(msgid), msgfmt, top ? 0 : ++partno);
1043 ct->c_id = getcpy(msgid);
1048 ** Fill out, or expand the various contents in the composition
1049 ** draft. Read-in any necessary files. Parse and execute any
1050 ** commands specified by profile composition strings.
1054 compose_content(CT ct)
1056 CE ce = ct->c_cefile;
1058 switch (ct->c_type) {
1063 char partnam[BUFSIZ];
1064 struct multipart *m = (struct multipart *) ct->c_ctparams;
1068 snprintf(partnam, sizeof(partnam), "%s.",
1070 pp = partnam + strlen(partnam);
1075 /* first, we call compose_content on all the subparts */
1076 for (part = m->mp_parts, partnum = 1; part;
1077 part = part->mp_next, partnum++) {
1078 CT p = part->mp_part;
1080 sprintf(pp, "%d", partnum);
1081 p->c_partno = getcpy(partnam);
1082 if (compose_content(p) == NOTOK)
1089 /* Nothing to do for type message */
1093 ** Discrete types (text/application/audio/image/video)
1098 int xstdout, len, buflen;
1099 char *bp, **ap, *cp;
1100 char *vec[4], buffer[BUFSIZ];
1102 CI ci = &ct->c_ctinfo;
1105 if (!(cp = ci->ci_magic))
1106 adios(NULL, "internal error(5)");
1108 tfile = m_mktemp2(NULL, invo_name, NULL, NULL);
1109 if (tfile == NULL) {
1110 adios("mhbuild", "unable to create temporary file");
1112 ce->ce_file = getcpy(tfile);
1117 /* Get buffer ready to go */
1120 buflen = sizeof(buffer);
1123 ** Parse composition string into buffer
1125 for ( ; *cp; cp++) {
1131 ** insert parameters from
1137 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
1138 snprintf(bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
1148 /* %f, and stdout is not-redirected */
1154 ** insert temporary filename
1155 ** where content should be
1158 snprintf(bp, buflen, "%s", ce->ce_file);
1162 /* insert content subtype */
1163 strncpy(bp, ci->ci_subtype, buflen);
1167 /* insert character % */
1188 printf("composing content %s/%s from command\n\t%s\n", ci->ci_type, ci->ci_subtype, buffer);
1190 fflush(stdout); /* not sure if need for -noverbose */
1197 if ((out = fopen(ce->ce_file, "w")) == NULL)
1198 adios(ce->ce_file, "unable to open for writing");
1200 switch (child_id = fork()) {
1202 adios("fork", "unable to fork");
1207 dup2(fileno(out), 1);
1209 execvp("/bin/sh", vec);
1210 fprintf(stderr, "unable to exec ");
1217 if (pidXwait(child_id, NULL))
1230 ** Scan the content.
1232 ** 1) choose a transfer encoding.
1233 ** 2) check for clashes with multipart boundary string.
1234 ** 3) for text content, figure out which character set is being used.
1236 ** If there is a clash with one of the contents and the multipart boundary,
1237 ** this function will exit with NOTOK. This will cause the scanning process
1238 ** to be repeated with a different multipart boundary. It is possible
1239 ** (although highly unlikely) that this scan will be repeated multiple times.
1246 int check8bit = 0, contains8bit = 0; /* check if contains 8bit data */
1247 int checklinelen = 0, linelen = 0; /* check for long lines */
1248 int checkboundary = 0, boundaryclash = 0; /* check if clashes with multipart boundary */
1249 int checklinespace = 0, linespace = 0; /* check if any line ends with space */
1250 unsigned char *cp = NULL, buffer[BUFSIZ];
1251 struct text *t = NULL;
1253 CE ce = ct->c_cefile;
1256 ** handle multipart by scanning all subparts
1257 ** and then checking their encoding.
1259 if (ct->c_type == CT_MULTIPART) {
1260 struct multipart *m = (struct multipart *) ct->c_ctparams;
1263 /* initially mark the domain of enclosing multipart as 7bit */
1264 ct->c_encoding = CE_7BIT;
1266 for (part = m->mp_parts; part; part = part->mp_next) {
1267 CT p = part->mp_part;
1269 if (scan_content(p) == NOTOK) {
1270 /* choose encoding for subpart */
1275 ** if necessary, enlarge encoding for enclosing
1278 if (p->c_encoding == CE_BINARY)
1279 ct->c_encoding = CE_BINARY;
1280 if (p->c_encoding == CE_8BIT &&
1281 ct->c_encoding != CE_BINARY)
1282 ct->c_encoding = CE_8BIT;
1289 ** Decide what to check while scanning this content.
1291 switch (ct->c_type) {
1295 if (ct->c_subtype == TEXT_PLAIN) {
1304 case CT_APPLICATION:
1322 ** Don't check anything for these types,
1323 ** since we are forcing use of base64.
1333 ** Scan the unencoded content
1335 if (check8bit || checklinelen || checklinespace || checkboundary) {
1336 if ((in = fopen(ce->ce_file, "r")) == NULL)
1337 adios(ce->ce_file, "unable to open for reading");
1338 len = strlen(prefix);
1340 while (fgets(buffer, sizeof(buffer) - 1, in)) {
1342 ** Check for 8bit data.
1345 for (cp = buffer; *cp; cp++) {
1346 if (!isascii(*cp)) {
1348 /* no need to keep checking */
1355 ** Check line length.
1357 if (checklinelen && (strlen(buffer) > CPERLIN + 1)) {
1359 checklinelen = 0; /* no need to keep checking */
1363 ** Check if line ends with a space.
1365 if (checklinespace &&
1366 (cp = buffer + strlen(buffer) - 2) >
1367 buffer && isspace(*cp)) {
1369 /* no need to keep checking */
1374 ** Check if content contains a line that clashes
1375 ** with our standard boundary for multipart messages.
1377 if (checkboundary && buffer[0] == '-' &&
1379 for (cp = buffer + strlen(buffer) - 1;
1384 if (strncmp(buffer + 2, prefix, len)==0 &&
1385 isdigit(buffer[2 + len])) {
1387 /* no need to keep checking */
1396 ** Decide which transfer encoding to use.
1398 switch (ct->c_type) {
1401 ** If the text content didn't specify a character
1402 ** set, we need to figure out which one was used.
1404 t = (struct text *) ct->c_ctparams;
1405 if (t->tx_charset == CHARSET_UNSPECIFIED) {
1406 CI ci = &ct->c_ctinfo;
1409 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++)
1413 t->tx_charset = CHARSET_UNKNOWN;
1414 *ap = concat("charset=", write_charset_8bit(),
1417 t->tx_charset = CHARSET_USASCII;
1418 *ap = getcpy("charset=us-ascii");
1421 cp = strchr(*ap++, '=');
1427 if (contains8bit || linelen || linespace)
1428 ct->c_encoding = CE_QUOTED;
1430 ct->c_encoding = CE_7BIT;
1433 case CT_APPLICATION:
1434 /* For application type, use base64, except when postscript */
1435 if (contains8bit || linelen || linespace)
1436 ct->c_encoding = (ct->c_subtype ==
1437 APPLICATION_POSTSCRIPT) ?
1438 CE_QUOTED : CE_BASE64;
1440 ct->c_encoding = CE_7BIT;
1444 ct->c_encoding = CE_7BIT;
1450 /* For audio, image, and video contents, just use base64 */
1451 ct->c_encoding = CE_BASE64;
1455 return (boundaryclash ? NOTOK : OK);
1460 ** Scan the content structures, and build header
1461 ** fields that will need to be output into the
1466 build_headers(CT ct)
1470 char *np, *vp, buffer[BUFSIZ];
1471 CI ci = &ct->c_ctinfo;
1474 ** If message is type multipart, then add the multipart
1475 ** boundary to the list of attribute/value pairs.
1477 if (ct->c_type == CT_MULTIPART) {
1479 static int level = 0; /* store nesting level */
1483 snprintf(buffer, sizeof(buffer), "boundary=%s%d",
1485 cp = strchr(*ap++ = getcpy(buffer), '=');
1492 ** output the content type and subtype
1494 np = getcpy(TYPE_FIELD);
1495 vp = concat(" ", ci->ci_type, "/", ci->ci_subtype, NULL);
1497 /* keep track of length of line */
1498 len = strlen(TYPE_FIELD) + strlen(ci->ci_type) +
1499 strlen(ci->ci_subtype) + 3;
1502 ** Append the attribute/value pairs to
1503 ** the end of the Content-Type line.
1505 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
1509 snprintf(buffer, sizeof(buffer), "%s=\"%s\"", *ap, *ep);
1510 if (len + 1 + (cc = strlen(buffer)) >= CPERLIN) {
1511 vp = add("\n\t", vp);
1517 vp = add(buffer, vp);
1522 ** Append any RFC-822 comment to the end of
1523 ** the Content-Type line.
1525 if (ci->ci_comment) {
1526 snprintf(buffer, sizeof(buffer), "(%s)", ci->ci_comment);
1527 if (len + 1 + (cc = 2 + strlen(ci->ci_comment)) >= CPERLIN) {
1528 vp = add("\n\t", vp);
1534 vp = add(buffer, vp);
1538 add_header(ct, np, vp);
1541 ** output the Content-ID
1544 np = getcpy(ID_FIELD);
1545 vp = concat(" ", ct->c_id, NULL);
1546 add_header(ct, np, vp);
1550 ** output the Content-Description
1553 np = getcpy(DESCR_FIELD);
1554 vp = concat(" ", ct->c_descr, NULL);
1555 add_header(ct, np, vp);
1559 ** output the Content-Disposition
1562 np = getcpy(DISPO_FIELD);
1563 vp = concat(" ", ct->c_dispo, NULL);
1564 add_header(ct, np, vp);
1568 ** output the Content-Transfer-Encoding
1570 switch (ct->c_encoding) {
1572 /* Nothing to output */
1576 if (ct->c_type == CT_MESSAGE)
1577 adios(NULL, "internal error, invalid encoding");
1579 np = getcpy(ENCODING_FIELD);
1580 vp = concat(" ", "8bit", "\n", NULL);
1581 add_header(ct, np, vp);
1585 if (ct->c_type == CT_MESSAGE || ct->c_type == CT_MULTIPART)
1586 adios(NULL, "internal error, invalid encoding");
1588 np = getcpy(ENCODING_FIELD);
1589 vp = concat(" ", "quoted-printable", "\n", NULL);
1590 add_header(ct, np, vp);
1594 if (ct->c_type == CT_MESSAGE || ct->c_type == CT_MULTIPART)
1595 adios(NULL, "internal error, invalid encoding");
1597 np = getcpy(ENCODING_FIELD);
1598 vp = concat(" ", "base64", "\n", NULL);
1599 add_header(ct, np, vp);
1603 if (ct->c_type == CT_MESSAGE)
1604 adios(NULL, "internal error, invalid encoding");
1606 np = getcpy(ENCODING_FIELD);
1607 vp = concat(" ", "binary", "\n", NULL);
1608 add_header(ct, np, vp);
1612 adios(NULL, "unknown transfer encoding in content");
1617 ** Additional content specific header processing
1619 switch (ct->c_type) {
1622 struct multipart *m;
1625 m = (struct multipart *) ct->c_ctparams;
1626 for (part = m->mp_parts; part; part = part->mp_next) {