]> diplodocus.org Git - nmh/blob - uip/mhfixmsg.c
add rmf(1) and folder(1) to one another's SEE ALSO sections
[nmh] / uip / mhfixmsg.c
1 /*
2 * mhfixmsg.c -- rewrite a message with various tranformations
3 *
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.
7 */
8
9 #include <h/mh.h>
10 #include <h/mime.h>
11 #include <h/mhparse.h>
12 #include <h/utils.h>
13 #include <h/signals.h>
14 #include <fcntl.h>
15
16 #define MHFIXMSG_SWITCHES \
17 X("decodetext 8bit|7bit", 0, DECODETEXTSW) \
18 X("nodecodetext", 0, NDECODETEXTSW) \
19 X("textcharset", 0, TEXTCHARSETSW) \
20 X("notextcharset", 0, NTEXTCHARSETSW) \
21 X("reformat", 0, REFORMATSW) \
22 X("noreformat", 0, NREFORMATSW) \
23 X("replacetextplain", 0, REPLACETEXTPLAINSW) \
24 X("noreplacetextplain", 0, NREPLACETEXTPLAINSW) \
25 X("fixboundary", 0, FIXBOUNDARYSW) \
26 X("nofixboundary", 0, NFIXBOUNDARYSW) \
27 X("fixcte", 0, FIXCTESW) \
28 X("nofixcte", 0, NFIXCTESW) \
29 X("file file", 0, FILESW) \
30 X("outfile file", 0, OUTFILESW) \
31 X("rmmproc program", 0, RPROCSW) \
32 X("normmproc", 0, NRPRCSW) \
33 X("verbose", 0, VERBSW) \
34 X("noverbose", 0, NVERBSW) \
35 X("version", 0, VERSIONSW) \
36 X("help", 0, HELPSW) \
37
38 #define X(sw, minchars, id) id,
39 DEFINE_SWITCH_ENUM(MHFIXMSG);
40 #undef X
41
42 #define X(sw, minchars, id) { sw, minchars, id },
43 DEFINE_SWITCH_ARRAY(MHFIXMSG, switches);
44 #undef X
45
46
47 int verbosw;
48 int debugsw; /* Needed by mhparse.c. */
49
50 #define quitser pipeser
51
52 /* mhparse.c */
53 extern int skip_mp_cte_check; /* flag to InitMultiPart */
54 extern int suppress_bogus_mp_content_warning; /* flag to InitMultiPart */
55 extern int bogus_mp_content; /* flag from InitMultiPart */
56 CT parse_mime (char *);
57 void reverse_parts (CT);
58
59 /* mhoutsbr.c */
60 int output_message (CT, char *);
61
62 /* mhshowsbr.c */
63 int show_content_aux (CT, int, char *, char *);
64
65 /* mhmisc.c */
66 void flush_errors (void);
67
68 /* mhfree.c */
69 extern CT *cts;
70 void freects_done (int) NORETURN;
71
72 /*
73 * static prototypes
74 */
75 typedef struct fix_transformations {
76 int fixboundary;
77 int fixcte;
78 int reformat;
79 int replacetextplain;
80 int decodetext;
81 char *textcharset;
82 } fix_transformations;
83
84 int mhfixmsgsbr (CT *, const fix_transformations *, char *);
85 static void reverse_alternative_parts (CT);
86 static int fix_boundary (CT *, int *);
87 static int get_multipart_boundary (CT, char **);
88 static int replace_boundary (CT, char *, char *);
89 static int fix_multipart_cte (CT, int *);
90 static int set_ce (CT, int);
91 static int ensure_text_plain (CT *, CT, int *, int);
92 static CT build_text_plain_part (CT);
93 static CT divide_part (CT);
94 static void copy_ctinfo (CI, CI);
95 static int decode_part (CT);
96 static int reformat_part (CT, char *, char *, char *, int);
97 static int charset_encoding (CT);
98 static CT build_multipart_alt (CT, CT, int, int);
99 static int boundary_in_content (FILE **, char *, const char *);
100 static void transfer_noncontent_headers (CT, CT);
101 static int set_ct_type (CT, int type, int subtype, int encoding);
102 static int decode_text_parts (CT, int, int *);
103 static int content_encoding (CT, const char **);
104 static int strip_crs (CT, int *);
105 static int convert_charsets (CT, char *, int *);
106 static int write_content (CT, char *, char *, int, int);
107 static int remove_file (char *);
108 static void report (char *, char *, char *, char *, ...);
109 static void pipeser (int);
110
111
112 int
113 main (int argc, char **argv) {
114 int msgnum;
115 char *cp, *file = NULL, *folder = NULL;
116 char *maildir, buf[100], *outfile = NULL;
117 char **argp, **arguments;
118 struct msgs_array msgs = { 0, 0, NULL };
119 struct msgs *mp = NULL;
120 CT *ctp;
121 FILE *fp;
122 int using_stdin = 0;
123 int status = OK;
124 fix_transformations fx;
125 fx.reformat = fx.fixcte = fx.fixboundary = 1;
126 fx.replacetextplain = 0;
127 fx.decodetext = CE_8BIT;
128 fx.textcharset = NULL;
129
130 if (nmh_init(argv[0], 1)) { return 1; }
131
132 done = freects_done;
133
134 arguments = getarguments (invo_name, argc, argv, 1);
135 argp = arguments;
136
137 /*
138 * Parse arguments
139 */
140 while ((cp = *argp++)) {
141 if (*cp == '-') {
142 switch (smatch (++cp, switches)) {
143 case AMBIGSW:
144 ambigsw (cp, switches);
145 done (1);
146 case UNKWNSW:
147 adios (NULL, "-%s unknown", cp);
148
149 case HELPSW:
150 snprintf (buf, sizeof buf, "%s [+folder] [msgs] [switches]",
151 invo_name);
152 print_help (buf, switches, 1);
153 done (0);
154 case VERSIONSW:
155 print_version(invo_name);
156 done (0);
157
158 case DECODETEXTSW:
159 if (! (cp = *argp++) || *cp == '-')
160 adios (NULL, "missing argument to %s", argp[-2]);
161 if (! strcasecmp (cp, "8bit")) {
162 fx.decodetext = CE_8BIT;
163 } else if (! strcasecmp (cp, "7bit")) {
164 fx.decodetext = CE_7BIT;
165 } else {
166 adios (NULL, "invalid argument to %s", argp[-2]);
167 }
168 continue;
169 case NDECODETEXTSW:
170 fx.decodetext = 0;
171 continue;
172 case TEXTCHARSETSW:
173 if (! (cp = *argp++) || (*cp == '-' && cp[1]))
174 adios (NULL, "missing argument to %s", argp[-2]);
175 fx.textcharset = cp;
176 continue;
177 case NTEXTCHARSETSW:
178 fx.textcharset = 0;
179 continue;
180 case FIXBOUNDARYSW:
181 fx.fixboundary = 1;
182 continue;
183 case NFIXBOUNDARYSW:
184 fx.fixboundary = 0;
185 continue;
186 case FIXCTESW:
187 fx.fixcte = 1;
188 continue;
189 case NFIXCTESW:
190 fx.fixcte = 0;
191 continue;
192 case REFORMATSW:
193 fx.reformat = 1;
194 continue;
195 case NREFORMATSW:
196 fx.reformat = 0;
197 continue;
198 case REPLACETEXTPLAINSW:
199 fx.replacetextplain = 1;
200 continue;
201 case NREPLACETEXTPLAINSW:
202 fx.replacetextplain = 0;
203 continue;
204 case FILESW:
205 if (! (cp = *argp++) || (*cp == '-' && cp[1]))
206 adios (NULL, "missing argument to %s", argp[-2]);
207 file = *cp == '-' ? add (cp, NULL) : path (cp, TFILE);
208 continue;
209 case OUTFILESW:
210 if (! (cp = *argp++) || (*cp == '-' && cp[1]))
211 adios (NULL, "missing argument to %s", argp[-2]);
212 outfile = *cp == '-' ? add (cp, NULL) : path (cp, TFILE);
213 continue;
214 case RPROCSW:
215 if (!(rmmproc = *argp++) || *rmmproc == '-')
216 adios (NULL, "missing argument to %s", argp[-2]);
217 continue;
218 case NRPRCSW:
219 rmmproc = NULL;
220 continue;
221 case VERBSW:
222 verbosw = 1;
223 continue;
224 case NVERBSW:
225 verbosw = 0;
226 continue;
227 }
228 }
229 if (*cp == '+' || *cp == '@') {
230 if (folder)
231 adios (NULL, "only one folder at a time!");
232 else
233 folder = pluspath (cp);
234 } else {
235 if (*cp == '/') {
236 /* Interpret a full path as a filename, not a message. */
237 file = add (cp, NULL);
238 } else {
239 app_msgarg (&msgs, cp);
240 }
241 }
242 }
243
244 SIGNAL (SIGQUIT, quitser);
245 SIGNAL (SIGPIPE, pipeser);
246
247 /*
248 * Read the standard profile setup
249 */
250 if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
251 readconfig ((struct node **) 0, fp, cp, 0);
252 fclose (fp);
253 }
254
255 suppress_bogus_mp_content_warning = skip_mp_cte_check = 1;
256
257 if (! context_find ("path"))
258 free (path ("./", TFOLDER));
259
260 if (file && msgs.size)
261 adios (NULL, "cannot specify msg and file at same time!");
262
263 /*
264 * check if message is coming from file
265 */
266 if (file) {
267 /* If file is stdin, create a tmp file name before parse_mime()
268 has a chance, because it might put in on a different
269 filesystem than the output file. Instead, put it in the
270 user's preferred tmp directory. */
271 CT ct;
272
273 if (! strcmp ("-", file)) {
274 int fd;
275 char *cp;
276
277 using_stdin = 1;
278
279 if ((cp = m_mktemp2 (NULL, invo_name, &fd, NULL)) == NULL) {
280 adios (NULL, "unable to create temporary file in %s",
281 get_temp_dir());
282 } else {
283 free (file);
284 file = add (cp, NULL);
285 cpydata (STDIN_FILENO, fd, "-", file);
286 }
287
288 if (close (fd)) {
289 (void) m_unlink (file);
290 adios (NULL, "failed to write temporary file");
291 }
292 }
293
294 if (! (cts = (CT *) calloc ((size_t) 2, sizeof *cts)))
295 adios (NULL, "out of memory");
296 ctp = cts;
297
298 if ((ct = parse_mime (file))) *ctp++ = ct;
299 } else {
300 /*
301 * message(s) are coming from a folder
302 */
303 CT ct;
304
305 if (! msgs.size)
306 app_msgarg(&msgs, "cur");
307 if (! folder)
308 folder = getfolder (1);
309 maildir = m_maildir (folder);
310
311 if (chdir (maildir) == NOTOK)
312 adios (maildir, "unable to change directory to");
313
314 /* read folder and create message structure */
315 if (! (mp = folder_read (folder, 1)))
316 adios (NULL, "unable to read folder %s", folder);
317
318 /* check for empty folder */
319 if (mp->nummsg == 0)
320 adios (NULL, "no messages in %s", folder);
321
322 /* parse all the message ranges/sequences and set SELECTED */
323 for (msgnum = 0; msgnum < msgs.size; msgnum++)
324 if (! m_convert (mp, msgs.msgs[msgnum]))
325 done (1);
326 seq_setprev (mp); /* set the previous-sequence */
327
328 if (! (cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof *cts)))
329 adios (NULL, "out of memory");
330 ctp = cts;
331
332 for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
333 if (is_selected(mp, msgnum)) {
334 char *msgnam;
335
336 msgnam = m_name (msgnum);
337 if ((ct = parse_mime (msgnam))) *ctp++ = ct;
338 }
339 }
340
341 seq_setcur (mp, mp->hghsel); /* update current message */
342 seq_save (mp); /* synchronize sequences */
343 context_replace (pfolder, folder);/* update current folder */
344 context_save (); /* save the context file */
345 }
346
347 if (*cts) {
348 for (ctp = cts; *ctp; ++ctp) {
349 status += mhfixmsgsbr (ctp, &fx, outfile);
350
351 if (using_stdin) {
352 (void) m_unlink (file);
353
354 if (! outfile) {
355 /* Just calling m_backup() unlinks the backup file. */
356 (void) m_backup (file);
357 }
358 }
359 }
360 } else {
361 status = 1;
362 }
363
364 free (outfile);
365 free (file);
366
367 /* done is freects_done, which will clean up all of cts. */
368 done (status);
369 return NOTOK;
370 }
371
372
373 int
374 mhfixmsgsbr (CT *ctp, const fix_transformations *fx, char *outfile) {
375 /* Store input filename in case one of the transformations, i.e.,
376 fix_boundary(), rewrites to a tmp file. */
377 char *input_filename = add ((*ctp)->c_file, NULL);
378 int modify_inplace = 0;
379 int message_mods = 0;
380 int status = OK;
381
382 if (outfile == NULL) {
383 modify_inplace = 1;
384
385 if ((*ctp)->c_file) {
386 char *tempfile;
387 if ((tempfile = m_mktemp2 (NULL, invo_name, NULL, NULL)) == NULL) {
388 adios (NULL, "unable to create temporary file in %s",
389 get_temp_dir());
390 }
391 outfile = add (tempfile, NULL);
392 } else {
393 adios (NULL, "missing both input and output filenames\n");
394 }
395 }
396
397 reverse_alternative_parts (*ctp);
398 if (status == OK && fx->fixboundary) {
399 status = fix_boundary (ctp, &message_mods);
400 }
401 if (status == OK && fx->fixcte) {
402 status = fix_multipart_cte (*ctp, &message_mods);
403 }
404 if (status == OK && fx->reformat) {
405 status =
406 ensure_text_plain (ctp, NULL, &message_mods, fx->replacetextplain);
407 }
408 if (status == OK && fx->decodetext) {
409 status = decode_text_parts (*ctp, fx->decodetext, &message_mods);
410 }
411 if (status == OK && fx->textcharset != NULL) {
412 status = convert_charsets (*ctp, fx->textcharset, &message_mods);
413 }
414
415 if (! (*ctp)->c_umask) {
416 /* Set the umask for the contents file. This currently
417 isn't used but just in case it is in the future. */
418 struct stat st;
419
420 if (stat ((*ctp)->c_file, &st) != NOTOK) {
421 (*ctp)->c_umask = ~(st.st_mode & 0777);
422 } else {
423 (*ctp)->c_umask = ~m_gmprot();
424 }
425 }
426
427 /*
428 * Write the content to a file
429 */
430 if (status == OK) {
431 status = write_content (*ctp, input_filename, outfile, modify_inplace,
432 message_mods);
433 } else if (! modify_inplace) {
434 /* Something went wrong. Output might be expected, such
435 as if this were run as a filter. Just copy the input
436 to the output. */
437 int in = open (input_filename, O_RDONLY);
438 int out = strcmp (outfile, "-")
439 ? open (outfile, O_WRONLY | O_CREAT, m_gmprot ())
440 : STDOUT_FILENO;
441
442 if (in != -1 && out != -1) {
443 cpydata (in, out, input_filename, outfile);
444 } else {
445 status = NOTOK;
446 }
447
448 close (out);
449 close (in);
450 }
451
452 if (modify_inplace) {
453 if (status != OK) (void) m_unlink (outfile);
454 free (outfile);
455 outfile = NULL;
456 }
457
458 free (input_filename);
459
460 return status;
461 }
462
463
464 /* parse_mime() arranges alternates in reverse (priority) order, so
465 reverse them back. This will put a text/plain part at the front of
466 a multipart/alternative part, for example, where it belongs. */
467 static void
468 reverse_alternative_parts (CT ct) {
469 if (ct->c_type == CT_MULTIPART) {
470 struct multipart *m = (struct multipart *) ct->c_ctparams;
471 struct part *part;
472
473 if (ct->c_subtype == MULTI_ALTERNATE) {
474 reverse_parts (ct);
475 }
476
477 /* And call recursively on each part of a multipart. */
478 for (part = m->mp_parts; part; part = part->mp_next) {
479 reverse_alternative_parts (part->mp_part);
480 }
481 }
482 }
483
484
485 static int
486 fix_boundary (CT *ct, int *message_mods) {
487 struct multipart *mp;
488 int status = OK;
489
490 if (bogus_mp_content) {
491 mp = (struct multipart *) (*ct)->c_ctparams;
492
493 /*
494 * 1) Get boundary at end of part.
495 * 2) Get boundary at beginning of part and compare to the end-of-part
496 * boundary.
497 * 3) Write out contents of ct to tmp file, replacing boundary in
498 * header with boundary from part. Set c_unlink to 1.
499 * 4) Free ct.
500 * 5) Call parse_mime() on the tmp file, replacing ct.
501 */
502
503 if (mp && mp->mp_start) {
504 char *part_boundary;
505
506 if (get_multipart_boundary (*ct, &part_boundary) == OK) {
507 char *fixed;
508
509 if ((fixed = m_mktemp2 (NULL, invo_name, NULL, &(*ct)->c_fp))) {
510 if (replace_boundary (*ct, fixed, part_boundary) == OK) {
511 char *filename = add ((*ct)->c_file, NULL);
512
513 free_content (*ct);
514 if ((*ct = parse_mime (fixed))) {
515 (*ct)->c_unlink = 1;
516
517 ++*message_mods;
518 if (verbosw) {
519 report (NULL, NULL, filename,
520 "fix multipart boundary");
521 }
522 }
523 free (filename);
524 } else {
525 advise (NULL, "unable to replace broken boundary");
526 status = NOTOK;
527 }
528 } else {
529 advise (NULL, "unable to create temporary file in %s",
530 get_temp_dir());
531 status = NOTOK;
532 }
533
534 free (part_boundary);
535 }
536 }
537 }
538
539 return status;
540 }
541
542
543 static int
544 get_multipart_boundary (CT ct, char **part_boundary) {
545 char buffer[BUFSIZ];
546 char *end_boundary = NULL;
547 off_t begin = (off_t) ct->c_end > (off_t) (ct->c_begin + sizeof buffer)
548 ? (off_t) (ct->c_end - sizeof buffer)
549 : (off_t) ct->c_begin;
550 size_t bytes_read;
551 int status = OK;
552
553 /* This will fail if the boundary spans fread() calls. BUFSIZ should
554 be big enough, even if it's just 1024, to make that unlikely. */
555
556 /* free_content() will close ct->c_fp. */
557 if (! ct->c_fp && (ct->c_fp = fopen (ct->c_file, "r")) == NULL) {
558 advise (ct->c_file, "unable to open for reading");
559 return NOTOK;
560 }
561
562 /* Get boundary at end of multipart. */
563 while (begin >= (off_t) ct->c_begin) {
564 fseeko (ct->c_fp, begin, SEEK_SET);
565 while ((bytes_read = fread (buffer, 1, sizeof buffer, ct->c_fp)) > 0) {
566 char *cp = rfind_str (buffer, bytes_read, "--");
567
568 if (cp) {
569 char *end;
570
571 /* Trim off trailing "--" and anything beyond. */
572 *cp-- = '\0';
573 if ((end = rfind_str (buffer, cp - buffer, "\n"))) {
574 if (strlen (end) > 3 && *end++ == '\n' &&
575 *end++ == '-' && *end++ == '-') {
576 end_boundary = add (end, NULL);
577 break;
578 }
579 }
580 }
581 }
582
583 if (! end_boundary && begin > (off_t) (ct->c_begin + sizeof buffer)) {
584 begin -= sizeof buffer;
585 } else {
586 break;
587 }
588 }
589
590 /* Get boundary at beginning of multipart. */
591 if (end_boundary) {
592 fseeko (ct->c_fp, ct->c_begin, SEEK_SET);
593 while ((bytes_read = fread (buffer, 1, sizeof buffer, ct->c_fp)) > 0) {
594 if (bytes_read >= strlen (end_boundary)) {
595 char *cp = find_str (buffer, bytes_read, end_boundary);
596
597 if (cp && cp - buffer >= 2 && *--cp == '-' &&
598 *--cp == '-' && (cp > buffer && *--cp == '\n')) {
599 status = OK;
600 break;
601 }
602 } else {
603 /* The start and end boundaries didn't match, or the
604 start boundary doesn't begin with "\n--" (or "--"
605 if at the beginning of buffer). Keep trying. */
606 status = NOTOK;
607 }
608 }
609 } else {
610 status = NOTOK;
611 }
612
613 if (status == OK) {
614 *part_boundary = end_boundary;
615 } else {
616 *part_boundary = NULL;
617 free (end_boundary);
618 }
619
620 return status;
621 }
622
623
624 /* Open and copy ct->c_file to file, replacing the multipart boundary. */
625 static int
626 replace_boundary (CT ct, char *file, char *boundary) {
627 FILE *fpin, *fpout;
628 int compnum, state;
629 char buf[BUFSIZ], name[NAMESZ];
630 char *np, *vp;
631 m_getfld_state_t gstate = 0;
632 int status = OK;
633
634 if (ct->c_file == NULL) {
635 advise (NULL, "missing input filename");
636 return NOTOK;
637 }
638
639 if ((fpin = fopen (ct->c_file, "r")) == NULL) {
640 advise (ct->c_file, "unable to open for reading");
641 return NOTOK;
642 }
643
644 if ((fpout = fopen (file, "w")) == NULL) {
645 fclose (fpin);
646 advise (file, "unable to open for writing");
647 return NOTOK;
648 }
649
650 for (compnum = 1;;) {
651 int bufsz = (int) sizeof buf;
652
653 switch (state = m_getfld (&gstate, name, buf, &bufsz, fpin)) {
654 case FLD:
655 case FLDPLUS:
656 compnum++;
657
658 /* get copies of the buffers */
659 np = add (name, NULL);
660 vp = add (buf, NULL);
661
662 /* if necessary, get rest of field */
663 while (state == FLDPLUS) {
664 bufsz = sizeof buf;
665 state = m_getfld (&gstate, name, buf, &bufsz, fpin);
666 vp = add (buf, vp); /* add to previous value */
667 }
668
669 if (strcasecmp (TYPE_FIELD, np)) {
670 fprintf (fpout, "%s:%s", np, vp);
671 } else {
672 char *new_ctline, *new_params;
673
674 replace_param(&ct->c_ctinfo.ci_first_pm,
675 &ct->c_ctinfo.ci_last_pm, "boundary",
676 boundary, 0);
677
678 new_ctline = concat(" ", ct->c_ctinfo.ci_type, "/",
679 ct->c_ctinfo.ci_subtype, NULL);
680 new_params = output_params(strlen(TYPE_FIELD) +
681 strlen(new_ctline) + 1,
682 ct->c_ctinfo.ci_first_pm, NULL, 0);
683 fprintf (fpout, "%s:%s%s\n", np, new_ctline,
684 new_params ? new_params : "");
685 free(new_ctline);
686 if (new_params)
687 free(new_params);
688 }
689
690 free (vp);
691 free (np);
692
693 continue;
694
695 case BODY:
696 fputs ("\n", fpout);
697 /* buf will have a terminating NULL, skip it. */
698 fwrite (buf, 1, bufsz-1, fpout);
699 continue;
700
701 case FILEEOF:
702 break;
703
704 case LENERR:
705 case FMTERR:
706 advise (NULL, "message format error in component #%d", compnum);
707 status = NOTOK;
708 break;
709
710 default:
711 advise (NULL, "getfld() returned %d", state);
712 status = NOTOK;
713 break;
714 }
715
716 break;
717 }
718
719 m_getfld_state_destroy (&gstate);
720 fclose (fpout);
721 fclose (fpin);
722
723 return status;
724 }
725
726
727 static int
728 fix_multipart_cte (CT ct, int *message_mods) {
729 int status = OK;
730
731 if (ct->c_type == CT_MULTIPART) {
732 struct multipart *m;
733 struct part *part;
734
735 if (ct->c_encoding != CE_7BIT && ct->c_encoding != CE_8BIT &&
736 ct->c_encoding != CE_BINARY) {
737 HF hf;
738
739 for (hf = ct->c_first_hf; hf; hf = hf->next) {
740 char *name = hf->name;
741 for (; *name && isspace ((unsigned char) *name); ++name) {
742 continue;
743 }
744
745 if (! strncasecmp (name, ENCODING_FIELD,
746 strlen (ENCODING_FIELD))) {
747 char *prefix = "Nmh-REPLACED-INVALID-";
748 HF h = mh_xmalloc (sizeof *h);
749
750 h->name = add (hf->name, NULL);
751 h->hf_encoding = hf->hf_encoding;
752 h->next = hf->next;
753 hf->next = h;
754
755 /* Retain old header but prefix its name. */
756 free (hf->name);
757 hf->name = concat (prefix, h->name, NULL);
758
759 ++*message_mods;
760 if (verbosw) {
761 char *encoding = cpytrim (hf->value);
762 report (NULL, ct->c_partno, ct->c_file,
763 "replace Content-Transfer-Encoding of %s "
764 "with 8 bit", encoding);
765 free (encoding);
766 }
767
768 h->value = add (" 8bit\n", NULL);
769
770 /* Don't need to warn for multiple C-T-E header
771 fields, parse_mime() already does that. But
772 if there are any, fix them all as necessary. */
773 hf = h;
774 }
775 }
776
777 set_ce (ct, CE_8BIT);
778 }
779
780 m = (struct multipart *) ct->c_ctparams;
781 for (part = m->mp_parts; part; part = part->mp_next) {
782 if (fix_multipart_cte (part->mp_part, message_mods) != OK) {
783 status = NOTOK;
784 break;
785 }
786 }
787 }
788
789 return status;
790 }
791
792
793 static int
794 set_ce (CT ct, int encoding) {
795 const char *ce = ce_str (encoding);
796 const struct str2init *ctinit = get_ce_method (ce);
797
798 if (ctinit) {
799 char *cte = concat (" ", ce, "\n", NULL);
800 int found_cte = 0;
801 HF hf;
802 /* Decoded contents might be in ct->c_cefile.ce_file, if the
803 caller is decode_text_parts (). Save because we'll
804 overwrite below. */
805 struct cefile decoded_content_info = ct->c_cefile;
806
807 ct->c_encoding = encoding;
808
809 ct->c_ctinitfnx = ctinit->si_init;
810 /* This will assign ct->c_cefile with an all-0 struct, which
811 is what we want. */
812 (*ctinit->si_init) (ct);
813 /* After returning, the caller should set
814 ct->c_cefile.ce_file to the name of the file containing
815 the contents. */
816
817 /* Restore the cefile. */
818 ct->c_cefile = decoded_content_info;
819
820 /* Update/add Content-Transfer-Encoding header field. */
821 for (hf = ct->c_first_hf; hf; hf = hf->next) {
822 if (! strcasecmp (ENCODING_FIELD, hf->name)) {
823 found_cte = 1;
824 free (hf->value);
825 hf->value = cte;
826 }
827 }
828 if (! found_cte) {
829 add_header (ct, add (ENCODING_FIELD, NULL), cte);
830 }
831
832 /* Update c_celine. It's used only by mhlist -debug. */
833 free (ct->c_celine);
834 ct->c_celine = add (cte, NULL);
835
836 return OK;
837 } else {
838 return NOTOK;
839 }
840 }
841
842
843 /* Make sure each text part has a corresponding text/plain part. */
844 static int
845 ensure_text_plain (CT *ct, CT parent, int *message_mods, int replacetextplain) {
846 int status = OK;
847
848 switch ((*ct)->c_type) {
849 case CT_TEXT: {
850 int has_text_plain = 0;
851
852 /* Nothing to do for text/plain. */
853 if ((*ct)->c_subtype == TEXT_PLAIN) return OK;
854
855 if (parent && parent->c_type == CT_MULTIPART &&
856 parent->c_subtype == MULTI_ALTERNATE) {
857 struct multipart *mp = (struct multipart *) parent->c_ctparams;
858 struct part *part, *prev;
859 int new_subpart_number = 1;
860
861 /* See if there is a sibling text/plain. */
862 for (prev = part = mp->mp_parts; part; part = part->mp_next) {
863 ++new_subpart_number;
864 if (part->mp_part->c_type == CT_TEXT &&
865 part->mp_part->c_subtype == TEXT_PLAIN) {
866 if (replacetextplain) {
867 struct part *old_part;
868 if (part == mp->mp_parts) {
869 old_part = mp->mp_parts;
870 mp->mp_parts = part->mp_next;
871 } else {
872 old_part = prev->mp_next;
873 prev->mp_next = part->mp_next;
874 }
875 if (verbosw) {
876 report (NULL, parent->c_partno, parent->c_file,
877 "remove text/plain part %s",
878 old_part->mp_part->c_partno);
879 }
880 free_content (old_part->mp_part);
881 free (old_part);
882 } else {
883 has_text_plain = 1;
884 }
885 break;
886 }
887 prev = part;
888 }
889
890 if (! has_text_plain) {
891 /* Parent is a multipart/alternative. Insert a new
892 text/plain subpart. */
893 struct part *new_part = mh_xmalloc (sizeof *new_part);
894
895 if ((new_part->mp_part = build_text_plain_part (*ct))) {
896 char buffer[16];
897 snprintf (buffer, sizeof buffer, "%d", new_subpart_number);
898
899 new_part->mp_next = mp->mp_parts;
900 mp->mp_parts = new_part;
901 new_part->mp_part->c_partno =
902 concat (parent->c_partno ? parent->c_partno : "1", ".",
903 buffer, NULL);
904
905 ++*message_mods;
906 if (verbosw) {
907 report (NULL, parent->c_partno, parent->c_file,
908 "insert text/plain part");
909 }
910 } else {
911 free_content (new_part->mp_part);
912 free (new_part);
913 status = NOTOK;
914 }
915 }
916 } else {
917 /* Slip new text/plain part into a new multipart/alternative. */
918 CT tp_part = build_text_plain_part (*ct);
919
920 if (tp_part) {
921 CT mp_alt = build_multipart_alt (*ct, tp_part, CT_MULTIPART,
922 MULTI_ALTERNATE);
923 if (mp_alt) {
924 struct multipart *mp =
925 (struct multipart *) mp_alt->c_ctparams;
926
927 if (mp && mp->mp_parts) {
928 mp->mp_parts->mp_part = tp_part;
929 /* Make the new multipart/alternative the parent. */
930 *ct = mp_alt;
931
932 ++*message_mods;
933 if (verbosw) {
934 report (NULL, (*ct)->c_partno, (*ct)->c_file,
935 "insert text/plain part");
936 }
937 } else {
938 free_content (tp_part);
939 free_content (mp_alt);
940 status = NOTOK;
941 }
942 } else {
943 status = NOTOK;
944 }
945 } else {
946 status = NOTOK;
947 }
948 }
949 break;
950 }
951
952 case CT_MULTIPART: {
953 struct multipart *mp = (struct multipart *) (*ct)->c_ctparams;
954 struct part *part;
955
956 for (part = mp->mp_parts; status == OK && part; part = part->mp_next) {
957 if ((*ct)->c_type == CT_MULTIPART) {
958 status = ensure_text_plain (&part->mp_part, *ct, message_mods,
959 replacetextplain);
960 }
961 }
962 break;
963 }
964
965 case CT_MESSAGE:
966 if ((*ct)->c_subtype == MESSAGE_EXTERNAL) {
967 struct exbody *e;
968
969 e = (struct exbody *) (*ct)->c_ctparams;
970 status = ensure_text_plain (&e->eb_content, *ct, message_mods,
971 replacetextplain);
972 }
973 break;
974 }
975
976 return status;
977 }
978
979
980 static CT
981 build_text_plain_part (CT encoded_part) {
982 CT tp_part = divide_part (encoded_part);
983 char *tmp_plain_file = NULL;
984
985 if (decode_part (tp_part) == OK) {
986 /* Now, tp_part->c_cefile.ce_file is the name of the tmp file that
987 contains the decoded contents. And the decoding function, such
988 as openQuoted, will have set ...->ce_unlink to 1 so that it will
989 be unlinked by free_content (). */
990 char *tempfile;
991
992 if ((tempfile = m_mktemp2 (NULL, invo_name, NULL, NULL)) == NULL) {
993 advise (NULL, "unable to create temporary file in %s",
994 get_temp_dir());
995 }
996 tmp_plain_file = add (tempfile, NULL);
997 if (reformat_part (tp_part, tmp_plain_file,
998 tp_part->c_ctinfo.ci_type,
999 tp_part->c_ctinfo.ci_subtype,
1000 tp_part->c_type) == OK) {
1001 return tp_part;
1002 }
1003 }
1004
1005 free_content (tp_part);
1006 (void) m_unlink (tmp_plain_file);
1007 free (tmp_plain_file);
1008
1009 return NULL;
1010 }
1011
1012
1013 static CT
1014 divide_part (CT ct) {
1015 CT new_part;
1016
1017 if ((new_part = (CT) calloc (1, sizeof *new_part)) == NULL)
1018 adios (NULL, "out of memory");
1019
1020 /* Just copy over what is needed for decoding. c_vrsn and
1021 c_celine aren't necessary. */
1022 new_part->c_file = add (ct->c_file, NULL);
1023 new_part->c_begin = ct->c_begin;
1024 new_part->c_end = ct->c_end;
1025 copy_ctinfo (&new_part->c_ctinfo, &ct->c_ctinfo);
1026 new_part->c_type = ct->c_type;
1027 new_part->c_cefile = ct->c_cefile;
1028 new_part->c_encoding = ct->c_encoding;
1029 new_part->c_ctinitfnx = ct->c_ctinitfnx;
1030 new_part->c_ceopenfnx = ct->c_ceopenfnx;
1031 new_part->c_ceclosefnx = ct->c_ceclosefnx;
1032 new_part->c_cesizefnx = ct->c_cesizefnx;
1033
1034 /* c_ctline is used by reformat__part(), so it can preserve
1035 anything after the type/subtype. */
1036 new_part->c_ctline = add (ct->c_ctline, NULL);
1037
1038 return new_part;
1039 }
1040
1041
1042 static void
1043 copy_ctinfo (CI dest, CI src) {
1044 PM s_pm, d_pm;
1045
1046 dest->ci_type = src->ci_type ? add (src->ci_type, NULL) : NULL;
1047 dest->ci_subtype = src->ci_subtype ? add (src->ci_subtype, NULL) : NULL;
1048
1049 for (s_pm = src->ci_first_pm; s_pm; s_pm = s_pm->pm_next) {
1050 d_pm = add_param(&dest->ci_first_pm, &dest->ci_last_pm, s_pm->pm_name,
1051 s_pm->pm_value, 0);
1052 if (s_pm->pm_charset)
1053 d_pm->pm_charset = getcpy(s_pm->pm_charset);
1054 if (s_pm->pm_lang)
1055 d_pm->pm_lang = getcpy(s_pm->pm_lang);
1056 }
1057
1058 dest->ci_comment = src->ci_comment ? add (src->ci_comment, NULL) : NULL;
1059 dest->ci_magic = src->ci_magic ? add (src->ci_magic, NULL) : NULL;
1060 }
1061
1062
1063 static int
1064 decode_part (CT ct) {
1065 char *tmp_decoded;
1066 int status;
1067 char *tempfile;
1068
1069 if ((tempfile = m_mktemp2 (NULL, invo_name, NULL, NULL)) == NULL) {
1070 adios (NULL, "unable to create temporary file in %s", get_temp_dir());
1071 }
1072 tmp_decoded = add (tempfile, NULL);
1073 /* The following call will load ct->c_cefile.ce_file with the tmp
1074 filename of the decoded content. tmp_decoded will contain the
1075 encoded output, get rid of that. */
1076 status = output_message (ct, tmp_decoded);
1077 (void) m_unlink (tmp_decoded);
1078 free (tmp_decoded);
1079
1080 return status;
1081 }
1082
1083
1084 /* Some of the arguments aren't really needed now, but maybe will
1085 be in the future for other than text types. */
1086 static int
1087 reformat_part (CT ct, char *file, char *type, char *subtype, int c_type) {
1088 int output_subtype, output_encoding;
1089 char *cp, *cf;
1090 int status;
1091
1092 /* Hacky: this redirects the output from whatever command is used
1093 to show the part to a file. So, the user can't have any output
1094 redirection in that command.
1095 Could show_multi() in mhshowsbr.c avoid this? */
1096
1097 /* Check for invo_name-format-type/subtype. */
1098 cp = concat (invo_name, "-format-", type, "/", subtype, NULL);
1099 if ((cf = context_find (cp)) && *cf != '\0') {
1100 if (strchr (cf, '>')) {
1101 free (cp);
1102 advise (NULL, "'>' prohibited in \"%s\",\nplease fix your "
1103 "%s-format-%s/%s profile entry", cf, invo_name, type,
1104 subtype);
1105 return NOTOK;
1106 }
1107 } else {
1108 free (cp);
1109
1110 /* Check for invo_name-format-type. */
1111 cp = concat (invo_name, "-format-", type, NULL);
1112 if (! (cf = context_find (cp)) || *cf == '\0') {
1113 free (cp);
1114 if (verbosw) {
1115 advise (NULL, "Don't know how to convert %s, there is no "
1116 "%s-format-%s/%s profile entry",
1117 ct->c_file, invo_name, type, subtype);
1118 }
1119 return NOTOK;
1120 }
1121
1122 if (strchr (cf, '>')) {
1123 free (cp);
1124 advise (NULL, "'>' prohibited in \"%s\"", cf);
1125 return NOTOK;
1126 }
1127 }
1128 free (cp);
1129
1130 cp = concat (cf, " >", file, NULL);
1131 status = show_content_aux (ct, 0, cp, NULL);
1132 free (cp);
1133
1134 /* Unlink decoded content tmp file and free its filename to avoid
1135 leaks. The file stream should already have been closed. */
1136 if (ct->c_cefile.ce_unlink) {
1137 (void) m_unlink (ct->c_cefile.ce_file);
1138 free (ct->c_cefile.ce_file);
1139 ct->c_cefile.ce_file = NULL;
1140 ct->c_cefile.ce_unlink = 0;
1141 }
1142
1143 if (c_type == CT_TEXT) {
1144 output_subtype = TEXT_PLAIN;
1145 } else {
1146 /* Set subtype to 0, which is always an UNKNOWN subtype. */
1147 output_subtype = 0;
1148 }
1149 output_encoding = charset_encoding (ct);
1150
1151 if (set_ct_type (ct, c_type, output_subtype, output_encoding) == OK) {
1152 ct->c_cefile.ce_file = file;
1153 ct->c_cefile.ce_unlink = 1;
1154 } else {
1155 ct->c_cefile.ce_unlink = 0;
1156 status = NOTOK;
1157 }
1158
1159 return status;
1160 }
1161
1162
1163 /* Identifies 7bit or 8bit content based on charset. */
1164 static int
1165 charset_encoding (CT ct) {
1166 /* norm_charmap() is case sensitive. */
1167 char *charset = upcase (content_charset (ct));
1168 int encoding =
1169 strcmp (norm_charmap (charset), "US-ASCII") ? CE_8BIT : CE_7BIT;
1170
1171 free (charset);
1172 return encoding;
1173 }
1174
1175
1176 static CT
1177 build_multipart_alt (CT first_alt, CT new_part, int type, int subtype) {
1178 char *boundary_prefix = "----=_nmh-multipart";
1179 char *boundary = concat (boundary_prefix, first_alt->c_partno, NULL);
1180 char *boundary_indicator = "; boundary=";
1181 char *typename, *subtypename, *name;
1182 CT ct;
1183 struct part *p;
1184 struct multipart *m;
1185 const struct str2init *ctinit;
1186
1187 if ((ct = (CT) calloc (1, sizeof *ct)) == NULL)
1188 adios (NULL, "out of memory");
1189
1190 /* Set up the multipart/alternative part. These fields of *ct were
1191 initialized to 0 by calloc():
1192 c_fp, c_unlink, c_begin, c_end,
1193 c_vrsn, c_ctline, c_celine,
1194 c_id, c_descr, c_dispo, c_partno,
1195 c_ctinfo.ci_comment, c_ctinfo.ci_magic,
1196 c_cefile, c_encoding,
1197 c_digested, c_digest[16], c_ctexbody,
1198 c_ctinitfnx, c_ceopenfnx, c_ceclosefnx, c_cesizefnx,
1199 c_umask, c_rfc934,
1200 c_showproc, c_termproc, c_storeproc, c_storage, c_folder
1201 */
1202
1203 ct->c_file = add (first_alt->c_file, NULL);
1204 ct->c_type = type;
1205 ct->c_subtype = subtype;
1206
1207 ctinit = get_ct_init (ct->c_type);
1208
1209 typename = ct_type_str (type);
1210 subtypename = ct_subtype_str (type, subtype);
1211
1212 {
1213 int serial = 0;
1214 int found_boundary = 1;
1215
1216 while (found_boundary && serial < 1000000) {
1217 found_boundary = 0;
1218
1219 /* Ensure that the boundary doesn't appear in the decoded
1220 content. */
1221 if (new_part->c_cefile.ce_file) {
1222 if ((found_boundary =
1223 boundary_in_content (&new_part->c_cefile.ce_fp,
1224 new_part->c_cefile.ce_file,
1225 boundary)) == -1) {
1226 free (ct);
1227 return NULL;
1228 }
1229 }
1230
1231 /* Ensure that the boundary doesn't appear in the encoded
1232 content. */
1233 if (! found_boundary && new_part->c_file) {
1234 if ((found_boundary = boundary_in_content (&new_part->c_fp,
1235 new_part->c_file,
1236 boundary)) == -1) {
1237 free (ct);
1238 return NULL;
1239 }
1240 }
1241
1242 if (found_boundary) {
1243 /* Try a slightly different boundary. */
1244 char buffer2[16];
1245
1246 free (boundary);
1247 ++serial;
1248 snprintf (buffer2, sizeof buffer2, "%d", serial);
1249 boundary =
1250 concat (boundary_prefix,
1251 first_alt->c_partno ? first_alt->c_partno : "",
1252 "-", buffer2, NULL);
1253 }
1254 }
1255
1256 if (found_boundary) {
1257 advise (NULL, "giving up trying to find a unique boundary");
1258 free (ct);
1259 return NULL;
1260 }
1261 }
1262
1263 name = concat (" ", typename, "/", subtypename, boundary_indicator, "\"",
1264 boundary, "\"", NULL);
1265
1266 /* Load c_first_hf and c_last_hf. */
1267 transfer_noncontent_headers (first_alt, ct);
1268 add_header (ct, add (TYPE_FIELD, NULL), concat (name, "\n", NULL));
1269 free (name);
1270
1271 /* Load c_partno. */
1272 if (first_alt->c_partno) {
1273 ct->c_partno = add (first_alt->c_partno, NULL);
1274 free (first_alt->c_partno);
1275 first_alt->c_partno = concat (ct->c_partno, ".1", NULL);
1276 new_part->c_partno = concat (ct->c_partno, ".2", NULL);
1277 } else {
1278 first_alt->c_partno = add ("1", NULL);
1279 new_part->c_partno = add ("2", NULL);
1280 }
1281
1282 if (ctinit) {
1283 ct->c_ctinfo.ci_type = add (typename, NULL);
1284 ct->c_ctinfo.ci_subtype = add (subtypename, NULL);
1285 }
1286
1287 add_param(&ct->c_ctinfo.ci_first_pm, &ct->c_ctinfo.ci_last_pm,
1288 "boundary", boundary, 0);
1289
1290 p = (struct part *) mh_xmalloc (sizeof *p);
1291 p->mp_next = (struct part *) mh_xmalloc (sizeof *p->mp_next);
1292 p->mp_next->mp_next = NULL;
1293 p->mp_next->mp_part = first_alt;
1294
1295 if ((m = (struct multipart *) calloc (1, sizeof (struct multipart))) ==
1296 NULL)
1297 adios (NULL, "out of memory");
1298 m->mp_start = concat (boundary, "\n", NULL);
1299 m->mp_stop = concat (boundary, "--\n", NULL);
1300 m->mp_parts = p;
1301 ct->c_ctparams = (void *) m;
1302
1303 free (boundary);
1304
1305 return ct;
1306 }
1307
1308
1309 /* Check that the boundary does not appear in the content. */
1310 static int
1311 boundary_in_content (FILE **fp, char *file, const char *boundary) {
1312 char buffer[BUFSIZ];
1313 size_t bytes_read;
1314 int found_boundary = 0;
1315
1316 /* free_content() will close *fp if we fopen it here. */
1317 if (! *fp && (*fp = fopen (file, "r")) == NULL) {
1318 advise (file, "unable to open %s for reading", file);
1319 return NOTOK;
1320 }
1321
1322 fseeko (*fp, 0L, SEEK_SET);
1323 while ((bytes_read = fread (buffer, 1, sizeof buffer, *fp)) > 0) {
1324 if (find_str (buffer, bytes_read, boundary)) {
1325 found_boundary = 1;
1326 break;
1327 }
1328 }
1329
1330 return found_boundary;
1331 }
1332
1333
1334 /* Remove all non-Content headers. */
1335 static void
1336 transfer_noncontent_headers (CT old, CT new) {
1337 HF hp, hp_prev;
1338
1339 hp_prev = hp = old->c_first_hf;
1340 while (hp) {
1341 HF next = hp->next;
1342
1343 if (strncasecmp (XXX_FIELD_PRF, hp->name, strlen (XXX_FIELD_PRF))) {
1344 if (hp == old->c_last_hf) {
1345 if (hp == old->c_first_hf) {
1346 old->c_last_hf = old->c_first_hf = NULL;
1347 } else {
1348 hp_prev->next = NULL;
1349 old->c_last_hf = hp_prev;
1350 }
1351 } else {
1352 if (hp == old->c_first_hf) {
1353 old->c_first_hf = next;
1354 } else {
1355 hp_prev->next = next;
1356 }
1357 }
1358
1359 /* Put node hp in the new CT. */
1360 if (new->c_first_hf == NULL) {
1361 new->c_first_hf = hp;
1362 } else {
1363 new->c_last_hf->next = hp;
1364 }
1365 new->c_last_hf = hp;
1366 } else {
1367 /* A Content- header, leave in old. */
1368 hp_prev = hp;
1369 }
1370
1371 hp = next;
1372 }
1373 }
1374
1375
1376 static int
1377 set_ct_type (CT ct, int type, int subtype, int encoding) {
1378 char *typename = ct_type_str (type);
1379 char *subtypename = ct_subtype_str (type, subtype);
1380 /* E.g, " text/plain" */
1381 char *type_subtypename = concat (" ", typename, "/", subtypename, NULL);
1382 /* E.g, " text/plain\n" */
1383 char *name_plus_nl = concat (type_subtypename, "\n", NULL);
1384 int found_content_type = 0;
1385 HF hf;
1386 const char *cp = NULL;
1387 char *ctline;
1388 int status;
1389
1390 /* Update/add Content-Type header field. */
1391 for (hf = ct->c_first_hf; hf; hf = hf->next) {
1392 if (! strcasecmp (TYPE_FIELD, hf->name)) {
1393 found_content_type = 1;
1394 free (hf->value);
1395 hf->value = (cp = strchr (ct->c_ctline, ';'))
1396 ? concat (type_subtypename, cp, "\n", NULL)
1397 : add (name_plus_nl, NULL);
1398 }
1399 }
1400 if (! found_content_type) {
1401 add_header (ct, add (TYPE_FIELD, NULL),
1402 (cp = strchr (ct->c_ctline, ';'))
1403 ? concat (type_subtypename, cp, "\n", NULL)
1404 : add (name_plus_nl, NULL));
1405 }
1406
1407 /* Some of these might not be used, but set them anyway. */
1408 ctline = cp
1409 ? concat (type_subtypename, cp, NULL)
1410 : concat (type_subtypename, NULL);
1411 free (ct->c_ctline);
1412 ct->c_ctline = ctline;
1413 /* Leave other ctinfo members as they were. */
1414 free (ct->c_ctinfo.ci_type);
1415 ct->c_ctinfo.ci_type = add (typename, NULL);
1416 free (ct->c_ctinfo.ci_subtype);
1417 ct->c_ctinfo.ci_subtype = add (subtypename, NULL);
1418 ct->c_type = type;
1419 ct->c_subtype = subtype;
1420
1421 free (name_plus_nl);
1422 free (type_subtypename);
1423
1424 status = set_ce (ct, encoding);
1425
1426 return status;
1427 }
1428
1429
1430 static int
1431 decode_text_parts (CT ct, int encoding, int *message_mods) {
1432 int status = OK;
1433
1434 switch (ct->c_type) {
1435 case CT_TEXT:
1436 switch (ct->c_encoding) {
1437 case CE_BASE64:
1438 case CE_QUOTED: {
1439 int ct_encoding;
1440
1441 if (decode_part (ct) == OK && ct->c_cefile.ce_file) {
1442 const char *reason = NULL;
1443
1444 if ((ct_encoding = content_encoding (ct, &reason)) == CE_BINARY
1445 && encoding != CE_BINARY) {
1446 /* The decoding isn't acceptable so discard it.
1447 Leave status as OK to allow other transformations. */
1448 if (verbosw) {
1449 report (NULL, ct->c_partno, ct->c_file,
1450 "will not decode%s because it is binary (%s)",
1451 ct->c_partno ? ""
1452 : ct->c_ctline ? ct->c_ctline
1453 : "",
1454 reason);
1455 }
1456 (void) m_unlink (ct->c_cefile.ce_file);
1457 free (ct->c_cefile.ce_file);
1458 ct->c_cefile.ce_file = NULL;
1459 } else if (ct->c_encoding == CE_QUOTED &&
1460 ct_encoding == CE_8BIT && encoding == CE_7BIT) {
1461 /* The decoding isn't acceptable so discard it.
1462 Leave status as OK to allow other transformations. */
1463 if (verbosw) {
1464 report (NULL, ct->c_partno, ct->c_file,
1465 "will not decode%s because it is 8bit",
1466 ct->c_partno ? ""
1467 : ct->c_ctline ? ct->c_ctline
1468 : "");
1469 }
1470 (void) m_unlink (ct->c_cefile.ce_file);
1471 free (ct->c_cefile.ce_file);
1472 ct->c_cefile.ce_file = NULL;
1473 } else {
1474 int enc;
1475 if (ct_encoding == CE_BINARY)
1476 enc = CE_BINARY;
1477 else if (ct_encoding == CE_8BIT && encoding == CE_7BIT)
1478 enc = CE_QUOTED;
1479 else
1480 enc = charset_encoding (ct);
1481 if (set_ce (ct, enc) == OK) {
1482 ++*message_mods;
1483 if (verbosw) {
1484 report (NULL, ct->c_partno, ct->c_file, "decode%s",
1485 ct->c_ctline ? ct->c_ctline : "");
1486 }
1487 strip_crs (ct, message_mods);
1488 } else {
1489 status = NOTOK;
1490 }
1491 }
1492 } else {
1493 status = NOTOK;
1494 }
1495 break;
1496 }
1497 case CE_8BIT:
1498 case CE_7BIT:
1499 strip_crs (ct, message_mods);
1500 break;
1501 default:
1502 break;
1503 }
1504
1505 break;
1506
1507 case CT_MULTIPART: {
1508 struct multipart *m = (struct multipart *) ct->c_ctparams;
1509 struct part *part;
1510
1511 /* Should check to see if the body for this part is encoded?
1512 For now, it gets passed along as-is by InitMultiPart(). */
1513 for (part = m->mp_parts; status == OK && part; part = part->mp_next) {
1514 status = decode_text_parts (part->mp_part, encoding, message_mods);
1515 }
1516 break;
1517 }
1518
1519 case CT_MESSAGE:
1520 if (ct->c_subtype == MESSAGE_EXTERNAL) {
1521 struct exbody *e;
1522
1523 e = (struct exbody *) ct->c_ctparams;
1524 status = decode_text_parts (e->eb_content, encoding, message_mods);
1525 }
1526 break;
1527
1528 default:
1529 break;
1530 }
1531
1532 return status;
1533 }
1534
1535
1536 /* See if the decoded content is 7bit, 8bit, or binary. It's binary
1537 if it has any NUL characters, a CR not followed by a LF, or lines
1538 greater than 998 characters in length. If binary, reason is set
1539 to a string explaining why. */
1540 static int
1541 content_encoding (CT ct, const char **reason) {
1542 CE ce = &ct->c_cefile;
1543 int encoding = CE_7BIT;
1544
1545 if (ce->ce_file) {
1546 size_t line_len = 0;
1547 char buffer[BUFSIZ];
1548 size_t inbytes;
1549
1550 if (! ce->ce_fp && (ce->ce_fp = fopen (ce->ce_file, "r")) == NULL) {
1551 advise (ce->ce_file, "unable to open for reading");
1552 return CE_UNKNOWN;
1553 }
1554
1555 fseeko (ce->ce_fp, 0L, SEEK_SET);
1556 while (encoding != CE_BINARY &&
1557 (inbytes = fread (buffer, 1, sizeof buffer, ce->ce_fp)) > 0) {
1558 char *cp;
1559 size_t i;
1560 int last_char_was_cr = 0;
1561
1562 for (i = 0, cp = buffer; i < inbytes; ++i, ++cp) {
1563 if (*cp == '\0' || ++line_len > 998 ||
1564 (*cp != '\n' && last_char_was_cr)) {
1565 encoding = CE_BINARY;
1566 if (*cp == '\0') {
1567 *reason = "null character";
1568 } else if (line_len > 998) {
1569 *reason = "line length > 998";
1570 } else if (*cp != '\n' && last_char_was_cr) {
1571 *reason = "CR not followed by LF";
1572 } else {
1573 /* Should not reach this. */
1574 *reason = "";
1575 }
1576 break;
1577 } else if (*cp == '\n') {
1578 line_len = 0;
1579 } else if (! isascii ((unsigned char) *cp)) {
1580 encoding = CE_8BIT;
1581 }
1582
1583 last_char_was_cr = *cp == '\r' ? 1 : 0;
1584 }
1585 }
1586
1587 fclose (ce->ce_fp);
1588 ce->ce_fp = NULL;
1589 } /* else should never happen */
1590
1591 return encoding;
1592 }
1593
1594
1595 static int
1596 strip_crs (CT ct, int *message_mods) {
1597 /* norm_charmap() is case sensitive. */
1598 char *charset = upcase (content_charset (ct));
1599 int status = OK;
1600
1601 /* Only strip carriage returns if content is ASCII or another
1602 charset that has the same readily recognizable CR followed by a
1603 LF. We can include UTF-8 here because if the high-order bit of
1604 a UTF-8 byte is 0, then it must be a single-byte ASCII
1605 character. */
1606 if (! strcmp (norm_charmap (charset), "US-ASCII") ||
1607 ! strncmp (norm_charmap (charset), "ISO-8859-", 9) ||
1608 ! strncmp (norm_charmap (charset), "UTF-8", 5) ||
1609 ! strncmp (norm_charmap (charset), "WINDOWS-12", 10)) {
1610 char **file = NULL;
1611 FILE **fp = NULL;
1612 size_t begin;
1613 size_t end;
1614 int has_crs = 0;
1615 int opened_input_file = 0;
1616
1617 if (ct->c_cefile.ce_file) {
1618 file = &ct->c_cefile.ce_file;
1619 fp = &ct->c_cefile.ce_fp;
1620 begin = end = 0;
1621 } else if (ct->c_file) {
1622 file = &ct->c_file;
1623 fp = &ct->c_fp;
1624 begin = (size_t) ct->c_begin;
1625 end = (size_t) ct->c_end;
1626 } /* else don't know where the content is */
1627
1628 if (file && *file && fp) {
1629 if (! *fp) {
1630 if ((*fp = fopen (*file, "r")) == NULL) {
1631 advise (*file, "unable to open for reading");
1632 status = NOTOK;
1633 } else {
1634 opened_input_file = 1;
1635 }
1636 }
1637 }
1638
1639 if (fp && *fp) {
1640 char buffer[BUFSIZ];
1641 size_t bytes_read;
1642 size_t bytes_to_read =
1643 end > 0 && end > begin ? end - begin : sizeof buffer;
1644
1645 fseeko (*fp, begin, SEEK_SET);
1646 while ((bytes_read = fread (buffer, 1,
1647 min (bytes_to_read, sizeof buffer),
1648 *fp)) > 0) {
1649 /* Look for CR followed by a LF. This is supposed to
1650 be text so there should be LF's. If not, don't
1651 modify the content. */
1652 char *cp;
1653 size_t i;
1654 int last_char_was_cr = 0;
1655
1656 if (end > 0) bytes_to_read -= bytes_read;
1657
1658 for (i = 0, cp = buffer; i < bytes_read; ++i, ++cp) {
1659 if (*cp == '\n' && last_char_was_cr) {
1660 has_crs = 1;
1661 break;
1662 }
1663
1664 last_char_was_cr = *cp == '\r' ? 1 : 0;
1665 }
1666 }
1667
1668 if (has_crs) {
1669 int fd;
1670 char *stripped_content_file;
1671 char *tempfile = m_mktemp2 (NULL, invo_name, &fd, NULL);
1672
1673 if (tempfile == NULL) {
1674 adios (NULL, "unable to create temporary file in %s",
1675 get_temp_dir());
1676 }
1677 stripped_content_file = add (tempfile, NULL);
1678
1679 /* Strip each CR before a LF from the content. */
1680 fseeko (*fp, begin, SEEK_SET);
1681 while ((bytes_read = fread (buffer, 1, sizeof buffer, *fp)) >
1682 0) {
1683 char *cp;
1684 size_t i;
1685 int last_char_was_cr = 0;
1686
1687 for (i = 0, cp = buffer; i < bytes_read; ++i, ++cp) {
1688 if (*cp == '\r') {
1689 last_char_was_cr = 1;
1690 } else if (last_char_was_cr) {
1691 if (*cp != '\n') write (fd, "\r", 1);
1692 write (fd, cp, 1);
1693 last_char_was_cr = 0;
1694 } else {
1695 write (fd, cp, 1);
1696 last_char_was_cr = 0;
1697 }
1698 }
1699 }
1700
1701 if (close (fd)) {
1702 admonish (NULL, "unable to write temporary file %s",
1703 stripped_content_file);
1704 (void) m_unlink (stripped_content_file);
1705 status = NOTOK;
1706 } else {
1707 /* Replace the decoded file with the converted one. */
1708 if (ct->c_cefile.ce_file) {
1709 if (ct->c_cefile.ce_unlink) {
1710 (void) m_unlink (ct->c_cefile.ce_file);
1711 }
1712 free (ct->c_cefile.ce_file);
1713 }
1714 ct->c_cefile.ce_file = stripped_content_file;
1715 ct->c_cefile.ce_unlink = 1;
1716
1717 ++*message_mods;
1718 if (verbosw) {
1719 report (NULL, ct->c_partno,
1720 begin == 0 && end == 0 ? "" : *file,
1721 "stripped CRs");
1722 }
1723 }
1724 }
1725
1726 if (opened_input_file) {
1727 fclose (*fp);
1728 *fp = NULL;
1729 }
1730 }
1731 }
1732
1733 free (charset);
1734 return status;
1735 }
1736
1737
1738 static int
1739 convert_charsets (CT ct, char *dest_charset, int *message_mods) {
1740 int status = OK;
1741
1742 switch (ct->c_type) {
1743 case CT_TEXT:
1744 if (ct->c_subtype == TEXT_PLAIN) {
1745 status = convert_charset (ct, dest_charset, message_mods);
1746 if (status == OK) {
1747 if (verbosw) {
1748 report (NULL, ct->c_partno, ct->c_file,
1749 "convert %s to %s",
1750 content_charset(ct), dest_charset);
1751 }
1752 } else {
1753 report ("iconv", ct->c_partno, ct->c_file,
1754 "failed to convert %s to %s",
1755 content_charset(ct), dest_charset);
1756 }
1757 }
1758 break;
1759
1760 case CT_MULTIPART: {
1761 struct multipart *m = (struct multipart *) ct->c_ctparams;
1762 struct part *part;
1763
1764 /* Should check to see if the body for this part is encoded?
1765 For now, it gets passed along as-is by InitMultiPart(). */
1766 for (part = m->mp_parts; status == OK && part; part = part->mp_next) {
1767 status =
1768 convert_charsets (part->mp_part, dest_charset, message_mods);
1769 }
1770 break;
1771 }
1772
1773 case CT_MESSAGE:
1774 if (ct->c_subtype == MESSAGE_EXTERNAL) {
1775 struct exbody *e;
1776
1777 e = (struct exbody *) ct->c_ctparams;
1778 status =
1779 convert_charsets (e->eb_content, dest_charset, message_mods);
1780 }
1781 break;
1782
1783 default:
1784 break;
1785 }
1786
1787 return status;
1788 }
1789
1790
1791 static int
1792 write_content (CT ct, char *input_filename, char *outfile, int modify_inplace,
1793 int message_mods) {
1794 int status = OK;
1795
1796 if (modify_inplace) {
1797 if (message_mods > 0) {
1798 if ((status = output_message (ct, outfile)) == OK) {
1799 char *infile = input_filename
1800 ? add (input_filename, NULL)
1801 : add (ct->c_file ? ct->c_file : "-", NULL);
1802
1803 if (remove_file (infile) == OK) {
1804 if (rename (outfile, infile)) {
1805 /* Rename didn't work, possibly because of an
1806 attempt to rename across filesystems. Try
1807 brute force copy. */
1808 int old = open (outfile, O_RDONLY);
1809 int new =
1810 open (infile, O_WRONLY | O_CREAT, m_gmprot ());
1811 int i = -1;
1812
1813 if (old != -1 && new != -1) {
1814 char buffer[BUFSIZ];
1815
1816 while ((i = read (old, buffer, sizeof buffer)) >
1817 0) {
1818 if (write (new, buffer, i) != i) {
1819 i = -1;
1820 break;
1821 }
1822 }
1823 }
1824 if (new != -1) close (new);
1825 if (old != -1) close (old);
1826 (void) m_unlink (outfile);
1827
1828 if (i < 0) {
1829 /* The -file argument processing used path() to
1830 expand filename to absolute path. */
1831 int file = ct->c_file && ct->c_file[0] == '/';
1832
1833 admonish (NULL, "unable to rename %s %s to %s",
1834 file ? "file" : "message", outfile,
1835 infile);
1836 status = NOTOK;
1837 }
1838 }
1839 } else {
1840 admonish (NULL, "unable to remove input file %s, "
1841 "not modifying it", infile);
1842 (void) m_unlink (outfile);
1843 status = NOTOK;
1844 }
1845
1846 free (infile);
1847 } else {
1848 status = NOTOK;
1849 }
1850 } else {
1851 /* No modifications and didn't need the tmp outfile. */
1852 (void) m_unlink (outfile);
1853 }
1854 } else {
1855 /* Output is going to some file. Produce it whether or not
1856 there were modifications. */
1857 status = output_message (ct, outfile);
1858 }
1859
1860 flush_errors ();
1861 return status;
1862 }
1863
1864
1865 /*
1866 * If "rmmproc" is defined, call that to remove the file. Otherwise,
1867 * use the standard MH backup file.
1868 */
1869 static int
1870 remove_file (char *file) {
1871 if (rmmproc) {
1872 char *rmm_command = concat (rmmproc, " ", file, NULL);
1873 int status = system (rmm_command);
1874
1875 free (rmm_command);
1876 return WIFEXITED (status) ? WEXITSTATUS (status) : NOTOK;
1877 } else {
1878 /* This is OK for a non-message file, it still uses the
1879 BACKUP_PREFIX form. The backup file will be in the same
1880 directory as file. */
1881 return rename (file, m_backup (file));
1882 }
1883 }
1884
1885
1886 static void
1887 report (char *what, char *partno, char *filename, char *message, ...) {
1888 va_list args;
1889 char *fmt;
1890
1891 if (verbosw) {
1892 va_start (args, message);
1893 fmt = concat (filename, partno ? " part " : ", ",
1894 partno ? partno : "", partno ? ", " : "", message, NULL);
1895
1896 advertise (what, NULL, fmt, args);
1897
1898 free (fmt);
1899 va_end (args);
1900 }
1901 }
1902
1903
1904 static void
1905 pipeser (int i)
1906 {
1907 if (i == SIGQUIT) {
1908 fflush (stdout);
1909 fprintf (stderr, "\n");
1910 fflush (stderr);
1911 }
1912
1913 done (1);
1914 /* NOTREACHED */
1915 }