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