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