]> diplodocus.org Git - nmh/blob - uip/mhshowsbr.c
Fixed m_getfld() to not return EOF indicator, so that files need
[nmh] / uip / mhshowsbr.c
1
2 /*
3 * mhshowsbr.c -- routines to display the contents of MIME messages
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <fcntl.h>
12 #include <h/signals.h>
13 #include <h/md5.h>
14 #include <setjmp.h>
15 #include <h/mts.h>
16 #include <h/tws.h>
17 #include <h/mime.h>
18 #include <h/mhparse.h>
19 #include <h/utils.h>
20 #ifdef HAVE_ICONV
21 # include <iconv.h>
22 #endif /* ! HAVE_ICONV */
23
24 extern int debugsw;
25
26 int pausesw = 1;
27 int serialsw = 0;
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 pid_t xpid = 0;
37
38 static sigjmp_buf intrenv;
39
40
41 /* mhmisc.c */
42 int part_ok (CT, int);
43 int type_ok (CT, int);
44 void content_error (char *, CT, char *, ...);
45 void flush_errors (void);
46
47 /* mhlistsbr.c */
48 int list_switch (CT, int, int, int, int);
49 int list_content (CT, int, int, int, int);
50
51 /*
52 * prototypes
53 */
54 void show_all_messages (CT *);
55 int show_content_aux (CT, int, int, char *, char *);
56
57 /*
58 * static prototypes
59 */
60 static void show_single_message (CT, char *);
61 static void DisplayMsgHeader (CT, char *);
62 static int show_switch (CT, int, int);
63 static int show_content (CT, int, int);
64 static int show_content_aux2 (CT, int, int, char *, char *, int, int, int, int,
65 int);
66 static int show_text (CT, int, int);
67 static int show_multi (CT, int, int);
68 static int show_multi_internal (CT, int, int);
69 static int show_multi_aux (CT, int, int, char *);
70 static int show_message_rfc822 (CT, int, int);
71 static int show_partial (CT, int, int);
72 static int show_external (CT, int, int);
73 static int parse_display_string (CT, char *, int *, int *, int *, int *, char *,
74 char *, size_t, int multipart);
75 static int convert_content_charset (CT, char **);
76 static const char *parameter_value (CI, const char *);
77 static void intrser (int);
78
79
80 /*
81 * Top level entry point to show/display a group of messages
82 */
83
84 void
85 show_all_messages (CT *cts)
86 {
87 CT ct, *ctp;
88
89 /*
90 * If form is not specified, then get default form
91 * for showing headers of MIME messages.
92 */
93 if (!formsw)
94 formsw = getcpy (etcpath ("mhl.headers"));
95
96 /*
97 * If form is "mhl.null", suppress display of header.
98 */
99 if (!strcmp (formsw, "mhl.null"))
100 formsw = NULL;
101
102 for (ctp = cts; *ctp; ctp++) {
103 ct = *ctp;
104
105 /* if top-level type is ok, then display message */
106 if (type_ok (ct, 1))
107 show_single_message (ct, formsw);
108 }
109 }
110
111
112 /*
113 * Entry point to show/display a single message
114 */
115
116 static void
117 show_single_message (CT ct, char *form)
118 {
119 sigset_t set, oset;
120
121 int status;
122
123 /* Allow user executable bit so that temporary directories created by
124 * the viewer (e.g., lynx) are going to be accessible */
125 umask (ct->c_umask & ~(0100));
126
127 /*
128 * If you have a format file, then display
129 * the message headers.
130 */
131 if (form)
132 DisplayMsgHeader(ct, form);
133 else
134 xpid = 0;
135
136 /* Show the body of the message */
137 show_switch (ct, 1, 0);
138
139 if (ct->c_fp) {
140 fclose (ct->c_fp);
141 ct->c_fp = NULL;
142 }
143 if (ct->c_ceclosefnx)
144 (*ct->c_ceclosefnx) (ct);
145
146 /* block a few signals */
147 sigemptyset (&set);
148 sigaddset (&set, SIGHUP);
149 sigaddset (&set, SIGINT);
150 sigaddset (&set, SIGQUIT);
151 sigaddset (&set, SIGTERM);
152 sigprocmask (SIG_BLOCK, &set, &oset);
153
154 while (wait (&status) != NOTOK) {
155 pidcheck (status);
156 continue;
157 }
158
159 /* reset the signal mask */
160 sigprocmask (SIG_SETMASK, &oset, &set);
161
162 xpid = 0;
163 flush_errors ();
164 }
165
166
167 /*
168 * Use the mhlproc to show the header fields
169 */
170
171 static void
172 DisplayMsgHeader (CT ct, char *form)
173 {
174 pid_t child_id;
175 int i, vecp;
176 char **vec;
177 char *file;
178
179 vec = argsplit(mhlproc, &file, &vecp);
180 vec[vecp++] = getcpy("-form");
181 vec[vecp++] = getcpy(form);
182 vec[vecp++] = getcpy("-nobody");
183 vec[vecp++] = getcpy(ct->c_file);
184
185 /*
186 * If we've specified -(no)moreproc,
187 * then just pass that along.
188 */
189 if (nomore) {
190 vec[vecp++] = getcpy("-nomoreproc");
191 } else if (progsw) {
192 vec[vecp++] = getcpy("-moreproc");
193 vec[vecp++] = getcpy(progsw);
194 }
195 vec[vecp] = NULL;
196
197 fflush (stdout);
198
199 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
200 sleep (5);
201
202 switch (child_id) {
203 case NOTOK:
204 adios ("fork", "unable to");
205 /* NOTREACHED */
206
207 case OK:
208 execvp (file, vec);
209 fprintf (stderr, "unable to exec ");
210 perror (mhlproc);
211 _exit (-1);
212 /* NOTREACHED */
213
214 default:
215 xpid = -child_id;
216 break;
217 }
218
219 arglist_free(file, vec);
220 }
221
222
223 /*
224 * Switching routine. Call the correct routine
225 * based on content type.
226 */
227
228 static int
229 show_switch (CT ct, int serial, int alternate)
230 {
231 switch (ct->c_type) {
232 case CT_MULTIPART:
233 return show_multi (ct, serial, alternate);
234
235 case CT_MESSAGE:
236 switch (ct->c_subtype) {
237 case MESSAGE_PARTIAL:
238 return show_partial (ct, serial, alternate);
239
240 case MESSAGE_EXTERNAL:
241 return show_external (ct, serial, alternate);
242
243 case MESSAGE_RFC822:
244 default:
245 return show_message_rfc822 (ct, serial, alternate);
246 }
247
248 case CT_TEXT:
249 return show_text (ct, serial, alternate);
250
251 case CT_AUDIO:
252 case CT_IMAGE:
253 case CT_VIDEO:
254 case CT_APPLICATION:
255 return show_content (ct, serial, alternate);
256
257 default:
258 adios (NULL, "unknown content type %d", ct->c_type);
259 }
260
261 return 0; /* NOT REACHED */
262 }
263
264
265 /*
266 * Generic method for displaying content
267 */
268
269 static int
270 show_content (CT ct, int serial, int alternate)
271 {
272 char *cp, buffer[BUFSIZ];
273 CI ci = &ct->c_ctinfo;
274
275 /* Check for invo_name-show-type/subtype */
276 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
277 invo_name, ci->ci_type, ci->ci_subtype);
278 if ((cp = context_find (buffer)) && *cp != '\0')
279 return show_content_aux (ct, serial, alternate, cp, NULL);
280
281 /* Check for invo_name-show-type */
282 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
283 if ((cp = context_find (buffer)) && *cp != '\0')
284 return show_content_aux (ct, serial, alternate, cp, NULL);
285
286 if ((cp = ct->c_showproc))
287 return show_content_aux (ct, serial, alternate, cp, NULL);
288
289 /* complain if we are not a part of a multipart/alternative */
290 if (!alternate)
291 content_error (NULL, ct, "don't know how to display content");
292
293 return NOTOK;
294 }
295
296
297 /*
298 * Parse the display string for displaying generic content
299 */
300
301 int
302 show_content_aux (CT ct, int serial, int alternate, char *cp, char *cracked)
303 {
304 int fd;
305 int xstdin = 0, xlist = 0, xpause = 0, xtty = 0;
306 char *file, buffer[BUFSIZ];
307
308 if (!ct->c_ceopenfnx) {
309 if (!alternate)
310 content_error (NULL, ct, "don't know how to decode content");
311
312 return NOTOK;
313 }
314
315 file = NULL;
316 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
317 return NOTOK;
318 if (ct->c_showproc && !strcmp (ct->c_showproc, "true"))
319 return (alternate ? DONE : OK);
320
321 if (! strcmp(invo_name, "mhshow") &&
322 ct->c_type == CT_TEXT && ct->c_subtype == TEXT_PLAIN) {
323 /* This has to be done after calling c_ceopenfnx, so
324 unfortunately the type checks are necessary without
325 some code rearrangement. And to make this really ugly,
326 only do it in mhshow, not mhfixmsg, mhn, or mhstore. */
327 if (convert_content_charset (ct, &file) != OK) {
328 admonish (NULL, "unable to convert character set%s to %s",
329 ct->c_partno ? "of part " : "",
330 ct->c_partno ? ct->c_partno : "",
331 content_charset (ct));
332 }
333 }
334
335 if (cracked) {
336 strncpy (buffer, cp, sizeof(buffer));
337 goto got_command;
338 }
339
340 if (parse_display_string (ct, cp, &xstdin, &xlist, &xpause, &xtty, file,
341 buffer, sizeof(buffer) - 1, 0)) {
342 admonish (NULL, "Buffer overflow constructing show command!\n");
343 return NOTOK;
344 }
345
346 got_command:
347 return show_content_aux2 (ct, serial, alternate, cracked, buffer,
348 fd, xlist, xpause, xstdin, xtty);
349 }
350
351
352 /*
353 * Routine to actually display the content
354 */
355
356 static int
357 show_content_aux2 (CT ct, int serial, int alternate, char *cracked, char *buffer,
358 int fd, int xlist, int xpause, int xstdin, int xtty)
359 {
360 pid_t child_id;
361 int i, vecp;
362 char **vec, *file;
363
364 if (debugsw || cracked) {
365 fflush (stdout);
366
367 fprintf (stderr, "%s msg %s", cracked ? "storing" : "show",
368 ct->c_file);
369 if (ct->c_partno)
370 fprintf (stderr, " part %s", ct->c_partno);
371 if (cracked)
372 fprintf (stderr, " using command (cd %s; %s)\n", cracked, buffer);
373 else
374 fprintf (stderr, " using command %s\n", buffer);
375 }
376
377 if (xpid < 0 || (xtty && xpid)) {
378 if (xpid < 0)
379 xpid = -xpid;
380 pidcheck(pidwait (xpid, NOTOK));
381 xpid = 0;
382 }
383
384 if (xlist) {
385 char prompt[BUFSIZ];
386
387 if (ct->c_type == CT_MULTIPART)
388 list_content (ct, -1, 1, 0, 0);
389 else
390 list_switch (ct, -1, 1, 0, 0);
391
392 if (xpause && isatty (fileno (stdout))) {
393 int intr;
394 SIGNAL_HANDLER istat;
395
396 if (SOprintf ("Press <return> to show content..."))
397 printf ("Press <return> to show content...");
398
399 istat = SIGNAL (SIGINT, intrser);
400 if ((intr = sigsetjmp (intrenv, 1)) == OK) {
401 fflush (stdout);
402 prompt[0] = 0;
403 read (fileno (stdout), prompt, sizeof(prompt));
404 }
405 SIGNAL (SIGINT, istat);
406 if (intr != OK || prompt[0] == 'n') {
407 (*ct->c_ceclosefnx) (ct);
408 return (alternate ? DONE : NOTOK);
409 }
410 if (prompt[0] == 'q') done(OK);
411 }
412 }
413
414 vec = argsplit(buffer, &file, &vecp);
415 vec[vecp++] = NULL;
416
417 fflush (stdout);
418
419 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
420 sleep (5);
421 switch (child_id) {
422 case NOTOK:
423 advise ("fork", "unable to");
424 (*ct->c_ceclosefnx) (ct);
425 return NOTOK;
426
427 case OK:
428 if (cracked)
429 chdir (cracked);
430 if (!xstdin)
431 dup2 (fd, 0);
432 close (fd);
433 execvp (file, vec);
434 fprintf (stderr, "unable to exec ");
435 perror ("/bin/sh");
436 _exit (-1);
437 /* NOTREACHED */
438
439 default:
440 arglist_free(file, vec);
441
442 if (!serial) {
443 ct->c_pid = child_id;
444 if (xtty)
445 xpid = child_id;
446 } else {
447 pidcheck (pidXwait (child_id, NULL));
448 }
449
450 if (fd != NOTOK)
451 (*ct->c_ceclosefnx) (ct);
452 return (alternate ? DONE : OK);
453 }
454 }
455
456
457 /*
458 * show content of type "text"
459 */
460
461 static int
462 show_text (CT ct, int serial, int alternate)
463 {
464 char *cp, buffer[BUFSIZ];
465 CI ci = &ct->c_ctinfo;
466
467 /* Check for invo_name-show-type/subtype */
468 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
469 invo_name, ci->ci_type, ci->ci_subtype);
470 if ((cp = context_find (buffer)) && *cp != '\0')
471 return show_content_aux (ct, serial, alternate, cp, NULL);
472
473 /* Check for invo_name-show-type */
474 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
475 if ((cp = context_find (buffer)) && *cp != '\0')
476 return show_content_aux (ct, serial, alternate, cp, NULL);
477
478 /*
479 * Use default method if content is text/plain, or if
480 * if it is not a text part of a multipart/alternative
481 */
482 if (!alternate || ct->c_subtype == TEXT_PLAIN) {
483 snprintf (buffer, sizeof(buffer), "%%p%s '%%F'", progsw ? progsw :
484 moreproc && *moreproc ? moreproc : DEFAULT_PAGER);
485 cp = (ct->c_showproc = add (buffer, NULL));
486 return show_content_aux (ct, serial, alternate, cp, NULL);
487 }
488
489 return NOTOK;
490 }
491
492
493 /*
494 * show message body of type "multipart"
495 */
496
497 static int
498 show_multi (CT ct, int serial, int alternate)
499 {
500 char *cp, buffer[BUFSIZ];
501 CI ci = &ct->c_ctinfo;
502
503 /* Check for invo_name-show-type/subtype */
504 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
505 invo_name, ci->ci_type, ci->ci_subtype);
506 if ((cp = context_find (buffer)) && *cp != '\0')
507 return show_multi_aux (ct, serial, alternate, cp);
508
509 /* Check for invo_name-show-type */
510 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
511 if ((cp = context_find (buffer)) && *cp != '\0')
512 return show_multi_aux (ct, serial, alternate, cp);
513
514 if ((cp = ct->c_showproc))
515 return show_multi_aux (ct, serial, alternate, cp);
516
517 /*
518 * Use default method to display this multipart content. Even
519 * unknown types are displayable, since they're treated as mixed
520 * per RFC 2046.
521 */
522 return show_multi_internal (ct, serial, alternate);
523 }
524
525
526 /*
527 * show message body of subtypes of multipart that
528 * we understand directly (mixed, alternate, etc...)
529 */
530
531 static int
532 show_multi_internal (CT ct, int serial, int alternate)
533 {
534 int alternating, nowalternate, nowserial, result;
535 struct multipart *m = (struct multipart *) ct->c_ctparams;
536 struct part *part;
537 CT p;
538 sigset_t set, oset;
539
540 alternating = 0;
541 nowalternate = alternate;
542
543 if (ct->c_subtype == MULTI_PARALLEL) {
544 nowserial = serialsw;
545 } else if (ct->c_subtype == MULTI_ALTERNATE) {
546 nowalternate = 1;
547 alternating = 1;
548 nowserial = serial;
549 } else {
550 /*
551 * multipart/mixed
552 * mutlipart/digest
553 * unknown subtypes of multipart (treat as mixed per rfc2046)
554 */
555 nowserial = serial;
556 }
557
558 /* block a few signals */
559 if (!nowserial) {
560 sigemptyset (&set);
561 sigaddset (&set, SIGHUP);
562 sigaddset (&set, SIGINT);
563 sigaddset (&set, SIGQUIT);
564 sigaddset (&set, SIGTERM);
565 sigprocmask (SIG_BLOCK, &set, &oset);
566 }
567
568 /*
569 * alternate -> we are a part inside an multipart/alternative
570 * alternating -> we are a multipart/alternative
571 */
572
573 result = alternate ? NOTOK : OK;
574
575 for (part = m->mp_parts; part; part = part->mp_next) {
576 p = part->mp_part;
577
578 if (part_ok (p, 1) && type_ok (p, 1)) {
579 int inneresult;
580
581 inneresult = show_switch (p, nowserial, nowalternate);
582 switch (inneresult) {
583 case NOTOK:
584 if (alternate && !alternating) {
585 result = NOTOK;
586 goto out;
587 }
588 continue;
589
590 case OK:
591 case DONE:
592 if (alternating) {
593 result = DONE;
594 break;
595 }
596 if (alternate) {
597 alternate = nowalternate = 0;
598 if (result == NOTOK)
599 result = inneresult;
600 }
601 continue;
602 }
603 break;
604 }
605 }
606
607 if (alternating && !part) {
608 if (!alternate)
609 content_error (NULL, ct, "don't know how to display any of the contents");
610 result = NOTOK;
611 goto out;
612 }
613
614 if (serial && !nowserial) {
615 pid_t pid;
616 int kids;
617 int status;
618
619 kids = 0;
620 for (part = m->mp_parts; part; part = part->mp_next) {
621 p = part->mp_part;
622
623 if (p->c_pid > OK) {
624 if (kill (p->c_pid, 0) == NOTOK)
625 p->c_pid = 0;
626 else
627 kids++;
628 }
629 }
630
631 while (kids > 0 && (pid = wait (&status)) != NOTOK) {
632 pidcheck (status);
633
634 for (part = m->mp_parts; part; part = part->mp_next) {
635 p = part->mp_part;
636
637 if (xpid == pid)
638 xpid = 0;
639 if (p->c_pid == pid) {
640 p->c_pid = 0;
641 kids--;
642 break;
643 }
644 }
645 }
646 }
647
648 out:
649 if (!nowserial) {
650 /* reset the signal mask */
651 sigprocmask (SIG_SETMASK, &oset, &set);
652 }
653
654 return result;
655 }
656
657
658 /*
659 * Parse display string for multipart content
660 * and use external program to display it.
661 */
662
663 static int
664 show_multi_aux (CT ct, int serial, int alternate, char *cp)
665 {
666 /* xstdin is only used in the call to parse_display_string():
667 its value is ignored in the function. */
668 int xstdin = 0, xlist = 0, xpause = 0, xtty = 0;
669 char *file, buffer[BUFSIZ];
670 struct multipart *m = (struct multipart *) ct->c_ctparams;
671 struct part *part;
672 CT p;
673
674 for (part = m->mp_parts; part; part = part->mp_next) {
675 p = part->mp_part;
676
677 if (!p->c_ceopenfnx) {
678 if (!alternate)
679 content_error (NULL, p, "don't know how to decode content");
680 return NOTOK;
681 }
682
683 if (p->c_storage == NULL) {
684 file = NULL;
685 if ((*p->c_ceopenfnx) (p, &file) == NOTOK)
686 return NOTOK;
687
688 p->c_storage = add (file, NULL);
689
690 if (p->c_showproc && !strcmp (p->c_showproc, "true"))
691 return (alternate ? DONE : OK);
692 (*p->c_ceclosefnx) (p);
693 }
694 }
695
696 if (parse_display_string (ct, cp, &xstdin, &xlist, &xpause, &xtty, file,
697 buffer, sizeof(buffer) - 1, 1)) {
698 admonish (NULL, "Buffer overflow constructing show command!\n");
699 return NOTOK;
700 }
701
702 return show_content_aux2 (ct, serial, alternate, NULL, buffer,
703 NOTOK, xlist, xpause, 0, xtty);
704 }
705
706
707 /*
708 * show content of type "message/rfc822"
709 */
710
711 static int
712 show_message_rfc822 (CT ct, int serial, int alternate)
713 {
714 char *cp, buffer[BUFSIZ];
715 CI ci = &ct->c_ctinfo;
716
717 /* Check for invo_name-show-type/subtype */
718 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
719 invo_name, ci->ci_type, ci->ci_subtype);
720 if ((cp = context_find (buffer)) && *cp != '\0')
721 return show_content_aux (ct, serial, alternate, cp, NULL);
722
723 /* Check for invo_name-show-type */
724 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
725 if ((cp = context_find (buffer)) && *cp != '\0')
726 return show_content_aux (ct, serial, alternate, cp, NULL);
727
728 if ((cp = ct->c_showproc))
729 return show_content_aux (ct, serial, alternate, cp, NULL);
730
731 /* default method for message/rfc822 */
732 if (ct->c_subtype == MESSAGE_RFC822) {
733 cp = (ct->c_showproc = add ("%pshow -file '%F'", NULL));
734 return show_content_aux (ct, serial, alternate, cp, NULL);
735 }
736
737 /* complain if we are not a part of a multipart/alternative */
738 if (!alternate)
739 content_error (NULL, ct, "don't know how to display content");
740
741 return NOTOK;
742 }
743
744
745 /*
746 * Show content of type "message/partial".
747 */
748
749 static int
750 show_partial (CT ct, int serial, int alternate)
751 {
752 NMH_UNUSED (serial);
753 NMH_UNUSED (alternate);
754
755 content_error (NULL, ct,
756 "in order to display this message, you must reassemble it");
757 return NOTOK;
758 }
759
760
761 /*
762 * Show content of type "message/external".
763 *
764 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
765 */
766
767 static int
768 show_external (CT ct, int serial, int alternate)
769 {
770 struct exbody *e = (struct exbody *) ct->c_ctparams;
771 CT p = e->eb_content;
772
773 if (!type_ok (p, 0))
774 return OK;
775
776 return show_switch (p, serial, alternate);
777 }
778
779
780 static int
781 parse_display_string (CT ct, char *cp, int *xstdin, int *xlist, int *xpause,
782 int *xtty, char *file, char *buffer, size_t buflen,
783 int multipart) {
784 int len, quoted = 0;
785 char *bp = buffer, *pp;
786 CI ci = &ct->c_ctinfo;
787
788 bp[0] = bp[buflen] = '\0';
789
790 for ( ; *cp && buflen > 0; cp++) {
791 if (*cp == '%') {
792 pp = bp;
793
794 switch (*++cp) {
795 case 'a':
796 /* insert parameters from Content-Type field */
797 {
798 char **ap, **ep;
799 char *s = "";
800
801 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
802 snprintf (bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
803 len = strlen (bp);
804 bp += len;
805 buflen -= len;
806 s = " ";
807 }
808 }
809 break;
810
811 case 'd':
812 /* insert content description */
813 if (ct->c_descr) {
814 char *s;
815
816 s = trimcpy (ct->c_descr);
817 strncpy (bp, s, buflen);
818 free (s);
819 }
820 break;
821
822 case 'e':
823 /* exclusive execution */
824 *xtty = 1;
825 break;
826
827 case 'F':
828 /* %e, %f, and stdin is terminal not content */
829 *xstdin = 1;
830 *xtty = 1;
831 /* and fall... */
832
833 case 'f':
834 if (multipart) {
835 /* insert filename(s) containing content */
836 struct multipart *m = (struct multipart *) ct->c_ctparams;
837 struct part *part;
838 char *s = "";
839 CT p;
840
841 for (part = m->mp_parts; part; part = part->mp_next) {
842 p = part->mp_part;
843
844 /* Always quote the filename. I'm not sure if that's
845 right. And maybe not necessary: is this always
846 a temp file? If so, the quoting is harmless. */
847 snprintf (bp, buflen, "%s'%s'", s, p->c_storage);
848 len = strlen (bp);
849 bp += len;
850 buflen -= len;
851 s = " ";
852 }
853 } else {
854 /* insert filename containing content */
855 if (bp > buffer && *(bp-1) == '\'') {
856 /* Don't quote filename if it's already quoted. */
857 /* If there isn't a matching close quote, bail
858 out. */
859 if (*(cp+1) != '\'') {
860 adios(NULL, "%%f/%%F not properly escaped: %s%s\n",
861 buffer, cp);
862 }
863 snprintf (bp, buflen, "%s", file);
864 } else {
865 if (*(cp+1) != '\0' && *(cp+1) == '\'') {
866 adios(NULL, "%%f/%%F not properly escaped: %s%s\n",
867 buffer, cp);
868 }
869 snprintf (bp, buflen, "'%s'", file);
870 }
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 * That's why I added the condition above to not
890 * quote if the escape was wrapped with single
891 * quotes. It would be (much) better to rely on
892 * the quoting code below, but until I understand
893 * what is wrong with it, I won't do that.
894 */
895 len = strlen(bp);
896 bp += len;
897 buflen -= len;
898 }
899
900 /* set our starting pointer back to bp, to avoid
901 * requoting the filenames we just added
902 */
903 pp = bp;
904 break;
905
906 case 'p':
907 /* %l, and pause prior to displaying content */
908 *xpause = pausesw;
909 /* and fall... */
910
911 case 'l':
912 /* display listing prior to displaying content */
913 *xlist = !nolist;
914 break;
915
916 case 's':
917 /* insert subtype of content */
918 strncpy (bp, ci->ci_subtype, buflen);
919 break;
920
921 case '%':
922 /* insert character % */
923 goto raw;
924
925 case '{' : {
926 const char *closing_brace = strchr(cp, '}');
927
928 if (closing_brace) {
929 const size_t param_len = closing_brace - cp - 1;
930 char *param = mh_xmalloc(param_len + 1);
931 const char *value;
932
933 (void) strncpy(param, cp + 1, param_len);
934 param[param_len] = '\0';
935 value = parameter_value(ci, param);
936 free(param);
937
938 cp += param_len + 1; /* Skip both braces, too. */
939
940 if (value) {
941 /* %{param} is set in the Content-Type header.
942 After the break below, quote it if necessary. */
943 (void) strncpy(bp, value, buflen);
944 } else {
945 /* %{param} not found, so skip it completely. cp
946 was advanced above. */
947 continue;
948 }
949 } else {
950 /* This will get confused if there are multiple %{}'s,
951 but its real purpose is to avoid doing bad things
952 above if a closing brace wasn't found. */
953 admonish(NULL,
954 "no closing brace for display string escape %s",
955 cp);
956 }
957 break;
958 }
959
960 default:
961 *bp++ = *--cp;
962 *bp = '\0';
963 buflen--;
964 continue;
965 }
966 len = strlen (bp);
967 bp += len;
968 buflen -= len;
969
970 /* Did we actually insert something? */
971 if (bp != pp) {
972 /* Insert single quote if not inside quotes already */
973 if (!quoted && buflen) {
974 len = strlen (pp);
975 memmove (pp + 1, pp, len);
976 *pp++ = '\'';
977 buflen--;
978 bp++;
979 }
980 /* Escape existing quotes */
981 while ((pp = strchr (pp, '\'')) && buflen > 3) {
982 len = strlen (pp++);
983 memmove (pp + 3, pp, len);
984 *pp++ = '\\';
985 *pp++ = '\'';
986 *pp++ = '\'';
987 buflen -= 3;
988 bp += 3;
989 }
990 /* If pp is still set, that means we ran out of space. */
991 if (pp)
992 buflen = 0;
993 if (!quoted && buflen) {
994 *bp++ = '\'';
995 *bp = '\0';
996 buflen--;
997 }
998 }
999 } else {
1000 raw:
1001 *bp++ = *cp;
1002 *bp = '\0';
1003 buflen--;
1004
1005 if (*cp == '\'')
1006 quoted = !quoted;
1007 }
1008 }
1009
1010 if (buflen <= 0 ||
1011 (ct->c_termproc && buflen <= strlen(ct->c_termproc))) {
1012 /* content_error would provide a more useful error message
1013 * here, except that if we got overrun, it probably would
1014 * too.
1015 */
1016 return NOTOK;
1017 }
1018
1019 /* use charset string to modify display method */
1020 if (ct->c_termproc) {
1021 char term[BUFSIZ];
1022
1023 strncpy (term, buffer, sizeof(term));
1024 snprintf (buffer, buflen, ct->c_termproc, term);
1025 }
1026
1027 return OK;
1028 }
1029
1030
1031 int
1032 convert_charset (CT ct, char *dest_charset, int *message_mods) {
1033 char *src_charset = content_charset (ct);
1034 int status = OK;
1035
1036 /* norm_charmap() is case sensitive. */
1037 char *src_charset_u = upcase (src_charset);
1038 char *dest_charset_u = upcase (dest_charset);
1039 int different_charsets =
1040 strcmp (norm_charmap (src_charset), norm_charmap (dest_charset));
1041
1042 free (dest_charset_u);
1043 free (src_charset_u);
1044
1045 if (different_charsets) {
1046 #ifdef HAVE_ICONV
1047 iconv_t conv_desc = NULL;
1048 char *dest;
1049 int fd = -1;
1050 char **file = NULL;
1051 FILE **fp = NULL;
1052 size_t begin;
1053 size_t end;
1054 int opened_input_file = 0;
1055 char src_buffer[BUFSIZ];
1056 HF hf;
1057 char *tempfile;
1058
1059 if ((conv_desc = iconv_open (dest_charset, src_charset)) ==
1060 (iconv_t) -1) {
1061 advise (NULL, "Can't convert %s to %s", src_charset, dest_charset);
1062 return NOTOK;
1063 }
1064
1065 if ((tempfile = m_mktemp2 (NULL, invo_name, &fd, NULL)) == NULL) {
1066 adios (NULL, "unable to create temporary file in %s",
1067 get_temp_dir());
1068 }
1069 dest = add (tempfile, NULL);
1070
1071 if (ct->c_cefile.ce_file) {
1072 file = &ct->c_cefile.ce_file;
1073 fp = &ct->c_cefile.ce_fp;
1074 begin = end = 0;
1075 } else if (ct->c_file) {
1076 file = &ct->c_file;
1077 fp = &ct->c_fp;
1078 begin = (size_t) ct->c_begin;
1079 end = (size_t) ct->c_end;
1080 } /* else no input file: shouldn't happen */
1081
1082 if (file && *file && fp) {
1083 if (! *fp) {
1084 if ((*fp = fopen (*file, "r")) == NULL) {
1085 advise (*file, "unable to open for reading");
1086 status = NOTOK;
1087 } else {
1088 opened_input_file = 1;
1089 }
1090 }
1091 }
1092
1093 if (fp && *fp) {
1094 size_t inbytes;
1095 size_t bytes_to_read =
1096 end > 0 && end > begin ? end - begin : sizeof src_buffer;
1097
1098 fseeko (*fp, begin, SEEK_SET);
1099 while ((inbytes = fread (src_buffer, 1,
1100 min (bytes_to_read, sizeof src_buffer),
1101 *fp)) > 0) {
1102 char dest_buffer[BUFSIZ];
1103 ICONV_CONST char *ib = src_buffer;
1104 char *ob = dest_buffer;
1105 size_t outbytes = sizeof dest_buffer;
1106 size_t outbytes_before = outbytes;
1107
1108 if (end > 0) bytes_to_read -= inbytes;
1109
1110 if (iconv (conv_desc, &ib, &inbytes, &ob, &outbytes) ==
1111 (size_t) -1) {
1112 status = NOTOK;
1113 break;
1114 } else {
1115 write (fd, dest_buffer, outbytes_before - outbytes);
1116 }
1117 }
1118
1119 if (opened_input_file) {
1120 fclose (*fp);
1121 *fp = NULL;
1122 }
1123 }
1124
1125 iconv_close (conv_desc);
1126 close (fd);
1127
1128 if (status == OK) {
1129 /* Replace the decoded file with the converted one. */
1130 if (ct->c_cefile.ce_file) {
1131 if (ct->c_cefile.ce_unlink) {
1132 (void) m_unlink (ct->c_cefile.ce_file);
1133 }
1134 free (ct->c_cefile.ce_file);
1135 }
1136 ct->c_cefile.ce_file = dest;
1137 ct->c_cefile.ce_unlink = 1;
1138
1139 ++*message_mods;
1140
1141 /* Update ci_attrs. */
1142 src_charset = dest_charset;
1143
1144 /* Update ct->c_ctline. */
1145 if (ct->c_ctline) {
1146 char *ctline =
1147 update_attr (ct->c_ctline, "charset=", dest_charset);
1148
1149 free (ct->c_ctline);
1150 ct->c_ctline = ctline;
1151 } /* else no CT line, which is odd */
1152
1153 /* Update Content-Type header field. */
1154 for (hf = ct->c_first_hf; hf; hf = hf->next) {
1155 if (! strcasecmp (TYPE_FIELD, hf->name)) {
1156 char *ctline_less_newline =
1157 update_attr (hf->value, "charset=", dest_charset);
1158 char *ctline = concat (ctline_less_newline, "\n", NULL);
1159 free (ctline_less_newline);
1160
1161 free (hf->value);
1162 hf->value = ctline;
1163 break;
1164 }
1165 }
1166 } else {
1167 (void) m_unlink (dest);
1168 }
1169 #else /* ! HAVE_ICONV */
1170 NMH_UNUSED (message_mods);
1171
1172 advise (NULL, "Can't convert %s to %s without iconv", src_charset,
1173 dest_charset);
1174 status = NOTOK;
1175 #endif /* ! HAVE_ICONV */
1176 }
1177
1178 return status;
1179 }
1180
1181
1182 static int
1183 convert_content_charset (CT ct, char **file) {
1184 #ifdef HAVE_ICONV
1185 /* Using current locale, see if the content needs to be converted. */
1186
1187 /* content_charset() cannot return NULL. */
1188 char *charset = content_charset (ct);
1189
1190 if (! check_charset (charset, strlen (charset))) {
1191 int unused = 0;
1192
1193 if (convert_charset (ct, get_charset (), &unused) == 0) {
1194 *file = ct->c_cefile.ce_file;
1195 } else {
1196 return NOTOK;
1197 }
1198 }
1199 #else /* ! HAVE_ICONV */
1200 NMH_UNUSED (ct);
1201 NMH_UNUSED (file);
1202 #endif /* ! HAVE_ICONV */
1203
1204 return OK;
1205 }
1206
1207
1208 /*
1209 * If a Content-Type parameter named param exists, returns its value.
1210 * Otherwise, returns NULL.
1211 */
1212 static const char *
1213 parameter_value (CI ci, const char *param) {
1214 const char *value = NULL;
1215 char **ap, **vp;
1216
1217 for (ap = ci->ci_attrs, vp = ci->ci_values; *ap; ++ap, ++vp) {
1218 if (! strcasecmp (*ap, param)) {
1219 value = *vp;
1220 break;
1221 }
1222 }
1223
1224 return value;
1225 }
1226
1227
1228 static void
1229 intrser (int i)
1230 {
1231 NMH_UNUSED (i);
1232
1233 putchar ('\n');
1234 siglongjmp (intrenv, DONE);
1235 }