2 * mhoutsbr.c -- routines to output MIME messages
3 * -- given a Content structure
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <h/signals.h>
19 #include <h/mhparse.h>
21 #ifdef HAVE_SYS_WAIT_H
22 # include <sys/wait.h>
28 static char ebcdicsafe[0x100] = {
29 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
30 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
31 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
32 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
33 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01,
34 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
35 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
36 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
37 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
38 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
39 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
40 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01,
41 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
42 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
43 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
44 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
45 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
46 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
47 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
48 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
50 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
52 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
53 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
54 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
55 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
56 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
57 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
58 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
59 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
60 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
63 static char nib2b64[0x40+1] =
64 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
69 int output_message (CT, char *);
70 int output_message_fp (CT, FILE *, char *);
71 int writeBase64aux (FILE *, FILE *);
76 static int output_content (CT, FILE *);
77 static void output_headers (CT, FILE *);
78 static int writeExternalBody (CT, FILE *);
79 static int write8Bit (CT, FILE *);
80 static int writeQuoted (CT, FILE *);
81 static int writeBase64 (CT, FILE *);
85 * Main routine to output a MIME message contained
86 * in a Content structure, to a file. Any necessary
87 * transfer encoding is added.
91 output_message_fp (CT ct, FILE *fp, char *file)
93 if (output_content (ct, fp) == NOTOK)
97 advise ((file?file:"<FILE*>"), "error writing to");
104 output_message (CT ct, char *file)
109 if ((fp = fopen (file, "w")) == NULL) {
110 advise (file, "unable to open for writing");
113 status = output_message_fp(ct, fp, file);
120 * Output a Content structure to a file.
124 output_content (CT ct, FILE *out)
127 CI ci = &ct->c_ctinfo;
130 * Output all header fields for this content
132 output_headers (ct, out);
135 * If this is the internal content structure for a
136 * "message/external", then we are done with the
137 * headers (since it has no body).
143 * Now output the content bodies.
145 switch (ct->c_type) {
154 m = (struct multipart *) ct->c_ctparams;
155 for (part = m->mp_parts; part; part = part->mp_next) {
156 CT p = part->mp_part;
158 fprintf (out, "\n--%s\n", ci->ci_values[0]);
159 if (output_content (p, out) == NOTOK)
162 fprintf (out, "\n--%s--\n", ci->ci_values[0]);
168 if (ct->c_subtype == MESSAGE_EXTERNAL) {
171 e = (struct exbody *) ct->c_ctparams;
172 if (output_content (e->eb_content, out) == NOTOK)
175 /* output phantom body for access-type "mail-server" */
177 writeExternalBody (ct, out);
179 result = write8Bit (ct, out);
184 * Handle discrete types (text/application/audio/image/video)
187 switch (ct->c_encoding) {
190 result = write8Bit (ct, out);
195 result = write8Bit (ct, out);
200 result = writeQuoted (ct, out);
205 result = writeBase64 (ct, out);
209 advise (NULL, "can't handle binary transfer encoding in content");
214 advise (NULL, "unknown transfer encoding in content");
226 * Output all the header fields for a content
230 output_headers (CT ct, FILE *out)
236 fprintf (out, "%s:%s", hp->name, hp->value);
243 * Write the phantom body for access-type "mail-server".
247 writeExternalBody (CT ct, FILE *out)
249 char **ap, **ep, *cp;
250 struct exbody *e = (struct exbody *) ct->c_ctparams;
253 for (cp = e->eb_body; *cp; cp++) {
254 CT ct2 = e->eb_content;
255 CI ci2 = &ct2->c_ctinfo;
261 char *dp = trimcpy (ct2->c_id);
269 for (ap = ci2->ci_attrs, ep = ci2->ci_values; *ap; ap++, ep++)
270 if (!mh_strcasecmp (*ap, "name")) {
271 fprintf (out, "%s", *ep);
277 fprintf (out, "%s/%s", ci2->ci_type, ci2->ci_subtype);
278 for (ap = ci2->ci_attrs, ep = ci2->ci_values; *ap; ap++, ep++)
279 fprintf (out, "; %s=\"%s\"", *ap, *ep);
312 * Output a content without any transfer encoding
316 write8Bit (CT ct, FILE *out)
319 char c, *file, buffer[BUFSIZ];
320 CE ce = ct->c_cefile;
323 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
327 while (fgets (buffer, sizeof(buffer) - 1, ce->ce_fp)) {
328 c = buffer[strlen (buffer) - 1];
334 (*ct->c_ceclosefnx) (ct);
340 * Output a content using quoted-printable
344 writeQuoted (CT ct, FILE *out)
348 char c, buffer[BUFSIZ];
349 CE ce = ct->c_cefile;
352 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
355 while (fgets (buffer, sizeof(buffer) - 1, ce->ce_fp)) {
358 cp = buffer + strlen (buffer) - 1;
359 if ((c = *cp) == '\n')
362 if (strncmp (cp = buffer, "From ", sizeof("From ") - 1) == 0) {
363 fprintf (out, "=%02X", *cp++ & 0xff);
369 if (n > CPERLIN - 3) {
382 if (*cp < '!' || *cp > '~'
383 || (ebcdicsw && !ebcdicsafe[*cp & 0xff]))
391 fprintf (out, "=%02X", *cp & 0xff);
398 if (cp > buffer && (*--cp == ' ' || *cp == '\t'))
407 (*ct->c_ceclosefnx) (ct);
413 * Output a content using base64
417 writeBase64 (CT ct, FILE *out)
421 CE ce = ct->c_cefile;
424 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
427 result = writeBase64aux (ce->ce_fp, out);
428 (*ct->c_ceclosefnx) (ct);
434 writeBase64aux (FILE *in, FILE *out)
440 while ((cc = fread (inbuf, sizeof(*inbuf), sizeof(inbuf), in)) > 0) {
445 if (cc < sizeof(inbuf)) {
447 if (cc < sizeof(inbuf) - 1)
450 bits = (inbuf[0] & 0xff) << 16;
451 bits |= (inbuf[1] & 0xff) << 8;
452 bits |= inbuf[2] & 0xff;
454 for (bp = outbuf + sizeof(outbuf); bp > outbuf; bits >>= 6)
455 *--bp = nib2b64[bits & 0x3f];
456 if (cc < sizeof(inbuf)) {
458 if (cc < sizeof inbuf - 1)
462 fwrite (outbuf, sizeof(*outbuf), sizeof(outbuf), out);
464 if (cc < sizeof(inbuf)) {