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