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