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