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