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>
16 #include <h/mhparse.h>
34 void content_error(char *, CT, char *, ...);
35 void flush_errors(void);
38 int list_switch(CT, int, int, int);
39 int list_content(CT, 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 (!type_ok(ct, 1)) { /* top-level type */
96 printf(">>> Message %s\n\n", ct->c_file);
98 show_single_message(ct, formsw);
104 ** Entry point to show/display a single message
108 show_single_message(CT ct, char *form)
111 ** Allow user executable bit so that temporary directories created by
112 ** the viewer (e.g., lynx) are going to be accessible
114 umask(ct->c_umask & ~(0100));
117 ** If you have a format file, then display
118 ** the message headers.
121 DisplayMsgHeader(ct, form);
123 /* Show the body of the message */
130 if (ct->c_ceclosefnx)
131 (*ct->c_ceclosefnx) (ct);
138 ** Use mhl to show the header fields
141 DisplayMsgHeader(CT ct, char *form)
149 vec[vecp++] = "-form";
151 vec[vecp++] = "-nobody";
152 vec[vecp++] = ct->c_file;
157 switch (child_id = fork()) {
159 adios("fork", "unable to");
164 fprintf(stderr, "unable to exec ");
170 pidcheck(pidwait(child_id, NOTOK));
177 ** Switching routine. Call the correct routine
178 ** based on content type.
182 show_switch(CT ct, int alternate)
184 switch (ct->c_type) {
186 return show_multi(ct, alternate);
190 switch (ct->c_subtype) {
191 case MESSAGE_PARTIAL:
192 return show_partial(ct, alternate);
195 case MESSAGE_EXTERNAL:
196 return show_external(ct, alternate);
201 return show_message_rfc822(ct, alternate);
207 return show_text(ct, alternate);
214 return show_content(ct, alternate);
218 adios(NULL, "unknown content type %d", ct->c_type);
222 return 0; /* NOT REACHED */
227 ** Generic method for displaying content
231 show_content(CT ct, int alternate)
233 char *cp, buffer[BUFSIZ];
234 CI ci = &ct->c_ctinfo;
236 /* Check for mhshow-show-type/subtype */
237 snprintf(buffer, sizeof(buffer), "mhshow-show-%s/%s",
238 ci->ci_type, ci->ci_subtype);
239 if ((cp = context_find(buffer)) && *cp != '\0')
240 return show_content_aux(ct, alternate, cp, NULL);
242 /* Check for mhshow-show-type */
243 snprintf(buffer, sizeof(buffer), "mhshow-show-%s", ci->ci_type);
244 if ((cp = context_find(buffer)) && *cp != '\0')
245 return show_content_aux(ct, alternate, cp, NULL);
247 if ((cp = ct->c_showproc))
248 return show_content_aux(ct, alternate, cp, NULL);
250 /* complain if we are not a part of a multipart/alternative */
252 return show_content_aux(ct, alternate, "%l", NULL);
259 ** Parse the display string for displaying generic content
262 show_content_aux(CT ct, int alternate, char *cp, char *cracked)
264 int fd, len, buflen, quoted;
266 char *bp, *pp, *file, buffer[BUFSIZ];
267 CI ci = &ct->c_ctinfo;
269 if (!ct->c_ceopenfnx) {
271 content_error(NULL, ct,
272 "don't know how to decode content");
278 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
280 if (ct->c_showproc && strcmp(ct->c_showproc, "true")==0)
281 return (alternate ? DONE : OK);
287 strncpy(buffer, cp, sizeof(buffer));
291 /* get buffer ready to go */
293 buflen = sizeof(buffer) - 1;
294 bp[0] = bp[buflen] = '\0';
297 /* Now parse display string */
298 for ( ; *cp && buflen > 0; cp++) {
304 /* insert parameters from Content-Type field */
309 for (ap = ci->ci_attrs, ep = ci->ci_values;
311 snprintf(bp, buflen, "%s%s=\"%s\"",
323 strncpy(bp, ct->c_charset ? ct->c_charset :
328 /* insert content description */
332 s = trimcpy(ct->c_descr);
333 strncpy(bp, s, buflen);
339 /* %f, and stdin is terminal not content */
344 /* insert filename containing content */
345 snprintf(bp, buflen, "'%s'", file);
347 ** since we've quoted the file argument,
348 ** set things up to look past it, to avoid
349 ** problems with the quoting logic below.
350 ** (I know, I should figure out what's
351 ** broken with the quoting logic, but..)
361 ** display listing prior to displaying content
367 /* insert subtype of content */
368 strncpy(bp, ci->ci_subtype, buflen);
372 /* insert character % */
385 /* Did we actually insert something? */
388 ** Insert single quote if not inside quotes
391 if (!quoted && buflen) {
393 memmove(pp + 1, pp, len);
398 /* Escape existing quotes */
399 while ((pp = strchr(pp, '\'')) && buflen > 3) {
401 memmove(pp + 3, pp, len);
409 ** If pp is still set, that means we ran
414 if (!quoted && buflen) {
432 return show_content_aux2(ct, alternate, cracked, buffer,
438 ** Routine to actually display the content
441 show_content_aux2(CT ct, int alternate, char *cracked,
442 char *buffer, int fd, int xlist, int xstdin)
446 if (debugsw || cracked) {
449 fprintf(stderr, "%s msg %s", cracked ? "storing" : "show",
452 fprintf(stderr, " part %s", ct->c_partno);
454 fprintf(stderr, " using command (cd %s; %s)\n",
457 fprintf(stderr, " using command %s\n", buffer);
461 if (ct->c_type == CT_MULTIPART)
462 list_content(ct, -1, 0, 0);
464 list_switch(ct, -1, 0, 0);
469 switch (child_id = fork()) {
471 advise("fork", "unable to");
472 (*ct->c_ceclosefnx) (ct);
481 execlp("/bin/sh", "/bin/sh", "-c", buffer, NULL);
482 fprintf(stderr, "unable to exec ");
488 pidcheck(pidXwait(child_id, NULL));
491 (*ct->c_ceclosefnx) (ct);
492 return (alternate ? DONE : OK);
498 ** show content of type "text"
502 show_text(CT ct, int alternate)
504 char *cp, buffer[BUFSIZ];
505 CI ci = &ct->c_ctinfo;
507 /* Check for mhshow-show-type/subtype */
508 snprintf(buffer, sizeof(buffer), "mhshow-show-%s/%s",
509 ci->ci_type, ci->ci_subtype);
510 if ((cp = context_find(buffer)) && *cp != '\0')
511 return show_content_aux(ct, alternate, cp, NULL);
513 /* Check for mhshow-show-type */
514 snprintf(buffer, sizeof(buffer), "mhshow-show-%s", ci->ci_type);
515 if ((cp = context_find(buffer)) && *cp != '\0')
516 return show_content_aux(ct, alternate, cp, NULL);
519 ** Use default method if content is text/plain, or if
520 ** if it is not a text part of a multipart/alternative
522 if (!alternate || ct->c_subtype == TEXT_PLAIN) {
523 if (ct->c_charset && !is_native_charset(ct->c_charset)) {
524 snprintf(buffer, sizeof(buffer), "%%liconv -f '%s'",
527 snprintf(buffer, sizeof(buffer), "%%lcat");
529 ct->c_showproc = getcpy(buffer);
530 return show_content_aux(ct, alternate, ct->c_showproc, NULL);
538 ** show message body of type "multipart"
542 show_multi(CT ct, int alternate)
544 char *cp, buffer[BUFSIZ];
545 CI ci = &ct->c_ctinfo;
547 /* Check for mhshow-show-type/subtype */
548 snprintf(buffer, sizeof(buffer), "mhshow-show-%s/%s",
549 ci->ci_type, ci->ci_subtype);
550 if ((cp = context_find(buffer)) && *cp != '\0')
551 return show_multi_aux(ct, alternate, cp);
553 /* Check for mhshow-show-type */
554 snprintf(buffer, sizeof(buffer), "mhshow-show-%s", ci->ci_type);
555 if ((cp = context_find(buffer)) && *cp != '\0')
556 return show_multi_aux(ct, alternate, cp);
558 if ((cp = ct->c_showproc))
559 return show_multi_aux(ct, alternate, cp);
562 ** Use default method to display this multipart content
563 ** if it is not a (nested) part of a multipart/alternative,
564 ** or if it is one of the known subtypes of multipart.
566 if (!alternate || ct->c_subtype != MULTI_UNKNOWN)
567 return show_multi_internal(ct, alternate);
574 ** show message body of subtypes of multipart that
575 ** we understand directly (mixed, alternate, etc...)
579 show_multi_internal(CT ct, int alternate)
581 int alternating, nowalternate, result;
582 struct multipart *m = (struct multipart *) ct->c_ctparams;
587 nowalternate = alternate;
589 if (ct->c_subtype == MULTI_ALTERNATE) {
595 ** Other possible multipart types are:
596 ** - multipart/parallel
598 ** - multipart/digest
599 ** - unknown subtypes of multipart (treat as mixed per rfc2046)
603 ** alternate -> we are a part inside an multipart/alternative
604 ** alternating -> we are a multipart/alternative
607 result = alternate ? NOTOK : OK;
609 for (part = m->mp_parts; part; part = part->mp_next) {
612 if (part_ok(p, 1) && type_ok(p, 1)) {
615 inneresult = show_switch(p, nowalternate);
616 switch (inneresult) {
618 if (alternate && !alternating) {
631 alternate = nowalternate = 0;
641 if (alternating && !part) {
643 content_error(NULL, ct, "don't know how to display any of the contents");
653 ** Parse display string for multipart content
654 ** and use external program to display it.
658 show_multi_aux(CT ct, int alternate, char *cp)
660 int len, buflen, quoted;
662 char *bp, *pp, *file, buffer[BUFSIZ];
663 struct multipart *m = (struct multipart *) ct->c_ctparams;
665 CI ci = &ct->c_ctinfo;
668 for (part = m->mp_parts; part; part = part->mp_next) {
671 if (!p->c_ceopenfnx) {
673 content_error(NULL, p, "don't know how to decode content");
677 if (p->c_storage == NULL) {
679 if ((*p->c_ceopenfnx) (p, &file) == NOTOK)
682 /* I'm not sure if this is necessary? */
683 p->c_storage = getcpy(file);
685 if (p->c_showproc && strcmp(p->c_showproc, "true")==0)
686 return (alternate ? DONE : OK);
687 (*p->c_ceclosefnx) (p);
693 /* get buffer ready to go */
695 buflen = sizeof(buffer) - 1;
696 bp[0] = bp[buflen] = '\0';
699 /* Now parse display string */
700 for ( ; *cp && buflen > 0; cp++) {
705 /* insert parameters from Content-Type field */
710 for (ap = ci->ci_attrs, ep = ci->ci_values;
712 snprintf(bp, buflen, "%s%s=\"%s\"",
724 strncpy(bp, ct->c_charset ? ct->c_charset :
729 /* insert content description */
733 s = trimcpy(ct->c_descr);
734 strncpy(bp, s, buflen);
741 /* insert filename(s) containing content */
745 for (part = m->mp_parts; part;
746 part = part->mp_next) {
749 snprintf(bp, buflen, "%s'%s'",
757 ** set our starting pointer back to bp,
758 ** to avoid requoting the filenames we
767 ** display listing prior to displaying content
773 /* insert subtype of content */
774 strncpy(bp, ci->ci_subtype, buflen);
778 /* insert character % */
791 /* Did we actually insert something? */
794 ** Insert single quote if not inside quotes
797 if (!quoted && buflen) {
799 memmove(pp + 1, pp, len);
804 /* Escape existing quotes */
805 while ((pp = strchr(pp, '\'')) && buflen > 3) {
807 memmove(pp + 3, pp, len);
815 ** If pp is still set, that means we ran
820 if (!quoted && buflen) {
837 return show_content_aux2(ct, alternate, NULL, buffer,
843 ** show content of type "message/rfc822"
847 show_message_rfc822(CT ct, int alternate)
849 char *cp, buffer[BUFSIZ];
850 CI ci = &ct->c_ctinfo;
852 /* Check for mhshow-show-type/subtype */
853 snprintf(buffer, sizeof(buffer), "mhshow-show-%s/%s",
854 ci->ci_type, ci->ci_subtype);
855 if ((cp = context_find(buffer)) && *cp != '\0')
856 return show_content_aux(ct, alternate, cp, NULL);
858 /* Check for mhshow-show-type */
859 snprintf(buffer, sizeof(buffer), "mhshow-show-%s", ci->ci_type);
860 if ((cp = context_find(buffer)) && *cp != '\0')
861 return show_content_aux(ct, alternate, cp, NULL);
863 if ((cp = ct->c_showproc))
864 return show_content_aux(ct, alternate, cp, NULL);
866 /* default method for message/rfc822 */
867 if (ct->c_subtype == MESSAGE_RFC822) {
868 cp = (ct->c_showproc = getcpy("%lshow -file %F"));
869 return show_content_aux(ct, alternate, cp, NULL);
872 /* complain if we are not a part of a multipart/alternative */
874 return show_content_aux(ct, alternate, "%l", NULL);
881 ** Show content of type "message/partial".
885 show_partial(CT ct, int alternate)
887 show_content_aux(ct, alternate, "%l", NULL);
888 puts("in order to display this message, you must reassemble it");
894 ** Show how to retrieve content of type "message/external".
897 show_external(CT ct, int alternate)
904 msg = add("You need to fetch the contents yourself:\n", NULL);
905 ap = ct->c_ctinfo.ci_attrs;
906 ep = ct->c_ctinfo.ci_values;
907 for (; *ap; ap++, ep++) {
908 msg = add(concat("\t", *ap, ": ", *ep, NULL), msg);
910 if (!(fp = fopen(ct->c_file, "r"))) {
911 adios(ct->c_file, "unable to open");
913 fseek(fp, ct->c_begin, SEEK_SET);
914 while (!feof(fp) && ftell(fp) < ct->c_end) {
915 if (!fgets(buf, sizeof buf, fp)) {
916 adios(ct->c_file, "unable to read");
918 *strchr(buf, '\n') = '\0';
920 continue; /* skip empty lines */
922 msg = add(concat("\t", buf, "\n", NULL), msg);
925 show_content_aux(ct, alternate, "%l", NULL);