]> diplodocus.org Git - nmh/blob - uip/mhoutsbr.c
When debugging is enabled, replace the ctype functions with
[nmh] / uip / mhoutsbr.c
1
2 /*
3 * mhoutsbr.c -- routines to output MIME messages
4 * -- given a Content structure
5 *
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.
9 */
10
11 #include <h/mh.h>
12 #include <fcntl.h>
13 #include <h/signals.h>
14 #include <h/md5.h>
15 #include <errno.h>
16 #include <signal.h>
17 #include <h/mts.h>
18 #include <h/tws.h>
19 #include <h/mime.h>
20 #include <h/mhparse.h>
21
22
23 /*
24 * prototypes
25 */
26 int output_message (CT, char *);
27 int output_message_fp (CT, FILE *, char *);
28
29 /*
30 * static prototypes
31 */
32 static int output_content (CT, FILE *);
33 static void output_headers (CT, FILE *);
34 static int writeExternalBody (CT, FILE *);
35 static int write8Bit (CT, FILE *);
36 static int writeQuoted (CT, FILE *);
37 static int writeBase64ct (CT, FILE *);
38
39
40 /*
41 * Main routine to output a MIME message contained
42 * in a Content structure, to a file. Any necessary
43 * transfer encoding is added.
44 */
45
46 int
47 output_message_fp (CT ct, FILE *fp, char *file)
48 {
49 if (output_content (ct, fp) == NOTOK)
50 return NOTOK;
51
52 if (fflush (fp)) {
53 advise ((file?file:"<FILE*>"), "error writing to");
54 return NOTOK;
55 }
56 return OK;
57 }
58
59 int
60 output_message (CT ct, char *file)
61 {
62 FILE *fp;
63 int status;
64
65 if (! strcmp (file, "-")) {
66 fp = stdout;
67 } else if ((fp = fopen (file, "w")) == NULL) {
68 advise (file, "unable to open for writing");
69 return NOTOK;
70 }
71 status = output_message_fp(ct, fp, file);
72 if (strcmp (file, "-")) fclose(fp);
73 return status;
74 }
75
76
77 /*
78 * Output a Content structure to a file.
79 */
80
81 static int
82 output_content (CT ct, FILE *out)
83 {
84 int result = 0;
85 CI ci = &ct->c_ctinfo;
86 char *boundary = ci->ci_values[0], **ap, **vp;
87
88 for (ap = ci->ci_attrs, vp = ci->ci_values; *ap; ++ap, ++vp) {
89 if (! mh_strcasecmp ("boundary", *ap)) {
90 boundary = *vp;
91 break;
92 }
93 }
94
95 /*
96 * Output all header fields for this content
97 */
98 output_headers (ct, out);
99
100 /*
101 * If this is the internal content structure for a
102 * "message/external", then we are done with the
103 * headers (since it has no body).
104 */
105 if (ct->c_ctexbody)
106 return OK;
107
108 /*
109 * Now output the content bodies.
110 */
111 switch (ct->c_type) {
112 case CT_MULTIPART:
113 {
114 struct multipart *m;
115 struct part *part;
116
117 if (ct->c_rfc934)
118 putc ('\n', out);
119
120 m = (struct multipart *) ct->c_ctparams;
121 for (part = m->mp_parts; part; part = part->mp_next) {
122 CT p = part->mp_part;
123
124 fprintf (out, "\n--%s\n", boundary);
125 if (output_content (p, out) == NOTOK)
126 return NOTOK;
127 }
128 fprintf (out, "\n--%s--\n", boundary);
129 }
130 break;
131
132 case CT_MESSAGE:
133 putc ('\n', out);
134 if (ct->c_subtype == MESSAGE_EXTERNAL) {
135 struct exbody *e;
136
137 e = (struct exbody *) ct->c_ctparams;
138 if (output_content (e->eb_content, out) == NOTOK)
139 return NOTOK;
140
141 /* output phantom body for access-type "mail-server" */
142 if (e->eb_body)
143 writeExternalBody (ct, out);
144 } else {
145 result = write8Bit (ct, out);
146 }
147 break;
148
149 /*
150 * Handle discrete types (text/application/audio/image/video)
151 */
152 default:
153 switch (ct->c_encoding) {
154 case CE_7BIT:
155 putc ('\n', out);
156 result = write8Bit (ct, out);
157 break;
158
159 case CE_8BIT:
160 putc ('\n', out);
161 result = write8Bit (ct, out);
162 break;
163
164 case CE_QUOTED:
165 putc ('\n', out);
166 result = writeQuoted (ct, out);
167 break;
168
169 case CE_BASE64:
170 putc ('\n', out);
171 result = writeBase64ct (ct, out);
172 break;
173
174 case CE_BINARY:
175 advise (NULL, "can't handle binary transfer encoding in content");
176 result = NOTOK;
177 break;
178
179 default:
180 advise (NULL, "unknown transfer encoding in content");
181 result = NOTOK;
182 break;
183 }
184 break;
185 }
186
187 return result;
188 }
189
190
191 /*
192 * Output all the header fields for a content
193 */
194
195 static void
196 output_headers (CT ct, FILE *out)
197 {
198 HF hp;
199
200 hp = ct->c_first_hf;
201 while (hp) {
202 fprintf (out, "%s:%s", hp->name, hp->value);
203 hp = hp->next;
204 }
205 }
206
207
208 /*
209 * Write the phantom body for access-type "mail-server".
210 */
211
212 static int
213 writeExternalBody (CT ct, FILE *out)
214 {
215 char **ap, **ep, *cp;
216 struct exbody *e = (struct exbody *) ct->c_ctparams;
217
218 putc ('\n', out);
219 for (cp = e->eb_body; *cp; cp++) {
220 CT ct2 = e->eb_content;
221 CI ci2 = &ct2->c_ctinfo;
222
223 if (*cp == '\\') {
224 switch (*++cp) {
225 case 'I':
226 if (ct2->c_id) {
227 char *dp = trimcpy (ct2->c_id);
228
229 fputs (dp, out);
230 free (dp);
231 }
232 continue;
233
234 case 'N':
235 for (ap = ci2->ci_attrs, ep = ci2->ci_values; *ap; ap++, ep++)
236 if (!mh_strcasecmp (*ap, "name")) {
237 fprintf (out, "%s", *ep);
238 break;
239 }
240 continue;
241
242 case 'T':
243 fprintf (out, "%s/%s", ci2->ci_type, ci2->ci_subtype);
244 for (ap = ci2->ci_attrs, ep = ci2->ci_values; *ap; ap++, ep++)
245 fprintf (out, "; %s=\"%s\"", *ap, *ep);
246 continue;
247
248 case 'n':
249 putc ('\n', out);
250 continue;
251
252 case 't':
253 putc ('\t', out);
254 continue;
255
256 case '\0':
257 cp--;
258 break;
259
260 case '\\':
261 case '"':
262 break;
263
264 default:
265 putc ('\\', out);
266 break;
267 }
268 }
269 putc (*cp, out);
270 }
271 putc ('\n', out);
272
273 return OK;
274 }
275
276
277 /*
278 * Output a content without any transfer encoding
279 */
280
281 static int
282 write8Bit (CT ct, FILE *out)
283 {
284 int fd;
285 char c, *file, buffer[BUFSIZ];
286 CE ce = ct->c_cefile;
287
288 file = NULL;
289 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
290 return NOTOK;
291
292 c = '\n';
293 while (fgets (buffer, sizeof(buffer) - 1, ce->ce_fp)) {
294 c = buffer[strlen (buffer) - 1];
295 fputs (buffer, out);
296 }
297 if (c != '\n')
298 putc ('\n', out);
299
300 (*ct->c_ceclosefnx) (ct);
301 return OK;
302 }
303
304
305 /*
306 * Output a content using quoted-printable
307 */
308
309 static int
310 writeQuoted (CT ct, FILE *out)
311 {
312 int fd;
313 char *cp, *file;
314 char c, buffer[BUFSIZ];
315 CE ce = ct->c_cefile;
316
317 file = NULL;
318 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
319 return NOTOK;
320
321 while (fgets (buffer, sizeof(buffer) - 1, ce->ce_fp)) {
322 int n;
323
324 cp = buffer + strlen (buffer) - 1;
325 if ((c = *cp) == '\n')
326 *cp = '\0';
327
328 if (strncmp (cp = buffer, "From ", sizeof("From ") - 1) == 0) {
329 fprintf (out, "=%02X", *cp++ & 0xff);
330 n = 3;
331 } else {
332 n = 0;
333 }
334 for (; *cp; cp++) {
335 if (n > CPERLIN - 3) {
336 fputs ("=\n", out);
337 n = 0;
338 }
339
340 switch (*cp) {
341 case ' ':
342 case '\t':
343 putc (*cp, out);
344 n++;
345 break;
346
347 default:
348 if (*cp < '!' || *cp > '~')
349 goto three_print;
350 putc (*cp, out);
351 n++;
352 break;
353
354 case '=':
355 three_print:
356 fprintf (out, "=%02X", *cp & 0xff);
357 n += 3;
358 break;
359 }
360 }
361
362 if (c == '\n') {
363 if (cp > buffer && (*--cp == ' ' || *cp == '\t'))
364 fputs ("=\n", out);
365
366 putc ('\n', out);
367 } else {
368 fputs ("=\n", out);
369 }
370 }
371
372 (*ct->c_ceclosefnx) (ct);
373 return OK;
374 }
375
376
377 /*
378 * Output a content using base64
379 */
380
381 static int
382 writeBase64ct (CT ct, FILE *out)
383 {
384 int fd, result;
385 char *file;
386 CE ce = ct->c_cefile;
387
388 file = NULL;
389 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
390 return NOTOK;
391
392 result = writeBase64aux (ce->ce_fp, out);
393 (*ct->c_ceclosefnx) (ct);
394 return result;
395 }