]> diplodocus.org Git - nmh/blob - uip/mhfixmsg.c
mhshowsbr.c: Delete single-use global int `nolist'.
[nmh] / uip / mhfixmsg.c
1 /* mhfixmsg.c -- rewrite a message with various transformations
2 *
3 * This code is Copyright (c) 2002 and 2013, by the authors of nmh.
4 * See the COPYRIGHT file in the root directory of the nmh
5 * distribution for complete copyright information.
6 */
7
8 #include "h/mh.h"
9 #include "sbr/m_name.h"
10 #include "sbr/m_gmprot.h"
11 #include "sbr/m_getfld.h"
12 #include "sbr/getarguments.h"
13 #include "sbr/concat.h"
14 #include "sbr/seq_setprev.h"
15 #include "sbr/seq_setcur.h"
16 #include "sbr/seq_save.h"
17 #include "sbr/smatch.h"
18 #include "sbr/fmt_rfc2047.h"
19 #include "sbr/cpydata.h"
20 #include "sbr/trimcpy.h"
21 #include "sbr/m_convert.h"
22 #include "sbr/m_backup.h"
23 #include "sbr/getfolder.h"
24 #include "sbr/folder_read.h"
25 #include "sbr/context_save.h"
26 #include "sbr/context_replace.h"
27 #include "sbr/context_find.h"
28 #include "sbr/readconfig.h"
29 #include "sbr/ambigsw.h"
30 #include "sbr/path.h"
31 #include "sbr/print_version.h"
32 #include "sbr/print_help.h"
33 #include "sbr/error.h"
34 #include "h/fmt_scan.h"
35 #include "h/mime.h"
36 #include "h/mhparse.h"
37 #include "h/done.h"
38 #include "h/utils.h"
39 #include "h/signals.h"
40 #include "sbr/m_maildir.h"
41 #include "sbr/m_mktemp.h"
42 #include "sbr/mime_type.h"
43 #include "mhmisc.h"
44 #include "mhfree.h"
45 #include "mhoutsbr.h"
46 #include "mhshowsbr.h"
47 #include <fcntl.h>
48
49 #define MHFIXMSG_SWITCHES \
50 X("decodetext 8bit|7bit|binary", 0, DECODETEXTSW) \
51 X("nodecodetext", 0, NDECODETEXTSW) \
52 X("decodetypes", 0, DECODETYPESW) \
53 X("decodeheaderfieldbodies utf-8", 0, DECODEHEADERFIELDBODIESSW) \
54 X("nodecodeheaderfieldbodies", 0, NDECODEHEADERFIELDBODIESSW) \
55 X("crlflinebreaks", 0, CRLFLINEBREAKSSW) \
56 X("nocrlflinebreaks", 0, NCRLFLINEBREAKSSW) \
57 X("textcharset", 0, TEXTCHARSETSW) \
58 X("notextcharset", 0, NTEXTCHARSETSW) \
59 X("reformat", 0, REFORMATSW) \
60 X("noreformat", 0, NREFORMATSW) \
61 X("replacetextplain", 0, REPLACETEXTPLAINSW) \
62 X("noreplacetextplain", 0, NREPLACETEXTPLAINSW) \
63 X("fixboundary", 0, FIXBOUNDARYSW) \
64 X("nofixboundary", 0, NFIXBOUNDARYSW) \
65 X("fixcte", 0, FIXCOMPOSITECTESW) \
66 X("nofixcte", 0, NFIXCOMPOSITECTESW) \
67 X("fixtype mimetype", 0, FIXTYPESW) \
68 X("file file", 0, FILESW) \
69 X("outfile file", 0, OUTFILESW) \
70 X("rmmproc program", 0, RPROCSW) \
71 X("normmproc", 0, NRPRCSW) \
72 X("changecur", 0, CHGSW) \
73 X("nochangecur", 0, NCHGSW) \
74 X("verbose", 0, VERBSW) \
75 X("noverbose", 0, NVERBSW) \
76 X("version", 0, VERSIONSW) \
77 X("help", 0, HELPSW) \
78
79 #define X(sw, minchars, id) id,
80 DEFINE_SWITCH_ENUM(MHFIXMSG);
81 #undef X
82
83 #define X(sw, minchars, id) { sw, minchars, id },
84 DEFINE_SWITCH_ARRAY(MHFIXMSG, switches);
85 #undef X
86
87
88 int verbosw;
89 int debugsw; /* Needed by mhparse.c. */
90
91 #define quitser pipeser
92
93 /*
94 * static prototypes
95 */
96 typedef struct fix_transformations {
97 int fixboundary;
98 int fixcompositecte;
99 svector_t fixtypes;
100 int reformat;
101 int replacetextplain;
102 int decodetext;
103 char *decodetypes;
104 char *decodeheaderfieldbodies; /* Either NULL or "utf-8". */
105 /* Whether to use CRLF linebreaks, per RFC 2046 Sec. 4.1.1, par.1. */
106 int lf_line_endings;
107 char *textcharset;
108 } fix_transformations;
109
110 static int mhfixmsgsbr (CT *, char *, const fix_transformations *,
111 FILE **, char *, FILE **);
112 static int fix_boundary (CT *, int *);
113 static int copy_input_to_output (const char *, FILE *, const char *, FILE *);
114 static int get_multipart_boundary (CT, char **);
115 static int replace_boundary (CT, char *, char *);
116 static int fix_types (CT, svector_t, int *);
117 static char *replace_substring (char **, const char *, const char *);
118 static char *remove_parameter (char *, const char *);
119 static int fix_composite_cte (CT, int *);
120 static int set_ce (CT, int);
121 static int ensure_text_plain (CT *, CT, int *, int);
122 static int find_textplain_sibling (CT, int, int *);
123 static int insert_new_text_plain_part (CT, int, CT);
124 static CT build_text_plain_part (CT);
125 static int insert_into_new_mp_alt (CT *, int *);
126 static CT divide_part (CT);
127 static void copy_ctinfo (CI, CI);
128 static int decode_part (CT);
129 static int reformat_part (CT, char *, char *, char *, int);
130 static CT build_multipart_alt (CT, CT, int, int);
131 static int boundary_in_content (FILE **, char *, const char *);
132 static void transfer_noncontent_headers (CT, CT);
133 static int set_ct_type (CT, int type, int subtype, int encoding);
134 static int decode_text_parts (CT, int, const char *, int *);
135 static int should_decode(const char *, const char *, const char *);
136 static int content_encoding (CT, const char **);
137 static int strip_crs (CT, int *);
138 static void update_cte (CT);
139 static int least_restrictive_encoding (CT) PURE;
140 static int less_restrictive (int, int);
141 static int convert_charsets (CT, char *, int *);
142 static int fix_always (CT, int *);
143 static int decode_header_field_bodies (CT, int *);
144 static int fix_filename_param (char *, char *, PM *, PM *);
145 static int fix_filename_encoding (CT);
146 static int write_content (CT, const char *, char *, FILE *, int, int);
147 static void set_text_ctparams(CT, char *, int);
148 static int remove_file (const char *);
149 static void report (char *, char *, char *, char *, ...)
150 CHECK_PRINTF(4, 5);
151 static void pipeser (int);
152
153
154 int
155 main (int argc, char **argv)
156 {
157 int msgnum;
158 char *cp, *file = NULL, *folder = NULL;
159 char *maildir = NULL, buf[100], *outfile = NULL;
160 char **argp, **arguments;
161 struct msgs_array msgs = { 0, 0, NULL };
162 struct msgs *mp = NULL;
163 CT *ctp;
164 FILE *fp, *infp = NULL, *outfp = NULL;
165 bool using_stdin = false;
166 bool chgflag = true;
167 int status = OK;
168 fix_transformations fx;
169 fx.reformat = fx.fixcompositecte = fx.fixboundary = 1;
170 fx.fixtypes = NULL;
171 fx.replacetextplain = 0;
172 fx.decodetext = CE_8BIT;
173 fx.decodetypes = "text,application/ics"; /* Default, per man page. */
174 fx.decodeheaderfieldbodies = NULL;
175 fx.lf_line_endings = 0;
176 fx.textcharset = NULL;
177
178 if (nmh_init(argv[0], true, false)) { return 1; }
179
180 arguments = getarguments (invo_name, argc, argv, 1);
181 argp = arguments;
182
183 /*
184 * Parse arguments
185 */
186 while ((cp = *argp++)) {
187 if (*cp == '-') {
188 switch (smatch (++cp, switches)) {
189 case AMBIGSW:
190 ambigsw (cp, switches);
191 done (1);
192 case UNKWNSW:
193 die("-%s unknown", cp);
194
195 case HELPSW:
196 snprintf (buf, sizeof buf, "%s [+folder] [msgs] [switches]",
197 invo_name);
198 print_help (buf, switches, 1);
199 done (0);
200 case VERSIONSW:
201 print_version(invo_name);
202 done (0);
203
204 case DECODETEXTSW:
205 if (! (cp = *argp++) || *cp == '-') {
206 die("missing argument to %s", argp[-2]);
207 }
208 if (! strcasecmp (cp, "8bit")) {
209 fx.decodetext = CE_8BIT;
210 } else if (! strcasecmp (cp, "7bit")) {
211 fx.decodetext = CE_7BIT;
212 } else if (! strcasecmp (cp, "binary")) {
213 fx.decodetext = CE_BINARY;
214 } else {
215 die("invalid argument to %s", argp[-2]);
216 }
217 continue;
218 case NDECODETEXTSW:
219 fx.decodetext = 0;
220 continue;
221 case DECODETYPESW:
222 if (! (cp = *argp++) || *cp == '-') {
223 die("missing argument to %s", argp[-2]);
224 }
225 fx.decodetypes = cp;
226 continue;
227 case DECODEHEADERFIELDBODIESSW:
228 if (! (cp = *argp++) || *cp == '-') {
229 die("missing argument to %s", argp[-2]);
230 }
231 fx.decodeheaderfieldbodies = cp;
232 if (strcasecmp (cp, "utf-8") && strcasecmp (cp, "utf8")) {
233 /* Because UTF-8 strings can't have embedded nulls. Other
234 encodings support that, too, but we won't bother to
235 enumerate them. */
236 die("-decodeheaderfieldbodies only supports utf-8");
237 }
238 continue;
239 case NDECODEHEADERFIELDBODIESSW:
240 fx.decodeheaderfieldbodies = NULL;
241 continue;
242 case CRLFLINEBREAKSSW:
243 fx.lf_line_endings = 0;
244 continue;
245 case NCRLFLINEBREAKSSW:
246 fx.lf_line_endings = 1;
247 continue;
248 case TEXTCHARSETSW:
249 if (! (cp = *argp++) || (*cp == '-' && cp[1])) {
250 die("missing argument to %s", argp[-2]);
251 }
252 fx.textcharset = cp;
253 continue;
254 case NTEXTCHARSETSW:
255 fx.textcharset = 0;
256 continue;
257 case FIXBOUNDARYSW:
258 fx.fixboundary = 1;
259 continue;
260 case NFIXBOUNDARYSW:
261 fx.fixboundary = 0;
262 continue;
263 case FIXCOMPOSITECTESW:
264 fx.fixcompositecte = 1;
265 continue;
266 case NFIXCOMPOSITECTESW:
267 fx.fixcompositecte = 0;
268 continue;
269 case FIXTYPESW:
270 if (! (cp = *argp++) || (*cp == '-' && cp[1])) {
271 die("missing argument to %s", argp[-2]);
272 }
273 if (! strncasecmp (cp, "multipart/", 10) ||
274 ! strncasecmp (cp, "message/", 8))
275 die("-fixtype %s not allowed", cp);
276 if (! strchr (cp, '/'))
277 die("-fixtype requires type/subtype");
278 if (fx.fixtypes == NULL) { fx.fixtypes = svector_create (10); }
279 svector_push_back (fx.fixtypes, cp);
280 continue;
281 case REFORMATSW:
282 fx.reformat = 1;
283 continue;
284 case NREFORMATSW:
285 fx.reformat = 0;
286 continue;
287 case REPLACETEXTPLAINSW:
288 fx.replacetextplain = 1;
289 continue;
290 case NREPLACETEXTPLAINSW:
291 fx.replacetextplain = 0;
292 continue;
293 case FILESW:
294 if (! (cp = *argp++) || (*cp == '-' && cp[1])) {
295 die("missing argument to %s", argp[-2]);
296 }
297 file = *cp == '-' ? mh_xstrdup (cp) : path (cp, TFILE);
298 continue;
299 case OUTFILESW:
300 if (! (cp = *argp++) || (*cp == '-' && cp[1])) {
301 die("missing argument to %s", argp[-2]);
302 }
303 outfile = *cp == '-' ? mh_xstrdup (cp) : path (cp, TFILE);
304 continue;
305 case RPROCSW:
306 if (!(rmmproc = *argp++) || *rmmproc == '-') {
307 die("missing argument to %s", argp[-2]);
308 }
309 continue;
310 case NRPRCSW:
311 rmmproc = NULL;
312 continue;
313 case CHGSW:
314 chgflag = true;
315 continue;
316 case NCHGSW:
317 chgflag = false;
318 continue;
319 case VERBSW:
320 verbosw = 1;
321 continue;
322 case NVERBSW:
323 verbosw = 0;
324 continue;
325 }
326 }
327 if (*cp == '+' || *cp == '@') {
328 if (folder)
329 die("only one folder at a time!");
330 folder = pluspath (cp);
331 } else {
332 if (*cp == '/') {
333 /* Interpret a full path as a filename, not a message. */
334 file = mh_xstrdup (cp);
335 } else {
336 app_msgarg (&msgs, cp);
337 }
338 }
339 }
340
341 SIGNAL (SIGQUIT, quitser);
342 SIGNAL (SIGPIPE, pipeser);
343
344 /*
345 * Read the standard profile setup
346 */
347 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
348 readconfig(NULL, fp, cp, 0);
349 fclose (fp);
350 }
351
352 suppress_bogus_mp_content_warning = skip_mp_cte_check = true;
353 suppress_extraneous_trailing_semicolon_warning = true;
354
355 if (! context_find ("path")) {
356 free (path ("./", TFOLDER));
357 }
358
359 if (file && msgs.size) {
360 die("cannot specify msg and file at same time!");
361 }
362
363 if (outfile) {
364 /* Open the outfile now, so we don't have to risk opening it
365 after running out of fds. */
366 if (strcmp (outfile, "-") == 0) {
367 outfp = stdout;
368 } else if ((outfp = fopen (outfile, "w")) == NULL) {
369 adios (outfile, "unable to open for writing");
370 }
371 }
372
373 /*
374 * check if message is coming from file
375 */
376 if (file) {
377 /* If file is stdin, create a tmp file name before parse_mime()
378 has a chance, because it might put in on a different
379 filesystem than the output file. Instead, put it in the
380 user's preferred tmp directory. */
381 CT ct;
382
383 if (! strcmp ("-", file)) {
384 int fd;
385 char *cp;
386
387 using_stdin = true;
388
389 if ((cp = m_mktemp2 (NULL, invo_name, &fd, NULL)) == NULL) {
390 die("unable to create temporary file in %s",
391 get_temp_dir());
392 } else {
393 free (file);
394 file = mh_xstrdup (cp);
395 cpydata (STDIN_FILENO, fd, "-", file);
396 }
397
398 if (close (fd)) {
399 (void) m_unlink (file);
400 die("failed to write temporary file");
401 }
402 }
403
404 cts = mh_xcalloc(2, sizeof *cts);
405 ctp = cts;
406
407 if ((ct = parse_mime (file))) {
408 set_text_ctparams(ct, fx.decodetypes, fx.lf_line_endings);
409 *ctp++ = ct;
410 } else {
411 inform("unable to parse message from file %s", file);
412 status = NOTOK;
413
414 /* If there's an outfile, pass the input message unchanged, so the
415 message won't get dropped from a pipeline. */
416 if (outfile) {
417 /* Something went wrong. Output might be expected, such as if
418 this were run as a filter. Just copy the input to the
419 output. */
420 if ((infp = fopen (file, "r")) == NULL) {
421 adios (file, "unable to open for reading");
422 }
423
424 if (copy_input_to_output (file, infp, outfile, outfp) != OK) {
425 inform("unable to copy message to %s, "
426 "it might be lost\n", outfile);
427 }
428
429 fclose (infp);
430 infp = NULL;
431 }
432 }
433 } else {
434 /*
435 * message(s) are coming from a folder
436 */
437 CT ct;
438
439 if (! msgs.size) {
440 app_msgarg(&msgs, "cur");
441 }
442 if (! folder) {
443 folder = getfolder (1);
444 }
445 maildir = mh_xstrdup(m_maildir (folder));
446
447 /* chdir so that error messages, esp. from MIME parser, just
448 refer to the message and not its path. */
449 if (chdir (maildir) == NOTOK) {
450 adios (maildir, "unable to change directory to");
451 }
452
453 /* read folder and create message structure */
454 if (! (mp = folder_read (folder, 1))) {
455 die("unable to read folder %s", folder);
456 }
457
458 /* check for empty folder */
459 if (mp->nummsg == 0) {
460 die("no messages in %s", folder);
461 }
462
463 /* parse all the message ranges/sequences and set SELECTED */
464 for (msgnum = 0; msgnum < msgs.size; msgnum++)
465 if (! m_convert (mp, msgs.msgs[msgnum])) {
466 done (1);
467 }
468 seq_setprev (mp); /* set the previous-sequence */
469
470 cts = mh_xcalloc(mp->numsel + 1, sizeof *cts);
471 ctp = cts;
472
473 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
474 if (is_selected(mp, msgnum)) {
475 char *msgnam = m_name (msgnum);
476
477 if ((ct = parse_mime (msgnam))) {
478 set_text_ctparams(ct, fx.decodetypes, fx.lf_line_endings);
479 *ctp++ = ct;
480 } else {
481 inform("unable to parse message %s", msgnam);
482 status = NOTOK;
483
484 /* If there's an outfile, pass the input message
485 unchanged, so the message won't get dropped from a
486 pipeline. */
487 if (outfile) {
488 /* Something went wrong. Output might be expected,
489 such as if this were run as a filter. Just copy
490 the input to the output. */
491 /* Can't use path() here because 1) it might have been
492 called before and it caches the pwd, and 2) we call
493 chdir() after that. */
494 char *input_filename =
495 concat (maildir, "/", msgnam, NULL);
496
497 if ((infp = fopen (input_filename, "r")) == NULL) {
498 adios (input_filename,
499 "unable to open for reading");
500 }
501
502 if (copy_input_to_output (input_filename, infp,
503 outfile, outfp) != OK) {
504 inform("unable to copy message to %s, "
505 "it might be lost\n", outfile);
506 }
507
508 fclose (infp);
509 infp = NULL;
510 free (input_filename);
511 }
512 }
513 }
514 }
515
516 if (chgflag) {
517 seq_setcur (mp, mp->hghsel); /* update current message */
518 }
519 seq_save (mp); /* synchronize sequences */
520 context_replace (pfolder, folder);/* update current folder */
521 context_save (); /* save the context file */
522 }
523
524 if (*cts) {
525 for (ctp = cts; *ctp; ++ctp) {
526 status =
527 mhfixmsgsbr (ctp, maildir, &fx, &infp, outfile, &outfp) == OK
528 ? 0
529 : 1;
530 free_content (*ctp);
531
532 if (using_stdin) {
533 (void) m_unlink (file);
534
535 if (! outfile) {
536 /* Just calling m_backup() unlinks the backup file. */
537 (void) m_backup (file);
538 }
539 }
540 }
541 } else {
542 status = 1;
543 }
544
545 free(maildir);
546 free (cts);
547
548 if (fx.fixtypes != NULL) { svector_free (fx.fixtypes); }
549 if (infp) { fclose (infp); } /* even if stdin */
550 if (outfp) { fclose (outfp); } /* even if stdout */
551 free (outfile);
552 free (file);
553 free (folder);
554 free (arguments);
555
556 done (status == OK ? 0 : 1);
557 return NOTOK;
558 }
559
560
561 /*
562 * Apply transformations to one message.
563 */
564 static int
565 mhfixmsgsbr (CT *ctp, char *maildir, const fix_transformations *fx,
566 FILE **infp, char *outfile, FILE **outfp)
567 {
568 /* Store input filename in case one of the transformations, i.e.,
569 fix_boundary(), rewrites to a tmp file. */
570 char *input_filename = maildir
571 ? concat (maildir, "/", (*ctp)->c_file, NULL)
572 : mh_xstrdup ((*ctp)->c_file);
573 bool modify_inplace = false;
574 int message_mods = 0;
575 int status = OK;
576
577 /* Though the input file won't need to be opened if everything goes
578 well, do it here just in case there's a failure, and that failure is
579 running out of file descriptors. */
580 if ((*infp = fopen (input_filename, "r")) == NULL) {
581 adios (input_filename, "unable to open for reading");
582 }
583
584 if (outfile == NULL) {
585 modify_inplace = true;
586
587 if ((*ctp)->c_file) {
588 char *tempfile;
589 /* outfp will be closed by the caller */
590 if ((tempfile = m_mktemp2 (NULL, invo_name, NULL, outfp)) ==
591 NULL) {
592 die("unable to create temporary file in %s",
593 get_temp_dir());
594 }
595 outfile = mh_xstrdup (tempfile);
596 } else {
597 die("missing both input and output filenames\n");
598 }
599 } /* else *outfp was defined by caller */
600
601 reverse_alternative_parts (*ctp);
602 status = fix_always (*ctp, &message_mods);
603 if (status == OK && fx->fixboundary) {
604 status = fix_boundary (ctp, &message_mods);
605 }
606 if (status == OK && fx->fixtypes != NULL) {
607 status = fix_types (*ctp, fx->fixtypes, &message_mods);
608 }
609 if (status == OK && fx->fixcompositecte) {
610 status = fix_composite_cte (*ctp, &message_mods);
611 }
612 if (status == OK && fx->reformat) {
613 status =
614 ensure_text_plain (ctp, NULL, &message_mods, fx->replacetextplain);
615 }
616 if (status == OK && fx->decodetext) {
617 status = decode_text_parts (*ctp, fx->decodetext, fx->decodetypes,
618 &message_mods);
619 update_cte (*ctp);
620 }
621 if (status == OK && fx->decodeheaderfieldbodies) {
622 status = decode_header_field_bodies(*ctp, &message_mods);
623 }
624 if (status == OK && fx->textcharset != NULL) {
625 status = convert_charsets (*ctp, fx->textcharset, &message_mods);
626 }
627
628 if (status == OK && ! (*ctp)->c_umask) {
629 /* Set the umask for the contents file. This currently
630 isn't used but just in case it is in the future. */
631 struct stat st;
632
633 if (stat ((*ctp)->c_file, &st) != NOTOK) {
634 (*ctp)->c_umask = ~(st.st_mode & 0777);
635 } else {
636 (*ctp)->c_umask = ~m_gmprot();
637 }
638 }
639
640 /*
641 * Write the content to a file
642 */
643 if (status == OK) {
644 status = write_content (*ctp, input_filename, outfile, *outfp,
645 modify_inplace, message_mods);
646 } else if (! modify_inplace) {
647 /* Something went wrong. Output might be expected, such
648 as if this were run as a filter. Just copy the input
649 to the output. */
650 if (copy_input_to_output (input_filename, *infp, outfile,
651 *outfp) != OK) {
652 inform("unable to copy message to %s, it might be lost\n",
653 outfile);
654 }
655 }
656
657 if (modify_inplace) {
658 if (status != OK) { (void) m_unlink (outfile); }
659 free (outfile);
660 outfile = NULL;
661 }
662
663 fclose (*infp);
664 *infp = NULL;
665 free (input_filename);
666
667 return status;
668 }
669
670
671 /*
672 * Copy input message to output. Assumes not modifying in place, so this
673 * might be running as part of a pipeline.
674 */
675 static int
676 copy_input_to_output (const char *input_filename, FILE *infp,
677 const char *output_filename, FILE *outfp)
678 {
679 int in = fileno (infp);
680 int out = fileno (outfp);
681 int status = OK;
682
683 if (in != -1 && out != -1) {
684 cpydata (in, out, input_filename, output_filename);
685 } else {
686 status = NOTOK;
687 }
688
689 return status;
690 }
691
692
693 /*
694 * Fix mismatched outer level boundary.
695 */
696 static int
697 fix_boundary (CT *ct, int *message_mods)
698 {
699 struct multipart *mp;
700 int status = OK;
701
702 if (ct && (*ct)->c_type == CT_MULTIPART && bogus_mp_content) {
703 mp = (struct multipart *) (*ct)->c_ctparams;
704
705 /*
706 * 1) Get boundary at end of part.
707 * 2) Get boundary at beginning of part and compare to the end-of-part
708 * boundary.
709 * 3) Write out contents of ct to tmp file, replacing boundary in
710 * header with boundary from part. Set c_unlink to 1.
711 * 4) Free ct.
712 * 5) Call parse_mime() on the tmp file, replacing ct.
713 */
714
715 if (mp && mp->mp_start) {
716 char *part_boundary;
717
718 if (get_multipart_boundary (*ct, &part_boundary) == OK) {
719 char *fixed;
720
721 if ((fixed = m_mktemp2 (NULL, invo_name, NULL, &(*ct)->c_fp))) {
722 if (replace_boundary (*ct, fixed, part_boundary) == OK) {
723 char *filename = mh_xstrdup ((*ct)->c_file);
724 CT fixed_ct;
725
726 free_content (*ct);
727 if ((fixed_ct = parse_mime (fixed))) {
728 *ct = fixed_ct;
729 (*ct)->c_unlink = 1;
730
731 ++*message_mods;
732 if (verbosw) {
733 report (NULL, NULL, filename,
734 "fix multipart boundary");
735 }
736 } else {
737 *ct = NULL;
738 inform("unable to parse fixed part");
739 status = NOTOK;
740 }
741 free (filename);
742 } else {
743 inform("unable to replace broken boundary");
744 status = NOTOK;
745 }
746 } else {
747 inform("unable to create temporary file in %s",
748 get_temp_dir());
749 status = NOTOK;
750 }
751
752 free (part_boundary);
753 } else {
754 /* Couldn't fix the boundary. Report failure so that mhfixmsg
755 doesn't modify the message. */
756 status = NOTOK;
757 }
758 } else {
759 /* No multipart struct, even though the content type is
760 CT_MULTIPART. Report failure so that mhfixmsg doesn't modify
761 the message. */
762 status = NOTOK;
763 }
764 }
765
766 return status;
767 }
768
769
770 /*
771 * Find boundary at end of multipart.
772 */
773 static int
774 get_multipart_boundary (CT ct, char **part_boundary)
775 {
776 char buffer[NMH_BUFSIZ];
777 char *end_boundary = NULL;
778 off_t begin = (off_t) ct->c_end > (off_t) (ct->c_begin + sizeof buffer)
779 ? (off_t) (ct->c_end - sizeof buffer)
780 : (off_t) ct->c_begin;
781 size_t bytes_read;
782 int status = OK;
783
784 /* This will fail if the boundary spans fread() calls. NMH_BUFSIZ should
785 be big enough, even if it's just 1024, to make that unlikely. */
786
787 /* free_content() will close ct->c_fp if bogus MP boundary is fixed. */
788 if (! ct->c_fp && (ct->c_fp = fopen (ct->c_file, "r")) == NULL) {
789 advise (ct->c_file, "unable to open for reading");
790 return NOTOK;
791 }
792
793 /* Get boundary at end of multipart. */
794 while (begin >= (off_t) ct->c_begin) {
795 fseeko (ct->c_fp, begin, SEEK_SET);
796 while ((bytes_read = fread (buffer, 1, sizeof buffer, ct->c_fp)) > 0) {
797 char *cp = rfind_str (buffer, bytes_read, "--");
798
799 if (cp) {
800 char *end;
801
802 /* Trim off trailing "--" and anything beyond. */
803 *cp-- = '\0';
804 if ((end = rfind_str (buffer, cp - buffer, "\n"))) {
805 if (strlen (end) > 3 && *end++ == '\n' &&
806 *end++ == '-' && *end++ == '-') {
807 end_boundary = mh_xstrdup (end);
808 break;
809 }
810 }
811 }
812 }
813
814 if (end_boundary || begin <= (off_t) (ct->c_begin + sizeof buffer))
815 break;
816 begin -= sizeof buffer;
817 }
818
819 /* Get boundary at beginning of multipart. */
820 if (end_boundary) {
821 fseeko (ct->c_fp, ct->c_begin, SEEK_SET);
822 while ((bytes_read = fread (buffer, 1, sizeof buffer, ct->c_fp)) > 0) {
823 if (bytes_read >= strlen (end_boundary)) {
824 char *cp = find_str (buffer, bytes_read, end_boundary);
825
826 if (cp && cp - buffer >= 2 && *--cp == '-' &&
827 *--cp == '-' && (cp > buffer && *--cp == '\n')) {
828 status = OK;
829 break;
830 }
831 } else {
832 /* The start and end boundaries didn't match, or the
833 start boundary doesn't begin with "\n--" (or "--"
834 if at the beginning of buffer). Keep trying. */
835 status = NOTOK;
836 }
837 }
838 } else {
839 status = NOTOK;
840 }
841
842 if (ct->c_fp) {
843 fclose (ct->c_fp);
844 ct->c_fp = NULL;
845 }
846
847 if (status == OK) {
848 *part_boundary = end_boundary;
849 } else {
850 *part_boundary = NULL;
851 free (end_boundary);
852 }
853
854 return status;
855 }
856
857
858 /*
859 * Open and copy ct->c_file to file, replacing the multipart boundary.
860 */
861 static int
862 replace_boundary (CT ct, char *file, char *boundary)
863 {
864 FILE *fpin, *fpout;
865 int compnum, state;
866 char buf[NMH_BUFSIZ], name[NAMESZ];
867 char *np, *vp;
868 m_getfld_state_t gstate;
869 int status = OK;
870
871 if (ct->c_file == NULL) {
872 inform("missing input filename");
873 return NOTOK;
874 }
875
876 if ((fpin = fopen (ct->c_file, "r")) == NULL) {
877 advise (ct->c_file, "unable to open for reading");
878 return NOTOK;
879 }
880
881 if ((fpout = fopen (file, "w")) == NULL) {
882 fclose (fpin);
883 advise (file, "unable to open for writing");
884 return NOTOK;
885 }
886
887 gstate = m_getfld_state_init(fpin);
888 for (compnum = 1;;) {
889 int bufsz = (int) sizeof buf;
890
891 switch (state = m_getfld2(&gstate, name, buf, &bufsz)) {
892 case FLD:
893 case FLDPLUS:
894 compnum++;
895
896 /* get copies of the buffers */
897 np = mh_xstrdup (name);
898 vp = mh_xstrdup (buf);
899
900 /* if necessary, get rest of field */
901 while (state == FLDPLUS) {
902 bufsz = sizeof buf;
903 state = m_getfld2(&gstate, name, buf, &bufsz);
904 vp = add (buf, vp); /* add to previous value */
905 }
906
907 if (strcasecmp (TYPE_FIELD, np)) {
908 fprintf (fpout, "%s:%s", np, vp);
909 } else {
910 char *new_ctline, *new_params;
911
912 replace_param(&ct->c_ctinfo.ci_first_pm,
913 &ct->c_ctinfo.ci_last_pm, "boundary",
914 boundary, 0);
915
916 new_ctline = concat(" ", ct->c_ctinfo.ci_type, "/",
917 ct->c_ctinfo.ci_subtype, NULL);
918 new_params = output_params(LEN(TYPE_FIELD) +
919 strlen(new_ctline) + 1,
920 ct->c_ctinfo.ci_first_pm, NULL, 0);
921 fprintf (fpout, "%s:%s%s\n", np, new_ctline,
922 FENDNULL(new_params));
923 free(new_ctline);
924 free(new_params);
925 }
926
927 free (vp);
928 free (np);
929
930 continue;
931
932 case BODY:
933 putc('\n', fpout);
934 /* buf will have a terminating NULL, skip it. */
935 if ((int) fwrite (buf, 1, bufsz-1, fpout) < bufsz-1) {
936 advise (file, "fwrite");
937 }
938 continue;
939
940 case FILEEOF:
941 break;
942
943 case LENERR:
944 case FMTERR:
945 inform("message format error in component #%d", compnum);
946 status = NOTOK;
947 break;
948
949 default:
950 inform("getfld() returned %d", state);
951 status = NOTOK;
952 break;
953 }
954
955 break;
956 }
957
958 m_getfld_state_destroy (&gstate);
959 fclose (fpout);
960 fclose (fpin);
961
962 return status;
963 }
964
965
966 /*
967 * Fix Content-Type header to reflect the content of its part.
968 */
969 static int
970 fix_types (CT ct, svector_t fixtypes, int *message_mods)
971 {
972 int status = OK;
973
974 switch (ct->c_type) {
975 case CT_MULTIPART: {
976 struct multipart *m = (struct multipart *) ct->c_ctparams;
977 struct part *part;
978
979 for (part = m->mp_parts; status == OK && part; part = part->mp_next) {
980 status = fix_types (part->mp_part, fixtypes, message_mods);
981 }
982 break;
983 }
984
985 case CT_MESSAGE:
986 if (ct->c_subtype == MESSAGE_EXTERNAL) {
987 struct exbody *e = (struct exbody *) ct->c_ctparams;
988
989 status = fix_types (e->eb_content, fixtypes, message_mods);
990 }
991 break;
992
993 default: {
994 char **typep, *type;
995
996 if (ct->c_ctinfo.ci_type && ct->c_ctinfo.ci_subtype) {
997 for (typep = svector_strs (fixtypes);
998 typep && (type = *typep);
999 ++typep) {
1000 char *type_subtype =
1001 concat (ct->c_ctinfo.ci_type, "/", ct->c_ctinfo.ci_subtype,
1002 NULL);
1003
1004 if (! strcasecmp (type, type_subtype) &&
1005 decode_part (ct) == OK &&
1006 ct->c_cefile.ce_file != NULL) {
1007 char *ct_type_subtype = mime_type (ct->c_cefile.ce_file);
1008 char *cp;
1009
1010 if ((cp = strchr (ct_type_subtype, ';'))) {
1011 /* Truncate to remove any parameter list from
1012 mime_type () result. */
1013 *cp = '\0';
1014 }
1015
1016 if (strcasecmp (type, ct_type_subtype)) {
1017 char *ct_type, *ct_subtype;
1018 HF hf;
1019
1020 /* The Content-Type header does not match the
1021 content, so update these struct Content
1022 fields to match:
1023 * c_type, c_subtype
1024 * c_ctinfo.ci_type, c_ctinfo.ci_subtype
1025 * c_ctline
1026 */
1027 /* Extract type and subtype from type/subtype. */
1028 ct_type = mh_xstrdup(ct_type_subtype);
1029 if ((cp = strchr (ct_type, '/'))) {
1030 *cp = '\0';
1031 ct_subtype = mh_xstrdup(++cp);
1032 } else {
1033 inform("missing / in MIME type of %s %s",
1034 ct->c_file, ct->c_partno);
1035 free (ct_type);
1036 return NOTOK;
1037 }
1038
1039 ct->c_type = ct_str_type (ct_type);
1040 ct->c_subtype = ct_str_subtype (ct->c_type, ct_subtype);
1041
1042 free (ct->c_ctinfo.ci_type);
1043 ct->c_ctinfo.ci_type = ct_type;
1044 free (ct->c_ctinfo.ci_subtype);
1045 ct->c_ctinfo.ci_subtype = ct_subtype;
1046 if (! replace_substring (&ct->c_ctline, type,
1047 ct_type_subtype)) {
1048 inform("did not find %s in %s",
1049 type, ct->c_ctline);
1050 }
1051
1052 /* Update Content-Type header field. */
1053 for (hf = ct->c_first_hf; hf; hf = hf->next) {
1054 if (! strcasecmp (TYPE_FIELD, hf->name)) {
1055 if (replace_substring (&hf->value, type,
1056 ct_type_subtype)) {
1057 ++*message_mods;
1058 if (verbosw) {
1059 report (NULL, ct->c_partno, ct->c_file,
1060 "change Content-Type in header "
1061 "from %s to %s",
1062 type, ct_type_subtype);
1063 }
1064 break;
1065 }
1066 inform("did not find %s in %s", type, hf->value);
1067 }
1068 }
1069 }
1070 free (ct_type_subtype);
1071 }
1072 free (type_subtype);
1073 }
1074 }
1075 }}
1076
1077 return status;
1078 }
1079
1080
1081 /*
1082 * Replace a substring, allocating space to hold the new one.
1083 */
1084 char *
1085 replace_substring (char **str, const char *old, const char *new)
1086 {
1087 char *cp;
1088
1089 if ((cp = strstr (*str, old))) {
1090 char *remainder = cp + strlen (old);
1091 char *prefix, *new_str;
1092
1093 if (cp - *str) {
1094 prefix = mh_xstrdup(*str);
1095 *(prefix + (cp - *str)) = '\0';
1096 new_str = concat (prefix, new, remainder, NULL);
1097 free (prefix);
1098 } else {
1099 new_str = concat (new, remainder, NULL);
1100 }
1101
1102 free (*str);
1103
1104 return *str = new_str;
1105 }
1106
1107 return NULL;
1108 }
1109
1110
1111 /*
1112 * Remove a name=value parameter, given just its name, from a header value.
1113 */
1114 char *
1115 remove_parameter (char *str, const char *name)
1116 {
1117 /* It looks to me, based on the BNF in RFC 2045, than there can't
1118 be whitespace between the parameter name and the "=", or
1119 between the "=" and the parameter value. */
1120 char *param_name = concat (name, "=", NULL);
1121 char *cp;
1122
1123 if ((cp = strstr (str, param_name))) {
1124 char *start, *end;
1125 size_t count = 1;
1126
1127 /* Remove any leading spaces, before the parameter name. */
1128 for (start = cp;
1129 start > str && isspace ((unsigned char) *(start-1));
1130 --start) {
1131 continue;
1132 }
1133 /* Remove a leading semicolon. */
1134 if (start > str && *(start-1) == ';') { --start; }
1135
1136 end = cp + strlen (name) + 1;
1137 if (*end == '"') {
1138 /* Skip past the quoted value, and then the final quote. */
1139 for (++end ; *end && *end != '"'; ++end) { continue; }
1140 ++end;
1141 } else {
1142 /* Skip past the value. */
1143 for (++end ; *end && ! isspace ((unsigned char) *end); ++end) {}
1144 }
1145
1146 /* Count how many characters need to be moved. Include
1147 trailing null, which is accounted for by the
1148 initialization of count to 1. */
1149 for (cp = end; *cp; ++cp) { ++count; }
1150 (void) memmove (start, end, count);
1151 }
1152
1153 free (param_name);
1154
1155 return str;
1156 }
1157
1158
1159 /*
1160 * Fix Content-Transfer-Encoding of composite,, e.g., message or multipart, part.
1161 * According to RFC 2045 Sec. 6.4, it must be 7bit, 8bit, or binary. Set it to
1162 * 8 bit.
1163 */
1164 static int
1165 fix_composite_cte (CT ct, int *message_mods)
1166 {
1167 int status = OK;
1168
1169 if (ct->c_type == CT_MESSAGE || ct->c_type == CT_MULTIPART) {
1170 if (ct->c_encoding != CE_7BIT && ct->c_encoding != CE_8BIT &&
1171 ct->c_encoding != CE_BINARY) {
1172 HF hf;
1173
1174 for (hf = ct->c_first_hf; hf; hf = hf->next) {
1175 char *name = hf->name;
1176 for (; isspace((unsigned char)*name); ++name) {
1177 continue;
1178 }
1179
1180 if (! strncasecmp (name, ENCODING_FIELD,
1181 LEN(ENCODING_FIELD))) {
1182 char *prefix = "Nmh-REPLACED-INVALID-";
1183 HF h;
1184
1185 NEW(h);
1186 h->name = mh_xstrdup (hf->name);
1187 h->next = hf->next;
1188 hf->next = h;
1189
1190 /* Retain old header but prefix its name. */
1191 free (hf->name);
1192 hf->name = concat (prefix, h->name, NULL);
1193
1194 ++*message_mods;
1195 if (verbosw) {
1196 char *encoding = cpytrim (hf->value);
1197 report (NULL, ct->c_partno, ct->c_file,
1198 "replace Content-Transfer-Encoding of %s "
1199 "with 8 bit", encoding);
1200 free (encoding);
1201 }
1202
1203 h->value = mh_xstrdup (" 8bit\n");
1204
1205 /* Don't need to warn for multiple C-T-E header
1206 fields, parse_mime() already does that. But
1207 if there are any, fix them all as necessary. */
1208 hf = h;
1209 }
1210 }
1211
1212 set_ce (ct, CE_8BIT);
1213 }
1214
1215 if (ct->c_type == CT_MULTIPART) {
1216 struct multipart *m;
1217 struct part *part;
1218
1219 m = (struct multipart *) ct->c_ctparams;
1220 for (part = m->mp_parts; part; part = part->mp_next) {
1221 if (fix_composite_cte (part->mp_part, message_mods) != OK) {
1222 status = NOTOK;
1223 break;
1224 }
1225 }
1226 }
1227 }
1228
1229 return status;
1230 }
1231
1232
1233 /*
1234 * Set content encoding.
1235 */
1236 static int
1237 set_ce (CT ct, int encoding)
1238 {
1239 const char *ce = ce_str (encoding);
1240 const struct str2init *ctinit = get_ce_method (ce);
1241
1242 if (ctinit) {
1243 char *cte = concat (" ", ce, "\n", NULL);
1244 bool found_cte = false;
1245 HF hf;
1246 /* Decoded contents might be in ct->c_cefile.ce_file, if the
1247 caller is decode_text_parts (). Save because we'll
1248 overwrite below. */
1249 struct cefile decoded_content_info = ct->c_cefile;
1250
1251 ct->c_encoding = encoding;
1252
1253 ct->c_ctinitfnx = ctinit->si_init;
1254 /* This will assign ct->c_cefile with an all-0 struct, which
1255 is what we want. */
1256 (*ctinit->si_init) (ct);
1257 /* After returning, the caller should set
1258 ct->c_cefile.ce_file to the name of the file containing
1259 the contents. */
1260
1261 if (ct->c_ceclosefnx) {
1262 (*ct->c_ceclosefnx) (ct);
1263 }
1264
1265 /* Restore the cefile. */
1266 ct->c_cefile = decoded_content_info;
1267
1268 /* Update/add Content-Transfer-Encoding header field. */
1269 for (hf = ct->c_first_hf; hf; hf = hf->next) {
1270 if (! strcasecmp (ENCODING_FIELD, hf->name)) {
1271 found_cte = true;
1272 free (hf->value);
1273 hf->value = cte;
1274 }
1275 }
1276 if (! found_cte) {
1277 add_header (ct, mh_xstrdup (ENCODING_FIELD), cte);
1278 }
1279
1280 /* Update c_celine. It's used only by mhlist -debug. */
1281 free (ct->c_celine);
1282 ct->c_celine = mh_xstrdup (cte);
1283
1284 return OK;
1285 }
1286
1287 return NOTOK;
1288 }
1289
1290
1291 /*
1292 * Make sure each text part has a corresponding text/plain part.
1293 */
1294 static int
1295 ensure_text_plain (CT *ct, CT parent, int *message_mods, int replacetextplain)
1296 {
1297 int status = OK;
1298
1299 switch ((*ct)->c_type) {
1300 case CT_TEXT: {
1301 /* Nothing to do for text/plain. */
1302 if ((*ct)->c_subtype == TEXT_PLAIN) { return OK; }
1303
1304 if (parent && parent->c_type == CT_MULTIPART &&
1305 parent->c_subtype == MULTI_ALTERNATE) {
1306 int new_subpart_number = 1;
1307 int has_text_plain =
1308 find_textplain_sibling (parent, replacetextplain,
1309 &new_subpart_number);
1310
1311 if (! has_text_plain) {
1312 /* Parent is a multipart/alternative. Insert a new
1313 text/plain subpart. */
1314 const int inserted =
1315 insert_new_text_plain_part (*ct, new_subpart_number,
1316 parent);
1317 if (inserted) {
1318 ++*message_mods;
1319 if (verbosw) {
1320 report (NULL, parent->c_partno, parent->c_file,
1321 "insert text/plain part");
1322 }
1323 } else {
1324 status = NOTOK;
1325 }
1326 }
1327 } else if (parent && parent->c_type == CT_MULTIPART &&
1328 parent->c_subtype == MULTI_RELATED) {
1329 char *type_subtype =
1330 concat ((*ct)->c_ctinfo.ci_type, "/",
1331 (*ct)->c_ctinfo.ci_subtype, NULL);
1332 const char *parent_type =
1333 get_param (parent->c_ctinfo.ci_first_pm, "type", '?', 1);
1334 int new_subpart_number = 1;
1335 int has_text_plain = 0;
1336
1337 /* Have to do string comparison on the subtype because we
1338 don't enumerate all of them in c_subtype values.
1339 parent_type will be NULL if the multipart/related part
1340 doesn't have a type parameter. The type parameter must
1341 be specified according to RFC 2387 Sec. 3.1 but not all
1342 messages comply. */
1343 if (parent_type && strcasecmp (type_subtype, parent_type) == 0) {
1344 /* The type of this part matches the root type of the
1345 parent multipart/related. Look to see if there's
1346 text/plain sibling. */
1347 has_text_plain =
1348 find_textplain_sibling (parent, replacetextplain,
1349 &new_subpart_number);
1350 }
1351
1352 free (type_subtype);
1353
1354 if (! has_text_plain) {
1355 struct multipart *mp = (struct multipart *) parent->c_ctparams;
1356 struct part *part;
1357 int siblings = 0;
1358
1359 for (part = mp->mp_parts; part; part = part->mp_next) {
1360 if (*ct != part->mp_part) {
1361 ++siblings;
1362 }
1363 }
1364
1365 if (siblings) {
1366 /* Parent is a multipart/related. Insert a new
1367 text/plain subpart in a new multipart/alternative. */
1368 if (insert_into_new_mp_alt (ct, message_mods)) {
1369 /* Not an error if text/plain couldn't be added. */
1370 }
1371 } else {
1372 /* There are no siblings, so insert a new text/plain
1373 subpart, and change the parent type from
1374 multipart/related to multipart/alternative. */
1375 const int inserted =
1376 insert_new_text_plain_part (*ct, new_subpart_number,
1377 parent);
1378
1379 if (inserted) {
1380 HF hf;
1381
1382 parent->c_subtype = MULTI_ALTERNATE;
1383 free (parent->c_ctinfo.ci_subtype);
1384 parent->c_ctinfo.ci_subtype = mh_xstrdup("alternative");
1385 if (! replace_substring (&parent->c_ctline, "/related",
1386 "/alternative")) {
1387 inform("did not find multipart/related in %s",
1388 parent->c_ctline);
1389 }
1390
1391 /* Update Content-Type header field. */
1392 for (hf = parent->c_first_hf; hf; hf = hf->next) {
1393 if (! strcasecmp (TYPE_FIELD, hf->name)) {
1394 if (replace_substring (&hf->value, "/related",
1395 "/alternative")) {
1396 ++*message_mods;
1397 if (verbosw) {
1398 report (NULL, parent->c_partno,
1399 parent->c_file,
1400 "insert text/plain part");
1401 }
1402
1403 /* Remove, e.g., type="text/html" from
1404 multipart/alternative. */
1405 remove_parameter (hf->value, "type");
1406 break;
1407 }
1408 inform("did not find multipart/"
1409 "related in header %s", hf->value);
1410 }
1411 }
1412 } else {
1413 /* Not an error if text/plain couldn't be inserted. */
1414 }
1415 }
1416 }
1417 } else {
1418 if (insert_into_new_mp_alt (ct, message_mods)) {
1419 status = NOTOK;
1420 }
1421 }
1422 break;
1423 }
1424
1425 case CT_MULTIPART: {
1426 struct multipart *mp = (struct multipart *) (*ct)->c_ctparams;
1427 struct part *part;
1428
1429 for (part = mp->mp_parts; status == OK && part; part = part->mp_next) {
1430 if ((*ct)->c_type == CT_MULTIPART) {
1431 status = ensure_text_plain (&part->mp_part, *ct, message_mods,
1432 replacetextplain);
1433 }
1434 }
1435 break;
1436 }
1437
1438 case CT_MESSAGE:
1439 if ((*ct)->c_subtype == MESSAGE_EXTERNAL) {
1440 struct exbody *e = (struct exbody *) (*ct)->c_ctparams;
1441
1442 status = ensure_text_plain (&e->eb_content, *ct, message_mods,
1443 replacetextplain);
1444 }
1445 break;
1446 }
1447
1448 return status;
1449 }
1450
1451
1452 /*
1453 * See if there is a sibling text/plain, and return its subpart number.
1454 */
1455 static int
1456 find_textplain_sibling (CT parent, int replacetextplain,
1457 int *new_subpart_number)
1458 {
1459 struct multipart *mp = (struct multipart *) parent->c_ctparams;
1460 struct part *part, *prev;
1461 bool has_text_plain = false;
1462
1463 for (prev = part = mp->mp_parts; part; part = part->mp_next) {
1464 ++*new_subpart_number;
1465 if (part->mp_part->c_type == CT_TEXT &&
1466 part->mp_part->c_subtype == TEXT_PLAIN) {
1467 if (replacetextplain) {
1468 struct part *old_part;
1469 if (part == mp->mp_parts) {
1470 old_part = mp->mp_parts;
1471 mp->mp_parts = part->mp_next;
1472 } else {
1473 old_part = prev->mp_next;
1474 prev->mp_next = part->mp_next;
1475 }
1476 if (verbosw) {
1477 report (NULL, parent->c_partno, parent->c_file,
1478 "remove text/plain part %s",
1479 old_part->mp_part->c_partno);
1480 }
1481 free_content (old_part->mp_part);
1482 free (old_part);
1483 } else {
1484 has_text_plain = true;
1485 }
1486 break;
1487 }
1488 prev = part;
1489 }
1490
1491 return has_text_plain;
1492 }
1493
1494
1495 /*
1496 * Insert a new text/plain part.
1497 */
1498 static int
1499 insert_new_text_plain_part (CT ct, int new_subpart_number, CT parent)
1500 {
1501 struct multipart *mp = (struct multipart *) parent->c_ctparams;
1502 struct part *new_part;
1503
1504 NEW(new_part);
1505 if ((new_part->mp_part = build_text_plain_part (ct))) {
1506 char buffer[16];
1507 snprintf (buffer, sizeof buffer, "%d", new_subpart_number);
1508
1509 new_part->mp_next = mp->mp_parts;
1510 mp->mp_parts = new_part;
1511 new_part->mp_part->c_partno =
1512 concat (parent->c_partno ? parent->c_partno : "1", ".",
1513 buffer, NULL);
1514
1515 return 1;
1516 }
1517
1518 free_content (new_part->mp_part);
1519 free (new_part);
1520
1521 return 0;
1522 }
1523
1524
1525 /*
1526 * Create a text/plain part to go along with non-plain sibling part.
1527 */
1528 static CT
1529 build_text_plain_part (CT encoded_part)
1530 {
1531 CT tp_part = divide_part (encoded_part);
1532 char *tmp_plain_file = NULL;
1533
1534 if (decode_part (tp_part) == OK) {
1535 /* Now, tp_part->c_cefile.ce_file is the name of the tmp file that
1536 contains the decoded contents. And the decoding function, such
1537 as openQuoted, will have set ...->ce_unlink to 1 so that it will
1538 be unlinked by free_content (). */
1539 char *tempfile;
1540
1541 /* This m_mktemp2() call closes the temp file. */
1542 if ((tempfile = m_mktemp2 (NULL, invo_name, NULL, NULL)) == NULL) {
1543 inform("unable to create temporary file in %s",
1544 get_temp_dir());
1545 } else {
1546 tmp_plain_file = mh_xstrdup (tempfile);
1547 if (reformat_part (tp_part, tmp_plain_file,
1548 tp_part->c_ctinfo.ci_type,
1549 tp_part->c_ctinfo.ci_subtype,
1550 tp_part->c_type) == OK) {
1551 return tp_part;
1552 }
1553 }
1554 }
1555
1556 free_content (tp_part);
1557 if (tmp_plain_file) { (void) m_unlink (tmp_plain_file); }
1558 free (tmp_plain_file);
1559
1560 return NULL;
1561 }
1562
1563
1564 /*
1565 * Slip new text/plain part into a new multipart/alternative.
1566 */
1567 static int
1568 insert_into_new_mp_alt (CT *ct, int *message_mods)
1569 {
1570 CT tp_part = build_text_plain_part (*ct);
1571 int status = OK;
1572
1573 if (tp_part) {
1574 CT mp_alt = build_multipart_alt (*ct, tp_part, CT_MULTIPART,
1575 MULTI_ALTERNATE);
1576 if (mp_alt) {
1577 struct multipart *mp = (struct multipart *) mp_alt->c_ctparams;
1578
1579 if (mp && mp->mp_parts) {
1580 mp->mp_parts->mp_part = tp_part;
1581 /* Make the new multipart/alternative the parent. */
1582 *ct = mp_alt;
1583
1584 ++*message_mods;
1585 if (verbosw) {
1586 report (NULL, (*ct)->c_partno, (*ct)->c_file,
1587 "insert text/plain part");
1588 }
1589 } else {
1590 free_content (tp_part);
1591 free_content (mp_alt);
1592 status = NOTOK;
1593 }
1594 } else {
1595 status = NOTOK;
1596 }
1597 } else {
1598 /* Not an error if text/plain couldn't be built. */
1599 }
1600
1601 return status;
1602 }
1603
1604
1605 /*
1606 * Clone a MIME part.
1607 */
1608 static CT
1609 divide_part (CT ct)
1610 {
1611 CT new_part;
1612
1613 NEW0(new_part);
1614 /* Just copy over what is needed for decoding. c_vrsn and
1615 c_celine aren't necessary. */
1616 new_part->c_file = mh_xstrdup (ct->c_file);
1617 new_part->c_begin = ct->c_begin;
1618 new_part->c_end = ct->c_end;
1619 copy_ctinfo (&new_part->c_ctinfo, &ct->c_ctinfo);
1620 new_part->c_type = ct->c_type;
1621 new_part->c_cefile = ct->c_cefile;
1622 new_part->c_encoding = ct->c_encoding;
1623 new_part->c_ctinitfnx = ct->c_ctinitfnx;
1624 new_part->c_ceopenfnx = ct->c_ceopenfnx;
1625 new_part->c_ceclosefnx = ct->c_ceclosefnx;
1626 new_part->c_cesizefnx = ct->c_cesizefnx;
1627
1628 /* c_ctline is used by reformat__part(), so it can preserve
1629 anything after the type/subtype. */
1630 new_part->c_ctline = mh_xstrdup (ct->c_ctline);
1631
1632 return new_part;
1633 }
1634
1635
1636 /*
1637 * Copy the content info from one part to another.
1638 */
1639 static void
1640 copy_ctinfo (CI dest, CI src)
1641 {
1642 PM s_pm, d_pm;
1643
1644 dest->ci_type = src->ci_type ? mh_xstrdup (src->ci_type) : NULL;
1645 dest->ci_subtype = src->ci_subtype ? mh_xstrdup (src->ci_subtype) : NULL;
1646
1647 for (s_pm = src->ci_first_pm; s_pm; s_pm = s_pm->pm_next) {
1648 d_pm = add_param(&dest->ci_first_pm, &dest->ci_last_pm, s_pm->pm_name,
1649 s_pm->pm_value, 0);
1650 if (s_pm->pm_charset) {
1651 d_pm->pm_charset = mh_xstrdup(s_pm->pm_charset);
1652 }
1653 if (s_pm->pm_lang) {
1654 d_pm->pm_lang = mh_xstrdup(s_pm->pm_lang);
1655 }
1656 }
1657
1658 dest->ci_comment = src->ci_comment ? mh_xstrdup (src->ci_comment) : NULL;
1659 dest->ci_magic = src->ci_magic ? mh_xstrdup (src->ci_magic) : NULL;
1660 }
1661
1662
1663 /*
1664 * Decode content.
1665 */
1666 static int
1667 decode_part (CT ct)
1668 {
1669 char *tmp_decoded;
1670 int status;
1671 FILE *file;
1672 char *tempfile;
1673
1674 if ((tempfile = m_mktemp2 (NULL, invo_name, NULL, &file)) == NULL) {
1675 die("unable to create temporary file in %s", get_temp_dir());
1676 }
1677 tmp_decoded = mh_xstrdup (tempfile);
1678 /* The following call will load ct->c_cefile.ce_file with the tmp
1679 filename of the decoded content. tmp_decoded will contain the
1680 encoded output, get rid of that. */
1681 status = output_message_fp (ct, file, tmp_decoded);
1682 (void) m_unlink (tmp_decoded);
1683 free (tmp_decoded);
1684 if (fclose (file)) {
1685 inform("unable to close temporary file %s, continuing...", tempfile);
1686 }
1687
1688 return status;
1689 }
1690
1691
1692 /*
1693 * Reformat content as plain text.
1694 * Some of the arguments aren't really needed now, but maybe will
1695 * be in the future for other than text types.
1696 */
1697 static int
1698 reformat_part (CT ct, char *file, char *type, char *subtype, int c_type)
1699 {
1700 int output_subtype, output_encoding;
1701 const char *reason = NULL;
1702 char *cp, *cf;
1703 int status;
1704
1705 /* Hacky: this redirects the output from whatever command is used
1706 to show the part to a file. So, the user can't have any output
1707 redirection in that command.
1708 Could show_multi() in mhshowsbr.c avoid this? */
1709
1710 /* Check for invo_name-format-type/subtype. */
1711 if ((cf = context_find_by_type ("format", type, subtype)) == NULL) {
1712 if (verbosw) {
1713 inform("Don't know how to convert %s, there is no "
1714 "%s-format-%s/%s profile entry",
1715 ct->c_file, invo_name, type, subtype);
1716 }
1717 return NOTOK;
1718 }
1719 if (strchr (cf, '>')) {
1720 inform("'>' prohibited in \"%s\",\nplease fix your "
1721 "%s-format-%s/%s profile entry", cf, invo_name, type,
1722 FENDNULL(subtype));
1723
1724 return NOTOK;
1725 }
1726
1727 cp = concat (cf, " >", file, NULL);
1728 status = show_content_aux (ct, 0, cp, NULL, NULL);
1729 free (cp);
1730
1731 /* Unlink decoded content tmp file and free its filename to avoid
1732 leaks. The file stream should already have been closed. */
1733 if (ct->c_cefile.ce_unlink) {
1734 (void) m_unlink (ct->c_cefile.ce_file);
1735 free (ct->c_cefile.ce_file);
1736 ct->c_cefile.ce_file = NULL;
1737 ct->c_cefile.ce_unlink = 0;
1738 }
1739
1740 if (c_type == CT_TEXT) {
1741 output_subtype = TEXT_PLAIN;
1742 } else {
1743 /* Set subtype to 0, which is always an UNKNOWN subtype. */
1744 output_subtype = 0;
1745 }
1746
1747 output_encoding = content_encoding (ct, &reason);
1748 if (status == OK &&
1749 set_ct_type (ct, c_type, output_subtype, output_encoding) == OK) {
1750 ct->c_cefile.ce_file = file;
1751 ct->c_cefile.ce_unlink = 1;
1752 } else {
1753 ct->c_cefile.ce_unlink = 0;
1754 status = NOTOK;
1755 }
1756
1757 return status;
1758 }
1759
1760
1761 /*
1762 * Fill in a multipart/alternative part.
1763 */
1764 static CT
1765 build_multipart_alt (CT first_alt, CT new_part, int type, int subtype)
1766 {
1767 char *boundary_prefix = "----=_nmh-multipart";
1768 char *boundary = concat (boundary_prefix, first_alt->c_partno, NULL);
1769 char *boundary_indicator = "; boundary=";
1770 char *typename, *subtypename, *name;
1771 CT ct;
1772 struct part *p;
1773 struct multipart *m;
1774 const struct str2init *ctinit;
1775
1776 NEW0(ct);
1777
1778 /* Set up the multipart/alternative part. These fields of *ct were
1779 initialized to 0 by mh_xcalloc():
1780 c_fp, c_unlink, c_begin, c_end,
1781 c_vrsn, c_ctline, c_celine,
1782 c_id, c_descr, c_dispo, c_partno,
1783 c_ctinfo.ci_comment, c_ctinfo.ci_magic,
1784 c_cefile, c_encoding,
1785 c_digested, c_digest[16], c_ctexbody,
1786 c_ctinitfnx, c_ceopenfnx, c_ceclosefnx, c_cesizefnx,
1787 c_umask, c_rfc934,
1788 c_showproc, c_termproc, c_storeproc, c_storage, c_folder
1789 */
1790
1791 ct->c_file = mh_xstrdup (first_alt->c_file);
1792 ct->c_type = type;
1793 ct->c_subtype = subtype;
1794
1795 ctinit = get_ct_init (ct->c_type);
1796
1797 typename = ct_type_str (type);
1798 subtypename = ct_subtype_str (type, subtype);
1799
1800 {
1801 int serial = 0;
1802 int found_boundary = 1;
1803
1804 while (found_boundary && serial < 1000000) {
1805 found_boundary = 0;
1806
1807 /* Ensure that the boundary doesn't appear in the decoded
1808 content. */
1809 if (new_part->c_cefile.ce_file) {
1810 if ((found_boundary =
1811 boundary_in_content (&new_part->c_cefile.ce_fp,
1812 new_part->c_cefile.ce_file,
1813 boundary)) == NOTOK) {
1814 goto return_null;
1815 }
1816 }
1817
1818 /* Ensure that the boundary doesn't appear in the encoded
1819 content. */
1820 if (! found_boundary && new_part->c_file) {
1821 if ((found_boundary =
1822 boundary_in_content (&new_part->c_fp,
1823 new_part->c_file,
1824 boundary)) == NOTOK) {
1825 goto return_null;
1826 }
1827 }
1828
1829 if (found_boundary) {
1830 /* Try a slightly different boundary. */
1831 char buffer2[16];
1832
1833 free (boundary);
1834 ++serial;
1835 snprintf (buffer2, sizeof buffer2, "%d", serial);
1836 boundary =
1837 concat (boundary_prefix,
1838 FENDNULL(first_alt->c_partno),
1839 "-", buffer2, NULL);
1840 }
1841 }
1842
1843 if (found_boundary) {
1844 inform("giving up trying to find a unique boundary");
1845 goto return_null;
1846 }
1847 }
1848
1849 name = concat (" ", typename, "/", subtypename, boundary_indicator, "\"",
1850 boundary, "\"", NULL);
1851
1852 /* Load c_first_hf and c_last_hf. */
1853 transfer_noncontent_headers (first_alt, ct);
1854 add_header (ct, mh_xstrdup (TYPE_FIELD), concat (name, "\n", NULL));
1855 free (name);
1856
1857 /* Load c_partno. */
1858 if (first_alt->c_partno) {
1859 ct->c_partno = mh_xstrdup (first_alt->c_partno);
1860 free (first_alt->c_partno);
1861 first_alt->c_partno = concat (ct->c_partno, ".1", NULL);
1862 new_part->c_partno = concat (ct->c_partno, ".2", NULL);
1863 } else {
1864 first_alt->c_partno = mh_xstrdup ("1");
1865 new_part->c_partno = mh_xstrdup ("2");
1866 }
1867
1868 if (ctinit) {
1869 ct->c_ctinfo.ci_type = mh_xstrdup (typename);
1870 ct->c_ctinfo.ci_subtype = mh_xstrdup (subtypename);
1871 }
1872
1873 add_param(&ct->c_ctinfo.ci_first_pm, &ct->c_ctinfo.ci_last_pm,
1874 "boundary", boundary, 0);
1875
1876 NEW(p);
1877 NEW(p->mp_next);
1878 p->mp_next->mp_next = NULL;
1879 p->mp_next->mp_part = first_alt;
1880
1881 NEW0(m);
1882 m->mp_start = concat (boundary, "\n", NULL);
1883 m->mp_stop = concat (boundary, "--\n", NULL);
1884 m->mp_parts = p;
1885 ct->c_ctparams = m;
1886
1887 free (boundary);
1888
1889 return ct;
1890
1891 return_null:
1892 free_content(ct);
1893 free(boundary);
1894 return NULL;
1895 }
1896
1897
1898 /*
1899 * Check that the boundary does not appear in the content.
1900 */
1901 static int
1902 boundary_in_content (FILE **fp, char *file, const char *boundary)
1903 {
1904 char buffer[NMH_BUFSIZ];
1905 size_t bytes_read;
1906 bool found_boundary = false;
1907
1908 /* free_content() will close *fp if we fopen it here. */
1909 if (! *fp && (*fp = fopen (file, "r")) == NULL) {
1910 advise (file, "unable to open %s for reading", file);
1911 return NOTOK;
1912 }
1913
1914 fseeko (*fp, 0L, SEEK_SET);
1915 while ((bytes_read = fread (buffer, 1, sizeof buffer, *fp)) > 0) {
1916 if (find_str (buffer, bytes_read, boundary)) {
1917 found_boundary = true;
1918 break;
1919 }
1920 }
1921
1922 return found_boundary;
1923 }
1924
1925
1926 /*
1927 * Remove all non-Content headers.
1928 */
1929 static void
1930 transfer_noncontent_headers (CT old, CT new)
1931 {
1932 HF hp, hp_prev;
1933
1934 hp_prev = hp = old->c_first_hf;
1935 while (hp) {
1936 HF next = hp->next;
1937
1938 if (strncasecmp (XXX_FIELD_PRF, hp->name, LEN(XXX_FIELD_PRF))) {
1939 if (hp == old->c_last_hf) {
1940 if (hp == old->c_first_hf) {
1941 old->c_last_hf = old->c_first_hf = NULL;
1942 } else {
1943 hp_prev->next = NULL;
1944 old->c_last_hf = hp_prev;
1945 }
1946 } else {
1947 if (hp == old->c_first_hf) {
1948 old->c_first_hf = next;
1949 } else {
1950 hp_prev->next = next;
1951 }
1952 }
1953
1954 /* Put node hp in the new CT. */
1955 if (new->c_first_hf == NULL) {
1956 new->c_first_hf = hp;
1957 } else {
1958 new->c_last_hf->next = hp;
1959 }
1960 new->c_last_hf = hp;
1961 } else {
1962 /* A Content- header, leave in old. */
1963 hp_prev = hp;
1964 }
1965
1966 hp = next;
1967 }
1968 }
1969
1970
1971 /*
1972 * Set content type.
1973 */
1974 static int
1975 set_ct_type (CT ct, int type, int subtype, int encoding)
1976 {
1977 char *typename = ct_type_str (type);
1978 char *subtypename = ct_subtype_str (type, subtype);
1979 /* E.g, " text/plain" */
1980 char *type_subtypename = concat (" ", typename, "/", subtypename, NULL);
1981 /* E.g, " text/plain\n" */
1982 char *name_plus_nl = concat (type_subtypename, "\n", NULL);
1983 bool found_content_type = false;
1984 HF hf;
1985 const char *cp = NULL;
1986 char *ctline;
1987 int status;
1988
1989 /* Update/add Content-Type header field. */
1990 for (hf = ct->c_first_hf; hf; hf = hf->next) {
1991 if (! strcasecmp (TYPE_FIELD, hf->name)) {
1992 found_content_type = true;
1993 free (hf->value);
1994 hf->value = (cp = strchr (ct->c_ctline, ';'))
1995 ? concat (type_subtypename, cp, "\n", NULL)
1996 : mh_xstrdup (name_plus_nl);
1997 }
1998 }
1999 if (! found_content_type) {
2000 add_header (ct, mh_xstrdup (TYPE_FIELD),
2001 (cp = strchr (ct->c_ctline, ';'))
2002 ? concat (type_subtypename, cp, "\n", NULL)
2003 : mh_xstrdup (name_plus_nl));
2004 }
2005
2006 /* Some of these might not be used, but set them anyway. */
2007 ctline = cp
2008 ? concat (type_subtypename, cp, NULL)
2009 : concat (type_subtypename, NULL);
2010 free (ct->c_ctline);
2011 ct->c_ctline = ctline;
2012 /* Leave other ctinfo members as they were. */
2013 free (ct->c_ctinfo.ci_type);
2014 ct->c_ctinfo.ci_type = mh_xstrdup (typename);
2015 free (ct->c_ctinfo.ci_subtype);
2016 ct->c_ctinfo.ci_subtype = mh_xstrdup (subtypename);
2017 ct->c_type = type;
2018 ct->c_subtype = subtype;
2019
2020 free (name_plus_nl);
2021 free (type_subtypename);
2022
2023 status = set_ce (ct, encoding);
2024
2025 return status;
2026 }
2027
2028
2029 /*
2030 * It's not necessary to update the charset parameter of a Content-Type
2031 * header for a text part. According to RFC 2045 Sec. 6.4, the body
2032 * (content) was originally in the specified charset, "and will be in
2033 * that character set again after decoding."
2034 */
2035 static int
2036 decode_text_parts (CT ct, int encoding, const char *decodetypes,
2037 int *message_mods)
2038 {
2039 int status = OK;
2040 int lf_line_endings = 0;
2041
2042 switch (ct->c_type) {
2043 case CT_MULTIPART: {
2044 struct multipart *m = (struct multipart *) ct->c_ctparams;
2045 struct part *part;
2046
2047 /* Should check to see if the body for this part is encoded?
2048 For now, it gets passed along as-is by InitMultiPart(). */
2049 for (part = m->mp_parts; status == OK && part; part = part->mp_next) {
2050 status = decode_text_parts (part->mp_part, encoding, decodetypes,
2051 message_mods);
2052 }
2053 break;
2054 }
2055
2056 case CT_MESSAGE:
2057 if (ct->c_subtype == MESSAGE_EXTERNAL) {
2058 struct exbody *e = (struct exbody *) ct->c_ctparams;
2059
2060 status = decode_text_parts (e->eb_content, encoding, decodetypes,
2061 message_mods);
2062 }
2063 break;
2064
2065 default:
2066 if (! should_decode(decodetypes, ct->c_ctinfo.ci_type, ct->c_ctinfo.ci_subtype)) {
2067 break;
2068 }
2069
2070 lf_line_endings =
2071 ct->c_ctparams && ((struct text *) ct->c_ctparams)->lf_line_endings;
2072
2073 switch (ct->c_encoding) {
2074 case CE_BASE64:
2075 case CE_QUOTED: {
2076 int ct_encoding;
2077
2078 if (decode_part (ct) == OK && ct->c_cefile.ce_file) {
2079 const char *reason = NULL;
2080
2081 if ((ct_encoding = content_encoding (ct, &reason)) == CE_BINARY
2082 && encoding != CE_BINARY) {
2083 /* The decoding isn't acceptable so discard it.
2084 Leave status as OK to allow other transformations. */
2085 if (verbosw) {
2086 report (NULL, ct->c_partno, ct->c_file,
2087 "will not decode%s because it is binary (%s)",
2088 ct->c_partno ? ""
2089 : (FENDNULL(ct->c_ctline)),
2090 reason);
2091 }
2092 (void) m_unlink (ct->c_cefile.ce_file);
2093 free (ct->c_cefile.ce_file);
2094 ct->c_cefile.ce_file = NULL;
2095 } else if (ct->c_encoding == CE_QUOTED &&
2096 ct_encoding == CE_8BIT && encoding == CE_7BIT) {
2097 /* The decoding isn't acceptable so discard it.
2098 Leave status as OK to allow other transformations. */
2099 if (verbosw) {
2100 report (NULL, ct->c_partno, ct->c_file,
2101 "will not decode%s because it is 8bit",
2102 ct->c_partno ? ""
2103 : (FENDNULL(ct->c_ctline)));
2104 }
2105 (void) m_unlink (ct->c_cefile.ce_file);
2106 free (ct->c_cefile.ce_file);
2107 ct->c_cefile.ce_file = NULL;
2108 } else {
2109 int enc;
2110
2111 if (ct_encoding == CE_BINARY) {
2112 enc = CE_BINARY;
2113 } else if (ct_encoding == CE_8BIT && encoding == CE_7BIT) {
2114 enc = CE_QUOTED;
2115 } else {
2116 enc = ct_encoding;
2117 }
2118 if (set_ce (ct, enc) == OK) {
2119 ++*message_mods;
2120 if (verbosw) {
2121 report (NULL, ct->c_partno, ct->c_file, "decode%s",
2122 FENDNULL(ct->c_ctline));
2123 }
2124 if (lf_line_endings) {
2125 strip_crs (ct, message_mods);
2126 }
2127 } else {
2128 status = NOTOK;
2129 }
2130 }
2131 } else {
2132 status = NOTOK;
2133 }
2134 break;
2135 }
2136 case CE_8BIT:
2137 case CE_7BIT:
2138 if (lf_line_endings) {
2139 strip_crs (ct, message_mods);
2140 }
2141 break;
2142 default:
2143 break;
2144 }
2145
2146 break;
2147 }
2148
2149 return status;
2150 }
2151
2152
2153 /*
2154 * Determine if the part with type[/subtype] should be decoded, according to
2155 * decodetypes (which came from the -decodetypes switch).
2156 */
2157 static int
2158 should_decode(const char *decodetypes, const char *type, const char *subtype)
2159 {
2160 /* Quick search for matching type[/subtype] in decodetypes: bracket
2161 decodetypes with commas, then search for ,type, and ,type/subtype, in
2162 it. */
2163
2164 bool found_match = false;
2165 char *delimited_decodetypes = concat(",", decodetypes, ",", NULL);
2166 char *delimited_type = concat(",", type, ",", NULL);
2167
2168 if (nmh_strcasestr(delimited_decodetypes, delimited_type)) {
2169 found_match = true;
2170 } else if (subtype != NULL) {
2171 char *delimited_type_subtype =
2172 concat(",", type, "/", subtype, ",", NULL);
2173
2174 if (nmh_strcasestr(delimited_decodetypes, delimited_type_subtype)) {
2175 found_match = true;
2176 }
2177 free(delimited_type_subtype);
2178 }
2179
2180 free(delimited_type);
2181 free(delimited_decodetypes);
2182
2183 return found_match;
2184 }
2185
2186
2187 /*
2188 * See if the decoded content is 7bit, 8bit, or binary. It's binary
2189 * if it has any NUL characters, a CR not followed by a LF, or lines
2190 * greater than 998 characters in length. If binary, reason is set
2191 * to a string explaining why.
2192 */
2193 static int
2194 content_encoding (CT ct, const char **reason)
2195 {
2196 CE ce = &ct->c_cefile;
2197 int encoding = CE_7BIT;
2198
2199 if (ce->ce_file) {
2200 size_t line_len = 0;
2201 char buffer[NMH_BUFSIZ];
2202 size_t inbytes;
2203
2204 if (! ce->ce_fp && (ce->ce_fp = fopen (ce->ce_file, "r")) == NULL) {
2205 advise (ce->ce_file, "unable to open for reading");
2206 return CE_UNKNOWN;
2207 }
2208
2209 fseeko (ce->ce_fp, 0L, SEEK_SET);
2210 while (encoding != CE_BINARY &&
2211 (inbytes = fread (buffer, 1, sizeof buffer, ce->ce_fp)) > 0) {
2212 char *cp;
2213 size_t i;
2214 int last_char_was_cr = 0;
2215
2216 for (i = 0, cp = buffer; i < inbytes; ++i, ++cp) {
2217 if (*cp == '\0' || ++line_len > 998 ||
2218 (*cp != '\n' && last_char_was_cr)) {
2219 encoding = CE_BINARY;
2220 if (*cp == '\0') {
2221 *reason = "null character";
2222 } else if (line_len > 998) {
2223 *reason = "line length > 998";
2224 } else if (*cp != '\n' && last_char_was_cr) {
2225 *reason = "CR not followed by LF";
2226 } else {
2227 /* Should not reach this. */
2228 *reason = "";
2229 }
2230 break;
2231 }
2232 if (*cp == '\n') {
2233 line_len = 0;
2234 } else if (! isascii ((unsigned char) *cp)) {
2235 encoding = CE_8BIT;
2236 }
2237
2238 last_char_was_cr = *cp == '\r';
2239 }
2240 }
2241
2242 fclose (ce->ce_fp);
2243 ce->ce_fp = NULL;
2244 } /* else should never happen */
2245
2246 return encoding;
2247 }
2248
2249
2250 /*
2251 * Strip carriage returns from content.
2252 */
2253 static int
2254 strip_crs (CT ct, int *message_mods)
2255 {
2256 char *charset = content_charset (ct);
2257 int status = OK;
2258
2259 /* Only strip carriage returns if content is ASCII or another
2260 charset that has the same readily recognizable CR followed by a
2261 LF. We can include UTF-8 here because if the high-order bit of
2262 a UTF-8 byte is 0, then it must be a single-byte ASCII
2263 character. */
2264 if (! strcasecmp (charset, "US-ASCII") ||
2265 ! strcasecmp (charset, "UTF-8") ||
2266 ! strncasecmp (charset, "ISO-8859-", 9) ||
2267 ! strncasecmp (charset, "WINDOWS-12", 10)) {
2268 char **file = NULL;
2269 FILE **fp = NULL;
2270 size_t begin;
2271 size_t end;
2272 bool has_crs = false;
2273 bool opened_input_file = false;
2274
2275 if (ct->c_cefile.ce_file) {
2276 file = &ct->c_cefile.ce_file;
2277 fp = &ct->c_cefile.ce_fp;
2278 begin = end = 0;
2279 } else if (ct->c_file) {
2280 file = &ct->c_file;
2281 fp = &ct->c_fp;
2282 begin = (size_t) ct->c_begin;
2283 end = (size_t) ct->c_end;
2284 } /* else don't know where the content is */
2285
2286 if (file && *file && fp) {
2287 if (! *fp) {
2288 if ((*fp = fopen (*file, "r")) == NULL) {
2289 advise (*file, "unable to open for reading");
2290 status = NOTOK;
2291 } else {
2292 opened_input_file = true;
2293 }
2294 }
2295 }
2296
2297 if (fp && *fp) {
2298 char buffer[NMH_BUFSIZ];
2299 size_t bytes_read;
2300 size_t bytes_to_read =
2301 end > 0 && end > begin ? end - begin : sizeof buffer;
2302
2303 fseeko (*fp, begin, SEEK_SET);
2304 while ((bytes_read = fread (buffer, 1,
2305 min (bytes_to_read, sizeof buffer),
2306 *fp)) > 0) {
2307 /* Look for CR followed by a LF. This is supposed to
2308 be text so there should be LF's. If not, don't
2309 modify the content. */
2310 char *cp;
2311 size_t i;
2312 bool last_char_was_cr = false;
2313
2314 if (end > 0) { bytes_to_read -= bytes_read; }
2315
2316 for (i = 0, cp = buffer; i < bytes_read; ++i, ++cp) {
2317 if (*cp == '\n' && last_char_was_cr) {
2318 has_crs = true;
2319 break;
2320 }
2321
2322 last_char_was_cr = *cp == '\r';
2323 }
2324 }
2325
2326 if (has_crs) {
2327 int fd;
2328 char *stripped_content_file;
2329 char *tempfile = m_mktemp2 (NULL, invo_name, &fd, NULL);
2330
2331 if (tempfile == NULL) {
2332 die("unable to create temporary file in %s",
2333 get_temp_dir());
2334 }
2335 stripped_content_file = mh_xstrdup (tempfile);
2336
2337 /* Strip each CR before a LF from the content. */
2338 fseeko (*fp, begin, SEEK_SET);
2339 while ((bytes_read = fread (buffer, 1, sizeof buffer, *fp)) >
2340 0) {
2341 char *cp;
2342 size_t i;
2343 bool last_char_was_cr = false;
2344
2345 for (i = 0, cp = buffer; i < bytes_read; ++i, ++cp) {
2346 if (*cp == '\r') {
2347 last_char_was_cr = true;
2348 } else if (last_char_was_cr) {
2349 if (*cp != '\n') {
2350 if (write (fd, "\r", 1) < 0) {
2351 advise (tempfile, "CR write");
2352 }
2353 }
2354 if (write (fd, cp, 1) < 0) {
2355 advise (tempfile, "write");
2356 }
2357 last_char_was_cr = false;
2358 } else {
2359 if (write (fd, cp, 1) < 0) {
2360 advise (tempfile, "write");
2361 }
2362 last_char_was_cr = false;
2363 }
2364 }
2365 }
2366
2367 if (close (fd)) {
2368 inform("unable to write temporary file %s, continuing...",
2369 stripped_content_file);
2370 (void) m_unlink (stripped_content_file);
2371 free(stripped_content_file);
2372 status = NOTOK;
2373 } else {
2374 /* Replace the decoded file with the converted one. */
2375 if (ct->c_cefile.ce_file && ct->c_cefile.ce_unlink)
2376 (void) m_unlink (ct->c_cefile.ce_file);
2377
2378 free(ct->c_cefile.ce_file);
2379 ct->c_cefile.ce_file = stripped_content_file;
2380 ct->c_cefile.ce_unlink = 1;
2381
2382 ++*message_mods;
2383 if (verbosw) {
2384 report (NULL, ct->c_partno,
2385 begin == 0 && end == 0 ? "" : *file,
2386 "stripped CRs");
2387 }
2388 }
2389 }
2390
2391 if (opened_input_file) {
2392 fclose (*fp);
2393 *fp = NULL;
2394 }
2395 }
2396 }
2397
2398 free (charset);
2399
2400 return status;
2401 }
2402
2403
2404 /*
2405 * Add/update, if necessary, the message C-T-E, based on the least restrictive
2406 * of the part C-T-E's.
2407 */
2408 static void
2409 update_cte (CT ct)
2410 {
2411 const int least_restrictive_enc = least_restrictive_encoding (ct);
2412
2413 if (least_restrictive_enc != CE_UNKNOWN &&
2414 least_restrictive_enc != CE_7BIT) {
2415 char *cte = concat (" ", ce_str (least_restrictive_enc), "\n", NULL);
2416 HF hf;
2417 bool found_cte = false;
2418
2419 /* Update/add Content-Transfer-Encoding header field. */
2420 for (hf = ct->c_first_hf; hf; hf = hf->next) {
2421 if (! strcasecmp (ENCODING_FIELD, hf->name)) {
2422 found_cte = true;
2423 free (hf->value);
2424 hf->value = cte;
2425 }
2426 }
2427 if (! found_cte) {
2428 add_header (ct, mh_xstrdup (ENCODING_FIELD), cte);
2429 }
2430 }
2431 }
2432
2433
2434 /*
2435 * Find the least restrictive encoding (7bit, 8bit, binary) of the parts
2436 * within a message.
2437 */
2438 static int
2439 least_restrictive_encoding (CT ct)
2440 {
2441 int encoding = CE_UNKNOWN;
2442
2443 switch (ct->c_type) {
2444 case CT_MULTIPART: {
2445 struct multipart *m = (struct multipart *) ct->c_ctparams;
2446 struct part *part;
2447
2448 for (part = m->mp_parts; part; part = part->mp_next) {
2449 const int part_encoding =
2450 least_restrictive_encoding (part->mp_part);
2451
2452 if (less_restrictive (encoding, part_encoding)) {
2453 encoding = part_encoding;
2454 }
2455 }
2456 break;
2457 }
2458
2459 case CT_MESSAGE:
2460 if (ct->c_subtype == MESSAGE_EXTERNAL) {
2461 struct exbody *e = (struct exbody *) ct->c_ctparams;
2462 const int part_encoding =
2463 least_restrictive_encoding (e->eb_content);
2464
2465 if (less_restrictive (encoding, part_encoding)) {
2466 encoding = part_encoding;
2467 }
2468 }
2469 break;
2470
2471 default: {
2472 if (less_restrictive (encoding, ct->c_encoding)) {
2473 encoding = ct->c_encoding;
2474 }
2475 }}
2476
2477 return encoding;
2478 }
2479
2480
2481 /*
2482 * Return whether the second encoding is less restrictive than the first, where
2483 * "less restrictive" is in the sense used by RFC 2045 Secs. 6.1 and 6.4. So,
2484 * CE_BINARY is less restrictive than CE_8BIT and
2485 * CE_8BIT is less restrictive than CE_7BIT.
2486 */
2487 static int
2488 less_restrictive (int encoding, int second_encoding)
2489 {
2490 switch (second_encoding) {
2491 case CE_BINARY:
2492 return encoding != CE_BINARY;
2493 case CE_8BIT:
2494 return encoding != CE_BINARY && encoding != CE_8BIT;
2495 case CE_7BIT:
2496 return encoding != CE_BINARY && encoding != CE_8BIT &&
2497 encoding != CE_7BIT;
2498 default :
2499 return 0;
2500 }
2501 }
2502
2503
2504 /*
2505 * Convert character set of each part.
2506 */
2507 static int
2508 convert_charsets (CT ct, char *dest_charset, int *message_mods)
2509 {
2510 int status = OK;
2511
2512 switch (ct->c_type) {
2513 case CT_TEXT:
2514 if (ct->c_subtype == TEXT_PLAIN) {
2515 status = convert_charset (ct, dest_charset, message_mods);
2516 if (status == OK) {
2517 if (verbosw) {
2518 char *ct_charset = content_charset (ct);
2519
2520 report (NULL, ct->c_partno, ct->c_file,
2521 "convert %s to %s", ct_charset, dest_charset);
2522 free (ct_charset);
2523 }
2524 } else {
2525 char *ct_charset = content_charset (ct);
2526
2527 report ("iconv", ct->c_partno, ct->c_file,
2528 "failed to convert %s to %s", ct_charset, dest_charset);
2529 free (ct_charset);
2530 }
2531 }
2532 break;
2533
2534 case CT_MULTIPART: {
2535 struct multipart *m = (struct multipart *) ct->c_ctparams;
2536 struct part *part;
2537
2538 /* Should check to see if the body for this part is encoded?
2539 For now, it gets passed along as-is by InitMultiPart(). */
2540 for (part = m->mp_parts; status == OK && part; part = part->mp_next) {
2541 status =
2542 convert_charsets (part->mp_part, dest_charset, message_mods);
2543 }
2544 break;
2545 }
2546
2547 case CT_MESSAGE:
2548 if (ct->c_subtype == MESSAGE_EXTERNAL) {
2549 struct exbody *e = (struct exbody *) ct->c_ctparams;
2550
2551 status =
2552 convert_charsets (e->eb_content, dest_charset, message_mods);
2553 }
2554 break;
2555
2556 default:
2557 break;
2558 }
2559
2560 return status;
2561 }
2562
2563
2564 /*
2565 * Fix various problems that aren't handled elsewhere. These
2566 * are fixed unconditionally: there are no switches to disable
2567 * them. Currently, "problems" are these:
2568 * 1) remove extraneous semicolon at the end of a header parameter list
2569 * 2) replace RFC 2047 encoding with RFC 2231 encoding of name and
2570 * filename parameters in Content-Type and Content-Disposition
2571 * headers, respectively.
2572 */
2573 static int
2574 fix_always (CT ct, int *message_mods)
2575 {
2576 int status = OK;
2577
2578 switch (ct->c_type) {
2579 case CT_MULTIPART: {
2580 struct multipart *m = (struct multipart *) ct->c_ctparams;
2581 struct part *part;
2582
2583 for (part = m->mp_parts; status == OK && part; part = part->mp_next) {
2584 status = fix_always (part->mp_part, message_mods);
2585 }
2586 break;
2587 }
2588
2589 case CT_MESSAGE:
2590 if (ct->c_subtype == MESSAGE_EXTERNAL) {
2591 struct exbody *e = (struct exbody *) ct->c_ctparams;
2592
2593 status = fix_always (e->eb_content, message_mods);
2594 }
2595 break;
2596
2597 default: {
2598 HF hf;
2599
2600 if (ct->c_first_hf) {
2601 fix_filename_encoding (ct);
2602 }
2603
2604 for (hf = ct->c_first_hf; hf; hf = hf->next) {
2605 size_t len = strlen (hf->value);
2606
2607 if (strcasecmp (hf->name, TYPE_FIELD) != 0 &&
2608 strcasecmp (hf->name, DISPO_FIELD) != 0) {
2609 /* Only do this for Content-Type and
2610 Content-Disposition fields because those are the
2611 only headers that parse_mime() warns about. */
2612 continue;
2613 }
2614
2615 /* whitespace following a trailing ';' will be nuked as well */
2616 if (hf->value[len - 1] == '\n') {
2617 while (isspace((unsigned char)(hf->value[len - 2]))) {
2618 if (len-- == 0) { break; }
2619 }
2620 }
2621
2622 if (hf->value[len - 2] == ';') {
2623 /* Remove trailing ';' from parameter value. */
2624 hf->value[len - 2] = '\n';
2625 hf->value[len - 1] = '\0';
2626
2627 /* Also, if Content-Type parameter, remove trailing ';'
2628 from ct->c_ctline. This probably isn't necessary
2629 but can't hurt. */
2630 if (strcasecmp(hf->name, TYPE_FIELD) == 0 && ct->c_ctline) {
2631 size_t l = strlen(ct->c_ctline) - 1;
2632 while (isspace((unsigned char)(ct->c_ctline[l])) ||
2633 ct->c_ctline[l] == ';') {
2634 ct->c_ctline[l--] = '\0';
2635 if (l == 0) { break; }
2636 }
2637 }
2638
2639 ++*message_mods;
2640 if (verbosw) {
2641 report (NULL, ct->c_partno, ct->c_file,
2642 "remove trailing ; from %s parameter value",
2643 hf->name);
2644 }
2645 }
2646 }
2647 }}
2648
2649 return status;
2650 }
2651
2652
2653 /*
2654 * Decodes UTF-8 encoded header values. Similar to fix_filename_param(), but
2655 * does not modify any MIME parameter values.
2656 */
2657 static int
2658 decode_header_field_bodies (CT ct, int *message_mods)
2659 {
2660 int status = OK;
2661
2662 switch (ct->c_type) {
2663 case CT_MULTIPART: {
2664 struct multipart *m = (struct multipart *) ct->c_ctparams;
2665 struct part *part;
2666
2667 for (part = m->mp_parts; status == OK && part; part = part->mp_next) {
2668 status = decode_header_field_bodies (part->mp_part, message_mods);
2669 }
2670 break;
2671 }
2672
2673 case CT_MESSAGE:
2674 if (ct->c_subtype == MESSAGE_EXTERNAL) {
2675 struct exbody *e = (struct exbody *) ct->c_ctparams;
2676
2677 status = decode_header_field_bodies (e->eb_content, message_mods);
2678 }
2679 break;
2680 }
2681
2682 HF hf;
2683
2684 for (hf = ct->c_first_hf; hf; hf = hf->next) {
2685 /* Only decode UTF-8 values. */
2686 if (hf->value && has_suffix(hf->value, "?=\n") &&
2687 (! strncasecmp (hf->value, " =?utf8?", 8) ||
2688 ! strncasecmp (hf->value, " =?utf-8?", 9))) {
2689 /* Looks like an RFC 2047 encoded parameter. */
2690 char decoded[PATH_MAX + 1];
2691
2692 if (decode_rfc2047 (hf->value, decoded, sizeof decoded)) {
2693 const size_t len = strlen(decoded);
2694
2695 /* decode_rfc2047() could truncate if the buffer fills up.
2696 Detect and discard if that happened. */
2697 if (len < sizeof(decoded) - 1 && strcmp(hf->value, decoded)) {
2698 hf->value = mh_xrealloc (hf->value, len + 1);
2699 strncpy (hf->value, decoded, len + 1);
2700 ++*message_mods;
2701 }
2702 } else {
2703 inform("failed to decode %s parameter %s", hf->name, hf->value);
2704 status = NOTOK;
2705 }
2706 }
2707 }
2708
2709 return status;
2710 }
2711
2712
2713 /*
2714 * Factor out common code for loops in fix_filename_encoding().
2715 */
2716 static int
2717 fix_filename_param (char *name, char *value, PM *first_pm, PM *last_pm)
2718 {
2719 bool fixed = false;
2720
2721 if (has_prefix(value, "=?") && has_suffix(value, "?=")) {
2722 /* Looks like an RFC 2047 encoded parameter. */
2723 char decoded[PATH_MAX + 1];
2724
2725 if (decode_rfc2047 (value, decoded, sizeof decoded)) {
2726 /* Encode using RFC 2231. */
2727 replace_param (first_pm, last_pm, name, decoded, 0);
2728 fixed = true;
2729 } else {
2730 inform("failed to decode %s parameter %s", name, value);
2731 }
2732 }
2733
2734 return fixed;
2735 }
2736
2737
2738 /*
2739 * Replace RFC 2047 encoding with RFC 2231 encoding of name and
2740 * filename parameters in Content-Type and Content-Disposition
2741 * headers, respectively.
2742 */
2743 static int
2744 fix_filename_encoding (CT ct)
2745 {
2746 PM pm;
2747 HF hf;
2748 int fixed = 0;
2749
2750 for (pm = ct->c_ctinfo.ci_first_pm; pm; pm = pm->pm_next) {
2751 if (pm->pm_name && pm->pm_value &&
2752 strcasecmp (pm->pm_name, "name") == 0) {
2753 fixed = fix_filename_param (pm->pm_name, pm->pm_value,
2754 &ct->c_ctinfo.ci_first_pm,
2755 &ct->c_ctinfo.ci_last_pm);
2756 }
2757 }
2758
2759 for (pm = ct->c_dispo_first; pm; pm = pm->pm_next) {
2760 if (pm->pm_name && pm->pm_value &&
2761 strcasecmp (pm->pm_name, "filename") == 0) {
2762 fixed = fix_filename_param (pm->pm_name, pm->pm_value,
2763 &ct->c_dispo_first,
2764 &ct->c_dispo_last);
2765 }
2766 }
2767
2768 /* Fix hf values to correspond. */
2769 for (hf = ct->c_first_hf; fixed && hf; hf = hf->next) {
2770 enum { OTHER, TYPE_HEADER, DISPO_HEADER } field = OTHER;
2771
2772 if (strcasecmp (hf->name, TYPE_FIELD) == 0) {
2773 field = TYPE_HEADER;
2774 } else if (strcasecmp (hf->name, DISPO_FIELD) == 0) {
2775 field = DISPO_HEADER;
2776 }
2777
2778 if (field != OTHER) {
2779 const char *const semicolon_loc = strchr (hf->value, ';');
2780
2781 if (semicolon_loc) {
2782 const size_t len =
2783 strlen (hf->name) + 1 + semicolon_loc - hf->value;
2784 const char *const params =
2785 output_params (len,
2786 field == TYPE_HEADER
2787 ? ct->c_ctinfo.ci_first_pm
2788 : ct->c_dispo_first,
2789 NULL, 0);
2790 const char *const new_params = concat (params, "\n", NULL);
2791
2792 replace_substring (&hf->value, semicolon_loc, new_params);
2793 free((void *)new_params); /* Cast away const. Sigh. */
2794 free((void *)params);
2795 } else {
2796 inform("did not find semicolon in %s:%s\n",
2797 hf->name, hf->value);
2798 }
2799 }
2800 }
2801
2802 return OK;
2803 }
2804
2805
2806 /*
2807 * Output content in input file to output file.
2808 */
2809 static int
2810 write_content (CT ct, const char *input_filename, char *outfile, FILE *outfp,
2811 int modify_inplace, int message_mods)
2812 {
2813 int status = OK;
2814
2815 if (modify_inplace) {
2816 if (message_mods > 0) {
2817 if ((status = output_message_fp (ct, outfp, outfile)) == OK) {
2818 char *infile = input_filename
2819 ? mh_xstrdup (input_filename)
2820 : mh_xstrdup (ct->c_file ? ct->c_file : "-");
2821
2822 if (remove_file (infile) == OK) {
2823 if (rename (outfile, infile)) {
2824 /* Rename didn't work, possibly because of an
2825 attempt to rename across filesystems. Try
2826 brute force copy. */
2827 int old = open (outfile, O_RDONLY);
2828 int new =
2829 open (infile, O_WRONLY | O_CREAT, m_gmprot ());
2830 int i = -1;
2831
2832 if (old != -1 && new != -1) {
2833 char buffer[NMH_BUFSIZ];
2834
2835 while ((i = read (old, buffer, sizeof buffer)) >
2836 0) {
2837 if (write (new, buffer, i) != i) {
2838 i = -1;
2839 break;
2840 }
2841 }
2842 }
2843 if (new != -1) { close (new); }
2844 if (old != -1) { close (old); }
2845 (void) m_unlink (outfile);
2846
2847 if (i < 0) {
2848 /* The -file argument processing used path() to
2849 expand filename to absolute path. */
2850 int file = ct->c_file && ct->c_file[0] == '/';
2851
2852 inform("unable to rename %s %s to %s, continuing...",
2853 file ? "file" : "message", outfile,
2854 infile);
2855 status = NOTOK;
2856 }
2857 }
2858 } else {
2859 inform("unable to remove input file %s, "
2860 "not modifying it, continuing...", infile);
2861 (void) m_unlink (outfile);
2862 status = NOTOK;
2863 }
2864
2865 free (infile);
2866 } else {
2867 status = NOTOK;
2868 }
2869 } else {
2870 /* No modifications and didn't need the tmp outfile. */
2871 (void) m_unlink (outfile);
2872 }
2873 } else {
2874 /* Output is going to some file. Produce it whether or not
2875 there were modifications. */
2876 status = output_message_fp (ct, outfp, outfile);
2877 }
2878
2879 flush_errors ();
2880 return status;
2881 }
2882
2883
2884 /*
2885 * parse_mime() does not set lf_line_endings in struct text, so use this
2886 * function to do it. It touches the parts the decodetypes identifies.
2887 */
2888 static void
2889 set_text_ctparams(CT ct, char *decodetypes, int lf_line_endings)
2890 {
2891 switch (ct->c_type) {
2892 case CT_MULTIPART: {
2893 struct multipart *m = (struct multipart *) ct->c_ctparams;
2894 struct part *part;
2895
2896 for (part = m->mp_parts; part; part = part->mp_next) {
2897 set_text_ctparams(part->mp_part, decodetypes, lf_line_endings);
2898 }
2899 break;
2900 }
2901
2902 case CT_MESSAGE:
2903 if (ct->c_subtype == MESSAGE_EXTERNAL) {
2904 struct exbody *e = (struct exbody *) ct->c_ctparams;
2905
2906 set_text_ctparams(e->eb_content, decodetypes, lf_line_endings);
2907 }
2908 break;
2909
2910 default:
2911 if (should_decode(decodetypes, ct->c_ctinfo.ci_type, ct->c_ctinfo.ci_subtype)) {
2912 if (ct->c_ctparams == NULL) {
2913 ct->c_ctparams = mh_xcalloc(1, sizeof (struct text));
2914 }
2915 ((struct text *) ct->c_ctparams)->lf_line_endings = lf_line_endings;
2916 }
2917 }
2918 }
2919
2920
2921 /*
2922 * If "rmmproc" is defined, call that to remove the file. Otherwise,
2923 * use the standard MH backup file.
2924 */
2925 static int
2926 remove_file (const char *file)
2927 {
2928 if (rmmproc) {
2929 char *rmm_command = concat (rmmproc, " ", file, NULL);
2930 int status = system (rmm_command);
2931
2932 free (rmm_command);
2933 return WIFEXITED (status) ? WEXITSTATUS (status) : NOTOK;
2934 }
2935 /* This is OK for a non-message file, it still uses the
2936 BACKUP_PREFIX form. The backup file will be in the same
2937 directory as file. */
2938 return rename (file, m_backup (file));
2939 }
2940
2941
2942 /*
2943 * Output formatted message to user.
2944 */
2945 static void
2946 report (char *what, char *partno, char *filename, char *message, ...)
2947 {
2948 va_list args;
2949 char *fmt;
2950
2951 if (verbosw) {
2952 va_start (args, message);
2953 fmt = concat (filename, partno ? " part " : ", ",
2954 FENDNULL(partno), partno ? ", " : "", message, NULL);
2955
2956 advertise (what, NULL, fmt, args);
2957
2958 free (fmt);
2959 va_end (args);
2960 }
2961 }
2962
2963
2964 static void
2965 pipeser (int i)
2966 {
2967 if (i == SIGQUIT) {
2968 fflush (stdout);
2969 fprintf (stderr, "\n");
2970 fflush (stderr);
2971 }
2972
2973 done (1);
2974 /* NOTREACHED */
2975 }