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("changecur", 0, CHGSW) \
34 X("nochangecur", 0, NCHGSW) \
35 X("verbose", 0, VERBSW) \
36 X("noverbose", 0, NVERBSW) \
37 X("version", 0, VERSIONSW) \
38 X("help", 0, HELPSW) \
40 #define X(sw, minchars, id) id,
41 DEFINE_SWITCH_ENUM(MHFIXMSG
);
44 #define X(sw, minchars, id) { sw, minchars, id },
45 DEFINE_SWITCH_ARRAY(MHFIXMSG
, switches
);
50 int debugsw
; /* Needed by mhparse.c. */
52 #define quitser pipeser
55 extern int skip_mp_cte_check
; /* flag to InitMultiPart */
56 extern int suppress_bogus_mp_content_warning
; /* flag to InitMultiPart */
57 extern int bogus_mp_content
; /* flag from InitMultiPart */
60 int output_message (CT
, char *);
63 void flush_errors (void);
67 void freects_done (int) NORETURN
;
72 typedef struct fix_transformations
{
79 } fix_transformations
;
81 int mhfixmsgsbr (CT
*, const fix_transformations
*, char *);
82 static int fix_boundary (CT
*, int *);
83 static int get_multipart_boundary (CT
, char **);
84 static int replace_boundary (CT
, char *, char *);
85 static int fix_multipart_cte (CT
, int *);
86 static int set_ce (CT
, int);
87 static int ensure_text_plain (CT
*, CT
, int *, int);
88 static int find_textplain_sibling (CT
, int, int *);
89 static int insert_new_text_plain_part (CT
, int, CT
);
90 static CT
build_text_plain_part (CT
);
91 static CT
divide_part (CT
);
92 static void copy_ctinfo (CI
, CI
);
93 static int decode_part (CT
);
94 static int reformat_part (CT
, char *, char *, char *, int);
95 static int charset_encoding (CT
);
96 static CT
build_multipart_alt (CT
, CT
, int, int);
97 static int boundary_in_content (FILE **, char *, const char *);
98 static void transfer_noncontent_headers (CT
, CT
);
99 static int set_ct_type (CT
, int type
, int subtype
, int encoding
);
100 static int decode_text_parts (CT
, int, int *);
101 static int content_encoding (CT
, const char **);
102 static int strip_crs (CT
, int *);
103 static int convert_charsets (CT
, char *, int *);
104 static int write_content (CT
, char *, char *, int, int);
105 static int remove_file (char *);
106 static void report (char *, char *, char *, char *, ...);
107 static void pipeser (int);
111 main (int argc
, char **argv
) {
113 char *cp
, *file
= NULL
, *folder
= NULL
;
114 char *maildir
, buf
[100], *outfile
= NULL
;
115 char **argp
, **arguments
;
116 struct msgs_array msgs
= { 0, 0, NULL
};
117 struct msgs
*mp
= NULL
;
123 fix_transformations fx
;
124 fx
.reformat
= fx
.fixcte
= fx
.fixboundary
= 1;
125 fx
.replacetextplain
= 0;
126 fx
.decodetext
= CE_8BIT
;
127 fx
.textcharset
= NULL
;
129 if (nmh_init(argv
[0], 1)) { return 1; }
133 arguments
= getarguments (invo_name
, argc
, argv
, 1);
139 while ((cp
= *argp
++)) {
141 switch (smatch (++cp
, switches
)) {
143 ambigsw (cp
, switches
);
146 adios (NULL
, "-%s unknown", cp
);
149 snprintf (buf
, sizeof buf
, "%s [+folder] [msgs] [switches]",
151 print_help (buf
, switches
, 1);
154 print_version(invo_name
);
158 if (! (cp
= *argp
++) || *cp
== '-')
159 adios (NULL
, "missing argument to %s", argp
[-2]);
160 if (! strcasecmp (cp
, "8bit")) {
161 fx
.decodetext
= CE_8BIT
;
162 } else if (! strcasecmp (cp
, "7bit")) {
163 fx
.decodetext
= CE_7BIT
;
165 adios (NULL
, "invalid argument to %s", argp
[-2]);
172 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
173 adios (NULL
, "missing argument to %s", argp
[-2]);
197 case REPLACETEXTPLAINSW
:
198 fx
.replacetextplain
= 1;
200 case NREPLACETEXTPLAINSW
:
201 fx
.replacetextplain
= 0;
204 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
205 adios (NULL
, "missing argument to %s", argp
[-2]);
206 file
= *cp
== '-' ? add (cp
, NULL
) : path (cp
, TFILE
);
209 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
210 adios (NULL
, "missing argument to %s", argp
[-2]);
211 outfile
= *cp
== '-' ? add (cp
, NULL
) : path (cp
, TFILE
);
214 if (!(rmmproc
= *argp
++) || *rmmproc
== '-')
215 adios (NULL
, "missing argument to %s", argp
[-2]);
234 if (*cp
== '+' || *cp
== '@') {
236 adios (NULL
, "only one folder at a time!");
238 folder
= pluspath (cp
);
241 /* Interpret a full path as a filename, not a message. */
242 file
= add (cp
, NULL
);
244 app_msgarg (&msgs
, cp
);
249 SIGNAL (SIGQUIT
, quitser
);
250 SIGNAL (SIGPIPE
, pipeser
);
253 * Read the standard profile setup
255 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
256 readconfig ((struct node
**) 0, fp
, cp
, 0);
260 suppress_bogus_mp_content_warning
= skip_mp_cte_check
= 1;
262 if (! context_find ("path"))
263 free (path ("./", TFOLDER
));
265 if (file
&& msgs
.size
)
266 adios (NULL
, "cannot specify msg and file at same time!");
269 * check if message is coming from file
272 /* If file is stdin, create a tmp file name before parse_mime()
273 has a chance, because it might put in on a different
274 filesystem than the output file. Instead, put it in the
275 user's preferred tmp directory. */
278 if (! strcmp ("-", file
)) {
284 if ((cp
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
285 adios (NULL
, "unable to create temporary file in %s",
289 file
= add (cp
, NULL
);
290 cpydata (STDIN_FILENO
, fd
, "-", file
);
294 (void) m_unlink (file
);
295 adios (NULL
, "failed to write temporary file");
299 if (! (cts
= (CT
*) mh_xcalloc ((size_t) 2, sizeof *cts
))) {
300 adios (NULL
, "out of memory");
304 if ((ct
= parse_mime (file
))) { *ctp
++ = ct
; }
307 * message(s) are coming from a folder
312 app_msgarg(&msgs
, "cur");
314 folder
= getfolder (1);
315 maildir
= m_maildir (folder
);
317 if (chdir (maildir
) == NOTOK
)
318 adios (maildir
, "unable to change directory to");
320 /* read folder and create message structure */
321 if (! (mp
= folder_read (folder
, 1)))
322 adios (NULL
, "unable to read folder %s", folder
);
324 /* check for empty folder */
326 adios (NULL
, "no messages in %s", folder
);
328 /* parse all the message ranges/sequences and set SELECTED */
329 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
330 if (! m_convert (mp
, msgs
.msgs
[msgnum
]))
332 seq_setprev (mp
); /* set the previous-sequence */
335 (CT
*) mh_xcalloc ((size_t) (mp
->numsel
+ 1), sizeof *cts
))) {
336 adios (NULL
, "out of memory");
340 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
341 if (is_selected(mp
, msgnum
)) {
344 msgnam
= m_name (msgnum
);
345 if ((ct
= parse_mime (msgnam
))) { *ctp
++ = ct
; }
350 seq_setcur (mp
, mp
->hghsel
); /* update current message */
352 seq_save (mp
); /* synchronize sequences */
353 context_replace (pfolder
, folder
);/* update current folder */
354 context_save (); /* save the context file */
358 for (ctp
= cts
; *ctp
; ++ctp
) {
359 status
+= mhfixmsgsbr (ctp
, &fx
, outfile
);
362 (void) m_unlink (file
);
365 /* Just calling m_backup() unlinks the backup file. */
366 (void) m_backup (file
);
377 /* done is freects_done, which will clean up all of cts. */
384 mhfixmsgsbr (CT
*ctp
, const fix_transformations
*fx
, char *outfile
) {
385 /* Store input filename in case one of the transformations, i.e.,
386 fix_boundary(), rewrites to a tmp file. */
387 char *input_filename
= add ((*ctp
)->c_file
, NULL
);
388 int modify_inplace
= 0;
389 int message_mods
= 0;
392 if (outfile
== NULL
) {
395 if ((*ctp
)->c_file
) {
397 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
398 adios (NULL
, "unable to create temporary file in %s",
401 outfile
= add (tempfile
, NULL
);
403 adios (NULL
, "missing both input and output filenames\n");
407 reverse_alternative_parts (*ctp
);
408 if (status
== OK
&& fx
->fixboundary
) {
409 status
= fix_boundary (ctp
, &message_mods
);
411 if (status
== OK
&& fx
->fixcte
) {
412 status
= fix_multipart_cte (*ctp
, &message_mods
);
414 if (status
== OK
&& fx
->reformat
) {
416 ensure_text_plain (ctp
, NULL
, &message_mods
, fx
->replacetextplain
);
418 if (status
== OK
&& fx
->decodetext
) {
419 status
= decode_text_parts (*ctp
, fx
->decodetext
, &message_mods
);
421 if (status
== OK
&& fx
->textcharset
!= NULL
) {
422 status
= convert_charsets (*ctp
, fx
->textcharset
, &message_mods
);
425 if (status
== OK
&& ! (*ctp
)->c_umask
) {
426 /* Set the umask for the contents file. This currently
427 isn't used but just in case it is in the future. */
430 if (stat ((*ctp
)->c_file
, &st
) != NOTOK
) {
431 (*ctp
)->c_umask
= ~(st
.st_mode
& 0777);
433 (*ctp
)->c_umask
= ~m_gmprot();
438 * Write the content to a file
441 status
= write_content (*ctp
, input_filename
, outfile
, modify_inplace
,
443 } else if (! modify_inplace
) {
444 /* Something went wrong. Output might be expected, such
445 as if this were run as a filter. Just copy the input
447 int in
= open (input_filename
, O_RDONLY
);
448 int out
= strcmp (outfile
, "-")
449 ? open (outfile
, O_WRONLY
| O_CREAT
, m_gmprot ())
452 if (in
!= -1 && out
!= -1) {
453 cpydata (in
, out
, input_filename
, outfile
);
462 if (modify_inplace
) {
463 if (status
!= OK
) { (void) m_unlink (outfile
); }
468 free (input_filename
);
475 fix_boundary (CT
*ct
, int *message_mods
) {
476 struct multipart
*mp
;
479 if (ct
&& (*ct
)->c_type
== CT_MULTIPART
&& bogus_mp_content
) {
480 mp
= (struct multipart
*) (*ct
)->c_ctparams
;
483 * 1) Get boundary at end of part.
484 * 2) Get boundary at beginning of part and compare to the end-of-part
486 * 3) Write out contents of ct to tmp file, replacing boundary in
487 * header with boundary from part. Set c_unlink to 1.
489 * 5) Call parse_mime() on the tmp file, replacing ct.
492 if (mp
&& mp
->mp_start
) {
495 if (get_multipart_boundary (*ct
, &part_boundary
) == OK
) {
498 if ((fixed
= m_mktemp2 (NULL
, invo_name
, NULL
, &(*ct
)->c_fp
))) {
499 if (replace_boundary (*ct
, fixed
, part_boundary
) == OK
) {
500 char *filename
= add ((*ct
)->c_file
, NULL
);
504 if ((fixed_ct
= parse_mime (fixed
))) {
510 report (NULL
, NULL
, filename
,
511 "fix multipart boundary");
515 advise (NULL
, "unable to parse fixed part");
520 advise (NULL
, "unable to replace broken boundary");
524 advise (NULL
, "unable to create temporary file in %s",
529 free (part_boundary
);
539 get_multipart_boundary (CT ct
, char **part_boundary
) {
541 char *end_boundary
= NULL
;
542 off_t begin
= (off_t
) ct
->c_end
> (off_t
) (ct
->c_begin
+ sizeof buffer
)
543 ? (off_t
) (ct
->c_end
- sizeof buffer
)
544 : (off_t
) ct
->c_begin
;
548 /* This will fail if the boundary spans fread() calls. BUFSIZ should
549 be big enough, even if it's just 1024, to make that unlikely. */
551 /* free_content() will close ct->c_fp. */
552 if (! ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
553 advise (ct
->c_file
, "unable to open for reading");
557 /* Get boundary at end of multipart. */
558 while (begin
>= (off_t
) ct
->c_begin
) {
559 fseeko (ct
->c_fp
, begin
, SEEK_SET
);
560 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
561 char *cp
= rfind_str (buffer
, bytes_read
, "--");
566 /* Trim off trailing "--" and anything beyond. */
568 if ((end
= rfind_str (buffer
, cp
- buffer
, "\n"))) {
569 if (strlen (end
) > 3 && *end
++ == '\n' &&
570 *end
++ == '-' && *end
++ == '-') {
571 end_boundary
= add (end
, NULL
);
578 if (! end_boundary
&& begin
> (off_t
) (ct
->c_begin
+ sizeof buffer
)) {
579 begin
-= sizeof buffer
;
585 /* Get boundary at beginning of multipart. */
587 fseeko (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
588 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
589 if (bytes_read
>= strlen (end_boundary
)) {
590 char *cp
= find_str (buffer
, bytes_read
, end_boundary
);
592 if (cp
&& cp
- buffer
>= 2 && *--cp
== '-' &&
593 *--cp
== '-' && (cp
> buffer
&& *--cp
== '\n')) {
598 /* The start and end boundaries didn't match, or the
599 start boundary doesn't begin with "\n--" (or "--"
600 if at the beginning of buffer). Keep trying. */
609 *part_boundary
= end_boundary
;
611 *part_boundary
= NULL
;
619 /* Open and copy ct->c_file to file, replacing the multipart boundary. */
621 replace_boundary (CT ct
, char *file
, char *boundary
) {
624 char buf
[BUFSIZ
], name
[NAMESZ
];
626 m_getfld_state_t gstate
= 0;
629 if (ct
->c_file
== NULL
) {
630 advise (NULL
, "missing input filename");
634 if ((fpin
= fopen (ct
->c_file
, "r")) == NULL
) {
635 advise (ct
->c_file
, "unable to open for reading");
639 if ((fpout
= fopen (file
, "w")) == NULL
) {
641 advise (file
, "unable to open for writing");
645 for (compnum
= 1;;) {
646 int bufsz
= (int) sizeof buf
;
648 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
)) {
653 /* get copies of the buffers */
654 np
= add (name
, NULL
);
655 vp
= add (buf
, NULL
);
657 /* if necessary, get rest of field */
658 while (state
== FLDPLUS
) {
660 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
);
661 vp
= add (buf
, vp
); /* add to previous value */
664 if (strcasecmp (TYPE_FIELD
, np
)) {
665 fprintf (fpout
, "%s:%s", np
, vp
);
667 char *new_ctline
, *new_params
;
669 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
670 &ct
->c_ctinfo
.ci_last_pm
, "boundary",
673 new_ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
674 ct
->c_ctinfo
.ci_subtype
, NULL
);
675 new_params
= output_params(strlen(TYPE_FIELD
) +
676 strlen(new_ctline
) + 1,
677 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
678 fprintf (fpout
, "%s:%s%s\n", np
, new_ctline
,
679 new_params
? new_params
: "");
692 /* buf will have a terminating NULL, skip it. */
693 if ((int) fwrite (buf
, 1, bufsz
-1, fpout
) < bufsz
-1) {
694 advise (file
, "fwrite");
703 advise (NULL
, "message format error in component #%d", compnum
);
708 advise (NULL
, "getfld() returned %d", state
);
716 m_getfld_state_destroy (&gstate
);
725 fix_multipart_cte (CT ct
, int *message_mods
) {
728 if (ct
->c_type
== CT_MULTIPART
) {
732 if (ct
->c_encoding
!= CE_7BIT
&& ct
->c_encoding
!= CE_8BIT
&&
733 ct
->c_encoding
!= CE_BINARY
) {
736 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
737 char *name
= hf
->name
;
738 for (; *name
&& isspace ((unsigned char) *name
); ++name
) {
742 if (! strncasecmp (name
, ENCODING_FIELD
,
743 strlen (ENCODING_FIELD
))) {
744 char *prefix
= "Nmh-REPLACED-INVALID-";
745 HF h
= mh_xmalloc (sizeof *h
);
747 h
->name
= add (hf
->name
, NULL
);
748 h
->hf_encoding
= hf
->hf_encoding
;
752 /* Retain old header but prefix its name. */
754 hf
->name
= concat (prefix
, h
->name
, NULL
);
758 char *encoding
= cpytrim (hf
->value
);
759 report (NULL
, ct
->c_partno
, ct
->c_file
,
760 "replace Content-Transfer-Encoding of %s "
761 "with 8 bit", encoding
);
765 h
->value
= add (" 8bit\n", NULL
);
767 /* Don't need to warn for multiple C-T-E header
768 fields, parse_mime() already does that. But
769 if there are any, fix them all as necessary. */
774 set_ce (ct
, CE_8BIT
);
777 m
= (struct multipart
*) ct
->c_ctparams
;
778 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
779 if (fix_multipart_cte (part
->mp_part
, message_mods
) != OK
) {
791 set_ce (CT ct
, int encoding
) {
792 const char *ce
= ce_str (encoding
);
793 const struct str2init
*ctinit
= get_ce_method (ce
);
796 char *cte
= concat (" ", ce
, "\n", NULL
);
799 /* Decoded contents might be in ct->c_cefile.ce_file, if the
800 caller is decode_text_parts (). Save because we'll
802 struct cefile decoded_content_info
= ct
->c_cefile
;
804 ct
->c_encoding
= encoding
;
806 ct
->c_ctinitfnx
= ctinit
->si_init
;
807 /* This will assign ct->c_cefile with an all-0 struct, which
809 (*ctinit
->si_init
) (ct
);
810 /* After returning, the caller should set
811 ct->c_cefile.ce_file to the name of the file containing
814 /* Restore the cefile. */
815 ct
->c_cefile
= decoded_content_info
;
817 /* Update/add Content-Transfer-Encoding header field. */
818 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
819 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
826 add_header (ct
, add (ENCODING_FIELD
, NULL
), cte
);
829 /* Update c_celine. It's used only by mhlist -debug. */
831 ct
->c_celine
= add (cte
, NULL
);
840 /* Make sure each text part has a corresponding text/plain part. */
842 ensure_text_plain (CT
*ct
, CT parent
, int *message_mods
, int replacetextplain
) {
845 switch ((*ct
)->c_type
) {
847 /* Nothing to do for text/plain. */
848 if ((*ct
)->c_subtype
== TEXT_PLAIN
) { return OK
; }
850 if (parent
&& parent
->c_type
== CT_MULTIPART
&&
851 parent
->c_subtype
== MULTI_ALTERNATE
) {
852 int new_subpart_number
= 1;
854 find_textplain_sibling (parent
, replacetextplain
,
855 &new_subpart_number
);
857 if (! has_text_plain
) {
858 /* Parent is a multipart/alternative. Insert a new
859 text/plain subpart. */
861 insert_new_text_plain_part (*ct
, new_subpart_number
,
866 report (NULL
, parent
->c_partno
, parent
->c_file
,
867 "insert text/plain part");
873 } else if (parent
&& parent
->c_type
== CT_MULTIPART
&&
874 parent
->c_subtype
== MULTI_RELATED
) {
876 concat ((*ct
)->c_ctinfo
.ci_type
, "/",
877 (*ct
)->c_ctinfo
.ci_subtype
, NULL
);
878 const char *parent_type
=
879 get_param (parent
->c_ctinfo
.ci_first_pm
, "type", '?', 1);
880 int new_subpart_number
= 1;
881 int has_text_plain
= 0;
883 /* Have to do string comparison on the subtype because we
884 don't enumerate all of them in c_subtype values.
885 parent_type will be NULL if the multipart/related part
886 doesn't have a type parameter. The type parameter must
887 be specified according to RFC 2387 Sec. 3.1 but not all
889 if (parent_type
&& strcasecmp (type_subtype
, parent_type
) == 0) {
890 /* The type of this part matches the root type of the
891 parent multipart/related. Look to see if there's
892 text/plain sibling. */
894 find_textplain_sibling (parent
, replacetextplain
,
895 &new_subpart_number
);
900 if (! has_text_plain
) {
901 /* Parent is a multipart/alternative. Insert a new
902 text/plain subpart. */
904 insert_new_text_plain_part (*ct
, new_subpart_number
,
909 report (NULL
, parent
->c_partno
, parent
->c_file
,
910 "insert text/plain 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
) {
967 struct exbody
*e
= (struct exbody
*) (*ct
)->c_ctparams
;
969 status
= ensure_text_plain (&e
->eb_content
, *ct
, message_mods
,
979 /* See if there is a sibling text/plain. */
981 find_textplain_sibling (CT parent
, int replacetextplain
,
982 int *new_subpart_number
) {
983 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
984 struct part
*part
, *prev
;
985 int has_text_plain
= 0;
987 for (prev
= part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
988 ++*new_subpart_number
;
989 if (part
->mp_part
->c_type
== CT_TEXT
&&
990 part
->mp_part
->c_subtype
== TEXT_PLAIN
) {
991 if (replacetextplain
) {
992 struct part
*old_part
;
993 if (part
== mp
->mp_parts
) {
994 old_part
= mp
->mp_parts
;
995 mp
->mp_parts
= part
->mp_next
;
997 old_part
= prev
->mp_next
;
998 prev
->mp_next
= part
->mp_next
;
1001 report (NULL
, parent
->c_partno
, parent
->c_file
,
1002 "remove text/plain part %s",
1003 old_part
->mp_part
->c_partno
);
1005 free_content (old_part
->mp_part
);
1015 return has_text_plain
;
1020 insert_new_text_plain_part (CT ct
, int new_subpart_number
, CT parent
) {
1021 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1022 struct part
*new_part
= mh_xmalloc (sizeof *new_part
);
1024 if ((new_part
->mp_part
= build_text_plain_part (ct
))) {
1026 snprintf (buffer
, sizeof buffer
, "%d", new_subpart_number
);
1028 new_part
->mp_next
= mp
->mp_parts
;
1029 mp
->mp_parts
= new_part
;
1030 new_part
->mp_part
->c_partno
=
1031 concat (parent
->c_partno
? parent
->c_partno
: "1", ".",
1036 free_content (new_part
->mp_part
);
1045 build_text_plain_part (CT encoded_part
) {
1046 CT tp_part
= divide_part (encoded_part
);
1047 char *tmp_plain_file
= NULL
;
1049 if (decode_part (tp_part
) == OK
) {
1050 /* Now, tp_part->c_cefile.ce_file is the name of the tmp file that
1051 contains the decoded contents. And the decoding function, such
1052 as openQuoted, will have set ...->ce_unlink to 1 so that it will
1053 be unlinked by free_content (). */
1056 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1057 advise (NULL
, "unable to create temporary file in %s",
1060 tmp_plain_file
= add (tempfile
, NULL
);
1061 if (reformat_part (tp_part
, tmp_plain_file
,
1062 tp_part
->c_ctinfo
.ci_type
,
1063 tp_part
->c_ctinfo
.ci_subtype
,
1064 tp_part
->c_type
) == OK
) {
1069 free_content (tp_part
);
1070 if (tmp_plain_file
) { (void) m_unlink (tmp_plain_file
); }
1071 free (tmp_plain_file
);
1078 divide_part (CT ct
) {
1081 if ((new_part
= (CT
) mh_xcalloc (1, sizeof *new_part
)) == NULL
)
1082 adios (NULL
, "out of memory");
1084 /* Just copy over what is needed for decoding. c_vrsn and
1085 c_celine aren't necessary. */
1086 new_part
->c_file
= add (ct
->c_file
, NULL
);
1087 new_part
->c_begin
= ct
->c_begin
;
1088 new_part
->c_end
= ct
->c_end
;
1089 copy_ctinfo (&new_part
->c_ctinfo
, &ct
->c_ctinfo
);
1090 new_part
->c_type
= ct
->c_type
;
1091 new_part
->c_cefile
= ct
->c_cefile
;
1092 new_part
->c_encoding
= ct
->c_encoding
;
1093 new_part
->c_ctinitfnx
= ct
->c_ctinitfnx
;
1094 new_part
->c_ceopenfnx
= ct
->c_ceopenfnx
;
1095 new_part
->c_ceclosefnx
= ct
->c_ceclosefnx
;
1096 new_part
->c_cesizefnx
= ct
->c_cesizefnx
;
1098 /* c_ctline is used by reformat__part(), so it can preserve
1099 anything after the type/subtype. */
1100 new_part
->c_ctline
= add (ct
->c_ctline
, NULL
);
1107 copy_ctinfo (CI dest
, CI src
) {
1110 dest
->ci_type
= src
->ci_type
? add (src
->ci_type
, NULL
) : NULL
;
1111 dest
->ci_subtype
= src
->ci_subtype
? add (src
->ci_subtype
, NULL
) : NULL
;
1113 for (s_pm
= src
->ci_first_pm
; s_pm
; s_pm
= s_pm
->pm_next
) {
1114 d_pm
= add_param(&dest
->ci_first_pm
, &dest
->ci_last_pm
, s_pm
->pm_name
,
1116 if (s_pm
->pm_charset
)
1117 d_pm
->pm_charset
= getcpy(s_pm
->pm_charset
);
1119 d_pm
->pm_lang
= getcpy(s_pm
->pm_lang
);
1122 dest
->ci_comment
= src
->ci_comment
? add (src
->ci_comment
, NULL
) : NULL
;
1123 dest
->ci_magic
= src
->ci_magic
? add (src
->ci_magic
, NULL
) : NULL
;
1128 decode_part (CT ct
) {
1133 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1134 adios (NULL
, "unable to create temporary file in %s", get_temp_dir());
1136 tmp_decoded
= add (tempfile
, NULL
);
1137 /* The following call will load ct->c_cefile.ce_file with the tmp
1138 filename of the decoded content. tmp_decoded will contain the
1139 encoded output, get rid of that. */
1140 status
= output_message (ct
, tmp_decoded
);
1141 (void) m_unlink (tmp_decoded
);
1148 /* Some of the arguments aren't really needed now, but maybe will
1149 be in the future for other than text types. */
1151 reformat_part (CT ct
, char *file
, char *type
, char *subtype
, int c_type
) {
1152 int output_subtype
, output_encoding
;
1156 /* Hacky: this redirects the output from whatever command is used
1157 to show the part to a file. So, the user can't have any output
1158 redirection in that command.
1159 Could show_multi() in mhshowsbr.c avoid this? */
1161 /* Check for invo_name-format-type/subtype. */
1162 if ((cf
= context_find_by_type ("format", type
, subtype
)) == NULL
) {
1164 advise (NULL
, "Don't know how to convert %s, there is no "
1165 "%s-format-%s/%s profile entry",
1166 ct
->c_file
, invo_name
, type
, subtype
);
1170 if (strchr (cf
, '>')) {
1171 advise (NULL
, "'>' prohibited in \"%s\",\nplease fix your "
1172 "%s-format-%s/%s profile entry", cf
, invo_name
, type
,
1173 subtype
? subtype
: "");
1179 cp
= concat (cf
, " >", file
, NULL
);
1180 status
= show_content_aux (ct
, 0, cp
, NULL
, NULL
);
1183 /* Unlink decoded content tmp file and free its filename to avoid
1184 leaks. The file stream should already have been closed. */
1185 if (ct
->c_cefile
.ce_unlink
) {
1186 (void) m_unlink (ct
->c_cefile
.ce_file
);
1187 free (ct
->c_cefile
.ce_file
);
1188 ct
->c_cefile
.ce_file
= NULL
;
1189 ct
->c_cefile
.ce_unlink
= 0;
1192 if (c_type
== CT_TEXT
) {
1193 output_subtype
= TEXT_PLAIN
;
1195 /* Set subtype to 0, which is always an UNKNOWN subtype. */
1198 output_encoding
= charset_encoding (ct
);
1200 if (set_ct_type (ct
, c_type
, output_subtype
, output_encoding
) == OK
) {
1201 ct
->c_cefile
.ce_file
= file
;
1202 ct
->c_cefile
.ce_unlink
= 1;
1204 ct
->c_cefile
.ce_unlink
= 0;
1212 /* Identifies 7bit or 8bit content based on charset. */
1214 charset_encoding (CT ct
) {
1215 char *ct_charset
= content_charset (ct
);
1216 int encoding
= strcasecmp (ct_charset
, "US-ASCII") ? CE_8BIT
: CE_7BIT
;
1225 build_multipart_alt (CT first_alt
, CT new_part
, int type
, int subtype
) {
1226 char *boundary_prefix
= "----=_nmh-multipart";
1227 char *boundary
= concat (boundary_prefix
, first_alt
->c_partno
, NULL
);
1228 char *boundary_indicator
= "; boundary=";
1229 char *typename
, *subtypename
, *name
;
1232 struct multipart
*m
;
1233 const struct str2init
*ctinit
;
1235 if ((ct
= (CT
) mh_xcalloc (1, sizeof *ct
)) == NULL
)
1236 adios (NULL
, "out of memory");
1238 /* Set up the multipart/alternative part. These fields of *ct were
1239 initialized to 0 by mh_xcalloc():
1240 c_fp, c_unlink, c_begin, c_end,
1241 c_vrsn, c_ctline, c_celine,
1242 c_id, c_descr, c_dispo, c_partno,
1243 c_ctinfo.ci_comment, c_ctinfo.ci_magic,
1244 c_cefile, c_encoding,
1245 c_digested, c_digest[16], c_ctexbody,
1246 c_ctinitfnx, c_ceopenfnx, c_ceclosefnx, c_cesizefnx,
1248 c_showproc, c_termproc, c_storeproc, c_storage, c_folder
1251 ct
->c_file
= add (first_alt
->c_file
, NULL
);
1253 ct
->c_subtype
= subtype
;
1255 ctinit
= get_ct_init (ct
->c_type
);
1257 typename
= ct_type_str (type
);
1258 subtypename
= ct_subtype_str (type
, subtype
);
1262 int found_boundary
= 1;
1264 while (found_boundary
&& serial
< 1000000) {
1267 /* Ensure that the boundary doesn't appear in the decoded
1269 if (new_part
->c_cefile
.ce_file
) {
1270 if ((found_boundary
=
1271 boundary_in_content (&new_part
->c_cefile
.ce_fp
,
1272 new_part
->c_cefile
.ce_file
,
1279 /* Ensure that the boundary doesn't appear in the encoded
1281 if (! found_boundary
&& new_part
->c_file
) {
1282 if ((found_boundary
= boundary_in_content (&new_part
->c_fp
,
1290 if (found_boundary
) {
1291 /* Try a slightly different boundary. */
1296 snprintf (buffer2
, sizeof buffer2
, "%d", serial
);
1298 concat (boundary_prefix
,
1299 first_alt
->c_partno
? first_alt
->c_partno
: "",
1300 "-", buffer2
, NULL
);
1304 if (found_boundary
) {
1305 advise (NULL
, "giving up trying to find a unique boundary");
1311 name
= concat (" ", typename
, "/", subtypename
, boundary_indicator
, "\"",
1312 boundary
, "\"", NULL
);
1314 /* Load c_first_hf and c_last_hf. */
1315 transfer_noncontent_headers (first_alt
, ct
);
1316 add_header (ct
, add (TYPE_FIELD
, NULL
), concat (name
, "\n", NULL
));
1319 /* Load c_partno. */
1320 if (first_alt
->c_partno
) {
1321 ct
->c_partno
= add (first_alt
->c_partno
, NULL
);
1322 free (first_alt
->c_partno
);
1323 first_alt
->c_partno
= concat (ct
->c_partno
, ".1", NULL
);
1324 new_part
->c_partno
= concat (ct
->c_partno
, ".2", NULL
);
1326 first_alt
->c_partno
= add ("1", NULL
);
1327 new_part
->c_partno
= add ("2", NULL
);
1331 ct
->c_ctinfo
.ci_type
= add (typename
, NULL
);
1332 ct
->c_ctinfo
.ci_subtype
= add (subtypename
, NULL
);
1335 add_param(&ct
->c_ctinfo
.ci_first_pm
, &ct
->c_ctinfo
.ci_last_pm
,
1336 "boundary", boundary
, 0);
1338 p
= (struct part
*) mh_xmalloc (sizeof *p
);
1339 p
->mp_next
= (struct part
*) mh_xmalloc (sizeof *p
->mp_next
);
1340 p
->mp_next
->mp_next
= NULL
;
1341 p
->mp_next
->mp_part
= first_alt
;
1343 if ((m
= (struct multipart
*) mh_xcalloc (1, sizeof (struct multipart
))) ==
1345 adios (NULL
, "out of memory");
1346 m
->mp_start
= concat (boundary
, "\n", NULL
);
1347 m
->mp_stop
= concat (boundary
, "--\n", NULL
);
1357 /* Check that the boundary does not appear in the content. */
1359 boundary_in_content (FILE **fp
, char *file
, const char *boundary
) {
1360 char buffer
[BUFSIZ
];
1362 int found_boundary
= 0;
1364 /* free_content() will close *fp if we fopen it here. */
1365 if (! *fp
&& (*fp
= fopen (file
, "r")) == NULL
) {
1366 advise (file
, "unable to open %s for reading", file
);
1370 fseeko (*fp
, 0L, SEEK_SET
);
1371 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) > 0) {
1372 if (find_str (buffer
, bytes_read
, boundary
)) {
1378 return found_boundary
;
1382 /* Remove all non-Content headers. */
1384 transfer_noncontent_headers (CT old
, CT
new) {
1387 hp_prev
= hp
= old
->c_first_hf
;
1391 if (strncasecmp (XXX_FIELD_PRF
, hp
->name
, strlen (XXX_FIELD_PRF
))) {
1392 if (hp
== old
->c_last_hf
) {
1393 if (hp
== old
->c_first_hf
) {
1394 old
->c_last_hf
= old
->c_first_hf
= NULL
;
1396 hp_prev
->next
= NULL
;
1397 old
->c_last_hf
= hp_prev
;
1400 if (hp
== old
->c_first_hf
) {
1401 old
->c_first_hf
= next
;
1403 hp_prev
->next
= next
;
1407 /* Put node hp in the new CT. */
1408 if (new->c_first_hf
== NULL
) {
1409 new->c_first_hf
= hp
;
1411 new->c_last_hf
->next
= hp
;
1413 new->c_last_hf
= hp
;
1415 /* A Content- header, leave in old. */
1425 set_ct_type (CT ct
, int type
, int subtype
, int encoding
) {
1426 char *typename
= ct_type_str (type
);
1427 char *subtypename
= ct_subtype_str (type
, subtype
);
1428 /* E.g, " text/plain" */
1429 char *type_subtypename
= concat (" ", typename
, "/", subtypename
, NULL
);
1430 /* E.g, " text/plain\n" */
1431 char *name_plus_nl
= concat (type_subtypename
, "\n", NULL
);
1432 int found_content_type
= 0;
1434 const char *cp
= NULL
;
1438 /* Update/add Content-Type header field. */
1439 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1440 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1441 found_content_type
= 1;
1443 hf
->value
= (cp
= strchr (ct
->c_ctline
, ';'))
1444 ? concat (type_subtypename
, cp
, "\n", NULL
)
1445 : add (name_plus_nl
, NULL
);
1448 if (! found_content_type
) {
1449 add_header (ct
, add (TYPE_FIELD
, NULL
),
1450 (cp
= strchr (ct
->c_ctline
, ';'))
1451 ? concat (type_subtypename
, cp
, "\n", NULL
)
1452 : add (name_plus_nl
, NULL
));
1455 /* Some of these might not be used, but set them anyway. */
1457 ? concat (type_subtypename
, cp
, NULL
)
1458 : concat (type_subtypename
, NULL
);
1459 free (ct
->c_ctline
);
1460 ct
->c_ctline
= ctline
;
1461 /* Leave other ctinfo members as they were. */
1462 free (ct
->c_ctinfo
.ci_type
);
1463 ct
->c_ctinfo
.ci_type
= add (typename
, NULL
);
1464 free (ct
->c_ctinfo
.ci_subtype
);
1465 ct
->c_ctinfo
.ci_subtype
= add (subtypename
, NULL
);
1467 ct
->c_subtype
= subtype
;
1469 free (name_plus_nl
);
1470 free (type_subtypename
);
1472 status
= set_ce (ct
, encoding
);
1479 decode_text_parts (CT ct
, int encoding
, int *message_mods
) {
1482 switch (ct
->c_type
) {
1484 switch (ct
->c_encoding
) {
1489 if (decode_part (ct
) == OK
&& ct
->c_cefile
.ce_file
) {
1490 const char *reason
= NULL
;
1492 if ((ct_encoding
= content_encoding (ct
, &reason
)) == CE_BINARY
1493 && encoding
!= CE_BINARY
) {
1494 /* The decoding isn't acceptable so discard it.
1495 Leave status as OK to allow other transformations. */
1497 report (NULL
, ct
->c_partno
, ct
->c_file
,
1498 "will not decode%s because it is binary (%s)",
1500 : ct
->c_ctline
? ct
->c_ctline
1504 (void) m_unlink (ct
->c_cefile
.ce_file
);
1505 free (ct
->c_cefile
.ce_file
);
1506 ct
->c_cefile
.ce_file
= NULL
;
1507 } else if (ct
->c_encoding
== CE_QUOTED
&&
1508 ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
1509 /* The decoding isn't acceptable so discard it.
1510 Leave status as OK to allow other transformations. */
1512 report (NULL
, ct
->c_partno
, ct
->c_file
,
1513 "will not decode%s because it is 8bit",
1515 : ct
->c_ctline
? ct
->c_ctline
1518 (void) m_unlink (ct
->c_cefile
.ce_file
);
1519 free (ct
->c_cefile
.ce_file
);
1520 ct
->c_cefile
.ce_file
= NULL
;
1523 if (ct_encoding
== CE_BINARY
)
1525 else if (ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
)
1528 enc
= charset_encoding (ct
);
1529 if (set_ce (ct
, enc
) == OK
) {
1532 report (NULL
, ct
->c_partno
, ct
->c_file
, "decode%s",
1533 ct
->c_ctline
? ct
->c_ctline
: "");
1535 strip_crs (ct
, message_mods
);
1547 strip_crs (ct
, message_mods
);
1555 case CT_MULTIPART
: {
1556 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1559 /* Should check to see if the body for this part is encoded?
1560 For now, it gets passed along as-is by InitMultiPart(). */
1561 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1562 status
= decode_text_parts (part
->mp_part
, encoding
, message_mods
);
1568 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1569 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
1571 status
= decode_text_parts (e
->eb_content
, encoding
, message_mods
);
1583 /* See if the decoded content is 7bit, 8bit, or binary. It's binary
1584 if it has any NUL characters, a CR not followed by a LF, or lines
1585 greater than 998 characters in length. If binary, reason is set
1586 to a string explaining why. */
1588 content_encoding (CT ct
, const char **reason
) {
1589 CE ce
= &ct
->c_cefile
;
1590 int encoding
= CE_7BIT
;
1593 size_t line_len
= 0;
1594 char buffer
[BUFSIZ
];
1597 if (! ce
->ce_fp
&& (ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
1598 advise (ce
->ce_file
, "unable to open for reading");
1602 fseeko (ce
->ce_fp
, 0L, SEEK_SET
);
1603 while (encoding
!= CE_BINARY
&&
1604 (inbytes
= fread (buffer
, 1, sizeof buffer
, ce
->ce_fp
)) > 0) {
1607 int last_char_was_cr
= 0;
1609 for (i
= 0, cp
= buffer
; i
< inbytes
; ++i
, ++cp
) {
1610 if (*cp
== '\0' || ++line_len
> 998 ||
1611 (*cp
!= '\n' && last_char_was_cr
)) {
1612 encoding
= CE_BINARY
;
1614 *reason
= "null character";
1615 } else if (line_len
> 998) {
1616 *reason
= "line length > 998";
1617 } else if (*cp
!= '\n' && last_char_was_cr
) {
1618 *reason
= "CR not followed by LF";
1620 /* Should not reach this. */
1624 } else if (*cp
== '\n') {
1626 } else if (! isascii ((unsigned char) *cp
)) {
1630 last_char_was_cr
= *cp
== '\r' ? 1 : 0;
1636 } /* else should never happen */
1643 strip_crs (CT ct
, int *message_mods
) {
1644 char *charset
= content_charset (ct
);
1647 /* Only strip carriage returns if content is ASCII or another
1648 charset that has the same readily recognizable CR followed by a
1649 LF. We can include UTF-8 here because if the high-order bit of
1650 a UTF-8 byte is 0, then it must be a single-byte ASCII
1652 if (! strcasecmp (charset
, "US-ASCII") ||
1653 ! strcasecmp (charset
, "UTF-8") ||
1654 ! strncasecmp (charset
, "ISO-8859-", 9) ||
1655 ! strncasecmp (charset
, "WINDOWS-12", 10)) {
1661 int opened_input_file
= 0;
1663 if (ct
->c_cefile
.ce_file
) {
1664 file
= &ct
->c_cefile
.ce_file
;
1665 fp
= &ct
->c_cefile
.ce_fp
;
1667 } else if (ct
->c_file
) {
1670 begin
= (size_t) ct
->c_begin
;
1671 end
= (size_t) ct
->c_end
;
1672 } /* else don't know where the content is */
1674 if (file
&& *file
&& fp
) {
1676 if ((*fp
= fopen (*file
, "r")) == NULL
) {
1677 advise (*file
, "unable to open for reading");
1680 opened_input_file
= 1;
1686 char buffer
[BUFSIZ
];
1688 size_t bytes_to_read
=
1689 end
> 0 && end
> begin
? end
- begin
: sizeof buffer
;
1691 fseeko (*fp
, begin
, SEEK_SET
);
1692 while ((bytes_read
= fread (buffer
, 1,
1693 min (bytes_to_read
, sizeof buffer
),
1695 /* Look for CR followed by a LF. This is supposed to
1696 be text so there should be LF's. If not, don't
1697 modify the content. */
1700 int last_char_was_cr
= 0;
1702 if (end
> 0) { bytes_to_read
-= bytes_read
; }
1704 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
1705 if (*cp
== '\n' && last_char_was_cr
) {
1710 last_char_was_cr
= *cp
== '\r' ? 1 : 0;
1716 char *stripped_content_file
;
1717 char *tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
);
1719 if (tempfile
== NULL
) {
1720 adios (NULL
, "unable to create temporary file in %s",
1723 stripped_content_file
= add (tempfile
, NULL
);
1725 /* Strip each CR before a LF from the content. */
1726 fseeko (*fp
, begin
, SEEK_SET
);
1727 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) >
1731 int last_char_was_cr
= 0;
1733 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
1735 last_char_was_cr
= 1;
1736 } else if (last_char_was_cr
) {
1738 if (write (fd
, "\r", 1) < 0) {
1739 advise (tempfile
, "CR write");
1742 if (write (fd
, cp
, 1) < 0) {
1743 advise (tempfile
, "write");
1745 last_char_was_cr
= 0;
1747 if (write (fd
, cp
, 1) < 0) {
1748 advise (tempfile
, "write");
1750 last_char_was_cr
= 0;
1756 admonish (NULL
, "unable to write temporary file %s",
1757 stripped_content_file
);
1758 (void) m_unlink (stripped_content_file
);
1761 /* Replace the decoded file with the converted one. */
1762 if (ct
->c_cefile
.ce_file
) {
1763 if (ct
->c_cefile
.ce_unlink
) {
1764 (void) m_unlink (ct
->c_cefile
.ce_file
);
1766 free (ct
->c_cefile
.ce_file
);
1768 ct
->c_cefile
.ce_file
= stripped_content_file
;
1769 ct
->c_cefile
.ce_unlink
= 1;
1773 report (NULL
, ct
->c_partno
,
1774 begin
== 0 && end
== 0 ? "" : *file
,
1780 if (opened_input_file
) {
1794 convert_charsets (CT ct
, char *dest_charset
, int *message_mods
) {
1797 switch (ct
->c_type
) {
1799 if (ct
->c_subtype
== TEXT_PLAIN
) {
1800 status
= convert_charset (ct
, dest_charset
, message_mods
);
1803 char *ct_charset
= content_charset (ct
);
1805 report (NULL
, ct
->c_partno
, ct
->c_file
,
1806 "convert %s to %s", ct_charset
, dest_charset
);
1810 char *ct_charset
= content_charset (ct
);
1812 report ("iconv", ct
->c_partno
, ct
->c_file
,
1813 "failed to convert %s to %s", ct_charset
, dest_charset
);
1819 case CT_MULTIPART
: {
1820 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1823 /* Should check to see if the body for this part is encoded?
1824 For now, it gets passed along as-is by InitMultiPart(). */
1825 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1827 convert_charsets (part
->mp_part
, dest_charset
, message_mods
);
1833 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1834 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
1837 convert_charsets (e
->eb_content
, dest_charset
, message_mods
);
1850 write_content (CT ct
, char *input_filename
, char *outfile
, int modify_inplace
,
1854 if (modify_inplace
) {
1855 if (message_mods
> 0) {
1856 if ((status
= output_message (ct
, outfile
)) == OK
) {
1857 char *infile
= input_filename
1858 ? add (input_filename
, NULL
)
1859 : add (ct
->c_file
? ct
->c_file
: "-", NULL
);
1861 if (remove_file (infile
) == OK
) {
1862 if (rename (outfile
, infile
)) {
1863 /* Rename didn't work, possibly because of an
1864 attempt to rename across filesystems. Try
1865 brute force copy. */
1866 int old
= open (outfile
, O_RDONLY
);
1868 open (infile
, O_WRONLY
| O_CREAT
, m_gmprot ());
1871 if (old
!= -1 && new != -1) {
1872 char buffer
[BUFSIZ
];
1874 while ((i
= read (old
, buffer
, sizeof buffer
)) >
1876 if (write (new, buffer
, i
) != i
) {
1882 if (new != -1) { close (new); }
1883 if (old
!= -1) { close (old
); }
1884 (void) m_unlink (outfile
);
1887 /* The -file argument processing used path() to
1888 expand filename to absolute path. */
1889 int file
= ct
->c_file
&& ct
->c_file
[0] == '/';
1891 admonish (NULL
, "unable to rename %s %s to %s",
1892 file
? "file" : "message", outfile
,
1898 admonish (NULL
, "unable to remove input file %s, "
1899 "not modifying it", infile
);
1900 (void) m_unlink (outfile
);
1909 /* No modifications and didn't need the tmp outfile. */
1910 (void) m_unlink (outfile
);
1913 /* Output is going to some file. Produce it whether or not
1914 there were modifications. */
1915 status
= output_message (ct
, outfile
);
1924 * If "rmmproc" is defined, call that to remove the file. Otherwise,
1925 * use the standard MH backup file.
1928 remove_file (char *file
) {
1930 char *rmm_command
= concat (rmmproc
, " ", file
, NULL
);
1931 int status
= system (rmm_command
);
1934 return WIFEXITED (status
) ? WEXITSTATUS (status
) : NOTOK
;
1936 /* This is OK for a non-message file, it still uses the
1937 BACKUP_PREFIX form. The backup file will be in the same
1938 directory as file. */
1939 return rename (file
, m_backup (file
));
1945 report (char *what
, char *partno
, char *filename
, char *message
, ...) {
1950 va_start (args
, message
);
1951 fmt
= concat (filename
, partno
? " part " : ", ",
1952 partno
? partno
: "", partno
? ", " : "", message
, NULL
);
1954 advertise (what
, NULL
, fmt
, args
);
1967 fprintf (stderr
, "\n");