3 * mhoutsbr.c -- routines to output MIME messages
4 * -- given a Content structure
6 * This code is Copyright (c) 2002, by the authors of nmh. See the
7 * COPYRIGHT file in the root directory of the nmh distribution for
8 * complete copyright information.
13 #include <h/signals.h>
20 #include <h/mhparse.h>
25 static char ebcdicsafe[0x100] = {
26 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
27 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
28 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
29 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
30 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01,
31 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
32 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
33 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
34 0x00, 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 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01,
38 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
39 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
40 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
41 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
42 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
43 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
44 0x00, 0x00, 0x00, 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
60 static char nib2b64[0x40+1] =
61 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
66 int output_message (CT, char *);
67 int output_message_fp (CT, FILE *, char *);
68 int writeBase64aux (FILE *, FILE *);
73 static int output_content (CT, FILE *);
74 static void output_headers (CT, FILE *);
75 static int writeExternalBody (CT, FILE *);
76 static int write8Bit (CT, FILE *);
77 static int writeQuoted (CT, FILE *);
78 static int writeBase64 (CT, FILE *);
82 * Main routine to output a MIME message contained
83 * in a Content structure, to a file. Any necessary
84 * transfer encoding is added.
88 output_message_fp (CT ct, FILE *fp, char *file)
90 if (output_content (ct, fp) == NOTOK)
94 advise ((file?file:"<FILE*>"), "error writing to");
101 output_message (CT ct, char *file)
106 if ((fp = fopen (file, "w")) == NULL) {
107 advise (file, "unable to open for writing");
110 status = output_message_fp(ct, fp, file);
117 * Output a Content structure to a file.
121 output_content (CT ct, FILE *out)
124 CI ci = &ct->c_ctinfo;
127 * Output all header fields for this content
129 output_headers (ct, out);
132 * If this is the internal content structure for a
133 * "message/external", then we are done with the
134 * headers (since it has no body).
140 * Now output the content bodies.
142 switch (ct->c_type) {
151 m = (struct multipart *) ct->c_ctparams;
152 for (part = m->mp_parts; part; part = part->mp_next) {
153 CT p = part->mp_part;
155 fprintf (out, "\n--%s\n", ci->ci_values[0]);
156 if (output_content (p, out) == NOTOK)
159 fprintf (out, "\n--%s--\n", ci->ci_values[0]);
165 if (ct->c_subtype == MESSAGE_EXTERNAL) {
168 e = (struct exbody *) ct->c_ctparams;
169 if (output_content (e->eb_content, out) == NOTOK)
172 /* output phantom body for access-type "mail-server" */
174 writeExternalBody (ct, out);
176 result = write8Bit (ct, out);
181 * Handle discrete types (text/application/audio/image/video)
184 switch (ct->c_encoding) {
187 result = write8Bit (ct, out);
192 result = write8Bit (ct, out);
197 result = writeQuoted (ct, out);
202 result = writeBase64 (ct, out);
206 advise (NULL, "can't handle binary transfer encoding in content");
211 advise (NULL, "unknown transfer encoding in content");
223 * Output all the header fields for a content
227 output_headers (CT ct, FILE *out)
233 fprintf (out, "%s:%s", hp->name, hp->value);
240 * Write the phantom body for access-type "mail-server".
244 writeExternalBody (CT ct, FILE *out)
246 char **ap, **ep, *cp;
247 struct exbody *e = (struct exbody *) ct->c_ctparams;
250 for (cp = e->eb_body; *cp; cp++) {
251 CT ct2 = e->eb_content;
252 CI ci2 = &ct2->c_ctinfo;
258 char *dp = trimcpy (ct2->c_id);
266 for (ap = ci2->ci_attrs, ep = ci2->ci_values; *ap; ap++, ep++)
267 if (!mh_strcasecmp (*ap, "name")) {
268 fprintf (out, "%s", *ep);
274 fprintf (out, "%s/%s", ci2->ci_type, ci2->ci_subtype);
275 for (ap = ci2->ci_attrs, ep = ci2->ci_values; *ap; ap++, ep++)
276 fprintf (out, "; %s=\"%s\"", *ap, *ep);
309 * Output a content without any transfer encoding
313 write8Bit (CT ct, FILE *out)
316 char c, *file, buffer[BUFSIZ];
317 CE ce = ct->c_cefile;
320 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
324 while (fgets (buffer, sizeof(buffer) - 1, ce->ce_fp)) {
325 c = buffer[strlen (buffer) - 1];
331 (*ct->c_ceclosefnx) (ct);
337 * Output a content using quoted-printable
341 writeQuoted (CT ct, FILE *out)
345 char c, buffer[BUFSIZ];
346 CE ce = ct->c_cefile;
349 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
352 while (fgets (buffer, sizeof(buffer) - 1, ce->ce_fp)) {
355 cp = buffer + strlen (buffer) - 1;
356 if ((c = *cp) == '\n')
359 if (strncmp (cp = buffer, "From ", sizeof("From ") - 1) == 0) {
360 fprintf (out, "=%02X", *cp++ & 0xff);
366 if (n > CPERLIN - 3) {
379 if (*cp < '!' || *cp > '~'
380 || (ebcdicsw && !ebcdicsafe[*cp & 0xff]))
388 fprintf (out, "=%02X", *cp & 0xff);
395 if (cp > buffer && (*--cp == ' ' || *cp == '\t'))
404 (*ct->c_ceclosefnx) (ct);
410 * Output a content using base64
414 writeBase64 (CT ct, FILE *out)
418 CE ce = ct->c_cefile;
421 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
424 result = writeBase64aux (ce->ce_fp, out);
425 (*ct->c_ceclosefnx) (ct);
431 writeBase64aux (FILE *in, FILE *out)
437 while ((cc = fread (inbuf, sizeof(*inbuf), sizeof(inbuf), in)) > 0) {
442 if (cc < sizeof(inbuf)) {
444 if (cc < sizeof(inbuf) - 1)
447 bits = (inbuf[0] & 0xff) << 16;
448 bits |= (inbuf[1] & 0xff) << 8;
449 bits |= inbuf[2] & 0xff;
451 for (bp = outbuf + sizeof(outbuf); bp > outbuf; bits >>= 6)
452 *--bp = nib2b64[bits & 0x3f];
453 if (cc < sizeof(inbuf)) {
455 if (cc < sizeof inbuf - 1)
459 fwrite (outbuf, sizeof(*outbuf), sizeof(outbuf), out);
461 if (cc < sizeof(inbuf)) {