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[] = {
38 #define NCONTENTIDSW 3
51 ** Directory to place tmp files. This must
52 ** be set before these routines are called.
58 static char prefix[] = "----- =_aaaaaaaaaa";
66 int make_intermediates(char *);
67 void content_error(char *, CT, char *, ...);
70 void free_content(CT);
72 void free_encoding(CT, int);
77 static int init_decoded_content(CT);
78 static char *fgetstr(char *, int, FILE *);
79 static int user_content(FILE *, char *, char *, CT *);
80 static void set_id(CT, int);
81 static int compose_content(CT);
82 static int scan_content(CT);
83 static int build_headers(CT);
84 static CT build_mime(char *);
95 static char infile[BUFSIZ];
96 static int unlink_infile = 0;
98 static char outfile[BUFSIZ];
99 static int unlink_outfile = 0;
101 static void unlink_done(int) NORETURN;
104 int output_message(CT, char *);
105 int output_message_fp(CT, FILE *, char*);
108 void set_endian(void);
111 void free_content(CT);
115 main(int argc, char **argv)
117 char *cp, buf[BUFSIZ];
118 char buffer[BUFSIZ], *compfile = NULL;
119 char **argp, **arguments;
127 setlocale(LC_ALL, "");
129 invo_name = mhbasename(argv[0]);
131 /* read user profile/context */
134 arguments = getarguments(invo_name, argc, argv, 1);
137 while ((cp = *argp++)) {
138 if (cp[0] == '-' && cp[1] == '\0') {
140 adios(NULL, "cannot specify both standard input and a file");
143 verbosw = 0; /* turn off -verbose listings */
147 switch (smatch(++cp, switches)) {
149 ambigsw(cp, switches);
152 adios(NULL, "-%s unknown", cp);
155 snprintf(buf, sizeof(buf), "%s [switches] file", invo_name);
156 print_help(buf, switches, 1);
159 print_version(invo_name);
181 adios(NULL, "only one composition file allowed");
189 ** Check if we've specified an additional profile
191 if ((cp = getenv("MHBUILD"))) {
192 if ((fp = fopen(cp, "r"))) {
193 readconfig((struct node **) 0, fp, cp, 0);
196 admonish("", "unable to read $MHBUILD profile (%s)",
202 ** Read the standard profile setup
204 if ((fp = fopen(cp = etcpath("mhn.defaults"), "r"))) {
205 readconfig((struct node **) 0, fp, cp, 0);
210 ** Check for storage directory. If defined, we
211 ** will store temporary files there. Else we
212 ** store them in standard nmh directory.
214 if ((cp = context_find(nmhstorage)) && *cp)
215 tmp = concat(cp, "/", invo_name, NULL);
217 tmp = getcpy(toabsdir(invo_name));
219 /* Check if we have a file to process */
221 adios(NULL, "need to specify a %s composition file",
225 ** Process the composition file from standard input.
227 if (compfile[0] == '-' && compfile[1] == '\0') {
228 /* copy standard input to temporary file */
229 strncpy(infile, m_mktemp(invo_name, NULL, &fp),
231 while (fgets(buffer, BUFSIZ, stdin))
236 /* build the content structures for MIME message */
237 ct = build_mime(infile);
241 /* output MIME message to this temporary file */
242 strncpy(outfile, m_mktemp(invo_name, NULL, &fp_out),
246 /* output the message */
247 output_message_fp(ct, fp_out, outfile);
250 /* output the temp file to standard output */
251 if ((fp = fopen(outfile, "r")) == NULL)
252 adios(outfile, "unable to open");
253 while (fgets(buffer, BUFSIZ, fp))
254 fputs(buffer, stdout);
268 ** Process the composition file from a file.
271 /* build the content structures for MIME message */
272 ct = build_mime(compfile);
276 /* output MIME message to this temporary file */
277 strncpy(outfile, m_mktemp2(compfile, invo_name, NULL, &fp_out),
281 /* output the message */
282 output_message_fp(ct, fp_out, outfile);
285 /* Rename composition draft */
286 snprintf(buffer, sizeof(buffer), "%s.orig", m_backup(compfile));
287 if (rename(compfile, buffer) == NOTOK) {
288 adios(compfile, "unable to rename comp draft %s to", buffer);
291 /* Rename output file to take its place */
292 if (rename(outfile, compfile) == NOTOK) {
293 advise(outfile, "unable to rename output %s to", compfile);
294 rename(buffer, compfile);
306 unlink_done(int status)
309 ** Check if we need to remove stray temporary files.
320 ** Main routine for translating composition file
321 ** into valid MIME message. It translates the draft
322 ** into a content structure (actually a tree of content
323 ** structures). This message then can be manipulated
324 ** in various ways, including being output via
328 build_mime(char *infile)
331 char buf[BUFSIZ], name[NAMESZ];
340 /* open the composition draft */
341 if ((in = fopen(infile, "r")) == NULL)
342 adios(infile, "unable to open for reading");
345 ** Allocate space for primary (outside) content
347 if ((ct = (CT) calloc(1, sizeof(*ct))) == NULL)
348 adios(NULL, "out of memory");
351 ** Allocate structure for handling decoded content
352 ** for this part. We don't really need this, but
353 ** allocate it to remain consistent.
355 init_decoded_content(ct);
358 ** Parse some of the header fields in the composition
359 ** draft into the linked list of header fields for
360 ** the new MIME message.
362 for (compnum = 1, state = FLD;;) {
363 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
369 /* abort if draft has Mime-Version header field */
370 if (!mh_strcasecmp(name, VRSN_FIELD))
371 adios(NULL, "draft shouldn't contain %s: field", VRSN_FIELD);
374 ** abort if draft has Content-Transfer-Encoding
377 if (!mh_strcasecmp(name, ENCODING_FIELD))
378 adios(NULL, "draft shouldn't contain %s: field", ENCODING_FIELD);
380 /* ignore any Content-Type fields in the header */
381 if (!mh_strcasecmp(name, TYPE_FIELD)) {
382 while (state == FLDPLUS)
383 state = m_getfld(state, name, buf,
388 /* get copies of the buffers */
392 /* if necessary, get rest of field */
393 while (state == FLDPLUS) {
394 state = m_getfld(state, name, buf,
396 vp = add(buf, vp); /* add to prev value */
399 /* Now add the header data to the list */
400 add_header(ct, np, vp);
403 /* if this wasn't the last hdr field, then continue */
409 adios(NULL, "draft has empty body -- no directives!");
414 fseek(in, (long) (-strlen(buf)), SEEK_CUR);
419 adios(NULL, "message format error in component #%d",
423 adios(NULL, "getfld() returned %d", state);
429 ** Now add the MIME-Version header field
430 ** to the list of header fields.
432 np = getcpy(VRSN_FIELD);
433 vp = concat(" ", VRSN_VALUE, "\n", NULL);
434 add_header(ct, np, vp);
437 ** We initally assume we will find multiple contents in the
438 ** draft. So create a multipart/mixed content to hold everything.
439 ** We can remove this later, if it is not needed.
441 if (get_ctinfo("multipart/mixed", ct, 0) == NOTOK)
443 ct->c_type = CT_MULTIPART;
444 ct->c_subtype = MULTI_MIXED;
445 ct->c_file = getcpy(infile);
447 if ((m = (struct multipart *) calloc(1, sizeof(*m))) == NULL)
448 adios(NULL, "out of memory");
449 ct->c_ctparams = (void *) m;
453 ** read and parse the composition file
454 ** and the directives it contains.
456 while (fgetstr(buf, sizeof(buf) - 1, in)) {
460 if (user_content(in, infile, buf, &p) == DONE) {
461 admonish(NULL, "ignoring spurious #end");
467 if ((part = (struct part *) calloc(1, sizeof(*part))) == NULL)
468 adios(NULL, "out of memory");
475 ** close the composition draft since
476 ** it's not needed any longer.
480 /* check if any contents were found */
482 adios(NULL, "no content directives found");
485 ** If only one content was found, then remove and
486 ** free the outer multipart content.
488 if (!m->mp_parts->mp_next) {
491 p = m->mp_parts->mp_part;
492 m->mp_parts->mp_part = NULL;
494 /* move header fields */
495 p->c_first_hf = ct->c_first_hf;
496 p->c_last_hf = ct->c_last_hf;
497 ct->c_first_hf = NULL;
498 ct->c_last_hf = NULL;
507 ** Fill out, or expand directives. Parse and execute
508 ** commands specified by profile composition strings.
512 if ((cp = strchr(prefix, 'a')) == NULL)
513 adios(NULL, "internal error(4)");
516 ** Scan the contents. Choose a transfer encoding, and
517 ** check if prefix for multipart boundary clashes with
518 ** any of the contents.
520 while (scan_content(ct) == NOTOK) {
525 adios(NULL, "giving up trying to find a unique delimiter string");
531 /* Build the rest of the header field structures */
539 ** Set up structures for placing unencoded
540 ** content when building parts.
544 init_decoded_content(CT ct)
548 if ((ce = (CE) calloc(1, sizeof(*ce))) == NULL)
549 adios(NULL, "out of memory");
552 ct->c_ceopenfnx = open7Bit; /* since unencoded */
553 ct->c_ceclosefnx = close_encoding;
554 ct->c_cesizefnx = NULL; /* since unencoded */
561 fgetstr(char *s, int n, FILE *stream)
565 for (ep = (cp = s) + n; cp < ep; ) {
568 if (!fgets(cp, n, stream))
569 return (cp != s ? s : NULL);
570 if (cp == s && *cp != '#')
573 cp += (i = strlen(cp)) - 1;
574 if (i <= 1 || *cp-- != '\n' || *cp != '\\')
585 ** Parse the composition draft for text and directives.
586 ** Do initial setup of Content structure.
590 user_content(FILE *in, char *file, char *buf, CT *ctp)
598 struct str2init *s2i;
603 if (buf[0] == '\n' || strcmp(buf, "#\n") == 0) {
608 /* allocate basic Content structure */
609 if ((ct = (CT) calloc(1, sizeof(*ct))) == NULL)
610 adios(NULL, "out of memory");
613 /* allocate basic structure for handling decoded content */
614 init_decoded_content(ct);
621 ** Handle inline text. Check if line
622 ** is one of the following forms:
624 ** 1) doesn't begin with '#' (implicit directive)
625 ** 2) begins with "##" (implicit directive)
626 ** 3) begins with "#<"
628 if (buf[0] != '#' || buf[1] == '#' || buf[1] == '<') {
632 char content[BUFSIZ];
636 cp = m_mktemp2(NULL, invo_name, NULL, &out);
638 adios("mhbuild", "unable to create temporary file");
640 /* use a temp file to collect the plain text lines */
641 ce->ce_file = getcpy(cp);
644 if (buf[0] == '#' && buf[1] == '<') {
645 strncpy(content, buf + 2, sizeof(content));
652 /* the directive is implicit */
653 strncpy(content, "text/plain", sizeof(content));
655 strncpy(buffer, buf[0] != '#' ? buf : buf + 1, sizeof(buffer));
659 if (headers >= 0 && uprf(buffer, DESCR_FIELD) &&
660 buffer[i=strlen(DESCR_FIELD)] == ':') {
664 ct->c_descr = add(buffer + i + 1, ct->c_descr);
665 if (!fgetstr(buffer, sizeof(buffer) - 1, in))
666 adios(NULL, "end-of-file after %s: field in plaintext", DESCR_FIELD);
674 adios(NULL, "#-directive after %s: field in plaintext", DESCR_FIELD);
682 if (headers >= 0 && uprf(buffer, DISPO_FIELD)
683 && buffer[i = strlen(DISPO_FIELD)] == ':') {
687 ct->c_dispo = add(buffer + i + 1, ct->c_dispo);
688 if (!fgetstr(buffer, sizeof(buffer) - 1, in))
689 adios(NULL, "end-of-file after %s: field in plaintext", DISPO_FIELD);
697 adios(NULL, "#-directive after %s: field in plaintext", DISPO_FIELD);
705 if (headers != 1 || buffer[0] != '\n')
711 if ((cp = fgetstr(buffer, sizeof(buffer) - 1, in))
714 if (buffer[0] == '#') {
717 if (buffer[1] != '#')
719 for (cp = (bp = buffer) + 1; *cp; cp++)
727 /* parse content type */
728 if (get_ctinfo(content, ct, inlineD) == NOTOK)
731 for (s2i = str2cts; s2i->si_key; s2i++)
732 if (!mh_strcasecmp(ci->ci_type, s2i->si_key))
734 if (!s2i->si_key && !uprf(ci->ci_type, "X-"))
738 ** check type specified (possibly implicitly)
740 switch (ct->c_type = s2i->si_val) {
742 if (!mh_strcasecmp(ci->ci_subtype, "rfc822")) {
743 ct->c_encoding = CE_7BIT;
748 adios(NULL, "it doesn't make sense to define an in-line %s content",
749 ct->c_type == CT_MESSAGE ? "message" :
755 if ((ct->c_ctinitfnx = s2i->si_init))
756 (*ct->c_ctinitfnx) (ct);
761 fseek(in, pos, SEEK_SET);
766 ** If we've reached this point, the next line
767 ** must be some type of explicit directive.
771 adios(NULL, "The #@ directive i.e. message/external-body "
772 "is not supported anymore.");
775 /* parse directive */
776 if (get_ctinfo(buf+1, ct, 1) == NOTOK)
779 /* check directive against the list of MIME types */
780 for (s2i = str2cts; s2i->si_key; s2i++)
781 if (!mh_strcasecmp(ci->ci_type, s2i->si_key))
785 ** Check if the directive specified a valid type.
786 ** This will happen if it was one of the following forms:
792 adios(NULL, "missing subtype in \"#%s\"", ci->ci_type);
794 switch (ct->c_type = s2i->si_val) {
796 adios(NULL, "use \"#begin ... #end\" instead of \"#%s/%s\"", ci->ci_type, ci->ci_subtype);
800 if (!mh_strcasecmp(ci->ci_subtype, "partial") ||
801 !mh_strcasecmp(ci->ci_subtype,
803 adios(NULL, "sorry, \"#%s/%s\" isn't supported", ci->ci_type, ci->ci_subtype);
806 adios(NULL, "use \"#forw [+folder] [msgs]\" instead of \"#%s/%s\"", ci->ci_type, ci->ci_subtype);
810 if ((ct->c_ctinitfnx = s2i->si_init))
811 (*ct->c_ctinitfnx) (ct);
815 /* Handle [file] argument */
817 /* check if specifies command to execute */
818 if (*ci->ci_magic == '|' || *ci->ci_magic == '!') {
819 for (cp = ci->ci_magic + 1; isspace(*cp); cp++)
822 adios(NULL, "empty pipe command for #%s directive", ci->ci_type);
827 /* record filename of decoded contents */
828 ce->ce_file = ci->ci_magic;
829 if (access(ce->ce_file, R_OK) == NOTOK)
830 adios("reading", "unable to access %s for", ce->ce_file);
837 ** No [file] argument, so check profile for
838 ** method to compose content.
840 snprintf(buffer, sizeof(buffer), "%s-compose-%s/%s",
841 invo_name, ci->ci_type, ci->ci_subtype);
842 if ((cp = context_find(buffer)) == NULL || *cp == '\0') {
843 snprintf(buffer, sizeof(buffer), "%s-compose-%s",
844 invo_name, ci->ci_type);
845 if ((cp = context_find(buffer)) == NULL ||
847 content_error(NULL, ct, "don't know how to compose content");
851 ci->ci_magic = getcpy(cp);
857 ** #forw [+folder] [msgs]
859 if (!mh_strcasecmp(ci->ci_type, "forw")) {
861 char *folder, *arguments[MAXARGS];
867 ap = brkstring(ci->ci_magic, " ", "\n");
868 for (i=0; ap[i] && i<MAXARGS-1; i++) {
869 arguments[i] = ap[i];
874 arguments[0] = seq_cur;
879 /* search the arguments for a folder name */
880 for (ap = arguments; *ap; ap++) {
882 if (*cp == '+' || *cp == '@') {
884 adios(NULL, "only one folder per #forw directive");
886 folder = getcpy(expandfol(cp));
890 /* else, use the current folder */
892 folder = getcpy(getcurfol());
894 if (!(mp = folder_read(folder)))
895 adios(NULL, "unable to read folder %s", folder);
896 for (ap = arguments; *ap; ap++) {
898 if (*cp != '+' && *cp != '@')
899 if (!m_convert(mp, cp))
906 ** If there is more than one message to include, make this
907 ** a content of type "multipart/digest" and insert each message
908 ** as a subpart. If there is only one message, then make this
909 ** a content of type "message/rfc822".
911 if (mp->numsel > 1) {
912 /* we are forwarding multiple messages */
913 if (get_ctinfo("multipart/digest", ct, 0) == NOTOK)
915 ct->c_type = CT_MULTIPART;
916 ct->c_subtype = MULTI_DIGEST;
918 if ((m = (struct multipart *)
919 calloc(1, sizeof(*m))) == NULL)
920 adios(NULL, "out of memory");
921 ct->c_ctparams = (void *) m;
924 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
925 if (is_selected(mp, msgnum)) {
930 if ((p = (CT) calloc(1, sizeof(*p)))
932 adios(NULL, "out of memory");
933 init_decoded_content(p);
935 if (get_ctinfo("message/rfc822", p, 0)
938 p->c_type = CT_MESSAGE;
939 p->c_subtype = MESSAGE_RFC822;
941 snprintf(buffer, sizeof(buffer),
942 "%s/%d", mp->foldpath,
944 pe->ce_file = getcpy(buffer);
946 if ((part = (struct part *) calloc(1, sizeof(*part))) == NULL)
947 adios(NULL, "out of memory");
954 /* we are forwarding one message */
955 if (get_ctinfo("message/rfc822", ct, 0) == NOTOK)
957 ct->c_type = CT_MESSAGE;
958 ct->c_subtype = MESSAGE_RFC822;
961 snprintf(buffer, sizeof(buffer), "%s/%d",
962 mp->foldpath, msgnum);
963 ce->ce_file = getcpy(buffer);
966 folder_free(mp); /* free folder/message structure */
973 if (!mh_strcasecmp(ci->ci_type, "end")) {
980 ** #begin [ alternative | parallel ]
982 if (!mh_strcasecmp(ci->ci_type, "begin")) {
985 cp = SubMultiPart[vrsn - 1].kv_key;
986 } else if (!mh_strcasecmp(ci->ci_magic, "alternative")) {
987 vrsn = MULTI_ALTERNATE;
988 cp = SubMultiPart[vrsn - 1].kv_key;
989 } else if (!mh_strcasecmp(ci->ci_magic, "parallel")) {
990 vrsn = MULTI_PARALLEL;
991 cp = SubMultiPart[vrsn - 1].kv_key;
992 } else if (uprf(ci->ci_magic, "digest")) {
995 vrsn = MULTI_UNKNOWN;
1000 snprintf(buffer, sizeof(buffer), "multipart/%s", cp);
1001 if (get_ctinfo(buffer, ct, 0) == NOTOK)
1003 ct->c_type = CT_MULTIPART;
1004 ct->c_subtype = vrsn;
1006 if ((m = (struct multipart *) calloc(1, sizeof(*m))) == NULL)
1007 adios(NULL, "out of memory");
1008 ct->c_ctparams = (void *) m;
1011 while (fgetstr(buffer, sizeof(buffer) - 1, in)) {
1015 if (user_content(in, file, buffer, &p) == DONE) {
1017 adios(NULL, "empty \"#begin ... #end\" sequence");
1023 if ((part = (struct part *)
1024 calloc(1, sizeof(*part))) == NULL)
1025 adios(NULL, "out of memory");
1027 pp = &part->mp_next;
1030 admonish(NULL, "premature end-of-file, missing #end");
1035 ** Unknown directive
1037 adios(NULL, "unknown directive \"#%s\"", ci->ci_type);
1038 return NOTOK; /* NOT REACHED */
1043 set_id(CT ct, int top)
1047 static time_t clock = 0;
1048 static char *msgfmt;
1052 snprintf(msgid, sizeof(msgid), "<%d.%ld.%%d@%s>\n",
1053 (int) getpid(), (long) clock, LocalName());
1055 msgfmt = getcpy(msgid);
1057 snprintf(msgid, sizeof(msgid), msgfmt, top ? 0 : ++partno);
1058 ct->c_id = getcpy(msgid);
1063 ** Fill out, or expand the various contents in the composition
1064 ** draft. Read-in any necessary files. Parse and execute any
1065 ** commands specified by profile composition strings.
1069 compose_content(CT ct)
1071 CE ce = ct->c_cefile;
1073 switch (ct->c_type) {
1078 char partnam[BUFSIZ];
1079 struct multipart *m = (struct multipart *) ct->c_ctparams;
1083 snprintf(partnam, sizeof(partnam), "%s.",
1085 pp = partnam + strlen(partnam);
1090 /* first, we call compose_content on all the subparts */
1091 for (part = m->mp_parts, partnum = 1; part;
1092 part = part->mp_next, partnum++) {
1093 CT p = part->mp_part;
1095 sprintf(pp, "%d", partnum);
1096 p->c_partno = getcpy(partnam);
1097 if (compose_content(p) == NOTOK)
1104 /* Nothing to do for type message */
1108 ** Discrete types (text/application/audio/image/video)
1113 int xstdout, len, buflen;
1114 char *bp, **ap, *cp;
1115 char *vec[4], buffer[BUFSIZ];
1117 CI ci = &ct->c_ctinfo;
1120 if (!(cp = ci->ci_magic))
1121 adios(NULL, "internal error(5)");
1123 tfile = m_mktemp2(NULL, invo_name, NULL, NULL);
1124 if (tfile == NULL) {
1125 adios("mhbuild", "unable to create temporary file");
1127 ce->ce_file = getcpy(tfile);
1132 /* Get buffer ready to go */
1135 buflen = sizeof(buffer);
1138 ** Parse composition string into buffer
1140 for ( ; *cp; cp++) {
1146 ** insert parameters from
1152 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
1153 snprintf(bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
1163 /* %f, and stdout is not-redirected */
1169 ** insert temporary filename
1170 ** where content should be
1173 snprintf(bp, buflen, "%s", ce->ce_file);
1177 /* insert content subtype */
1178 strncpy(bp, ci->ci_subtype, buflen);
1182 /* insert character % */
1203 printf("composing content %s/%s from command\n\t%s\n", ci->ci_type, ci->ci_subtype, buffer);
1205 fflush(stdout); /* not sure if need for -noverbose */
1212 if ((out = fopen(ce->ce_file, "w")) == NULL)
1213 adios(ce->ce_file, "unable to open for writing");
1215 switch (child_id = fork()) {
1217 adios("fork", "unable to fork");
1222 dup2(fileno(out), 1);
1224 execvp("/bin/sh", vec);
1225 fprintf(stderr, "unable to exec ");
1232 if (pidXwait(child_id, NULL))
1245 ** Scan the content.
1247 ** 1) choose a transfer encoding.
1248 ** 2) check for clashes with multipart boundary string.
1249 ** 3) for text content, figure out which character set is being used.
1251 ** If there is a clash with one of the contents and the multipart boundary,
1252 ** this function will exit with NOTOK. This will cause the scanning process
1253 ** to be repeated with a different multipart boundary. It is possible
1254 ** (although highly unlikely) that this scan will be repeated multiple times.
1261 int check8bit = 0, contains8bit = 0; /* check if contains 8bit data */
1262 int checklinelen = 0, linelen = 0; /* check for long lines */
1263 int checkboundary = 0, boundaryclash = 0; /* check if clashes with multipart boundary */
1264 int checklinespace = 0, linespace = 0; /* check if any line ends with space */
1265 unsigned char *cp = NULL, buffer[BUFSIZ];
1266 struct text *t = NULL;
1268 CE ce = ct->c_cefile;
1271 ** handle multipart by scanning all subparts
1272 ** and then checking their encoding.
1274 if (ct->c_type == CT_MULTIPART) {
1275 struct multipart *m = (struct multipart *) ct->c_ctparams;
1278 /* initially mark the domain of enclosing multipart as 7bit */
1279 ct->c_encoding = CE_7BIT;
1281 for (part = m->mp_parts; part; part = part->mp_next) {
1282 CT p = part->mp_part;
1284 if (scan_content(p) == NOTOK) {
1285 /* choose encoding for subpart */
1290 ** if necessary, enlarge encoding for enclosing
1293 if (p->c_encoding == CE_BINARY)
1294 ct->c_encoding = CE_BINARY;
1295 if (p->c_encoding == CE_8BIT &&
1296 ct->c_encoding != CE_BINARY)
1297 ct->c_encoding = CE_8BIT;
1304 ** Decide what to check while scanning this content.
1306 switch (ct->c_type) {
1310 if (ct->c_subtype == TEXT_PLAIN) {
1319 case CT_APPLICATION:
1337 ** Don't check anything for these types,
1338 ** since we are forcing use of base64.
1348 ** Scan the unencoded content
1350 if (check8bit || checklinelen || checklinespace || checkboundary) {
1351 if ((in = fopen(ce->ce_file, "r")) == NULL)
1352 adios(ce->ce_file, "unable to open for reading");
1353 len = strlen(prefix);
1355 while (fgets(buffer, sizeof(buffer) - 1, in)) {
1357 ** Check for 8bit data.
1360 for (cp = buffer; *cp; cp++) {
1361 if (!isascii(*cp)) {
1363 /* no need to keep checking */
1370 ** Check line length.
1372 if (checklinelen && (strlen(buffer) > CPERLIN + 1)) {
1374 checklinelen = 0; /* no need to keep checking */
1378 ** Check if line ends with a space.
1380 if (checklinespace &&
1381 (cp = buffer + strlen(buffer) - 2) >
1382 buffer && isspace(*cp)) {
1384 /* no need to keep checking */
1389 ** Check if content contains a line that clashes
1390 ** with our standard boundary for multipart messages.
1392 if (checkboundary && buffer[0] == '-' &&
1394 for (cp = buffer + strlen(buffer) - 1;
1399 if (strncmp(buffer + 2, prefix, len)==0 &&
1400 isdigit(buffer[2 + len])) {
1402 /* no need to keep checking */
1411 ** Decide which transfer encoding to use.
1413 switch (ct->c_type) {
1416 ** If the text content didn't specify a character
1417 ** set, we need to figure out which one was used.
1419 t = (struct text *) ct->c_ctparams;
1420 if (t->tx_charset == CHARSET_UNSPECIFIED) {
1421 CI ci = &ct->c_ctinfo;
1424 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++)
1428 t->tx_charset = CHARSET_UNKNOWN;
1429 *ap = concat("charset=", write_charset_8bit(),
1432 t->tx_charset = CHARSET_USASCII;
1433 *ap = getcpy("charset=us-ascii");
1436 cp = strchr(*ap++, '=');
1442 if (contains8bit || linelen || linespace)
1443 ct->c_encoding = CE_QUOTED;
1445 ct->c_encoding = CE_7BIT;
1448 case CT_APPLICATION:
1449 /* For application type, use base64, except when postscript */
1450 if (contains8bit || linelen || linespace)
1451 ct->c_encoding = (ct->c_subtype ==
1452 APPLICATION_POSTSCRIPT) ?
1453 CE_QUOTED : CE_BASE64;
1455 ct->c_encoding = CE_7BIT;
1459 ct->c_encoding = CE_7BIT;
1465 /* For audio, image, and video contents, just use base64 */
1466 ct->c_encoding = CE_BASE64;
1470 return (boundaryclash ? NOTOK : OK);
1475 ** Scan the content structures, and build header
1476 ** fields that will need to be output into the
1481 build_headers(CT ct)
1485 char *np, *vp, buffer[BUFSIZ];
1486 CI ci = &ct->c_ctinfo;
1489 ** If message is type multipart, then add the multipart
1490 ** boundary to the list of attribute/value pairs.
1492 if (ct->c_type == CT_MULTIPART) {
1494 static int level = 0; /* store nesting level */
1498 snprintf(buffer, sizeof(buffer), "boundary=%s%d",
1500 cp = strchr(*ap++ = getcpy(buffer), '=');
1507 ** output the content type and subtype
1509 np = getcpy(TYPE_FIELD);
1510 vp = concat(" ", ci->ci_type, "/", ci->ci_subtype, NULL);
1512 /* keep track of length of line */
1513 len = strlen(TYPE_FIELD) + strlen(ci->ci_type) +
1514 strlen(ci->ci_subtype) + 3;
1517 ** Append the attribute/value pairs to
1518 ** the end of the Content-Type line.
1520 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
1524 snprintf(buffer, sizeof(buffer), "%s=\"%s\"", *ap, *ep);
1525 if (len + 1 + (cc = strlen(buffer)) >= CPERLIN) {
1526 vp = add("\n\t", vp);
1532 vp = add(buffer, vp);
1537 ** Append any RFC-822 comment to the end of
1538 ** the Content-Type line.
1540 if (ci->ci_comment) {
1541 snprintf(buffer, sizeof(buffer), "(%s)", ci->ci_comment);
1542 if (len + 1 + (cc = 2 + strlen(ci->ci_comment)) >= CPERLIN) {
1543 vp = add("\n\t", vp);
1549 vp = add(buffer, vp);
1553 add_header(ct, np, vp);
1556 ** output the Content-ID, unless disabled by -nocontentid
1558 if (contentidsw && ct->c_id) {
1559 np = getcpy(ID_FIELD);
1560 vp = concat(" ", ct->c_id, NULL);
1561 add_header(ct, np, vp);
1565 ** output the Content-Description
1568 np = getcpy(DESCR_FIELD);
1569 vp = concat(" ", ct->c_descr, NULL);
1570 add_header(ct, np, vp);
1574 ** output the Content-Disposition
1577 np = getcpy(DISPO_FIELD);
1578 vp = concat(" ", ct->c_dispo, NULL);
1579 add_header(ct, np, vp);
1583 ** output the Content-Transfer-Encoding
1585 switch (ct->c_encoding) {
1587 /* Nothing to output */
1591 if (ct->c_type == CT_MESSAGE)
1592 adios(NULL, "internal error, invalid encoding");
1594 np = getcpy(ENCODING_FIELD);
1595 vp = concat(" ", "8bit", "\n", NULL);
1596 add_header(ct, np, vp);
1600 if (ct->c_type == CT_MESSAGE || ct->c_type == CT_MULTIPART)
1601 adios(NULL, "internal error, invalid encoding");
1603 np = getcpy(ENCODING_FIELD);
1604 vp = concat(" ", "quoted-printable", "\n", NULL);
1605 add_header(ct, np, vp);
1609 if (ct->c_type == CT_MESSAGE || ct->c_type == CT_MULTIPART)
1610 adios(NULL, "internal error, invalid encoding");
1612 np = getcpy(ENCODING_FIELD);
1613 vp = concat(" ", "base64", "\n", NULL);
1614 add_header(ct, np, vp);
1618 if (ct->c_type == CT_MESSAGE)
1619 adios(NULL, "internal error, invalid encoding");
1621 np = getcpy(ENCODING_FIELD);
1622 vp = concat(" ", "binary", "\n", NULL);
1623 add_header(ct, np, vp);
1627 adios(NULL, "unknown transfer encoding in content");
1632 ** Additional content specific header processing
1634 switch (ct->c_type) {
1637 struct multipart *m;
1640 m = (struct multipart *) ct->c_ctparams;
1641 for (part = m->mp_parts; part; part = part->mp_next) {