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