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>
22 #ifdef HAVE_SYS_WAIT_H
23 # include <sys/wait.h>
29 static char ebcdicsafe[0x100] = {
30 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
31 0x00, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
32 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
33 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
34 0x01, 0x00, 0x00, 0x00, 0x00, 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, 0x01, 0x01, 0x01, 0x01, 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, 0x01,
42 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
43 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
44 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
45 0x01, 0x01, 0x01, 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,
61 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
64 static char nib2b64[0x40+1] =
65 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
70 int output_message (CT, char *);
71 int output_message_fp (CT, FILE *, char *);
72 int writeBase64aux (FILE *, FILE *);
77 static int output_content (CT, FILE *);
78 static void output_headers (CT, FILE *);
79 static int writeExternalBody (CT, FILE *);
80 static int write8Bit (CT, FILE *);
81 static int writeQuoted (CT, FILE *);
82 static int writeBase64 (CT, FILE *);
86 * Main routine to output a MIME message contained
87 * in a Content structure, to a file. Any necessary
88 * transfer encoding is added.
92 output_message_fp (CT ct, FILE *fp, char *file)
94 if (output_content (ct, fp) == NOTOK)
98 advise ((file?file:"<FILE*>"), "error writing to");
105 output_message (CT ct, char *file)
110 if ((fp = fopen (file, "w")) == NULL) {
111 advise (file, "unable to open for writing");
114 status = output_message_fp(ct, fp, file);
121 * Output a Content structure to a file.
125 output_content (CT ct, FILE *out)
128 CI ci = &ct->c_ctinfo;
131 * Output all header fields for this content
133 output_headers (ct, out);
136 * If this is the internal content structure for a
137 * "message/external", then we are done with the
138 * headers (since it has no body).
144 * Now output the content bodies.
146 switch (ct->c_type) {
155 m = (struct multipart *) ct->c_ctparams;
156 for (part = m->mp_parts; part; part = part->mp_next) {
157 CT p = part->mp_part;
159 fprintf (out, "\n--%s\n", ci->ci_values[0]);
160 if (output_content (p, out) == NOTOK)
163 fprintf (out, "\n--%s--\n", ci->ci_values[0]);
169 if (ct->c_subtype == MESSAGE_EXTERNAL) {
172 e = (struct exbody *) ct->c_ctparams;
173 if (output_content (e->eb_content, out) == NOTOK)
176 /* output phantom body for access-type "mail-server" */
178 writeExternalBody (ct, out);
180 result = write8Bit (ct, out);
185 * Handle discrete types (text/application/audio/image/video)
188 switch (ct->c_encoding) {
191 result = write8Bit (ct, out);
196 result = write8Bit (ct, out);
201 result = writeQuoted (ct, out);
206 result = writeBase64 (ct, out);
210 advise (NULL, "can't handle binary transfer encoding in content");
215 advise (NULL, "unknown transfer encoding in content");
227 * Output all the header fields for a content
231 output_headers (CT ct, FILE *out)
237 fprintf (out, "%s:%s", hp->name, hp->value);
244 * Write the phantom body for access-type "mail-server".
248 writeExternalBody (CT ct, FILE *out)
250 char **ap, **ep, *cp;
251 struct exbody *e = (struct exbody *) ct->c_ctparams;
254 for (cp = e->eb_body; *cp; cp++) {
255 CT ct2 = e->eb_content;
256 CI ci2 = &ct2->c_ctinfo;
262 char *dp = trimcpy (ct2->c_id);
270 for (ap = ci2->ci_attrs, ep = ci2->ci_values; *ap; ap++, ep++)
271 if (!mh_strcasecmp (*ap, "name")) {
272 fprintf (out, "%s", *ep);
278 fprintf (out, "%s/%s", ci2->ci_type, ci2->ci_subtype);
279 for (ap = ci2->ci_attrs, ep = ci2->ci_values; *ap; ap++, ep++)
280 fprintf (out, "; %s=\"%s\"", *ap, *ep);
313 * Output a content without any transfer encoding
317 write8Bit (CT ct, FILE *out)
320 char c, *file, buffer[BUFSIZ];
321 CE ce = ct->c_cefile;
324 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
328 while (fgets (buffer, sizeof(buffer) - 1, ce->ce_fp)) {
329 c = buffer[strlen (buffer) - 1];
335 (*ct->c_ceclosefnx) (ct);
341 * Output a content using quoted-printable
345 writeQuoted (CT ct, FILE *out)
349 char c, buffer[BUFSIZ];
350 CE ce = ct->c_cefile;
353 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
356 while (fgets (buffer, sizeof(buffer) - 1, ce->ce_fp)) {
359 cp = buffer + strlen (buffer) - 1;
360 if ((c = *cp) == '\n')
363 if (strncmp (cp = buffer, "From ", sizeof("From ") - 1) == 0) {
364 fprintf (out, "=%02X", *cp++ & 0xff);
370 if (n > CPERLIN - 3) {
383 if (*cp < '!' || *cp > '~'
384 || (ebcdicsw && !ebcdicsafe[*cp & 0xff]))
392 fprintf (out, "=%02X", *cp & 0xff);
399 if (cp > buffer && (*--cp == ' ' || *cp == '\t'))
408 (*ct->c_ceclosefnx) (ct);
414 * Output a content using base64
418 writeBase64 (CT ct, FILE *out)
422 CE ce = ct->c_cefile;
425 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
428 result = writeBase64aux (ce->ce_fp, out);
429 (*ct->c_ceclosefnx) (ct);
435 writeBase64aux (FILE *in, FILE *out)
441 while ((cc = fread (inbuf, sizeof(*inbuf), sizeof(inbuf), in)) > 0) {
446 if (cc < sizeof(inbuf)) {
448 if (cc < sizeof(inbuf) - 1)
451 bits = (inbuf[0] & 0xff) << 16;
452 bits |= (inbuf[1] & 0xff) << 8;
453 bits |= inbuf[2] & 0xff;
455 for (bp = outbuf + sizeof(outbuf); bp > outbuf; bits >>= 6)
456 *--bp = nib2b64[bits & 0x3f];
457 if (cc < sizeof(inbuf)) {
459 if (cc < sizeof inbuf - 1)
463 fwrite (outbuf, sizeof(*outbuf), sizeof(outbuf), out);
465 if (cc < sizeof(inbuf)) {