1 /* mhfixmsg.c -- rewrite a message with various transformations
3 * This code is Copyright (c) 2002 and 2013, by the authors of nmh.
4 * See the COPYRIGHT file in the root directory of the nmh
5 * distribution for complete copyright information.
9 #include "sbr/m_name.h"
10 #include "sbr/m_gmprot.h"
11 #include "sbr/m_getfld.h"
12 #include "sbr/getarguments.h"
13 #include "sbr/concat.h"
14 #include "sbr/seq_setprev.h"
15 #include "sbr/seq_setcur.h"
16 #include "sbr/seq_save.h"
17 #include "sbr/smatch.h"
18 #include "sbr/fmt_rfc2047.h"
19 #include "sbr/cpydata.h"
20 #include "sbr/trimcpy.h"
21 #include "sbr/m_convert.h"
22 #include "sbr/m_backup.h"
23 #include "sbr/getfolder.h"
24 #include "sbr/folder_read.h"
25 #include "sbr/context_save.h"
26 #include "sbr/context_replace.h"
27 #include "sbr/context_find.h"
28 #include "sbr/readconfig.h"
29 #include "sbr/ambigsw.h"
31 #include "sbr/print_version.h"
32 #include "sbr/print_help.h"
33 #include "sbr/error.h"
34 #include "h/fmt_scan.h"
36 #include "h/mhparse.h"
39 #include "h/signals.h"
40 #include "sbr/m_maildir.h"
41 #include "sbr/m_mktemp.h"
42 #include "sbr/mime_type.h"
46 #include "mhshowsbr.h"
49 #define MHFIXMSG_SWITCHES \
50 X("decodetext 8bit|7bit|binary", 0, DECODETEXTSW) \
51 X("nodecodetext", 0, NDECODETEXTSW) \
52 X("decodetypes", 0, DECODETYPESW) \
53 X("decodeheaderfieldbodies utf-8", 0, DECODEHEADERFIELDBODIESSW) \
54 X("nodecodeheaderfieldbodies", 0, NDECODEHEADERFIELDBODIESSW) \
55 X("crlflinebreaks", 0, CRLFLINEBREAKSSW) \
56 X("nocrlflinebreaks", 0, NCRLFLINEBREAKSSW) \
57 X("textcharset", 0, TEXTCHARSETSW) \
58 X("notextcharset", 0, NTEXTCHARSETSW) \
59 X("reformat", 0, REFORMATSW) \
60 X("noreformat", 0, NREFORMATSW) \
61 X("replacetextplain", 0, REPLACETEXTPLAINSW) \
62 X("noreplacetextplain", 0, NREPLACETEXTPLAINSW) \
63 X("fixboundary", 0, FIXBOUNDARYSW) \
64 X("nofixboundary", 0, NFIXBOUNDARYSW) \
65 X("fixcte", 0, FIXCOMPOSITECTESW) \
66 X("nofixcte", 0, NFIXCOMPOSITECTESW) \
67 X("fixtype mimetype", 0, FIXTYPESW) \
68 X("file file", 0, FILESW) \
69 X("outfile file", 0, OUTFILESW) \
70 X("rmmproc program", 0, RPROCSW) \
71 X("normmproc", 0, NRPRCSW) \
72 X("changecur", 0, CHGSW) \
73 X("nochangecur", 0, NCHGSW) \
74 X("verbose", 0, VERBSW) \
75 X("noverbose", 0, NVERBSW) \
76 X("version", 0, VERSIONSW) \
77 X("help", 0, HELPSW) \
79 #define X(sw, minchars, id) id,
80 DEFINE_SWITCH_ENUM(MHFIXMSG
);
83 #define X(sw, minchars, id) { sw, minchars, id },
84 DEFINE_SWITCH_ARRAY(MHFIXMSG
, switches
);
89 int debugsw
; /* Needed by mhparse.c. */
91 #define quitser pipeser
96 typedef struct fix_transformations
{
101 int replacetextplain
;
104 char *decodeheaderfieldbodies
; /* Either NULL or "utf-8". */
105 /* Whether to use CRLF linebreaks, per RFC 2046 Sec. 4.1.1, par.1. */
108 } fix_transformations
;
110 static int mhfixmsgsbr (CT
*, char *, const fix_transformations
*,
111 FILE **, char *, FILE **);
112 static int fix_boundary (CT
*, int *);
113 static int copy_input_to_output (const char *, FILE *, const char *, FILE *);
114 static int get_multipart_boundary (CT
, char **);
115 static int replace_boundary (CT
, char *, char *);
116 static int fix_types (CT
, svector_t
, int *);
117 static char *replace_substring (char **, const char *, const char *);
118 static char *remove_parameter (char *, const char *);
119 static int fix_composite_cte (CT
, int *);
120 static int set_ce (CT
, int);
121 static int ensure_text_plain (CT
*, CT
, int *, int);
122 static int find_textplain_sibling (CT
, int, int *);
123 static int insert_new_text_plain_part (CT
, int, CT
);
124 static CT
build_text_plain_part (CT
);
125 static int insert_into_new_mp_alt (CT
*, int *);
126 static CT
divide_part (CT
);
127 static void copy_ctinfo (CI
, CI
);
128 static int decode_part (CT
);
129 static int reformat_part (CT
, char *, char *, char *, int);
130 static CT
build_multipart_alt (CT
, CT
, int, int);
131 static int boundary_in_content (FILE **, char *, const char *);
132 static void transfer_noncontent_headers (CT
, CT
);
133 static int set_ct_type (CT
, int type
, int subtype
, int encoding
);
134 static int decode_text_parts (CT
, int, const char *, int *);
135 static int should_decode(const char *, const char *, const char *);
136 static int content_encoding (CT
, const char **);
137 static int strip_crs (CT
, int *);
138 static void update_cte (CT
);
139 static int least_restrictive_encoding (CT
) PURE
;
140 static int less_restrictive (int, int);
141 static int convert_charsets (CT
, char *, int *);
142 static int fix_always (CT
, int *);
143 static int decode_header_field_bodies (CT
, int *);
144 static int fix_filename_param (char *, char *, PM
*, PM
*);
145 static int fix_filename_encoding (CT
);
146 static int write_content (CT
, const char *, char *, FILE *, int, int);
147 static void set_text_ctparams(CT
, char *, int);
148 static int remove_file (const char *);
149 static void report (char *, char *, char *, char *, ...)
151 static void pipeser (int);
155 main (int argc
, char **argv
)
158 char *cp
, *file
= NULL
, *folder
= NULL
;
159 char *maildir
= NULL
, buf
[100], *outfile
= NULL
;
160 char **argp
, **arguments
;
161 struct msgs_array msgs
= { 0, 0, NULL
};
162 struct msgs
*mp
= NULL
;
164 FILE *fp
, *infp
= NULL
, *outfp
= NULL
;
165 bool using_stdin
= false;
168 fix_transformations fx
;
169 fx
.reformat
= fx
.fixcompositecte
= fx
.fixboundary
= 1;
171 fx
.replacetextplain
= 0;
172 fx
.decodetext
= CE_8BIT
;
173 fx
.decodetypes
= "text,application/ics"; /* Default, per man page. */
174 fx
.decodeheaderfieldbodies
= NULL
;
175 fx
.lf_line_endings
= 0;
176 fx
.textcharset
= NULL
;
178 if (nmh_init(argv
[0], true, false)) { return 1; }
180 arguments
= getarguments (invo_name
, argc
, argv
, 1);
186 while ((cp
= *argp
++)) {
188 switch (smatch (++cp
, switches
)) {
190 ambigsw (cp
, switches
);
193 die("-%s unknown", cp
);
196 snprintf (buf
, sizeof buf
, "%s [+folder] [msgs] [switches]",
198 print_help (buf
, switches
, 1);
201 print_version(invo_name
);
205 if (! (cp
= *argp
++) || *cp
== '-') {
206 die("missing argument to %s", argp
[-2]);
208 if (! strcasecmp (cp
, "8bit")) {
209 fx
.decodetext
= CE_8BIT
;
210 } else if (! strcasecmp (cp
, "7bit")) {
211 fx
.decodetext
= CE_7BIT
;
212 } else if (! strcasecmp (cp
, "binary")) {
213 fx
.decodetext
= CE_BINARY
;
215 die("invalid argument to %s", argp
[-2]);
222 if (! (cp
= *argp
++) || *cp
== '-') {
223 die("missing argument to %s", argp
[-2]);
227 case DECODEHEADERFIELDBODIESSW
:
228 if (! (cp
= *argp
++) || *cp
== '-') {
229 die("missing argument to %s", argp
[-2]);
231 fx
.decodeheaderfieldbodies
= cp
;
232 if (strcasecmp (cp
, "utf-8") && strcasecmp (cp
, "utf8")) {
233 /* Because UTF-8 strings can't have embedded nulls. Other
234 encodings support that, too, but we won't bother to
236 die("-decodeheaderfieldbodies only supports utf-8");
239 case NDECODEHEADERFIELDBODIESSW
:
240 fx
.decodeheaderfieldbodies
= NULL
;
242 case CRLFLINEBREAKSSW
:
243 fx
.lf_line_endings
= 0;
245 case NCRLFLINEBREAKSSW
:
246 fx
.lf_line_endings
= 1;
249 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
250 die("missing argument to %s", argp
[-2]);
263 case FIXCOMPOSITECTESW
:
264 fx
.fixcompositecte
= 1;
266 case NFIXCOMPOSITECTESW
:
267 fx
.fixcompositecte
= 0;
270 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
271 die("missing argument to %s", argp
[-2]);
273 if (! strncasecmp (cp
, "multipart/", 10) ||
274 ! strncasecmp (cp
, "message/", 8))
275 die("-fixtype %s not allowed", cp
);
276 if (! strchr (cp
, '/'))
277 die("-fixtype requires type/subtype");
278 if (fx
.fixtypes
== NULL
) { fx
.fixtypes
= svector_create (10); }
279 svector_push_back (fx
.fixtypes
, cp
);
287 case REPLACETEXTPLAINSW
:
288 fx
.replacetextplain
= 1;
290 case NREPLACETEXTPLAINSW
:
291 fx
.replacetextplain
= 0;
294 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
295 die("missing argument to %s", argp
[-2]);
297 file
= *cp
== '-' ? mh_xstrdup (cp
) : path (cp
, TFILE
);
300 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
301 die("missing argument to %s", argp
[-2]);
303 outfile
= *cp
== '-' ? mh_xstrdup (cp
) : path (cp
, TFILE
);
306 if (!(rmmproc
= *argp
++) || *rmmproc
== '-') {
307 die("missing argument to %s", argp
[-2]);
327 if (*cp
== '+' || *cp
== '@') {
329 die("only one folder at a time!");
330 folder
= pluspath (cp
);
333 /* Interpret a full path as a filename, not a message. */
334 file
= mh_xstrdup (cp
);
336 app_msgarg (&msgs
, cp
);
341 SIGNAL (SIGQUIT
, quitser
);
342 SIGNAL (SIGPIPE
, pipeser
);
345 * Read the standard profile setup
347 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
348 readconfig(NULL
, fp
, cp
, 0);
352 suppress_bogus_mp_content_warning
= skip_mp_cte_check
= true;
353 suppress_extraneous_trailing_semicolon_warning
= true;
355 if (! context_find ("path")) {
356 free (path ("./", TFOLDER
));
359 if (file
&& msgs
.size
) {
360 die("cannot specify msg and file at same time!");
364 /* Open the outfile now, so we don't have to risk opening it
365 after running out of fds. */
366 if (strcmp (outfile
, "-") == 0) {
368 } else if ((outfp
= fopen (outfile
, "w")) == NULL
) {
369 adios (outfile
, "unable to open for writing");
374 * check if message is coming from file
377 /* If file is stdin, create a tmp file name before parse_mime()
378 has a chance, because it might put in on a different
379 filesystem than the output file. Instead, put it in the
380 user's preferred tmp directory. */
383 if (! strcmp ("-", file
)) {
389 if ((cp
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
390 die("unable to create temporary file in %s",
394 file
= mh_xstrdup (cp
);
395 cpydata (STDIN_FILENO
, fd
, "-", file
);
399 (void) m_unlink (file
);
400 die("failed to write temporary file");
404 cts
= mh_xcalloc(2, sizeof *cts
);
407 if ((ct
= parse_mime (file
))) {
408 set_text_ctparams(ct
, fx
.decodetypes
, fx
.lf_line_endings
);
411 inform("unable to parse message from file %s", file
);
414 /* If there's an outfile, pass the input message unchanged, so the
415 message won't get dropped from a pipeline. */
417 /* Something went wrong. Output might be expected, such as if
418 this were run as a filter. Just copy the input to the
420 if ((infp
= fopen (file
, "r")) == NULL
) {
421 adios (file
, "unable to open for reading");
424 if (copy_input_to_output (file
, infp
, outfile
, outfp
) != OK
) {
425 inform("unable to copy message to %s, "
426 "it might be lost\n", outfile
);
435 * message(s) are coming from a folder
440 app_msgarg(&msgs
, "cur");
443 folder
= getfolder (1);
445 maildir
= mh_xstrdup(m_maildir (folder
));
447 /* chdir so that error messages, esp. from MIME parser, just
448 refer to the message and not its path. */
449 if (chdir (maildir
) == NOTOK
) {
450 adios (maildir
, "unable to change directory to");
453 /* read folder and create message structure */
454 if (! (mp
= folder_read (folder
, 1))) {
455 die("unable to read folder %s", folder
);
458 /* check for empty folder */
459 if (mp
->nummsg
== 0) {
460 die("no messages in %s", folder
);
463 /* parse all the message ranges/sequences and set SELECTED */
464 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
465 if (! m_convert (mp
, msgs
.msgs
[msgnum
])) {
468 seq_setprev (mp
); /* set the previous-sequence */
470 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
473 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
474 if (is_selected(mp
, msgnum
)) {
475 char *msgnam
= m_name (msgnum
);
477 if ((ct
= parse_mime (msgnam
))) {
478 set_text_ctparams(ct
, fx
.decodetypes
, fx
.lf_line_endings
);
481 inform("unable to parse message %s", msgnam
);
484 /* If there's an outfile, pass the input message
485 unchanged, so the message won't get dropped from a
488 /* Something went wrong. Output might be expected,
489 such as if this were run as a filter. Just copy
490 the input to the output. */
491 /* Can't use path() here because 1) it might have been
492 called before and it caches the pwd, and 2) we call
493 chdir() after that. */
494 char *input_filename
=
495 concat (maildir
, "/", msgnam
, NULL
);
497 if ((infp
= fopen (input_filename
, "r")) == NULL
) {
498 adios (input_filename
,
499 "unable to open for reading");
502 if (copy_input_to_output (input_filename
, infp
,
503 outfile
, outfp
) != OK
) {
504 inform("unable to copy message to %s, "
505 "it might be lost\n", outfile
);
510 free (input_filename
);
517 seq_setcur (mp
, mp
->hghsel
); /* update current message */
519 seq_save (mp
); /* synchronize sequences */
520 context_replace (pfolder
, folder
);/* update current folder */
521 context_save (); /* save the context file */
525 for (ctp
= cts
; *ctp
; ++ctp
) {
527 mhfixmsgsbr (ctp
, maildir
, &fx
, &infp
, outfile
, &outfp
) == OK
533 (void) m_unlink (file
);
536 /* Just calling m_backup() unlinks the backup file. */
537 (void) m_backup (file
);
548 if (fx
.fixtypes
!= NULL
) { svector_free (fx
.fixtypes
); }
549 if (infp
) { fclose (infp
); } /* even if stdin */
550 if (outfp
) { fclose (outfp
); } /* even if stdout */
556 done (status
== OK
? 0 : 1);
562 * Apply transformations to one message.
565 mhfixmsgsbr (CT
*ctp
, char *maildir
, const fix_transformations
*fx
,
566 FILE **infp
, char *outfile
, FILE **outfp
)
568 /* Store input filename in case one of the transformations, i.e.,
569 fix_boundary(), rewrites to a tmp file. */
570 char *input_filename
= maildir
571 ? concat (maildir
, "/", (*ctp
)->c_file
, NULL
)
572 : mh_xstrdup ((*ctp
)->c_file
);
573 bool modify_inplace
= false;
574 int message_mods
= 0;
577 /* Though the input file won't need to be opened if everything goes
578 well, do it here just in case there's a failure, and that failure is
579 running out of file descriptors. */
580 if ((*infp
= fopen (input_filename
, "r")) == NULL
) {
581 adios (input_filename
, "unable to open for reading");
584 if (outfile
== NULL
) {
585 modify_inplace
= true;
587 if ((*ctp
)->c_file
) {
589 /* outfp will be closed by the caller */
590 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, outfp
)) ==
592 die("unable to create temporary file in %s",
595 outfile
= mh_xstrdup (tempfile
);
597 die("missing both input and output filenames\n");
599 } /* else *outfp was defined by caller */
601 reverse_alternative_parts (*ctp
);
602 status
= fix_always (*ctp
, &message_mods
);
603 if (status
== OK
&& fx
->fixboundary
) {
604 status
= fix_boundary (ctp
, &message_mods
);
606 if (status
== OK
&& fx
->fixtypes
!= NULL
) {
607 status
= fix_types (*ctp
, fx
->fixtypes
, &message_mods
);
609 if (status
== OK
&& fx
->fixcompositecte
) {
610 status
= fix_composite_cte (*ctp
, &message_mods
);
612 if (status
== OK
&& fx
->reformat
) {
614 ensure_text_plain (ctp
, NULL
, &message_mods
, fx
->replacetextplain
);
616 if (status
== OK
&& fx
->decodetext
) {
617 status
= decode_text_parts (*ctp
, fx
->decodetext
, fx
->decodetypes
,
621 if (status
== OK
&& fx
->decodeheaderfieldbodies
) {
622 status
= decode_header_field_bodies(*ctp
, &message_mods
);
624 if (status
== OK
&& fx
->textcharset
!= NULL
) {
625 status
= convert_charsets (*ctp
, fx
->textcharset
, &message_mods
);
628 if (status
== OK
&& ! (*ctp
)->c_umask
) {
629 /* Set the umask for the contents file. This currently
630 isn't used but just in case it is in the future. */
633 if (stat ((*ctp
)->c_file
, &st
) != NOTOK
) {
634 (*ctp
)->c_umask
= ~(st
.st_mode
& 0777);
636 (*ctp
)->c_umask
= ~m_gmprot();
641 * Write the content to a file
644 status
= write_content (*ctp
, input_filename
, outfile
, *outfp
,
645 modify_inplace
, message_mods
);
646 } else if (! modify_inplace
) {
647 /* Something went wrong. Output might be expected, such
648 as if this were run as a filter. Just copy the input
650 if (copy_input_to_output (input_filename
, *infp
, outfile
,
652 inform("unable to copy message to %s, it might be lost\n",
657 if (modify_inplace
) {
658 if (status
!= OK
) { (void) m_unlink (outfile
); }
665 free (input_filename
);
672 * Copy input message to output. Assumes not modifying in place, so this
673 * might be running as part of a pipeline.
676 copy_input_to_output (const char *input_filename
, FILE *infp
,
677 const char *output_filename
, FILE *outfp
)
679 int in
= fileno (infp
);
680 int out
= fileno (outfp
);
683 if (in
!= -1 && out
!= -1) {
684 cpydata (in
, out
, input_filename
, output_filename
);
694 * Fix mismatched outer level boundary.
697 fix_boundary (CT
*ct
, int *message_mods
)
699 struct multipart
*mp
;
702 if (ct
&& (*ct
)->c_type
== CT_MULTIPART
&& bogus_mp_content
) {
703 mp
= (struct multipart
*) (*ct
)->c_ctparams
;
706 * 1) Get boundary at end of part.
707 * 2) Get boundary at beginning of part and compare to the end-of-part
709 * 3) Write out contents of ct to tmp file, replacing boundary in
710 * header with boundary from part. Set c_unlink to 1.
712 * 5) Call parse_mime() on the tmp file, replacing ct.
715 if (mp
&& mp
->mp_start
) {
718 if (get_multipart_boundary (*ct
, &part_boundary
) == OK
) {
721 if ((fixed
= m_mktemp2 (NULL
, invo_name
, NULL
, &(*ct
)->c_fp
))) {
722 if (replace_boundary (*ct
, fixed
, part_boundary
) == OK
) {
723 char *filename
= mh_xstrdup ((*ct
)->c_file
);
727 if ((fixed_ct
= parse_mime (fixed
))) {
733 report (NULL
, NULL
, filename
,
734 "fix multipart boundary");
738 inform("unable to parse fixed part");
743 inform("unable to replace broken boundary");
747 inform("unable to create temporary file in %s",
752 free (part_boundary
);
754 /* Couldn't fix the boundary. Report failure so that mhfixmsg
755 doesn't modify the message. */
759 /* No multipart struct, even though the content type is
760 CT_MULTIPART. Report failure so that mhfixmsg doesn't modify
771 * Find boundary at end of multipart.
774 get_multipart_boundary (CT ct
, char **part_boundary
)
776 char buffer
[NMH_BUFSIZ
];
777 char *end_boundary
= NULL
;
778 off_t begin
= (off_t
) ct
->c_end
> (off_t
) (ct
->c_begin
+ sizeof buffer
)
779 ? (off_t
) (ct
->c_end
- sizeof buffer
)
780 : (off_t
) ct
->c_begin
;
784 /* This will fail if the boundary spans fread() calls. NMH_BUFSIZ should
785 be big enough, even if it's just 1024, to make that unlikely. */
787 /* free_content() will close ct->c_fp if bogus MP boundary is fixed. */
788 if (! ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
789 advise (ct
->c_file
, "unable to open for reading");
793 /* Get boundary at end of multipart. */
794 while (begin
>= (off_t
) ct
->c_begin
) {
795 fseeko (ct
->c_fp
, begin
, SEEK_SET
);
796 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
797 char *cp
= rfind_str (buffer
, bytes_read
, "--");
802 /* Trim off trailing "--" and anything beyond. */
804 if ((end
= rfind_str (buffer
, cp
- buffer
, "\n"))) {
805 if (strlen (end
) > 3 && *end
++ == '\n' &&
806 *end
++ == '-' && *end
++ == '-') {
807 end_boundary
= mh_xstrdup (end
);
814 if (end_boundary
|| begin
<= (off_t
) (ct
->c_begin
+ sizeof buffer
))
816 begin
-= sizeof buffer
;
819 /* Get boundary at beginning of multipart. */
821 fseeko (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
822 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
823 if (bytes_read
>= strlen (end_boundary
)) {
824 char *cp
= find_str (buffer
, bytes_read
, end_boundary
);
826 if (cp
&& cp
- buffer
>= 2 && *--cp
== '-' &&
827 *--cp
== '-' && (cp
> buffer
&& *--cp
== '\n')) {
832 /* The start and end boundaries didn't match, or the
833 start boundary doesn't begin with "\n--" (or "--"
834 if at the beginning of buffer). Keep trying. */
848 *part_boundary
= end_boundary
;
850 *part_boundary
= NULL
;
859 * Open and copy ct->c_file to file, replacing the multipart boundary.
862 replace_boundary (CT ct
, char *file
, char *boundary
)
866 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
868 m_getfld_state_t gstate
;
871 if (ct
->c_file
== NULL
) {
872 inform("missing input filename");
876 if ((fpin
= fopen (ct
->c_file
, "r")) == NULL
) {
877 advise (ct
->c_file
, "unable to open for reading");
881 if ((fpout
= fopen (file
, "w")) == NULL
) {
883 advise (file
, "unable to open for writing");
887 gstate
= m_getfld_state_init(fpin
);
888 for (compnum
= 1;;) {
889 int bufsz
= (int) sizeof buf
;
891 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
896 /* get copies of the buffers */
897 np
= mh_xstrdup (name
);
898 vp
= mh_xstrdup (buf
);
900 /* if necessary, get rest of field */
901 while (state
== FLDPLUS
) {
903 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
904 vp
= add (buf
, vp
); /* add to previous value */
907 if (strcasecmp (TYPE_FIELD
, np
)) {
908 fprintf (fpout
, "%s:%s", np
, vp
);
910 char *new_ctline
, *new_params
;
912 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
913 &ct
->c_ctinfo
.ci_last_pm
, "boundary",
916 new_ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
917 ct
->c_ctinfo
.ci_subtype
, NULL
);
918 new_params
= output_params(LEN(TYPE_FIELD
) +
919 strlen(new_ctline
) + 1,
920 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
921 fprintf (fpout
, "%s:%s%s\n", np
, new_ctline
,
922 FENDNULL(new_params
));
934 /* buf will have a terminating NULL, skip it. */
935 if ((int) fwrite (buf
, 1, bufsz
-1, fpout
) < bufsz
-1) {
936 advise (file
, "fwrite");
945 inform("message format error in component #%d", compnum
);
950 inform("getfld() returned %d", state
);
958 m_getfld_state_destroy (&gstate
);
967 * Fix Content-Type header to reflect the content of its part.
970 fix_types (CT ct
, svector_t fixtypes
, int *message_mods
)
974 switch (ct
->c_type
) {
976 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
979 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
980 status
= fix_types (part
->mp_part
, fixtypes
, message_mods
);
986 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
987 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
989 status
= fix_types (e
->eb_content
, fixtypes
, message_mods
);
996 if (ct
->c_ctinfo
.ci_type
&& ct
->c_ctinfo
.ci_subtype
) {
997 for (typep
= svector_strs (fixtypes
);
998 typep
&& (type
= *typep
);
1000 char *type_subtype
=
1001 concat (ct
->c_ctinfo
.ci_type
, "/", ct
->c_ctinfo
.ci_subtype
,
1004 if (! strcasecmp (type
, type_subtype
) &&
1005 decode_part (ct
) == OK
&&
1006 ct
->c_cefile
.ce_file
!= NULL
) {
1007 char *ct_type_subtype
= mime_type (ct
->c_cefile
.ce_file
);
1010 if ((cp
= strchr (ct_type_subtype
, ';'))) {
1011 /* Truncate to remove any parameter list from
1012 mime_type () result. */
1016 if (strcasecmp (type
, ct_type_subtype
)) {
1017 char *ct_type
, *ct_subtype
;
1020 /* The Content-Type header does not match the
1021 content, so update these struct Content
1024 * c_ctinfo.ci_type, c_ctinfo.ci_subtype
1027 /* Extract type and subtype from type/subtype. */
1028 ct_type
= mh_xstrdup(ct_type_subtype
);
1029 if ((cp
= strchr (ct_type
, '/'))) {
1031 ct_subtype
= mh_xstrdup(++cp
);
1033 inform("missing / in MIME type of %s %s",
1034 ct
->c_file
, ct
->c_partno
);
1039 ct
->c_type
= ct_str_type (ct_type
);
1040 ct
->c_subtype
= ct_str_subtype (ct
->c_type
, ct_subtype
);
1042 free (ct
->c_ctinfo
.ci_type
);
1043 ct
->c_ctinfo
.ci_type
= ct_type
;
1044 free (ct
->c_ctinfo
.ci_subtype
);
1045 ct
->c_ctinfo
.ci_subtype
= ct_subtype
;
1046 if (! replace_substring (&ct
->c_ctline
, type
,
1048 inform("did not find %s in %s",
1049 type
, ct
->c_ctline
);
1052 /* Update Content-Type header field. */
1053 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1054 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1055 if (replace_substring (&hf
->value
, type
,
1059 report (NULL
, ct
->c_partno
, ct
->c_file
,
1060 "change Content-Type in header "
1062 type
, ct_type_subtype
);
1066 inform("did not find %s in %s", type
, hf
->value
);
1070 free (ct_type_subtype
);
1072 free (type_subtype
);
1082 * Replace a substring, allocating space to hold the new one.
1085 replace_substring (char **str
, const char *old
, const char *new)
1089 if ((cp
= strstr (*str
, old
))) {
1090 char *remainder
= cp
+ strlen (old
);
1091 char *prefix
, *new_str
;
1094 prefix
= mh_xstrdup(*str
);
1095 *(prefix
+ (cp
- *str
)) = '\0';
1096 new_str
= concat (prefix
, new, remainder
, NULL
);
1099 new_str
= concat (new, remainder
, NULL
);
1104 return *str
= new_str
;
1112 * Remove a name=value parameter, given just its name, from a header value.
1115 remove_parameter (char *str
, const char *name
)
1117 /* It looks to me, based on the BNF in RFC 2045, than there can't
1118 be whitespace between the parameter name and the "=", or
1119 between the "=" and the parameter value. */
1120 char *param_name
= concat (name
, "=", NULL
);
1123 if ((cp
= strstr (str
, param_name
))) {
1127 /* Remove any leading spaces, before the parameter name. */
1129 start
> str
&& isspace ((unsigned char) *(start
-1));
1133 /* Remove a leading semicolon. */
1134 if (start
> str
&& *(start
-1) == ';') { --start
; }
1136 end
= cp
+ strlen (name
) + 1;
1138 /* Skip past the quoted value, and then the final quote. */
1139 for (++end
; *end
&& *end
!= '"'; ++end
) { continue; }
1142 /* Skip past the value. */
1143 for (++end
; *end
&& ! isspace ((unsigned char) *end
); ++end
) {}
1146 /* Count how many characters need to be moved. Include
1147 trailing null, which is accounted for by the
1148 initialization of count to 1. */
1149 for (cp
= end
; *cp
; ++cp
) { ++count
; }
1150 (void) memmove (start
, end
, count
);
1160 * Fix Content-Transfer-Encoding of composite,, e.g., message or multipart, part.
1161 * According to RFC 2045 Sec. 6.4, it must be 7bit, 8bit, or binary. Set it to
1165 fix_composite_cte (CT ct
, int *message_mods
)
1169 if (ct
->c_type
== CT_MESSAGE
|| ct
->c_type
== CT_MULTIPART
) {
1170 if (ct
->c_encoding
!= CE_7BIT
&& ct
->c_encoding
!= CE_8BIT
&&
1171 ct
->c_encoding
!= CE_BINARY
) {
1174 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1175 char *name
= hf
->name
;
1176 for (; isspace((unsigned char)*name
); ++name
) {
1180 if (! strncasecmp (name
, ENCODING_FIELD
,
1181 LEN(ENCODING_FIELD
))) {
1182 char *prefix
= "Nmh-REPLACED-INVALID-";
1186 h
->name
= mh_xstrdup (hf
->name
);
1190 /* Retain old header but prefix its name. */
1192 hf
->name
= concat (prefix
, h
->name
, NULL
);
1196 char *encoding
= cpytrim (hf
->value
);
1197 report (NULL
, ct
->c_partno
, ct
->c_file
,
1198 "replace Content-Transfer-Encoding of %s "
1199 "with 8 bit", encoding
);
1203 h
->value
= mh_xstrdup (" 8bit\n");
1205 /* Don't need to warn for multiple C-T-E header
1206 fields, parse_mime() already does that. But
1207 if there are any, fix them all as necessary. */
1212 set_ce (ct
, CE_8BIT
);
1215 if (ct
->c_type
== CT_MULTIPART
) {
1216 struct multipart
*m
;
1219 m
= (struct multipart
*) ct
->c_ctparams
;
1220 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1221 if (fix_composite_cte (part
->mp_part
, message_mods
) != OK
) {
1234 * Set content encoding.
1237 set_ce (CT ct
, int encoding
)
1239 const char *ce
= ce_str (encoding
);
1240 const struct str2init
*ctinit
= get_ce_method (ce
);
1243 char *cte
= concat (" ", ce
, "\n", NULL
);
1244 bool found_cte
= false;
1246 /* Decoded contents might be in ct->c_cefile.ce_file, if the
1247 caller is decode_text_parts (). Save because we'll
1249 struct cefile decoded_content_info
= ct
->c_cefile
;
1251 ct
->c_encoding
= encoding
;
1253 ct
->c_ctinitfnx
= ctinit
->si_init
;
1254 /* This will assign ct->c_cefile with an all-0 struct, which
1256 (*ctinit
->si_init
) (ct
);
1257 /* After returning, the caller should set
1258 ct->c_cefile.ce_file to the name of the file containing
1261 if (ct
->c_ceclosefnx
) {
1262 (*ct
->c_ceclosefnx
) (ct
);
1265 /* Restore the cefile. */
1266 ct
->c_cefile
= decoded_content_info
;
1268 /* Update/add Content-Transfer-Encoding header field. */
1269 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1270 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
1277 add_header (ct
, mh_xstrdup (ENCODING_FIELD
), cte
);
1280 /* Update c_celine. It's used only by mhlist -debug. */
1281 free (ct
->c_celine
);
1282 ct
->c_celine
= mh_xstrdup (cte
);
1292 * Make sure each text part has a corresponding text/plain part.
1295 ensure_text_plain (CT
*ct
, CT parent
, int *message_mods
, int replacetextplain
)
1299 switch ((*ct
)->c_type
) {
1301 /* Nothing to do for text/plain. */
1302 if ((*ct
)->c_subtype
== TEXT_PLAIN
) { return OK
; }
1304 if (parent
&& parent
->c_type
== CT_MULTIPART
&&
1305 parent
->c_subtype
== MULTI_ALTERNATE
) {
1306 int new_subpart_number
= 1;
1307 int has_text_plain
=
1308 find_textplain_sibling (parent
, replacetextplain
,
1309 &new_subpart_number
);
1311 if (! has_text_plain
) {
1312 /* Parent is a multipart/alternative. Insert a new
1313 text/plain subpart. */
1314 const int inserted
=
1315 insert_new_text_plain_part (*ct
, new_subpart_number
,
1320 report (NULL
, parent
->c_partno
, parent
->c_file
,
1321 "insert text/plain part");
1327 } else if (parent
&& parent
->c_type
== CT_MULTIPART
&&
1328 parent
->c_subtype
== MULTI_RELATED
) {
1329 char *type_subtype
=
1330 concat ((*ct
)->c_ctinfo
.ci_type
, "/",
1331 (*ct
)->c_ctinfo
.ci_subtype
, NULL
);
1332 const char *parent_type
=
1333 get_param (parent
->c_ctinfo
.ci_first_pm
, "type", '?', 1);
1334 int new_subpart_number
= 1;
1335 int has_text_plain
= 0;
1337 /* Have to do string comparison on the subtype because we
1338 don't enumerate all of them in c_subtype values.
1339 parent_type will be NULL if the multipart/related part
1340 doesn't have a type parameter. The type parameter must
1341 be specified according to RFC 2387 Sec. 3.1 but not all
1343 if (parent_type
&& strcasecmp (type_subtype
, parent_type
) == 0) {
1344 /* The type of this part matches the root type of the
1345 parent multipart/related. Look to see if there's
1346 text/plain sibling. */
1348 find_textplain_sibling (parent
, replacetextplain
,
1349 &new_subpart_number
);
1352 free (type_subtype
);
1354 if (! has_text_plain
) {
1355 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1359 for (part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
1360 if (*ct
!= part
->mp_part
) {
1366 /* Parent is a multipart/related. Insert a new
1367 text/plain subpart in a new multipart/alternative. */
1368 if (insert_into_new_mp_alt (ct
, message_mods
)) {
1369 /* Not an error if text/plain couldn't be added. */
1372 /* There are no siblings, so insert a new text/plain
1373 subpart, and change the parent type from
1374 multipart/related to multipart/alternative. */
1375 const int inserted
=
1376 insert_new_text_plain_part (*ct
, new_subpart_number
,
1382 parent
->c_subtype
= MULTI_ALTERNATE
;
1383 free (parent
->c_ctinfo
.ci_subtype
);
1384 parent
->c_ctinfo
.ci_subtype
= mh_xstrdup("alternative");
1385 if (! replace_substring (&parent
->c_ctline
, "/related",
1387 inform("did not find multipart/related in %s",
1391 /* Update Content-Type header field. */
1392 for (hf
= parent
->c_first_hf
; hf
; hf
= hf
->next
) {
1393 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1394 if (replace_substring (&hf
->value
, "/related",
1398 report (NULL
, parent
->c_partno
,
1400 "insert text/plain part");
1403 /* Remove, e.g., type="text/html" from
1404 multipart/alternative. */
1405 remove_parameter (hf
->value
, "type");
1408 inform("did not find multipart/"
1409 "related in header %s", hf
->value
);
1413 /* Not an error if text/plain couldn't be inserted. */
1418 if (insert_into_new_mp_alt (ct
, message_mods
)) {
1425 case CT_MULTIPART
: {
1426 struct multipart
*mp
= (struct multipart
*) (*ct
)->c_ctparams
;
1429 for (part
= mp
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1430 if ((*ct
)->c_type
== CT_MULTIPART
) {
1431 status
= ensure_text_plain (&part
->mp_part
, *ct
, message_mods
,
1439 if ((*ct
)->c_subtype
== MESSAGE_EXTERNAL
) {
1440 struct exbody
*e
= (struct exbody
*) (*ct
)->c_ctparams
;
1442 status
= ensure_text_plain (&e
->eb_content
, *ct
, message_mods
,
1453 * See if there is a sibling text/plain, and return its subpart number.
1456 find_textplain_sibling (CT parent
, int replacetextplain
,
1457 int *new_subpart_number
)
1459 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1460 struct part
*part
, *prev
;
1461 bool has_text_plain
= false;
1463 for (prev
= part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
1464 ++*new_subpart_number
;
1465 if (part
->mp_part
->c_type
== CT_TEXT
&&
1466 part
->mp_part
->c_subtype
== TEXT_PLAIN
) {
1467 if (replacetextplain
) {
1468 struct part
*old_part
;
1469 if (part
== mp
->mp_parts
) {
1470 old_part
= mp
->mp_parts
;
1471 mp
->mp_parts
= part
->mp_next
;
1473 old_part
= prev
->mp_next
;
1474 prev
->mp_next
= part
->mp_next
;
1477 report (NULL
, parent
->c_partno
, parent
->c_file
,
1478 "remove text/plain part %s",
1479 old_part
->mp_part
->c_partno
);
1481 free_content (old_part
->mp_part
);
1484 has_text_plain
= true;
1491 return has_text_plain
;
1496 * Insert a new text/plain part.
1499 insert_new_text_plain_part (CT ct
, int new_subpart_number
, CT parent
)
1501 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1502 struct part
*new_part
;
1505 if ((new_part
->mp_part
= build_text_plain_part (ct
))) {
1507 snprintf (buffer
, sizeof buffer
, "%d", new_subpart_number
);
1509 new_part
->mp_next
= mp
->mp_parts
;
1510 mp
->mp_parts
= new_part
;
1511 new_part
->mp_part
->c_partno
=
1512 concat (parent
->c_partno
? parent
->c_partno
: "1", ".",
1518 free_content (new_part
->mp_part
);
1526 * Create a text/plain part to go along with non-plain sibling part.
1529 build_text_plain_part (CT encoded_part
)
1531 CT tp_part
= divide_part (encoded_part
);
1532 char *tmp_plain_file
= NULL
;
1534 if (decode_part (tp_part
) == OK
) {
1535 /* Now, tp_part->c_cefile.ce_file is the name of the tmp file that
1536 contains the decoded contents. And the decoding function, such
1537 as openQuoted, will have set ...->ce_unlink to 1 so that it will
1538 be unlinked by free_content (). */
1541 /* This m_mktemp2() call closes the temp file. */
1542 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1543 inform("unable to create temporary file in %s",
1546 tmp_plain_file
= mh_xstrdup (tempfile
);
1547 if (reformat_part (tp_part
, tmp_plain_file
,
1548 tp_part
->c_ctinfo
.ci_type
,
1549 tp_part
->c_ctinfo
.ci_subtype
,
1550 tp_part
->c_type
) == OK
) {
1556 free_content (tp_part
);
1557 if (tmp_plain_file
) { (void) m_unlink (tmp_plain_file
); }
1558 free (tmp_plain_file
);
1565 * Slip new text/plain part into a new multipart/alternative.
1568 insert_into_new_mp_alt (CT
*ct
, int *message_mods
)
1570 CT tp_part
= build_text_plain_part (*ct
);
1574 CT mp_alt
= build_multipart_alt (*ct
, tp_part
, CT_MULTIPART
,
1577 struct multipart
*mp
= (struct multipart
*) mp_alt
->c_ctparams
;
1579 if (mp
&& mp
->mp_parts
) {
1580 mp
->mp_parts
->mp_part
= tp_part
;
1581 /* Make the new multipart/alternative the parent. */
1586 report (NULL
, (*ct
)->c_partno
, (*ct
)->c_file
,
1587 "insert text/plain part");
1590 free_content (tp_part
);
1591 free_content (mp_alt
);
1598 /* Not an error if text/plain couldn't be built. */
1606 * Clone a MIME part.
1614 /* Just copy over what is needed for decoding. c_vrsn and
1615 c_celine aren't necessary. */
1616 new_part
->c_file
= mh_xstrdup (ct
->c_file
);
1617 new_part
->c_begin
= ct
->c_begin
;
1618 new_part
->c_end
= ct
->c_end
;
1619 copy_ctinfo (&new_part
->c_ctinfo
, &ct
->c_ctinfo
);
1620 new_part
->c_type
= ct
->c_type
;
1621 new_part
->c_cefile
= ct
->c_cefile
;
1622 new_part
->c_encoding
= ct
->c_encoding
;
1623 new_part
->c_ctinitfnx
= ct
->c_ctinitfnx
;
1624 new_part
->c_ceopenfnx
= ct
->c_ceopenfnx
;
1625 new_part
->c_ceclosefnx
= ct
->c_ceclosefnx
;
1626 new_part
->c_cesizefnx
= ct
->c_cesizefnx
;
1628 /* c_ctline is used by reformat__part(), so it can preserve
1629 anything after the type/subtype. */
1630 new_part
->c_ctline
= mh_xstrdup (ct
->c_ctline
);
1637 * Copy the content info from one part to another.
1640 copy_ctinfo (CI dest
, CI src
)
1644 dest
->ci_type
= src
->ci_type
? mh_xstrdup (src
->ci_type
) : NULL
;
1645 dest
->ci_subtype
= src
->ci_subtype
? mh_xstrdup (src
->ci_subtype
) : NULL
;
1647 for (s_pm
= src
->ci_first_pm
; s_pm
; s_pm
= s_pm
->pm_next
) {
1648 d_pm
= add_param(&dest
->ci_first_pm
, &dest
->ci_last_pm
, s_pm
->pm_name
,
1650 if (s_pm
->pm_charset
) {
1651 d_pm
->pm_charset
= mh_xstrdup(s_pm
->pm_charset
);
1653 if (s_pm
->pm_lang
) {
1654 d_pm
->pm_lang
= mh_xstrdup(s_pm
->pm_lang
);
1658 dest
->ci_comment
= src
->ci_comment
? mh_xstrdup (src
->ci_comment
) : NULL
;
1659 dest
->ci_magic
= src
->ci_magic
? mh_xstrdup (src
->ci_magic
) : NULL
;
1674 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, &file
)) == NULL
) {
1675 die("unable to create temporary file in %s", get_temp_dir());
1677 tmp_decoded
= mh_xstrdup (tempfile
);
1678 /* The following call will load ct->c_cefile.ce_file with the tmp
1679 filename of the decoded content. tmp_decoded will contain the
1680 encoded output, get rid of that. */
1681 status
= output_message_fp (ct
, file
, tmp_decoded
);
1682 (void) m_unlink (tmp_decoded
);
1684 if (fclose (file
)) {
1685 inform("unable to close temporary file %s, continuing...", tempfile
);
1693 * Reformat content as plain text.
1694 * Some of the arguments aren't really needed now, but maybe will
1695 * be in the future for other than text types.
1698 reformat_part (CT ct
, char *file
, char *type
, char *subtype
, int c_type
)
1700 int output_subtype
, output_encoding
;
1701 const char *reason
= NULL
;
1705 /* Hacky: this redirects the output from whatever command is used
1706 to show the part to a file. So, the user can't have any output
1707 redirection in that command.
1708 Could show_multi() in mhshowsbr.c avoid this? */
1710 /* Check for invo_name-format-type/subtype. */
1711 if ((cf
= context_find_by_type ("format", type
, subtype
)) == NULL
) {
1713 inform("Don't know how to convert %s, there is no "
1714 "%s-format-%s/%s profile entry",
1715 ct
->c_file
, invo_name
, type
, subtype
);
1719 if (strchr (cf
, '>')) {
1720 inform("'>' prohibited in \"%s\",\nplease fix your "
1721 "%s-format-%s/%s profile entry", cf
, invo_name
, type
,
1727 cp
= concat (cf
, " >", file
, NULL
);
1728 status
= show_content_aux (ct
, 0, cp
, NULL
, NULL
);
1731 /* Unlink decoded content tmp file and free its filename to avoid
1732 leaks. The file stream should already have been closed. */
1733 if (ct
->c_cefile
.ce_unlink
) {
1734 (void) m_unlink (ct
->c_cefile
.ce_file
);
1735 free (ct
->c_cefile
.ce_file
);
1736 ct
->c_cefile
.ce_file
= NULL
;
1737 ct
->c_cefile
.ce_unlink
= 0;
1740 if (c_type
== CT_TEXT
) {
1741 output_subtype
= TEXT_PLAIN
;
1743 /* Set subtype to 0, which is always an UNKNOWN subtype. */
1747 output_encoding
= content_encoding (ct
, &reason
);
1749 set_ct_type (ct
, c_type
, output_subtype
, output_encoding
) == OK
) {
1750 ct
->c_cefile
.ce_file
= file
;
1751 ct
->c_cefile
.ce_unlink
= 1;
1753 ct
->c_cefile
.ce_unlink
= 0;
1762 * Fill in a multipart/alternative part.
1765 build_multipart_alt (CT first_alt
, CT new_part
, int type
, int subtype
)
1767 char *boundary_prefix
= "----=_nmh-multipart";
1768 char *boundary
= concat (boundary_prefix
, first_alt
->c_partno
, NULL
);
1769 char *boundary_indicator
= "; boundary=";
1770 char *typename
, *subtypename
, *name
;
1773 struct multipart
*m
;
1774 const struct str2init
*ctinit
;
1778 /* Set up the multipart/alternative part. These fields of *ct were
1779 initialized to 0 by mh_xcalloc():
1780 c_fp, c_unlink, c_begin, c_end,
1781 c_vrsn, c_ctline, c_celine,
1782 c_id, c_descr, c_dispo, c_partno,
1783 c_ctinfo.ci_comment, c_ctinfo.ci_magic,
1784 c_cefile, c_encoding,
1785 c_digested, c_digest[16], c_ctexbody,
1786 c_ctinitfnx, c_ceopenfnx, c_ceclosefnx, c_cesizefnx,
1788 c_showproc, c_termproc, c_storeproc, c_storage, c_folder
1791 ct
->c_file
= mh_xstrdup (first_alt
->c_file
);
1793 ct
->c_subtype
= subtype
;
1795 ctinit
= get_ct_init (ct
->c_type
);
1797 typename
= ct_type_str (type
);
1798 subtypename
= ct_subtype_str (type
, subtype
);
1802 int found_boundary
= 1;
1804 while (found_boundary
&& serial
< 1000000) {
1807 /* Ensure that the boundary doesn't appear in the decoded
1809 if (new_part
->c_cefile
.ce_file
) {
1810 if ((found_boundary
=
1811 boundary_in_content (&new_part
->c_cefile
.ce_fp
,
1812 new_part
->c_cefile
.ce_file
,
1813 boundary
)) == NOTOK
) {
1818 /* Ensure that the boundary doesn't appear in the encoded
1820 if (! found_boundary
&& new_part
->c_file
) {
1821 if ((found_boundary
=
1822 boundary_in_content (&new_part
->c_fp
,
1824 boundary
)) == NOTOK
) {
1829 if (found_boundary
) {
1830 /* Try a slightly different boundary. */
1835 snprintf (buffer2
, sizeof buffer2
, "%d", serial
);
1837 concat (boundary_prefix
,
1838 FENDNULL(first_alt
->c_partno
),
1839 "-", buffer2
, NULL
);
1843 if (found_boundary
) {
1844 inform("giving up trying to find a unique boundary");
1849 name
= concat (" ", typename
, "/", subtypename
, boundary_indicator
, "\"",
1850 boundary
, "\"", NULL
);
1852 /* Load c_first_hf and c_last_hf. */
1853 transfer_noncontent_headers (first_alt
, ct
);
1854 add_header (ct
, mh_xstrdup (TYPE_FIELD
), concat (name
, "\n", NULL
));
1857 /* Load c_partno. */
1858 if (first_alt
->c_partno
) {
1859 ct
->c_partno
= mh_xstrdup (first_alt
->c_partno
);
1860 free (first_alt
->c_partno
);
1861 first_alt
->c_partno
= concat (ct
->c_partno
, ".1", NULL
);
1862 new_part
->c_partno
= concat (ct
->c_partno
, ".2", NULL
);
1864 first_alt
->c_partno
= mh_xstrdup ("1");
1865 new_part
->c_partno
= mh_xstrdup ("2");
1869 ct
->c_ctinfo
.ci_type
= mh_xstrdup (typename
);
1870 ct
->c_ctinfo
.ci_subtype
= mh_xstrdup (subtypename
);
1873 add_param(&ct
->c_ctinfo
.ci_first_pm
, &ct
->c_ctinfo
.ci_last_pm
,
1874 "boundary", boundary
, 0);
1878 p
->mp_next
->mp_next
= NULL
;
1879 p
->mp_next
->mp_part
= first_alt
;
1882 m
->mp_start
= concat (boundary
, "\n", NULL
);
1883 m
->mp_stop
= concat (boundary
, "--\n", NULL
);
1899 * Check that the boundary does not appear in the content.
1902 boundary_in_content (FILE **fp
, char *file
, const char *boundary
)
1904 char buffer
[NMH_BUFSIZ
];
1906 bool found_boundary
= false;
1908 /* free_content() will close *fp if we fopen it here. */
1909 if (! *fp
&& (*fp
= fopen (file
, "r")) == NULL
) {
1910 advise (file
, "unable to open %s for reading", file
);
1914 fseeko (*fp
, 0L, SEEK_SET
);
1915 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) > 0) {
1916 if (find_str (buffer
, bytes_read
, boundary
)) {
1917 found_boundary
= true;
1922 return found_boundary
;
1927 * Remove all non-Content headers.
1930 transfer_noncontent_headers (CT old
, CT
new)
1934 hp_prev
= hp
= old
->c_first_hf
;
1938 if (strncasecmp (XXX_FIELD_PRF
, hp
->name
, LEN(XXX_FIELD_PRF
))) {
1939 if (hp
== old
->c_last_hf
) {
1940 if (hp
== old
->c_first_hf
) {
1941 old
->c_last_hf
= old
->c_first_hf
= NULL
;
1943 hp_prev
->next
= NULL
;
1944 old
->c_last_hf
= hp_prev
;
1947 if (hp
== old
->c_first_hf
) {
1948 old
->c_first_hf
= next
;
1950 hp_prev
->next
= next
;
1954 /* Put node hp in the new CT. */
1955 if (new->c_first_hf
== NULL
) {
1956 new->c_first_hf
= hp
;
1958 new->c_last_hf
->next
= hp
;
1960 new->c_last_hf
= hp
;
1962 /* A Content- header, leave in old. */
1975 set_ct_type (CT ct
, int type
, int subtype
, int encoding
)
1977 char *typename
= ct_type_str (type
);
1978 char *subtypename
= ct_subtype_str (type
, subtype
);
1979 /* E.g, " text/plain" */
1980 char *type_subtypename
= concat (" ", typename
, "/", subtypename
, NULL
);
1981 /* E.g, " text/plain\n" */
1982 char *name_plus_nl
= concat (type_subtypename
, "\n", NULL
);
1983 bool found_content_type
= false;
1985 const char *cp
= NULL
;
1989 /* Update/add Content-Type header field. */
1990 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1991 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1992 found_content_type
= true;
1994 hf
->value
= (cp
= strchr (ct
->c_ctline
, ';'))
1995 ? concat (type_subtypename
, cp
, "\n", NULL
)
1996 : mh_xstrdup (name_plus_nl
);
1999 if (! found_content_type
) {
2000 add_header (ct
, mh_xstrdup (TYPE_FIELD
),
2001 (cp
= strchr (ct
->c_ctline
, ';'))
2002 ? concat (type_subtypename
, cp
, "\n", NULL
)
2003 : mh_xstrdup (name_plus_nl
));
2006 /* Some of these might not be used, but set them anyway. */
2008 ? concat (type_subtypename
, cp
, NULL
)
2009 : concat (type_subtypename
, NULL
);
2010 free (ct
->c_ctline
);
2011 ct
->c_ctline
= ctline
;
2012 /* Leave other ctinfo members as they were. */
2013 free (ct
->c_ctinfo
.ci_type
);
2014 ct
->c_ctinfo
.ci_type
= mh_xstrdup (typename
);
2015 free (ct
->c_ctinfo
.ci_subtype
);
2016 ct
->c_ctinfo
.ci_subtype
= mh_xstrdup (subtypename
);
2018 ct
->c_subtype
= subtype
;
2020 free (name_plus_nl
);
2021 free (type_subtypename
);
2023 status
= set_ce (ct
, encoding
);
2030 * It's not necessary to update the charset parameter of a Content-Type
2031 * header for a text part. According to RFC 2045 Sec. 6.4, the body
2032 * (content) was originally in the specified charset, "and will be in
2033 * that character set again after decoding."
2036 decode_text_parts (CT ct
, int encoding
, const char *decodetypes
,
2040 int lf_line_endings
= 0;
2042 switch (ct
->c_type
) {
2043 case CT_MULTIPART
: {
2044 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2047 /* Should check to see if the body for this part is encoded?
2048 For now, it gets passed along as-is by InitMultiPart(). */
2049 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2050 status
= decode_text_parts (part
->mp_part
, encoding
, decodetypes
,
2057 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2058 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2060 status
= decode_text_parts (e
->eb_content
, encoding
, decodetypes
,
2066 if (! should_decode(decodetypes
, ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
)) {
2071 ct
->c_ctparams
&& ((struct text
*) ct
->c_ctparams
)->lf_line_endings
;
2073 switch (ct
->c_encoding
) {
2078 if (decode_part (ct
) == OK
&& ct
->c_cefile
.ce_file
) {
2079 const char *reason
= NULL
;
2081 if ((ct_encoding
= content_encoding (ct
, &reason
)) == CE_BINARY
2082 && encoding
!= CE_BINARY
) {
2083 /* The decoding isn't acceptable so discard it.
2084 Leave status as OK to allow other transformations. */
2086 report (NULL
, ct
->c_partno
, ct
->c_file
,
2087 "will not decode%s because it is binary (%s)",
2089 : (FENDNULL(ct
->c_ctline
)),
2092 (void) m_unlink (ct
->c_cefile
.ce_file
);
2093 free (ct
->c_cefile
.ce_file
);
2094 ct
->c_cefile
.ce_file
= NULL
;
2095 } else if (ct
->c_encoding
== CE_QUOTED
&&
2096 ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
2097 /* The decoding isn't acceptable so discard it.
2098 Leave status as OK to allow other transformations. */
2100 report (NULL
, ct
->c_partno
, ct
->c_file
,
2101 "will not decode%s because it is 8bit",
2103 : (FENDNULL(ct
->c_ctline
)));
2105 (void) m_unlink (ct
->c_cefile
.ce_file
);
2106 free (ct
->c_cefile
.ce_file
);
2107 ct
->c_cefile
.ce_file
= NULL
;
2111 if (ct_encoding
== CE_BINARY
) {
2113 } else if (ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
2118 if (set_ce (ct
, enc
) == OK
) {
2121 report (NULL
, ct
->c_partno
, ct
->c_file
, "decode%s",
2122 FENDNULL(ct
->c_ctline
));
2124 if (lf_line_endings
) {
2125 strip_crs (ct
, message_mods
);
2138 if (lf_line_endings
) {
2139 strip_crs (ct
, message_mods
);
2154 * Determine if the part with type[/subtype] should be decoded, according to
2155 * decodetypes (which came from the -decodetypes switch).
2158 should_decode(const char *decodetypes
, const char *type
, const char *subtype
)
2160 /* Quick search for matching type[/subtype] in decodetypes: bracket
2161 decodetypes with commas, then search for ,type, and ,type/subtype, in
2164 bool found_match
= false;
2165 char *delimited_decodetypes
= concat(",", decodetypes
, ",", NULL
);
2166 char *delimited_type
= concat(",", type
, ",", NULL
);
2168 if (nmh_strcasestr(delimited_decodetypes
, delimited_type
)) {
2170 } else if (subtype
!= NULL
) {
2171 char *delimited_type_subtype
=
2172 concat(",", type
, "/", subtype
, ",", NULL
);
2174 if (nmh_strcasestr(delimited_decodetypes
, delimited_type_subtype
)) {
2177 free(delimited_type_subtype
);
2180 free(delimited_type
);
2181 free(delimited_decodetypes
);
2188 * See if the decoded content is 7bit, 8bit, or binary. It's binary
2189 * if it has any NUL characters, a CR not followed by a LF, or lines
2190 * greater than 998 characters in length. If binary, reason is set
2191 * to a string explaining why.
2194 content_encoding (CT ct
, const char **reason
)
2196 CE ce
= &ct
->c_cefile
;
2197 int encoding
= CE_7BIT
;
2200 size_t line_len
= 0;
2201 char buffer
[NMH_BUFSIZ
];
2204 if (! ce
->ce_fp
&& (ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2205 advise (ce
->ce_file
, "unable to open for reading");
2209 fseeko (ce
->ce_fp
, 0L, SEEK_SET
);
2210 while (encoding
!= CE_BINARY
&&
2211 (inbytes
= fread (buffer
, 1, sizeof buffer
, ce
->ce_fp
)) > 0) {
2214 int last_char_was_cr
= 0;
2216 for (i
= 0, cp
= buffer
; i
< inbytes
; ++i
, ++cp
) {
2217 if (*cp
== '\0' || ++line_len
> 998 ||
2218 (*cp
!= '\n' && last_char_was_cr
)) {
2219 encoding
= CE_BINARY
;
2221 *reason
= "null character";
2222 } else if (line_len
> 998) {
2223 *reason
= "line length > 998";
2224 } else if (*cp
!= '\n' && last_char_was_cr
) {
2225 *reason
= "CR not followed by LF";
2227 /* Should not reach this. */
2234 } else if (! isascii ((unsigned char) *cp
)) {
2238 last_char_was_cr
= *cp
== '\r';
2244 } /* else should never happen */
2251 * Strip carriage returns from content.
2254 strip_crs (CT ct
, int *message_mods
)
2256 char *charset
= content_charset (ct
);
2259 /* Only strip carriage returns if content is ASCII or another
2260 charset that has the same readily recognizable CR followed by a
2261 LF. We can include UTF-8 here because if the high-order bit of
2262 a UTF-8 byte is 0, then it must be a single-byte ASCII
2264 if (! strcasecmp (charset
, "US-ASCII") ||
2265 ! strcasecmp (charset
, "UTF-8") ||
2266 ! strncasecmp (charset
, "ISO-8859-", 9) ||
2267 ! strncasecmp (charset
, "WINDOWS-12", 10)) {
2272 bool has_crs
= false;
2273 bool opened_input_file
= false;
2275 if (ct
->c_cefile
.ce_file
) {
2276 file
= &ct
->c_cefile
.ce_file
;
2277 fp
= &ct
->c_cefile
.ce_fp
;
2279 } else if (ct
->c_file
) {
2282 begin
= (size_t) ct
->c_begin
;
2283 end
= (size_t) ct
->c_end
;
2284 } /* else don't know where the content is */
2286 if (file
&& *file
&& fp
) {
2288 if ((*fp
= fopen (*file
, "r")) == NULL
) {
2289 advise (*file
, "unable to open for reading");
2292 opened_input_file
= true;
2298 char buffer
[NMH_BUFSIZ
];
2300 size_t bytes_to_read
=
2301 end
> 0 && end
> begin
? end
- begin
: sizeof buffer
;
2303 fseeko (*fp
, begin
, SEEK_SET
);
2304 while ((bytes_read
= fread (buffer
, 1,
2305 min (bytes_to_read
, sizeof buffer
),
2307 /* Look for CR followed by a LF. This is supposed to
2308 be text so there should be LF's. If not, don't
2309 modify the content. */
2312 bool last_char_was_cr
= false;
2314 if (end
> 0) { bytes_to_read
-= bytes_read
; }
2316 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
2317 if (*cp
== '\n' && last_char_was_cr
) {
2322 last_char_was_cr
= *cp
== '\r';
2328 char *stripped_content_file
;
2329 char *tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
);
2331 if (tempfile
== NULL
) {
2332 die("unable to create temporary file in %s",
2335 stripped_content_file
= mh_xstrdup (tempfile
);
2337 /* Strip each CR before a LF from the content. */
2338 fseeko (*fp
, begin
, SEEK_SET
);
2339 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) >
2343 bool last_char_was_cr
= false;
2345 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
2347 last_char_was_cr
= true;
2348 } else if (last_char_was_cr
) {
2350 if (write (fd
, "\r", 1) < 0) {
2351 advise (tempfile
, "CR write");
2354 if (write (fd
, cp
, 1) < 0) {
2355 advise (tempfile
, "write");
2357 last_char_was_cr
= false;
2359 if (write (fd
, cp
, 1) < 0) {
2360 advise (tempfile
, "write");
2362 last_char_was_cr
= false;
2368 inform("unable to write temporary file %s, continuing...",
2369 stripped_content_file
);
2370 (void) m_unlink (stripped_content_file
);
2371 free(stripped_content_file
);
2374 /* Replace the decoded file with the converted one. */
2375 if (ct
->c_cefile
.ce_file
&& ct
->c_cefile
.ce_unlink
)
2376 (void) m_unlink (ct
->c_cefile
.ce_file
);
2378 free(ct
->c_cefile
.ce_file
);
2379 ct
->c_cefile
.ce_file
= stripped_content_file
;
2380 ct
->c_cefile
.ce_unlink
= 1;
2384 report (NULL
, ct
->c_partno
,
2385 begin
== 0 && end
== 0 ? "" : *file
,
2391 if (opened_input_file
) {
2405 * Add/update, if necessary, the message C-T-E, based on the least restrictive
2406 * of the part C-T-E's.
2411 const int least_restrictive_enc
= least_restrictive_encoding (ct
);
2413 if (least_restrictive_enc
!= CE_UNKNOWN
&&
2414 least_restrictive_enc
!= CE_7BIT
) {
2415 char *cte
= concat (" ", ce_str (least_restrictive_enc
), "\n", NULL
);
2417 bool found_cte
= false;
2419 /* Update/add Content-Transfer-Encoding header field. */
2420 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
2421 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
2428 add_header (ct
, mh_xstrdup (ENCODING_FIELD
), cte
);
2435 * Find the least restrictive encoding (7bit, 8bit, binary) of the parts
2439 least_restrictive_encoding (CT ct
)
2441 int encoding
= CE_UNKNOWN
;
2443 switch (ct
->c_type
) {
2444 case CT_MULTIPART
: {
2445 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2448 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
2449 const int part_encoding
=
2450 least_restrictive_encoding (part
->mp_part
);
2452 if (less_restrictive (encoding
, part_encoding
)) {
2453 encoding
= part_encoding
;
2460 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2461 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2462 const int part_encoding
=
2463 least_restrictive_encoding (e
->eb_content
);
2465 if (less_restrictive (encoding
, part_encoding
)) {
2466 encoding
= part_encoding
;
2472 if (less_restrictive (encoding
, ct
->c_encoding
)) {
2473 encoding
= ct
->c_encoding
;
2482 * Return whether the second encoding is less restrictive than the first, where
2483 * "less restrictive" is in the sense used by RFC 2045 Secs. 6.1 and 6.4. So,
2484 * CE_BINARY is less restrictive than CE_8BIT and
2485 * CE_8BIT is less restrictive than CE_7BIT.
2488 less_restrictive (int encoding
, int second_encoding
)
2490 switch (second_encoding
) {
2492 return encoding
!= CE_BINARY
;
2494 return encoding
!= CE_BINARY
&& encoding
!= CE_8BIT
;
2496 return encoding
!= CE_BINARY
&& encoding
!= CE_8BIT
&&
2497 encoding
!= CE_7BIT
;
2505 * Convert character set of each part.
2508 convert_charsets (CT ct
, char *dest_charset
, int *message_mods
)
2512 switch (ct
->c_type
) {
2514 if (ct
->c_subtype
== TEXT_PLAIN
) {
2515 status
= convert_charset (ct
, dest_charset
, message_mods
);
2518 char *ct_charset
= content_charset (ct
);
2520 report (NULL
, ct
->c_partno
, ct
->c_file
,
2521 "convert %s to %s", ct_charset
, dest_charset
);
2525 char *ct_charset
= content_charset (ct
);
2527 report ("iconv", ct
->c_partno
, ct
->c_file
,
2528 "failed to convert %s to %s", ct_charset
, dest_charset
);
2534 case CT_MULTIPART
: {
2535 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2538 /* Should check to see if the body for this part is encoded?
2539 For now, it gets passed along as-is by InitMultiPart(). */
2540 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2542 convert_charsets (part
->mp_part
, dest_charset
, message_mods
);
2548 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2549 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2552 convert_charsets (e
->eb_content
, dest_charset
, message_mods
);
2565 * Fix various problems that aren't handled elsewhere. These
2566 * are fixed unconditionally: there are no switches to disable
2567 * them. Currently, "problems" are these:
2568 * 1) remove extraneous semicolon at the end of a header parameter list
2569 * 2) replace RFC 2047 encoding with RFC 2231 encoding of name and
2570 * filename parameters in Content-Type and Content-Disposition
2571 * headers, respectively.
2574 fix_always (CT ct
, int *message_mods
)
2578 switch (ct
->c_type
) {
2579 case CT_MULTIPART
: {
2580 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2583 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2584 status
= fix_always (part
->mp_part
, message_mods
);
2590 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2591 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2593 status
= fix_always (e
->eb_content
, message_mods
);
2600 if (ct
->c_first_hf
) {
2601 fix_filename_encoding (ct
);
2604 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
2605 size_t len
= strlen (hf
->value
);
2607 if (strcasecmp (hf
->name
, TYPE_FIELD
) != 0 &&
2608 strcasecmp (hf
->name
, DISPO_FIELD
) != 0) {
2609 /* Only do this for Content-Type and
2610 Content-Disposition fields because those are the
2611 only headers that parse_mime() warns about. */
2615 /* whitespace following a trailing ';' will be nuked as well */
2616 if (hf
->value
[len
- 1] == '\n') {
2617 while (isspace((unsigned char)(hf
->value
[len
- 2]))) {
2618 if (len
-- == 0) { break; }
2622 if (hf
->value
[len
- 2] == ';') {
2623 /* Remove trailing ';' from parameter value. */
2624 hf
->value
[len
- 2] = '\n';
2625 hf
->value
[len
- 1] = '\0';
2627 /* Also, if Content-Type parameter, remove trailing ';'
2628 from ct->c_ctline. This probably isn't necessary
2630 if (strcasecmp(hf
->name
, TYPE_FIELD
) == 0 && ct
->c_ctline
) {
2631 size_t l
= strlen(ct
->c_ctline
) - 1;
2632 while (isspace((unsigned char)(ct
->c_ctline
[l
])) ||
2633 ct
->c_ctline
[l
] == ';') {
2634 ct
->c_ctline
[l
--] = '\0';
2635 if (l
== 0) { break; }
2641 report (NULL
, ct
->c_partno
, ct
->c_file
,
2642 "remove trailing ; from %s parameter value",
2654 * Decodes UTF-8 encoded header values. Similar to fix_filename_param(), but
2655 * does not modify any MIME parameter values.
2658 decode_header_field_bodies (CT ct
, int *message_mods
)
2662 switch (ct
->c_type
) {
2663 case CT_MULTIPART
: {
2664 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2667 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2668 status
= decode_header_field_bodies (part
->mp_part
, message_mods
);
2674 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2675 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2677 status
= decode_header_field_bodies (e
->eb_content
, message_mods
);
2684 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
2685 /* Only decode UTF-8 values. */
2686 if (hf
->value
&& has_suffix(hf
->value
, "?=\n") &&
2687 (! strncasecmp (hf
->value
, " =?utf8?", 8) ||
2688 ! strncasecmp (hf
->value
, " =?utf-8?", 9))) {
2689 /* Looks like an RFC 2047 encoded parameter. */
2690 char decoded
[PATH_MAX
+ 1];
2692 if (decode_rfc2047 (hf
->value
, decoded
, sizeof decoded
)) {
2693 const size_t len
= strlen(decoded
);
2695 /* decode_rfc2047() could truncate if the buffer fills up.
2696 Detect and discard if that happened. */
2697 if (len
< sizeof(decoded
) - 1 && strcmp(hf
->value
, decoded
)) {
2698 hf
->value
= mh_xrealloc (hf
->value
, len
+ 1);
2699 strncpy (hf
->value
, decoded
, len
+ 1);
2703 inform("failed to decode %s parameter %s", hf
->name
, hf
->value
);
2714 * Factor out common code for loops in fix_filename_encoding().
2717 fix_filename_param (char *name
, char *value
, PM
*first_pm
, PM
*last_pm
)
2721 if (has_prefix(value
, "=?") && has_suffix(value
, "?=")) {
2722 /* Looks like an RFC 2047 encoded parameter. */
2723 char decoded
[PATH_MAX
+ 1];
2725 if (decode_rfc2047 (value
, decoded
, sizeof decoded
)) {
2726 /* Encode using RFC 2231. */
2727 replace_param (first_pm
, last_pm
, name
, decoded
, 0);
2730 inform("failed to decode %s parameter %s", name
, value
);
2739 * Replace RFC 2047 encoding with RFC 2231 encoding of name and
2740 * filename parameters in Content-Type and Content-Disposition
2741 * headers, respectively.
2744 fix_filename_encoding (CT ct
)
2750 for (pm
= ct
->c_ctinfo
.ci_first_pm
; pm
; pm
= pm
->pm_next
) {
2751 if (pm
->pm_name
&& pm
->pm_value
&&
2752 strcasecmp (pm
->pm_name
, "name") == 0) {
2753 fixed
= fix_filename_param (pm
->pm_name
, pm
->pm_value
,
2754 &ct
->c_ctinfo
.ci_first_pm
,
2755 &ct
->c_ctinfo
.ci_last_pm
);
2759 for (pm
= ct
->c_dispo_first
; pm
; pm
= pm
->pm_next
) {
2760 if (pm
->pm_name
&& pm
->pm_value
&&
2761 strcasecmp (pm
->pm_name
, "filename") == 0) {
2762 fixed
= fix_filename_param (pm
->pm_name
, pm
->pm_value
,
2768 /* Fix hf values to correspond. */
2769 for (hf
= ct
->c_first_hf
; fixed
&& hf
; hf
= hf
->next
) {
2770 enum { OTHER
, TYPE_HEADER
, DISPO_HEADER
} field
= OTHER
;
2772 if (strcasecmp (hf
->name
, TYPE_FIELD
) == 0) {
2773 field
= TYPE_HEADER
;
2774 } else if (strcasecmp (hf
->name
, DISPO_FIELD
) == 0) {
2775 field
= DISPO_HEADER
;
2778 if (field
!= OTHER
) {
2779 const char *const semicolon_loc
= strchr (hf
->value
, ';');
2781 if (semicolon_loc
) {
2783 strlen (hf
->name
) + 1 + semicolon_loc
- hf
->value
;
2784 const char *const params
=
2786 field
== TYPE_HEADER
2787 ? ct
->c_ctinfo
.ci_first_pm
2788 : ct
->c_dispo_first
,
2790 const char *const new_params
= concat (params
, "\n", NULL
);
2792 replace_substring (&hf
->value
, semicolon_loc
, new_params
);
2793 free((void *)new_params
); /* Cast away const. Sigh. */
2794 free((void *)params
);
2796 inform("did not find semicolon in %s:%s\n",
2797 hf
->name
, hf
->value
);
2807 * Output content in input file to output file.
2810 write_content (CT ct
, const char *input_filename
, char *outfile
, FILE *outfp
,
2811 int modify_inplace
, int message_mods
)
2815 if (modify_inplace
) {
2816 if (message_mods
> 0) {
2817 if ((status
= output_message_fp (ct
, outfp
, outfile
)) == OK
) {
2818 char *infile
= input_filename
2819 ? mh_xstrdup (input_filename
)
2820 : mh_xstrdup (ct
->c_file
? ct
->c_file
: "-");
2822 if (remove_file (infile
) == OK
) {
2823 if (rename (outfile
, infile
)) {
2824 /* Rename didn't work, possibly because of an
2825 attempt to rename across filesystems. Try
2826 brute force copy. */
2827 int old
= open (outfile
, O_RDONLY
);
2829 open (infile
, O_WRONLY
| O_CREAT
, m_gmprot ());
2832 if (old
!= -1 && new != -1) {
2833 char buffer
[NMH_BUFSIZ
];
2835 while ((i
= read (old
, buffer
, sizeof buffer
)) >
2837 if (write (new, buffer
, i
) != i
) {
2843 if (new != -1) { close (new); }
2844 if (old
!= -1) { close (old
); }
2845 (void) m_unlink (outfile
);
2848 /* The -file argument processing used path() to
2849 expand filename to absolute path. */
2850 int file
= ct
->c_file
&& ct
->c_file
[0] == '/';
2852 inform("unable to rename %s %s to %s, continuing...",
2853 file
? "file" : "message", outfile
,
2859 inform("unable to remove input file %s, "
2860 "not modifying it, continuing...", infile
);
2861 (void) m_unlink (outfile
);
2870 /* No modifications and didn't need the tmp outfile. */
2871 (void) m_unlink (outfile
);
2874 /* Output is going to some file. Produce it whether or not
2875 there were modifications. */
2876 status
= output_message_fp (ct
, outfp
, outfile
);
2885 * parse_mime() does not set lf_line_endings in struct text, so use this
2886 * function to do it. It touches the parts the decodetypes identifies.
2889 set_text_ctparams(CT ct
, char *decodetypes
, int lf_line_endings
)
2891 switch (ct
->c_type
) {
2892 case CT_MULTIPART
: {
2893 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2896 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
2897 set_text_ctparams(part
->mp_part
, decodetypes
, lf_line_endings
);
2903 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2904 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2906 set_text_ctparams(e
->eb_content
, decodetypes
, lf_line_endings
);
2911 if (should_decode(decodetypes
, ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
)) {
2912 if (ct
->c_ctparams
== NULL
) {
2913 ct
->c_ctparams
= mh_xcalloc(1, sizeof (struct text
));
2915 ((struct text
*) ct
->c_ctparams
)->lf_line_endings
= lf_line_endings
;
2922 * If "rmmproc" is defined, call that to remove the file. Otherwise,
2923 * use the standard MH backup file.
2926 remove_file (const char *file
)
2929 char *rmm_command
= concat (rmmproc
, " ", file
, NULL
);
2930 int status
= system (rmm_command
);
2933 return WIFEXITED (status
) ? WEXITSTATUS (status
) : NOTOK
;
2935 /* This is OK for a non-message file, it still uses the
2936 BACKUP_PREFIX form. The backup file will be in the same
2937 directory as file. */
2938 return rename (file
, m_backup (file
));
2943 * Output formatted message to user.
2946 report (char *what
, char *partno
, char *filename
, char *message
, ...)
2952 va_start (args
, message
);
2953 fmt
= concat (filename
, partno
? " part " : ", ",
2954 FENDNULL(partno
), partno
? ", " : "", message
, NULL
);
2956 advertise (what
, NULL
, fmt
, args
);
2969 fprintf (stderr
, "\n");