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