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>
33 void content_error(char *, CT, char *, ...);
34 void flush_errors(void);
37 int list_switch(CT, int, int, int, int);
38 int list_content(CT, int, int, int, int);
43 void show_all_messages(CT *);
44 int show_content_aux(CT, int, char *, char *);
49 static void show_single_message(CT, char *);
50 static void DisplayMsgHeader(CT, char *);
51 static int show_switch(CT, int);
52 static int show_content(CT, int);
53 static int show_content_aux2(CT, int, char *, char *, int, int, int);
54 static int show_text(CT, int);
55 static int show_multi(CT, int);
56 static int show_multi_internal(CT, int);
57 static int show_multi_aux(CT, int, char *);
58 static int show_message_rfc822(CT, int);
59 static int show_partial(CT, int);
60 static int show_external(CT, int);
64 ** Top level entry point to show/display a group of messages
68 show_all_messages(CT *cts)
73 ** If form is not specified, then get default form
74 ** for showing headers of MIME messages.
77 formsw = getcpy(etcpath("mhl.headers"));
80 ** If form is "mhl.null", suppress display of header.
82 if (strcmp(formsw, "mhl.null")==0)
85 for (ctp = cts; *ctp; ctp++) {
88 if (!type_ok(ct, 1)) { /* top-level type */
95 printf(">>> Message %s\n\n", ct->c_file);
97 show_single_message(ct, formsw);
103 ** Entry point to show/display a single message
107 show_single_message(CT ct, char *form)
110 ** Allow user executable bit so that temporary directories created by
111 ** the viewer (e.g., lynx) are going to be accessible
113 umask(ct->c_umask & ~(0100));
116 ** If you have a format file, then display
117 ** the message headers.
120 DisplayMsgHeader(ct, form);
122 /* Show the body of the message */
129 if (ct->c_ceclosefnx)
130 (*ct->c_ceclosefnx) (ct);
137 ** Use mhl to show the header fields
140 DisplayMsgHeader(CT ct, char *form)
148 vec[vecp++] = "-form";
150 vec[vecp++] = "-nobody";
151 vec[vecp++] = ct->c_file;
156 switch (child_id = fork()) {
158 adios("fork", "unable to");
163 fprintf(stderr, "unable to exec ");
169 pidcheck(pidwait(child_id, NOTOK));
176 ** Switching routine. Call the correct routine
177 ** based on content type.
181 show_switch(CT ct, int alternate)
183 switch (ct->c_type) {
185 return show_multi(ct, alternate);
189 switch (ct->c_subtype) {
190 case MESSAGE_PARTIAL:
191 return show_partial(ct, alternate);
194 case MESSAGE_EXTERNAL:
195 return show_external(ct, alternate);
200 return show_message_rfc822(ct, alternate);
206 return show_text(ct, alternate);
213 return show_content(ct, alternate);
217 adios(NULL, "unknown content type %d", ct->c_type);
221 return 0; /* NOT REACHED */
226 ** Generic method for displaying content
230 show_content(CT ct, int alternate)
232 char *cp, buffer[BUFSIZ];
233 CI ci = &ct->c_ctinfo;
235 /* Check for mhshow-show-type/subtype */
236 snprintf(buffer, sizeof(buffer), "mhshow-show-%s/%s",
237 ci->ci_type, ci->ci_subtype);
238 if ((cp = context_find(buffer)) && *cp != '\0')
239 return show_content_aux(ct, alternate, cp, NULL);
241 /* Check for mhshow-show-type */
242 snprintf(buffer, sizeof(buffer), "mhshow-show-%s", ci->ci_type);
243 if ((cp = context_find(buffer)) && *cp != '\0')
244 return show_content_aux(ct, alternate, cp, NULL);
246 if ((cp = ct->c_showproc))
247 return show_content_aux(ct, alternate, cp, NULL);
249 /* complain if we are not a part of a multipart/alternative */
251 content_error(NULL, ct, "don't know how to display content");
258 ** Parse the display string for displaying generic content
261 show_content_aux(CT ct, int alternate, char *cp, char *cracked)
263 int fd, len, buflen, quoted;
265 char *bp, *pp, *file, buffer[BUFSIZ];
266 CI ci = &ct->c_ctinfo;
268 if (!ct->c_ceopenfnx) {
270 content_error(NULL, ct,
271 "don't know how to decode content");
277 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
279 if (ct->c_showproc && strcmp(ct->c_showproc, "true")==0)
280 return (alternate ? DONE : OK);
286 strncpy(buffer, cp, sizeof(buffer));
290 /* get buffer ready to go */
292 buflen = sizeof(buffer) - 1;
293 bp[0] = bp[buflen] = '\0';
296 /* Now parse display string */
297 for ( ; *cp && buflen > 0; cp++) {
303 /* insert parameters from Content-Type field */
308 for (ap = ci->ci_attrs, ep = ci->ci_values;
310 snprintf(bp, buflen, "%s%s=\"%s\"",
322 strncpy(bp, ct->c_charset ? ct->c_charset :
327 /* insert content description */
331 s = trimcpy(ct->c_descr);
332 strncpy(bp, s, buflen);
338 /* %f, and stdin is terminal not content */
343 /* insert filename containing content */
344 snprintf(bp, buflen, "'%s'", file);
346 ** since we've quoted the file argument,
347 ** set things up to look past it, to avoid
348 ** problems with the quoting logic below.
349 ** (I know, I should figure out what's
350 ** broken with the quoting logic, but..)
360 ** display listing prior to displaying content
366 /* insert subtype of content */
367 strncpy(bp, ci->ci_subtype, buflen);
371 /* insert character % */
384 /* Did we actually insert something? */
387 ** Insert single quote if not inside quotes
390 if (!quoted && buflen) {
392 memmove(pp + 1, pp, len);
397 /* Escape existing quotes */
398 while ((pp = strchr(pp, '\'')) && buflen > 3) {
400 memmove(pp + 3, pp, len);
408 ** If pp is still set, that means we ran
413 if (!quoted && buflen) {
431 return show_content_aux2(ct, alternate, cracked, buffer,
437 ** Routine to actually display the content
440 show_content_aux2(CT ct, int alternate, char *cracked,
441 char *buffer, int fd, int xlist, int xstdin)
445 if (debugsw || cracked) {
448 fprintf(stderr, "%s msg %s", cracked ? "storing" : "show",
451 fprintf(stderr, " part %s", ct->c_partno);
453 fprintf(stderr, " using command (cd %s; %s)\n",
456 fprintf(stderr, " using command %s\n", buffer);
460 if (ct->c_type == CT_MULTIPART)
461 list_content(ct, -1, 1, 0, 0);
463 list_switch(ct, -1, 1, 0, 0);
468 switch (child_id = fork()) {
470 advise("fork", "unable to");
471 (*ct->c_ceclosefnx) (ct);
480 execlp("/bin/sh", "/bin/sh", "-c", buffer, NULL);
481 fprintf(stderr, "unable to exec ");
487 pidcheck(pidXwait(child_id, NULL));
490 (*ct->c_ceclosefnx) (ct);
491 return (alternate ? DONE : OK);
497 ** show content of type "text"
501 show_text(CT ct, int alternate)
503 char *cp, buffer[BUFSIZ];
504 CI ci = &ct->c_ctinfo;
506 /* Check for mhshow-show-type/subtype */
507 snprintf(buffer, sizeof(buffer), "mhshow-show-%s/%s",
508 ci->ci_type, ci->ci_subtype);
509 if ((cp = context_find(buffer)) && *cp != '\0')
510 return show_content_aux(ct, alternate, cp, NULL);
512 /* Check for mhshow-show-type */
513 snprintf(buffer, sizeof(buffer), "mhshow-show-%s", ci->ci_type);
514 if ((cp = context_find(buffer)) && *cp != '\0')
515 return show_content_aux(ct, alternate, cp, NULL);
518 ** Use default method if content is text/plain, or if
519 ** if it is not a text part of a multipart/alternative
521 if (!alternate || ct->c_subtype == TEXT_PLAIN) {
522 if (ct->c_charset && !check_charset(ct->c_charset,
523 strlen(ct->c_charset))) {
524 snprintf(buffer, sizeof(buffer), "%%liconv -f '%s'",
527 snprintf(buffer, sizeof(buffer), "%%lcat");
529 cp = (ct->c_showproc = getcpy(buffer));
530 return show_content_aux(ct, alternate, cp, 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 content_error(NULL, ct, "don't know how to display content");
881 ** Show content of type "message/partial".
885 show_partial(CT ct, int alternate)
887 content_error(NULL, ct,
888 "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:", NULL);
905 ap = ct->c_ctinfo.ci_attrs;
906 ep = ct->c_ctinfo.ci_values;
907 for (; *ap; ap++, ep++) {
908 msg = add(concat("\n\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';
919 msg = add(concat("\n\t", buf, NULL), msg);
922 content_error(NULL, ct, msg);