]> diplodocus.org Git - nmh/blob - uip/mhshowsbr.c
fdcompare.c: Move interface to own file.
[nmh] / uip / mhshowsbr.c
1 /* mhshowsbr.c -- routines to display the contents of MIME messages
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include "h/mh.h"
9 #include "sbr/context_find.h"
10 #include "sbr/pidstatus.h"
11 #include "sbr/arglist.h"
12 #include "sbr/error.h"
13 #include <fcntl.h>
14 #include "h/signals.h"
15 #include "h/md5.h"
16 #include "h/mts.h"
17 #include "h/tws.h"
18 #include "h/mime.h"
19 #include "h/mhparse.h"
20 #include "h/fmt_scan.h"
21 #include "h/done.h"
22 #include "h/utils.h"
23 #include "mhmisc.h"
24 #include "mhshowsbr.h"
25 #include "sbr/m_mktemp.h"
26 #ifdef HAVE_ICONV
27 # include <iconv.h>
28 #endif /* ! HAVE_ICONV */
29
30 extern int debugsw;
31
32 int nolist = 0;
33
34 char *progsw = NULL;
35
36 /* flags for moreproc/header display */
37 int nomore = 0;
38 char *formsw = NULL;
39
40 /* for output markers and headers */
41 char *folder = NULL;
42 char *markerform;
43 char *headerform;
44 int headersw = -1;
45
46
47 /*
48 * static prototypes
49 */
50 static void show_single_message (CT, char *, int, int, int, struct format *);
51 static void DisplayMsgHeader (CT, char *, int);
52 static int show_switch (CT, int, int, int, int, struct format *);
53 static int show_content (CT, int, int, int, struct format *);
54 static int show_content_aux2 (CT, int, char *, char *, int, int, int, struct format *);
55 static int show_text (CT, int, int, struct format *);
56 static int show_multi (CT, int, int, int, int, struct format *);
57 static int show_multi_internal (CT, int, int, int, int, struct format *);
58 static int show_multi_aux (CT, int, char *, struct format *);
59 static int show_message_rfc822 (CT, int, struct format *);
60 static int show_partial (CT, int);
61 static int show_external (CT, int, int, int, int, struct format *);
62 static int parse_display_string (CT, char *, int *, int *, char *, char *,
63 size_t, int multipart);
64 static int convert_content_charset (CT, char **);
65 static struct format *compile_header(char *);
66 static struct format *compile_marker(char *);
67 static void output_header (CT, struct format *);
68 static void output_marker (CT, struct format *, int);
69 static void free_markercomps (void);
70 static int pidcheck(int);
71
72 /*
73 * Components (and list of parameters/components) we care about for the
74 * content marker display.
75 */
76
77 static struct comp *folder_comp = NULL;
78 static struct comp *part_comp = NULL;
79 static struct comp *ctype_comp = NULL;
80 static struct comp *description_comp = NULL;
81 static struct comp *dispo_comp = NULL;
82
83 struct param_comp_list {
84 char *param;
85 struct comp *comp;
86 struct param_comp_list *next;
87 };
88
89 static struct param_comp_list *ctype_pc_list = NULL;
90 static struct param_comp_list *dispo_pc_list = NULL;
91
92
93 /*
94 * Top level entry point to show/display a group of messages
95 */
96
97 void
98 show_all_messages(CT *cts, int concat, int textonly, int inlineonly)
99 {
100 CT ct, *ctp;
101 struct format *hfmt, *mfmt;
102
103 /*
104 * If form is not specified, then get default form
105 * for showing headers of MIME messages.
106 */
107 if (!formsw)
108 formsw = mh_xstrdup(etcpath("mhl.headers"));
109
110 /*
111 * Compile the content marker and header format lines
112 */
113 mfmt = compile_marker(markerform);
114 hfmt = compile_header(headerform);
115
116 /*
117 * If form is "mhl.null", suppress display of header.
118 */
119 if (!strcmp (formsw, "mhl.null"))
120 formsw = NULL;
121
122 for (ctp = cts; *ctp; ctp++) {
123 ct = *ctp;
124
125 /* if top-level type is ok, then display message */
126 if (type_ok (ct, 1)) {
127 if (headersw) output_header(ct, hfmt);
128
129 show_single_message (ct, formsw, concat, textonly, inlineonly,
130 mfmt);
131 }
132 }
133
134 free_markercomps();
135 fmt_free(hfmt, 1);
136 fmt_free(mfmt, 1);
137 }
138
139
140 /*
141 * Entry point to show/display a single message
142 */
143
144 static void
145 show_single_message (CT ct, char *form, int concatsw, int textonly,
146 int inlineonly, struct format *fmt)
147 {
148 sigset_t set, oset;
149
150 int status = OK;
151
152 /* Allow user executable bit so that temporary directories created by
153 * the viewer (e.g., lynx) are going to be accessible */
154 umask (ct->c_umask & ~(0100));
155
156 /*
157 * If you have a format file, then display
158 * the message headers.
159 */
160 if (form)
161 DisplayMsgHeader(ct, form, concatsw);
162
163 /* Show the body of the message */
164 show_switch (ct, 0, concatsw, textonly, inlineonly, fmt);
165
166 if (ct->c_fp) {
167 fclose (ct->c_fp);
168 ct->c_fp = NULL;
169 }
170 if (ct->c_ceclosefnx)
171 (*ct->c_ceclosefnx) (ct);
172
173 /* block a few signals */
174 sigemptyset (&set);
175 sigaddset (&set, SIGHUP);
176 sigaddset (&set, SIGINT);
177 sigaddset (&set, SIGQUIT);
178 sigaddset (&set, SIGTERM);
179 sigprocmask (SIG_BLOCK, &set, &oset);
180
181 while (!concatsw && wait (&status) != NOTOK) {
182 pidcheck (status);
183 continue;
184 }
185
186 /* reset the signal mask */
187 sigprocmask (SIG_SETMASK, &oset, &set);
188
189 flush_errors ();
190 }
191
192
193 /*
194 * Use the mhlproc to show the header fields
195 */
196
197 static void
198 DisplayMsgHeader (CT ct, char *form, int concatsw)
199 {
200 pid_t child_id;
201 int vecp;
202 char **vec;
203 char *file;
204
205 vec = argsplit(mhlproc, &file, &vecp);
206 vec[vecp++] = mh_xstrdup("-form");
207 vec[vecp++] = mh_xstrdup(form);
208 vec[vecp++] = mh_xstrdup("-nobody");
209 vec[vecp++] = getcpy(ct->c_file);
210
211 /*
212 * If we've specified -(no)moreproc,
213 * then just pass that along.
214 */
215 if (nomore || concatsw) {
216 vec[vecp++] = mh_xstrdup("-nomoreproc");
217 } else if (progsw) {
218 vec[vecp++] = mh_xstrdup("-moreproc");
219 vec[vecp++] = mh_xstrdup(progsw);
220 }
221 vec[vecp] = NULL;
222
223 fflush (stdout);
224
225 child_id = fork();
226 switch (child_id) {
227 case NOTOK:
228 adios ("fork", "unable to");
229 /* NOTREACHED */
230
231 case OK:
232 execvp (file, vec);
233 fprintf (stderr, "unable to exec ");
234 perror (mhlproc);
235 _exit(1);
236 /* NOTREACHED */
237
238 default:
239 pidcheck(pidwait(child_id, NOTOK));
240 break;
241 }
242
243 arglist_free(file, vec);
244 }
245
246
247 /*
248 * Switching routine. Call the correct routine
249 * based on content type.
250 */
251
252 static int
253 show_switch (CT ct, int alternate, int concatsw, int textonly, int inlineonly,
254 struct format *fmt)
255 {
256 switch (ct->c_type) {
257 case CT_MULTIPART:
258 return show_multi (ct, alternate, concatsw, textonly,
259 inlineonly, fmt);
260
261 case CT_MESSAGE:
262 switch (ct->c_subtype) {
263 case MESSAGE_PARTIAL:
264 return show_partial (ct, alternate);
265
266 case MESSAGE_EXTERNAL:
267 return show_external (ct, alternate, concatsw, textonly,
268 inlineonly, fmt);
269
270 case MESSAGE_RFC822:
271 return show_message_rfc822 (ct, alternate, fmt);
272
273 /*
274 * Treat unknown message types as equivalent to
275 * application/octet-stream for now
276 */
277 default:
278 return show_content (ct, alternate, textonly,
279 inlineonly, fmt);
280 }
281
282 case CT_TEXT:
283 return show_text (ct, alternate, concatsw, fmt);
284
285 case CT_AUDIO:
286 case CT_IMAGE:
287 case CT_VIDEO:
288 case CT_APPLICATION:
289 default:
290 return show_content (ct, alternate, textonly, inlineonly, fmt);
291 }
292
293 return 0; /* NOT REACHED */
294 }
295
296
297 /*
298 * Generic method for displaying content
299 */
300
301 static int
302 show_content (CT ct, int alternate, int textonly, int inlineonly,
303 struct format *fmt)
304 {
305 char *cp;
306 CI ci = &ct->c_ctinfo;
307
308 /*
309 * If we're here, we are not a text type. So we don't need to check
310 * the content-type.
311 */
312
313 if (textonly || (inlineonly && !is_inline(ct))) {
314 output_marker(ct, fmt, 1);
315 return OK;
316 }
317
318 /* Check for invo_name-show-type[/subtype] */
319 if ((cp = context_find_by_type ("show", ci->ci_type, ci->ci_subtype)))
320 return show_content_aux (ct, alternate, cp, NULL, fmt);
321
322 if ((cp = ct->c_showproc))
323 return show_content_aux (ct, alternate, cp, NULL, fmt);
324
325 /* complain if we are not a part of a multipart/alternative */
326 if (!alternate)
327 content_error (NULL, ct, "don't know how to display content");
328
329 return NOTOK;
330 }
331
332
333 /*
334 * Parse the display string for displaying generic content
335 */
336
337 int
338 show_content_aux (CT ct, int alternate, char *cp, char *cracked, struct format *fmt)
339 {
340 int fd;
341 int xstdin = 0, xlist = 0;
342 char *file = NULL, buffer[NMH_BUFSIZ];
343
344 if (!ct->c_ceopenfnx) {
345 if (!alternate)
346 content_error (NULL, ct, "don't know how to decode content");
347
348 return NOTOK;
349 }
350
351 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
352 return NOTOK;
353 if (ct->c_showproc && !strcmp (ct->c_showproc, "true"))
354 return OK;
355
356 if (! strcmp(invo_name, "mhshow") &&
357 ct->c_type == CT_TEXT && ct->c_subtype == TEXT_PLAIN) {
358 /* This has to be done after calling c_ceopenfnx, so
359 unfortunately the type checks are necessary without
360 some code rearrangement. And to make this really ugly,
361 only do it in mhshow, not mhfixmsg, mhn, or mhstore. */
362 if (convert_content_charset (ct, &file) == OK) {
363 (*ct->c_ceclosefnx) (ct);
364 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
365 return NOTOK;
366 } else {
367 char *charset = content_charset (ct);
368 inform("unable to convert character set%s%s from %s, continuing...",
369 ct->c_partno ? " of part " : "",
370 FENDNULL(ct->c_partno),
371 charset);
372 free (charset);
373 }
374 }
375
376 if (cracked) {
377 strncpy (buffer, cp, sizeof(buffer));
378 goto got_command;
379 }
380
381 if (parse_display_string (ct, cp, &xstdin, &xlist, file, buffer,
382 sizeof(buffer) - 1, 0)) {
383 inform("Buffer overflow constructing show command, continuing...");
384 return NOTOK;
385 }
386
387 got_command:
388 return show_content_aux2 (ct, alternate, cracked, buffer,
389 fd, xlist, xstdin, fmt);
390 }
391
392
393 /*
394 * Routine to actually display the content
395 */
396
397 static int
398 show_content_aux2 (CT ct, int alternate, char *cracked, char *buffer,
399 int fd, int xlist, int xstdin, struct format *fmt)
400 {
401 pid_t child_id;
402 int vecp;
403 char **vec, *file;
404
405 if (debugsw || cracked) {
406 fflush (stdout);
407
408 fprintf (stderr, "%s msg %s", cracked ? "storing" : "show",
409 ct->c_file);
410 if (ct->c_partno)
411 fprintf (stderr, " part %s", ct->c_partno);
412 if (cracked)
413 fprintf (stderr, " using command (cd %s; %s)\n", cracked, buffer);
414 else
415 fprintf (stderr, " using command %s\n", buffer);
416 }
417
418 if (xlist && fmt) {
419 output_marker(ct, fmt, 0);
420 }
421
422 /*
423 * If the command is a zero-length string, just write the output on
424 * stdout.
425 */
426
427 if (buffer[0] == '\0') {
428 char readbuf[BUFSIZ];
429 ssize_t cc;
430 char lastchar = '\n';
431
432 if (fd == NOTOK) {
433 inform("Cannot use NULL command to display content-type "
434 "%s/%s", ct->c_ctinfo.ci_type, ct->c_ctinfo.ci_subtype);
435 return NOTOK;
436 }
437
438 while ((cc = read(fd, readbuf, sizeof(readbuf))) > 0) {
439 if ((ssize_t) fwrite(readbuf, 1, cc, stdout) < cc) {
440 advise ("putline", "fwrite");
441 }
442 lastchar = readbuf[cc - 1];
443 }
444
445 if (cc < 0) {
446 advise("read", "while reading text content");
447 return NOTOK;
448 }
449
450 /*
451 * The MIME standards allow content to not have a trailing newline.
452 * But because we are (presumably) sending this to stdout, include
453 * a newline for text content if the final character was not a
454 * newline. Only do this for mhshow.
455 */
456
457 if (strcmp(invo_name, "mhshow") == 0 && ct->c_type == CT_TEXT &&
458 ct->c_subtype == TEXT_PLAIN && lastchar != '\n') {
459 putchar('\n');
460 }
461
462 fflush(stdout);
463
464 return OK;
465 }
466
467 vec = argsplit(buffer, &file, &vecp);
468 vec[vecp++] = NULL;
469
470 fflush (stdout);
471
472 child_id = fork();
473 switch (child_id) {
474 case NOTOK:
475 advise ("fork", "unable to");
476 (*ct->c_ceclosefnx) (ct);
477 return NOTOK;
478
479 case OK:
480 if (cracked) {
481 if (chdir (cracked) < 0) {
482 advise (cracked, "chdir");
483 }
484 }
485 if (!xstdin)
486 dup2 (fd, 0);
487 close (fd);
488 execvp (file, vec);
489 fprintf (stderr, "unable to exec ");
490 perror (buffer);
491 _exit(1);
492 /* NOTREACHED */
493
494 default: {
495 int status;
496 char *display_prog = vecp > 2 && vec[2][0] != '\0'
497 /* Copy the real display program name. This relies on the
498 specific construction of vec[] by argsplit(). */
499 ? vec[2]
500 : NULL;
501
502 pidcheck ((status = pidXwait (child_id, display_prog)));
503
504 arglist_free(file, vec);
505 if (fd != NOTOK)
506 (*ct->c_ceclosefnx) (ct);
507 return alternate ? OK : status;
508 }
509 }
510 }
511
512
513 /*
514 * show content of type "text"
515 */
516
517 static int
518 show_text (CT ct, int alternate, int concatsw, struct format *fmt)
519 {
520 char *cp, buffer[BUFSIZ];
521 CI ci = &ct->c_ctinfo;
522
523 /* Check for invo_name-show-type[/subtype] */
524 if ((cp = context_find_by_type ("show", ci->ci_type, ci->ci_subtype)))
525 return show_content_aux (ct, alternate, cp, NULL, fmt);
526
527 /*
528 * Use default method if content is text/plain, or if
529 * if it is not a text part of a multipart/alternative
530 */
531 if (!alternate || ct->c_subtype == TEXT_PLAIN) {
532 if (concatsw) {
533 if (ct->c_termproc)
534 snprintf(buffer, sizeof(buffer), "%%lcat");
535 else
536 snprintf(buffer, sizeof(buffer), "%%l");
537 } else
538 snprintf (buffer, sizeof(buffer), "%%l%s %%F", progsw ? progsw :
539 moreproc && *moreproc ? moreproc : DEFAULT_PAGER);
540 cp = (ct->c_showproc = mh_xstrdup(buffer));
541 return show_content_aux (ct, alternate, cp, NULL, fmt);
542 }
543
544 return NOTOK;
545 }
546
547
548 /*
549 * show message body of type "multipart"
550 */
551
552 static int
553 show_multi (CT ct, int alternate, int concatsw, int textonly, int inlineonly,
554 struct format *fmt)
555 {
556 char *cp;
557 CI ci = &ct->c_ctinfo;
558
559 /* Check for invo_name-show-type[/subtype] */
560 if ((cp = context_find_by_type ("show", ci->ci_type, ci->ci_subtype)))
561 return show_multi_aux (ct, alternate, cp, fmt);
562
563 if ((cp = ct->c_showproc)) {
564 return show_multi_aux (ct, alternate, cp, fmt);
565 }
566
567 /*
568 * Use default method to display this multipart content. Even
569 * unknown types are displayable, since they're treated as mixed
570 * per RFC 2046.
571 */
572 return show_multi_internal (ct, alternate, concatsw, textonly,
573 inlineonly, fmt);
574 }
575
576
577 /*
578 * show message body of subtypes of multipart that
579 * we understand directly (mixed, alternate, etc...)
580 */
581
582 static int
583 show_multi_internal (CT ct, int alternate, int concatsw, int textonly,
584 int inlineonly, struct format *fmt)
585 {
586 int alternating, nowalternate, result;
587 struct multipart *m = (struct multipart *) ct->c_ctparams;
588 struct part *part;
589 bool request_matched;
590 bool display_success;
591 bool mult_alt_done;
592 int ret;
593 CT p;
594
595 alternating = 0;
596 nowalternate = alternate;
597
598 if (ct->c_subtype == MULTI_ALTERNATE) {
599 nowalternate = 1;
600 alternating = 1;
601 }
602
603 /*
604 * alternate -> we are a part inside a multipart/alternative
605 * alternating -> we are a multipart/alternative
606 */
607
608 result = NOTOK;
609 request_matched = false;
610 display_success = false;
611 mult_alt_done = false;
612
613 for (part = m->mp_parts; part; part = part->mp_next) {
614 p = part->mp_part;
615
616 /* while looking for the right displayable alternative, we
617 * use a looser search criterion than we do after finding it.
618 * specifically, while still looking, part_ok() will match
619 * "parent" parts (e.g. "-part 2" where 2 is a high-level
620 * multipart). after finding it, we use part_exact() to only
621 * choose a part that was requested explicitly.
622 */
623 if ((part_exact(p) && type_ok(p, 1)) ||
624 (!mult_alt_done && part_ok (p) && type_ok (p, 1))) {
625
626 int inneresult;
627
628 inneresult = show_switch (p, nowalternate, concatsw, textonly,
629 inlineonly, fmt);
630 switch (inneresult) {
631 case NOTOK: /* hard display error */
632 request_matched = true;
633 if (alternate && !alternating) {
634 result = NOTOK;
635 goto out;
636 }
637 continue;
638
639 case DONE: /* found no match on content type */
640 continue;
641
642 case OK: /* display successful */
643 request_matched = true;
644 display_success = true;
645 result = OK;
646
647 /* if we got success on a sub-part of
648 * multipart/alternative, we're done, unless
649 * there's a chance an explicit part should be
650 * matched later in the alternatives. */
651 if (alternating) {
652 mult_alt_done = true;
653 } else if (alternate) {
654 alternate = nowalternate = 0;
655 }
656 continue;
657 }
658 break;
659 }
660 }
661
662 /* we're supposed to be displaying at least something from a
663 * multipart/alternative. if we've had parts to consider, and
664 * we've had no success, then we should complain. we shouldn't
665 * complain if none of the parts matched any -part or -type option.
666 */
667 if (alternating && request_matched && !display_success) {
668 /* if we're ourselves an alternate. don't complain yet. */
669 if (!alternate)
670 content_error (NULL, ct, "don't know how to display any of the contents");
671 result = NOTOK;
672 }
673
674 out:
675 /* if no parts matched what was requested, there can't have been
676 * any display errors. we report DONE rather than OK. */
677 ret = request_matched ? result : DONE;
678 return ret;
679 }
680
681
682 /*
683 * Parse display string for multipart content
684 * and use external program to display it.
685 */
686
687 static int
688 show_multi_aux (CT ct, int alternate, char *cp, struct format *fmt)
689 {
690 /* xstdin is only used in the call to parse_display_string():
691 its value is ignored in the function. */
692 int xstdin = 0, xlist = 0;
693 char *file = NULL, buffer[BUFSIZ];
694 struct multipart *m = (struct multipart *) ct->c_ctparams;
695 struct part *part;
696 CT p;
697
698 for (part = m->mp_parts; part; part = part->mp_next) {
699 p = part->mp_part;
700
701 if (!p->c_ceopenfnx) {
702 if (!alternate)
703 content_error (NULL, p, "don't know how to decode content");
704 return NOTOK;
705 }
706
707 if (p->c_storage == NULL) {
708 if ((*p->c_ceopenfnx) (p, &file) == NOTOK)
709 return NOTOK;
710
711 p->c_storage = mh_xstrdup(FENDNULL(file));
712
713 if (p->c_showproc && !strcmp (p->c_showproc, "true"))
714 return OK;
715 (*p->c_ceclosefnx) (p);
716 }
717 }
718
719 if (parse_display_string (ct, cp, &xstdin, &xlist, file,
720 buffer, sizeof(buffer) - 1, 1)) {
721 inform("Buffer overflow constructing show command, continuing...");
722 return NOTOK;
723 }
724
725 return show_content_aux2 (ct, alternate, NULL, buffer, NOTOK, xlist, 0, fmt);
726 }
727
728
729 /*
730 * show content of type "message/rfc822"
731 */
732
733 static int
734 show_message_rfc822 (CT ct, int alternate, struct format *fmt)
735 {
736 char *cp;
737 CI ci = &ct->c_ctinfo;
738
739 /* Check for invo_name-show-type[/subtype] */
740 if ((cp = context_find_by_type ("show", ci->ci_type, ci->ci_subtype)))
741 return show_content_aux (ct, alternate, cp, NULL, fmt);
742
743 if ((cp = ct->c_showproc))
744 return show_content_aux (ct, alternate, cp, NULL, fmt);
745
746 /* default method for message/rfc822 */
747 if (ct->c_subtype == MESSAGE_RFC822) {
748 cp = (ct->c_showproc = mh_xstrdup("%pshow -file %F"));
749 return show_content_aux (ct, alternate, cp, NULL, fmt);
750 }
751
752 /* complain if we are not a part of a multipart/alternative */
753 if (!alternate)
754 content_error (NULL, ct, "don't know how to display content");
755
756 return NOTOK;
757 }
758
759
760 /*
761 * Show content of type "message/partial".
762 */
763
764 static int
765 show_partial (CT ct, int alternate)
766 {
767 NMH_UNUSED (alternate);
768
769 content_error (NULL, ct,
770 "in order to display this message, you must reassemble it");
771 return NOTOK;
772 }
773
774
775 /*
776 * Show content of type "message/external".
777 *
778 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
779 */
780
781 static int
782 show_external (CT ct, int alternate, int concatsw, int textonly, int inlineonly,
783 struct format *fmt)
784 {
785 struct exbody *e = (struct exbody *) ct->c_ctparams;
786 CT p = e->eb_content;
787
788 if (!type_ok (p, 0))
789 return OK;
790
791 return show_switch (p, alternate, concatsw, textonly, inlineonly, fmt);
792 }
793
794
795 static int
796 parse_display_string (CT ct, char *cp, int *xstdin, int *xlist,
797 char *file, char *buffer, size_t buflen,
798 int multipart)
799 {
800 int len;
801 bool quoted = false;
802 char *bp = buffer, *pp;
803 CI ci = &ct->c_ctinfo;
804
805 bp[0] = bp[buflen] = '\0';
806
807 for ( ; *cp && buflen > 0; cp++) {
808 if (*cp == '%') {
809 pp = bp;
810
811 switch (*++cp) {
812 case 'a':
813 /* insert parameters from Content-Type field */
814 {
815 PM pm;
816 char *s = "";
817
818 for (pm = ci->ci_first_pm; pm; pm = pm->pm_next) {
819 snprintf (bp, buflen, "%s%s=\"%s\"", s, pm->pm_name,
820 get_param_value(pm, '?'));
821 len = strlen (bp);
822 bp += len;
823 buflen -= len;
824 s = " ";
825 }
826 }
827 break;
828
829 case 'd':
830 /* insert content description */
831 if (ct->c_descr) {
832 char *s;
833
834 s = trimcpy (ct->c_descr);
835 strncpy (bp, s, buflen);
836 free (s);
837 }
838 break;
839
840 case 'e':
841 /* no longer implemented */
842 break;
843
844 case 'F':
845 /* %f, and stdin is terminal not content */
846 *xstdin = 1;
847 /* FALLTHRU */
848
849 case 'f':
850 if (multipart) {
851 /* insert filename(s) containing content */
852 struct multipart *m = (struct multipart *) ct->c_ctparams;
853 struct part *part;
854 char *s = "";
855 CT p;
856
857 for (part = m->mp_parts; part; part = part->mp_next) {
858 p = part->mp_part;
859
860 snprintf (bp, buflen, "%s%s", s, p->c_storage);
861 len = strlen (bp);
862 bp += len;
863 buflen -= len;
864 s = " ";
865 }
866 } else {
867 /* insert filename containing content */
868 snprintf (bp, buflen, "%s", file);
869
870 /*
871 * Old comments below are left here for posterity.
872 * This was/is tricky.
873 */
874 /* since we've quoted the file argument, set things up
875 * to look past it, to avoid problems with the quoting
876 * logic below. (I know, I should figure out what's
877 * broken with the quoting logic, but..)
878 */
879 /*
880 * Here's the email that submitted the patch with
881 * the comment above:
882 * https://www.mail-archive.com/nmh-workers@mhost.com/
883 * msg00288.html
884 * I can't tell from that exactly what was broken,
885 * beyond misquoting of the filename. The profile
886 * had appearances of %F both with and without quotes.
887 * The unquoted ones should have been quoted by the
888 * code below.
889 * The fix was to always quote the filename. But
890 * that broke '%F' because it expanded to ''filename''.
891 */
892 /*
893 * Old comments above are left here for posterity.
894 * The quoting below should work properly now.
895 */
896 }
897 break;
898
899 case 'p':
900 /* No longer supported */
901 /* FALLTHRU */
902
903 case 'l':
904 /* display listing prior to displaying content */
905 *xlist = !nolist;
906 break;
907
908 case 's':
909 /* insert subtype of content */
910 strncpy (bp, ci->ci_subtype, buflen);
911 break;
912
913 case '%':
914 /* insert character % */
915 goto raw;
916
917 case '{' : {
918 const char *closing_brace = strchr(cp, '}');
919
920 if (closing_brace) {
921 const size_t param_len = closing_brace - cp - 1;
922 char *param = mh_xmalloc(param_len + 1);
923 char *value;
924
925 (void) strncpy(param, cp + 1, param_len);
926 param[param_len] = '\0';
927 value = get_param(ci->ci_first_pm, param, '?', 0);
928 free(param);
929
930 cp += param_len + 1; /* Skip both braces, too. */
931
932 if (value) {
933 /* %{param} is set in the Content-Type header.
934 After the break below, quote it if necessary. */
935 (void) strncpy(bp, value, buflen);
936 free(value);
937 } else {
938 /* %{param} not found, so skip it completely. cp
939 was advanced above. */
940 continue;
941 }
942 } else {
943 /* This will get confused if there are multiple %{}'s,
944 but its real purpose is to avoid doing bad things
945 above if a closing brace wasn't found. */
946 inform("no closing brace for display string escape %s, continuing...",
947 cp);
948 }
949 break;
950 }
951
952 default:
953 *bp++ = *--cp;
954 *bp = '\0';
955 buflen--;
956 continue;
957 }
958 len = strlen (bp);
959 bp += len;
960 buflen -= len;
961 *bp = '\0';
962
963 /* Did we actually insert something? */
964 if (bp != pp) {
965 /* Insert single quote if not inside quotes already */
966 if (!quoted && buflen) {
967 len = strlen (pp);
968 memmove (pp + 1, pp, len+1);
969 *pp++ = '\'';
970 buflen--;
971 bp++;
972 quoted = true;
973 }
974 /* Escape existing quotes */
975 while ((pp = strchr (pp, '\'')) && buflen > 3) {
976 len = strlen (pp++);
977 if (quoted) {
978 /* Quoted. Let this quote close that quoting.
979 Insert an escaped quote to replace it and
980 another quote to reopen quoting, which will be
981 closed below. */
982 memmove (pp + 2, pp, len);
983 *pp++ = '\\';
984 *pp++ = '\'';
985 buflen -= 2;
986 bp += 2;
987 quoted = false;
988 } else {
989 /* Not quoted. This should not be reached with
990 the current code, but handle the condition
991 in case the code changes. Just escape the
992 quote. */
993 memmove (pp, pp-1, len+1);
994 *(pp++-1) = '\\';
995 buflen--;
996 bp++;
997 }
998 }
999 /* If pp is still set, that means we ran out of space. */
1000 if (pp)
1001 buflen = 0;
1002 /* Close quoting. */
1003 if (quoted && buflen) {
1004 /* See if we need to close the quote by looking
1005 for an odd number of unescaped close quotes in
1006 the remainder of the display string. */
1007 int found_quote = 0, escaped = 0;
1008 char *c;
1009
1010 for (c = cp+1; *c; ++c) {
1011 if (*c == '\\') {
1012 escaped = ! escaped;
1013 } else {
1014 if (escaped) {
1015 escaped = 0;
1016 } else {
1017 if (*c == '\'') {
1018 found_quote = ! found_quote;
1019 }
1020 }
1021 }
1022 }
1023 if (! found_quote) {
1024 *bp++ = '\'';
1025 buflen--;
1026 quoted = false;
1027 }
1028 }
1029 }
1030 } else {
1031 raw:
1032 *bp++ = *cp;
1033 buflen--;
1034
1035 if (*cp == '\'')
1036 quoted = !quoted;
1037 }
1038
1039 *bp = '\0';
1040 }
1041
1042 if (buflen <= 0 ||
1043 (ct->c_termproc && buflen <= strlen(ct->c_termproc))) {
1044 /* content_error would provide a more useful error message
1045 * here, except that if we got overrun, it probably would
1046 * too.
1047 */
1048 return NOTOK;
1049 }
1050
1051 /* use charset string to modify display method */
1052 if (ct->c_termproc) {
1053 char term[BUFSIZ];
1054
1055 strncpy (term, buffer, sizeof(term));
1056 snprintf (buffer, buflen, ct->c_termproc, term);
1057 }
1058
1059 return OK;
1060 }
1061
1062
1063 int
1064 convert_charset (CT ct, char *dest_charset, int *message_mods)
1065 {
1066 char *src_charset = content_charset (ct);
1067 int status = OK;
1068
1069 if (strcasecmp (src_charset, dest_charset)) {
1070 #ifdef HAVE_ICONV
1071 iconv_t conv_desc = NULL;
1072 char *dest;
1073 int fd = -1;
1074 char **file = NULL;
1075 FILE **fp = NULL;
1076 size_t begin;
1077 size_t end;
1078 bool opened_input_file = false;
1079 char src_buffer[BUFSIZ];
1080 size_t dest_buffer_size = BUFSIZ;
1081 char *dest_buffer = mh_xmalloc(dest_buffer_size);
1082 HF hf;
1083 char *tempfile;
1084 int fromutf8 = !strcasecmp(src_charset, "UTF-8");
1085
1086 if ((conv_desc = iconv_open (dest_charset, src_charset)) ==
1087 (iconv_t) -1) {
1088 inform("Can't convert %s to %s", src_charset, dest_charset);
1089 free (src_charset);
1090 return NOTOK;
1091 }
1092
1093 if ((tempfile = m_mktemp2 (NULL, invo_name, &fd, NULL)) == NULL) {
1094 die("unable to create temporary file in %s",
1095 get_temp_dir());
1096 }
1097 dest = mh_xstrdup(tempfile);
1098
1099 if (ct->c_cefile.ce_file) {
1100 file = &ct->c_cefile.ce_file;
1101 fp = &ct->c_cefile.ce_fp;
1102 begin = end = 0;
1103 } else if (ct->c_file) {
1104 file = &ct->c_file;
1105 fp = &ct->c_fp;
1106 begin = (size_t) ct->c_begin;
1107 end = (size_t) ct->c_end;
1108 } /* else no input file: shouldn't happen */
1109
1110 if (file && *file && fp) {
1111 if (! *fp) {
1112 if ((*fp = fopen (*file, "r")) == NULL) {
1113 advise (*file, "unable to open for reading");
1114 status = NOTOK;
1115 } else {
1116 opened_input_file = true;
1117 }
1118 }
1119 }
1120
1121 if (fp && *fp) {
1122 size_t inbytes;
1123 size_t bytes_to_read =
1124 end > 0 && end > begin ? end - begin : sizeof src_buffer;
1125
1126 fseeko (*fp, begin, SEEK_SET);
1127 while ((inbytes = fread (src_buffer, 1,
1128 min (bytes_to_read, sizeof src_buffer),
1129 *fp)) > 0) {
1130 ICONV_CONST char *ib = src_buffer;
1131 char *ob = dest_buffer;
1132 size_t outbytes = dest_buffer_size;
1133 size_t outbytes_before = outbytes;
1134
1135 if (end > 0) bytes_to_read -= inbytes;
1136
1137 iconv_start:
1138 if (iconv (conv_desc, &ib, &inbytes, &ob, &outbytes) ==
1139 (size_t) -1) {
1140 if (errno == E2BIG) {
1141 /*
1142 * Bump up the buffer by at least a factor of 2
1143 * over what we need.
1144 */
1145 size_t bumpup = inbytes * 2, ob_off = ob - dest_buffer;
1146 dest_buffer_size += bumpup;
1147 dest_buffer = mh_xrealloc(dest_buffer,
1148 dest_buffer_size);
1149 ob = dest_buffer + ob_off;
1150 outbytes += bumpup;
1151 outbytes_before += bumpup;
1152 goto iconv_start;
1153 }
1154 if (errno == EINVAL) {
1155 /* middle of multi-byte sequence */
1156 if (write (fd, dest_buffer, outbytes_before - outbytes) < 0) {
1157 advise (dest, "write");
1158 }
1159 fseeko (*fp, -inbytes, SEEK_CUR);
1160 if (end > 0) bytes_to_read += inbytes;
1161 /* inform("convert_charset: EINVAL"); */
1162 continue;
1163 }
1164 if (errno == EILSEQ) {
1165 /* invalid multi-byte sequence */
1166 if (fromutf8) {
1167 for (++ib, --inbytes;
1168 inbytes > 0 &&
1169 (((unsigned char) *ib) & 0xc0) == 0x80;
1170 ++ib, --inbytes)
1171 continue;
1172 } else {
1173 ib++; inbytes--; /* skip it */
1174 }
1175 (*ob++) = '?'; outbytes --;
1176 /* inform("convert_charset: EILSEQ"); */
1177 goto iconv_start;
1178 }
1179 inform("convert_charset: errno = %d", errno);
1180 status = NOTOK;
1181 break;
1182 }
1183
1184 if (write (fd, dest_buffer, outbytes_before - outbytes) < 0) {
1185 advise (dest, "write");
1186 }
1187 }
1188
1189 if (opened_input_file) {
1190 fclose (*fp);
1191 *fp = NULL;
1192 }
1193 }
1194
1195 iconv_close (conv_desc);
1196 close (fd);
1197
1198 if (status == OK) {
1199 /* Replace the decoded file with the converted one. */
1200 if (ct->c_cefile.ce_file) {
1201 if (ct->c_cefile.ce_unlink) {
1202 (void) m_unlink (ct->c_cefile.ce_file);
1203 }
1204 free (ct->c_cefile.ce_file);
1205 }
1206 ct->c_cefile.ce_file = dest;
1207 ct->c_cefile.ce_unlink = 1;
1208
1209 ++*message_mods;
1210
1211 /* Update ct->c_ctline. */
1212 if (ct->c_ctline) {
1213 char *ctline = concat(" ", ct->c_ctinfo.ci_type, "/",
1214 ct->c_ctinfo.ci_subtype, NULL);
1215 char *outline;
1216
1217 replace_param(&ct->c_ctinfo.ci_first_pm,
1218 &ct->c_ctinfo.ci_last_pm, "charset",
1219 dest_charset, 0);
1220 outline = output_params(LEN(TYPE_FIELD) + 1 + strlen(ctline),
1221 ct->c_ctinfo.ci_first_pm, NULL, 0);
1222 if (outline) {
1223 ctline = add(outline, ctline);
1224 free(outline);
1225 }
1226
1227 free (ct->c_ctline);
1228 ct->c_ctline = ctline;
1229 } /* else no CT line, which is odd */
1230
1231 /* Update Content-Type header field. */
1232 for (hf = ct->c_first_hf; hf; hf = hf->next) {
1233 if (! strcasecmp (TYPE_FIELD, hf->name)) {
1234 char *ctline = concat (ct->c_ctline, "\n", NULL);
1235
1236 free (hf->value);
1237 hf->value = ctline;
1238 break;
1239 }
1240 }
1241 } else {
1242 (void) m_unlink (dest);
1243 }
1244 free(dest_buffer);
1245 #else /* ! HAVE_ICONV */
1246 NMH_UNUSED (message_mods);
1247
1248 inform("Can't convert %s to %s without iconv", src_charset,
1249 dest_charset);
1250 errno = ENOSYS;
1251 status = NOTOK;
1252 #endif /* ! HAVE_ICONV */
1253 }
1254
1255 free (src_charset);
1256 return status;
1257 }
1258
1259
1260 static int
1261 convert_content_charset (CT ct, char **file)
1262 {
1263 int status = OK;
1264
1265 #ifdef HAVE_ICONV
1266 /* Using current locale, see if the content needs to be converted. */
1267
1268 /* content_charset() cannot return NULL. */
1269 char *src_charset = content_charset (ct);
1270
1271 if (! check_charset (src_charset, strlen (src_charset))) {
1272 int unused = 0;
1273
1274 char *dest_charset = getcpy (get_charset ());
1275
1276 if (convert_charset (ct, dest_charset, &unused) == 0) {
1277 *file = ct->c_cefile.ce_file;
1278 } else {
1279 status = NOTOK;
1280 }
1281
1282 free (dest_charset);
1283 }
1284 free (src_charset);
1285 #else /* ! HAVE_ICONV */
1286 NMH_UNUSED (ct);
1287 NMH_UNUSED (file);
1288 #endif /* ! HAVE_ICONV */
1289
1290 return status;
1291 }
1292
1293 /*
1294 * Compile our format string and save any parameters we care about.
1295 */
1296
1297 #define DEFAULT_HEADER "[ Message %{folder}%<{folder}:%>%(msg) ]"
1298 #define DEFAULT_MARKER "[ part %{part} - %{content-type} - " \
1299 "%<{description}%{description}" \
1300 "%?{cdispo-filename}%{cdispo-filename}" \
1301 "%|%{ctype-name}%> " \
1302 "%(kilo(size))B %<(unseen)\\(suppressed\\)%> ]"
1303
1304 static struct format *
1305 compile_header(char *form)
1306 {
1307 struct format *fmt;
1308 char *fmtstring;
1309 struct comp *comp = NULL;
1310 unsigned int bucket;
1311
1312 fmtstring = new_fs(form, NULL, DEFAULT_HEADER);
1313
1314 (void) fmt_compile(fmtstring, &fmt, 1);
1315 free_fs();
1316
1317 while ((comp = fmt_nextcomp(comp, &bucket)) != NULL) {
1318 if (strcasecmp(comp->c_name, "folder") == 0) {
1319 folder_comp = comp;
1320 }
1321 }
1322
1323 return fmt;
1324 }
1325
1326 static struct format *
1327 compile_marker(char *form)
1328 {
1329 struct format *fmt;
1330 char *fmtstring;
1331 struct comp *comp = NULL;
1332 unsigned int bucket;
1333 struct param_comp_list *pc_entry;
1334
1335 fmtstring = new_fs(form, NULL, DEFAULT_MARKER);
1336
1337 (void) fmt_compile(fmtstring, &fmt, 1);
1338 free_fs();
1339
1340 /*
1341 * Things we care about:
1342 *
1343 * part - Part name (e.g., 1.1)
1344 * content-type - Content-Type
1345 * description - Content-Description
1346 * disposition - Content-Disposition (inline, attachment)
1347 * ctype-<param> - Content-Type parameter
1348 * cdispo-<param> - Content-Disposition parameter
1349 */
1350
1351 while ((comp = fmt_nextcomp(comp, &bucket)) != NULL) {
1352 if (strcasecmp(comp->c_name, "part") == 0) {
1353 part_comp = comp;
1354 } else if (strcasecmp(comp->c_name, "content-type") == 0) {
1355 ctype_comp = comp;
1356 } else if (strcasecmp(comp->c_name, "description") == 0) {
1357 description_comp = comp;
1358 } else if (strcasecmp(comp->c_name, "disposition") == 0) {
1359 dispo_comp = comp;
1360 } else if (strncasecmp(comp->c_name, "ctype-", 6) == 0 &&
1361 strlen(comp->c_name) > 6) {
1362 NEW(pc_entry);
1363 pc_entry->param = mh_xstrdup(comp->c_name + 6);
1364 pc_entry->comp = comp;
1365 pc_entry->next = ctype_pc_list;
1366 ctype_pc_list = pc_entry;
1367 } else if (strncasecmp(comp->c_name, "cdispo-", 7) == 0 &&
1368 strlen(comp->c_name) > 7) {
1369 NEW(pc_entry);
1370 pc_entry->param = mh_xstrdup(comp->c_name + 7);
1371 pc_entry->comp = comp;
1372 pc_entry->next = dispo_pc_list;
1373 dispo_pc_list = pc_entry;
1374 }
1375 }
1376
1377 return fmt;
1378 }
1379
1380 /*
1381 * Output on stdout an appropriate marker for this content, using mh-format
1382 */
1383
1384 static void
1385 output_header(CT ct, struct format *fmt)
1386 {
1387 charstring_t outbuf = charstring_create (BUFSIZ);
1388 int dat[5] = { 0 };
1389 char *endp;
1390 int message = 0;
1391
1392 if (folder_comp)
1393 folder_comp->c_text = getcpy(folder);
1394
1395 if (ct->c_file && *ct->c_file) {
1396 message = strtol(ct->c_file, &endp, 10);
1397 if (*endp) message = 0;
1398 dat[0] = message;
1399 }
1400
1401 /* it would be nice to populate dat[2], for %(size) here,
1402 * but it's not available. it might also be nice to know
1403 * if the message originally had any mime parts or not -- but
1404 * there's also no record of that. (except for MIME-version:)
1405 */
1406
1407 fmt_scan(fmt, outbuf, BUFSIZ, dat, NULL);
1408
1409 fputs(charstring_buffer (outbuf), stdout);
1410 charstring_free (outbuf);
1411
1412 fmt_freecomptext();
1413 }
1414
1415 static void
1416 output_marker(CT ct, struct format *fmt, int hidden)
1417 {
1418 charstring_t outbuf = charstring_create (BUFSIZ);
1419 struct param_comp_list *pcentry;
1420 int partsize;
1421 int message = 0;
1422 char *endp;
1423 int dat[5] = { 0 };
1424
1425 /*
1426 * Grab any items we care about.
1427 */
1428
1429 if (ctype_comp && ct->c_ctinfo.ci_type) {
1430 ctype_comp->c_text = concat(ct->c_ctinfo.ci_type, "/",
1431 ct->c_ctinfo.ci_subtype, NULL);
1432 }
1433
1434 if (part_comp && ct->c_partno) {
1435 part_comp->c_text = mh_xstrdup(ct->c_partno);
1436 }
1437
1438 if (description_comp && ct->c_descr) {
1439 description_comp->c_text = mh_xstrdup(ct->c_descr);
1440 }
1441
1442 if (dispo_comp && ct->c_dispo_type) {
1443 dispo_comp->c_text = mh_xstrdup(ct->c_dispo_type);
1444 }
1445
1446 for (pcentry = ctype_pc_list; pcentry != NULL; pcentry = pcentry->next) {
1447 pcentry->comp->c_text = get_param(ct->c_ctinfo.ci_first_pm,
1448 pcentry->param, '?', 0);
1449 }
1450
1451 for (pcentry = dispo_pc_list; pcentry != NULL; pcentry = pcentry->next) {
1452 pcentry->comp->c_text = get_param(ct->c_dispo_first,
1453 pcentry->param, '?', 0);
1454 }
1455
1456 if (ct->c_cesizefnx)
1457 partsize = (*ct->c_cesizefnx) (ct);
1458 else
1459 partsize = ct->c_end - ct->c_begin;
1460
1461 if (ct->c_file && *ct->c_file) {
1462 message = strtol(ct->c_file, &endp, 10);
1463 if (*endp) message = 0;
1464 dat[0] = message;
1465 }
1466 dat[2] = partsize;
1467
1468 /* make the part's hidden aspect available by overloading the
1469 * %(unseen) function. make the part's size available via %(size).
1470 * see comments in h/fmt_scan.h.
1471 */
1472 dat[4] = hidden;
1473
1474 fmt_scan(fmt, outbuf, BUFSIZ, dat, NULL);
1475
1476 fputs(charstring_buffer (outbuf), stdout);
1477 charstring_free (outbuf);
1478
1479 fmt_freecomptext();
1480 }
1481
1482 /*
1483 * Reset (and free) any of the saved marker text
1484 */
1485
1486 static void
1487 free_markercomps(void)
1488 {
1489 struct param_comp_list *pc_entry, *pc2;
1490
1491 folder_comp = NULL;
1492 part_comp = NULL;
1493 ctype_comp = NULL;
1494 description_comp = NULL;
1495 dispo_comp = NULL;
1496
1497 for (pc_entry = ctype_pc_list; pc_entry != NULL; ) {
1498 free(pc_entry->param);
1499 pc2 = pc_entry->next;
1500 free(pc_entry);
1501 pc_entry = pc2;
1502 }
1503
1504 for (pc_entry = dispo_pc_list; pc_entry != NULL; ) {
1505 free(pc_entry->param);
1506 pc2 = pc_entry->next;
1507 free(pc_entry);
1508 pc_entry = pc2;
1509 }
1510 }
1511
1512 /*
1513 * Exit if the display process returned with a nonzero exit code, or terminated
1514 * with a SIGQUIT signal.
1515 */
1516
1517 static int
1518 pidcheck (int status)
1519 {
1520 if ((status & 0xff00) == 0xff00 || (status & 0x007f) != SIGQUIT)
1521 return status;
1522
1523 fflush (stdout);
1524 fflush (stderr);
1525 done (1);
1526 return 1;
1527 }