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>
18 #include <h/mhparse.h>
22 static char ebcdicsafe[0x100] = {
23 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
24 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
25 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
26 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
27 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01,
28 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
29 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
30 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
31 0x00, 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 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01,
35 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
36 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
37 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
38 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
39 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
40 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
41 0x00, 0x00, 0x00, 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
57 static char nib2b64[0x40+1] =
58 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
63 int output_message(CT, char *);
64 int output_message_fp(CT, FILE *, char *);
65 int writeBase64aux(FILE *, FILE *);
70 static int output_content(CT, FILE *);
71 static void output_headers(CT, FILE *);
72 static int writeExternalBody(CT, FILE *);
73 static int write8Bit(CT, FILE *);
74 static int writeQuoted(CT, FILE *);
75 static int writeBase64(CT, FILE *);
79 ** Main routine to output a MIME message contained
80 ** in a Content structure, to a file. Any necessary
81 ** transfer encoding is added.
85 output_message_fp(CT ct, FILE *fp, char *file)
87 if (output_content(ct, fp) == NOTOK)
91 advise((file?file:"<FILE*>"), "error writing to");
98 output_message(CT ct, char *file)
103 if ((fp = fopen(file, "w")) == NULL) {
104 advise(file, "unable to open for writing");
107 status = output_message_fp(ct, fp, file);
114 ** Output a Content structure to a file.
118 output_content(CT ct, FILE *out)
121 CI ci = &ct->c_ctinfo;
124 ** Output all header fields for this content
126 output_headers(ct, out);
129 ** If this is the internal content structure for a
130 ** "message/external", then we are done with the
131 ** headers (since it has no body).
137 ** Now output the content bodies.
139 switch (ct->c_type) {
148 m = (struct multipart *) ct->c_ctparams;
149 for (part = m->mp_parts; part; part = part->mp_next) {
150 CT p = part->mp_part;
152 fprintf(out, "\n--%s\n", ci->ci_values[0]);
153 if (output_content(p, out) == NOTOK)
156 fprintf(out, "\n--%s--\n", ci->ci_values[0]);
162 if (ct->c_subtype == MESSAGE_EXTERNAL) {
165 e = (struct exbody *) ct->c_ctparams;
166 if (output_content(e->eb_content, out) == NOTOK)
169 /* output phantom body for access-type "mail-server" */
171 writeExternalBody(ct, out);
173 result = write8Bit(ct, out);
178 ** Handle discrete types (text/application/audio/image/video)
181 switch (ct->c_encoding) {
184 result = write8Bit(ct, out);
189 result = write8Bit(ct, out);
194 result = writeQuoted(ct, out);
199 result = writeBase64(ct, out);
203 advise(NULL, "can't handle binary transfer encoding in content");
208 advise(NULL, "unknown transfer encoding in content");
220 ** Output all the header fields for a content
224 output_headers(CT ct, FILE *out)
230 fprintf(out, "%s:%s", hp->name, hp->value);
237 ** Write the phantom body for access-type "mail-server".
241 writeExternalBody(CT ct, FILE *out)
243 char **ap, **ep, *cp;
244 struct exbody *e = (struct exbody *) ct->c_ctparams;
247 for (cp = e->eb_body; *cp; cp++) {
248 CT ct2 = e->eb_content;
249 CI ci2 = &ct2->c_ctinfo;
255 char *dp = trimcpy(ct2->c_id);
263 for (ap = ci2->ci_attrs, ep = ci2->ci_values;
265 if (!mh_strcasecmp(*ap, "name")) {
266 fprintf(out, "%s", *ep);
272 fprintf(out, "%s/%s", ci2->ci_type,
274 for (ap = ci2->ci_attrs, ep = ci2->ci_values;
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 > '~' || (ebcdicsw && !ebcdicsafe[*cp & 0xff]))
387 fprintf(out, "=%02X", *cp & 0xff);
394 if (cp > buffer && (*--cp == ' ' || *cp == '\t'))
403 (*ct->c_ceclosefnx) (ct);
409 ** Output a content using base64
413 writeBase64(CT ct, FILE *out)
417 CE ce = ct->c_cefile;
420 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
423 result = writeBase64aux(ce->ce_fp, out);
424 (*ct->c_ceclosefnx) (ct);
430 writeBase64aux(FILE *in, FILE *out)
436 while ((cc = fread(inbuf, sizeof(*inbuf), sizeof(inbuf), in)) > 0) {
441 if (cc < sizeof(inbuf)) {
443 if (cc < sizeof(inbuf) - 1)
446 bits = (inbuf[0] & 0xff) << 16;
447 bits |= (inbuf[1] & 0xff) << 8;
448 bits |= inbuf[2] & 0xff;
450 for (bp = outbuf + sizeof(outbuf); bp > outbuf; bits >>= 6)
451 *--bp = nib2b64[bits & 0x3f];
452 if (cc < sizeof(inbuf)) {
454 if (cc < sizeof inbuf - 1)
458 fwrite(outbuf, sizeof(*outbuf), sizeof(outbuf), out);
460 if (cc < sizeof(inbuf)) {