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