2 ** mhshowsbr.c -- routines to display the contents of MIME messages
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
11 #include <h/signals.h>
17 #include <h/mhparse.h>
34 void content_error(char *, CT, char *, ...);
35 void flush_errors(void);
38 int list_switch(CT, int, int, int, int);
39 int list_content(CT, int, int, int, int);
44 void show_all_messages(CT *);
45 int show_content_aux(CT, int, char *, char *);
50 static void show_single_message(CT, char *);
51 static void DisplayMsgHeader(CT, char *);
52 static int show_switch(CT, int);
53 static int show_content(CT, int);
54 static int show_content_aux2(CT, int, char *, char *, int, int, int);
55 static int show_text(CT, int);
56 static int show_multi(CT, int);
57 static int show_multi_internal(CT, int);
58 static int show_multi_aux(CT, int, char *);
59 static int show_message_rfc822(CT, int);
60 static int show_partial(CT, int);
61 static int show_external(CT, int);
65 ** Top level entry point to show/display a group of messages
69 show_all_messages(CT *cts)
74 ** If form is not specified, then get default form
75 ** for showing headers of MIME messages.
78 formsw = getcpy(etcpath("mhl.headers"));
81 ** If form is "mhl.null", suppress display of header.
83 if (strcmp(formsw, "mhl.null")==0)
86 for (ctp = cts; *ctp; ctp++) {
89 /* if top-level type is ok, then display message */
91 show_single_message(ct, formsw);
97 ** Entry point to show/display a single message
101 show_single_message(CT ct, char *form)
107 ** Allow user executable bit so that temporary directories created by
108 ** the viewer (e.g., lynx) are going to be accessible
110 umask(ct->c_umask & ~(0100));
113 ** If you have a format file, then display
114 ** the message headers.
117 DisplayMsgHeader(ct, form);
121 /* Show the body of the message */
128 if (ct->c_ceclosefnx)
129 (*ct->c_ceclosefnx) (ct);
131 /* block a few signals */
133 sigaddset(&set, SIGHUP);
134 sigaddset(&set, SIGINT);
135 sigaddset(&set, SIGQUIT);
136 sigaddset(&set, SIGTERM);
137 sigprocmask(SIG_BLOCK, &set, &oset);
139 while (wait(&status) != NOTOK) {
144 /* reset the signal mask */
145 sigprocmask(SIG_SETMASK, &oset, &set);
153 ** Use mhl to show the header fields
156 DisplayMsgHeader(CT ct, char *form)
164 vec[vecp++] = "-form";
166 vec[vecp++] = "-nobody";
167 vec[vecp++] = ct->c_file;
172 switch (child_id = fork()) {
174 adios("fork", "unable to");
179 fprintf(stderr, "unable to exec ");
192 ** Switching routine. Call the correct routine
193 ** based on content type.
197 show_switch(CT ct, int alternate)
199 switch (ct->c_type) {
201 return show_multi(ct, alternate);
205 switch (ct->c_subtype) {
206 case MESSAGE_PARTIAL:
207 return show_partial(ct, alternate);
210 case MESSAGE_EXTERNAL:
211 return show_external(ct, alternate);
216 return show_message_rfc822(ct, alternate);
222 return show_text(ct, alternate);
229 return show_content(ct, alternate);
233 adios(NULL, "unknown content type %d", ct->c_type);
237 return 0; /* NOT REACHED */
242 ** Generic method for displaying content
246 show_content(CT ct, int alternate)
248 char *cp, buffer[BUFSIZ];
249 CI ci = &ct->c_ctinfo;
251 /* Check for mhshow-show-type/subtype */
252 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
253 invo_name, ci->ci_type, ci->ci_subtype);
254 if ((cp = context_find(buffer)) && *cp != '\0')
255 return show_content_aux(ct, alternate, cp, NULL);
257 /* Check for mhshow-show-type */
258 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
259 if ((cp = context_find(buffer)) && *cp != '\0')
260 return show_content_aux(ct, alternate, cp, NULL);
262 if ((cp = ct->c_showproc))
263 return show_content_aux(ct, alternate, cp, NULL);
265 /* complain if we are not a part of a multipart/alternative */
267 content_error(NULL, ct, "don't know how to display content");
274 ** Parse the display string for displaying generic content
277 show_content_aux(CT ct, int alternate, char *cp, char *cracked)
279 int fd, len, buflen, quoted;
281 char *bp, *pp, *file, buffer[BUFSIZ];
282 CI ci = &ct->c_ctinfo;
284 if (!ct->c_ceopenfnx) {
286 content_error(NULL, ct,
287 "don't know how to decode content");
293 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
295 if (ct->c_showproc && strcmp(ct->c_showproc, "true")==0)
296 return (alternate ? DONE : OK);
302 strncpy(buffer, cp, sizeof(buffer));
306 /* get buffer ready to go */
308 buflen = sizeof(buffer) - 1;
309 bp[0] = bp[buflen] = '\0';
312 /* Now parse display string */
313 for ( ; *cp && buflen > 0; cp++) {
319 /* insert parameters from Content-Type field */
324 for (ap = ci->ci_attrs, ep = ci->ci_values;
326 snprintf(bp, buflen, "%s%s=\"%s\"",
339 strncpy(bp, ct->c_charset, buflen);
344 /* insert content description */
348 s = trimcpy(ct->c_descr);
349 strncpy(bp, s, buflen);
355 /* %f, and stdin is terminal not content */
360 /* insert filename containing content */
361 snprintf(bp, buflen, "'%s'", file);
363 ** since we've quoted the file argument,
364 ** set things up to look past it, to avoid
365 ** problems with the quoting logic below.
366 ** (I know, I should figure out what's
367 ** broken with the quoting logic, but..)
377 ** display listing prior to displaying content
383 /* insert subtype of content */
384 strncpy(bp, ci->ci_subtype, buflen);
388 /* insert character % */
401 /* Did we actually insert something? */
404 ** Insert single quote if not inside quotes
407 if (!quoted && buflen) {
409 memmove(pp + 1, pp, len);
414 /* Escape existing quotes */
415 while ((pp = strchr(pp, '\'')) && buflen > 3) {
417 memmove(pp + 3, pp, len);
425 ** If pp is still set, that means we ran
430 if (!quoted && buflen) {
448 return show_content_aux2(ct, alternate, cracked, buffer,
454 ** Routine to actually display the content
457 show_content_aux2(CT ct, int alternate, char *cracked,
458 char *buffer, int fd, int xlist, int xstdin)
463 pidcheck(pidwait(xpid, NOTOK));
467 if (debugsw || cracked) {
470 fprintf(stderr, "%s msg %s", cracked ? "storing" : "show",
473 fprintf(stderr, " part %s", ct->c_partno);
475 fprintf(stderr, " using command (cd %s; %s)\n",
478 fprintf(stderr, " using command %s\n", buffer);
482 if (ct->c_type == CT_MULTIPART)
483 list_content(ct, -1, 1, 0, 0);
485 list_switch(ct, -1, 1, 0, 0);
490 switch (child_id = fork()) {
492 advise("fork", "unable to");
493 (*ct->c_ceclosefnx) (ct);
502 execlp("/bin/sh", "/bin/sh", "-c", buffer, NULL);
503 fprintf(stderr, "unable to exec ");
509 pidcheck(pidXwait(child_id, NULL));
512 (*ct->c_ceclosefnx) (ct);
513 return (alternate ? DONE : OK);
519 ** show content of type "text"
523 show_text(CT ct, int alternate)
525 char *cp, buffer[BUFSIZ];
526 CI ci = &ct->c_ctinfo;
528 /* Check for mhshow-show-type/subtype */
529 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
530 invo_name, ci->ci_type, ci->ci_subtype);
531 if ((cp = context_find(buffer)) && *cp != '\0')
532 return show_content_aux(ct, alternate, cp, NULL);
534 /* Check for mhshow-show-type */
535 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
536 if ((cp = context_find(buffer)) && *cp != '\0')
537 return show_content_aux(ct, alternate, cp, NULL);
540 ** Use default method if content is text/plain, or if
541 ** if it is not a text part of a multipart/alternative
543 if (!alternate || ct->c_subtype == TEXT_PLAIN) {
544 if (!check_charset(ct->c_charset, strlen(ct->c_charset))) {
545 snprintf(buffer, sizeof(buffer), "%%liconv -f '%s'",
548 snprintf(buffer, sizeof(buffer), "%%lcat");
550 cp = (ct->c_showproc = getcpy(buffer));
551 return show_content_aux(ct, alternate, cp, NULL);
559 ** show message body of type "multipart"
563 show_multi(CT ct, int alternate)
565 char *cp, buffer[BUFSIZ];
566 CI ci = &ct->c_ctinfo;
568 /* Check for mhshow-show-type/subtype */
569 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
570 invo_name, ci->ci_type, ci->ci_subtype);
571 if ((cp = context_find(buffer)) && *cp != '\0')
572 return show_multi_aux(ct, alternate, cp);
574 /* Check for mhshow-show-type */
575 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
576 if ((cp = context_find(buffer)) && *cp != '\0')
577 return show_multi_aux(ct, alternate, cp);
579 if ((cp = ct->c_showproc))
580 return show_multi_aux(ct, alternate, cp);
583 ** Use default method to display this multipart content
584 ** if it is not a (nested) part of a multipart/alternative,
585 ** or if it is one of the known subtypes of multipart.
587 if (!alternate || ct->c_subtype != MULTI_UNKNOWN)
588 return show_multi_internal(ct, alternate);
595 ** show message body of subtypes of multipart that
596 ** we understand directly (mixed, alternate, etc...)
600 show_multi_internal(CT ct, int alternate)
602 int alternating, nowalternate, result;
603 struct multipart *m = (struct multipart *) ct->c_ctparams;
608 nowalternate = alternate;
610 if (ct->c_subtype == MULTI_ALTERNATE) {
616 ** Other possible multipart types are:
617 ** - multipart/parallel
619 ** - multipart/digest
620 ** - unknown subtypes of multipart (treat as mixed per rfc2046)
624 ** alternate -> we are a part inside an multipart/alternative
625 ** alternating -> we are a multipart/alternative
628 result = alternate ? NOTOK : OK;
630 for (part = m->mp_parts; part; part = part->mp_next) {
633 if (part_ok(p, 1) && type_ok(p, 1)) {
636 inneresult = show_switch(p, nowalternate);
637 switch (inneresult) {
639 if (alternate && !alternating) {
652 alternate = nowalternate = 0;
662 if (alternating && !part) {
664 content_error(NULL, ct, "don't know how to display any of the contents");
674 ** Parse display string for multipart content
675 ** and use external program to display it.
679 show_multi_aux(CT ct, int alternate, char *cp)
681 int len, buflen, quoted;
683 char *bp, *pp, *file, buffer[BUFSIZ];
684 struct multipart *m = (struct multipart *) ct->c_ctparams;
686 CI ci = &ct->c_ctinfo;
689 for (part = m->mp_parts; part; part = part->mp_next) {
692 if (!p->c_ceopenfnx) {
694 content_error(NULL, p, "don't know how to decode content");
698 if (p->c_storage == NULL) {
700 if ((*p->c_ceopenfnx) (p, &file) == NOTOK)
703 /* I'm not sure if this is necessary? */
704 p->c_storage = getcpy(file);
706 if (p->c_showproc && strcmp(p->c_showproc, "true")==0)
707 return (alternate ? DONE : OK);
708 (*p->c_ceclosefnx) (p);
714 /* get buffer ready to go */
716 buflen = sizeof(buffer) - 1;
717 bp[0] = bp[buflen] = '\0';
720 /* Now parse display string */
721 for ( ; *cp && buflen > 0; cp++) {
726 /* insert parameters from Content-Type field */
731 for (ap = ci->ci_attrs, ep = ci->ci_values;
733 snprintf(bp, buflen, "%s%s=\"%s\"",
746 strncpy(bp, ct->c_charset, buflen);
751 /* insert content description */
755 s = trimcpy(ct->c_descr);
756 strncpy(bp, s, buflen);
763 /* insert filename(s) containing content */
767 for (part = m->mp_parts; part;
768 part = part->mp_next) {
771 snprintf(bp, buflen, "%s'%s'",
779 ** set our starting pointer back to bp,
780 ** to avoid requoting the filenames we
789 ** display listing prior to displaying content
795 /* insert subtype of content */
796 strncpy(bp, ci->ci_subtype, buflen);
800 /* insert character % */
813 /* Did we actually insert something? */
816 ** Insert single quote if not inside quotes
819 if (!quoted && buflen) {
821 memmove(pp + 1, pp, len);
826 /* Escape existing quotes */
827 while ((pp = strchr(pp, '\'')) && buflen > 3) {
829 memmove(pp + 3, pp, len);
837 ** If pp is still set, that means we ran
842 if (!quoted && buflen) {
859 return show_content_aux2(ct, alternate, NULL, buffer,
865 ** show content of type "message/rfc822"
869 show_message_rfc822(CT ct, int alternate)
871 char *cp, buffer[BUFSIZ];
872 CI ci = &ct->c_ctinfo;
874 /* Check for mhshow-show-type/subtype */
875 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
876 invo_name, ci->ci_type, ci->ci_subtype);
877 if ((cp = context_find(buffer)) && *cp != '\0')
878 return show_content_aux(ct, alternate, cp, NULL);
880 /* Check for mhshow-show-type */
881 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
882 if ((cp = context_find(buffer)) && *cp != '\0')
883 return show_content_aux(ct, alternate, cp, NULL);
885 if ((cp = ct->c_showproc))
886 return show_content_aux(ct, alternate, cp, NULL);
888 /* default method for message/rfc822 */
889 if (ct->c_subtype == MESSAGE_RFC822) {
890 cp = (ct->c_showproc = getcpy("%lshow -file %F"));
891 return show_content_aux(ct, alternate, cp, NULL);
894 /* complain if we are not a part of a multipart/alternative */
896 content_error(NULL, ct, "don't know how to display content");
903 ** Show content of type "message/partial".
907 show_partial(CT ct, int alternate)
909 content_error(NULL, ct,
910 "in order to display this message, you must reassemble it");
916 ** Show how to retrieve content of type "message/external".
919 show_external(CT ct, int alternate)
926 msg = add("You need to fetch the contents yourself:", NULL);
927 ap = ct->c_ctinfo.ci_attrs;
928 ep = ct->c_ctinfo.ci_values;
929 for (; *ap; ap++, ep++) {
930 msg = add(concat("\n\t", *ap, ": ", *ep, NULL), msg);
932 if (!(fp = fopen(ct->c_file, "r"))) {
933 adios(ct->c_file, "unable to open");
935 fseek(fp, ct->c_begin, SEEK_SET);
936 while (!feof(fp) && ftell(fp) < ct->c_end) {
937 if (!fgets(buf, sizeof buf, fp)) {
938 adios(ct->c_file, "unable to read");
940 *strchr(buf, '\n') = '\0';
941 msg = add(concat("\n\t", buf, NULL), msg);
944 content_error(NULL, ct, msg);