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("fixtype mimetype", 0, FIXTYPESW) \
30 X("file file", 0, FILESW) \
31 X("outfile file", 0, OUTFILESW) \
32 X("rmmproc program", 0, RPROCSW) \
33 X("normmproc", 0, NRPRCSW) \
34 X("changecur", 0, CHGSW) \
35 X("nochangecur", 0, NCHGSW) \
36 X("verbose", 0, VERBSW) \
37 X("noverbose", 0, NVERBSW) \
38 X("version", 0, VERSIONSW) \
39 X("help", 0, HELPSW) \
41 #define X(sw, minchars, id) id,
42 DEFINE_SWITCH_ENUM(MHFIXMSG
);
45 #define X(sw, minchars, id) { sw, minchars, id },
46 DEFINE_SWITCH_ARRAY(MHFIXMSG
, switches
);
51 int debugsw
; /* Needed by mhparse.c. */
53 #define quitser pipeser
56 extern int skip_mp_cte_check
; /* flag to InitMultiPart */
57 extern int suppress_bogus_mp_content_warning
; /* flag to InitMultiPart */
58 extern int bogus_mp_content
; /* flag from InitMultiPart */
59 /* flags to/from parse_header_attrs */
60 extern int suppress_extraneous_trailing_semicolon_warning
;
61 extern int extraneous_trailing_semicolon
;
64 int output_message (CT
, char *);
67 void flush_errors (void);
71 void freects_done (int) NORETURN
;
76 typedef struct fix_transformations
{
84 } fix_transformations
;
86 int mhfixmsgsbr (CT
*, const fix_transformations
*, char *);
87 static int fix_boundary (CT
*, int *);
88 static int get_multipart_boundary (CT
, char **);
89 static int replace_boundary (CT
, char *, char *);
90 static int fix_types (CT
, svector_t
, int *);
91 static char *replace_substring (char **, const char *, const char *);
92 static char *remove_parameter (char *, const char *);
93 static int fix_multipart_cte (CT
, int *);
94 static int set_ce (CT
, int);
95 static int ensure_text_plain (CT
*, CT
, int *, int);
96 static int find_textplain_sibling (CT
, int, int *);
97 static int insert_new_text_plain_part (CT
, int, CT
);
98 static CT
build_text_plain_part (CT
);
99 static int insert_into_new_mp_alt (CT
*, int *);
100 static CT
divide_part (CT
);
101 static void copy_ctinfo (CI
, CI
);
102 static int decode_part (CT
);
103 static int reformat_part (CT
, char *, char *, char *, int);
104 static int charset_encoding (CT
);
105 static CT
build_multipart_alt (CT
, CT
, int, int);
106 static int boundary_in_content (FILE **, char *, const char *);
107 static void transfer_noncontent_headers (CT
, CT
);
108 static int set_ct_type (CT
, int type
, int subtype
, int encoding
);
109 static int decode_text_parts (CT
, int, int *);
110 static int content_encoding (CT
, const char **);
111 static int strip_crs (CT
, int *);
112 static int convert_charsets (CT
, char *, int *);
113 static int fix_always (CT
, int *);
114 static int write_content (CT
, char *, char *, int, int);
115 static int remove_file (char *);
116 static void report (char *, char *, char *, char *, ...);
117 static void pipeser (int);
121 main (int argc
, char **argv
) {
123 char *cp
, *file
= NULL
, *folder
= NULL
;
124 char *maildir
, buf
[100], *outfile
= NULL
;
125 char **argp
, **arguments
;
126 struct msgs_array msgs
= { 0, 0, NULL
};
127 struct msgs
*mp
= NULL
;
133 fix_transformations fx
;
134 fx
.reformat
= fx
.fixcte
= fx
.fixboundary
= 1;
136 fx
.replacetextplain
= 0;
137 fx
.decodetext
= CE_8BIT
;
138 fx
.textcharset
= NULL
;
140 if (nmh_init(argv
[0], 1)) { return 1; }
144 arguments
= getarguments (invo_name
, argc
, argv
, 1);
150 while ((cp
= *argp
++)) {
152 switch (smatch (++cp
, switches
)) {
154 ambigsw (cp
, switches
);
157 adios (NULL
, "-%s unknown", cp
);
160 snprintf (buf
, sizeof buf
, "%s [+folder] [msgs] [switches]",
162 print_help (buf
, switches
, 1);
165 print_version(invo_name
);
169 if (! (cp
= *argp
++) || *cp
== '-')
170 adios (NULL
, "missing argument to %s", argp
[-2]);
171 if (! strcasecmp (cp
, "8bit")) {
172 fx
.decodetext
= CE_8BIT
;
173 } else if (! strcasecmp (cp
, "7bit")) {
174 fx
.decodetext
= CE_7BIT
;
176 adios (NULL
, "invalid argument to %s", argp
[-2]);
183 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
184 adios (NULL
, "missing argument to %s", argp
[-2]);
203 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
204 adios (NULL
, "missing argument to %s", argp
[-2]);
205 if (! strncasecmp (cp
, "multipart/", 10) ||
206 ! strncasecmp (cp
, "message/", 8)) {
207 adios (NULL
, "-fixtype %s not allowed", cp
);
208 } else if (! strchr (cp
, '/')) {
209 adios (NULL
, "-fixtype requires type/subtype");
211 if (fx
.fixtypes
== NULL
) { fx
.fixtypes
= svector_create (10); }
212 svector_push_back (fx
.fixtypes
, cp
);
220 case REPLACETEXTPLAINSW
:
221 fx
.replacetextplain
= 1;
223 case NREPLACETEXTPLAINSW
:
224 fx
.replacetextplain
= 0;
227 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
228 adios (NULL
, "missing argument to %s", argp
[-2]);
229 file
= *cp
== '-' ? add (cp
, NULL
) : path (cp
, TFILE
);
232 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
233 adios (NULL
, "missing argument to %s", argp
[-2]);
234 outfile
= *cp
== '-' ? add (cp
, NULL
) : path (cp
, TFILE
);
237 if (!(rmmproc
= *argp
++) || *rmmproc
== '-')
238 adios (NULL
, "missing argument to %s", argp
[-2]);
257 if (*cp
== '+' || *cp
== '@') {
259 adios (NULL
, "only one folder at a time!");
261 folder
= pluspath (cp
);
264 /* Interpret a full path as a filename, not a message. */
265 file
= add (cp
, NULL
);
267 app_msgarg (&msgs
, cp
);
272 SIGNAL (SIGQUIT
, quitser
);
273 SIGNAL (SIGPIPE
, pipeser
);
276 * Read the standard profile setup
278 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
279 readconfig ((struct node
**) 0, fp
, cp
, 0);
283 suppress_bogus_mp_content_warning
= skip_mp_cte_check
= 1;
284 suppress_extraneous_trailing_semicolon_warning
= 1;
286 if (! context_find ("path"))
287 free (path ("./", TFOLDER
));
289 if (file
&& msgs
.size
)
290 adios (NULL
, "cannot specify msg and file at same time!");
293 * check if message is coming from file
296 /* If file is stdin, create a tmp file name before parse_mime()
297 has a chance, because it might put in on a different
298 filesystem than the output file. Instead, put it in the
299 user's preferred tmp directory. */
302 if (! strcmp ("-", file
)) {
308 if ((cp
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
309 adios (NULL
, "unable to create temporary file in %s",
313 file
= add (cp
, NULL
);
314 cpydata (STDIN_FILENO
, fd
, "-", file
);
318 (void) m_unlink (file
);
319 adios (NULL
, "failed to write temporary file");
323 if (! (cts
= (CT
*) mh_xcalloc ((size_t) 2, sizeof *cts
))) {
324 adios (NULL
, "out of memory");
328 if ((ct
= parse_mime (file
))) { *ctp
++ = ct
; }
331 * message(s) are coming from a folder
336 app_msgarg(&msgs
, "cur");
338 folder
= getfolder (1);
339 maildir
= m_maildir (folder
);
341 if (chdir (maildir
) == NOTOK
)
342 adios (maildir
, "unable to change directory to");
344 /* read folder and create message structure */
345 if (! (mp
= folder_read (folder
, 1)))
346 adios (NULL
, "unable to read folder %s", folder
);
348 /* check for empty folder */
350 adios (NULL
, "no messages in %s", folder
);
352 /* parse all the message ranges/sequences and set SELECTED */
353 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
354 if (! m_convert (mp
, msgs
.msgs
[msgnum
]))
356 seq_setprev (mp
); /* set the previous-sequence */
359 (CT
*) mh_xcalloc ((size_t) (mp
->numsel
+ 1), sizeof *cts
))) {
360 adios (NULL
, "out of memory");
364 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
365 if (is_selected(mp
, msgnum
)) {
368 msgnam
= m_name (msgnum
);
369 if ((ct
= parse_mime (msgnam
))) { *ctp
++ = ct
; }
374 seq_setcur (mp
, mp
->hghsel
); /* update current message */
376 seq_save (mp
); /* synchronize sequences */
377 context_replace (pfolder
, folder
);/* update current folder */
378 context_save (); /* save the context file */
382 for (ctp
= cts
; *ctp
; ++ctp
) {
383 status
+= mhfixmsgsbr (ctp
, &fx
, outfile
);
386 (void) m_unlink (file
);
389 /* Just calling m_backup() unlinks the backup file. */
390 (void) m_backup (file
);
398 if (fx
.fixtypes
!= NULL
) { svector_free (fx
.fixtypes
); }
402 /* done is freects_done, which will clean up all of cts. */
409 mhfixmsgsbr (CT
*ctp
, const fix_transformations
*fx
, char *outfile
) {
410 /* Store input filename in case one of the transformations, i.e.,
411 fix_boundary(), rewrites to a tmp file. */
412 char *input_filename
= add ((*ctp
)->c_file
, NULL
);
413 int modify_inplace
= 0;
414 int message_mods
= 0;
417 if (outfile
== NULL
) {
420 if ((*ctp
)->c_file
) {
422 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
423 adios (NULL
, "unable to create temporary file in %s",
426 outfile
= add (tempfile
, NULL
);
428 adios (NULL
, "missing both input and output filenames\n");
432 reverse_alternative_parts (*ctp
);
433 status
= fix_always (*ctp
, &message_mods
);
434 if (status
== OK
&& fx
->fixboundary
) {
435 status
= fix_boundary (ctp
, &message_mods
);
437 if (status
== OK
&& fx
->fixtypes
!= NULL
) {
438 status
= fix_types (*ctp
, fx
->fixtypes
, &message_mods
);
440 if (status
== OK
&& fx
->fixcte
) {
441 status
= fix_multipart_cte (*ctp
, &message_mods
);
443 if (status
== OK
&& fx
->reformat
) {
445 ensure_text_plain (ctp
, NULL
, &message_mods
, fx
->replacetextplain
);
447 if (status
== OK
&& fx
->decodetext
) {
448 status
= decode_text_parts (*ctp
, fx
->decodetext
, &message_mods
);
450 if (status
== OK
&& fx
->textcharset
!= NULL
) {
451 status
= convert_charsets (*ctp
, fx
->textcharset
, &message_mods
);
454 if (status
== OK
&& ! (*ctp
)->c_umask
) {
455 /* Set the umask for the contents file. This currently
456 isn't used but just in case it is in the future. */
459 if (stat ((*ctp
)->c_file
, &st
) != NOTOK
) {
460 (*ctp
)->c_umask
= ~(st
.st_mode
& 0777);
462 (*ctp
)->c_umask
= ~m_gmprot();
467 * Write the content to a file
470 status
= write_content (*ctp
, input_filename
, outfile
, modify_inplace
,
472 } else if (! modify_inplace
) {
473 /* Something went wrong. Output might be expected, such
474 as if this were run as a filter. Just copy the input
476 int in
= open (input_filename
, O_RDONLY
);
477 int out
= strcmp (outfile
, "-")
478 ? open (outfile
, O_WRONLY
| O_CREAT
, m_gmprot ())
481 if (in
!= -1 && out
!= -1) {
482 cpydata (in
, out
, input_filename
, outfile
);
491 if (modify_inplace
) {
492 if (status
!= OK
) { (void) m_unlink (outfile
); }
497 free (input_filename
);
504 fix_boundary (CT
*ct
, int *message_mods
) {
505 struct multipart
*mp
;
508 if (ct
&& (*ct
)->c_type
== CT_MULTIPART
&& bogus_mp_content
) {
509 mp
= (struct multipart
*) (*ct
)->c_ctparams
;
512 * 1) Get boundary at end of part.
513 * 2) Get boundary at beginning of part and compare to the end-of-part
515 * 3) Write out contents of ct to tmp file, replacing boundary in
516 * header with boundary from part. Set c_unlink to 1.
518 * 5) Call parse_mime() on the tmp file, replacing ct.
521 if (mp
&& mp
->mp_start
) {
524 if (get_multipart_boundary (*ct
, &part_boundary
) == OK
) {
527 if ((fixed
= m_mktemp2 (NULL
, invo_name
, NULL
, &(*ct
)->c_fp
))) {
528 if (replace_boundary (*ct
, fixed
, part_boundary
) == OK
) {
529 char *filename
= add ((*ct
)->c_file
, NULL
);
533 if ((fixed_ct
= parse_mime (fixed
))) {
539 report (NULL
, NULL
, filename
,
540 "fix multipart boundary");
544 advise (NULL
, "unable to parse fixed part");
549 advise (NULL
, "unable to replace broken boundary");
553 advise (NULL
, "unable to create temporary file in %s",
558 free (part_boundary
);
568 get_multipart_boundary (CT ct
, char **part_boundary
) {
570 char *end_boundary
= NULL
;
571 off_t begin
= (off_t
) ct
->c_end
> (off_t
) (ct
->c_begin
+ sizeof buffer
)
572 ? (off_t
) (ct
->c_end
- sizeof buffer
)
573 : (off_t
) ct
->c_begin
;
577 /* This will fail if the boundary spans fread() calls. BUFSIZ should
578 be big enough, even if it's just 1024, to make that unlikely. */
580 /* free_content() will close ct->c_fp. */
581 if (! ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
582 advise (ct
->c_file
, "unable to open for reading");
586 /* Get boundary at end of multipart. */
587 while (begin
>= (off_t
) ct
->c_begin
) {
588 fseeko (ct
->c_fp
, begin
, SEEK_SET
);
589 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
590 char *cp
= rfind_str (buffer
, bytes_read
, "--");
595 /* Trim off trailing "--" and anything beyond. */
597 if ((end
= rfind_str (buffer
, cp
- buffer
, "\n"))) {
598 if (strlen (end
) > 3 && *end
++ == '\n' &&
599 *end
++ == '-' && *end
++ == '-') {
600 end_boundary
= add (end
, NULL
);
607 if (! end_boundary
&& begin
> (off_t
) (ct
->c_begin
+ sizeof buffer
)) {
608 begin
-= sizeof buffer
;
614 /* Get boundary at beginning of multipart. */
616 fseeko (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
617 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
618 if (bytes_read
>= strlen (end_boundary
)) {
619 char *cp
= find_str (buffer
, bytes_read
, end_boundary
);
621 if (cp
&& cp
- buffer
>= 2 && *--cp
== '-' &&
622 *--cp
== '-' && (cp
> buffer
&& *--cp
== '\n')) {
627 /* The start and end boundaries didn't match, or the
628 start boundary doesn't begin with "\n--" (or "--"
629 if at the beginning of buffer). Keep trying. */
638 *part_boundary
= end_boundary
;
640 *part_boundary
= NULL
;
648 /* Open and copy ct->c_file to file, replacing the multipart boundary. */
650 replace_boundary (CT ct
, char *file
, char *boundary
) {
653 char buf
[BUFSIZ
], name
[NAMESZ
];
655 m_getfld_state_t gstate
= 0;
658 if (ct
->c_file
== NULL
) {
659 advise (NULL
, "missing input filename");
663 if ((fpin
= fopen (ct
->c_file
, "r")) == NULL
) {
664 advise (ct
->c_file
, "unable to open for reading");
668 if ((fpout
= fopen (file
, "w")) == NULL
) {
670 advise (file
, "unable to open for writing");
674 for (compnum
= 1;;) {
675 int bufsz
= (int) sizeof buf
;
677 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
)) {
682 /* get copies of the buffers */
683 np
= add (name
, NULL
);
684 vp
= add (buf
, NULL
);
686 /* if necessary, get rest of field */
687 while (state
== FLDPLUS
) {
689 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
);
690 vp
= add (buf
, vp
); /* add to previous value */
693 if (strcasecmp (TYPE_FIELD
, np
)) {
694 fprintf (fpout
, "%s:%s", np
, vp
);
696 char *new_ctline
, *new_params
;
698 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
699 &ct
->c_ctinfo
.ci_last_pm
, "boundary",
702 new_ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
703 ct
->c_ctinfo
.ci_subtype
, NULL
);
704 new_params
= output_params(strlen(TYPE_FIELD
) +
705 strlen(new_ctline
) + 1,
706 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
707 fprintf (fpout
, "%s:%s%s\n", np
, new_ctline
,
708 new_params
? new_params
: "");
721 /* buf will have a terminating NULL, skip it. */
722 if ((int) fwrite (buf
, 1, bufsz
-1, fpout
) < bufsz
-1) {
723 advise (file
, "fwrite");
732 advise (NULL
, "message format error in component #%d", compnum
);
737 advise (NULL
, "getfld() returned %d", state
);
745 m_getfld_state_destroy (&gstate
);
754 fix_types (CT ct
, svector_t fixtypes
, int *message_mods
) {
757 switch (ct
->c_type
) {
759 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
762 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
763 status
= fix_types (part
->mp_part
, fixtypes
, message_mods
);
769 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
770 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
772 status
= fix_types (e
->eb_content
, fixtypes
, message_mods
);
779 if (ct
->c_ctinfo
.ci_type
&& ct
->c_ctinfo
.ci_subtype
) {
780 for (typep
= svector_strs (fixtypes
);
781 typep
&& (type
= *typep
);
784 concat (ct
->c_ctinfo
.ci_type
, "/", ct
->c_ctinfo
.ci_subtype
,
787 if (! strcasecmp (type
, type_subtype
) &&
788 decode_part (ct
) == OK
&&
789 ct
->c_cefile
.ce_file
!= NULL
) {
790 char *ct_type_subtype
= mime_type (ct
->c_cefile
.ce_file
);
793 if ((cp
= strchr (ct_type_subtype
, ';'))) {
794 /* Truncate to remove any parameter list from
795 mime_type () result. */
799 if (strcasecmp (type
, ct_type_subtype
)) {
800 char *ct_type
, *ct_subtype
;
803 /* The Content-Type header does not match the
804 content, so update these struct Content
807 * c_ctinfo.ci_type, c_ctinfo.ci_subtype
810 /* Extract type and subtype from type/subtype. */
811 ct_type
= getcpy (ct_type_subtype
);
812 if ((cp
= strchr (ct_type
, '/'))) {
814 ct_subtype
= getcpy (++cp
);
816 advise (NULL
, "missing / in MIME type of %s %s",
817 ct
->c_file
, ct
->c_partno
);
822 ct
->c_type
= ct_str_type (ct_type
);
823 ct
->c_subtype
= ct_str_subtype (ct
->c_type
, ct_subtype
);
825 free (ct
->c_ctinfo
.ci_type
);
826 ct
->c_ctinfo
.ci_type
= ct_type
;
827 free (ct
->c_ctinfo
.ci_subtype
);
828 ct
->c_ctinfo
.ci_subtype
= ct_subtype
;
829 if (! replace_substring (&ct
->c_ctline
, type
,
831 advise (NULL
, "did not find %s in %s",
835 /* Update Content-Type header field. */
836 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
837 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
838 if (replace_substring (&hf
->value
, type
,
842 report (NULL
, ct
->c_partno
, ct
->c_file
,
843 "change Content-Type in header "
845 type
, ct_type_subtype
);
849 advise (NULL
, "did not find %s in %s",
855 free (ct_type_subtype
);
866 replace_substring (char **str
, const char *old
, const char *new) {
869 if ((cp
= strstr (*str
, old
))) {
870 char *remainder
= cp
+ strlen (old
);
871 char *prefix
, *new_str
;
874 prefix
= getcpy (*str
);
875 *(prefix
+ (cp
- *str
)) = '\0';
876 new_str
= concat (prefix
, new, remainder
, NULL
);
879 new_str
= concat (new, remainder
, NULL
);
884 return *str
= new_str
;
891 * Remove a name=value parameter, given just its name, from a header value.
894 remove_parameter (char *str
, const char *name
) {
895 /* It looks to me, based on the BNF in RFC 2045, than there can't
896 be whitespace betwwen the parameter name and the "=", or
897 between the "=" and the parameter value. */
898 char *param_name
= concat (name
, "=", NULL
);
901 if ((cp
= strstr (str
, param_name
))) {
905 /* Remove any leading spaces, before the parameter name. */
907 start
> str
&& isspace ((unsigned char) *(start
-1));
911 /* Remove a leading semicolon. */
912 if (start
> str
&& *(start
-1) == ';') { --start
; }
914 end
= cp
+ strlen (name
) + 1;
916 /* Skip past the quoted value, and then the final quote. */
917 for (++end
; *end
&& *end
!= '"'; ++end
) { continue; }
920 /* Skip past the value. */
921 for (++end
; *end
&& ! isspace ((unsigned char) *end
); ++end
) {}
924 /* Count how many characters need to be moved. Include
925 trailing null, which is accounted for by the
926 initialization of count to 1. */
927 for (cp
= end
; *cp
; ++cp
) { ++count
; }
928 (void) memmove (start
, end
, count
);
937 fix_multipart_cte (CT ct
, int *message_mods
) {
940 if (ct
->c_type
== CT_MULTIPART
) {
944 if (ct
->c_encoding
!= CE_7BIT
&& ct
->c_encoding
!= CE_8BIT
&&
945 ct
->c_encoding
!= CE_BINARY
) {
948 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
949 char *name
= hf
->name
;
950 for (; *name
&& isspace ((unsigned char) *name
); ++name
) {
954 if (! strncasecmp (name
, ENCODING_FIELD
,
955 strlen (ENCODING_FIELD
))) {
956 char *prefix
= "Nmh-REPLACED-INVALID-";
957 HF h
= mh_xmalloc (sizeof *h
);
959 h
->name
= add (hf
->name
, NULL
);
960 h
->hf_encoding
= hf
->hf_encoding
;
964 /* Retain old header but prefix its name. */
966 hf
->name
= concat (prefix
, h
->name
, NULL
);
970 char *encoding
= cpytrim (hf
->value
);
971 report (NULL
, ct
->c_partno
, ct
->c_file
,
972 "replace Content-Transfer-Encoding of %s "
973 "with 8 bit", encoding
);
977 h
->value
= add (" 8bit\n", NULL
);
979 /* Don't need to warn for multiple C-T-E header
980 fields, parse_mime() already does that. But
981 if there are any, fix them all as necessary. */
986 set_ce (ct
, CE_8BIT
);
989 m
= (struct multipart
*) ct
->c_ctparams
;
990 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
991 if (fix_multipart_cte (part
->mp_part
, message_mods
) != OK
) {
1003 set_ce (CT ct
, int encoding
) {
1004 const char *ce
= ce_str (encoding
);
1005 const struct str2init
*ctinit
= get_ce_method (ce
);
1008 char *cte
= concat (" ", ce
, "\n", NULL
);
1011 /* Decoded contents might be in ct->c_cefile.ce_file, if the
1012 caller is decode_text_parts (). Save because we'll
1014 struct cefile decoded_content_info
= ct
->c_cefile
;
1016 ct
->c_encoding
= encoding
;
1018 ct
->c_ctinitfnx
= ctinit
->si_init
;
1019 /* This will assign ct->c_cefile with an all-0 struct, which
1021 (*ctinit
->si_init
) (ct
);
1022 /* After returning, the caller should set
1023 ct->c_cefile.ce_file to the name of the file containing
1026 /* Restore the cefile. */
1027 ct
->c_cefile
= decoded_content_info
;
1029 /* Update/add Content-Transfer-Encoding header field. */
1030 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1031 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
1038 add_header (ct
, add (ENCODING_FIELD
, NULL
), cte
);
1041 /* Update c_celine. It's used only by mhlist -debug. */
1042 free (ct
->c_celine
);
1043 ct
->c_celine
= add (cte
, NULL
);
1052 /* Make sure each text part has a corresponding text/plain part. */
1054 ensure_text_plain (CT
*ct
, CT parent
, int *message_mods
, int replacetextplain
) {
1057 switch ((*ct
)->c_type
) {
1059 /* Nothing to do for text/plain. */
1060 if ((*ct
)->c_subtype
== TEXT_PLAIN
) { return OK
; }
1062 if (parent
&& parent
->c_type
== CT_MULTIPART
&&
1063 parent
->c_subtype
== MULTI_ALTERNATE
) {
1064 int new_subpart_number
= 1;
1065 int has_text_plain
=
1066 find_textplain_sibling (parent
, replacetextplain
,
1067 &new_subpart_number
);
1069 if (! has_text_plain
) {
1070 /* Parent is a multipart/alternative. Insert a new
1071 text/plain subpart. */
1072 const int inserted
=
1073 insert_new_text_plain_part (*ct
, new_subpart_number
,
1078 report (NULL
, parent
->c_partno
, parent
->c_file
,
1079 "insert text/plain part");
1085 } else if (parent
&& parent
->c_type
== CT_MULTIPART
&&
1086 parent
->c_subtype
== MULTI_RELATED
) {
1087 char *type_subtype
=
1088 concat ((*ct
)->c_ctinfo
.ci_type
, "/",
1089 (*ct
)->c_ctinfo
.ci_subtype
, NULL
);
1090 const char *parent_type
=
1091 get_param (parent
->c_ctinfo
.ci_first_pm
, "type", '?', 1);
1092 int new_subpart_number
= 1;
1093 int has_text_plain
= 0;
1095 /* Have to do string comparison on the subtype because we
1096 don't enumerate all of them in c_subtype values.
1097 parent_type will be NULL if the multipart/related part
1098 doesn't have a type parameter. The type parameter must
1099 be specified according to RFC 2387 Sec. 3.1 but not all
1101 if (parent_type
&& strcasecmp (type_subtype
, parent_type
) == 0) {
1102 /* The type of this part matches the root type of the
1103 parent multipart/related. Look to see if there's
1104 text/plain sibling. */
1106 find_textplain_sibling (parent
, replacetextplain
,
1107 &new_subpart_number
);
1110 free (type_subtype
);
1112 if (! has_text_plain
) {
1113 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1117 for (part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
1118 if (*ct
!= part
->mp_part
) {
1124 /* Parent is a multipart/related. Insert a new
1125 text/plain subpart in a new multipart/alternative. */
1126 if (insert_into_new_mp_alt (ct
, message_mods
)) {
1127 /* Not an error if text/plain couldn't be added. */
1130 /* There are no siblings, so insert a new text/plain
1131 subpart, and change the parent type from
1132 multipart/related to multipart/alternative. */
1133 const int inserted
=
1134 insert_new_text_plain_part (*ct
, new_subpart_number
,
1140 parent
->c_subtype
= MULTI_ALTERNATE
;
1141 parent
->c_ctinfo
.ci_subtype
= getcpy ("alternative");
1142 if (! replace_substring (&parent
->c_ctline
, "/related",
1145 "did not find multipart/related in %s",
1149 /* Update Content-Type header field. */
1150 for (hf
= parent
->c_first_hf
; hf
; hf
= hf
->next
) {
1151 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1152 if (replace_substring (&hf
->value
, "/related",
1156 report (NULL
, parent
->c_partno
,
1158 "insert text/plain part");
1161 /* Remove, e.g., type="text/html" from
1162 multipart/alternative. */
1163 remove_parameter (hf
->value
, "type");
1166 advise (NULL
, "did not find multipart/"
1167 "related in header %s",
1173 /* Not an error if text/plain couldn't be inserted. */
1178 if (insert_into_new_mp_alt (ct
, message_mods
)) {
1185 case CT_MULTIPART
: {
1186 struct multipart
*mp
= (struct multipart
*) (*ct
)->c_ctparams
;
1189 for (part
= mp
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1190 if ((*ct
)->c_type
== CT_MULTIPART
) {
1191 status
= ensure_text_plain (&part
->mp_part
, *ct
, message_mods
,
1199 if ((*ct
)->c_subtype
== MESSAGE_EXTERNAL
) {
1200 struct exbody
*e
= (struct exbody
*) (*ct
)->c_ctparams
;
1202 status
= ensure_text_plain (&e
->eb_content
, *ct
, message_mods
,
1212 /* See if there is a sibling text/plain. */
1214 find_textplain_sibling (CT parent
, int replacetextplain
,
1215 int *new_subpart_number
) {
1216 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1217 struct part
*part
, *prev
;
1218 int has_text_plain
= 0;
1220 for (prev
= part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
1221 ++*new_subpart_number
;
1222 if (part
->mp_part
->c_type
== CT_TEXT
&&
1223 part
->mp_part
->c_subtype
== TEXT_PLAIN
) {
1224 if (replacetextplain
) {
1225 struct part
*old_part
;
1226 if (part
== mp
->mp_parts
) {
1227 old_part
= mp
->mp_parts
;
1228 mp
->mp_parts
= part
->mp_next
;
1230 old_part
= prev
->mp_next
;
1231 prev
->mp_next
= part
->mp_next
;
1234 report (NULL
, parent
->c_partno
, parent
->c_file
,
1235 "remove text/plain part %s",
1236 old_part
->mp_part
->c_partno
);
1238 free_content (old_part
->mp_part
);
1248 return has_text_plain
;
1253 insert_new_text_plain_part (CT ct
, int new_subpart_number
, CT parent
) {
1254 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1255 struct part
*new_part
= mh_xmalloc (sizeof *new_part
);
1257 if ((new_part
->mp_part
= build_text_plain_part (ct
))) {
1259 snprintf (buffer
, sizeof buffer
, "%d", new_subpart_number
);
1261 new_part
->mp_next
= mp
->mp_parts
;
1262 mp
->mp_parts
= new_part
;
1263 new_part
->mp_part
->c_partno
=
1264 concat (parent
->c_partno
? parent
->c_partno
: "1", ".",
1269 free_content (new_part
->mp_part
);
1278 build_text_plain_part (CT encoded_part
) {
1279 CT tp_part
= divide_part (encoded_part
);
1280 char *tmp_plain_file
= NULL
;
1282 if (decode_part (tp_part
) == OK
) {
1283 /* Now, tp_part->c_cefile.ce_file is the name of the tmp file that
1284 contains the decoded contents. And the decoding function, such
1285 as openQuoted, will have set ...->ce_unlink to 1 so that it will
1286 be unlinked by free_content (). */
1289 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1290 advise (NULL
, "unable to create temporary file in %s",
1293 tmp_plain_file
= add (tempfile
, NULL
);
1294 if (reformat_part (tp_part
, tmp_plain_file
,
1295 tp_part
->c_ctinfo
.ci_type
,
1296 tp_part
->c_ctinfo
.ci_subtype
,
1297 tp_part
->c_type
) == OK
) {
1303 free_content (tp_part
);
1304 if (tmp_plain_file
) { (void) m_unlink (tmp_plain_file
); }
1305 free (tmp_plain_file
);
1311 /* Slip new text/plain part into a new multipart/alternative. */
1313 insert_into_new_mp_alt (CT
*ct
, int *message_mods
) {
1314 CT tp_part
= build_text_plain_part (*ct
);
1318 CT mp_alt
= build_multipart_alt (*ct
, tp_part
, CT_MULTIPART
,
1321 struct multipart
*mp
=
1322 (struct multipart
*) mp_alt
->c_ctparams
;
1324 if (mp
&& mp
->mp_parts
) {
1325 mp
->mp_parts
->mp_part
= tp_part
;
1326 /* Make the new multipart/alternative the parent. */
1331 report (NULL
, (*ct
)->c_partno
, (*ct
)->c_file
,
1332 "insert text/plain part");
1335 free_content (tp_part
);
1336 free_content (mp_alt
);
1350 divide_part (CT ct
) {
1353 if ((new_part
= (CT
) mh_xcalloc (1, sizeof *new_part
)) == NULL
)
1354 adios (NULL
, "out of memory");
1356 /* Just copy over what is needed for decoding. c_vrsn and
1357 c_celine aren't necessary. */
1358 new_part
->c_file
= add (ct
->c_file
, NULL
);
1359 new_part
->c_begin
= ct
->c_begin
;
1360 new_part
->c_end
= ct
->c_end
;
1361 copy_ctinfo (&new_part
->c_ctinfo
, &ct
->c_ctinfo
);
1362 new_part
->c_type
= ct
->c_type
;
1363 new_part
->c_cefile
= ct
->c_cefile
;
1364 new_part
->c_encoding
= ct
->c_encoding
;
1365 new_part
->c_ctinitfnx
= ct
->c_ctinitfnx
;
1366 new_part
->c_ceopenfnx
= ct
->c_ceopenfnx
;
1367 new_part
->c_ceclosefnx
= ct
->c_ceclosefnx
;
1368 new_part
->c_cesizefnx
= ct
->c_cesizefnx
;
1370 /* c_ctline is used by reformat__part(), so it can preserve
1371 anything after the type/subtype. */
1372 new_part
->c_ctline
= add (ct
->c_ctline
, NULL
);
1379 copy_ctinfo (CI dest
, CI src
) {
1382 dest
->ci_type
= src
->ci_type
? add (src
->ci_type
, NULL
) : NULL
;
1383 dest
->ci_subtype
= src
->ci_subtype
? add (src
->ci_subtype
, NULL
) : NULL
;
1385 for (s_pm
= src
->ci_first_pm
; s_pm
; s_pm
= s_pm
->pm_next
) {
1386 d_pm
= add_param(&dest
->ci_first_pm
, &dest
->ci_last_pm
, s_pm
->pm_name
,
1388 if (s_pm
->pm_charset
)
1389 d_pm
->pm_charset
= getcpy(s_pm
->pm_charset
);
1391 d_pm
->pm_lang
= getcpy(s_pm
->pm_lang
);
1394 dest
->ci_comment
= src
->ci_comment
? add (src
->ci_comment
, NULL
) : NULL
;
1395 dest
->ci_magic
= src
->ci_magic
? add (src
->ci_magic
, NULL
) : NULL
;
1400 decode_part (CT ct
) {
1405 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1406 adios (NULL
, "unable to create temporary file in %s", get_temp_dir());
1408 tmp_decoded
= add (tempfile
, NULL
);
1409 /* The following call will load ct->c_cefile.ce_file with the tmp
1410 filename of the decoded content. tmp_decoded will contain the
1411 encoded output, get rid of that. */
1412 status
= output_message (ct
, tmp_decoded
);
1413 (void) m_unlink (tmp_decoded
);
1420 /* Some of the arguments aren't really needed now, but maybe will
1421 be in the future for other than text types. */
1423 reformat_part (CT ct
, char *file
, char *type
, char *subtype
, int c_type
) {
1424 int output_subtype
, output_encoding
;
1428 /* Hacky: this redirects the output from whatever command is used
1429 to show the part to a file. So, the user can't have any output
1430 redirection in that command.
1431 Could show_multi() in mhshowsbr.c avoid this? */
1433 /* Check for invo_name-format-type/subtype. */
1434 if ((cf
= context_find_by_type ("format", type
, subtype
)) == NULL
) {
1436 advise (NULL
, "Don't know how to convert %s, there is no "
1437 "%s-format-%s/%s profile entry",
1438 ct
->c_file
, invo_name
, type
, subtype
);
1442 if (strchr (cf
, '>')) {
1443 advise (NULL
, "'>' prohibited in \"%s\",\nplease fix your "
1444 "%s-format-%s/%s profile entry", cf
, invo_name
, type
,
1445 subtype
? subtype
: "");
1451 cp
= concat (cf
, " >", file
, NULL
);
1452 status
= show_content_aux (ct
, 0, cp
, NULL
, NULL
);
1455 /* Unlink decoded content tmp file and free its filename to avoid
1456 leaks. The file stream should already have been closed. */
1457 if (ct
->c_cefile
.ce_unlink
) {
1458 (void) m_unlink (ct
->c_cefile
.ce_file
);
1459 free (ct
->c_cefile
.ce_file
);
1460 ct
->c_cefile
.ce_file
= NULL
;
1461 ct
->c_cefile
.ce_unlink
= 0;
1464 if (c_type
== CT_TEXT
) {
1465 output_subtype
= TEXT_PLAIN
;
1467 /* Set subtype to 0, which is always an UNKNOWN subtype. */
1470 output_encoding
= charset_encoding (ct
);
1472 if (set_ct_type (ct
, c_type
, output_subtype
, output_encoding
) == OK
) {
1473 ct
->c_cefile
.ce_file
= file
;
1474 ct
->c_cefile
.ce_unlink
= 1;
1476 ct
->c_cefile
.ce_unlink
= 0;
1484 /* Identifies 7bit or 8bit content based on charset. */
1486 charset_encoding (CT ct
) {
1487 char *ct_charset
= content_charset (ct
);
1488 int encoding
= strcasecmp (ct_charset
, "US-ASCII") ? CE_8BIT
: CE_7BIT
;
1497 build_multipart_alt (CT first_alt
, CT new_part
, int type
, int subtype
) {
1498 char *boundary_prefix
= "----=_nmh-multipart";
1499 char *boundary
= concat (boundary_prefix
, first_alt
->c_partno
, NULL
);
1500 char *boundary_indicator
= "; boundary=";
1501 char *typename
, *subtypename
, *name
;
1504 struct multipart
*m
;
1505 const struct str2init
*ctinit
;
1507 if ((ct
= (CT
) mh_xcalloc (1, sizeof *ct
)) == NULL
)
1508 adios (NULL
, "out of memory");
1510 /* Set up the multipart/alternative part. These fields of *ct were
1511 initialized to 0 by mh_xcalloc():
1512 c_fp, c_unlink, c_begin, c_end,
1513 c_vrsn, c_ctline, c_celine,
1514 c_id, c_descr, c_dispo, c_partno,
1515 c_ctinfo.ci_comment, c_ctinfo.ci_magic,
1516 c_cefile, c_encoding,
1517 c_digested, c_digest[16], c_ctexbody,
1518 c_ctinitfnx, c_ceopenfnx, c_ceclosefnx, c_cesizefnx,
1520 c_showproc, c_termproc, c_storeproc, c_storage, c_folder
1523 ct
->c_file
= add (first_alt
->c_file
, NULL
);
1525 ct
->c_subtype
= subtype
;
1527 ctinit
= get_ct_init (ct
->c_type
);
1529 typename
= ct_type_str (type
);
1530 subtypename
= ct_subtype_str (type
, subtype
);
1534 int found_boundary
= 1;
1536 while (found_boundary
&& serial
< 1000000) {
1539 /* Ensure that the boundary doesn't appear in the decoded
1541 if (new_part
->c_cefile
.ce_file
) {
1542 if ((found_boundary
=
1543 boundary_in_content (&new_part
->c_cefile
.ce_fp
,
1544 new_part
->c_cefile
.ce_file
,
1551 /* Ensure that the boundary doesn't appear in the encoded
1553 if (! found_boundary
&& new_part
->c_file
) {
1554 if ((found_boundary
= boundary_in_content (&new_part
->c_fp
,
1562 if (found_boundary
) {
1563 /* Try a slightly different boundary. */
1568 snprintf (buffer2
, sizeof buffer2
, "%d", serial
);
1570 concat (boundary_prefix
,
1571 first_alt
->c_partno
? first_alt
->c_partno
: "",
1572 "-", buffer2
, NULL
);
1576 if (found_boundary
) {
1577 advise (NULL
, "giving up trying to find a unique boundary");
1583 name
= concat (" ", typename
, "/", subtypename
, boundary_indicator
, "\"",
1584 boundary
, "\"", NULL
);
1586 /* Load c_first_hf and c_last_hf. */
1587 transfer_noncontent_headers (first_alt
, ct
);
1588 add_header (ct
, add (TYPE_FIELD
, NULL
), concat (name
, "\n", NULL
));
1591 /* Load c_partno. */
1592 if (first_alt
->c_partno
) {
1593 ct
->c_partno
= add (first_alt
->c_partno
, NULL
);
1594 free (first_alt
->c_partno
);
1595 first_alt
->c_partno
= concat (ct
->c_partno
, ".1", NULL
);
1596 new_part
->c_partno
= concat (ct
->c_partno
, ".2", NULL
);
1598 first_alt
->c_partno
= add ("1", NULL
);
1599 new_part
->c_partno
= add ("2", NULL
);
1603 ct
->c_ctinfo
.ci_type
= add (typename
, NULL
);
1604 ct
->c_ctinfo
.ci_subtype
= add (subtypename
, NULL
);
1607 add_param(&ct
->c_ctinfo
.ci_first_pm
, &ct
->c_ctinfo
.ci_last_pm
,
1608 "boundary", boundary
, 0);
1610 p
= (struct part
*) mh_xmalloc (sizeof *p
);
1611 p
->mp_next
= (struct part
*) mh_xmalloc (sizeof *p
->mp_next
);
1612 p
->mp_next
->mp_next
= NULL
;
1613 p
->mp_next
->mp_part
= first_alt
;
1615 if ((m
= (struct multipart
*) mh_xcalloc (1, sizeof (struct multipart
))) ==
1617 adios (NULL
, "out of memory");
1618 m
->mp_start
= concat (boundary
, "\n", NULL
);
1619 m
->mp_stop
= concat (boundary
, "--\n", NULL
);
1629 /* Check that the boundary does not appear in the content. */
1631 boundary_in_content (FILE **fp
, char *file
, const char *boundary
) {
1632 char buffer
[BUFSIZ
];
1634 int found_boundary
= 0;
1636 /* free_content() will close *fp if we fopen it here. */
1637 if (! *fp
&& (*fp
= fopen (file
, "r")) == NULL
) {
1638 advise (file
, "unable to open %s for reading", file
);
1642 fseeko (*fp
, 0L, SEEK_SET
);
1643 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) > 0) {
1644 if (find_str (buffer
, bytes_read
, boundary
)) {
1650 return found_boundary
;
1654 /* Remove all non-Content headers. */
1656 transfer_noncontent_headers (CT old
, CT
new) {
1659 hp_prev
= hp
= old
->c_first_hf
;
1663 if (strncasecmp (XXX_FIELD_PRF
, hp
->name
, strlen (XXX_FIELD_PRF
))) {
1664 if (hp
== old
->c_last_hf
) {
1665 if (hp
== old
->c_first_hf
) {
1666 old
->c_last_hf
= old
->c_first_hf
= NULL
;
1668 hp_prev
->next
= NULL
;
1669 old
->c_last_hf
= hp_prev
;
1672 if (hp
== old
->c_first_hf
) {
1673 old
->c_first_hf
= next
;
1675 hp_prev
->next
= next
;
1679 /* Put node hp in the new CT. */
1680 if (new->c_first_hf
== NULL
) {
1681 new->c_first_hf
= hp
;
1683 new->c_last_hf
->next
= hp
;
1685 new->c_last_hf
= hp
;
1687 /* A Content- header, leave in old. */
1697 set_ct_type (CT ct
, int type
, int subtype
, int encoding
) {
1698 char *typename
= ct_type_str (type
);
1699 char *subtypename
= ct_subtype_str (type
, subtype
);
1700 /* E.g, " text/plain" */
1701 char *type_subtypename
= concat (" ", typename
, "/", subtypename
, NULL
);
1702 /* E.g, " text/plain\n" */
1703 char *name_plus_nl
= concat (type_subtypename
, "\n", NULL
);
1704 int found_content_type
= 0;
1706 const char *cp
= NULL
;
1710 /* Update/add Content-Type header field. */
1711 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1712 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1713 found_content_type
= 1;
1715 hf
->value
= (cp
= strchr (ct
->c_ctline
, ';'))
1716 ? concat (type_subtypename
, cp
, "\n", NULL
)
1717 : add (name_plus_nl
, NULL
);
1720 if (! found_content_type
) {
1721 add_header (ct
, add (TYPE_FIELD
, NULL
),
1722 (cp
= strchr (ct
->c_ctline
, ';'))
1723 ? concat (type_subtypename
, cp
, "\n", NULL
)
1724 : add (name_plus_nl
, NULL
));
1727 /* Some of these might not be used, but set them anyway. */
1729 ? concat (type_subtypename
, cp
, NULL
)
1730 : concat (type_subtypename
, NULL
);
1731 free (ct
->c_ctline
);
1732 ct
->c_ctline
= ctline
;
1733 /* Leave other ctinfo members as they were. */
1734 free (ct
->c_ctinfo
.ci_type
);
1735 ct
->c_ctinfo
.ci_type
= add (typename
, NULL
);
1736 free (ct
->c_ctinfo
.ci_subtype
);
1737 ct
->c_ctinfo
.ci_subtype
= add (subtypename
, NULL
);
1739 ct
->c_subtype
= subtype
;
1741 free (name_plus_nl
);
1742 free (type_subtypename
);
1744 status
= set_ce (ct
, encoding
);
1751 decode_text_parts (CT ct
, int encoding
, int *message_mods
) {
1754 switch (ct
->c_type
) {
1756 switch (ct
->c_encoding
) {
1761 if (decode_part (ct
) == OK
&& ct
->c_cefile
.ce_file
) {
1762 const char *reason
= NULL
;
1764 if ((ct_encoding
= content_encoding (ct
, &reason
)) == CE_BINARY
1765 && encoding
!= CE_BINARY
) {
1766 /* The decoding isn't acceptable so discard it.
1767 Leave status as OK to allow other transformations. */
1769 report (NULL
, ct
->c_partno
, ct
->c_file
,
1770 "will not decode%s because it is binary (%s)",
1772 : ct
->c_ctline
? ct
->c_ctline
1776 (void) m_unlink (ct
->c_cefile
.ce_file
);
1777 free (ct
->c_cefile
.ce_file
);
1778 ct
->c_cefile
.ce_file
= NULL
;
1779 } else if (ct
->c_encoding
== CE_QUOTED
&&
1780 ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
1781 /* The decoding isn't acceptable so discard it.
1782 Leave status as OK to allow other transformations. */
1784 report (NULL
, ct
->c_partno
, ct
->c_file
,
1785 "will not decode%s because it is 8bit",
1787 : ct
->c_ctline
? ct
->c_ctline
1790 (void) m_unlink (ct
->c_cefile
.ce_file
);
1791 free (ct
->c_cefile
.ce_file
);
1792 ct
->c_cefile
.ce_file
= NULL
;
1795 if (ct_encoding
== CE_BINARY
)
1797 else if (ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
)
1800 enc
= charset_encoding (ct
);
1801 if (set_ce (ct
, enc
) == OK
) {
1804 report (NULL
, ct
->c_partno
, ct
->c_file
, "decode%s",
1805 ct
->c_ctline
? ct
->c_ctline
: "");
1807 strip_crs (ct
, message_mods
);
1819 strip_crs (ct
, message_mods
);
1827 case CT_MULTIPART
: {
1828 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1831 /* Should check to see if the body for this part is encoded?
1832 For now, it gets passed along as-is by InitMultiPart(). */
1833 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1834 status
= decode_text_parts (part
->mp_part
, encoding
, message_mods
);
1840 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1841 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
1843 status
= decode_text_parts (e
->eb_content
, encoding
, message_mods
);
1855 /* See if the decoded content is 7bit, 8bit, or binary. It's binary
1856 if it has any NUL characters, a CR not followed by a LF, or lines
1857 greater than 998 characters in length. If binary, reason is set
1858 to a string explaining why. */
1860 content_encoding (CT ct
, const char **reason
) {
1861 CE ce
= &ct
->c_cefile
;
1862 int encoding
= CE_7BIT
;
1865 size_t line_len
= 0;
1866 char buffer
[BUFSIZ
];
1869 if (! ce
->ce_fp
&& (ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
1870 advise (ce
->ce_file
, "unable to open for reading");
1874 fseeko (ce
->ce_fp
, 0L, SEEK_SET
);
1875 while (encoding
!= CE_BINARY
&&
1876 (inbytes
= fread (buffer
, 1, sizeof buffer
, ce
->ce_fp
)) > 0) {
1879 int last_char_was_cr
= 0;
1881 for (i
= 0, cp
= buffer
; i
< inbytes
; ++i
, ++cp
) {
1882 if (*cp
== '\0' || ++line_len
> 998 ||
1883 (*cp
!= '\n' && last_char_was_cr
)) {
1884 encoding
= CE_BINARY
;
1886 *reason
= "null character";
1887 } else if (line_len
> 998) {
1888 *reason
= "line length > 998";
1889 } else if (*cp
!= '\n' && last_char_was_cr
) {
1890 *reason
= "CR not followed by LF";
1892 /* Should not reach this. */
1896 } else if (*cp
== '\n') {
1898 } else if (! isascii ((unsigned char) *cp
)) {
1902 last_char_was_cr
= *cp
== '\r' ? 1 : 0;
1908 } /* else should never happen */
1915 strip_crs (CT ct
, int *message_mods
) {
1916 char *charset
= content_charset (ct
);
1919 /* Only strip carriage returns if content is ASCII or another
1920 charset that has the same readily recognizable CR followed by a
1921 LF. We can include UTF-8 here because if the high-order bit of
1922 a UTF-8 byte is 0, then it must be a single-byte ASCII
1924 if (! strcasecmp (charset
, "US-ASCII") ||
1925 ! strcasecmp (charset
, "UTF-8") ||
1926 ! strncasecmp (charset
, "ISO-8859-", 9) ||
1927 ! strncasecmp (charset
, "WINDOWS-12", 10)) {
1933 int opened_input_file
= 0;
1935 if (ct
->c_cefile
.ce_file
) {
1936 file
= &ct
->c_cefile
.ce_file
;
1937 fp
= &ct
->c_cefile
.ce_fp
;
1939 } else if (ct
->c_file
) {
1942 begin
= (size_t) ct
->c_begin
;
1943 end
= (size_t) ct
->c_end
;
1944 } /* else don't know where the content is */
1946 if (file
&& *file
&& fp
) {
1948 if ((*fp
= fopen (*file
, "r")) == NULL
) {
1949 advise (*file
, "unable to open for reading");
1952 opened_input_file
= 1;
1958 char buffer
[BUFSIZ
];
1960 size_t bytes_to_read
=
1961 end
> 0 && end
> begin
? end
- begin
: sizeof buffer
;
1963 fseeko (*fp
, begin
, SEEK_SET
);
1964 while ((bytes_read
= fread (buffer
, 1,
1965 min (bytes_to_read
, sizeof buffer
),
1967 /* Look for CR followed by a LF. This is supposed to
1968 be text so there should be LF's. If not, don't
1969 modify the content. */
1972 int last_char_was_cr
= 0;
1974 if (end
> 0) { bytes_to_read
-= bytes_read
; }
1976 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
1977 if (*cp
== '\n' && last_char_was_cr
) {
1982 last_char_was_cr
= *cp
== '\r' ? 1 : 0;
1988 char *stripped_content_file
;
1989 char *tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
);
1991 if (tempfile
== NULL
) {
1992 adios (NULL
, "unable to create temporary file in %s",
1995 stripped_content_file
= add (tempfile
, NULL
);
1997 /* Strip each CR before a LF from the content. */
1998 fseeko (*fp
, begin
, SEEK_SET
);
1999 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) >
2003 int last_char_was_cr
= 0;
2005 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
2007 last_char_was_cr
= 1;
2008 } else if (last_char_was_cr
) {
2010 if (write (fd
, "\r", 1) < 0) {
2011 advise (tempfile
, "CR write");
2014 if (write (fd
, cp
, 1) < 0) {
2015 advise (tempfile
, "write");
2017 last_char_was_cr
= 0;
2019 if (write (fd
, cp
, 1) < 0) {
2020 advise (tempfile
, "write");
2022 last_char_was_cr
= 0;
2028 admonish (NULL
, "unable to write temporary file %s",
2029 stripped_content_file
);
2030 (void) m_unlink (stripped_content_file
);
2033 /* Replace the decoded file with the converted one. */
2034 if (ct
->c_cefile
.ce_file
) {
2035 if (ct
->c_cefile
.ce_unlink
) {
2036 (void) m_unlink (ct
->c_cefile
.ce_file
);
2038 free (ct
->c_cefile
.ce_file
);
2040 ct
->c_cefile
.ce_file
= stripped_content_file
;
2041 ct
->c_cefile
.ce_unlink
= 1;
2045 report (NULL
, ct
->c_partno
,
2046 begin
== 0 && end
== 0 ? "" : *file
,
2052 if (opened_input_file
) {
2066 convert_charsets (CT ct
, char *dest_charset
, int *message_mods
) {
2069 switch (ct
->c_type
) {
2071 if (ct
->c_subtype
== TEXT_PLAIN
) {
2072 status
= convert_charset (ct
, dest_charset
, message_mods
);
2075 char *ct_charset
= content_charset (ct
);
2077 report (NULL
, ct
->c_partno
, ct
->c_file
,
2078 "convert %s to %s", ct_charset
, dest_charset
);
2082 char *ct_charset
= content_charset (ct
);
2084 report ("iconv", ct
->c_partno
, ct
->c_file
,
2085 "failed to convert %s to %s", ct_charset
, dest_charset
);
2091 case CT_MULTIPART
: {
2092 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2095 /* Should check to see if the body for this part is encoded?
2096 For now, it gets passed along as-is by InitMultiPart(). */
2097 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2099 convert_charsets (part
->mp_part
, dest_charset
, message_mods
);
2105 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2106 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2109 convert_charsets (e
->eb_content
, dest_charset
, message_mods
);
2122 * Fix various problems that aren't handled elsewhere. These
2123 * are fixed unconditionally: there are no switches to disable
2124 * them. (Currently, "problems" is just one: an extraneous
2125 * semicolon at the end of a header parameter list.)
2128 fix_always (CT ct
, int *message_mods
) {
2131 switch (ct
->c_type
) {
2132 case CT_MULTIPART
: {
2133 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2136 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2137 status
= fix_always (part
->mp_part
, message_mods
);
2143 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2144 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2146 status
= fix_always (e
->eb_content
, message_mods
);
2153 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
2154 size_t len
= strlen (hf
->value
);
2156 if (strcasecmp (hf
->name
, TYPE_FIELD
) != 0 &&
2157 strcasecmp (hf
->name
, DISPO_FIELD
) != 0) {
2158 /* Only do this for Content-Type and
2159 Content-Disposition fields because those are the
2160 only headers that parse_mime() warns about. */
2164 /* whitespace following a trailing ';' will be nuked as well */
2165 if (hf
->value
[len
- 1] == '\n') {
2166 while (isspace((unsigned char)(hf
->value
[len
- 2]))) {
2167 if (len
-- == 0) { break; }
2171 if (hf
->value
[len
- 2] == ';') {
2172 /* Remove trailing ';' from parameter value. */
2173 hf
->value
[len
- 2] = '\n';
2174 hf
->value
[len
- 1] = '\0';
2176 /* Also, if Content-Type parameter, remove trailing ';'
2177 from ct->c_ctline. This probably isn't necessary
2179 if (strcasecmp(hf
->name
, TYPE_FIELD
) == 0 && ct
->c_ctline
) {
2180 size_t l
= strlen(ct
->c_ctline
) - 1;
2181 while (isspace((unsigned char)(ct
->c_ctline
[l
])) ||
2182 ct
->c_ctline
[l
] == ';') {
2183 ct
->c_ctline
[l
--] = '\0';
2184 if (l
== 0) { break; }
2190 report (NULL
, ct
->c_partno
, ct
->c_file
,
2191 "remove trailing ; from %s parameter value",
2203 write_content (CT ct
, char *input_filename
, char *outfile
, int modify_inplace
,
2207 if (modify_inplace
) {
2208 if (message_mods
> 0) {
2209 if ((status
= output_message (ct
, outfile
)) == OK
) {
2210 char *infile
= input_filename
2211 ? add (input_filename
, NULL
)
2212 : add (ct
->c_file
? ct
->c_file
: "-", NULL
);
2214 if (remove_file (infile
) == OK
) {
2215 if (rename (outfile
, infile
)) {
2216 /* Rename didn't work, possibly because of an
2217 attempt to rename across filesystems. Try
2218 brute force copy. */
2219 int old
= open (outfile
, O_RDONLY
);
2221 open (infile
, O_WRONLY
| O_CREAT
, m_gmprot ());
2224 if (old
!= -1 && new != -1) {
2225 char buffer
[BUFSIZ
];
2227 while ((i
= read (old
, buffer
, sizeof buffer
)) >
2229 if (write (new, buffer
, i
) != i
) {
2235 if (new != -1) { close (new); }
2236 if (old
!= -1) { close (old
); }
2237 (void) m_unlink (outfile
);
2240 /* The -file argument processing used path() to
2241 expand filename to absolute path. */
2242 int file
= ct
->c_file
&& ct
->c_file
[0] == '/';
2244 admonish (NULL
, "unable to rename %s %s to %s",
2245 file
? "file" : "message", outfile
,
2251 admonish (NULL
, "unable to remove input file %s, "
2252 "not modifying it", infile
);
2253 (void) m_unlink (outfile
);
2262 /* No modifications and didn't need the tmp outfile. */
2263 (void) m_unlink (outfile
);
2266 /* Output is going to some file. Produce it whether or not
2267 there were modifications. */
2268 status
= output_message (ct
, outfile
);
2277 * If "rmmproc" is defined, call that to remove the file. Otherwise,
2278 * use the standard MH backup file.
2281 remove_file (char *file
) {
2283 char *rmm_command
= concat (rmmproc
, " ", file
, NULL
);
2284 int status
= system (rmm_command
);
2287 return WIFEXITED (status
) ? WEXITSTATUS (status
) : NOTOK
;
2289 /* This is OK for a non-message file, it still uses the
2290 BACKUP_PREFIX form. The backup file will be in the same
2291 directory as file. */
2292 return rename (file
, m_backup (file
));
2298 report (char *what
, char *partno
, char *filename
, char *message
, ...) {
2303 va_start (args
, message
);
2304 fmt
= concat (filename
, partno
? " part " : ", ",
2305 partno
? partno
: "", partno
? ", " : "", message
, NULL
);
2307 advertise (what
, NULL
, fmt
, args
);
2320 fprintf (stderr
, "\n");