]>
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 (! strcmp (file
, "-")) {
67 } else if ((fp
= fopen (file
, "w")) == NULL
) {
68 advise (file
, "unable to open for writing");
71 status
= output_message_fp(ct
, fp
, file
);
72 if (strcmp (file
, "-")) fclose(fp
);
78 * Output a Content structure to a file.
82 output_content (CT ct
, FILE *out
)
85 CI ci
= &ct
->c_ctinfo
;
86 char *boundary
= ci
->ci_values
[0], **ap
, **vp
;
88 for (ap
= ci
->ci_attrs
, vp
= ci
->ci_values
; *ap
; ++ap
, ++vp
) {
89 if (! mh_strcasecmp ("boundary", *ap
)) {
96 * Output all header fields for this content
98 output_headers (ct
, out
);
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).
109 * Now output the content bodies.
111 switch (ct
->c_type
) {
120 m
= (struct multipart
*) ct
->c_ctparams
;
121 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
122 CT p
= part
->mp_part
;
124 fprintf (out
, "\n--%s\n", boundary
);
125 if (output_content (p
, out
) == NOTOK
)
128 fprintf (out
, "\n--%s--\n", boundary
);
134 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
137 e
= (struct exbody
*) ct
->c_ctparams
;
138 if (output_content (e
->eb_content
, out
) == NOTOK
)
141 /* output phantom body for access-type "mail-server" */
143 writeExternalBody (ct
, out
);
145 result
= write8Bit (ct
, out
);
150 * Handle discrete types (text/application/audio/image/video)
153 switch (ct
->c_encoding
) {
156 result
= write8Bit (ct
, out
);
161 result
= write8Bit (ct
, out
);
166 result
= writeQuoted (ct
, out
);
171 result
= writeBase64ct (ct
, out
);
175 advise (NULL
, "can't handle binary transfer encoding in content");
180 advise (NULL
, "unknown transfer encoding in content");
192 * Output all the header fields for a content
196 output_headers (CT ct
, FILE *out
)
202 fprintf (out
, "%s:%s", hp
->name
, hp
->value
);
209 * Write the phantom body for access-type "mail-server".
213 writeExternalBody (CT ct
, FILE *out
)
215 char **ap
, **ep
, *cp
;
216 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
219 for (cp
= e
->eb_body
; *cp
; cp
++) {
220 CT ct2
= e
->eb_content
;
221 CI ci2
= &ct2
->c_ctinfo
;
227 char *dp
= trimcpy (ct2
->c_id
);
235 for (ap
= ci2
->ci_attrs
, ep
= ci2
->ci_values
; *ap
; ap
++, ep
++)
236 if (!mh_strcasecmp (*ap
, "name")) {
237 fprintf (out
, "%s", *ep
);
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
);
278 * Output a content without any transfer encoding
282 write8Bit (CT ct
, FILE *out
)
285 char c
, *file
, buffer
[BUFSIZ
];
286 CE ce
= ct
->c_cefile
;
289 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
293 while (fgets (buffer
, sizeof(buffer
) - 1, ce
->ce_fp
)) {
294 c
= buffer
[strlen (buffer
) - 1];
300 (*ct
->c_ceclosefnx
) (ct
);
306 * Output a content using quoted-printable
310 writeQuoted (CT ct
, FILE *out
)
314 char c
, buffer
[BUFSIZ
];
315 CE ce
= ct
->c_cefile
;
318 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
321 while (fgets (buffer
, sizeof(buffer
) - 1, ce
->ce_fp
)) {
324 cp
= buffer
+ strlen (buffer
) - 1;
325 if ((c
= *cp
) == '\n')
328 if (strncmp (cp
= buffer
, "From ", sizeof("From ") - 1) == 0) {
329 fprintf (out
, "=%02X", *cp
++ & 0xff);
335 if (n
> CPERLIN
- 3) {
348 if (*cp
< '!' || *cp
> '~')
356 fprintf (out
, "=%02X", *cp
& 0xff);
363 if (cp
> buffer
&& (*--cp
== ' ' || *cp
== '\t'))
372 (*ct
->c_ceclosefnx
) (ct
);
378 * Output a content using base64
382 writeBase64ct (CT ct
, FILE *out
)
386 CE ce
= ct
->c_cefile
;
389 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
392 result
= writeBase64aux (ce
->ce_fp
, out
);
393 (*ct
->c_ceclosefnx
) (ct
);