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