3 * mhoutsbr.c -- routines to output MIME messages
4 * -- given a Content structure
11 #include <h/signals.h>
15 #include <zotnet/mts/mts.h>
18 #include <h/mhparse.h>
20 #ifdef HAVE_SYS_WAIT_H
21 # 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 writeBase64aux (FILE *, FILE *);
75 static int output_content (CT, FILE *);
76 static void output_headers (CT, FILE *);
77 static int writeExternalBody (CT, FILE *);
78 static int write8Bit (CT, FILE *);
79 static int writeQuoted (CT, FILE *);
80 static int writeBase64 (CT, FILE *);
84 * Main routine to output a MIME message contained
85 * in a Content structure, to a file. Any necessary
86 * transfer encoding is added.
90 output_message (CT ct, char *file)
94 if ((fp = fopen (file, "w")) == NULL) {
95 advise (file, "unable to open for writing");
99 if (output_content (ct, fp) == NOTOK)
103 advise (file, "error writing to");
113 * Output a Content structure to a file.
117 output_content (CT ct, FILE *out)
120 CI ci = &ct->c_ctinfo;
123 * Output all header fields for this content
125 output_headers (ct, out);
128 * If this is the internal content structure for a
129 * "message/external", then we are done with the
130 * headers (since it has no body).
136 * Now output the content bodies.
138 switch (ct->c_type) {
147 m = (struct multipart *) ct->c_ctparams;
148 for (part = m->mp_parts; part; part = part->mp_next) {
149 CT p = part->mp_part;
151 fprintf (out, "\n--%s\n", ci->ci_values[0]);
152 if (output_content (p, out) == NOTOK)
155 fprintf (out, "\n--%s--\n", ci->ci_values[0]);
161 if (ct->c_subtype == MESSAGE_EXTERNAL) {
164 e = (struct exbody *) ct->c_ctparams;
165 if (output_content (e->eb_content, out) == NOTOK)
168 /* output phantom body for access-type "mail-server" */
170 writeExternalBody (ct, out);
172 result = write8Bit (ct, out);
177 * Handle discrete types (text/application/audio/image/video)
180 switch (ct->c_encoding) {
183 result = write8Bit (ct, out);
188 result = write8Bit (ct, out);
193 result = writeQuoted (ct, out);
198 result = writeBase64 (ct, out);
202 advise (NULL, "can't handle binary transfer encoding in content");
207 advise (NULL, "unknown transfer encoding in content");
219 * Output all the header fields for a content
223 output_headers (CT ct, FILE *out)
229 fprintf (out, "%s:%s", hp->name, hp->value);
236 * Write the phantom body for access-type "mail-server".
240 writeExternalBody (CT ct, FILE *out)
242 char **ap, **ep, *cp;
243 struct exbody *e = (struct exbody *) ct->c_ctparams;
246 for (cp = e->eb_body; *cp; cp++) {
247 CT ct2 = e->eb_content;
248 CI ci2 = &ct2->c_ctinfo;
254 char *dp = trimcpy (ct2->c_id);
262 for (ap = ci2->ci_attrs, ep = ci2->ci_values; *ap; ap++, ep++)
263 if (!strcasecmp (*ap, "name")) {
264 fprintf (out, "%s", *ep);
270 fprintf (out, "%s/%s", ci2->ci_type, ci2->ci_subtype);
271 for (ap = ci2->ci_attrs, ep = ci2->ci_values; *ap; ap++, ep++)
272 fprintf (out, "; %s=\"%s\"", *ap, *ep);
305 * Output a content without any transfer encoding
309 write8Bit (CT ct, FILE *out)
312 char c, *file, buffer[BUFSIZ];
313 CE ce = ct->c_cefile;
316 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
320 while (fgets (buffer, sizeof(buffer) - 1, ce->ce_fp)) {
321 c = buffer[strlen (buffer) - 1];
327 (*ct->c_ceclosefnx) (ct);
333 * Output a content using quoted-printable
337 writeQuoted (CT ct, FILE *out)
341 char c, buffer[BUFSIZ];
342 CE ce = ct->c_cefile;
345 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
348 while (fgets (buffer, sizeof(buffer) - 1, ce->ce_fp)) {
351 cp = buffer + strlen (buffer) - 1;
352 if ((c = *cp) == '\n')
355 if (strncmp (cp = buffer, "From ", sizeof("From ") - 1) == 0) {
356 fprintf (out, "=%02X", *cp++ & 0xff);
362 if (n > CPERLIN - 3) {
375 if (*cp < '!' || *cp > '~'
376 || (ebcdicsw && !ebcdicsafe[*cp & 0xff]))
384 fprintf (out, "=%02X", *cp & 0xff);
391 if (cp > buffer && (*--cp == ' ' || *cp == '\t'))
400 (*ct->c_ceclosefnx) (ct);
406 * Output a content using base64
410 writeBase64 (CT ct, FILE *out)
414 CE ce = ct->c_cefile;
417 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
420 result = writeBase64aux (ce->ce_fp, out);
421 (*ct->c_ceclosefnx) (ct);
427 writeBase64aux (FILE *in, FILE *out)
433 while ((cc = fread (inbuf, sizeof(*inbuf), sizeof(inbuf), in)) > 0) {
438 if (cc < sizeof(inbuf)) {
440 if (cc < sizeof(inbuf) - 1)
443 bits = (inbuf[0] & 0xff) << 16;
444 bits |= (inbuf[1] & 0xff) << 8;
445 bits |= inbuf[2] & 0xff;
447 for (bp = outbuf + sizeof(outbuf); bp > outbuf; bits >>= 6)
448 *--bp = nib2b64[bits & 0x3f];
449 if (cc < sizeof(inbuf)) {
451 if (cc < sizeof inbuf - 1)
455 fwrite (outbuf, sizeof(*outbuf), sizeof(outbuf), out);
457 if (cc < sizeof(inbuf)) {