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[] = {
46 #define NCONTENTIDSW 7
59 ** Directory to place tmp files. This must
60 ** be set before these routines are called.
66 static char prefix[] = "----- =_aaaaaaaaaa";
74 int make_intermediates(char *);
75 void content_error(char *, CT, char *, ...);
78 void free_content(CT);
80 void free_encoding(CT, int);
85 static int init_decoded_content(CT);
86 static char *fgetstr(char *, int, FILE *);
87 static int user_content(FILE *, char *, char *, CT *);
88 static void set_id(CT, int);
89 static int compose_content(CT);
90 static int scan_content(CT);
91 static int build_headers(CT);
92 static CT build_mime(char *);
104 static char infile[BUFSIZ];
105 static int unlink_infile = 0;
107 static char outfile[BUFSIZ];
108 static int unlink_outfile = 0;
110 static void unlink_done(int) NORETURN;
113 int output_message(CT, char *);
114 int output_message_fp(CT, FILE *, char*);
117 int list_all_messages(CT *, int, int, int);
120 void set_endian(void);
123 void free_content(CT);
127 main(int argc, char **argv)
130 char *cp, buf[BUFSIZ];
131 char buffer[BUFSIZ], *compfile = NULL;
132 char **argp, **arguments;
140 setlocale(LC_ALL, "");
142 invo_name = mhbasename(argv[0]);
144 /* read user profile/context */
147 arguments = getarguments(invo_name, argc, argv, 1);
150 while ((cp = *argp++)) {
151 if (cp[0] == '-' && cp[1] == '\0') {
153 adios(NULL, "cannot specify both standard input and a file");
156 listsw = 0; /* turn off -list if using std in/out */
157 verbosw = 0; /* turn off -verbose listings */
161 switch (smatch(++cp, switches)) {
163 ambigsw(cp, switches);
166 adios(NULL, "-%s unknown", cp);
169 snprintf(buf, sizeof(buf), "%s [switches] file", invo_name);
170 print_help(buf, switches, 1);
173 print_version(invo_name);
209 adios(NULL, "only one composition file allowed");
216 if ((cp = getenv("MM_NOASK")) && strcmp(cp, "1")==0)
220 ** Check if we've specified an additional profile
222 if ((cp = getenv("MHBUILD"))) {
223 if ((fp = fopen(cp, "r"))) {
224 readconfig((struct node **) 0, fp, cp, 0);
227 admonish("", "unable to read $MHBUILD profile (%s)",
233 ** Read the standard profile setup
235 if ((fp = fopen(cp = etcpath("mhn.defaults"), "r"))) {
236 readconfig((struct node **) 0, fp, cp, 0);
241 ** Check for storage directory. If defined, we
242 ** will store temporary files there. Else we
243 ** store them in standard nmh directory.
245 if ((cp = context_find(nmhstorage)) && *cp)
246 tmp = concat(cp, "/", invo_name, NULL);
248 tmp = getcpy(toabsdir(invo_name));
250 /* Check if we have a file to process */
252 adios(NULL, "need to specify a %s composition file",
256 ** Process the composition file from standard input.
258 if (compfile[0] == '-' && compfile[1] == '\0') {
259 /* copy standard input to temporary file */
260 strncpy(infile, m_mktemp(invo_name, NULL, &fp),
262 while (fgets(buffer, BUFSIZ, stdin))
267 /* build the content structures for MIME message */
268 ct = build_mime(infile);
272 /* output MIME message to this temporary file */
273 strncpy(outfile, m_mktemp(invo_name, NULL, &fp_out),
277 /* output the message */
278 output_message_fp(ct, fp_out, outfile);
281 /* output the temp file to standard output */
282 if ((fp = fopen(outfile, "r")) == NULL)
283 adios(outfile, "unable to open");
284 while (fgets(buffer, BUFSIZ, fp))
285 fputs(buffer, stdout);
299 ** Process the composition file from a file.
302 /* build the content structures for MIME message */
303 ct = build_mime(compfile);
307 /* output MIME message to this temporary file */
308 strncpy(outfile, m_mktemp2(compfile, invo_name, NULL, &fp_out),
312 /* output the message */
313 output_message_fp(ct, fp_out, outfile);
317 ** List the message info
320 list_all_messages(cts, headsw, verbosw, debugsw);
322 /* Rename composition draft */
323 snprintf(buffer, sizeof(buffer), "%s.orig", m_backup(compfile));
324 if (rename(compfile, buffer) == NOTOK) {
325 adios(compfile, "unable to rename comp draft %s to", buffer);
328 /* Rename output file to take its place */
329 if (rename(outfile, compfile) == NOTOK) {
330 advise(outfile, "unable to rename output %s to", compfile);
331 rename(buffer, compfile);
343 unlink_done(int status)
346 ** Check if we need to remove stray temporary files.
357 ** Main routine for translating composition file
358 ** into valid MIME message. It translates the draft
359 ** into a content structure (actually a tree of content
360 ** structures). This message then can be manipulated
361 ** in various ways, including being output via
365 build_mime(char *infile)
368 char buf[BUFSIZ], name[NAMESZ];
377 /* open the composition draft */
378 if ((in = fopen(infile, "r")) == NULL)
379 adios(infile, "unable to open for reading");
382 ** Allocate space for primary (outside) content
384 if ((ct = (CT) calloc(1, sizeof(*ct))) == NULL)
385 adios(NULL, "out of memory");
388 ** Allocate structure for handling decoded content
389 ** for this part. We don't really need this, but
390 ** allocate it to remain consistent.
392 init_decoded_content(ct);
395 ** Parse some of the header fields in the composition
396 ** draft into the linked list of header fields for
397 ** the new MIME message.
399 for (compnum = 1, state = FLD;;) {
400 switch (state = m_getfld(state, name, buf, sizeof(buf), in)) {
406 /* abort if draft has Mime-Version header field */
407 if (!mh_strcasecmp(name, VRSN_FIELD))
408 adios(NULL, "draft shouldn't contain %s: field", VRSN_FIELD);
411 ** abort if draft has Content-Transfer-Encoding
414 if (!mh_strcasecmp(name, ENCODING_FIELD))
415 adios(NULL, "draft shouldn't contain %s: field", ENCODING_FIELD);
417 /* ignore any Content-Type fields in the header */
418 if (!mh_strcasecmp(name, TYPE_FIELD)) {
419 while (state == FLDPLUS)
420 state = m_getfld(state, name, buf,
425 /* get copies of the buffers */
429 /* if necessary, get rest of field */
430 while (state == FLDPLUS) {
431 state = m_getfld(state, name, buf,
433 vp = add(buf, vp); /* add to prev value */
436 /* Now add the header data to the list */
437 add_header(ct, np, vp);
440 /* if this wasn't the last hdr field, then continue */
446 adios(NULL, "draft has empty body -- no directives!");
451 fseek(in, (long) (-strlen(buf)), SEEK_CUR);
456 adios(NULL, "message format error in component #%d",
460 adios(NULL, "getfld() returned %d", state);
466 ** Now add the MIME-Version header field
467 ** to the list of header fields.
469 np = getcpy(VRSN_FIELD);
470 vp = concat(" ", VRSN_VALUE, "\n", NULL);
471 add_header(ct, np, vp);
474 ** We initally assume we will find multiple contents in the
475 ** draft. So create a multipart/mixed content to hold everything.
476 ** We can remove this later, if it is not needed.
478 if (get_ctinfo("multipart/mixed", ct, 0) == NOTOK)
480 ct->c_type = CT_MULTIPART;
481 ct->c_subtype = MULTI_MIXED;
482 ct->c_file = getcpy(infile);
484 if ((m = (struct multipart *) calloc(1, sizeof(*m))) == NULL)
485 adios(NULL, "out of memory");
486 ct->c_ctparams = (void *) m;
490 ** read and parse the composition file
491 ** and the directives it contains.
493 while (fgetstr(buf, sizeof(buf) - 1, in)) {
497 if (user_content(in, infile, buf, &p) == DONE) {
498 admonish(NULL, "ignoring spurious #end");
504 if ((part = (struct part *) calloc(1, sizeof(*part))) == NULL)
505 adios(NULL, "out of memory");
512 ** close the composition draft since
513 ** it's not needed any longer.
517 /* check if any contents were found */
519 adios(NULL, "no content directives found");
522 ** If only one content was found, then remove and
523 ** free the outer multipart content.
525 if (!m->mp_parts->mp_next) {
528 p = m->mp_parts->mp_part;
529 m->mp_parts->mp_part = NULL;
531 /* move header fields */
532 p->c_first_hf = ct->c_first_hf;
533 p->c_last_hf = ct->c_last_hf;
534 ct->c_first_hf = NULL;
535 ct->c_last_hf = NULL;
544 ** Fill out, or expand directives. Parse and execute
545 ** commands specified by profile composition strings.
549 if ((cp = strchr(prefix, 'a')) == NULL)
550 adios(NULL, "internal error(4)");
553 ** Scan the contents. Choose a transfer encoding, and
554 ** check if prefix for multipart boundary clashes with
555 ** any of the contents.
557 while (scan_content(ct) == NOTOK) {
562 adios(NULL, "giving up trying to find a unique delimiter string");
568 /* Build the rest of the header field structures */
576 ** Set up structures for placing unencoded
577 ** content when building parts.
581 init_decoded_content(CT ct)
585 if ((ce = (CE) calloc(1, sizeof(*ce))) == NULL)
586 adios(NULL, "out of memory");
589 ct->c_ceopenfnx = open7Bit; /* since unencoded */
590 ct->c_ceclosefnx = close_encoding;
591 ct->c_cesizefnx = NULL; /* since unencoded */
598 fgetstr(char *s, int n, FILE *stream)
602 for (ep = (cp = s) + n; cp < ep; ) {
605 if (!fgets(cp, n, stream))
606 return (cp != s ? s : NULL);
607 if (cp == s && *cp != '#')
610 cp += (i = strlen(cp)) - 1;
611 if (i <= 1 || *cp-- != '\n' || *cp != '\\')
622 ** Parse the composition draft for text and directives.
623 ** Do initial setup of Content structure.
627 user_content(FILE *in, char *file, char *buf, CT *ctp)
636 struct str2init *s2i;
641 if (buf[0] == '\n' || strcmp(buf, "#\n") == 0) {
646 /* allocate basic Content structure */
647 if ((ct = (CT) calloc(1, sizeof(*ct))) == NULL)
648 adios(NULL, "out of memory");
651 /* allocate basic structure for handling decoded content */
652 init_decoded_content(ct);
659 ** Handle inline text. Check if line
660 ** is one of the following forms:
662 ** 1) doesn't begin with '#' (implicit directive)
663 ** 2) begins with "##" (implicit directive)
664 ** 3) begins with "#<"
666 if (buf[0] != '#' || buf[1] == '#' || buf[1] == '<') {
670 char content[BUFSIZ];
674 cp = m_mktemp2(NULL, invo_name, NULL, &out);
676 adios("mhbuild", "unable to create temporary file");
678 /* use a temp file to collect the plain text lines */
679 ce->ce_file = getcpy(cp);
682 if (buf[0] == '#' && buf[1] == '<') {
683 strncpy(content, buf + 2, sizeof(content));
690 /* the directive is implicit */
691 strncpy(content, "text/plain", sizeof(content));
693 strncpy(buffer, buf[0] != '#' ? buf : buf + 1, sizeof(buffer));
697 if (headers >= 0 && uprf(buffer, DESCR_FIELD) &&
698 buffer[i=strlen(DESCR_FIELD)] == ':') {
702 ct->c_descr = add(buffer + i + 1, ct->c_descr);
703 if (!fgetstr(buffer, sizeof(buffer) - 1, in))
704 adios(NULL, "end-of-file after %s: field in plaintext", DESCR_FIELD);
712 adios(NULL, "#-directive after %s: field in plaintext", DESCR_FIELD);
720 if (headers >= 0 && uprf(buffer, DISPO_FIELD)
721 && buffer[i = strlen(DISPO_FIELD)] == ':') {
725 ct->c_dispo = add(buffer + i + 1, ct->c_dispo);
726 if (!fgetstr(buffer, sizeof(buffer) - 1, in))
727 adios(NULL, "end-of-file after %s: field in plaintext", DISPO_FIELD);
735 adios(NULL, "#-directive after %s: field in plaintext", DISPO_FIELD);
743 if (headers != 1 || buffer[0] != '\n')
749 if ((cp = fgetstr(buffer, sizeof(buffer) - 1, in))
752 if (buffer[0] == '#') {
755 if (buffer[1] != '#')
757 for (cp = (bp = buffer) + 1; *cp; cp++)
764 ct->c_end = ftell(out);
767 /* parse content type */
768 if (get_ctinfo(content, ct, inlineD) == NOTOK)
771 for (s2i = str2cts; s2i->si_key; s2i++)
772 if (!mh_strcasecmp(ci->ci_type, s2i->si_key))
774 if (!s2i->si_key && !uprf(ci->ci_type, "X-"))
778 ** check type specified (possibly implicitly)
780 switch (ct->c_type = s2i->si_val) {
782 if (!mh_strcasecmp(ci->ci_subtype, "rfc822")) {
783 ct->c_encoding = CE_7BIT;
788 adios(NULL, "it doesn't make sense to define an in-line %s content",
789 ct->c_type == CT_MESSAGE ? "message" :
795 if ((ct->c_ctinitfnx = s2i->si_init))
796 (*ct->c_ctinitfnx) (ct);
801 fseek(in, pos, SEEK_SET);
806 ** If we've reached this point, the next line
807 ** must be some type of explicit directive.
811 adios(NULL, "The #@ directive i.e. message/external-body "
812 "is not supported anymore.");
815 /* parse directive */
816 if (get_ctinfo(buf+1, ct, 1) == NOTOK)
819 /* check directive against the list of MIME types */
820 for (s2i = str2cts; s2i->si_key; s2i++)
821 if (!mh_strcasecmp(ci->ci_type, s2i->si_key))
825 ** Check if the directive specified a valid type.
826 ** This will happen if it was one of the following forms:
832 adios(NULL, "missing subtype in \"#%s\"", ci->ci_type);
834 switch (ct->c_type = s2i->si_val) {
836 adios(NULL, "use \"#begin ... #end\" instead of \"#%s/%s\"", ci->ci_type, ci->ci_subtype);
840 if (!mh_strcasecmp(ci->ci_subtype, "partial") ||
841 !mh_strcasecmp(ci->ci_subtype,
843 adios(NULL, "sorry, \"#%s/%s\" isn't supported", ci->ci_type, ci->ci_subtype);
846 adios(NULL, "use \"#forw [+folder] [msgs]\" instead of \"#%s/%s\"", ci->ci_type, ci->ci_subtype);
850 if ((ct->c_ctinitfnx = s2i->si_init))
851 (*ct->c_ctinitfnx) (ct);
855 /* Handle [file] argument */
857 /* check if specifies command to execute */
858 if (*ci->ci_magic == '|' || *ci->ci_magic == '!') {
859 for (cp = ci->ci_magic + 1; isspace(*cp); cp++)
862 adios(NULL, "empty pipe command for #%s directive", ci->ci_type);
867 /* record filename of decoded contents */
868 ce->ce_file = ci->ci_magic;
869 if (access(ce->ce_file, R_OK) == NOTOK)
870 adios("reading", "unable to access %s for", ce->ce_file);
871 if (listsw && stat(ce->ce_file, &st) != NOTOK)
872 ct->c_end = (long) st.st_size;
879 ** No [file] argument, so check profile for
880 ** method to compose content.
882 snprintf(buffer, sizeof(buffer), "%s-compose-%s/%s",
883 invo_name, ci->ci_type, ci->ci_subtype);
884 if ((cp = context_find(buffer)) == NULL || *cp == '\0') {
885 snprintf(buffer, sizeof(buffer), "%s-compose-%s",
886 invo_name, ci->ci_type);
887 if ((cp = context_find(buffer)) == NULL ||
889 content_error(NULL, ct, "don't know how to compose content");
893 ci->ci_magic = getcpy(cp);
899 ** #forw [+folder] [msgs]
901 if (!mh_strcasecmp(ci->ci_type, "forw")) {
903 char *folder, *arguments[MAXARGS];
909 ap = brkstring(ci->ci_magic, " ", "\n");
910 for (i=0; ap[i] && i<MAXARGS-1; i++) {
911 arguments[i] = ap[i];
916 arguments[0] = seq_cur;
921 /* search the arguments for a folder name */
922 for (ap = arguments; *ap; ap++) {
924 if (*cp == '+' || *cp == '@') {
926 adios(NULL, "only one folder per #forw directive");
928 folder = getcpy(expandfol(cp));
932 /* else, use the current folder */
934 folder = getcpy(getcurfol());
936 if (!(mp = folder_read(folder)))
937 adios(NULL, "unable to read folder %s", folder);
938 for (ap = arguments; *ap; ap++) {
940 if (*cp != '+' && *cp != '@')
941 if (!m_convert(mp, cp))
948 ** If there is more than one message to include, make this
949 ** a content of type "multipart/digest" and insert each message
950 ** as a subpart. If there is only one message, then make this
951 ** a content of type "message/rfc822".
953 if (mp->numsel > 1) {
954 /* we are forwarding multiple messages */
955 if (get_ctinfo("multipart/digest", ct, 0) == NOTOK)
957 ct->c_type = CT_MULTIPART;
958 ct->c_subtype = MULTI_DIGEST;
960 if ((m = (struct multipart *)
961 calloc(1, sizeof(*m))) == NULL)
962 adios(NULL, "out of memory");
963 ct->c_ctparams = (void *) m;
966 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
967 if (is_selected(mp, msgnum)) {
972 if ((p = (CT) calloc(1, sizeof(*p)))
974 adios(NULL, "out of memory");
975 init_decoded_content(p);
977 if (get_ctinfo("message/rfc822", p, 0)
980 p->c_type = CT_MESSAGE;
981 p->c_subtype = MESSAGE_RFC822;
983 snprintf(buffer, sizeof(buffer),
984 "%s/%d", mp->foldpath,
986 pe->ce_file = getcpy(buffer);
987 if (listsw && stat(pe->ce_file, &st)
989 p->c_end = (long) st.st_size;
991 if ((part = (struct part *) calloc(1, sizeof(*part))) == NULL)
992 adios(NULL, "out of memory");
999 /* we are forwarding one message */
1000 if (get_ctinfo("message/rfc822", ct, 0) == NOTOK)
1002 ct->c_type = CT_MESSAGE;
1003 ct->c_subtype = MESSAGE_RFC822;
1005 msgnum = mp->lowsel;
1006 snprintf(buffer, sizeof(buffer), "%s/%d",
1007 mp->foldpath, msgnum);
1008 ce->ce_file = getcpy(buffer);
1009 if (listsw && stat(ce->ce_file, &st) != NOTOK)
1010 ct->c_end = (long) st.st_size;
1013 folder_free(mp); /* free folder/message structure */
1020 if (!mh_strcasecmp(ci->ci_type, "end")) {
1027 ** #begin [ alternative | parallel ]
1029 if (!mh_strcasecmp(ci->ci_type, "begin")) {
1030 if (!ci->ci_magic) {
1032 cp = SubMultiPart[vrsn - 1].kv_key;
1033 } else if (!mh_strcasecmp(ci->ci_magic, "alternative")) {
1034 vrsn = MULTI_ALTERNATE;
1035 cp = SubMultiPart[vrsn - 1].kv_key;
1036 } else if (!mh_strcasecmp(ci->ci_magic, "parallel")) {
1037 vrsn = MULTI_PARALLEL;
1038 cp = SubMultiPart[vrsn - 1].kv_key;
1039 } else if (uprf(ci->ci_magic, "digest")) {
1042 vrsn = MULTI_UNKNOWN;
1047 snprintf(buffer, sizeof(buffer), "multipart/%s", cp);
1048 if (get_ctinfo(buffer, ct, 0) == NOTOK)
1050 ct->c_type = CT_MULTIPART;
1051 ct->c_subtype = vrsn;
1053 if ((m = (struct multipart *) calloc(1, sizeof(*m))) == NULL)
1054 adios(NULL, "out of memory");
1055 ct->c_ctparams = (void *) m;
1058 while (fgetstr(buffer, sizeof(buffer) - 1, in)) {
1062 if (user_content(in, file, buffer, &p) == DONE) {
1064 adios(NULL, "empty \"#begin ... #end\" sequence");
1070 if ((part = (struct part *)
1071 calloc(1, sizeof(*part))) == NULL)
1072 adios(NULL, "out of memory");
1074 pp = &part->mp_next;
1077 admonish(NULL, "premature end-of-file, missing #end");
1082 ** Unknown directive
1084 adios(NULL, "unknown directive \"#%s\"", ci->ci_type);
1085 return NOTOK; /* NOT REACHED */
1090 set_id(CT ct, int top)
1094 static time_t clock = 0;
1095 static char *msgfmt;
1099 snprintf(msgid, sizeof(msgid), "<%d.%ld.%%d@%s>\n",
1100 (int) getpid(), (long) clock, LocalName());
1102 msgfmt = getcpy(msgid);
1104 snprintf(msgid, sizeof(msgid), msgfmt, top ? 0 : ++partno);
1105 ct->c_id = getcpy(msgid);
1110 ** Fill out, or expand the various contents in the composition
1111 ** draft. Read-in any necessary files. Parse and execute any
1112 ** commands specified by profile composition strings.
1116 compose_content(CT ct)
1118 CE ce = ct->c_cefile;
1120 switch (ct->c_type) {
1125 char partnam[BUFSIZ];
1126 struct multipart *m = (struct multipart *) ct->c_ctparams;
1130 snprintf(partnam, sizeof(partnam), "%s.",
1132 pp = partnam + strlen(partnam);
1137 /* first, we call compose_content on all the subparts */
1138 for (part = m->mp_parts, partnum = 1; part;
1139 part = part->mp_next, partnum++) {
1140 CT p = part->mp_part;
1142 sprintf(pp, "%d", partnum);
1143 p->c_partno = getcpy(partnam);
1144 if (compose_content(p) == NOTOK)
1149 ct->c_end = (partnum = strlen(prefix) + 2) + 2;
1150 for (part = m->mp_parts; part; part = part->mp_next)
1151 ct->c_end += part->mp_part->c_end + partnum;
1157 /* Nothing to do for type message */
1161 ** Discrete types (text/application/audio/image/video)
1166 int xstdout, len, buflen;
1167 char *bp, **ap, *cp;
1168 char *vec[4], buffer[BUFSIZ];
1170 CI ci = &ct->c_ctinfo;
1173 if (!(cp = ci->ci_magic))
1174 adios(NULL, "internal error(5)");
1176 tfile = m_mktemp2(NULL, invo_name, NULL, NULL);
1177 if (tfile == NULL) {
1178 adios("mhbuild", "unable to create temporary file");
1180 ce->ce_file = getcpy(tfile);
1185 /* Get buffer ready to go */
1188 buflen = sizeof(buffer);
1191 ** Parse composition string into buffer
1193 for ( ; *cp; cp++) {
1199 ** insert parameters from
1205 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
1206 snprintf(bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
1216 /* %f, and stdout is not-redirected */
1222 ** insert temporary filename
1223 ** where content should be
1226 snprintf(bp, buflen, "%s", ce->ce_file);
1230 /* insert content subtype */
1231 strncpy(bp, ci->ci_subtype, buflen);
1235 /* insert character % */
1256 printf("composing content %s/%s from command\n\t%s\n", ci->ci_type, ci->ci_subtype, buffer);
1258 fflush(stdout); /* not sure if need for -noverbose */
1265 if ((out = fopen(ce->ce_file, "w")) == NULL)
1266 adios(ce->ce_file, "unable to open for writing");
1268 switch (child_id = fork()) {
1270 adios("fork", "unable to fork");
1275 dup2(fileno(out), 1);
1277 execvp("/bin/sh", vec);
1278 fprintf(stderr, "unable to exec ");
1285 if (pidXwait(child_id, NULL))
1291 /* Check size of file */
1292 if (listsw && ct->c_end == 0L) {
1295 if (stat(ce->ce_file, &st) != NOTOK)
1296 ct->c_end = (long) st.st_size;
1306 ** Scan the content.
1308 ** 1) choose a transfer encoding.
1309 ** 2) check for clashes with multipart boundary string.
1310 ** 3) for text content, figure out which character set is being used.
1312 ** If there is a clash with one of the contents and the multipart boundary,
1313 ** this function will exit with NOTOK. This will cause the scanning process
1314 ** to be repeated with a different multipart boundary. It is possible
1315 ** (although highly unlikely) that this scan will be repeated multiple times.
1322 int check8bit = 0, contains8bit = 0; /* check if contains 8bit data */
1323 int checklinelen = 0, linelen = 0; /* check for long lines */
1324 int checkboundary = 0, boundaryclash = 0; /* check if clashes with multipart boundary */
1325 int checklinespace = 0, linespace = 0; /* check if any line ends with space */
1326 unsigned char *cp = NULL, buffer[BUFSIZ];
1327 struct text *t = NULL;
1329 CE ce = ct->c_cefile;
1332 ** handle multipart by scanning all subparts
1333 ** and then checking their encoding.
1335 if (ct->c_type == CT_MULTIPART) {
1336 struct multipart *m = (struct multipart *) ct->c_ctparams;
1339 /* initially mark the domain of enclosing multipart as 7bit */
1340 ct->c_encoding = CE_7BIT;
1342 for (part = m->mp_parts; part; part = part->mp_next) {
1343 CT p = part->mp_part;
1345 if (scan_content(p) == NOTOK) {
1346 /* choose encoding for subpart */
1351 ** if necessary, enlarge encoding for enclosing
1354 if (p->c_encoding == CE_BINARY)
1355 ct->c_encoding = CE_BINARY;
1356 if (p->c_encoding == CE_8BIT &&
1357 ct->c_encoding != CE_BINARY)
1358 ct->c_encoding = CE_8BIT;
1365 ** Decide what to check while scanning this content.
1367 switch (ct->c_type) {
1371 if (ct->c_subtype == TEXT_PLAIN) {
1380 case CT_APPLICATION:
1398 ** Don't check anything for these types,
1399 ** since we are forcing use of base64.
1409 ** Scan the unencoded content
1411 if (check8bit || checklinelen || checklinespace || checkboundary) {
1412 if ((in = fopen(ce->ce_file, "r")) == NULL)
1413 adios(ce->ce_file, "unable to open for reading");
1414 len = strlen(prefix);
1416 while (fgets(buffer, sizeof(buffer) - 1, in)) {
1418 ** Check for 8bit data.
1421 for (cp = buffer; *cp; cp++) {
1422 if (!isascii(*cp)) {
1424 /* no need to keep checking */
1431 ** Check line length.
1433 if (checklinelen && (strlen(buffer) > CPERLIN + 1)) {
1435 checklinelen = 0; /* no need to keep checking */
1439 ** Check if line ends with a space.
1441 if (checklinespace &&
1442 (cp = buffer + strlen(buffer) - 2) >
1443 buffer && isspace(*cp)) {
1445 /* no need to keep checking */
1450 ** Check if content contains a line that clashes
1451 ** with our standard boundary for multipart messages.
1453 if (checkboundary && buffer[0] == '-' &&
1455 for (cp = buffer + strlen(buffer) - 1;
1460 if (strncmp(buffer + 2, prefix, len)==0 &&
1461 isdigit(buffer[2 + len])) {
1463 /* no need to keep checking */
1472 ** Decide which transfer encoding to use.
1474 switch (ct->c_type) {
1477 ** If the text content didn't specify a character
1478 ** set, we need to figure out which one was used.
1480 t = (struct text *) ct->c_ctparams;
1481 if (t->tx_charset == CHARSET_UNSPECIFIED) {
1482 CI ci = &ct->c_ctinfo;
1485 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++)
1489 t->tx_charset = CHARSET_UNKNOWN;
1490 *ap = concat("charset=", write_charset_8bit(),
1493 t->tx_charset = CHARSET_USASCII;
1494 *ap = getcpy("charset=us-ascii");
1497 cp = strchr(*ap++, '=');
1503 if (contains8bit || linelen || linespace)
1504 ct->c_encoding = CE_QUOTED;
1506 ct->c_encoding = CE_7BIT;
1509 case CT_APPLICATION:
1510 /* For application type, use base64, except when postscript */
1511 if (contains8bit || linelen || linespace)
1512 ct->c_encoding = (ct->c_subtype ==
1513 APPLICATION_POSTSCRIPT) ?
1514 CE_QUOTED : CE_BASE64;
1516 ct->c_encoding = CE_7BIT;
1520 ct->c_encoding = CE_7BIT;
1526 /* For audio, image, and video contents, just use base64 */
1527 ct->c_encoding = CE_BASE64;
1531 return (boundaryclash ? NOTOK : OK);
1536 ** Scan the content structures, and build header
1537 ** fields that will need to be output into the
1542 build_headers(CT ct)
1546 char *np, *vp, buffer[BUFSIZ];
1547 CI ci = &ct->c_ctinfo;
1550 ** If message is type multipart, then add the multipart
1551 ** boundary to the list of attribute/value pairs.
1553 if (ct->c_type == CT_MULTIPART) {
1555 static int level = 0; /* store nesting level */
1559 snprintf(buffer, sizeof(buffer), "boundary=%s%d",
1561 cp = strchr(*ap++ = getcpy(buffer), '=');
1568 ** output the content type and subtype
1570 np = getcpy(TYPE_FIELD);
1571 vp = concat(" ", ci->ci_type, "/", ci->ci_subtype, NULL);
1573 /* keep track of length of line */
1574 len = strlen(TYPE_FIELD) + strlen(ci->ci_type) +
1575 strlen(ci->ci_subtype) + 3;
1578 ** Append the attribute/value pairs to
1579 ** the end of the Content-Type line.
1581 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
1585 snprintf(buffer, sizeof(buffer), "%s=\"%s\"", *ap, *ep);
1586 if (len + 1 + (cc = strlen(buffer)) >= CPERLIN) {
1587 vp = add("\n\t", vp);
1593 vp = add(buffer, vp);
1598 ** Append any RFC-822 comment to the end of
1599 ** the Content-Type line.
1601 if (ci->ci_comment) {
1602 snprintf(buffer, sizeof(buffer), "(%s)", ci->ci_comment);
1603 if (len + 1 + (cc = 2 + strlen(ci->ci_comment)) >= CPERLIN) {
1604 vp = add("\n\t", vp);
1610 vp = add(buffer, vp);
1614 add_header(ct, np, vp);
1617 ** output the Content-ID, unless disabled by -nocontentid
1619 if (contentidsw && ct->c_id) {
1620 np = getcpy(ID_FIELD);
1621 vp = concat(" ", ct->c_id, NULL);
1622 add_header(ct, np, vp);
1626 ** output the Content-Description
1629 np = getcpy(DESCR_FIELD);
1630 vp = concat(" ", ct->c_descr, NULL);
1631 add_header(ct, np, vp);
1635 ** output the Content-Disposition
1638 np = getcpy(DISPO_FIELD);
1639 vp = concat(" ", ct->c_dispo, NULL);
1640 add_header(ct, np, vp);
1644 ** output the Content-Transfer-Encoding
1646 switch (ct->c_encoding) {
1648 /* Nothing to output */
1652 if (ct->c_type == CT_MESSAGE)
1653 adios(NULL, "internal error, invalid encoding");
1655 np = getcpy(ENCODING_FIELD);
1656 vp = concat(" ", "8bit", "\n", NULL);
1657 add_header(ct, np, vp);
1661 if (ct->c_type == CT_MESSAGE || ct->c_type == CT_MULTIPART)
1662 adios(NULL, "internal error, invalid encoding");
1664 np = getcpy(ENCODING_FIELD);
1665 vp = concat(" ", "quoted-printable", "\n", NULL);
1666 add_header(ct, np, vp);
1670 if (ct->c_type == CT_MESSAGE || ct->c_type == CT_MULTIPART)
1671 adios(NULL, "internal error, invalid encoding");
1673 np = getcpy(ENCODING_FIELD);
1674 vp = concat(" ", "base64", "\n", NULL);
1675 add_header(ct, np, vp);
1679 if (ct->c_type == CT_MESSAGE)
1680 adios(NULL, "internal error, invalid encoding");
1682 np = getcpy(ENCODING_FIELD);
1683 vp = concat(" ", "binary", "\n", NULL);
1684 add_header(ct, np, vp);
1688 adios(NULL, "unknown transfer encoding in content");
1693 ** Additional content specific header processing
1695 switch (ct->c_type) {
1698 struct multipart *m;
1701 m = (struct multipart *) ct->c_ctparams;
1702 for (part = m->mp_parts; part; part = part->mp_next) {