]>
diplodocus.org Git - nmh/blob - uip/mhoutsbr.c
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>
26 int output_message (CT
, char *);
27 int output_message_fp (CT
, FILE *, char *);
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 *);
41 * Main routine to output a MIME message contained
42 * in a Content structure, to a file. Any necessary
43 * transfer encoding is added.
47 output_message_fp (CT ct
, FILE *fp
, char *file
)
49 if (output_content (ct
, fp
) == NOTOK
)
53 advise ((file
?file
:"<FILE*>"), "error writing to");
60 output_message (CT ct
, char *file
)
65 if ((fp
= fopen (file
, "w")) == NULL
) {
66 advise (file
, "unable to open for writing");
69 status
= output_message_fp(ct
, fp
, file
);
76 * Output a Content structure to a file.
80 output_content (CT ct
, FILE *out
)
83 CI ci
= &ct
->c_ctinfo
;
86 * Output all header fields for this content
88 output_headers (ct
, out
);
91 * If this is the internal content structure for a
92 * "message/external", then we are done with the
93 * headers (since it has no body).
99 * Now output the content bodies.
101 switch (ct
->c_type
) {
110 m
= (struct multipart
*) ct
->c_ctparams
;
111 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
112 CT p
= part
->mp_part
;
114 fprintf (out
, "\n--%s\n", ci
->ci_values
[0]);
115 if (output_content (p
, out
) == NOTOK
)
118 fprintf (out
, "\n--%s--\n", ci
->ci_values
[0]);
124 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
127 e
= (struct exbody
*) ct
->c_ctparams
;
128 if (output_content (e
->eb_content
, out
) == NOTOK
)
131 /* output phantom body for access-type "mail-server" */
133 writeExternalBody (ct
, out
);
135 result
= write8Bit (ct
, out
);
140 * Handle discrete types (text/application/audio/image/video)
143 switch (ct
->c_encoding
) {
146 result
= write8Bit (ct
, out
);
151 result
= write8Bit (ct
, out
);
156 result
= writeQuoted (ct
, out
);
161 result
= writeBase64ct (ct
, out
);
165 advise (NULL
, "can't handle binary transfer encoding in content");
170 advise (NULL
, "unknown transfer encoding in content");
182 * Output all the header fields for a content
186 output_headers (CT ct
, FILE *out
)
192 fprintf (out
, "%s:%s", hp
->name
, hp
->value
);
199 * Write the phantom body for access-type "mail-server".
203 writeExternalBody (CT ct
, FILE *out
)
205 char **ap
, **ep
, *cp
;
206 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
209 for (cp
= e
->eb_body
; *cp
; cp
++) {
210 CT ct2
= e
->eb_content
;
211 CI ci2
= &ct2
->c_ctinfo
;
217 char *dp
= trimcpy (ct2
->c_id
);
225 for (ap
= ci2
->ci_attrs
, ep
= ci2
->ci_values
; *ap
; ap
++, ep
++)
226 if (!mh_strcasecmp (*ap
, "name")) {
227 fprintf (out
, "%s", *ep
);
233 fprintf (out
, "%s/%s", ci2
->ci_type
, ci2
->ci_subtype
);
234 for (ap
= ci2
->ci_attrs
, ep
= ci2
->ci_values
; *ap
; ap
++, ep
++)
235 fprintf (out
, "; %s=\"%s\"", *ap
, *ep
);
268 * Output a content without any transfer encoding
272 write8Bit (CT ct
, FILE *out
)
275 char c
, *file
, buffer
[BUFSIZ
];
276 CE ce
= ct
->c_cefile
;
279 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
283 while (fgets (buffer
, sizeof(buffer
) - 1, ce
->ce_fp
)) {
284 c
= buffer
[strlen (buffer
) - 1];
290 (*ct
->c_ceclosefnx
) (ct
);
296 * Output a content using quoted-printable
300 writeQuoted (CT ct
, FILE *out
)
304 char c
, buffer
[BUFSIZ
];
305 CE ce
= ct
->c_cefile
;
308 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
311 while (fgets (buffer
, sizeof(buffer
) - 1, ce
->ce_fp
)) {
314 cp
= buffer
+ strlen (buffer
) - 1;
315 if ((c
= *cp
) == '\n')
318 if (strncmp (cp
= buffer
, "From ", sizeof("From ") - 1) == 0) {
319 fprintf (out
, "=%02X", *cp
++ & 0xff);
325 if (n
> CPERLIN
- 3) {
338 if (*cp
< '!' || *cp
> '~')
346 fprintf (out
, "=%02X", *cp
& 0xff);
353 if (cp
> buffer
&& (*--cp
== ' ' || *cp
== '\t'))
362 (*ct
->c_ceclosefnx
) (ct
);
368 * Output a content using base64
372 writeBase64ct (CT ct
, FILE *out
)
376 CE ce
= ct
->c_cefile
;
379 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
382 result
= writeBase64aux (ce
->ce_fp
, out
);
383 (*ct
->c_ceclosefnx
) (ct
);