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/folder_read.h"
10 #include "sbr/context_save.h"
11 #include "sbr/context_replace.h"
12 #include "sbr/context_find.h"
13 #include "sbr/readconfig.h"
14 #include "sbr/ambigsw.h"
16 #include "sbr/print_version.h"
17 #include "sbr/print_help.h"
18 #include "sbr/error.h"
19 #include "h/fmt_scan.h"
21 #include "h/mhparse.h"
24 #include "h/signals.h"
25 #include "sbr/m_maildir.h"
26 #include "sbr/m_mktemp.h"
27 #include "sbr/mime_type.h"
31 #include "mhshowsbr.h"
34 #define MHFIXMSG_SWITCHES \
35 X("decodetext 8bit|7bit|binary", 0, DECODETEXTSW) \
36 X("nodecodetext", 0, NDECODETEXTSW) \
37 X("decodetypes", 0, DECODETYPESW) \
38 X("crlflinebreaks", 0, CRLFLINEBREAKSSW) \
39 X("nocrlflinebreaks", 0, NCRLFLINEBREAKSSW) \
40 X("textcharset", 0, TEXTCHARSETSW) \
41 X("notextcharset", 0, NTEXTCHARSETSW) \
42 X("reformat", 0, REFORMATSW) \
43 X("noreformat", 0, NREFORMATSW) \
44 X("replacetextplain", 0, REPLACETEXTPLAINSW) \
45 X("noreplacetextplain", 0, NREPLACETEXTPLAINSW) \
46 X("fixboundary", 0, FIXBOUNDARYSW) \
47 X("nofixboundary", 0, NFIXBOUNDARYSW) \
48 X("fixcte", 0, FIXCOMPOSITECTESW) \
49 X("nofixcte", 0, NFIXCOMPOSITECTESW) \
50 X("fixtype mimetype", 0, FIXTYPESW) \
51 X("file file", 0, FILESW) \
52 X("outfile file", 0, OUTFILESW) \
53 X("rmmproc program", 0, RPROCSW) \
54 X("normmproc", 0, NRPRCSW) \
55 X("changecur", 0, CHGSW) \
56 X("nochangecur", 0, NCHGSW) \
57 X("verbose", 0, VERBSW) \
58 X("noverbose", 0, NVERBSW) \
59 X("version", 0, VERSIONSW) \
60 X("help", 0, HELPSW) \
62 #define X(sw, minchars, id) id,
63 DEFINE_SWITCH_ENUM(MHFIXMSG
);
66 #define X(sw, minchars, id) { sw, minchars, id },
67 DEFINE_SWITCH_ARRAY(MHFIXMSG
, switches
);
72 int debugsw
; /* Needed by mhparse.c. */
74 #define quitser pipeser
79 typedef struct fix_transformations
{
87 /* Whether to use CRLF linebreaks, per RFC 2046 Sec. 4.1.1, par.1. */
90 } fix_transformations
;
92 static int mhfixmsgsbr (CT
*, char *, const fix_transformations
*,
93 FILE **, char *, FILE **);
94 static int fix_boundary (CT
*, int *);
95 static int copy_input_to_output (const char *, FILE *, const char *, FILE *);
96 static int get_multipart_boundary (CT
, char **);
97 static int replace_boundary (CT
, char *, char *);
98 static int fix_types (CT
, svector_t
, int *);
99 static char *replace_substring (char **, const char *, const char *);
100 static char *remove_parameter (char *, const char *);
101 static int fix_composite_cte (CT
, int *);
102 static int set_ce (CT
, int);
103 static int ensure_text_plain (CT
*, CT
, int *, int);
104 static int find_textplain_sibling (CT
, int, int *);
105 static int insert_new_text_plain_part (CT
, int, CT
);
106 static CT
build_text_plain_part (CT
);
107 static int insert_into_new_mp_alt (CT
*, int *);
108 static CT
divide_part (CT
);
109 static void copy_ctinfo (CI
, CI
);
110 static int decode_part (CT
);
111 static int reformat_part (CT
, char *, char *, char *, int);
112 static CT
build_multipart_alt (CT
, CT
, int, int);
113 static int boundary_in_content (FILE **, char *, const char *);
114 static void transfer_noncontent_headers (CT
, CT
);
115 static int set_ct_type (CT
, int type
, int subtype
, int encoding
);
116 static int decode_text_parts (CT
, int, const char *, int *);
117 static int should_decode(const char *, const char *, const char *);
118 static int content_encoding (CT
, const char **);
119 static int strip_crs (CT
, int *);
120 static void update_cte (CT
);
121 static int least_restrictive_encoding (CT
) PURE
;
122 static int less_restrictive (int, int);
123 static int convert_charsets (CT
, char *, int *);
124 static int fix_always (CT
, int *);
125 static int fix_filename_param (char *, char *, PM
*, PM
*);
126 static int fix_filename_encoding (CT
);
127 static int write_content (CT
, const char *, char *, FILE *, int, int);
128 static void set_text_ctparams(CT
, char *, int);
129 static int remove_file (const char *);
130 static void report (char *, char *, char *, char *, ...)
132 static void pipeser (int);
136 main (int argc
, char **argv
)
139 char *cp
, *file
= NULL
, *folder
= NULL
;
140 char *maildir
= NULL
, buf
[100], *outfile
= NULL
;
141 char **argp
, **arguments
;
142 struct msgs_array msgs
= { 0, 0, NULL
};
143 struct msgs
*mp
= NULL
;
145 FILE *fp
, *infp
= NULL
, *outfp
= NULL
;
146 bool using_stdin
= false;
149 fix_transformations fx
;
150 fx
.reformat
= fx
.fixcompositecte
= fx
.fixboundary
= 1;
152 fx
.replacetextplain
= 0;
153 fx
.decodetext
= CE_8BIT
;
154 fx
.decodetypes
= "text,application/ics"; /* Default, per man page. */
155 fx
.lf_line_endings
= 0;
156 fx
.textcharset
= NULL
;
158 if (nmh_init(argv
[0], true, false)) { return 1; }
160 arguments
= getarguments (invo_name
, argc
, argv
, 1);
166 while ((cp
= *argp
++)) {
168 switch (smatch (++cp
, switches
)) {
170 ambigsw (cp
, switches
);
173 die("-%s unknown", cp
);
176 snprintf (buf
, sizeof buf
, "%s [+folder] [msgs] [switches]",
178 print_help (buf
, switches
, 1);
181 print_version(invo_name
);
185 if (! (cp
= *argp
++) || *cp
== '-') {
186 die("missing argument to %s", argp
[-2]);
188 if (! strcasecmp (cp
, "8bit")) {
189 fx
.decodetext
= CE_8BIT
;
190 } else if (! strcasecmp (cp
, "7bit")) {
191 fx
.decodetext
= CE_7BIT
;
192 } else if (! strcasecmp (cp
, "binary")) {
193 fx
.decodetext
= CE_BINARY
;
195 die("invalid argument to %s", argp
[-2]);
202 if (! (cp
= *argp
++) || *cp
== '-') {
203 die("missing argument to %s", argp
[-2]);
207 case CRLFLINEBREAKSSW
:
208 fx
.lf_line_endings
= 0;
210 case NCRLFLINEBREAKSSW
:
211 fx
.lf_line_endings
= 1;
214 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
215 die("missing argument to %s", argp
[-2]);
228 case FIXCOMPOSITECTESW
:
229 fx
.fixcompositecte
= 1;
231 case NFIXCOMPOSITECTESW
:
232 fx
.fixcompositecte
= 0;
235 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
236 die("missing argument to %s", argp
[-2]);
238 if (! strncasecmp (cp
, "multipart/", 10) ||
239 ! strncasecmp (cp
, "message/", 8))
240 die("-fixtype %s not allowed", cp
);
241 if (! strchr (cp
, '/'))
242 die("-fixtype requires type/subtype");
243 if (fx
.fixtypes
== NULL
) { fx
.fixtypes
= svector_create (10); }
244 svector_push_back (fx
.fixtypes
, cp
);
252 case REPLACETEXTPLAINSW
:
253 fx
.replacetextplain
= 1;
255 case NREPLACETEXTPLAINSW
:
256 fx
.replacetextplain
= 0;
259 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
260 die("missing argument to %s", argp
[-2]);
262 file
= *cp
== '-' ? mh_xstrdup (cp
) : path (cp
, TFILE
);
265 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
266 die("missing argument to %s", argp
[-2]);
268 outfile
= *cp
== '-' ? mh_xstrdup (cp
) : path (cp
, TFILE
);
271 if (!(rmmproc
= *argp
++) || *rmmproc
== '-') {
272 die("missing argument to %s", argp
[-2]);
292 if (*cp
== '+' || *cp
== '@') {
294 die("only one folder at a time!");
295 folder
= pluspath (cp
);
298 /* Interpret a full path as a filename, not a message. */
299 file
= mh_xstrdup (cp
);
301 app_msgarg (&msgs
, cp
);
306 SIGNAL (SIGQUIT
, quitser
);
307 SIGNAL (SIGPIPE
, pipeser
);
310 * Read the standard profile setup
312 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
313 readconfig(NULL
, fp
, cp
, 0);
317 suppress_bogus_mp_content_warning
= skip_mp_cte_check
= true;
318 suppress_extraneous_trailing_semicolon_warning
= true;
320 if (! context_find ("path")) {
321 free (path ("./", TFOLDER
));
324 if (file
&& msgs
.size
) {
325 die("cannot specify msg and file at same time!");
329 /* Open the outfile now, so we don't have to risk opening it
330 after running out of fds. */
331 if (strcmp (outfile
, "-") == 0) {
333 } else if ((outfp
= fopen (outfile
, "w")) == NULL
) {
334 adios (outfile
, "unable to open for writing");
339 * check if message is coming from file
342 /* If file is stdin, create a tmp file name before parse_mime()
343 has a chance, because it might put in on a different
344 filesystem than the output file. Instead, put it in the
345 user's preferred tmp directory. */
348 if (! strcmp ("-", file
)) {
354 if ((cp
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
355 die("unable to create temporary file in %s",
359 file
= mh_xstrdup (cp
);
360 cpydata (STDIN_FILENO
, fd
, "-", file
);
364 (void) m_unlink (file
);
365 die("failed to write temporary file");
369 cts
= mh_xcalloc(2, sizeof *cts
);
372 if ((ct
= parse_mime (file
))) {
373 set_text_ctparams(ct
, fx
.decodetypes
, fx
.lf_line_endings
);
376 inform("unable to parse message from file %s", file
);
379 /* If there's an outfile, pass the input message unchanged, so the
380 message won't get dropped from a pipeline. */
382 /* Something went wrong. Output might be expected, such as if
383 this were run as a filter. Just copy the input to the
385 if ((infp
= fopen (file
, "r")) == NULL
) {
386 adios (file
, "unable to open for reading");
389 if (copy_input_to_output (file
, infp
, outfile
, outfp
) != OK
) {
390 inform("unable to copy message to %s, "
391 "it might be lost\n", outfile
);
400 * message(s) are coming from a folder
405 app_msgarg(&msgs
, "cur");
408 folder
= getfolder (1);
410 maildir
= mh_xstrdup(m_maildir (folder
));
412 /* chdir so that error messages, esp. from MIME parser, just
413 refer to the message and not its path. */
414 if (chdir (maildir
) == NOTOK
) {
415 adios (maildir
, "unable to change directory to");
418 /* read folder and create message structure */
419 if (! (mp
= folder_read (folder
, 1))) {
420 die("unable to read folder %s", folder
);
423 /* check for empty folder */
424 if (mp
->nummsg
== 0) {
425 die("no messages in %s", folder
);
428 /* parse all the message ranges/sequences and set SELECTED */
429 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
430 if (! m_convert (mp
, msgs
.msgs
[msgnum
])) {
433 seq_setprev (mp
); /* set the previous-sequence */
435 cts
= mh_xcalloc(mp
->numsel
+ 1, sizeof *cts
);
438 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
439 if (is_selected(mp
, msgnum
)) {
440 char *msgnam
= m_name (msgnum
);
442 if ((ct
= parse_mime (msgnam
))) {
443 set_text_ctparams(ct
, fx
.decodetypes
, fx
.lf_line_endings
);
446 inform("unable to parse message %s", msgnam
);
449 /* If there's an outfile, pass the input message
450 unchanged, so the message won't get dropped from a
453 /* Something went wrong. Output might be expected,
454 such as if this were run as a filter. Just copy
455 the input to the output. */
456 /* Can't use path() here because 1) it might have been
457 called before and it caches the pwd, and 2) we call
458 chdir() after that. */
459 char *input_filename
=
460 concat (maildir
, "/", msgnam
, NULL
);
462 if ((infp
= fopen (input_filename
, "r")) == NULL
) {
463 adios (input_filename
,
464 "unable to open for reading");
467 if (copy_input_to_output (input_filename
, infp
,
468 outfile
, outfp
) != OK
) {
469 inform("unable to copy message to %s, "
470 "it might be lost\n", outfile
);
475 free (input_filename
);
482 seq_setcur (mp
, mp
->hghsel
); /* update current message */
484 seq_save (mp
); /* synchronize sequences */
485 context_replace (pfolder
, folder
);/* update current folder */
486 context_save (); /* save the context file */
490 for (ctp
= cts
; *ctp
; ++ctp
) {
492 mhfixmsgsbr (ctp
, maildir
, &fx
, &infp
, outfile
, &outfp
) == OK
498 (void) m_unlink (file
);
501 /* Just calling m_backup() unlinks the backup file. */
502 (void) m_backup (file
);
513 if (fx
.fixtypes
!= NULL
) { svector_free (fx
.fixtypes
); }
514 if (infp
) { fclose (infp
); } /* even if stdin */
515 if (outfp
) { fclose (outfp
); } /* even if stdout */
521 done (status
== OK
? 0 : 1);
527 * Apply transformations to one message.
530 mhfixmsgsbr (CT
*ctp
, char *maildir
, const fix_transformations
*fx
,
531 FILE **infp
, char *outfile
, FILE **outfp
)
533 /* Store input filename in case one of the transformations, i.e.,
534 fix_boundary(), rewrites to a tmp file. */
535 char *input_filename
= maildir
536 ? concat (maildir
, "/", (*ctp
)->c_file
, NULL
)
537 : mh_xstrdup ((*ctp
)->c_file
);
538 bool modify_inplace
= false;
539 int message_mods
= 0;
542 /* Though the input file won't need to be opened if everything goes
543 well, do it here just in case there's a failure, and that failure is
544 running out of file descriptors. */
545 if ((*infp
= fopen (input_filename
, "r")) == NULL
) {
546 adios (input_filename
, "unable to open for reading");
549 if (outfile
== NULL
) {
550 modify_inplace
= true;
552 if ((*ctp
)->c_file
) {
554 /* outfp will be closed by the caller */
555 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, outfp
)) ==
557 die("unable to create temporary file in %s",
560 outfile
= mh_xstrdup (tempfile
);
562 die("missing both input and output filenames\n");
564 } /* else *outfp was defined by caller */
566 reverse_alternative_parts (*ctp
);
567 status
= fix_always (*ctp
, &message_mods
);
568 if (status
== OK
&& fx
->fixboundary
) {
569 status
= fix_boundary (ctp
, &message_mods
);
571 if (status
== OK
&& fx
->fixtypes
!= NULL
) {
572 status
= fix_types (*ctp
, fx
->fixtypes
, &message_mods
);
574 if (status
== OK
&& fx
->fixcompositecte
) {
575 status
= fix_composite_cte (*ctp
, &message_mods
);
577 if (status
== OK
&& fx
->reformat
) {
579 ensure_text_plain (ctp
, NULL
, &message_mods
, fx
->replacetextplain
);
581 if (status
== OK
&& fx
->decodetext
) {
582 status
= decode_text_parts (*ctp
, fx
->decodetext
, fx
->decodetypes
,
586 if (status
== OK
&& fx
->textcharset
!= NULL
) {
587 status
= convert_charsets (*ctp
, fx
->textcharset
, &message_mods
);
590 if (status
== OK
&& ! (*ctp
)->c_umask
) {
591 /* Set the umask for the contents file. This currently
592 isn't used but just in case it is in the future. */
595 if (stat ((*ctp
)->c_file
, &st
) != NOTOK
) {
596 (*ctp
)->c_umask
= ~(st
.st_mode
& 0777);
598 (*ctp
)->c_umask
= ~m_gmprot();
603 * Write the content to a file
606 status
= write_content (*ctp
, input_filename
, outfile
, *outfp
,
607 modify_inplace
, message_mods
);
608 } else if (! modify_inplace
) {
609 /* Something went wrong. Output might be expected, such
610 as if this were run as a filter. Just copy the input
612 if (copy_input_to_output (input_filename
, *infp
, outfile
,
614 inform("unable to copy message to %s, it might be lost\n",
619 if (modify_inplace
) {
620 if (status
!= OK
) { (void) m_unlink (outfile
); }
627 free (input_filename
);
634 * Copy input message to output. Assumes not modifying in place, so this
635 * might be running as part of a pipeline.
638 copy_input_to_output (const char *input_filename
, FILE *infp
,
639 const char *output_filename
, FILE *outfp
)
641 int in
= fileno (infp
);
642 int out
= fileno (outfp
);
645 if (in
!= -1 && out
!= -1) {
646 cpydata (in
, out
, input_filename
, output_filename
);
656 * Fix mismatched outer level boundary.
659 fix_boundary (CT
*ct
, int *message_mods
)
661 struct multipart
*mp
;
664 if (ct
&& (*ct
)->c_type
== CT_MULTIPART
&& bogus_mp_content
) {
665 mp
= (struct multipart
*) (*ct
)->c_ctparams
;
668 * 1) Get boundary at end of part.
669 * 2) Get boundary at beginning of part and compare to the end-of-part
671 * 3) Write out contents of ct to tmp file, replacing boundary in
672 * header with boundary from part. Set c_unlink to 1.
674 * 5) Call parse_mime() on the tmp file, replacing ct.
677 if (mp
&& mp
->mp_start
) {
680 if (get_multipart_boundary (*ct
, &part_boundary
) == OK
) {
683 if ((fixed
= m_mktemp2 (NULL
, invo_name
, NULL
, &(*ct
)->c_fp
))) {
684 if (replace_boundary (*ct
, fixed
, part_boundary
) == OK
) {
685 char *filename
= mh_xstrdup ((*ct
)->c_file
);
689 if ((fixed_ct
= parse_mime (fixed
))) {
695 report (NULL
, NULL
, filename
,
696 "fix multipart boundary");
700 inform("unable to parse fixed part");
705 inform("unable to replace broken boundary");
709 inform("unable to create temporary file in %s",
714 free (part_boundary
);
716 /* Couldn't fix the boundary. Report failure so that mhfixmsg
717 doesn't modify the message. */
721 /* No multipart struct, even though the content type is
722 CT_MULTIPART. Report failure so that mhfixmsg doesn't modify
733 * Find boundary at end of multipart.
736 get_multipart_boundary (CT ct
, char **part_boundary
)
738 char buffer
[NMH_BUFSIZ
];
739 char *end_boundary
= NULL
;
740 off_t begin
= (off_t
) ct
->c_end
> (off_t
) (ct
->c_begin
+ sizeof buffer
)
741 ? (off_t
) (ct
->c_end
- sizeof buffer
)
742 : (off_t
) ct
->c_begin
;
746 /* This will fail if the boundary spans fread() calls. NMH_BUFSIZ should
747 be big enough, even if it's just 1024, to make that unlikely. */
749 /* free_content() will close ct->c_fp if bogus MP boundary is fixed. */
750 if (! ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
751 advise (ct
->c_file
, "unable to open for reading");
755 /* Get boundary at end of multipart. */
756 while (begin
>= (off_t
) ct
->c_begin
) {
757 fseeko (ct
->c_fp
, begin
, SEEK_SET
);
758 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
759 char *cp
= rfind_str (buffer
, bytes_read
, "--");
764 /* Trim off trailing "--" and anything beyond. */
766 if ((end
= rfind_str (buffer
, cp
- buffer
, "\n"))) {
767 if (strlen (end
) > 3 && *end
++ == '\n' &&
768 *end
++ == '-' && *end
++ == '-') {
769 end_boundary
= mh_xstrdup (end
);
776 if (end_boundary
|| begin
<= (off_t
) (ct
->c_begin
+ sizeof buffer
))
778 begin
-= sizeof buffer
;
781 /* Get boundary at beginning of multipart. */
783 fseeko (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
784 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
785 if (bytes_read
>= strlen (end_boundary
)) {
786 char *cp
= find_str (buffer
, bytes_read
, end_boundary
);
788 if (cp
&& cp
- buffer
>= 2 && *--cp
== '-' &&
789 *--cp
== '-' && (cp
> buffer
&& *--cp
== '\n')) {
794 /* The start and end boundaries didn't match, or the
795 start boundary doesn't begin with "\n--" (or "--"
796 if at the beginning of buffer). Keep trying. */
810 *part_boundary
= end_boundary
;
812 *part_boundary
= NULL
;
821 * Open and copy ct->c_file to file, replacing the multipart boundary.
824 replace_boundary (CT ct
, char *file
, char *boundary
)
828 char buf
[NMH_BUFSIZ
], name
[NAMESZ
];
830 m_getfld_state_t gstate
;
833 if (ct
->c_file
== NULL
) {
834 inform("missing input filename");
838 if ((fpin
= fopen (ct
->c_file
, "r")) == NULL
) {
839 advise (ct
->c_file
, "unable to open for reading");
843 if ((fpout
= fopen (file
, "w")) == NULL
) {
845 advise (file
, "unable to open for writing");
849 gstate
= m_getfld_state_init(fpin
);
850 for (compnum
= 1;;) {
851 int bufsz
= (int) sizeof buf
;
853 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
858 /* get copies of the buffers */
859 np
= mh_xstrdup (name
);
860 vp
= mh_xstrdup (buf
);
862 /* if necessary, get rest of field */
863 while (state
== FLDPLUS
) {
865 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
866 vp
= add (buf
, vp
); /* add to previous value */
869 if (strcasecmp (TYPE_FIELD
, np
)) {
870 fprintf (fpout
, "%s:%s", np
, vp
);
872 char *new_ctline
, *new_params
;
874 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
875 &ct
->c_ctinfo
.ci_last_pm
, "boundary",
878 new_ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
879 ct
->c_ctinfo
.ci_subtype
, NULL
);
880 new_params
= output_params(LEN(TYPE_FIELD
) +
881 strlen(new_ctline
) + 1,
882 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
883 fprintf (fpout
, "%s:%s%s\n", np
, new_ctline
,
884 FENDNULL(new_params
));
896 /* buf will have a terminating NULL, skip it. */
897 if ((int) fwrite (buf
, 1, bufsz
-1, fpout
) < bufsz
-1) {
898 advise (file
, "fwrite");
907 inform("message format error in component #%d", compnum
);
912 inform("getfld() returned %d", state
);
920 m_getfld_state_destroy (&gstate
);
929 * Fix Content-Type header to reflect the content of its part.
932 fix_types (CT ct
, svector_t fixtypes
, int *message_mods
)
936 switch (ct
->c_type
) {
938 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
941 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
942 status
= fix_types (part
->mp_part
, fixtypes
, message_mods
);
948 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
949 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
951 status
= fix_types (e
->eb_content
, fixtypes
, message_mods
);
958 if (ct
->c_ctinfo
.ci_type
&& ct
->c_ctinfo
.ci_subtype
) {
959 for (typep
= svector_strs (fixtypes
);
960 typep
&& (type
= *typep
);
963 concat (ct
->c_ctinfo
.ci_type
, "/", ct
->c_ctinfo
.ci_subtype
,
966 if (! strcasecmp (type
, type_subtype
) &&
967 decode_part (ct
) == OK
&&
968 ct
->c_cefile
.ce_file
!= NULL
) {
969 char *ct_type_subtype
= mime_type (ct
->c_cefile
.ce_file
);
972 if ((cp
= strchr (ct_type_subtype
, ';'))) {
973 /* Truncate to remove any parameter list from
974 mime_type () result. */
978 if (strcasecmp (type
, ct_type_subtype
)) {
979 char *ct_type
, *ct_subtype
;
982 /* The Content-Type header does not match the
983 content, so update these struct Content
986 * c_ctinfo.ci_type, c_ctinfo.ci_subtype
989 /* Extract type and subtype from type/subtype. */
990 ct_type
= mh_xstrdup(ct_type_subtype
);
991 if ((cp
= strchr (ct_type
, '/'))) {
993 ct_subtype
= mh_xstrdup(++cp
);
995 inform("missing / in MIME type of %s %s",
996 ct
->c_file
, ct
->c_partno
);
1001 ct
->c_type
= ct_str_type (ct_type
);
1002 ct
->c_subtype
= ct_str_subtype (ct
->c_type
, ct_subtype
);
1004 free (ct
->c_ctinfo
.ci_type
);
1005 ct
->c_ctinfo
.ci_type
= ct_type
;
1006 free (ct
->c_ctinfo
.ci_subtype
);
1007 ct
->c_ctinfo
.ci_subtype
= ct_subtype
;
1008 if (! replace_substring (&ct
->c_ctline
, type
,
1010 inform("did not find %s in %s",
1011 type
, ct
->c_ctline
);
1014 /* Update Content-Type header field. */
1015 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1016 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1017 if (replace_substring (&hf
->value
, type
,
1021 report (NULL
, ct
->c_partno
, ct
->c_file
,
1022 "change Content-Type in header "
1024 type
, ct_type_subtype
);
1028 inform("did not find %s in %s", type
, hf
->value
);
1032 free (ct_type_subtype
);
1034 free (type_subtype
);
1044 * Replace a substring, allocating space to hold the new one.
1047 replace_substring (char **str
, const char *old
, const char *new)
1051 if ((cp
= strstr (*str
, old
))) {
1052 char *remainder
= cp
+ strlen (old
);
1053 char *prefix
, *new_str
;
1056 prefix
= mh_xstrdup(*str
);
1057 *(prefix
+ (cp
- *str
)) = '\0';
1058 new_str
= concat (prefix
, new, remainder
, NULL
);
1061 new_str
= concat (new, remainder
, NULL
);
1066 return *str
= new_str
;
1074 * Remove a name=value parameter, given just its name, from a header value.
1077 remove_parameter (char *str
, const char *name
)
1079 /* It looks to me, based on the BNF in RFC 2045, than there can't
1080 be whitespace between the parameter name and the "=", or
1081 between the "=" and the parameter value. */
1082 char *param_name
= concat (name
, "=", NULL
);
1085 if ((cp
= strstr (str
, param_name
))) {
1089 /* Remove any leading spaces, before the parameter name. */
1091 start
> str
&& isspace ((unsigned char) *(start
-1));
1095 /* Remove a leading semicolon. */
1096 if (start
> str
&& *(start
-1) == ';') { --start
; }
1098 end
= cp
+ strlen (name
) + 1;
1100 /* Skip past the quoted value, and then the final quote. */
1101 for (++end
; *end
&& *end
!= '"'; ++end
) { continue; }
1104 /* Skip past the value. */
1105 for (++end
; *end
&& ! isspace ((unsigned char) *end
); ++end
) {}
1108 /* Count how many characters need to be moved. Include
1109 trailing null, which is accounted for by the
1110 initialization of count to 1. */
1111 for (cp
= end
; *cp
; ++cp
) { ++count
; }
1112 (void) memmove (start
, end
, count
);
1122 * Fix Content-Transfer-Encoding of composite,, e.g., message or multipart, part.
1123 * According to RFC 2045 Sec. 6.4, it must be 7bit, 8bit, or binary. Set it to
1127 fix_composite_cte (CT ct
, int *message_mods
)
1131 if (ct
->c_type
== CT_MESSAGE
|| ct
->c_type
== CT_MULTIPART
) {
1132 if (ct
->c_encoding
!= CE_7BIT
&& ct
->c_encoding
!= CE_8BIT
&&
1133 ct
->c_encoding
!= CE_BINARY
) {
1136 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1137 char *name
= hf
->name
;
1138 for (; isspace((unsigned char)*name
); ++name
) {
1142 if (! strncasecmp (name
, ENCODING_FIELD
,
1143 LEN(ENCODING_FIELD
))) {
1144 char *prefix
= "Nmh-REPLACED-INVALID-";
1148 h
->name
= mh_xstrdup (hf
->name
);
1149 h
->hf_encoding
= hf
->hf_encoding
;
1153 /* Retain old header but prefix its name. */
1155 hf
->name
= concat (prefix
, h
->name
, NULL
);
1159 char *encoding
= cpytrim (hf
->value
);
1160 report (NULL
, ct
->c_partno
, ct
->c_file
,
1161 "replace Content-Transfer-Encoding of %s "
1162 "with 8 bit", encoding
);
1166 h
->value
= mh_xstrdup (" 8bit\n");
1168 /* Don't need to warn for multiple C-T-E header
1169 fields, parse_mime() already does that. But
1170 if there are any, fix them all as necessary. */
1175 set_ce (ct
, CE_8BIT
);
1178 if (ct
->c_type
== CT_MULTIPART
) {
1179 struct multipart
*m
;
1182 m
= (struct multipart
*) ct
->c_ctparams
;
1183 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1184 if (fix_composite_cte (part
->mp_part
, message_mods
) != OK
) {
1197 * Set content encoding.
1200 set_ce (CT ct
, int encoding
)
1202 const char *ce
= ce_str (encoding
);
1203 const struct str2init
*ctinit
= get_ce_method (ce
);
1206 char *cte
= concat (" ", ce
, "\n", NULL
);
1207 bool found_cte
= false;
1209 /* Decoded contents might be in ct->c_cefile.ce_file, if the
1210 caller is decode_text_parts (). Save because we'll
1212 struct cefile decoded_content_info
= ct
->c_cefile
;
1214 ct
->c_encoding
= encoding
;
1216 ct
->c_ctinitfnx
= ctinit
->si_init
;
1217 /* This will assign ct->c_cefile with an all-0 struct, which
1219 (*ctinit
->si_init
) (ct
);
1220 /* After returning, the caller should set
1221 ct->c_cefile.ce_file to the name of the file containing
1224 if (ct
->c_ceclosefnx
) {
1225 (*ct
->c_ceclosefnx
) (ct
);
1228 /* Restore the cefile. */
1229 ct
->c_cefile
= decoded_content_info
;
1231 /* Update/add Content-Transfer-Encoding header field. */
1232 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1233 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
1240 add_header (ct
, mh_xstrdup (ENCODING_FIELD
), cte
);
1243 /* Update c_celine. It's used only by mhlist -debug. */
1244 free (ct
->c_celine
);
1245 ct
->c_celine
= mh_xstrdup (cte
);
1255 * Make sure each text part has a corresponding text/plain part.
1258 ensure_text_plain (CT
*ct
, CT parent
, int *message_mods
, int replacetextplain
)
1262 switch ((*ct
)->c_type
) {
1264 /* Nothing to do for text/plain. */
1265 if ((*ct
)->c_subtype
== TEXT_PLAIN
) { return OK
; }
1267 if (parent
&& parent
->c_type
== CT_MULTIPART
&&
1268 parent
->c_subtype
== MULTI_ALTERNATE
) {
1269 int new_subpart_number
= 1;
1270 int has_text_plain
=
1271 find_textplain_sibling (parent
, replacetextplain
,
1272 &new_subpart_number
);
1274 if (! has_text_plain
) {
1275 /* Parent is a multipart/alternative. Insert a new
1276 text/plain subpart. */
1277 const int inserted
=
1278 insert_new_text_plain_part (*ct
, new_subpart_number
,
1283 report (NULL
, parent
->c_partno
, parent
->c_file
,
1284 "insert text/plain part");
1290 } else if (parent
&& parent
->c_type
== CT_MULTIPART
&&
1291 parent
->c_subtype
== MULTI_RELATED
) {
1292 char *type_subtype
=
1293 concat ((*ct
)->c_ctinfo
.ci_type
, "/",
1294 (*ct
)->c_ctinfo
.ci_subtype
, NULL
);
1295 const char *parent_type
=
1296 get_param (parent
->c_ctinfo
.ci_first_pm
, "type", '?', 1);
1297 int new_subpart_number
= 1;
1298 int has_text_plain
= 0;
1300 /* Have to do string comparison on the subtype because we
1301 don't enumerate all of them in c_subtype values.
1302 parent_type will be NULL if the multipart/related part
1303 doesn't have a type parameter. The type parameter must
1304 be specified according to RFC 2387 Sec. 3.1 but not all
1306 if (parent_type
&& strcasecmp (type_subtype
, parent_type
) == 0) {
1307 /* The type of this part matches the root type of the
1308 parent multipart/related. Look to see if there's
1309 text/plain sibling. */
1311 find_textplain_sibling (parent
, replacetextplain
,
1312 &new_subpart_number
);
1315 free (type_subtype
);
1317 if (! has_text_plain
) {
1318 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1322 for (part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
1323 if (*ct
!= part
->mp_part
) {
1329 /* Parent is a multipart/related. Insert a new
1330 text/plain subpart in a new multipart/alternative. */
1331 if (insert_into_new_mp_alt (ct
, message_mods
)) {
1332 /* Not an error if text/plain couldn't be added. */
1335 /* There are no siblings, so insert a new text/plain
1336 subpart, and change the parent type from
1337 multipart/related to multipart/alternative. */
1338 const int inserted
=
1339 insert_new_text_plain_part (*ct
, new_subpart_number
,
1345 parent
->c_subtype
= MULTI_ALTERNATE
;
1346 free (parent
->c_ctinfo
.ci_subtype
);
1347 parent
->c_ctinfo
.ci_subtype
= mh_xstrdup("alternative");
1348 if (! replace_substring (&parent
->c_ctline
, "/related",
1350 inform("did not find multipart/related in %s",
1354 /* Update Content-Type header field. */
1355 for (hf
= parent
->c_first_hf
; hf
; hf
= hf
->next
) {
1356 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1357 if (replace_substring (&hf
->value
, "/related",
1361 report (NULL
, parent
->c_partno
,
1363 "insert text/plain part");
1366 /* Remove, e.g., type="text/html" from
1367 multipart/alternative. */
1368 remove_parameter (hf
->value
, "type");
1371 inform("did not find multipart/"
1372 "related in header %s", hf
->value
);
1376 /* Not an error if text/plain couldn't be inserted. */
1381 if (insert_into_new_mp_alt (ct
, message_mods
)) {
1388 case CT_MULTIPART
: {
1389 struct multipart
*mp
= (struct multipart
*) (*ct
)->c_ctparams
;
1392 for (part
= mp
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1393 if ((*ct
)->c_type
== CT_MULTIPART
) {
1394 status
= ensure_text_plain (&part
->mp_part
, *ct
, message_mods
,
1402 if ((*ct
)->c_subtype
== MESSAGE_EXTERNAL
) {
1403 struct exbody
*e
= (struct exbody
*) (*ct
)->c_ctparams
;
1405 status
= ensure_text_plain (&e
->eb_content
, *ct
, message_mods
,
1416 * See if there is a sibling text/plain, and return its subpart number.
1419 find_textplain_sibling (CT parent
, int replacetextplain
,
1420 int *new_subpart_number
)
1422 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1423 struct part
*part
, *prev
;
1424 bool has_text_plain
= false;
1426 for (prev
= part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
1427 ++*new_subpart_number
;
1428 if (part
->mp_part
->c_type
== CT_TEXT
&&
1429 part
->mp_part
->c_subtype
== TEXT_PLAIN
) {
1430 if (replacetextplain
) {
1431 struct part
*old_part
;
1432 if (part
== mp
->mp_parts
) {
1433 old_part
= mp
->mp_parts
;
1434 mp
->mp_parts
= part
->mp_next
;
1436 old_part
= prev
->mp_next
;
1437 prev
->mp_next
= part
->mp_next
;
1440 report (NULL
, parent
->c_partno
, parent
->c_file
,
1441 "remove text/plain part %s",
1442 old_part
->mp_part
->c_partno
);
1444 free_content (old_part
->mp_part
);
1447 has_text_plain
= true;
1454 return has_text_plain
;
1459 * Insert a new text/plain part.
1462 insert_new_text_plain_part (CT ct
, int new_subpart_number
, CT parent
)
1464 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1465 struct part
*new_part
;
1468 if ((new_part
->mp_part
= build_text_plain_part (ct
))) {
1470 snprintf (buffer
, sizeof buffer
, "%d", new_subpart_number
);
1472 new_part
->mp_next
= mp
->mp_parts
;
1473 mp
->mp_parts
= new_part
;
1474 new_part
->mp_part
->c_partno
=
1475 concat (parent
->c_partno
? parent
->c_partno
: "1", ".",
1481 free_content (new_part
->mp_part
);
1489 * Create a text/plain part to go along with non-plain sibling part.
1492 build_text_plain_part (CT encoded_part
)
1494 CT tp_part
= divide_part (encoded_part
);
1495 char *tmp_plain_file
= NULL
;
1497 if (decode_part (tp_part
) == OK
) {
1498 /* Now, tp_part->c_cefile.ce_file is the name of the tmp file that
1499 contains the decoded contents. And the decoding function, such
1500 as openQuoted, will have set ...->ce_unlink to 1 so that it will
1501 be unlinked by free_content (). */
1504 /* This m_mktemp2() call closes the temp file. */
1505 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1506 inform("unable to create temporary file in %s",
1509 tmp_plain_file
= mh_xstrdup (tempfile
);
1510 if (reformat_part (tp_part
, tmp_plain_file
,
1511 tp_part
->c_ctinfo
.ci_type
,
1512 tp_part
->c_ctinfo
.ci_subtype
,
1513 tp_part
->c_type
) == OK
) {
1519 free_content (tp_part
);
1520 if (tmp_plain_file
) { (void) m_unlink (tmp_plain_file
); }
1521 free (tmp_plain_file
);
1528 * Slip new text/plain part into a new multipart/alternative.
1531 insert_into_new_mp_alt (CT
*ct
, int *message_mods
)
1533 CT tp_part
= build_text_plain_part (*ct
);
1537 CT mp_alt
= build_multipart_alt (*ct
, tp_part
, CT_MULTIPART
,
1540 struct multipart
*mp
= (struct multipart
*) mp_alt
->c_ctparams
;
1542 if (mp
&& mp
->mp_parts
) {
1543 mp
->mp_parts
->mp_part
= tp_part
;
1544 /* Make the new multipart/alternative the parent. */
1549 report (NULL
, (*ct
)->c_partno
, (*ct
)->c_file
,
1550 "insert text/plain part");
1553 free_content (tp_part
);
1554 free_content (mp_alt
);
1561 /* Not an error if text/plain couldn't be built. */
1569 * Clone a MIME part.
1577 /* Just copy over what is needed for decoding. c_vrsn and
1578 c_celine aren't necessary. */
1579 new_part
->c_file
= mh_xstrdup (ct
->c_file
);
1580 new_part
->c_begin
= ct
->c_begin
;
1581 new_part
->c_end
= ct
->c_end
;
1582 copy_ctinfo (&new_part
->c_ctinfo
, &ct
->c_ctinfo
);
1583 new_part
->c_type
= ct
->c_type
;
1584 new_part
->c_cefile
= ct
->c_cefile
;
1585 new_part
->c_encoding
= ct
->c_encoding
;
1586 new_part
->c_ctinitfnx
= ct
->c_ctinitfnx
;
1587 new_part
->c_ceopenfnx
= ct
->c_ceopenfnx
;
1588 new_part
->c_ceclosefnx
= ct
->c_ceclosefnx
;
1589 new_part
->c_cesizefnx
= ct
->c_cesizefnx
;
1591 /* c_ctline is used by reformat__part(), so it can preserve
1592 anything after the type/subtype. */
1593 new_part
->c_ctline
= mh_xstrdup (ct
->c_ctline
);
1600 * Copy the content info from one part to another.
1603 copy_ctinfo (CI dest
, CI src
)
1607 dest
->ci_type
= src
->ci_type
? mh_xstrdup (src
->ci_type
) : NULL
;
1608 dest
->ci_subtype
= src
->ci_subtype
? mh_xstrdup (src
->ci_subtype
) : NULL
;
1610 for (s_pm
= src
->ci_first_pm
; s_pm
; s_pm
= s_pm
->pm_next
) {
1611 d_pm
= add_param(&dest
->ci_first_pm
, &dest
->ci_last_pm
, s_pm
->pm_name
,
1613 if (s_pm
->pm_charset
) {
1614 d_pm
->pm_charset
= mh_xstrdup(s_pm
->pm_charset
);
1616 if (s_pm
->pm_lang
) {
1617 d_pm
->pm_lang
= mh_xstrdup(s_pm
->pm_lang
);
1621 dest
->ci_comment
= src
->ci_comment
? mh_xstrdup (src
->ci_comment
) : NULL
;
1622 dest
->ci_magic
= src
->ci_magic
? mh_xstrdup (src
->ci_magic
) : NULL
;
1637 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, &file
)) == NULL
) {
1638 die("unable to create temporary file in %s", get_temp_dir());
1640 tmp_decoded
= mh_xstrdup (tempfile
);
1641 /* The following call will load ct->c_cefile.ce_file with the tmp
1642 filename of the decoded content. tmp_decoded will contain the
1643 encoded output, get rid of that. */
1644 status
= output_message_fp (ct
, file
, tmp_decoded
);
1645 (void) m_unlink (tmp_decoded
);
1647 if (fclose (file
)) {
1648 inform("unable to close temporary file %s, continuing...", tempfile
);
1656 * Reformat content as plain text.
1657 * Some of the arguments aren't really needed now, but maybe will
1658 * be in the future for other than text types.
1661 reformat_part (CT ct
, char *file
, char *type
, char *subtype
, int c_type
)
1663 int output_subtype
, output_encoding
;
1664 const char *reason
= NULL
;
1668 /* Hacky: this redirects the output from whatever command is used
1669 to show the part to a file. So, the user can't have any output
1670 redirection in that command.
1671 Could show_multi() in mhshowsbr.c avoid this? */
1673 /* Check for invo_name-format-type/subtype. */
1674 if ((cf
= context_find_by_type ("format", type
, subtype
)) == NULL
) {
1676 inform("Don't know how to convert %s, there is no "
1677 "%s-format-%s/%s profile entry",
1678 ct
->c_file
, invo_name
, type
, subtype
);
1682 if (strchr (cf
, '>')) {
1683 inform("'>' prohibited in \"%s\",\nplease fix your "
1684 "%s-format-%s/%s profile entry", cf
, invo_name
, type
,
1690 cp
= concat (cf
, " >", file
, NULL
);
1691 status
= show_content_aux (ct
, 0, cp
, NULL
, NULL
);
1694 /* Unlink decoded content tmp file and free its filename to avoid
1695 leaks. The file stream should already have been closed. */
1696 if (ct
->c_cefile
.ce_unlink
) {
1697 (void) m_unlink (ct
->c_cefile
.ce_file
);
1698 free (ct
->c_cefile
.ce_file
);
1699 ct
->c_cefile
.ce_file
= NULL
;
1700 ct
->c_cefile
.ce_unlink
= 0;
1703 if (c_type
== CT_TEXT
) {
1704 output_subtype
= TEXT_PLAIN
;
1706 /* Set subtype to 0, which is always an UNKNOWN subtype. */
1710 output_encoding
= content_encoding (ct
, &reason
);
1712 set_ct_type (ct
, c_type
, output_subtype
, output_encoding
) == OK
) {
1713 ct
->c_cefile
.ce_file
= file
;
1714 ct
->c_cefile
.ce_unlink
= 1;
1716 ct
->c_cefile
.ce_unlink
= 0;
1725 * Fill in a multipart/alternative part.
1728 build_multipart_alt (CT first_alt
, CT new_part
, int type
, int subtype
)
1730 char *boundary_prefix
= "----=_nmh-multipart";
1731 char *boundary
= concat (boundary_prefix
, first_alt
->c_partno
, NULL
);
1732 char *boundary_indicator
= "; boundary=";
1733 char *typename
, *subtypename
, *name
;
1736 struct multipart
*m
;
1737 const struct str2init
*ctinit
;
1741 /* Set up the multipart/alternative part. These fields of *ct were
1742 initialized to 0 by mh_xcalloc():
1743 c_fp, c_unlink, c_begin, c_end,
1744 c_vrsn, c_ctline, c_celine,
1745 c_id, c_descr, c_dispo, c_partno,
1746 c_ctinfo.ci_comment, c_ctinfo.ci_magic,
1747 c_cefile, c_encoding,
1748 c_digested, c_digest[16], c_ctexbody,
1749 c_ctinitfnx, c_ceopenfnx, c_ceclosefnx, c_cesizefnx,
1751 c_showproc, c_termproc, c_storeproc, c_storage, c_folder
1754 ct
->c_file
= mh_xstrdup (first_alt
->c_file
);
1756 ct
->c_subtype
= subtype
;
1758 ctinit
= get_ct_init (ct
->c_type
);
1760 typename
= ct_type_str (type
);
1761 subtypename
= ct_subtype_str (type
, subtype
);
1765 int found_boundary
= 1;
1767 while (found_boundary
&& serial
< 1000000) {
1770 /* Ensure that the boundary doesn't appear in the decoded
1772 if (new_part
->c_cefile
.ce_file
) {
1773 if ((found_boundary
=
1774 boundary_in_content (&new_part
->c_cefile
.ce_fp
,
1775 new_part
->c_cefile
.ce_file
,
1776 boundary
)) == NOTOK
) {
1781 /* Ensure that the boundary doesn't appear in the encoded
1783 if (! found_boundary
&& new_part
->c_file
) {
1784 if ((found_boundary
=
1785 boundary_in_content (&new_part
->c_fp
,
1787 boundary
)) == NOTOK
) {
1792 if (found_boundary
) {
1793 /* Try a slightly different boundary. */
1798 snprintf (buffer2
, sizeof buffer2
, "%d", serial
);
1800 concat (boundary_prefix
,
1801 FENDNULL(first_alt
->c_partno
),
1802 "-", buffer2
, NULL
);
1806 if (found_boundary
) {
1807 inform("giving up trying to find a unique boundary");
1812 name
= concat (" ", typename
, "/", subtypename
, boundary_indicator
, "\"",
1813 boundary
, "\"", NULL
);
1815 /* Load c_first_hf and c_last_hf. */
1816 transfer_noncontent_headers (first_alt
, ct
);
1817 add_header (ct
, mh_xstrdup (TYPE_FIELD
), concat (name
, "\n", NULL
));
1820 /* Load c_partno. */
1821 if (first_alt
->c_partno
) {
1822 ct
->c_partno
= mh_xstrdup (first_alt
->c_partno
);
1823 free (first_alt
->c_partno
);
1824 first_alt
->c_partno
= concat (ct
->c_partno
, ".1", NULL
);
1825 new_part
->c_partno
= concat (ct
->c_partno
, ".2", NULL
);
1827 first_alt
->c_partno
= mh_xstrdup ("1");
1828 new_part
->c_partno
= mh_xstrdup ("2");
1832 ct
->c_ctinfo
.ci_type
= mh_xstrdup (typename
);
1833 ct
->c_ctinfo
.ci_subtype
= mh_xstrdup (subtypename
);
1836 add_param(&ct
->c_ctinfo
.ci_first_pm
, &ct
->c_ctinfo
.ci_last_pm
,
1837 "boundary", boundary
, 0);
1841 p
->mp_next
->mp_next
= NULL
;
1842 p
->mp_next
->mp_part
= first_alt
;
1845 m
->mp_start
= concat (boundary
, "\n", NULL
);
1846 m
->mp_stop
= concat (boundary
, "--\n", NULL
);
1862 * Check that the boundary does not appear in the content.
1865 boundary_in_content (FILE **fp
, char *file
, const char *boundary
)
1867 char buffer
[NMH_BUFSIZ
];
1869 bool found_boundary
= false;
1871 /* free_content() will close *fp if we fopen it here. */
1872 if (! *fp
&& (*fp
= fopen (file
, "r")) == NULL
) {
1873 advise (file
, "unable to open %s for reading", file
);
1877 fseeko (*fp
, 0L, SEEK_SET
);
1878 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) > 0) {
1879 if (find_str (buffer
, bytes_read
, boundary
)) {
1880 found_boundary
= true;
1885 return found_boundary
;
1890 * Remove all non-Content headers.
1893 transfer_noncontent_headers (CT old
, CT
new)
1897 hp_prev
= hp
= old
->c_first_hf
;
1901 if (strncasecmp (XXX_FIELD_PRF
, hp
->name
, LEN(XXX_FIELD_PRF
))) {
1902 if (hp
== old
->c_last_hf
) {
1903 if (hp
== old
->c_first_hf
) {
1904 old
->c_last_hf
= old
->c_first_hf
= NULL
;
1906 hp_prev
->next
= NULL
;
1907 old
->c_last_hf
= hp_prev
;
1910 if (hp
== old
->c_first_hf
) {
1911 old
->c_first_hf
= next
;
1913 hp_prev
->next
= next
;
1917 /* Put node hp in the new CT. */
1918 if (new->c_first_hf
== NULL
) {
1919 new->c_first_hf
= hp
;
1921 new->c_last_hf
->next
= hp
;
1923 new->c_last_hf
= hp
;
1925 /* A Content- header, leave in old. */
1938 set_ct_type (CT ct
, int type
, int subtype
, int encoding
)
1940 char *typename
= ct_type_str (type
);
1941 char *subtypename
= ct_subtype_str (type
, subtype
);
1942 /* E.g, " text/plain" */
1943 char *type_subtypename
= concat (" ", typename
, "/", subtypename
, NULL
);
1944 /* E.g, " text/plain\n" */
1945 char *name_plus_nl
= concat (type_subtypename
, "\n", NULL
);
1946 bool found_content_type
= false;
1948 const char *cp
= NULL
;
1952 /* Update/add Content-Type header field. */
1953 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1954 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1955 found_content_type
= true;
1957 hf
->value
= (cp
= strchr (ct
->c_ctline
, ';'))
1958 ? concat (type_subtypename
, cp
, "\n", NULL
)
1959 : mh_xstrdup (name_plus_nl
);
1962 if (! found_content_type
) {
1963 add_header (ct
, mh_xstrdup (TYPE_FIELD
),
1964 (cp
= strchr (ct
->c_ctline
, ';'))
1965 ? concat (type_subtypename
, cp
, "\n", NULL
)
1966 : mh_xstrdup (name_plus_nl
));
1969 /* Some of these might not be used, but set them anyway. */
1971 ? concat (type_subtypename
, cp
, NULL
)
1972 : concat (type_subtypename
, NULL
);
1973 free (ct
->c_ctline
);
1974 ct
->c_ctline
= ctline
;
1975 /* Leave other ctinfo members as they were. */
1976 free (ct
->c_ctinfo
.ci_type
);
1977 ct
->c_ctinfo
.ci_type
= mh_xstrdup (typename
);
1978 free (ct
->c_ctinfo
.ci_subtype
);
1979 ct
->c_ctinfo
.ci_subtype
= mh_xstrdup (subtypename
);
1981 ct
->c_subtype
= subtype
;
1983 free (name_plus_nl
);
1984 free (type_subtypename
);
1986 status
= set_ce (ct
, encoding
);
1993 * It's not necessary to update the charset parameter of a Content-Type
1994 * header for a text part. According to RFC 2045 Sec. 6.4, the body
1995 * (content) was originally in the specified charset, "and will be in
1996 * that character set again after decoding."
1999 decode_text_parts (CT ct
, int encoding
, const char *decodetypes
,
2003 int lf_line_endings
= 0;
2005 switch (ct
->c_type
) {
2006 case CT_MULTIPART
: {
2007 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2010 /* Should check to see if the body for this part is encoded?
2011 For now, it gets passed along as-is by InitMultiPart(). */
2012 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2013 status
= decode_text_parts (part
->mp_part
, encoding
, decodetypes
,
2020 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2021 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2023 status
= decode_text_parts (e
->eb_content
, encoding
, decodetypes
,
2029 if (! should_decode(decodetypes
, ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
)) {
2034 ct
->c_ctparams
&& ((struct text
*) ct
->c_ctparams
)->lf_line_endings
;
2036 switch (ct
->c_encoding
) {
2041 if (decode_part (ct
) == OK
&& ct
->c_cefile
.ce_file
) {
2042 const char *reason
= NULL
;
2044 if ((ct_encoding
= content_encoding (ct
, &reason
)) == CE_BINARY
2045 && encoding
!= CE_BINARY
) {
2046 /* The decoding isn't acceptable so discard it.
2047 Leave status as OK to allow other transformations. */
2049 report (NULL
, ct
->c_partno
, ct
->c_file
,
2050 "will not decode%s because it is binary (%s)",
2052 : (FENDNULL(ct
->c_ctline
)),
2055 (void) m_unlink (ct
->c_cefile
.ce_file
);
2056 free (ct
->c_cefile
.ce_file
);
2057 ct
->c_cefile
.ce_file
= NULL
;
2058 } else if (ct
->c_encoding
== CE_QUOTED
&&
2059 ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
2060 /* The decoding isn't acceptable so discard it.
2061 Leave status as OK to allow other transformations. */
2063 report (NULL
, ct
->c_partno
, ct
->c_file
,
2064 "will not decode%s because it is 8bit",
2066 : (FENDNULL(ct
->c_ctline
)));
2068 (void) m_unlink (ct
->c_cefile
.ce_file
);
2069 free (ct
->c_cefile
.ce_file
);
2070 ct
->c_cefile
.ce_file
= NULL
;
2074 if (ct_encoding
== CE_BINARY
) {
2076 } else if (ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
2081 if (set_ce (ct
, enc
) == OK
) {
2084 report (NULL
, ct
->c_partno
, ct
->c_file
, "decode%s",
2085 FENDNULL(ct
->c_ctline
));
2087 if (lf_line_endings
) {
2088 strip_crs (ct
, message_mods
);
2101 if (lf_line_endings
) {
2102 strip_crs (ct
, message_mods
);
2117 * Determine if the part with type[/subtype] should be decoded, according to
2118 * decodetypes (which came from the -decodetypes switch).
2121 should_decode(const char *decodetypes
, const char *type
, const char *subtype
)
2123 /* Quick search for matching type[/subtype] in decodetypes: bracket
2124 decodetypes with commas, then search for ,type, and ,type/subtype, in
2127 bool found_match
= false;
2128 char *delimited_decodetypes
= concat(",", decodetypes
, ",", NULL
);
2129 char *delimited_type
= concat(",", type
, ",", NULL
);
2131 if (nmh_strcasestr(delimited_decodetypes
, delimited_type
)) {
2133 } else if (subtype
!= NULL
) {
2134 char *delimited_type_subtype
=
2135 concat(",", type
, "/", subtype
, ",", NULL
);
2137 if (nmh_strcasestr(delimited_decodetypes
, delimited_type_subtype
)) {
2140 free(delimited_type_subtype
);
2143 free(delimited_type
);
2144 free(delimited_decodetypes
);
2151 * See if the decoded content is 7bit, 8bit, or binary. It's binary
2152 * if it has any NUL characters, a CR not followed by a LF, or lines
2153 * greater than 998 characters in length. If binary, reason is set
2154 * to a string explaining why.
2157 content_encoding (CT ct
, const char **reason
)
2159 CE ce
= &ct
->c_cefile
;
2160 int encoding
= CE_7BIT
;
2163 size_t line_len
= 0;
2164 char buffer
[NMH_BUFSIZ
];
2167 if (! ce
->ce_fp
&& (ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2168 advise (ce
->ce_file
, "unable to open for reading");
2172 fseeko (ce
->ce_fp
, 0L, SEEK_SET
);
2173 while (encoding
!= CE_BINARY
&&
2174 (inbytes
= fread (buffer
, 1, sizeof buffer
, ce
->ce_fp
)) > 0) {
2177 int last_char_was_cr
= 0;
2179 for (i
= 0, cp
= buffer
; i
< inbytes
; ++i
, ++cp
) {
2180 if (*cp
== '\0' || ++line_len
> 998 ||
2181 (*cp
!= '\n' && last_char_was_cr
)) {
2182 encoding
= CE_BINARY
;
2184 *reason
= "null character";
2185 } else if (line_len
> 998) {
2186 *reason
= "line length > 998";
2187 } else if (*cp
!= '\n' && last_char_was_cr
) {
2188 *reason
= "CR not followed by LF";
2190 /* Should not reach this. */
2197 } else if (! isascii ((unsigned char) *cp
)) {
2201 last_char_was_cr
= *cp
== '\r';
2207 } /* else should never happen */
2214 * Strip carriage returns from content.
2217 strip_crs (CT ct
, int *message_mods
)
2219 char *charset
= content_charset (ct
);
2222 /* Only strip carriage returns if content is ASCII or another
2223 charset that has the same readily recognizable CR followed by a
2224 LF. We can include UTF-8 here because if the high-order bit of
2225 a UTF-8 byte is 0, then it must be a single-byte ASCII
2227 if (! strcasecmp (charset
, "US-ASCII") ||
2228 ! strcasecmp (charset
, "UTF-8") ||
2229 ! strncasecmp (charset
, "ISO-8859-", 9) ||
2230 ! strncasecmp (charset
, "WINDOWS-12", 10)) {
2235 bool has_crs
= false;
2236 bool opened_input_file
= false;
2238 if (ct
->c_cefile
.ce_file
) {
2239 file
= &ct
->c_cefile
.ce_file
;
2240 fp
= &ct
->c_cefile
.ce_fp
;
2242 } else if (ct
->c_file
) {
2245 begin
= (size_t) ct
->c_begin
;
2246 end
= (size_t) ct
->c_end
;
2247 } /* else don't know where the content is */
2249 if (file
&& *file
&& fp
) {
2251 if ((*fp
= fopen (*file
, "r")) == NULL
) {
2252 advise (*file
, "unable to open for reading");
2255 opened_input_file
= true;
2261 char buffer
[NMH_BUFSIZ
];
2263 size_t bytes_to_read
=
2264 end
> 0 && end
> begin
? end
- begin
: sizeof buffer
;
2266 fseeko (*fp
, begin
, SEEK_SET
);
2267 while ((bytes_read
= fread (buffer
, 1,
2268 min (bytes_to_read
, sizeof buffer
),
2270 /* Look for CR followed by a LF. This is supposed to
2271 be text so there should be LF's. If not, don't
2272 modify the content. */
2275 bool last_char_was_cr
= false;
2277 if (end
> 0) { bytes_to_read
-= bytes_read
; }
2279 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
2280 if (*cp
== '\n' && last_char_was_cr
) {
2285 last_char_was_cr
= *cp
== '\r';
2291 char *stripped_content_file
;
2292 char *tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
);
2294 if (tempfile
== NULL
) {
2295 die("unable to create temporary file in %s",
2298 stripped_content_file
= mh_xstrdup (tempfile
);
2300 /* Strip each CR before a LF from the content. */
2301 fseeko (*fp
, begin
, SEEK_SET
);
2302 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) >
2306 bool last_char_was_cr
= false;
2308 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
2310 last_char_was_cr
= true;
2311 } else if (last_char_was_cr
) {
2313 if (write (fd
, "\r", 1) < 0) {
2314 advise (tempfile
, "CR write");
2317 if (write (fd
, cp
, 1) < 0) {
2318 advise (tempfile
, "write");
2320 last_char_was_cr
= false;
2322 if (write (fd
, cp
, 1) < 0) {
2323 advise (tempfile
, "write");
2325 last_char_was_cr
= false;
2331 inform("unable to write temporary file %s, continuing...",
2332 stripped_content_file
);
2333 (void) m_unlink (stripped_content_file
);
2334 free(stripped_content_file
);
2337 /* Replace the decoded file with the converted one. */
2338 if (ct
->c_cefile
.ce_file
&& ct
->c_cefile
.ce_unlink
)
2339 (void) m_unlink (ct
->c_cefile
.ce_file
);
2341 free(ct
->c_cefile
.ce_file
);
2342 ct
->c_cefile
.ce_file
= stripped_content_file
;
2343 ct
->c_cefile
.ce_unlink
= 1;
2347 report (NULL
, ct
->c_partno
,
2348 begin
== 0 && end
== 0 ? "" : *file
,
2354 if (opened_input_file
) {
2368 * Add/update, if necessary, the message C-T-E, based on the least restrictive
2369 * of the part C-T-E's.
2374 const int least_restrictive_enc
= least_restrictive_encoding (ct
);
2376 if (least_restrictive_enc
!= CE_UNKNOWN
&&
2377 least_restrictive_enc
!= CE_7BIT
) {
2378 char *cte
= concat (" ", ce_str (least_restrictive_enc
), "\n", NULL
);
2380 bool found_cte
= false;
2382 /* Update/add Content-Transfer-Encoding header field. */
2383 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
2384 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
2391 add_header (ct
, mh_xstrdup (ENCODING_FIELD
), cte
);
2398 * Find the least restrictive encoding (7bit, 8bit, binary) of the parts
2402 least_restrictive_encoding (CT ct
)
2404 int encoding
= CE_UNKNOWN
;
2406 switch (ct
->c_type
) {
2407 case CT_MULTIPART
: {
2408 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2411 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
2412 const int part_encoding
=
2413 least_restrictive_encoding (part
->mp_part
);
2415 if (less_restrictive (encoding
, part_encoding
)) {
2416 encoding
= part_encoding
;
2423 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2424 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2425 const int part_encoding
=
2426 least_restrictive_encoding (e
->eb_content
);
2428 if (less_restrictive (encoding
, part_encoding
)) {
2429 encoding
= part_encoding
;
2435 if (less_restrictive (encoding
, ct
->c_encoding
)) {
2436 encoding
= ct
->c_encoding
;
2445 * Return whether the second encoding is less restrictive than the first, where
2446 * "less restrictive" is in the sense used by RFC 2045 Secs. 6.1 and 6.4. So,
2447 * CE_BINARY is less restrictive than CE_8BIT and
2448 * CE_8BIT is less restrictive than CE_7BIT.
2451 less_restrictive (int encoding
, int second_encoding
)
2453 switch (second_encoding
) {
2455 return encoding
!= CE_BINARY
;
2457 return encoding
!= CE_BINARY
&& encoding
!= CE_8BIT
;
2459 return encoding
!= CE_BINARY
&& encoding
!= CE_8BIT
&&
2460 encoding
!= CE_7BIT
;
2468 * Convert character set of each part.
2471 convert_charsets (CT ct
, char *dest_charset
, int *message_mods
)
2475 switch (ct
->c_type
) {
2477 if (ct
->c_subtype
== TEXT_PLAIN
) {
2478 status
= convert_charset (ct
, dest_charset
, message_mods
);
2481 char *ct_charset
= content_charset (ct
);
2483 report (NULL
, ct
->c_partno
, ct
->c_file
,
2484 "convert %s to %s", ct_charset
, dest_charset
);
2488 char *ct_charset
= content_charset (ct
);
2490 report ("iconv", ct
->c_partno
, ct
->c_file
,
2491 "failed to convert %s to %s", ct_charset
, dest_charset
);
2497 case CT_MULTIPART
: {
2498 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2501 /* Should check to see if the body for this part is encoded?
2502 For now, it gets passed along as-is by InitMultiPart(). */
2503 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2505 convert_charsets (part
->mp_part
, dest_charset
, message_mods
);
2511 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2512 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2515 convert_charsets (e
->eb_content
, dest_charset
, message_mods
);
2528 * Fix various problems that aren't handled elsewhere. These
2529 * are fixed unconditionally: there are no switches to disable
2530 * them. Currently, "problems" are these:
2531 * 1) remove extraneous semicolon at the end of a header parameter list
2532 * 2) replace RFC 2047 encoding with RFC 2231 encoding of name and
2533 * filename parameters in Content-Type and Content-Disposition
2534 * headers, respectively.
2537 fix_always (CT ct
, int *message_mods
)
2541 switch (ct
->c_type
) {
2542 case CT_MULTIPART
: {
2543 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2546 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2547 status
= fix_always (part
->mp_part
, message_mods
);
2553 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2554 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2556 status
= fix_always (e
->eb_content
, message_mods
);
2563 if (ct
->c_first_hf
) {
2564 fix_filename_encoding (ct
);
2567 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
2568 size_t len
= strlen (hf
->value
);
2570 if (strcasecmp (hf
->name
, TYPE_FIELD
) != 0 &&
2571 strcasecmp (hf
->name
, DISPO_FIELD
) != 0) {
2572 /* Only do this for Content-Type and
2573 Content-Disposition fields because those are the
2574 only headers that parse_mime() warns about. */
2578 /* whitespace following a trailing ';' will be nuked as well */
2579 if (hf
->value
[len
- 1] == '\n') {
2580 while (isspace((unsigned char)(hf
->value
[len
- 2]))) {
2581 if (len
-- == 0) { break; }
2585 if (hf
->value
[len
- 2] == ';') {
2586 /* Remove trailing ';' from parameter value. */
2587 hf
->value
[len
- 2] = '\n';
2588 hf
->value
[len
- 1] = '\0';
2590 /* Also, if Content-Type parameter, remove trailing ';'
2591 from ct->c_ctline. This probably isn't necessary
2593 if (strcasecmp(hf
->name
, TYPE_FIELD
) == 0 && ct
->c_ctline
) {
2594 size_t l
= strlen(ct
->c_ctline
) - 1;
2595 while (isspace((unsigned char)(ct
->c_ctline
[l
])) ||
2596 ct
->c_ctline
[l
] == ';') {
2597 ct
->c_ctline
[l
--] = '\0';
2598 if (l
== 0) { break; }
2604 report (NULL
, ct
->c_partno
, ct
->c_file
,
2605 "remove trailing ; from %s parameter value",
2617 * Factor out common code for loops in fix_filename_encoding().
2620 fix_filename_param (char *name
, char *value
, PM
*first_pm
, PM
*last_pm
)
2624 if (has_prefix(value
, "=?") && has_suffix(value
, "?=")) {
2625 /* Looks like an RFC 2047 encoded parameter. */
2626 char decoded
[PATH_MAX
+ 1];
2628 if (decode_rfc2047 (value
, decoded
, sizeof decoded
)) {
2629 /* Encode using RFC 2231. */
2630 replace_param (first_pm
, last_pm
, name
, decoded
, 0);
2633 inform("failed to decode %s parameter %s", name
, value
);
2642 * Replace RFC 2047 encoding with RFC 2231 encoding of name and
2643 * filename parameters in Content-Type and Content-Disposition
2644 * headers, respectively.
2647 fix_filename_encoding (CT ct
)
2653 for (pm
= ct
->c_ctinfo
.ci_first_pm
; pm
; pm
= pm
->pm_next
) {
2654 if (pm
->pm_name
&& pm
->pm_value
&&
2655 strcasecmp (pm
->pm_name
, "name") == 0) {
2656 fixed
= fix_filename_param (pm
->pm_name
, pm
->pm_value
,
2657 &ct
->c_ctinfo
.ci_first_pm
,
2658 &ct
->c_ctinfo
.ci_last_pm
);
2662 for (pm
= ct
->c_dispo_first
; pm
; pm
= pm
->pm_next
) {
2663 if (pm
->pm_name
&& pm
->pm_value
&&
2664 strcasecmp (pm
->pm_name
, "filename") == 0) {
2665 fixed
= fix_filename_param (pm
->pm_name
, pm
->pm_value
,
2671 /* Fix hf values to correspond. */
2672 for (hf
= ct
->c_first_hf
; fixed
&& hf
; hf
= hf
->next
) {
2673 enum { OTHER
, TYPE_HEADER
, DISPO_HEADER
} field
= OTHER
;
2675 if (strcasecmp (hf
->name
, TYPE_FIELD
) == 0) {
2676 field
= TYPE_HEADER
;
2677 } else if (strcasecmp (hf
->name
, DISPO_FIELD
) == 0) {
2678 field
= DISPO_HEADER
;
2681 if (field
!= OTHER
) {
2682 const char *const semicolon_loc
= strchr (hf
->value
, ';');
2684 if (semicolon_loc
) {
2686 strlen (hf
->name
) + 1 + semicolon_loc
- hf
->value
;
2687 const char *const params
=
2689 field
== TYPE_HEADER
2690 ? ct
->c_ctinfo
.ci_first_pm
2691 : ct
->c_dispo_first
,
2693 const char *const new_params
= concat (params
, "\n", NULL
);
2695 replace_substring (&hf
->value
, semicolon_loc
, new_params
);
2696 free((void *)new_params
); /* Cast away const. Sigh. */
2697 free((void *)params
);
2699 inform("did not find semicolon in %s:%s\n",
2700 hf
->name
, hf
->value
);
2710 * Output content in input file to output file.
2713 write_content (CT ct
, const char *input_filename
, char *outfile
, FILE *outfp
,
2714 int modify_inplace
, int message_mods
)
2718 if (modify_inplace
) {
2719 if (message_mods
> 0) {
2720 if ((status
= output_message_fp (ct
, outfp
, outfile
)) == OK
) {
2721 char *infile
= input_filename
2722 ? mh_xstrdup (input_filename
)
2723 : mh_xstrdup (ct
->c_file
? ct
->c_file
: "-");
2725 if (remove_file (infile
) == OK
) {
2726 if (rename (outfile
, infile
)) {
2727 /* Rename didn't work, possibly because of an
2728 attempt to rename across filesystems. Try
2729 brute force copy. */
2730 int old
= open (outfile
, O_RDONLY
);
2732 open (infile
, O_WRONLY
| O_CREAT
, m_gmprot ());
2735 if (old
!= -1 && new != -1) {
2736 char buffer
[NMH_BUFSIZ
];
2738 while ((i
= read (old
, buffer
, sizeof buffer
)) >
2740 if (write (new, buffer
, i
) != i
) {
2746 if (new != -1) { close (new); }
2747 if (old
!= -1) { close (old
); }
2748 (void) m_unlink (outfile
);
2751 /* The -file argument processing used path() to
2752 expand filename to absolute path. */
2753 int file
= ct
->c_file
&& ct
->c_file
[0] == '/';
2755 inform("unable to rename %s %s to %s, continuing...",
2756 file
? "file" : "message", outfile
,
2762 inform("unable to remove input file %s, "
2763 "not modifying it, continuing...", infile
);
2764 (void) m_unlink (outfile
);
2773 /* No modifications and didn't need the tmp outfile. */
2774 (void) m_unlink (outfile
);
2777 /* Output is going to some file. Produce it whether or not
2778 there were modifications. */
2779 status
= output_message_fp (ct
, outfp
, outfile
);
2788 * parse_mime() does not set lf_line_endings in struct text, so use this
2789 * function to do it. It touches the parts the decodetypes identifies.
2792 set_text_ctparams(CT ct
, char *decodetypes
, int lf_line_endings
)
2794 switch (ct
->c_type
) {
2795 case CT_MULTIPART
: {
2796 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2799 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
2800 set_text_ctparams(part
->mp_part
, decodetypes
, lf_line_endings
);
2806 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2807 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2809 set_text_ctparams(e
->eb_content
, decodetypes
, lf_line_endings
);
2814 if (should_decode(decodetypes
, ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
)) {
2815 if (ct
->c_ctparams
== NULL
) {
2816 ct
->c_ctparams
= mh_xcalloc(1, sizeof (struct text
));
2818 ((struct text
*) ct
->c_ctparams
)->lf_line_endings
= lf_line_endings
;
2825 * If "rmmproc" is defined, call that to remove the file. Otherwise,
2826 * use the standard MH backup file.
2829 remove_file (const char *file
)
2832 char *rmm_command
= concat (rmmproc
, " ", file
, NULL
);
2833 int status
= system (rmm_command
);
2836 return WIFEXITED (status
) ? WEXITSTATUS (status
) : NOTOK
;
2838 /* This is OK for a non-message file, it still uses the
2839 BACKUP_PREFIX form. The backup file will be in the same
2840 directory as file. */
2841 return rename (file
, m_backup (file
));
2846 * Output formatted message to user.
2849 report (char *what
, char *partno
, char *filename
, char *message
, ...)
2855 va_start (args
, message
);
2856 fmt
= concat (filename
, partno
? " part " : ", ",
2857 FENDNULL(partno
), partno
? ", " : "", message
, NULL
);
2859 advertise (what
, NULL
, fmt
, args
);
2872 fprintf (stderr
, "\n");