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;
271 if (!mh_strcasecmp(*ap, "name")) {
272 fprintf(out, "%s", *ep);
278 fprintf(out, "%s/%s", ci2->ci_type,
280 for (ap = ci2->ci_attrs, ep = ci2->ci_values;
282 fprintf(out, "; %s=\"%s\"", *ap, *ep);
315 ** Output a content without any transfer encoding
319 write8Bit(CT ct, FILE *out)
322 char c, *file, buffer[BUFSIZ];
323 CE ce = ct->c_cefile;
326 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
330 while (fgets(buffer, sizeof(buffer) - 1, ce->ce_fp)) {
331 c = buffer[strlen(buffer) - 1];
337 (*ct->c_ceclosefnx) (ct);
343 ** Output a content using quoted-printable
347 writeQuoted(CT ct, FILE *out)
351 char c, buffer[BUFSIZ];
352 CE ce = ct->c_cefile;
355 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
358 while (fgets(buffer, sizeof(buffer) - 1, ce->ce_fp)) {
361 cp = buffer + strlen(buffer) - 1;
362 if ((c = *cp) == '\n')
365 if (strncmp(cp = buffer, "From ", sizeof("From ") - 1) == 0) {
366 fprintf(out, "=%02X", *cp++ & 0xff);
372 if (n > CPERLIN - 3) {
385 if (*cp < '!' || *cp > '~' || (ebcdicsw && !ebcdicsafe[*cp & 0xff]))
393 fprintf(out, "=%02X", *cp & 0xff);
400 if (cp > buffer && (*--cp == ' ' || *cp == '\t'))
409 (*ct->c_ceclosefnx) (ct);
415 ** Output a content using base64
419 writeBase64(CT ct, FILE *out)
423 CE ce = ct->c_cefile;
426 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
429 result = writeBase64aux(ce->ce_fp, out);
430 (*ct->c_ceclosefnx) (ct);
436 writeBase64aux(FILE *in, FILE *out)
442 while ((cc = fread(inbuf, sizeof(*inbuf), sizeof(inbuf), in)) > 0) {
447 if (cc < sizeof(inbuf)) {
449 if (cc < sizeof(inbuf) - 1)
452 bits = (inbuf[0] & 0xff) << 16;
453 bits |= (inbuf[1] & 0xff) << 8;
454 bits |= inbuf[2] & 0xff;
456 for (bp = outbuf + sizeof(outbuf); bp > outbuf; bits >>= 6)
457 *--bp = nib2b64[bits & 0x3f];
458 if (cc < sizeof(inbuf)) {
460 if (cc < sizeof inbuf - 1)
464 fwrite(outbuf, sizeof(*outbuf), sizeof(outbuf), out);
466 if (cc < sizeof(inbuf)) {