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/concat.h"
10 #include "sbr/seq_setprev.h"
11 #include "sbr/seq_setcur.h"
12 #include "sbr/seq_save.h"
13 #include "sbr/smatch.h"
14 #include "sbr/fmt_rfc2047.h"
15 #include "sbr/cpydata.h"
16 #include "sbr/trimcpy.h"
17 #include "sbr/m_convert.h"
18 #include "sbr/m_backup.h"
19 #include "sbr/getfolder.h"
20 #include "sbr/folder_read.h"
21 #include "sbr/context_save.h"
22 #include "sbr/context_replace.h"
23 #include "sbr/context_find.h"
24 #include "sbr/readconfig.h"
25 #include "sbr/ambigsw.h"
27 #include "sbr/print_version.h"
28 #include "sbr/print_help.h"
29 #include "sbr/error.h"
30 #include "h/fmt_scan.h"
32 #include "h/mhparse.h"
35 #include "h/signals.h"
36 #include "sbr/m_maildir.h"
37 #include "sbr/m_mktemp.h"
38 #include "sbr/mime_type.h"
42 #include "mhshowsbr.h"
45 #define MHFIXMSG_SWITCHES \
46 X("decodetext 8bit|7bit|binary", 0, DECODETEXTSW) \
47 X("nodecodetext", 0, NDECODETEXTSW) \
48 X("decodetypes", 0, DECODETYPESW) \
49 X("crlflinebreaks", 0, CRLFLINEBREAKSSW) \
50 X("nocrlflinebreaks", 0, NCRLFLINEBREAKSSW) \
51 X("textcharset", 0, TEXTCHARSETSW) \
52 X("notextcharset", 0, NTEXTCHARSETSW) \
53 X("reformat", 0, REFORMATSW) \
54 X("noreformat", 0, NREFORMATSW) \
55 X("replacetextplain", 0, REPLACETEXTPLAINSW) \
56 X("noreplacetextplain", 0, NREPLACETEXTPLAINSW) \
57 X("fixboundary", 0, FIXBOUNDARYSW) \
58 X("nofixboundary", 0, NFIXBOUNDARYSW) \
59 X("fixcte", 0, FIXCOMPOSITECTESW) \
60 X("nofixcte", 0, NFIXCOMPOSITECTESW) \
61 X("fixtype mimetype", 0, FIXTYPESW) \
62 X("file file", 0, FILESW) \
63 X("outfile file", 0, OUTFILESW) \
64 X("rmmproc program", 0, RPROCSW) \
65 X("normmproc", 0, NRPRCSW) \
66 X("changecur", 0, CHGSW) \
67 X("nochangecur", 0, NCHGSW) \
68 X("verbose", 0, VERBSW) \
69 X("noverbose", 0, NVERBSW) \
70 X("version", 0, VERSIONSW) \
71 X("help", 0, HELPSW) \
73 #define X(sw, minchars, id) id,
74 DEFINE_SWITCH_ENUM(MHFIXMSG
);
77 #define X(sw, minchars, id) { sw, minchars, id },
78 DEFINE_SWITCH_ARRAY(MHFIXMSG
, switches
);
83 int debugsw
; /* Needed by mhparse.c. */
85 #define quitser pipeser
90 typedef struct fix_transformations
{
98 /* Whether to use CRLF linebreaks, per RFC 2046 Sec. 4.1.1, par.1. */
101 } fix_transformations
;
103 static int mhfixmsgsbr (CT
*, char *, const fix_transformations
*,
104 FILE **, char *, FILE **);
105 static int fix_boundary (CT
*, int *);
106 static int copy_input_to_output (const char *, FILE *, const char *, FILE *);
107 static int get_multipart_boundary (CT
, char **);
108 static int replace_boundary (CT
, char *, char *);
109 static int fix_types (CT
, svector_t
, int *);
110 static char *replace_substring (char **, const char *, const char *);
111 static char *remove_parameter (char *, const char *);
112 static int fix_composite_cte (CT
, int *);
113 static int set_ce (CT
, int);
114 static int ensure_text_plain (CT
*, CT
, int *, int);
115 static int find_textplain_sibling (CT
, int, int *);
116 static int insert_new_text_plain_part (CT
, int, CT
);
117 static CT
build_text_plain_part (CT
);
118 static int insert_into_new_mp_alt (CT
*, int *);
119 static CT
divide_part (CT
);
120 static void copy_ctinfo (CI
, CI
);
121 static int decode_part (CT
);
122 static int reformat_part (CT
, char *, char *, char *, int);
123 static CT
build_multipart_alt (CT
, CT
, int, int);
124 static int boundary_in_content (FILE **, char *, const char *);
125 static void transfer_noncontent_headers (CT
, CT
);
126 static int set_ct_type (CT
, int type
, int subtype
, int encoding
);
127 static int decode_text_parts (CT
, int, const char *, int *);
128 static int should_decode(const char *, const char *, const char *);
129 static int content_encoding (CT
, const char **);
130 static int strip_crs (CT
, int *);
131 static void update_cte (CT
);
132 static int least_restrictive_encoding (CT
) PURE
;
133 static int less_restrictive (int, int);
134 static int convert_charsets (CT
, char *, int *);
135 static int fix_always (CT
, int *);
136 static int fix_filename_param (char *, char *, PM
*, PM
*);
137 static int fix_filename_encoding (CT
);
138 static int write_content (CT
, const char *, char *, FILE *, int, int);
139 static void set_text_ctparams(CT
, char *, int);
140 static int remove_file (const char *);
141 static void report (char *, char *, char *, char *, ...)
143 static void pipeser (int);
147 main (int argc
, char **argv
)
150 char *cp
, *file
= NULL
, *folder
= NULL
;
151 char *maildir
= NULL
, buf
[100], *outfile
= NULL
;
152 char **argp
, **arguments
;
153 struct msgs_array msgs
= { 0, 0, NULL
};
154 struct msgs
*mp
= NULL
;
156 FILE *fp
, *infp
= NULL
, *outfp
= NULL
;
157 bool using_stdin
= false;
160 fix_transformations fx
;
161 fx
.reformat
= fx
.fixcompositecte
= fx
.fixboundary
= 1;
163 fx
.replacetextplain
= 0;
164 fx
.decodetext
= CE_8BIT
;
165 fx
.decodetypes
= "text,application/ics"; /* Default, per man page. */
166 fx
.lf_line_endings
= 0;
167 fx
.textcharset
= NULL
;
169 if (nmh_init(argv
[0], true, false)) { return 1; }
171 arguments
= getarguments (invo_name
, argc
, argv
, 1);
177 while ((cp
= *argp
++)) {
179 switch (smatch (++cp
, switches
)) {
181 ambigsw (cp
, switches
);
184 die("-%s unknown", cp
);
187 snprintf (buf
, sizeof buf
, "%s [+folder] [msgs] [switches]",
189 print_help (buf
, switches
, 1);
192 print_version(invo_name
);
196 if (! (cp
= *argp
++) || *cp
== '-') {
197 die("missing argument to %s", argp
[-2]);
199 if (! strcasecmp (cp
, "8bit")) {
200 fx
.decodetext
= CE_8BIT
;
201 } else if (! strcasecmp (cp
, "7bit")) {
202 fx
.decodetext
= CE_7BIT
;
203 } else if (! strcasecmp (cp
, "binary")) {
204 fx
.decodetext
= CE_BINARY
;
206 die("invalid argument to %s", argp
[-2]);
213 if (! (cp
= *argp
++) || *cp
== '-') {
214 die("missing argument to %s", argp
[-2]);
218 case CRLFLINEBREAKSSW
:
219 fx
.lf_line_endings
= 0;
221 case NCRLFLINEBREAKSSW
:
222 fx
.lf_line_endings
= 1;
225 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
226 die("missing argument to %s", argp
[-2]);
239 case FIXCOMPOSITECTESW
:
240 fx
.fixcompositecte
= 1;
242 case NFIXCOMPOSITECTESW
:
243 fx
.fixcompositecte
= 0;
246 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
247 die("missing argument to %s", argp
[-2]);
249 if (! strncasecmp (cp
, "multipart/", 10) ||
250 ! strncasecmp (cp
, "message/", 8))
251 die("-fixtype %s not allowed", cp
);
252 if (! strchr (cp
, '/'))
253 die("-fixtype requires type/subtype");
254 if (fx
.fixtypes
== NULL
) { fx
.fixtypes
= svector_create (10); }
255 svector_push_back (fx
.fixtypes
, cp
);
263 case REPLACETEXTPLAINSW
:
264 fx
.replacetextplain
= 1;
266 case NREPLACETEXTPLAINSW
:
267 fx
.replacetextplain
= 0;
270 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
271 die("missing argument to %s", argp
[-2]);
273 file
= *cp
== '-' ? mh_xstrdup (cp
) : path (cp
, TFILE
);
276 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
277 die("missing argument to %s", argp
[-2]);
279 outfile
= *cp
== '-' ? mh_xstrdup (cp
) : path (cp
, TFILE
);
282 if (!(rmmproc
= *argp
++) || *rmmproc
== '-') {
283 die("missing argument to %s", argp
[-2]);
303 if (*cp
== '+' || *cp
== '@') {
305 die("only one folder at a time!");
306 folder
= pluspath (cp
);
309 /* Interpret a full path as a filename, not a message. */
310 file
= mh_xstrdup (cp
);
312 app_msgarg (&msgs
, cp
);
317 SIGNAL (SIGQUIT
, quitser
);
318 SIGNAL (SIGPIPE
, pipeser
);
321 * Read the standard profile setup
323 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
324 readconfig(NULL
, fp
, cp
, 0);
328 suppress_bogus_mp_content_warning
= skip_mp_cte_check
= true;
329 suppress_extraneous_trailing_semicolon_warning
= true;
331 if (! context_find ("path")) {
332 free (path ("./", TFOLDER
));
335 if (file
&& msgs
.size
) {
336 die("cannot specify msg and file at same time!");
340 /* Open the outfile now, so we don't have to risk opening it
341 after running out of fds. */
342 if (strcmp (outfile
, "-") == 0) {
344 } else if ((outfp
= fopen (outfile
, "w")) == NULL
) {
345 adios (outfile
, "unable to open for writing");
350 * check if message is coming from file
353 /* If file is stdin, create a tmp file name before parse_mime()
354 has a chance, because it might put in on a different
355 filesystem than the output file. Instead, put it in the
356 user's preferred tmp directory. */
359 if (! strcmp ("-", file
)) {
365 if ((cp
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
366 die("unable to create temporary file in %s",
370 file
= mh_xstrdup (cp
);
371 cpydata (STDIN_FILENO
, fd
, "-", file
);
375 (void) m_unlink (file
);
376 die("failed to write temporary file");
380 cts
= mh_xcalloc(2, sizeof *cts
);
383 if ((ct
= parse_mime (file
))) {
384 set_text_ctparams(ct
, fx
.decodetypes
, fx
.lf_line_endings
);
387 inform("unable to parse message from file %s", file
);
390 /* If there's an outfile, pass the input message unchanged, so the
391 message won't get dropped from a pipeline. */
393 /* Something went wrong. Output might be expected, such as if
394 this were run as a filter. Just copy the input to the
396 if ((infp
= fopen (file
, "r")) == NULL
) {
397 adios (file
, "unable to open for reading");
400 if (copy_input_to_output (file
, infp
, outfile
, outfp
) != OK
) {
401 inform("unable to copy message to %s, "
402 "it might be lost\n", outfile
);
411 * message(s) are coming from a folder
416 app_msgarg(&msgs
, "cur");
419 folder
= getfolder (1);
421 maildir
= mh_xstrdup(m_maildir (folder
));
423 /* chdir so that error messages, esp. from MIME parser, just
424 refer to the message and not its path. */
425 if (chdir (maildir
) == NOTOK
) {
426 adios (maildir
, "unable to change directory to");
429 /* read folder and create message structure */
430 if (! (mp
= folder_read (folder
, 1))) {
431 die("unable to read folder %s", folder
);
434 /* check for empty folder */
435 if (mp
->nummsg
== 0) {
436 die("no messages in %s", folder
);
439 /* parse all the message ranges/sequences and set SELECTED */
440 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
441 if (! m_convert (mp
, msgs
.msgs
[msgnum
])) {
444 seq_setprev (mp
); /* set the previous-sequence */
446 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
449 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
450 if (is_selected(mp
, msgnum
)) {
451 char *msgnam
= m_name (msgnum
);
453 if ((ct
= parse_mime (msgnam
))) {
454 set_text_ctparams(ct
, fx
.decodetypes
, fx
.lf_line_endings
);
457 inform("unable to parse message %s", msgnam
);
460 /* If there's an outfile, pass the input message
461 unchanged, so the message won't get dropped from a
464 /* Something went wrong. Output might be expected,
465 such as if this were run as a filter. Just copy
466 the input to the output. */
467 /* Can't use path() here because 1) it might have been
468 called before and it caches the pwd, and 2) we call
469 chdir() after that. */
470 char *input_filename
=
471 concat (maildir
, "/", msgnam
, NULL
);
473 if ((infp
= fopen (input_filename
, "r")) == NULL
) {
474 adios (input_filename
,
475 "unable to open for reading");
478 if (copy_input_to_output (input_filename
, infp
,
479 outfile
, outfp
) != OK
) {
480 inform("unable to copy message to %s, "
481 "it might be lost\n", outfile
);
486 free (input_filename
);
493 seq_setcur (mp
, mp
->hghsel
); /* update current message */
495 seq_save (mp
); /* synchronize sequences */
496 context_replace (pfolder
, folder
);/* update current folder */
497 context_save (); /* save the context file */
501 for (ctp
= cts
; *ctp
; ++ctp
) {
503 mhfixmsgsbr (ctp
, maildir
, &fx
, &infp
, outfile
, &outfp
) == OK
509 (void) m_unlink (file
);
512 /* Just calling m_backup() unlinks the backup file. */
513 (void) m_backup (file
);
524 if (fx
.fixtypes
!= NULL
) { svector_free (fx
.fixtypes
); }
525 if (infp
) { fclose (infp
); } /* even if stdin */
526 if (outfp
) { fclose (outfp
); } /* even if stdout */
532 done (status
== OK
? 0 : 1);
538 * Apply transformations to one message.
541 mhfixmsgsbr (CT
*ctp
, char *maildir
, const fix_transformations
*fx
,
542 FILE **infp
, char *outfile
, FILE **outfp
)
544 /* Store input filename in case one of the transformations, i.e.,
545 fix_boundary(), rewrites to a tmp file. */
546 char *input_filename
= maildir
547 ? concat (maildir
, "/", (*ctp
)->c_file
, NULL
)
548 : mh_xstrdup ((*ctp
)->c_file
);
549 bool modify_inplace
= false;
550 int message_mods
= 0;
553 /* Though the input file won't need to be opened if everything goes
554 well, do it here just in case there's a failure, and that failure is
555 running out of file descriptors. */
556 if ((*infp
= fopen (input_filename
, "r")) == NULL
) {
557 adios (input_filename
, "unable to open for reading");
560 if (outfile
== NULL
) {
561 modify_inplace
= true;
563 if ((*ctp
)->c_file
) {
565 /* outfp will be closed by the caller */
566 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, outfp
)) ==
568 die("unable to create temporary file in %s",
571 outfile
= mh_xstrdup (tempfile
);
573 die("missing both input and output filenames\n");
575 } /* else *outfp was defined by caller */
577 reverse_alternative_parts (*ctp
);
578 status
= fix_always (*ctp
, &message_mods
);
579 if (status
== OK
&& fx
->fixboundary
) {
580 status
= fix_boundary (ctp
, &message_mods
);
582 if (status
== OK
&& fx
->fixtypes
!= NULL
) {
583 status
= fix_types (*ctp
, fx
->fixtypes
, &message_mods
);
585 if (status
== OK
&& fx
->fixcompositecte
) {
586 status
= fix_composite_cte (*ctp
, &message_mods
);
588 if (status
== OK
&& fx
->reformat
) {
590 ensure_text_plain (ctp
, NULL
, &message_mods
, fx
->replacetextplain
);
592 if (status
== OK
&& fx
->decodetext
) {
593 status
= decode_text_parts (*ctp
, fx
->decodetext
, fx
->decodetypes
,
597 if (status
== OK
&& fx
->textcharset
!= NULL
) {
598 status
= convert_charsets (*ctp
, fx
->textcharset
, &message_mods
);
601 if (status
== OK
&& ! (*ctp
)->c_umask
) {
602 /* Set the umask for the contents file. This currently
603 isn't used but just in case it is in the future. */
606 if (stat ((*ctp
)->c_file
, &st
) != NOTOK
) {
607 (*ctp
)->c_umask
= ~(st
.st_mode
& 0777);
609 (*ctp
)->c_umask
= ~m_gmprot();
614 * Write the content to a file
617 status
= write_content (*ctp
, input_filename
, outfile
, *outfp
,
618 modify_inplace
, message_mods
);
619 } else if (! modify_inplace
) {
620 /* Something went wrong. Output might be expected, such
621 as if this were run as a filter. Just copy the input
623 if (copy_input_to_output (input_filename
, *infp
, outfile
,
625 inform("unable to copy message to %s, it might be lost\n",
630 if (modify_inplace
) {
631 if (status
!= OK
) { (void) m_unlink (outfile
); }
638 free (input_filename
);
645 * Copy input message to output. Assumes not modifying in place, so this
646 * might be running as part of a pipeline.
649 copy_input_to_output (const char *input_filename
, FILE *infp
,
650 const char *output_filename
, FILE *outfp
)
652 int in
= fileno (infp
);
653 int out
= fileno (outfp
);
656 if (in
!= -1 && out
!= -1) {
657 cpydata (in
, out
, input_filename
, output_filename
);
667 * Fix mismatched outer level boundary.
670 fix_boundary (CT
*ct
, int *message_mods
)
672 struct multipart
*mp
;
675 if (ct
&& (*ct
)->c_type
== CT_MULTIPART
&& bogus_mp_content
) {
676 mp
= (struct multipart
*) (*ct
)->c_ctparams
;
679 * 1) Get boundary at end of part.
680 * 2) Get boundary at beginning of part and compare to the end-of-part
682 * 3) Write out contents of ct to tmp file, replacing boundary in
683 * header with boundary from part. Set c_unlink to 1.
685 * 5) Call parse_mime() on the tmp file, replacing ct.
688 if (mp
&& mp
->mp_start
) {
691 if (get_multipart_boundary (*ct
, &part_boundary
) == OK
) {
694 if ((fixed
= m_mktemp2 (NULL
, invo_name
, NULL
, &(*ct
)->c_fp
))) {
695 if (replace_boundary (*ct
, fixed
, part_boundary
) == OK
) {
696 char *filename
= mh_xstrdup ((*ct
)->c_file
);
700 if ((fixed_ct
= parse_mime (fixed
))) {
706 report (NULL
, NULL
, filename
,
707 "fix multipart boundary");
711 inform("unable to parse fixed part");
716 inform("unable to replace broken boundary");
720 inform("unable to create temporary file in %s",
725 free (part_boundary
);
727 /* Couldn't fix the boundary. Report failure so that mhfixmsg
728 doesn't modify the message. */
732 /* No multipart struct, even though the content type is
733 CT_MULTIPART. Report failure so that mhfixmsg doesn't modify
744 * Find boundary at end of multipart.
747 get_multipart_boundary (CT ct
, char **part_boundary
)
749 char buffer
[NMH_BUFSIZ
];
750 char *end_boundary
= NULL
;
751 off_t begin
= (off_t
) ct
->c_end
> (off_t
) (ct
->c_begin
+ sizeof buffer
)
752 ? (off_t
) (ct
->c_end
- sizeof buffer
)
753 : (off_t
) ct
->c_begin
;
757 /* This will fail if the boundary spans fread() calls. NMH_BUFSIZ should
758 be big enough, even if it's just 1024, to make that unlikely. */
760 /* free_content() will close ct->c_fp if bogus MP boundary is fixed. */
761 if (! ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
762 advise (ct
->c_file
, "unable to open for reading");
766 /* Get boundary at end of multipart. */
767 while (begin
>= (off_t
) ct
->c_begin
) {
768 fseeko (ct
->c_fp
, begin
, SEEK_SET
);
769 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
770 char *cp
= rfind_str (buffer
, bytes_read
, "--");
775 /* Trim off trailing "--" and anything beyond. */
777 if ((end
= rfind_str (buffer
, cp
- buffer
, "\n"))) {
778 if (strlen (end
) > 3 && *end
++ == '\n' &&
779 *end
++ == '-' && *end
++ == '-') {
780 end_boundary
= mh_xstrdup (end
);
787 if (end_boundary
|| begin
<= (off_t
) (ct
->c_begin
+ sizeof buffer
))
789 begin
-= sizeof buffer
;
792 /* Get boundary at beginning of multipart. */
794 fseeko (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
795 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
796 if (bytes_read
>= strlen (end_boundary
)) {
797 char *cp
= find_str (buffer
, bytes_read
, end_boundary
);
799 if (cp
&& cp
- buffer
>= 2 && *--cp
== '-' &&
800 *--cp
== '-' && (cp
> buffer
&& *--cp
== '\n')) {
805 /* The start and end boundaries didn't match, or the
806 start boundary doesn't begin with "\n--" (or "--"
807 if at the beginning of buffer). Keep trying. */
821 *part_boundary
= end_boundary
;
823 *part_boundary
= NULL
;
832 * Open and copy ct->c_file to file, replacing the multipart boundary.
835 replace_boundary (CT ct
, char *file
, char *boundary
)
839 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
841 m_getfld_state_t gstate
;
844 if (ct
->c_file
== NULL
) {
845 inform("missing input filename");
849 if ((fpin
= fopen (ct
->c_file
, "r")) == NULL
) {
850 advise (ct
->c_file
, "unable to open for reading");
854 if ((fpout
= fopen (file
, "w")) == NULL
) {
856 advise (file
, "unable to open for writing");
860 gstate
= m_getfld_state_init(fpin
);
861 for (compnum
= 1;;) {
862 int bufsz
= (int) sizeof buf
;
864 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
869 /* get copies of the buffers */
870 np
= mh_xstrdup (name
);
871 vp
= mh_xstrdup (buf
);
873 /* if necessary, get rest of field */
874 while (state
== FLDPLUS
) {
876 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
877 vp
= add (buf
, vp
); /* add to previous value */
880 if (strcasecmp (TYPE_FIELD
, np
)) {
881 fprintf (fpout
, "%s:%s", np
, vp
);
883 char *new_ctline
, *new_params
;
885 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
886 &ct
->c_ctinfo
.ci_last_pm
, "boundary",
889 new_ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
890 ct
->c_ctinfo
.ci_subtype
, NULL
);
891 new_params
= output_params(LEN(TYPE_FIELD
) +
892 strlen(new_ctline
) + 1,
893 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
894 fprintf (fpout
, "%s:%s%s\n", np
, new_ctline
,
895 FENDNULL(new_params
));
907 /* buf will have a terminating NULL, skip it. */
908 if ((int) fwrite (buf
, 1, bufsz
-1, fpout
) < bufsz
-1) {
909 advise (file
, "fwrite");
918 inform("message format error in component #%d", compnum
);
923 inform("getfld() returned %d", state
);
931 m_getfld_state_destroy (&gstate
);
940 * Fix Content-Type header to reflect the content of its part.
943 fix_types (CT ct
, svector_t fixtypes
, int *message_mods
)
947 switch (ct
->c_type
) {
949 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
952 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
953 status
= fix_types (part
->mp_part
, fixtypes
, message_mods
);
959 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
960 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
962 status
= fix_types (e
->eb_content
, fixtypes
, message_mods
);
969 if (ct
->c_ctinfo
.ci_type
&& ct
->c_ctinfo
.ci_subtype
) {
970 for (typep
= svector_strs (fixtypes
);
971 typep
&& (type
= *typep
);
974 concat (ct
->c_ctinfo
.ci_type
, "/", ct
->c_ctinfo
.ci_subtype
,
977 if (! strcasecmp (type
, type_subtype
) &&
978 decode_part (ct
) == OK
&&
979 ct
->c_cefile
.ce_file
!= NULL
) {
980 char *ct_type_subtype
= mime_type (ct
->c_cefile
.ce_file
);
983 if ((cp
= strchr (ct_type_subtype
, ';'))) {
984 /* Truncate to remove any parameter list from
985 mime_type () result. */
989 if (strcasecmp (type
, ct_type_subtype
)) {
990 char *ct_type
, *ct_subtype
;
993 /* The Content-Type header does not match the
994 content, so update these struct Content
997 * c_ctinfo.ci_type, c_ctinfo.ci_subtype
1000 /* Extract type and subtype from type/subtype. */
1001 ct_type
= mh_xstrdup(ct_type_subtype
);
1002 if ((cp
= strchr (ct_type
, '/'))) {
1004 ct_subtype
= mh_xstrdup(++cp
);
1006 inform("missing / in MIME type of %s %s",
1007 ct
->c_file
, ct
->c_partno
);
1012 ct
->c_type
= ct_str_type (ct_type
);
1013 ct
->c_subtype
= ct_str_subtype (ct
->c_type
, ct_subtype
);
1015 free (ct
->c_ctinfo
.ci_type
);
1016 ct
->c_ctinfo
.ci_type
= ct_type
;
1017 free (ct
->c_ctinfo
.ci_subtype
);
1018 ct
->c_ctinfo
.ci_subtype
= ct_subtype
;
1019 if (! replace_substring (&ct
->c_ctline
, type
,
1021 inform("did not find %s in %s",
1022 type
, ct
->c_ctline
);
1025 /* Update Content-Type header field. */
1026 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1027 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1028 if (replace_substring (&hf
->value
, type
,
1032 report (NULL
, ct
->c_partno
, ct
->c_file
,
1033 "change Content-Type in header "
1035 type
, ct_type_subtype
);
1039 inform("did not find %s in %s", type
, hf
->value
);
1043 free (ct_type_subtype
);
1045 free (type_subtype
);
1055 * Replace a substring, allocating space to hold the new one.
1058 replace_substring (char **str
, const char *old
, const char *new)
1062 if ((cp
= strstr (*str
, old
))) {
1063 char *remainder
= cp
+ strlen (old
);
1064 char *prefix
, *new_str
;
1067 prefix
= mh_xstrdup(*str
);
1068 *(prefix
+ (cp
- *str
)) = '\0';
1069 new_str
= concat (prefix
, new, remainder
, NULL
);
1072 new_str
= concat (new, remainder
, NULL
);
1077 return *str
= new_str
;
1085 * Remove a name=value parameter, given just its name, from a header value.
1088 remove_parameter (char *str
, const char *name
)
1090 /* It looks to me, based on the BNF in RFC 2045, than there can't
1091 be whitespace between the parameter name and the "=", or
1092 between the "=" and the parameter value. */
1093 char *param_name
= concat (name
, "=", NULL
);
1096 if ((cp
= strstr (str
, param_name
))) {
1100 /* Remove any leading spaces, before the parameter name. */
1102 start
> str
&& isspace ((unsigned char) *(start
-1));
1106 /* Remove a leading semicolon. */
1107 if (start
> str
&& *(start
-1) == ';') { --start
; }
1109 end
= cp
+ strlen (name
) + 1;
1111 /* Skip past the quoted value, and then the final quote. */
1112 for (++end
; *end
&& *end
!= '"'; ++end
) { continue; }
1115 /* Skip past the value. */
1116 for (++end
; *end
&& ! isspace ((unsigned char) *end
); ++end
) {}
1119 /* Count how many characters need to be moved. Include
1120 trailing null, which is accounted for by the
1121 initialization of count to 1. */
1122 for (cp
= end
; *cp
; ++cp
) { ++count
; }
1123 (void) memmove (start
, end
, count
);
1133 * Fix Content-Transfer-Encoding of composite,, e.g., message or multipart, part.
1134 * According to RFC 2045 Sec. 6.4, it must be 7bit, 8bit, or binary. Set it to
1138 fix_composite_cte (CT ct
, int *message_mods
)
1142 if (ct
->c_type
== CT_MESSAGE
|| ct
->c_type
== CT_MULTIPART
) {
1143 if (ct
->c_encoding
!= CE_7BIT
&& ct
->c_encoding
!= CE_8BIT
&&
1144 ct
->c_encoding
!= CE_BINARY
) {
1147 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1148 char *name
= hf
->name
;
1149 for (; isspace((unsigned char)*name
); ++name
) {
1153 if (! strncasecmp (name
, ENCODING_FIELD
,
1154 LEN(ENCODING_FIELD
))) {
1155 char *prefix
= "Nmh-REPLACED-INVALID-";
1159 h
->name
= mh_xstrdup (hf
->name
);
1160 h
->hf_encoding
= hf
->hf_encoding
;
1164 /* Retain old header but prefix its name. */
1166 hf
->name
= concat (prefix
, h
->name
, NULL
);
1170 char *encoding
= cpytrim (hf
->value
);
1171 report (NULL
, ct
->c_partno
, ct
->c_file
,
1172 "replace Content-Transfer-Encoding of %s "
1173 "with 8 bit", encoding
);
1177 h
->value
= mh_xstrdup (" 8bit\n");
1179 /* Don't need to warn for multiple C-T-E header
1180 fields, parse_mime() already does that. But
1181 if there are any, fix them all as necessary. */
1186 set_ce (ct
, CE_8BIT
);
1189 if (ct
->c_type
== CT_MULTIPART
) {
1190 struct multipart
*m
;
1193 m
= (struct multipart
*) ct
->c_ctparams
;
1194 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1195 if (fix_composite_cte (part
->mp_part
, message_mods
) != OK
) {
1208 * Set content encoding.
1211 set_ce (CT ct
, int encoding
)
1213 const char *ce
= ce_str (encoding
);
1214 const struct str2init
*ctinit
= get_ce_method (ce
);
1217 char *cte
= concat (" ", ce
, "\n", NULL
);
1218 bool found_cte
= false;
1220 /* Decoded contents might be in ct->c_cefile.ce_file, if the
1221 caller is decode_text_parts (). Save because we'll
1223 struct cefile decoded_content_info
= ct
->c_cefile
;
1225 ct
->c_encoding
= encoding
;
1227 ct
->c_ctinitfnx
= ctinit
->si_init
;
1228 /* This will assign ct->c_cefile with an all-0 struct, which
1230 (*ctinit
->si_init
) (ct
);
1231 /* After returning, the caller should set
1232 ct->c_cefile.ce_file to the name of the file containing
1235 if (ct
->c_ceclosefnx
) {
1236 (*ct
->c_ceclosefnx
) (ct
);
1239 /* Restore the cefile. */
1240 ct
->c_cefile
= decoded_content_info
;
1242 /* Update/add Content-Transfer-Encoding header field. */
1243 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1244 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
1251 add_header (ct
, mh_xstrdup (ENCODING_FIELD
), cte
);
1254 /* Update c_celine. It's used only by mhlist -debug. */
1255 free (ct
->c_celine
);
1256 ct
->c_celine
= mh_xstrdup (cte
);
1266 * Make sure each text part has a corresponding text/plain part.
1269 ensure_text_plain (CT
*ct
, CT parent
, int *message_mods
, int replacetextplain
)
1273 switch ((*ct
)->c_type
) {
1275 /* Nothing to do for text/plain. */
1276 if ((*ct
)->c_subtype
== TEXT_PLAIN
) { return OK
; }
1278 if (parent
&& parent
->c_type
== CT_MULTIPART
&&
1279 parent
->c_subtype
== MULTI_ALTERNATE
) {
1280 int new_subpart_number
= 1;
1281 int has_text_plain
=
1282 find_textplain_sibling (parent
, replacetextplain
,
1283 &new_subpart_number
);
1285 if (! has_text_plain
) {
1286 /* Parent is a multipart/alternative. Insert a new
1287 text/plain subpart. */
1288 const int inserted
=
1289 insert_new_text_plain_part (*ct
, new_subpart_number
,
1294 report (NULL
, parent
->c_partno
, parent
->c_file
,
1295 "insert text/plain part");
1301 } else if (parent
&& parent
->c_type
== CT_MULTIPART
&&
1302 parent
->c_subtype
== MULTI_RELATED
) {
1303 char *type_subtype
=
1304 concat ((*ct
)->c_ctinfo
.ci_type
, "/",
1305 (*ct
)->c_ctinfo
.ci_subtype
, NULL
);
1306 const char *parent_type
=
1307 get_param (parent
->c_ctinfo
.ci_first_pm
, "type", '?', 1);
1308 int new_subpart_number
= 1;
1309 int has_text_plain
= 0;
1311 /* Have to do string comparison on the subtype because we
1312 don't enumerate all of them in c_subtype values.
1313 parent_type will be NULL if the multipart/related part
1314 doesn't have a type parameter. The type parameter must
1315 be specified according to RFC 2387 Sec. 3.1 but not all
1317 if (parent_type
&& strcasecmp (type_subtype
, parent_type
) == 0) {
1318 /* The type of this part matches the root type of the
1319 parent multipart/related. Look to see if there's
1320 text/plain sibling. */
1322 find_textplain_sibling (parent
, replacetextplain
,
1323 &new_subpart_number
);
1326 free (type_subtype
);
1328 if (! has_text_plain
) {
1329 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1333 for (part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
1334 if (*ct
!= part
->mp_part
) {
1340 /* Parent is a multipart/related. Insert a new
1341 text/plain subpart in a new multipart/alternative. */
1342 if (insert_into_new_mp_alt (ct
, message_mods
)) {
1343 /* Not an error if text/plain couldn't be added. */
1346 /* There are no siblings, so insert a new text/plain
1347 subpart, and change the parent type from
1348 multipart/related to multipart/alternative. */
1349 const int inserted
=
1350 insert_new_text_plain_part (*ct
, new_subpart_number
,
1356 parent
->c_subtype
= MULTI_ALTERNATE
;
1357 free (parent
->c_ctinfo
.ci_subtype
);
1358 parent
->c_ctinfo
.ci_subtype
= mh_xstrdup("alternative");
1359 if (! replace_substring (&parent
->c_ctline
, "/related",
1361 inform("did not find multipart/related in %s",
1365 /* Update Content-Type header field. */
1366 for (hf
= parent
->c_first_hf
; hf
; hf
= hf
->next
) {
1367 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1368 if (replace_substring (&hf
->value
, "/related",
1372 report (NULL
, parent
->c_partno
,
1374 "insert text/plain part");
1377 /* Remove, e.g., type="text/html" from
1378 multipart/alternative. */
1379 remove_parameter (hf
->value
, "type");
1382 inform("did not find multipart/"
1383 "related in header %s", hf
->value
);
1387 /* Not an error if text/plain couldn't be inserted. */
1392 if (insert_into_new_mp_alt (ct
, message_mods
)) {
1399 case CT_MULTIPART
: {
1400 struct multipart
*mp
= (struct multipart
*) (*ct
)->c_ctparams
;
1403 for (part
= mp
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1404 if ((*ct
)->c_type
== CT_MULTIPART
) {
1405 status
= ensure_text_plain (&part
->mp_part
, *ct
, message_mods
,
1413 if ((*ct
)->c_subtype
== MESSAGE_EXTERNAL
) {
1414 struct exbody
*e
= (struct exbody
*) (*ct
)->c_ctparams
;
1416 status
= ensure_text_plain (&e
->eb_content
, *ct
, message_mods
,
1427 * See if there is a sibling text/plain, and return its subpart number.
1430 find_textplain_sibling (CT parent
, int replacetextplain
,
1431 int *new_subpart_number
)
1433 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1434 struct part
*part
, *prev
;
1435 bool has_text_plain
= false;
1437 for (prev
= part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
1438 ++*new_subpart_number
;
1439 if (part
->mp_part
->c_type
== CT_TEXT
&&
1440 part
->mp_part
->c_subtype
== TEXT_PLAIN
) {
1441 if (replacetextplain
) {
1442 struct part
*old_part
;
1443 if (part
== mp
->mp_parts
) {
1444 old_part
= mp
->mp_parts
;
1445 mp
->mp_parts
= part
->mp_next
;
1447 old_part
= prev
->mp_next
;
1448 prev
->mp_next
= part
->mp_next
;
1451 report (NULL
, parent
->c_partno
, parent
->c_file
,
1452 "remove text/plain part %s",
1453 old_part
->mp_part
->c_partno
);
1455 free_content (old_part
->mp_part
);
1458 has_text_plain
= true;
1465 return has_text_plain
;
1470 * Insert a new text/plain part.
1473 insert_new_text_plain_part (CT ct
, int new_subpart_number
, CT parent
)
1475 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1476 struct part
*new_part
;
1479 if ((new_part
->mp_part
= build_text_plain_part (ct
))) {
1481 snprintf (buffer
, sizeof buffer
, "%d", new_subpart_number
);
1483 new_part
->mp_next
= mp
->mp_parts
;
1484 mp
->mp_parts
= new_part
;
1485 new_part
->mp_part
->c_partno
=
1486 concat (parent
->c_partno
? parent
->c_partno
: "1", ".",
1492 free_content (new_part
->mp_part
);
1500 * Create a text/plain part to go along with non-plain sibling part.
1503 build_text_plain_part (CT encoded_part
)
1505 CT tp_part
= divide_part (encoded_part
);
1506 char *tmp_plain_file
= NULL
;
1508 if (decode_part (tp_part
) == OK
) {
1509 /* Now, tp_part->c_cefile.ce_file is the name of the tmp file that
1510 contains the decoded contents. And the decoding function, such
1511 as openQuoted, will have set ...->ce_unlink to 1 so that it will
1512 be unlinked by free_content (). */
1515 /* This m_mktemp2() call closes the temp file. */
1516 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1517 inform("unable to create temporary file in %s",
1520 tmp_plain_file
= mh_xstrdup (tempfile
);
1521 if (reformat_part (tp_part
, tmp_plain_file
,
1522 tp_part
->c_ctinfo
.ci_type
,
1523 tp_part
->c_ctinfo
.ci_subtype
,
1524 tp_part
->c_type
) == OK
) {
1530 free_content (tp_part
);
1531 if (tmp_plain_file
) { (void) m_unlink (tmp_plain_file
); }
1532 free (tmp_plain_file
);
1539 * Slip new text/plain part into a new multipart/alternative.
1542 insert_into_new_mp_alt (CT
*ct
, int *message_mods
)
1544 CT tp_part
= build_text_plain_part (*ct
);
1548 CT mp_alt
= build_multipart_alt (*ct
, tp_part
, CT_MULTIPART
,
1551 struct multipart
*mp
= (struct multipart
*) mp_alt
->c_ctparams
;
1553 if (mp
&& mp
->mp_parts
) {
1554 mp
->mp_parts
->mp_part
= tp_part
;
1555 /* Make the new multipart/alternative the parent. */
1560 report (NULL
, (*ct
)->c_partno
, (*ct
)->c_file
,
1561 "insert text/plain part");
1564 free_content (tp_part
);
1565 free_content (mp_alt
);
1572 /* Not an error if text/plain couldn't be built. */
1580 * Clone a MIME part.
1588 /* Just copy over what is needed for decoding. c_vrsn and
1589 c_celine aren't necessary. */
1590 new_part
->c_file
= mh_xstrdup (ct
->c_file
);
1591 new_part
->c_begin
= ct
->c_begin
;
1592 new_part
->c_end
= ct
->c_end
;
1593 copy_ctinfo (&new_part
->c_ctinfo
, &ct
->c_ctinfo
);
1594 new_part
->c_type
= ct
->c_type
;
1595 new_part
->c_cefile
= ct
->c_cefile
;
1596 new_part
->c_encoding
= ct
->c_encoding
;
1597 new_part
->c_ctinitfnx
= ct
->c_ctinitfnx
;
1598 new_part
->c_ceopenfnx
= ct
->c_ceopenfnx
;
1599 new_part
->c_ceclosefnx
= ct
->c_ceclosefnx
;
1600 new_part
->c_cesizefnx
= ct
->c_cesizefnx
;
1602 /* c_ctline is used by reformat__part(), so it can preserve
1603 anything after the type/subtype. */
1604 new_part
->c_ctline
= mh_xstrdup (ct
->c_ctline
);
1611 * Copy the content info from one part to another.
1614 copy_ctinfo (CI dest
, CI src
)
1618 dest
->ci_type
= src
->ci_type
? mh_xstrdup (src
->ci_type
) : NULL
;
1619 dest
->ci_subtype
= src
->ci_subtype
? mh_xstrdup (src
->ci_subtype
) : NULL
;
1621 for (s_pm
= src
->ci_first_pm
; s_pm
; s_pm
= s_pm
->pm_next
) {
1622 d_pm
= add_param(&dest
->ci_first_pm
, &dest
->ci_last_pm
, s_pm
->pm_name
,
1624 if (s_pm
->pm_charset
) {
1625 d_pm
->pm_charset
= mh_xstrdup(s_pm
->pm_charset
);
1627 if (s_pm
->pm_lang
) {
1628 d_pm
->pm_lang
= mh_xstrdup(s_pm
->pm_lang
);
1632 dest
->ci_comment
= src
->ci_comment
? mh_xstrdup (src
->ci_comment
) : NULL
;
1633 dest
->ci_magic
= src
->ci_magic
? mh_xstrdup (src
->ci_magic
) : NULL
;
1648 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, &file
)) == NULL
) {
1649 die("unable to create temporary file in %s", get_temp_dir());
1651 tmp_decoded
= mh_xstrdup (tempfile
);
1652 /* The following call will load ct->c_cefile.ce_file with the tmp
1653 filename of the decoded content. tmp_decoded will contain the
1654 encoded output, get rid of that. */
1655 status
= output_message_fp (ct
, file
, tmp_decoded
);
1656 (void) m_unlink (tmp_decoded
);
1658 if (fclose (file
)) {
1659 inform("unable to close temporary file %s, continuing...", tempfile
);
1667 * Reformat content as plain text.
1668 * Some of the arguments aren't really needed now, but maybe will
1669 * be in the future for other than text types.
1672 reformat_part (CT ct
, char *file
, char *type
, char *subtype
, int c_type
)
1674 int output_subtype
, output_encoding
;
1675 const char *reason
= NULL
;
1679 /* Hacky: this redirects the output from whatever command is used
1680 to show the part to a file. So, the user can't have any output
1681 redirection in that command.
1682 Could show_multi() in mhshowsbr.c avoid this? */
1684 /* Check for invo_name-format-type/subtype. */
1685 if ((cf
= context_find_by_type ("format", type
, subtype
)) == NULL
) {
1687 inform("Don't know how to convert %s, there is no "
1688 "%s-format-%s/%s profile entry",
1689 ct
->c_file
, invo_name
, type
, subtype
);
1693 if (strchr (cf
, '>')) {
1694 inform("'>' prohibited in \"%s\",\nplease fix your "
1695 "%s-format-%s/%s profile entry", cf
, invo_name
, type
,
1701 cp
= concat (cf
, " >", file
, NULL
);
1702 status
= show_content_aux (ct
, 0, cp
, NULL
, NULL
);
1705 /* Unlink decoded content tmp file and free its filename to avoid
1706 leaks. The file stream should already have been closed. */
1707 if (ct
->c_cefile
.ce_unlink
) {
1708 (void) m_unlink (ct
->c_cefile
.ce_file
);
1709 free (ct
->c_cefile
.ce_file
);
1710 ct
->c_cefile
.ce_file
= NULL
;
1711 ct
->c_cefile
.ce_unlink
= 0;
1714 if (c_type
== CT_TEXT
) {
1715 output_subtype
= TEXT_PLAIN
;
1717 /* Set subtype to 0, which is always an UNKNOWN subtype. */
1721 output_encoding
= content_encoding (ct
, &reason
);
1723 set_ct_type (ct
, c_type
, output_subtype
, output_encoding
) == OK
) {
1724 ct
->c_cefile
.ce_file
= file
;
1725 ct
->c_cefile
.ce_unlink
= 1;
1727 ct
->c_cefile
.ce_unlink
= 0;
1736 * Fill in a multipart/alternative part.
1739 build_multipart_alt (CT first_alt
, CT new_part
, int type
, int subtype
)
1741 char *boundary_prefix
= "----=_nmh-multipart";
1742 char *boundary
= concat (boundary_prefix
, first_alt
->c_partno
, NULL
);
1743 char *boundary_indicator
= "; boundary=";
1744 char *typename
, *subtypename
, *name
;
1747 struct multipart
*m
;
1748 const struct str2init
*ctinit
;
1752 /* Set up the multipart/alternative part. These fields of *ct were
1753 initialized to 0 by mh_xcalloc():
1754 c_fp, c_unlink, c_begin, c_end,
1755 c_vrsn, c_ctline, c_celine,
1756 c_id, c_descr, c_dispo, c_partno,
1757 c_ctinfo.ci_comment, c_ctinfo.ci_magic,
1758 c_cefile, c_encoding,
1759 c_digested, c_digest[16], c_ctexbody,
1760 c_ctinitfnx, c_ceopenfnx, c_ceclosefnx, c_cesizefnx,
1762 c_showproc, c_termproc, c_storeproc, c_storage, c_folder
1765 ct
->c_file
= mh_xstrdup (first_alt
->c_file
);
1767 ct
->c_subtype
= subtype
;
1769 ctinit
= get_ct_init (ct
->c_type
);
1771 typename
= ct_type_str (type
);
1772 subtypename
= ct_subtype_str (type
, subtype
);
1776 int found_boundary
= 1;
1778 while (found_boundary
&& serial
< 1000000) {
1781 /* Ensure that the boundary doesn't appear in the decoded
1783 if (new_part
->c_cefile
.ce_file
) {
1784 if ((found_boundary
=
1785 boundary_in_content (&new_part
->c_cefile
.ce_fp
,
1786 new_part
->c_cefile
.ce_file
,
1787 boundary
)) == NOTOK
) {
1792 /* Ensure that the boundary doesn't appear in the encoded
1794 if (! found_boundary
&& new_part
->c_file
) {
1795 if ((found_boundary
=
1796 boundary_in_content (&new_part
->c_fp
,
1798 boundary
)) == NOTOK
) {
1803 if (found_boundary
) {
1804 /* Try a slightly different boundary. */
1809 snprintf (buffer2
, sizeof buffer2
, "%d", serial
);
1811 concat (boundary_prefix
,
1812 FENDNULL(first_alt
->c_partno
),
1813 "-", buffer2
, NULL
);
1817 if (found_boundary
) {
1818 inform("giving up trying to find a unique boundary");
1823 name
= concat (" ", typename
, "/", subtypename
, boundary_indicator
, "\"",
1824 boundary
, "\"", NULL
);
1826 /* Load c_first_hf and c_last_hf. */
1827 transfer_noncontent_headers (first_alt
, ct
);
1828 add_header (ct
, mh_xstrdup (TYPE_FIELD
), concat (name
, "\n", NULL
));
1831 /* Load c_partno. */
1832 if (first_alt
->c_partno
) {
1833 ct
->c_partno
= mh_xstrdup (first_alt
->c_partno
);
1834 free (first_alt
->c_partno
);
1835 first_alt
->c_partno
= concat (ct
->c_partno
, ".1", NULL
);
1836 new_part
->c_partno
= concat (ct
->c_partno
, ".2", NULL
);
1838 first_alt
->c_partno
= mh_xstrdup ("1");
1839 new_part
->c_partno
= mh_xstrdup ("2");
1843 ct
->c_ctinfo
.ci_type
= mh_xstrdup (typename
);
1844 ct
->c_ctinfo
.ci_subtype
= mh_xstrdup (subtypename
);
1847 add_param(&ct
->c_ctinfo
.ci_first_pm
, &ct
->c_ctinfo
.ci_last_pm
,
1848 "boundary", boundary
, 0);
1852 p
->mp_next
->mp_next
= NULL
;
1853 p
->mp_next
->mp_part
= first_alt
;
1856 m
->mp_start
= concat (boundary
, "\n", NULL
);
1857 m
->mp_stop
= concat (boundary
, "--\n", NULL
);
1873 * Check that the boundary does not appear in the content.
1876 boundary_in_content (FILE **fp
, char *file
, const char *boundary
)
1878 char buffer
[NMH_BUFSIZ
];
1880 bool found_boundary
= false;
1882 /* free_content() will close *fp if we fopen it here. */
1883 if (! *fp
&& (*fp
= fopen (file
, "r")) == NULL
) {
1884 advise (file
, "unable to open %s for reading", file
);
1888 fseeko (*fp
, 0L, SEEK_SET
);
1889 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) > 0) {
1890 if (find_str (buffer
, bytes_read
, boundary
)) {
1891 found_boundary
= true;
1896 return found_boundary
;
1901 * Remove all non-Content headers.
1904 transfer_noncontent_headers (CT old
, CT
new)
1908 hp_prev
= hp
= old
->c_first_hf
;
1912 if (strncasecmp (XXX_FIELD_PRF
, hp
->name
, LEN(XXX_FIELD_PRF
))) {
1913 if (hp
== old
->c_last_hf
) {
1914 if (hp
== old
->c_first_hf
) {
1915 old
->c_last_hf
= old
->c_first_hf
= NULL
;
1917 hp_prev
->next
= NULL
;
1918 old
->c_last_hf
= hp_prev
;
1921 if (hp
== old
->c_first_hf
) {
1922 old
->c_first_hf
= next
;
1924 hp_prev
->next
= next
;
1928 /* Put node hp in the new CT. */
1929 if (new->c_first_hf
== NULL
) {
1930 new->c_first_hf
= hp
;
1932 new->c_last_hf
->next
= hp
;
1934 new->c_last_hf
= hp
;
1936 /* A Content- header, leave in old. */
1949 set_ct_type (CT ct
, int type
, int subtype
, int encoding
)
1951 char *typename
= ct_type_str (type
);
1952 char *subtypename
= ct_subtype_str (type
, subtype
);
1953 /* E.g, " text/plain" */
1954 char *type_subtypename
= concat (" ", typename
, "/", subtypename
, NULL
);
1955 /* E.g, " text/plain\n" */
1956 char *name_plus_nl
= concat (type_subtypename
, "\n", NULL
);
1957 bool found_content_type
= false;
1959 const char *cp
= NULL
;
1963 /* Update/add Content-Type header field. */
1964 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1965 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1966 found_content_type
= true;
1968 hf
->value
= (cp
= strchr (ct
->c_ctline
, ';'))
1969 ? concat (type_subtypename
, cp
, "\n", NULL
)
1970 : mh_xstrdup (name_plus_nl
);
1973 if (! found_content_type
) {
1974 add_header (ct
, mh_xstrdup (TYPE_FIELD
),
1975 (cp
= strchr (ct
->c_ctline
, ';'))
1976 ? concat (type_subtypename
, cp
, "\n", NULL
)
1977 : mh_xstrdup (name_plus_nl
));
1980 /* Some of these might not be used, but set them anyway. */
1982 ? concat (type_subtypename
, cp
, NULL
)
1983 : concat (type_subtypename
, NULL
);
1984 free (ct
->c_ctline
);
1985 ct
->c_ctline
= ctline
;
1986 /* Leave other ctinfo members as they were. */
1987 free (ct
->c_ctinfo
.ci_type
);
1988 ct
->c_ctinfo
.ci_type
= mh_xstrdup (typename
);
1989 free (ct
->c_ctinfo
.ci_subtype
);
1990 ct
->c_ctinfo
.ci_subtype
= mh_xstrdup (subtypename
);
1992 ct
->c_subtype
= subtype
;
1994 free (name_plus_nl
);
1995 free (type_subtypename
);
1997 status
= set_ce (ct
, encoding
);
2004 * It's not necessary to update the charset parameter of a Content-Type
2005 * header for a text part. According to RFC 2045 Sec. 6.4, the body
2006 * (content) was originally in the specified charset, "and will be in
2007 * that character set again after decoding."
2010 decode_text_parts (CT ct
, int encoding
, const char *decodetypes
,
2014 int lf_line_endings
= 0;
2016 switch (ct
->c_type
) {
2017 case CT_MULTIPART
: {
2018 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2021 /* Should check to see if the body for this part is encoded?
2022 For now, it gets passed along as-is by InitMultiPart(). */
2023 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2024 status
= decode_text_parts (part
->mp_part
, encoding
, decodetypes
,
2031 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2032 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2034 status
= decode_text_parts (e
->eb_content
, encoding
, decodetypes
,
2040 if (! should_decode(decodetypes
, ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
)) {
2045 ct
->c_ctparams
&& ((struct text
*) ct
->c_ctparams
)->lf_line_endings
;
2047 switch (ct
->c_encoding
) {
2052 if (decode_part (ct
) == OK
&& ct
->c_cefile
.ce_file
) {
2053 const char *reason
= NULL
;
2055 if ((ct_encoding
= content_encoding (ct
, &reason
)) == CE_BINARY
2056 && encoding
!= CE_BINARY
) {
2057 /* The decoding isn't acceptable so discard it.
2058 Leave status as OK to allow other transformations. */
2060 report (NULL
, ct
->c_partno
, ct
->c_file
,
2061 "will not decode%s because it is binary (%s)",
2063 : (FENDNULL(ct
->c_ctline
)),
2066 (void) m_unlink (ct
->c_cefile
.ce_file
);
2067 free (ct
->c_cefile
.ce_file
);
2068 ct
->c_cefile
.ce_file
= NULL
;
2069 } else if (ct
->c_encoding
== CE_QUOTED
&&
2070 ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
2071 /* The decoding isn't acceptable so discard it.
2072 Leave status as OK to allow other transformations. */
2074 report (NULL
, ct
->c_partno
, ct
->c_file
,
2075 "will not decode%s because it is 8bit",
2077 : (FENDNULL(ct
->c_ctline
)));
2079 (void) m_unlink (ct
->c_cefile
.ce_file
);
2080 free (ct
->c_cefile
.ce_file
);
2081 ct
->c_cefile
.ce_file
= NULL
;
2085 if (ct_encoding
== CE_BINARY
) {
2087 } else if (ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
2092 if (set_ce (ct
, enc
) == OK
) {
2095 report (NULL
, ct
->c_partno
, ct
->c_file
, "decode%s",
2096 FENDNULL(ct
->c_ctline
));
2098 if (lf_line_endings
) {
2099 strip_crs (ct
, message_mods
);
2112 if (lf_line_endings
) {
2113 strip_crs (ct
, message_mods
);
2128 * Determine if the part with type[/subtype] should be decoded, according to
2129 * decodetypes (which came from the -decodetypes switch).
2132 should_decode(const char *decodetypes
, const char *type
, const char *subtype
)
2134 /* Quick search for matching type[/subtype] in decodetypes: bracket
2135 decodetypes with commas, then search for ,type, and ,type/subtype, in
2138 bool found_match
= false;
2139 char *delimited_decodetypes
= concat(",", decodetypes
, ",", NULL
);
2140 char *delimited_type
= concat(",", type
, ",", NULL
);
2142 if (nmh_strcasestr(delimited_decodetypes
, delimited_type
)) {
2144 } else if (subtype
!= NULL
) {
2145 char *delimited_type_subtype
=
2146 concat(",", type
, "/", subtype
, ",", NULL
);
2148 if (nmh_strcasestr(delimited_decodetypes
, delimited_type_subtype
)) {
2151 free(delimited_type_subtype
);
2154 free(delimited_type
);
2155 free(delimited_decodetypes
);
2162 * See if the decoded content is 7bit, 8bit, or binary. It's binary
2163 * if it has any NUL characters, a CR not followed by a LF, or lines
2164 * greater than 998 characters in length. If binary, reason is set
2165 * to a string explaining why.
2168 content_encoding (CT ct
, const char **reason
)
2170 CE ce
= &ct
->c_cefile
;
2171 int encoding
= CE_7BIT
;
2174 size_t line_len
= 0;
2175 char buffer
[NMH_BUFSIZ
];
2178 if (! ce
->ce_fp
&& (ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2179 advise (ce
->ce_file
, "unable to open for reading");
2183 fseeko (ce
->ce_fp
, 0L, SEEK_SET
);
2184 while (encoding
!= CE_BINARY
&&
2185 (inbytes
= fread (buffer
, 1, sizeof buffer
, ce
->ce_fp
)) > 0) {
2188 int last_char_was_cr
= 0;
2190 for (i
= 0, cp
= buffer
; i
< inbytes
; ++i
, ++cp
) {
2191 if (*cp
== '\0' || ++line_len
> 998 ||
2192 (*cp
!= '\n' && last_char_was_cr
)) {
2193 encoding
= CE_BINARY
;
2195 *reason
= "null character";
2196 } else if (line_len
> 998) {
2197 *reason
= "line length > 998";
2198 } else if (*cp
!= '\n' && last_char_was_cr
) {
2199 *reason
= "CR not followed by LF";
2201 /* Should not reach this. */
2208 } else if (! isascii ((unsigned char) *cp
)) {
2212 last_char_was_cr
= *cp
== '\r';
2218 } /* else should never happen */
2225 * Strip carriage returns from content.
2228 strip_crs (CT ct
, int *message_mods
)
2230 char *charset
= content_charset (ct
);
2233 /* Only strip carriage returns if content is ASCII or another
2234 charset that has the same readily recognizable CR followed by a
2235 LF. We can include UTF-8 here because if the high-order bit of
2236 a UTF-8 byte is 0, then it must be a single-byte ASCII
2238 if (! strcasecmp (charset
, "US-ASCII") ||
2239 ! strcasecmp (charset
, "UTF-8") ||
2240 ! strncasecmp (charset
, "ISO-8859-", 9) ||
2241 ! strncasecmp (charset
, "WINDOWS-12", 10)) {
2246 bool has_crs
= false;
2247 bool opened_input_file
= false;
2249 if (ct
->c_cefile
.ce_file
) {
2250 file
= &ct
->c_cefile
.ce_file
;
2251 fp
= &ct
->c_cefile
.ce_fp
;
2253 } else if (ct
->c_file
) {
2256 begin
= (size_t) ct
->c_begin
;
2257 end
= (size_t) ct
->c_end
;
2258 } /* else don't know where the content is */
2260 if (file
&& *file
&& fp
) {
2262 if ((*fp
= fopen (*file
, "r")) == NULL
) {
2263 advise (*file
, "unable to open for reading");
2266 opened_input_file
= true;
2272 char buffer
[NMH_BUFSIZ
];
2274 size_t bytes_to_read
=
2275 end
> 0 && end
> begin
? end
- begin
: sizeof buffer
;
2277 fseeko (*fp
, begin
, SEEK_SET
);
2278 while ((bytes_read
= fread (buffer
, 1,
2279 min (bytes_to_read
, sizeof buffer
),
2281 /* Look for CR followed by a LF. This is supposed to
2282 be text so there should be LF's. If not, don't
2283 modify the content. */
2286 bool last_char_was_cr
= false;
2288 if (end
> 0) { bytes_to_read
-= bytes_read
; }
2290 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
2291 if (*cp
== '\n' && last_char_was_cr
) {
2296 last_char_was_cr
= *cp
== '\r';
2302 char *stripped_content_file
;
2303 char *tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
);
2305 if (tempfile
== NULL
) {
2306 die("unable to create temporary file in %s",
2309 stripped_content_file
= mh_xstrdup (tempfile
);
2311 /* Strip each CR before a LF from the content. */
2312 fseeko (*fp
, begin
, SEEK_SET
);
2313 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) >
2317 bool last_char_was_cr
= false;
2319 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
2321 last_char_was_cr
= true;
2322 } else if (last_char_was_cr
) {
2324 if (write (fd
, "\r", 1) < 0) {
2325 advise (tempfile
, "CR write");
2328 if (write (fd
, cp
, 1) < 0) {
2329 advise (tempfile
, "write");
2331 last_char_was_cr
= false;
2333 if (write (fd
, cp
, 1) < 0) {
2334 advise (tempfile
, "write");
2336 last_char_was_cr
= false;
2342 inform("unable to write temporary file %s, continuing...",
2343 stripped_content_file
);
2344 (void) m_unlink (stripped_content_file
);
2345 free(stripped_content_file
);
2348 /* Replace the decoded file with the converted one. */
2349 if (ct
->c_cefile
.ce_file
&& ct
->c_cefile
.ce_unlink
)
2350 (void) m_unlink (ct
->c_cefile
.ce_file
);
2352 free(ct
->c_cefile
.ce_file
);
2353 ct
->c_cefile
.ce_file
= stripped_content_file
;
2354 ct
->c_cefile
.ce_unlink
= 1;
2358 report (NULL
, ct
->c_partno
,
2359 begin
== 0 && end
== 0 ? "" : *file
,
2365 if (opened_input_file
) {
2379 * Add/update, if necessary, the message C-T-E, based on the least restrictive
2380 * of the part C-T-E's.
2385 const int least_restrictive_enc
= least_restrictive_encoding (ct
);
2387 if (least_restrictive_enc
!= CE_UNKNOWN
&&
2388 least_restrictive_enc
!= CE_7BIT
) {
2389 char *cte
= concat (" ", ce_str (least_restrictive_enc
), "\n", NULL
);
2391 bool found_cte
= false;
2393 /* Update/add Content-Transfer-Encoding header field. */
2394 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
2395 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
2402 add_header (ct
, mh_xstrdup (ENCODING_FIELD
), cte
);
2409 * Find the least restrictive encoding (7bit, 8bit, binary) of the parts
2413 least_restrictive_encoding (CT ct
)
2415 int encoding
= CE_UNKNOWN
;
2417 switch (ct
->c_type
) {
2418 case CT_MULTIPART
: {
2419 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2422 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
2423 const int part_encoding
=
2424 least_restrictive_encoding (part
->mp_part
);
2426 if (less_restrictive (encoding
, part_encoding
)) {
2427 encoding
= part_encoding
;
2434 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2435 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2436 const int part_encoding
=
2437 least_restrictive_encoding (e
->eb_content
);
2439 if (less_restrictive (encoding
, part_encoding
)) {
2440 encoding
= part_encoding
;
2446 if (less_restrictive (encoding
, ct
->c_encoding
)) {
2447 encoding
= ct
->c_encoding
;
2456 * Return whether the second encoding is less restrictive than the first, where
2457 * "less restrictive" is in the sense used by RFC 2045 Secs. 6.1 and 6.4. So,
2458 * CE_BINARY is less restrictive than CE_8BIT and
2459 * CE_8BIT is less restrictive than CE_7BIT.
2462 less_restrictive (int encoding
, int second_encoding
)
2464 switch (second_encoding
) {
2466 return encoding
!= CE_BINARY
;
2468 return encoding
!= CE_BINARY
&& encoding
!= CE_8BIT
;
2470 return encoding
!= CE_BINARY
&& encoding
!= CE_8BIT
&&
2471 encoding
!= CE_7BIT
;
2479 * Convert character set of each part.
2482 convert_charsets (CT ct
, char *dest_charset
, int *message_mods
)
2486 switch (ct
->c_type
) {
2488 if (ct
->c_subtype
== TEXT_PLAIN
) {
2489 status
= convert_charset (ct
, dest_charset
, message_mods
);
2492 char *ct_charset
= content_charset (ct
);
2494 report (NULL
, ct
->c_partno
, ct
->c_file
,
2495 "convert %s to %s", ct_charset
, dest_charset
);
2499 char *ct_charset
= content_charset (ct
);
2501 report ("iconv", ct
->c_partno
, ct
->c_file
,
2502 "failed to convert %s to %s", ct_charset
, dest_charset
);
2508 case CT_MULTIPART
: {
2509 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2512 /* Should check to see if the body for this part is encoded?
2513 For now, it gets passed along as-is by InitMultiPart(). */
2514 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2516 convert_charsets (part
->mp_part
, dest_charset
, message_mods
);
2522 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2523 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2526 convert_charsets (e
->eb_content
, dest_charset
, message_mods
);
2539 * Fix various problems that aren't handled elsewhere. These
2540 * are fixed unconditionally: there are no switches to disable
2541 * them. Currently, "problems" are these:
2542 * 1) remove extraneous semicolon at the end of a header parameter list
2543 * 2) replace RFC 2047 encoding with RFC 2231 encoding of name and
2544 * filename parameters in Content-Type and Content-Disposition
2545 * headers, respectively.
2548 fix_always (CT ct
, int *message_mods
)
2552 switch (ct
->c_type
) {
2553 case CT_MULTIPART
: {
2554 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2557 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2558 status
= fix_always (part
->mp_part
, message_mods
);
2564 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2565 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2567 status
= fix_always (e
->eb_content
, message_mods
);
2574 if (ct
->c_first_hf
) {
2575 fix_filename_encoding (ct
);
2578 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
2579 size_t len
= strlen (hf
->value
);
2581 if (strcasecmp (hf
->name
, TYPE_FIELD
) != 0 &&
2582 strcasecmp (hf
->name
, DISPO_FIELD
) != 0) {
2583 /* Only do this for Content-Type and
2584 Content-Disposition fields because those are the
2585 only headers that parse_mime() warns about. */
2589 /* whitespace following a trailing ';' will be nuked as well */
2590 if (hf
->value
[len
- 1] == '\n') {
2591 while (isspace((unsigned char)(hf
->value
[len
- 2]))) {
2592 if (len
-- == 0) { break; }
2596 if (hf
->value
[len
- 2] == ';') {
2597 /* Remove trailing ';' from parameter value. */
2598 hf
->value
[len
- 2] = '\n';
2599 hf
->value
[len
- 1] = '\0';
2601 /* Also, if Content-Type parameter, remove trailing ';'
2602 from ct->c_ctline. This probably isn't necessary
2604 if (strcasecmp(hf
->name
, TYPE_FIELD
) == 0 && ct
->c_ctline
) {
2605 size_t l
= strlen(ct
->c_ctline
) - 1;
2606 while (isspace((unsigned char)(ct
->c_ctline
[l
])) ||
2607 ct
->c_ctline
[l
] == ';') {
2608 ct
->c_ctline
[l
--] = '\0';
2609 if (l
== 0) { break; }
2615 report (NULL
, ct
->c_partno
, ct
->c_file
,
2616 "remove trailing ; from %s parameter value",
2628 * Factor out common code for loops in fix_filename_encoding().
2631 fix_filename_param (char *name
, char *value
, PM
*first_pm
, PM
*last_pm
)
2635 if (has_prefix(value
, "=?") && has_suffix(value
, "?=")) {
2636 /* Looks like an RFC 2047 encoded parameter. */
2637 char decoded
[PATH_MAX
+ 1];
2639 if (decode_rfc2047 (value
, decoded
, sizeof decoded
)) {
2640 /* Encode using RFC 2231. */
2641 replace_param (first_pm
, last_pm
, name
, decoded
, 0);
2644 inform("failed to decode %s parameter %s", name
, value
);
2653 * Replace RFC 2047 encoding with RFC 2231 encoding of name and
2654 * filename parameters in Content-Type and Content-Disposition
2655 * headers, respectively.
2658 fix_filename_encoding (CT ct
)
2664 for (pm
= ct
->c_ctinfo
.ci_first_pm
; pm
; pm
= pm
->pm_next
) {
2665 if (pm
->pm_name
&& pm
->pm_value
&&
2666 strcasecmp (pm
->pm_name
, "name") == 0) {
2667 fixed
= fix_filename_param (pm
->pm_name
, pm
->pm_value
,
2668 &ct
->c_ctinfo
.ci_first_pm
,
2669 &ct
->c_ctinfo
.ci_last_pm
);
2673 for (pm
= ct
->c_dispo_first
; pm
; pm
= pm
->pm_next
) {
2674 if (pm
->pm_name
&& pm
->pm_value
&&
2675 strcasecmp (pm
->pm_name
, "filename") == 0) {
2676 fixed
= fix_filename_param (pm
->pm_name
, pm
->pm_value
,
2682 /* Fix hf values to correspond. */
2683 for (hf
= ct
->c_first_hf
; fixed
&& hf
; hf
= hf
->next
) {
2684 enum { OTHER
, TYPE_HEADER
, DISPO_HEADER
} field
= OTHER
;
2686 if (strcasecmp (hf
->name
, TYPE_FIELD
) == 0) {
2687 field
= TYPE_HEADER
;
2688 } else if (strcasecmp (hf
->name
, DISPO_FIELD
) == 0) {
2689 field
= DISPO_HEADER
;
2692 if (field
!= OTHER
) {
2693 const char *const semicolon_loc
= strchr (hf
->value
, ';');
2695 if (semicolon_loc
) {
2697 strlen (hf
->name
) + 1 + semicolon_loc
- hf
->value
;
2698 const char *const params
=
2700 field
== TYPE_HEADER
2701 ? ct
->c_ctinfo
.ci_first_pm
2702 : ct
->c_dispo_first
,
2704 const char *const new_params
= concat (params
, "\n", NULL
);
2706 replace_substring (&hf
->value
, semicolon_loc
, new_params
);
2707 free((void *)new_params
); /* Cast away const. Sigh. */
2708 free((void *)params
);
2710 inform("did not find semicolon in %s:%s\n",
2711 hf
->name
, hf
->value
);
2721 * Output content in input file to output file.
2724 write_content (CT ct
, const char *input_filename
, char *outfile
, FILE *outfp
,
2725 int modify_inplace
, int message_mods
)
2729 if (modify_inplace
) {
2730 if (message_mods
> 0) {
2731 if ((status
= output_message_fp (ct
, outfp
, outfile
)) == OK
) {
2732 char *infile
= input_filename
2733 ? mh_xstrdup (input_filename
)
2734 : mh_xstrdup (ct
->c_file
? ct
->c_file
: "-");
2736 if (remove_file (infile
) == OK
) {
2737 if (rename (outfile
, infile
)) {
2738 /* Rename didn't work, possibly because of an
2739 attempt to rename across filesystems. Try
2740 brute force copy. */
2741 int old
= open (outfile
, O_RDONLY
);
2743 open (infile
, O_WRONLY
| O_CREAT
, m_gmprot ());
2746 if (old
!= -1 && new != -1) {
2747 char buffer
[NMH_BUFSIZ
];
2749 while ((i
= read (old
, buffer
, sizeof buffer
)) >
2751 if (write (new, buffer
, i
) != i
) {
2757 if (new != -1) { close (new); }
2758 if (old
!= -1) { close (old
); }
2759 (void) m_unlink (outfile
);
2762 /* The -file argument processing used path() to
2763 expand filename to absolute path. */
2764 int file
= ct
->c_file
&& ct
->c_file
[0] == '/';
2766 inform("unable to rename %s %s to %s, continuing...",
2767 file
? "file" : "message", outfile
,
2773 inform("unable to remove input file %s, "
2774 "not modifying it, continuing...", infile
);
2775 (void) m_unlink (outfile
);
2784 /* No modifications and didn't need the tmp outfile. */
2785 (void) m_unlink (outfile
);
2788 /* Output is going to some file. Produce it whether or not
2789 there were modifications. */
2790 status
= output_message_fp (ct
, outfp
, outfile
);
2799 * parse_mime() does not set lf_line_endings in struct text, so use this
2800 * function to do it. It touches the parts the decodetypes identifies.
2803 set_text_ctparams(CT ct
, char *decodetypes
, int lf_line_endings
)
2805 switch (ct
->c_type
) {
2806 case CT_MULTIPART
: {
2807 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2810 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
2811 set_text_ctparams(part
->mp_part
, decodetypes
, lf_line_endings
);
2817 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2818 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2820 set_text_ctparams(e
->eb_content
, decodetypes
, lf_line_endings
);
2825 if (should_decode(decodetypes
, ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
)) {
2826 if (ct
->c_ctparams
== NULL
) {
2827 ct
->c_ctparams
= mh_xcalloc(1, sizeof (struct text
));
2829 ((struct text
*) ct
->c_ctparams
)->lf_line_endings
= lf_line_endings
;
2836 * If "rmmproc" is defined, call that to remove the file. Otherwise,
2837 * use the standard MH backup file.
2840 remove_file (const char *file
)
2843 char *rmm_command
= concat (rmmproc
, " ", file
, NULL
);
2844 int status
= system (rmm_command
);
2847 return WIFEXITED (status
) ? WEXITSTATUS (status
) : NOTOK
;
2849 /* This is OK for a non-message file, it still uses the
2850 BACKUP_PREFIX form. The backup file will be in the same
2851 directory as file. */
2852 return rename (file
, m_backup (file
));
2857 * Output formatted message to user.
2860 report (char *what
, char *partno
, char *filename
, char *message
, ...)
2866 va_start (args
, message
);
2867 fmt
= concat (filename
, partno
? " part " : ", ",
2868 FENDNULL(partno
), partno
? ", " : "", message
, NULL
);
2870 advertise (what
, NULL
, fmt
, args
);
2883 fprintf (stderr
, "\n");