2 * mhfixmsg.c -- rewrite a message with various tranformations
4 * This code is Copyright (c) 2002 and 2013, by the authors of nmh.
5 * See the COPYRIGHT file in the root directory of the nmh
6 * distribution for complete copyright information.
11 #include <h/mhparse.h>
13 #include <h/signals.h>
16 #define MHFIXMSG_SWITCHES \
17 X("decodetext 8bit|7bit", 0, DECODETEXTSW) \
18 X("nodecodetext", 0, NDECODETEXTSW) \
19 X("textcharset", 0, TEXTCHARSETSW) \
20 X("notextcharset", 0, NTEXTCHARSETSW) \
21 X("reformat", 0, REFORMATSW) \
22 X("noreformat", 0, NREFORMATSW) \
23 X("replacetextplain", 0, REPLACETEXTPLAINSW) \
24 X("noreplacetextplain", 0, NREPLACETEXTPLAINSW) \
25 X("fixboundary", 0, FIXBOUNDARYSW) \
26 X("nofixboundary", 0, NFIXBOUNDARYSW) \
27 X("fixcte", 0, FIXCTESW) \
28 X("nofixcte", 0, NFIXCTESW) \
29 X("file file", 0, FILESW) \
30 X("outfile file", 0, OUTFILESW) \
31 X("rmmproc program", 0, RPROCSW) \
32 X("normmproc", 0, NRPRCSW) \
33 X("verbose", 0, VERBSW) \
34 X("noverbose", 0, NVERBSW) \
35 X("version", 0, VERSIONSW) \
36 X("help", 0, HELPSW) \
38 #define X(sw, minchars, id) id,
39 DEFINE_SWITCH_ENUM(MHFIXMSG
);
42 #define X(sw, minchars, id) { sw, minchars, id },
43 DEFINE_SWITCH_ARRAY(MHFIXMSG
, switches
);
48 int debugsw
; /* Needed by mhparse.c. */
50 #define quitser pipeser
53 extern int skip_mp_cte_check
; /* flag to InitMultiPart */
54 extern int suppress_bogus_mp_content_warning
; /* flag to InitMultiPart */
55 extern int bogus_mp_content
; /* flag from InitMultiPart */
56 CT
parse_mime (char *);
57 void reverse_parts (CT
);
60 int output_message (CT
, char *);
63 int show_content_aux (CT
, int, char *, char *);
66 void flush_errors (void);
70 void freects_done (int) NORETURN
;
75 typedef struct fix_transformations
{
82 } fix_transformations
;
84 int mhfixmsgsbr (CT
*, const fix_transformations
*, char *);
85 static void reverse_alternative_parts (CT
);
86 static int fix_boundary (CT
*, int *);
87 static int get_multipart_boundary (CT
, char **);
88 static int replace_boundary (CT
, char *, char *);
89 static int fix_multipart_cte (CT
, int *);
90 static int set_ce (CT
, int);
91 static int ensure_text_plain (CT
*, CT
, int *, int);
92 static CT
build_text_plain_part (CT
);
93 static CT
divide_part (CT
);
94 static void copy_ctinfo (CI
, CI
);
95 static int decode_part (CT
);
96 static int reformat_part (CT
, char *, char *, char *, int);
97 static int charset_encoding (CT
);
98 static CT
build_multipart_alt (CT
, CT
, int, int);
99 static int boundary_in_content (FILE **, char *, const char *);
100 static void transfer_noncontent_headers (CT
, CT
);
101 static int set_ct_type (CT
, int type
, int subtype
, int encoding
);
102 static int decode_text_parts (CT
, int, int *);
103 static int content_encoding (CT
, const char **);
104 static int strip_crs (CT
, int *);
105 static int convert_charsets (CT
, char *, int *);
106 static int write_content (CT
, char *, char *, int, int);
107 static int remove_file (char *);
108 static void report (char *, char *, char *, char *, ...);
109 static void pipeser (int);
113 main (int argc
, char **argv
) {
115 char *cp
, *file
= NULL
, *folder
= NULL
;
116 char *maildir
, buf
[100], *outfile
= NULL
;
117 char **argp
, **arguments
;
118 struct msgs_array msgs
= { 0, 0, NULL
};
119 struct msgs
*mp
= NULL
;
124 fix_transformations fx
;
125 fx
.reformat
= fx
.fixcte
= fx
.fixboundary
= 1;
126 fx
.replacetextplain
= 0;
127 fx
.decodetext
= CE_8BIT
;
128 fx
.textcharset
= NULL
;
130 if (nmh_init(argv
[0], 1)) { return 1; }
134 arguments
= getarguments (invo_name
, argc
, argv
, 1);
140 while ((cp
= *argp
++)) {
142 switch (smatch (++cp
, switches
)) {
144 ambigsw (cp
, switches
);
147 adios (NULL
, "-%s unknown", cp
);
150 snprintf (buf
, sizeof buf
, "%s [+folder] [msgs] [switches]",
152 print_help (buf
, switches
, 1);
155 print_version(invo_name
);
159 if (! (cp
= *argp
++) || *cp
== '-')
160 adios (NULL
, "missing argument to %s", argp
[-2]);
161 if (! strcasecmp (cp
, "8bit")) {
162 fx
.decodetext
= CE_8BIT
;
163 } else if (! strcasecmp (cp
, "7bit")) {
164 fx
.decodetext
= CE_7BIT
;
166 adios (NULL
, "invalid argument to %s", argp
[-2]);
173 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
174 adios (NULL
, "missing argument to %s", argp
[-2]);
198 case REPLACETEXTPLAINSW
:
199 fx
.replacetextplain
= 1;
201 case NREPLACETEXTPLAINSW
:
202 fx
.replacetextplain
= 0;
205 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
206 adios (NULL
, "missing argument to %s", argp
[-2]);
207 file
= *cp
== '-' ? add (cp
, NULL
) : path (cp
, TFILE
);
210 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
211 adios (NULL
, "missing argument to %s", argp
[-2]);
212 outfile
= *cp
== '-' ? add (cp
, NULL
) : path (cp
, TFILE
);
215 if (!(rmmproc
= *argp
++) || *rmmproc
== '-')
216 adios (NULL
, "missing argument to %s", argp
[-2]);
229 if (*cp
== '+' || *cp
== '@') {
231 adios (NULL
, "only one folder at a time!");
233 folder
= pluspath (cp
);
236 /* Interpret a full path as a filename, not a message. */
237 file
= add (cp
, NULL
);
239 app_msgarg (&msgs
, cp
);
244 SIGNAL (SIGQUIT
, quitser
);
245 SIGNAL (SIGPIPE
, pipeser
);
248 * Read the standard profile setup
250 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
251 readconfig ((struct node
**) 0, fp
, cp
, 0);
255 suppress_bogus_mp_content_warning
= skip_mp_cte_check
= 1;
257 if (! context_find ("path"))
258 free (path ("./", TFOLDER
));
260 if (file
&& msgs
.size
)
261 adios (NULL
, "cannot specify msg and file at same time!");
264 * check if message is coming from file
267 /* If file is stdin, create a tmp file name before parse_mime()
268 has a chance, because it might put in on a different
269 filesystem than the output file. Instead, put it in the
270 user's preferred tmp directory. */
273 if (! strcmp ("-", file
)) {
279 if ((cp
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
280 adios (NULL
, "unable to create temporary file in %s",
284 file
= add (cp
, NULL
);
285 cpydata (STDIN_FILENO
, fd
, "-", file
);
289 (void) m_unlink (file
);
290 adios (NULL
, "failed to write temporary file");
294 if (! (cts
= (CT
*) calloc ((size_t) 2, sizeof *cts
)))
295 adios (NULL
, "out of memory");
298 if ((ct
= parse_mime (file
))) *ctp
++ = ct
;
301 * message(s) are coming from a folder
306 app_msgarg(&msgs
, "cur");
308 folder
= getfolder (1);
309 maildir
= m_maildir (folder
);
311 if (chdir (maildir
) == NOTOK
)
312 adios (maildir
, "unable to change directory to");
314 /* read folder and create message structure */
315 if (! (mp
= folder_read (folder
, 1)))
316 adios (NULL
, "unable to read folder %s", folder
);
318 /* check for empty folder */
320 adios (NULL
, "no messages in %s", folder
);
322 /* parse all the message ranges/sequences and set SELECTED */
323 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
324 if (! m_convert (mp
, msgs
.msgs
[msgnum
]))
326 seq_setprev (mp
); /* set the previous-sequence */
328 if (! (cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof *cts
)))
329 adios (NULL
, "out of memory");
332 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
333 if (is_selected(mp
, msgnum
)) {
336 msgnam
= m_name (msgnum
);
337 if ((ct
= parse_mime (msgnam
))) *ctp
++ = ct
;
341 seq_setcur (mp
, mp
->hghsel
); /* update current message */
342 seq_save (mp
); /* synchronize sequences */
343 context_replace (pfolder
, folder
);/* update current folder */
344 context_save (); /* save the context file */
348 for (ctp
= cts
; *ctp
; ++ctp
) {
349 status
+= mhfixmsgsbr (ctp
, &fx
, outfile
);
352 (void) m_unlink (file
);
355 /* Just calling m_backup() unlinks the backup file. */
356 (void) m_backup (file
);
367 /* done is freects_done, which will clean up all of cts. */
374 mhfixmsgsbr (CT
*ctp
, const fix_transformations
*fx
, char *outfile
) {
375 /* Store input filename in case one of the transformations, i.e.,
376 fix_boundary(), rewrites to a tmp file. */
377 char *input_filename
= add ((*ctp
)->c_file
, NULL
);
378 int modify_inplace
= 0;
379 int message_mods
= 0;
382 if (outfile
== NULL
) {
385 if ((*ctp
)->c_file
) {
387 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
388 adios (NULL
, "unable to create temporary file in %s",
391 outfile
= add (tempfile
, NULL
);
393 adios (NULL
, "missing both input and output filenames\n");
397 reverse_alternative_parts (*ctp
);
398 if (status
== OK
&& fx
->fixboundary
) {
399 status
= fix_boundary (ctp
, &message_mods
);
401 if (status
== OK
&& fx
->fixcte
) {
402 status
= fix_multipart_cte (*ctp
, &message_mods
);
404 if (status
== OK
&& fx
->reformat
) {
406 ensure_text_plain (ctp
, NULL
, &message_mods
, fx
->replacetextplain
);
408 if (status
== OK
&& fx
->decodetext
) {
409 status
= decode_text_parts (*ctp
, fx
->decodetext
, &message_mods
);
411 if (status
== OK
&& fx
->textcharset
!= NULL
) {
412 status
= convert_charsets (*ctp
, fx
->textcharset
, &message_mods
);
415 if (! (*ctp
)->c_umask
) {
416 /* Set the umask for the contents file. This currently
417 isn't used but just in case it is in the future. */
420 if (stat ((*ctp
)->c_file
, &st
) != NOTOK
) {
421 (*ctp
)->c_umask
= ~(st
.st_mode
& 0777);
423 (*ctp
)->c_umask
= ~m_gmprot();
428 * Write the content to a file
431 status
= write_content (*ctp
, input_filename
, outfile
, modify_inplace
,
433 } else if (! modify_inplace
) {
434 /* Something went wrong. Output might be expected, such
435 as if this were run as a filter. Just copy the input
437 int in
= open (input_filename
, O_RDONLY
);
438 int out
= strcmp (outfile
, "-")
439 ? open (outfile
, O_WRONLY
| O_CREAT
, m_gmprot ())
442 if (in
!= -1 && out
!= -1) {
443 cpydata (in
, out
, input_filename
, outfile
);
452 if (modify_inplace
) {
453 if (status
!= OK
) (void) m_unlink (outfile
);
458 free (input_filename
);
464 /* parse_mime() arranges alternates in reverse (priority) order, so
465 reverse them back. This will put a text/plain part at the front of
466 a multipart/alternative part, for example, where it belongs. */
468 reverse_alternative_parts (CT ct
) {
469 if (ct
->c_type
== CT_MULTIPART
) {
470 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
473 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
477 /* And call recursively on each part of a multipart. */
478 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
479 reverse_alternative_parts (part
->mp_part
);
486 fix_boundary (CT
*ct
, int *message_mods
) {
487 struct multipart
*mp
;
490 if (bogus_mp_content
) {
491 mp
= (struct multipart
*) (*ct
)->c_ctparams
;
494 * 1) Get boundary at end of part.
495 * 2) Get boundary at beginning of part and compare to the end-of-part
497 * 3) Write out contents of ct to tmp file, replacing boundary in
498 * header with boundary from part. Set c_unlink to 1.
500 * 5) Call parse_mime() on the tmp file, replacing ct.
503 if (mp
&& mp
->mp_start
) {
506 if (get_multipart_boundary (*ct
, &part_boundary
) == OK
) {
509 if ((fixed
= m_mktemp2 (NULL
, invo_name
, NULL
, &(*ct
)->c_fp
))) {
510 if (replace_boundary (*ct
, fixed
, part_boundary
) == OK
) {
511 char *filename
= add ((*ct
)->c_file
, NULL
);
514 if ((*ct
= parse_mime (fixed
))) {
519 report (NULL
, NULL
, filename
,
520 "fix multipart boundary");
525 advise (NULL
, "unable to replace broken boundary");
529 advise (NULL
, "unable to create temporary file in %s",
534 free (part_boundary
);
544 get_multipart_boundary (CT ct
, char **part_boundary
) {
546 char *end_boundary
= NULL
;
547 off_t begin
= (off_t
) ct
->c_end
> (off_t
) (ct
->c_begin
+ sizeof buffer
)
548 ? (off_t
) (ct
->c_end
- sizeof buffer
)
549 : (off_t
) ct
->c_begin
;
553 /* This will fail if the boundary spans fread() calls. BUFSIZ should
554 be big enough, even if it's just 1024, to make that unlikely. */
556 /* free_content() will close ct->c_fp. */
557 if (! ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
558 advise (ct
->c_file
, "unable to open for reading");
562 /* Get boundary at end of multipart. */
563 while (begin
>= (off_t
) ct
->c_begin
) {
564 fseeko (ct
->c_fp
, begin
, SEEK_SET
);
565 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
566 char *cp
= rfind_str (buffer
, bytes_read
, "--");
571 /* Trim off trailing "--" and anything beyond. */
573 if ((end
= rfind_str (buffer
, cp
- buffer
, "\n"))) {
574 if (strlen (end
) > 3 && *end
++ == '\n' &&
575 *end
++ == '-' && *end
++ == '-') {
576 end_boundary
= add (end
, NULL
);
583 if (! end_boundary
&& begin
> (off_t
) (ct
->c_begin
+ sizeof buffer
)) {
584 begin
-= sizeof buffer
;
590 /* Get boundary at beginning of multipart. */
592 fseeko (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
593 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
594 if (bytes_read
>= strlen (end_boundary
)) {
595 char *cp
= find_str (buffer
, bytes_read
, end_boundary
);
597 if (cp
&& cp
- buffer
>= 2 && *--cp
== '-' &&
598 *--cp
== '-' && (cp
> buffer
&& *--cp
== '\n')) {
603 /* The start and end boundaries didn't match, or the
604 start boundary doesn't begin with "\n--" (or "--"
605 if at the beginning of buffer). Keep trying. */
614 *part_boundary
= end_boundary
;
616 *part_boundary
= NULL
;
624 /* Open and copy ct->c_file to file, replacing the multipart boundary. */
626 replace_boundary (CT ct
, char *file
, char *boundary
) {
629 char buf
[BUFSIZ
], name
[NAMESZ
];
631 m_getfld_state_t gstate
= 0;
634 if (ct
->c_file
== NULL
) {
635 advise (NULL
, "missing input filename");
639 if ((fpin
= fopen (ct
->c_file
, "r")) == NULL
) {
640 advise (ct
->c_file
, "unable to open for reading");
644 if ((fpout
= fopen (file
, "w")) == NULL
) {
646 advise (file
, "unable to open for writing");
650 for (compnum
= 1;;) {
651 int bufsz
= (int) sizeof buf
;
653 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
)) {
658 /* get copies of the buffers */
659 np
= add (name
, NULL
);
660 vp
= add (buf
, NULL
);
662 /* if necessary, get rest of field */
663 while (state
== FLDPLUS
) {
665 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
);
666 vp
= add (buf
, vp
); /* add to previous value */
669 if (strcasecmp (TYPE_FIELD
, np
)) {
670 fprintf (fpout
, "%s:%s", np
, vp
);
672 char *new_ctline
, *new_params
;
674 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
675 &ct
->c_ctinfo
.ci_last_pm
, "boundary",
678 new_ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
679 ct
->c_ctinfo
.ci_subtype
, NULL
);
680 new_params
= output_params(strlen(TYPE_FIELD
) +
681 strlen(new_ctline
) + 1,
682 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
683 fprintf (fpout
, "%s:%s%s\n", np
, new_ctline
,
684 new_params
? new_params
: "");
697 /* buf will have a terminating NULL, skip it. */
698 fwrite (buf
, 1, bufsz
-1, fpout
);
706 advise (NULL
, "message format error in component #%d", compnum
);
711 advise (NULL
, "getfld() returned %d", state
);
719 m_getfld_state_destroy (&gstate
);
728 fix_multipart_cte (CT ct
, int *message_mods
) {
731 if (ct
->c_type
== CT_MULTIPART
) {
735 if (ct
->c_encoding
!= CE_7BIT
&& ct
->c_encoding
!= CE_8BIT
&&
736 ct
->c_encoding
!= CE_BINARY
) {
739 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
740 char *name
= hf
->name
;
741 for (; *name
&& isspace ((unsigned char) *name
); ++name
) {
745 if (! strncasecmp (name
, ENCODING_FIELD
,
746 strlen (ENCODING_FIELD
))) {
747 char *prefix
= "Nmh-REPLACED-INVALID-";
748 HF h
= mh_xmalloc (sizeof *h
);
750 h
->name
= add (hf
->name
, NULL
);
751 h
->hf_encoding
= hf
->hf_encoding
;
755 /* Retain old header but prefix its name. */
757 hf
->name
= concat (prefix
, h
->name
, NULL
);
761 char *encoding
= cpytrim (hf
->value
);
762 report (NULL
, ct
->c_partno
, ct
->c_file
,
763 "replace Content-Transfer-Encoding of %s "
764 "with 8 bit", encoding
);
768 h
->value
= add (" 8bit\n", NULL
);
770 /* Don't need to warn for multiple C-T-E header
771 fields, parse_mime() already does that. But
772 if there are any, fix them all as necessary. */
777 set_ce (ct
, CE_8BIT
);
780 m
= (struct multipart
*) ct
->c_ctparams
;
781 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
782 if (fix_multipart_cte (part
->mp_part
, message_mods
) != OK
) {
794 set_ce (CT ct
, int encoding
) {
795 const char *ce
= ce_str (encoding
);
796 const struct str2init
*ctinit
= get_ce_method (ce
);
799 char *cte
= concat (" ", ce
, "\n", NULL
);
802 /* Decoded contents might be in ct->c_cefile.ce_file, if the
803 caller is decode_text_parts (). Save because we'll
805 struct cefile decoded_content_info
= ct
->c_cefile
;
807 ct
->c_encoding
= encoding
;
809 ct
->c_ctinitfnx
= ctinit
->si_init
;
810 /* This will assign ct->c_cefile with an all-0 struct, which
812 (*ctinit
->si_init
) (ct
);
813 /* After returning, the caller should set
814 ct->c_cefile.ce_file to the name of the file containing
817 /* Restore the cefile. */
818 ct
->c_cefile
= decoded_content_info
;
820 /* Update/add Content-Transfer-Encoding header field. */
821 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
822 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
829 add_header (ct
, add (ENCODING_FIELD
, NULL
), cte
);
832 /* Update c_celine. It's used only by mhlist -debug. */
834 ct
->c_celine
= add (cte
, NULL
);
843 /* Make sure each text part has a corresponding text/plain part. */
845 ensure_text_plain (CT
*ct
, CT parent
, int *message_mods
, int replacetextplain
) {
848 switch ((*ct
)->c_type
) {
850 int has_text_plain
= 0;
852 /* Nothing to do for text/plain. */
853 if ((*ct
)->c_subtype
== TEXT_PLAIN
) return OK
;
855 if (parent
&& parent
->c_type
== CT_MULTIPART
&&
856 parent
->c_subtype
== MULTI_ALTERNATE
) {
857 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
858 struct part
*part
, *prev
;
859 int new_subpart_number
= 1;
861 /* See if there is a sibling text/plain. */
862 for (prev
= part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
863 ++new_subpart_number
;
864 if (part
->mp_part
->c_type
== CT_TEXT
&&
865 part
->mp_part
->c_subtype
== TEXT_PLAIN
) {
866 if (replacetextplain
) {
867 struct part
*old_part
;
868 if (part
== mp
->mp_parts
) {
869 old_part
= mp
->mp_parts
;
870 mp
->mp_parts
= part
->mp_next
;
872 old_part
= prev
->mp_next
;
873 prev
->mp_next
= part
->mp_next
;
876 report (NULL
, parent
->c_partno
, parent
->c_file
,
877 "remove text/plain part %s",
878 old_part
->mp_part
->c_partno
);
880 free_content (old_part
->mp_part
);
890 if (! has_text_plain
) {
891 /* Parent is a multipart/alternative. Insert a new
892 text/plain subpart. */
893 struct part
*new_part
= mh_xmalloc (sizeof *new_part
);
895 if ((new_part
->mp_part
= build_text_plain_part (*ct
))) {
897 snprintf (buffer
, sizeof buffer
, "%d", new_subpart_number
);
899 new_part
->mp_next
= mp
->mp_parts
;
900 mp
->mp_parts
= new_part
;
901 new_part
->mp_part
->c_partno
=
902 concat (parent
->c_partno
? parent
->c_partno
: "1", ".",
907 report (NULL
, parent
->c_partno
, parent
->c_file
,
908 "insert text/plain part");
911 free_content (new_part
->mp_part
);
917 /* Slip new text/plain part into a new multipart/alternative. */
918 CT tp_part
= build_text_plain_part (*ct
);
921 CT mp_alt
= build_multipart_alt (*ct
, tp_part
, CT_MULTIPART
,
924 struct multipart
*mp
=
925 (struct multipart
*) mp_alt
->c_ctparams
;
927 if (mp
&& mp
->mp_parts
) {
928 mp
->mp_parts
->mp_part
= tp_part
;
929 /* Make the new multipart/alternative the parent. */
934 report (NULL
, (*ct
)->c_partno
, (*ct
)->c_file
,
935 "insert text/plain part");
938 free_content (tp_part
);
939 free_content (mp_alt
);
953 struct multipart
*mp
= (struct multipart
*) (*ct
)->c_ctparams
;
956 for (part
= mp
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
957 if ((*ct
)->c_type
== CT_MULTIPART
) {
958 status
= ensure_text_plain (&part
->mp_part
, *ct
, message_mods
,
966 if ((*ct
)->c_subtype
== MESSAGE_EXTERNAL
) {
969 e
= (struct exbody
*) (*ct
)->c_ctparams
;
970 status
= ensure_text_plain (&e
->eb_content
, *ct
, message_mods
,
981 build_text_plain_part (CT encoded_part
) {
982 CT tp_part
= divide_part (encoded_part
);
983 char *tmp_plain_file
= NULL
;
985 if (decode_part (tp_part
) == OK
) {
986 /* Now, tp_part->c_cefile.ce_file is the name of the tmp file that
987 contains the decoded contents. And the decoding function, such
988 as openQuoted, will have set ...->ce_unlink to 1 so that it will
989 be unlinked by free_content (). */
992 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
993 advise (NULL
, "unable to create temporary file in %s",
996 tmp_plain_file
= add (tempfile
, NULL
);
997 if (reformat_part (tp_part
, tmp_plain_file
,
998 tp_part
->c_ctinfo
.ci_type
,
999 tp_part
->c_ctinfo
.ci_subtype
,
1000 tp_part
->c_type
) == OK
) {
1005 free_content (tp_part
);
1006 (void) m_unlink (tmp_plain_file
);
1007 free (tmp_plain_file
);
1014 divide_part (CT ct
) {
1017 if ((new_part
= (CT
) calloc (1, sizeof *new_part
)) == NULL
)
1018 adios (NULL
, "out of memory");
1020 /* Just copy over what is needed for decoding. c_vrsn and
1021 c_celine aren't necessary. */
1022 new_part
->c_file
= add (ct
->c_file
, NULL
);
1023 new_part
->c_begin
= ct
->c_begin
;
1024 new_part
->c_end
= ct
->c_end
;
1025 copy_ctinfo (&new_part
->c_ctinfo
, &ct
->c_ctinfo
);
1026 new_part
->c_type
= ct
->c_type
;
1027 new_part
->c_cefile
= ct
->c_cefile
;
1028 new_part
->c_encoding
= ct
->c_encoding
;
1029 new_part
->c_ctinitfnx
= ct
->c_ctinitfnx
;
1030 new_part
->c_ceopenfnx
= ct
->c_ceopenfnx
;
1031 new_part
->c_ceclosefnx
= ct
->c_ceclosefnx
;
1032 new_part
->c_cesizefnx
= ct
->c_cesizefnx
;
1034 /* c_ctline is used by reformat__part(), so it can preserve
1035 anything after the type/subtype. */
1036 new_part
->c_ctline
= add (ct
->c_ctline
, NULL
);
1043 copy_ctinfo (CI dest
, CI src
) {
1046 dest
->ci_type
= src
->ci_type
? add (src
->ci_type
, NULL
) : NULL
;
1047 dest
->ci_subtype
= src
->ci_subtype
? add (src
->ci_subtype
, NULL
) : NULL
;
1049 for (s_pm
= src
->ci_first_pm
; s_pm
; s_pm
= s_pm
->pm_next
) {
1050 d_pm
= add_param(&dest
->ci_first_pm
, &dest
->ci_last_pm
, s_pm
->pm_name
,
1052 if (s_pm
->pm_charset
)
1053 d_pm
->pm_charset
= getcpy(s_pm
->pm_charset
);
1055 d_pm
->pm_lang
= getcpy(s_pm
->pm_lang
);
1058 dest
->ci_comment
= src
->ci_comment
? add (src
->ci_comment
, NULL
) : NULL
;
1059 dest
->ci_magic
= src
->ci_magic
? add (src
->ci_magic
, NULL
) : NULL
;
1064 decode_part (CT ct
) {
1069 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1070 adios (NULL
, "unable to create temporary file in %s", get_temp_dir());
1072 tmp_decoded
= add (tempfile
, NULL
);
1073 /* The following call will load ct->c_cefile.ce_file with the tmp
1074 filename of the decoded content. tmp_decoded will contain the
1075 encoded output, get rid of that. */
1076 status
= output_message (ct
, tmp_decoded
);
1077 (void) m_unlink (tmp_decoded
);
1084 /* Some of the arguments aren't really needed now, but maybe will
1085 be in the future for other than text types. */
1087 reformat_part (CT ct
, char *file
, char *type
, char *subtype
, int c_type
) {
1088 int output_subtype
, output_encoding
;
1092 /* Hacky: this redirects the output from whatever command is used
1093 to show the part to a file. So, the user can't have any output
1094 redirection in that command.
1095 Could show_multi() in mhshowsbr.c avoid this? */
1097 /* Check for invo_name-format-type/subtype. */
1098 cp
= concat (invo_name
, "-format-", type
, "/", subtype
, NULL
);
1099 if ((cf
= context_find (cp
)) && *cf
!= '\0') {
1100 if (strchr (cf
, '>')) {
1102 advise (NULL
, "'>' prohibited in \"%s\",\nplease fix your "
1103 "%s-format-%s/%s profile entry", cf
, invo_name
, type
,
1110 /* Check for invo_name-format-type. */
1111 cp
= concat (invo_name
, "-format-", type
, NULL
);
1112 if (! (cf
= context_find (cp
)) || *cf
== '\0') {
1115 advise (NULL
, "Don't know how to convert %s, there is no "
1116 "%s-format-%s/%s profile entry",
1117 ct
->c_file
, invo_name
, type
, subtype
);
1122 if (strchr (cf
, '>')) {
1124 advise (NULL
, "'>' prohibited in \"%s\"", cf
);
1130 cp
= concat (cf
, " >", file
, NULL
);
1131 status
= show_content_aux (ct
, 0, cp
, NULL
);
1134 /* Unlink decoded content tmp file and free its filename to avoid
1135 leaks. The file stream should already have been closed. */
1136 if (ct
->c_cefile
.ce_unlink
) {
1137 (void) m_unlink (ct
->c_cefile
.ce_file
);
1138 free (ct
->c_cefile
.ce_file
);
1139 ct
->c_cefile
.ce_file
= NULL
;
1140 ct
->c_cefile
.ce_unlink
= 0;
1143 if (c_type
== CT_TEXT
) {
1144 output_subtype
= TEXT_PLAIN
;
1146 /* Set subtype to 0, which is always an UNKNOWN subtype. */
1149 output_encoding
= charset_encoding (ct
);
1151 if (set_ct_type (ct
, c_type
, output_subtype
, output_encoding
) == OK
) {
1152 ct
->c_cefile
.ce_file
= file
;
1153 ct
->c_cefile
.ce_unlink
= 1;
1155 ct
->c_cefile
.ce_unlink
= 0;
1163 /* Identifies 7bit or 8bit content based on charset. */
1165 charset_encoding (CT ct
) {
1166 /* norm_charmap() is case sensitive. */
1167 char *charset
= upcase (content_charset (ct
));
1169 strcmp (norm_charmap (charset
), "US-ASCII") ? CE_8BIT
: CE_7BIT
;
1177 build_multipart_alt (CT first_alt
, CT new_part
, int type
, int subtype
) {
1178 char *boundary_prefix
= "----=_nmh-multipart";
1179 char *boundary
= concat (boundary_prefix
, first_alt
->c_partno
, NULL
);
1180 char *boundary_indicator
= "; boundary=";
1181 char *typename
, *subtypename
, *name
;
1184 struct multipart
*m
;
1185 const struct str2init
*ctinit
;
1187 if ((ct
= (CT
) calloc (1, sizeof *ct
)) == NULL
)
1188 adios (NULL
, "out of memory");
1190 /* Set up the multipart/alternative part. These fields of *ct were
1191 initialized to 0 by calloc():
1192 c_fp, c_unlink, c_begin, c_end,
1193 c_vrsn, c_ctline, c_celine,
1194 c_id, c_descr, c_dispo, c_partno,
1195 c_ctinfo.ci_comment, c_ctinfo.ci_magic,
1196 c_cefile, c_encoding,
1197 c_digested, c_digest[16], c_ctexbody,
1198 c_ctinitfnx, c_ceopenfnx, c_ceclosefnx, c_cesizefnx,
1200 c_showproc, c_termproc, c_storeproc, c_storage, c_folder
1203 ct
->c_file
= add (first_alt
->c_file
, NULL
);
1205 ct
->c_subtype
= subtype
;
1207 ctinit
= get_ct_init (ct
->c_type
);
1209 typename
= ct_type_str (type
);
1210 subtypename
= ct_subtype_str (type
, subtype
);
1214 int found_boundary
= 1;
1216 while (found_boundary
&& serial
< 1000000) {
1219 /* Ensure that the boundary doesn't appear in the decoded
1221 if (new_part
->c_cefile
.ce_file
) {
1222 if ((found_boundary
=
1223 boundary_in_content (&new_part
->c_cefile
.ce_fp
,
1224 new_part
->c_cefile
.ce_file
,
1231 /* Ensure that the boundary doesn't appear in the encoded
1233 if (! found_boundary
&& new_part
->c_file
) {
1234 if ((found_boundary
= boundary_in_content (&new_part
->c_fp
,
1242 if (found_boundary
) {
1243 /* Try a slightly different boundary. */
1248 snprintf (buffer2
, sizeof buffer2
, "%d", serial
);
1250 concat (boundary_prefix
,
1251 first_alt
->c_partno
? first_alt
->c_partno
: "",
1252 "-", buffer2
, NULL
);
1256 if (found_boundary
) {
1257 advise (NULL
, "giving up trying to find a unique boundary");
1263 name
= concat (" ", typename
, "/", subtypename
, boundary_indicator
, "\"",
1264 boundary
, "\"", NULL
);
1266 /* Load c_first_hf and c_last_hf. */
1267 transfer_noncontent_headers (first_alt
, ct
);
1268 add_header (ct
, add (TYPE_FIELD
, NULL
), concat (name
, "\n", NULL
));
1271 /* Load c_partno. */
1272 if (first_alt
->c_partno
) {
1273 ct
->c_partno
= add (first_alt
->c_partno
, NULL
);
1274 free (first_alt
->c_partno
);
1275 first_alt
->c_partno
= concat (ct
->c_partno
, ".1", NULL
);
1276 new_part
->c_partno
= concat (ct
->c_partno
, ".2", NULL
);
1278 first_alt
->c_partno
= add ("1", NULL
);
1279 new_part
->c_partno
= add ("2", NULL
);
1283 ct
->c_ctinfo
.ci_type
= add (typename
, NULL
);
1284 ct
->c_ctinfo
.ci_subtype
= add (subtypename
, NULL
);
1287 add_param(&ct
->c_ctinfo
.ci_first_pm
, &ct
->c_ctinfo
.ci_last_pm
,
1288 "boundary", boundary
, 0);
1290 p
= (struct part
*) mh_xmalloc (sizeof *p
);
1291 p
->mp_next
= (struct part
*) mh_xmalloc (sizeof *p
->mp_next
);
1292 p
->mp_next
->mp_next
= NULL
;
1293 p
->mp_next
->mp_part
= first_alt
;
1295 if ((m
= (struct multipart
*) calloc (1, sizeof (struct multipart
))) ==
1297 adios (NULL
, "out of memory");
1298 m
->mp_start
= concat (boundary
, "\n", NULL
);
1299 m
->mp_stop
= concat (boundary
, "--\n", NULL
);
1301 ct
->c_ctparams
= (void *) m
;
1309 /* Check that the boundary does not appear in the content. */
1311 boundary_in_content (FILE **fp
, char *file
, const char *boundary
) {
1312 char buffer
[BUFSIZ
];
1314 int found_boundary
= 0;
1316 /* free_content() will close *fp if we fopen it here. */
1317 if (! *fp
&& (*fp
= fopen (file
, "r")) == NULL
) {
1318 advise (file
, "unable to open %s for reading", file
);
1322 fseeko (*fp
, 0L, SEEK_SET
);
1323 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) > 0) {
1324 if (find_str (buffer
, bytes_read
, boundary
)) {
1330 return found_boundary
;
1334 /* Remove all non-Content headers. */
1336 transfer_noncontent_headers (CT old
, CT
new) {
1339 hp_prev
= hp
= old
->c_first_hf
;
1343 if (strncasecmp (XXX_FIELD_PRF
, hp
->name
, strlen (XXX_FIELD_PRF
))) {
1344 if (hp
== old
->c_last_hf
) {
1345 if (hp
== old
->c_first_hf
) {
1346 old
->c_last_hf
= old
->c_first_hf
= NULL
;
1348 hp_prev
->next
= NULL
;
1349 old
->c_last_hf
= hp_prev
;
1352 if (hp
== old
->c_first_hf
) {
1353 old
->c_first_hf
= next
;
1355 hp_prev
->next
= next
;
1359 /* Put node hp in the new CT. */
1360 if (new->c_first_hf
== NULL
) {
1361 new->c_first_hf
= hp
;
1363 new->c_last_hf
->next
= hp
;
1365 new->c_last_hf
= hp
;
1367 /* A Content- header, leave in old. */
1377 set_ct_type (CT ct
, int type
, int subtype
, int encoding
) {
1378 char *typename
= ct_type_str (type
);
1379 char *subtypename
= ct_subtype_str (type
, subtype
);
1380 /* E.g, " text/plain" */
1381 char *type_subtypename
= concat (" ", typename
, "/", subtypename
, NULL
);
1382 /* E.g, " text/plain\n" */
1383 char *name_plus_nl
= concat (type_subtypename
, "\n", NULL
);
1384 int found_content_type
= 0;
1386 const char *cp
= NULL
;
1390 /* Update/add Content-Type header field. */
1391 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1392 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1393 found_content_type
= 1;
1395 hf
->value
= (cp
= strchr (ct
->c_ctline
, ';'))
1396 ? concat (type_subtypename
, cp
, "\n", NULL
)
1397 : add (name_plus_nl
, NULL
);
1400 if (! found_content_type
) {
1401 add_header (ct
, add (TYPE_FIELD
, NULL
),
1402 (cp
= strchr (ct
->c_ctline
, ';'))
1403 ? concat (type_subtypename
, cp
, "\n", NULL
)
1404 : add (name_plus_nl
, NULL
));
1407 /* Some of these might not be used, but set them anyway. */
1409 ? concat (type_subtypename
, cp
, NULL
)
1410 : concat (type_subtypename
, NULL
);
1411 free (ct
->c_ctline
);
1412 ct
->c_ctline
= ctline
;
1413 /* Leave other ctinfo members as they were. */
1414 free (ct
->c_ctinfo
.ci_type
);
1415 ct
->c_ctinfo
.ci_type
= add (typename
, NULL
);
1416 free (ct
->c_ctinfo
.ci_subtype
);
1417 ct
->c_ctinfo
.ci_subtype
= add (subtypename
, NULL
);
1419 ct
->c_subtype
= subtype
;
1421 free (name_plus_nl
);
1422 free (type_subtypename
);
1424 status
= set_ce (ct
, encoding
);
1431 decode_text_parts (CT ct
, int encoding
, int *message_mods
) {
1434 switch (ct
->c_type
) {
1436 switch (ct
->c_encoding
) {
1441 if (decode_part (ct
) == OK
&& ct
->c_cefile
.ce_file
) {
1442 const char *reason
= NULL
;
1444 if ((ct_encoding
= content_encoding (ct
, &reason
)) == CE_BINARY
1445 && encoding
!= CE_BINARY
) {
1446 /* The decoding isn't acceptable so discard it.
1447 Leave status as OK to allow other transformations. */
1449 report (NULL
, ct
->c_partno
, ct
->c_file
,
1450 "will not decode%s because it is binary (%s)",
1452 : ct
->c_ctline
? ct
->c_ctline
1456 (void) m_unlink (ct
->c_cefile
.ce_file
);
1457 free (ct
->c_cefile
.ce_file
);
1458 ct
->c_cefile
.ce_file
= NULL
;
1459 } else if (ct
->c_encoding
== CE_QUOTED
&&
1460 ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
1461 /* The decoding isn't acceptable so discard it.
1462 Leave status as OK to allow other transformations. */
1464 report (NULL
, ct
->c_partno
, ct
->c_file
,
1465 "will not decode%s because it is 8bit",
1467 : ct
->c_ctline
? ct
->c_ctline
1470 (void) m_unlink (ct
->c_cefile
.ce_file
);
1471 free (ct
->c_cefile
.ce_file
);
1472 ct
->c_cefile
.ce_file
= NULL
;
1475 if (ct_encoding
== CE_BINARY
)
1477 else if (ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
)
1480 enc
= charset_encoding (ct
);
1481 if (set_ce (ct
, enc
) == OK
) {
1484 report (NULL
, ct
->c_partno
, ct
->c_file
, "decode%s",
1485 ct
->c_ctline
? ct
->c_ctline
: "");
1487 strip_crs (ct
, message_mods
);
1499 strip_crs (ct
, message_mods
);
1507 case CT_MULTIPART
: {
1508 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1511 /* Should check to see if the body for this part is encoded?
1512 For now, it gets passed along as-is by InitMultiPart(). */
1513 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1514 status
= decode_text_parts (part
->mp_part
, encoding
, message_mods
);
1520 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1523 e
= (struct exbody
*) ct
->c_ctparams
;
1524 status
= decode_text_parts (e
->eb_content
, encoding
, message_mods
);
1536 /* See if the decoded content is 7bit, 8bit, or binary. It's binary
1537 if it has any NUL characters, a CR not followed by a LF, or lines
1538 greater than 998 characters in length. If binary, reason is set
1539 to a string explaining why. */
1541 content_encoding (CT ct
, const char **reason
) {
1542 CE ce
= &ct
->c_cefile
;
1543 int encoding
= CE_7BIT
;
1546 size_t line_len
= 0;
1547 char buffer
[BUFSIZ
];
1550 if (! ce
->ce_fp
&& (ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
1551 advise (ce
->ce_file
, "unable to open for reading");
1555 fseeko (ce
->ce_fp
, 0L, SEEK_SET
);
1556 while (encoding
!= CE_BINARY
&&
1557 (inbytes
= fread (buffer
, 1, sizeof buffer
, ce
->ce_fp
)) > 0) {
1560 int last_char_was_cr
= 0;
1562 for (i
= 0, cp
= buffer
; i
< inbytes
; ++i
, ++cp
) {
1563 if (*cp
== '\0' || ++line_len
> 998 ||
1564 (*cp
!= '\n' && last_char_was_cr
)) {
1565 encoding
= CE_BINARY
;
1567 *reason
= "null character";
1568 } else if (line_len
> 998) {
1569 *reason
= "line length > 998";
1570 } else if (*cp
!= '\n' && last_char_was_cr
) {
1571 *reason
= "CR not followed by LF";
1573 /* Should not reach this. */
1577 } else if (*cp
== '\n') {
1579 } else if (! isascii ((unsigned char) *cp
)) {
1583 last_char_was_cr
= *cp
== '\r' ? 1 : 0;
1589 } /* else should never happen */
1596 strip_crs (CT ct
, int *message_mods
) {
1597 /* norm_charmap() is case sensitive. */
1598 char *charset
= upcase (content_charset (ct
));
1601 /* Only strip carriage returns if content is ASCII or another
1602 charset that has the same readily recognizable CR followed by a
1603 LF. We can include UTF-8 here because if the high-order bit of
1604 a UTF-8 byte is 0, then it must be a single-byte ASCII
1606 if (! strcmp (norm_charmap (charset
), "US-ASCII") ||
1607 ! strncmp (norm_charmap (charset
), "ISO-8859-", 9) ||
1608 ! strncmp (norm_charmap (charset
), "UTF-8", 5) ||
1609 ! strncmp (norm_charmap (charset
), "WINDOWS-12", 10)) {
1615 int opened_input_file
= 0;
1617 if (ct
->c_cefile
.ce_file
) {
1618 file
= &ct
->c_cefile
.ce_file
;
1619 fp
= &ct
->c_cefile
.ce_fp
;
1621 } else if (ct
->c_file
) {
1624 begin
= (size_t) ct
->c_begin
;
1625 end
= (size_t) ct
->c_end
;
1626 } /* else don't know where the content is */
1628 if (file
&& *file
&& fp
) {
1630 if ((*fp
= fopen (*file
, "r")) == NULL
) {
1631 advise (*file
, "unable to open for reading");
1634 opened_input_file
= 1;
1640 char buffer
[BUFSIZ
];
1642 size_t bytes_to_read
=
1643 end
> 0 && end
> begin
? end
- begin
: sizeof buffer
;
1645 fseeko (*fp
, begin
, SEEK_SET
);
1646 while ((bytes_read
= fread (buffer
, 1,
1647 min (bytes_to_read
, sizeof buffer
),
1649 /* Look for CR followed by a LF. This is supposed to
1650 be text so there should be LF's. If not, don't
1651 modify the content. */
1654 int last_char_was_cr
= 0;
1656 if (end
> 0) bytes_to_read
-= bytes_read
;
1658 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
1659 if (*cp
== '\n' && last_char_was_cr
) {
1664 last_char_was_cr
= *cp
== '\r' ? 1 : 0;
1670 char *stripped_content_file
;
1671 char *tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
);
1673 if (tempfile
== NULL
) {
1674 adios (NULL
, "unable to create temporary file in %s",
1677 stripped_content_file
= add (tempfile
, NULL
);
1679 /* Strip each CR before a LF from the content. */
1680 fseeko (*fp
, begin
, SEEK_SET
);
1681 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) >
1685 int last_char_was_cr
= 0;
1687 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
1689 last_char_was_cr
= 1;
1690 } else if (last_char_was_cr
) {
1691 if (*cp
!= '\n') write (fd
, "\r", 1);
1693 last_char_was_cr
= 0;
1696 last_char_was_cr
= 0;
1702 admonish (NULL
, "unable to write temporary file %s",
1703 stripped_content_file
);
1704 (void) m_unlink (stripped_content_file
);
1707 /* Replace the decoded file with the converted one. */
1708 if (ct
->c_cefile
.ce_file
) {
1709 if (ct
->c_cefile
.ce_unlink
) {
1710 (void) m_unlink (ct
->c_cefile
.ce_file
);
1712 free (ct
->c_cefile
.ce_file
);
1714 ct
->c_cefile
.ce_file
= stripped_content_file
;
1715 ct
->c_cefile
.ce_unlink
= 1;
1719 report (NULL
, ct
->c_partno
,
1720 begin
== 0 && end
== 0 ? "" : *file
,
1726 if (opened_input_file
) {
1739 convert_charsets (CT ct
, char *dest_charset
, int *message_mods
) {
1742 switch (ct
->c_type
) {
1744 if (ct
->c_subtype
== TEXT_PLAIN
) {
1745 status
= convert_charset (ct
, dest_charset
, message_mods
);
1748 report (NULL
, ct
->c_partno
, ct
->c_file
,
1750 content_charset(ct
), dest_charset
);
1753 report ("iconv", ct
->c_partno
, ct
->c_file
,
1754 "failed to convert %s to %s",
1755 content_charset(ct
), dest_charset
);
1760 case CT_MULTIPART
: {
1761 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1764 /* Should check to see if the body for this part is encoded?
1765 For now, it gets passed along as-is by InitMultiPart(). */
1766 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1768 convert_charsets (part
->mp_part
, dest_charset
, message_mods
);
1774 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1777 e
= (struct exbody
*) ct
->c_ctparams
;
1779 convert_charsets (e
->eb_content
, dest_charset
, message_mods
);
1792 write_content (CT ct
, char *input_filename
, char *outfile
, int modify_inplace
,
1796 if (modify_inplace
) {
1797 if (message_mods
> 0) {
1798 if ((status
= output_message (ct
, outfile
)) == OK
) {
1799 char *infile
= input_filename
1800 ? add (input_filename
, NULL
)
1801 : add (ct
->c_file
? ct
->c_file
: "-", NULL
);
1803 if (remove_file (infile
) == OK
) {
1804 if (rename (outfile
, infile
)) {
1805 /* Rename didn't work, possibly because of an
1806 attempt to rename across filesystems. Try
1807 brute force copy. */
1808 int old
= open (outfile
, O_RDONLY
);
1810 open (infile
, O_WRONLY
| O_CREAT
, m_gmprot ());
1813 if (old
!= -1 && new != -1) {
1814 char buffer
[BUFSIZ
];
1816 while ((i
= read (old
, buffer
, sizeof buffer
)) >
1818 if (write (new, buffer
, i
) != i
) {
1824 if (new != -1) close (new);
1825 if (old
!= -1) close (old
);
1826 (void) m_unlink (outfile
);
1829 /* The -file argument processing used path() to
1830 expand filename to absolute path. */
1831 int file
= ct
->c_file
&& ct
->c_file
[0] == '/';
1833 admonish (NULL
, "unable to rename %s %s to %s",
1834 file
? "file" : "message", outfile
,
1840 admonish (NULL
, "unable to remove input file %s, "
1841 "not modifying it", infile
);
1842 (void) m_unlink (outfile
);
1851 /* No modifications and didn't need the tmp outfile. */
1852 (void) m_unlink (outfile
);
1855 /* Output is going to some file. Produce it whether or not
1856 there were modifications. */
1857 status
= output_message (ct
, outfile
);
1866 * If "rmmproc" is defined, call that to remove the file. Otherwise,
1867 * use the standard MH backup file.
1870 remove_file (char *file
) {
1872 char *rmm_command
= concat (rmmproc
, " ", file
, NULL
);
1873 int status
= system (rmm_command
);
1876 return WIFEXITED (status
) ? WEXITSTATUS (status
) : NOTOK
;
1878 /* This is OK for a non-message file, it still uses the
1879 BACKUP_PREFIX form. The backup file will be in the same
1880 directory as file. */
1881 return rename (file
, m_backup (file
));
1887 report (char *what
, char *partno
, char *filename
, char *message
, ...) {
1892 va_start (args
, message
);
1893 fmt
= concat (filename
, partno
? " part " : ", ",
1894 partno
? partno
: "", partno
? ", " : "", message
, NULL
);
1896 advertise (what
, NULL
, fmt
, args
);
1909 fprintf (stderr
, "\n");