2 * Copyright (C) 1996-2000,2002 Michael R. Elkins <me@mutt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31 #include "mutt_curses.h"
37 #include "mutt_crypt.h"
40 #define BUFI_SIZE 1000
41 #define BUFO_SIZE 2000
44 typedef int (*handler_t) (BODY *, STATE *);
46 int Index_hex[128] = {
47 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
48 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
49 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
50 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1,
51 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
52 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
53 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
54 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1
58 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
59 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
60 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
61 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
62 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
63 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
64 -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
65 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
68 static void state_prefix_put (const char *d, size_t dlen, STATE *s)
72 state_prefix_putc (*d++, s);
74 fwrite (d, dlen, 1, s->fpout);
77 static void mutt_convert_to_state(iconv_t cd, char *bufi, size_t *l, STATE *s)
86 if (cd != (iconv_t)(-1))
88 ob = bufo, obl = sizeof (bufo);
89 iconv (cd, 0, 0, &ob, &obl);
91 state_prefix_put (bufo, ob - bufo, s);
96 if (cd == (iconv_t)(-1))
98 state_prefix_put (bufi, *l, s);
106 ob = bufo, obl = sizeof (bufo);
107 mutt_iconv (cd, &ib, &ibl, &ob, &obl, 0, "?");
110 state_prefix_put (bufo, ob - bufo, s);
112 memmove (bufi, ib, ibl);
116 static void mutt_decode_xbit (STATE *s, long len, int istext, iconv_t cd)
119 char bufi[BUFI_SIZE];
126 while ((c = fgetc(s->fpin)) != EOF && len--)
130 if((ch = fgetc(s->fpin)) == '\n')
140 if (l == sizeof (bufi))
141 mutt_convert_to_state (cd, bufi, &l, s);
144 mutt_convert_to_state (cd, bufi, &l, s);
145 mutt_convert_to_state (cd, 0, 0, s);
147 state_reset_prefix (s);
150 mutt_copy_bytes (s->fpin, s->fpout, len);
153 static int qp_decode_triple (char *s, char *d)
155 /* soft line break */
156 if (*s == '=' && !(*(s+1)))
159 /* quoted-printable triple */
161 isxdigit ((unsigned char) *(s+1)) &&
162 isxdigit ((unsigned char) *(s+2)))
164 *d = (hexval (*(s+1)) << 4) | hexval (*(s+2));
172 static void qp_decode_line (char *dest, char *src, size_t *l,
181 /* decode the line */
183 for (d = dest, s = src; *s;)
185 switch ((kind = qp_decode_triple (s, &c)))
187 case 0: *d++ = c; s += 3; break; /* qp triple */
188 case -1: *d++ = *s++; break; /* single character */
189 case 1: soft = 1; s++; break; /* soft line break */
193 if (!soft && last == '\n')
201 * Decode an attachment encoded with quoted-printable.
203 * Why doesn't this overflow any buffers? First, it's guaranteed
204 * that the length of a line grows when you _en_-code it to
205 * quoted-printable. That means that we always can store the
206 * result in a buffer of at most the _same_ size.
208 * Now, we don't special-case if the line we read with fgets()
209 * isn't terminated. We don't care about this, since STRING > 78,
210 * so corrupted input will just be corrupted a bit more. That
211 * implies that STRING+1 bytes are always sufficient to store the
212 * result of qp_decode_line.
214 * Finally, at soft line breaks, some part of a multibyte character
215 * may have been left over by mutt_convert_to_state(). This shouldn't
216 * be more than 6 characters, so STRING + 7 should be sufficient
217 * memory to store the decoded data.
219 * Just to make sure that I didn't make some off-by-one error
220 * above, we just use STRING*2 for the target buffer's size.
224 static void mutt_decode_quoted (STATE *s, long len, int istext, iconv_t cd)
227 char decline[2*STRING];
229 size_t linelen; /* number of input bytes in `line' */
232 int last; /* store the last character in the input line */
242 * It's ok to use a fixed size buffer for input, even if the line turns
243 * out to be longer than this. Just process the line in chunks. This
244 * really shouldn't happen according the MIME spec, since Q-P encoded
245 * lines are at most 76 characters, but we should be liberal about what
248 if (fgets (line, MIN ((ssize_t)sizeof (line), len + 1), s->fpin) == NULL)
251 linelen = strlen(line);
255 * inspect the last character we read so we can tell if we got the
258 last = linelen ? line[linelen - 1] : 0;
260 /* chop trailing whitespace if we got the full line */
263 while (linelen > 0 && ISSPACE (line[linelen-1]))
268 /* decode and do character set conversion */
269 qp_decode_line (decline + l, line, &l3, last);
271 mutt_convert_to_state (cd, decline, &l, s);
274 mutt_convert_to_state (cd, 0, 0, s);
275 state_reset_prefix(s);
278 void mutt_decode_base64 (STATE *s, long len, int istext, iconv_t cd)
281 int c1, c2, c3, c4, ch, cr = 0, i;
282 char bufi[BUFI_SIZE];
292 for (i = 0 ; i < 4 && len > 0 ; len--)
294 if ((ch = fgetc (s->fpin)) == EOF)
296 if (ch >= 0 && ch < 128 && (base64val(ch) != -1 || ch == '='))
301 dprint (2, (debugfile, "%s:%d [mutt_decode_base64()]: "
302 "didn't get a multiple of 4 chars.\n", __FILE__, __LINE__));
306 c1 = base64val (buf[0]);
307 c2 = base64val (buf[1]);
308 ch = (c1 << 2) | (c2 >> 4);
310 if (cr && ch != '\n')
315 if (istext && ch == '\r')
322 c3 = base64val (buf[2]);
323 ch = ((c2 & 0xf) << 4) | (c3 >> 2);
325 if (cr && ch != '\n')
330 if (istext && ch == '\r')
335 if (buf[3] == '=') break;
336 c4 = base64val (buf[3]);
337 ch = ((c3 & 0x3) << 6) | c4;
339 if (cr && ch != '\n')
343 if (istext && ch == '\r')
348 if (l + 8 >= sizeof (bufi))
349 mutt_convert_to_state (cd, bufi, &l, s);
352 if (cr) bufi[l++] = '\r';
354 mutt_convert_to_state (cd, bufi, &l, s);
355 mutt_convert_to_state (cd, 0, 0, s);
357 state_reset_prefix(s);
360 static unsigned char decode_byte (char ch)
367 static void mutt_decode_uuencoded (STATE *s, long len, int istext, iconv_t cd)
369 char tmps[SHORT_STRING];
370 char linelen, c, l, out;
372 char bufi[BUFI_SIZE];
380 if ((fgets(tmps, sizeof(tmps), s->fpin)) == NULL)
382 len -= mutt_strlen(tmps);
383 if ((!mutt_strncmp (tmps, "begin", 5)) && ISSPACE (tmps[5]))
388 if ((fgets(tmps, sizeof(tmps), s->fpin)) == NULL)
390 len -= mutt_strlen(tmps);
391 if (!mutt_strncmp (tmps, "end", 3))
394 linelen = decode_byte (*pt);
396 for (c = 0; c < linelen;)
398 for (l = 2; l <= 6; l += 2)
400 out = decode_byte (*pt) << l;
402 out |= (decode_byte (*pt) >> (6 - l));
408 mutt_convert_to_state (cd, bufi, &k, s);
413 mutt_convert_to_state (cd, bufi, &k, s);
414 mutt_convert_to_state (cd, 0, 0, s);
416 state_reset_prefix(s);
419 /* ----------------------------------------------------------------------------
420 * A (not so) minimal implementation of RFC1563.
423 #define IndentSize (4)
425 enum { RICH_PARAM=0, RICH_BOLD, RICH_UNDERLINE, RICH_ITALIC, RICH_NOFILL,
426 RICH_INDENT, RICH_INDENT_RIGHT, RICH_EXCERPT, RICH_CENTER, RICH_FLUSHLEFT,
427 RICH_FLUSHRIGHT, RICH_COLOR, RICH_LAST_TAG };
430 const char *tag_name;
433 { "param", RICH_PARAM },
434 { "bold", RICH_BOLD },
435 { "italic", RICH_ITALIC },
436 { "underline", RICH_UNDERLINE },
437 { "nofill", RICH_NOFILL },
438 { "excerpt", RICH_EXCERPT },
439 { "indent", RICH_INDENT },
440 { "indentright", RICH_INDENT_RIGHT },
441 { "center", RICH_CENTER },
442 { "flushleft", RICH_FLUSHLEFT },
443 { "flushright", RICH_FLUSHRIGHT },
444 { "flushboth", RICH_FLUSHLEFT },
445 { "color", RICH_COLOR },
446 { "x-color", RICH_COLOR },
450 struct enriched_state
464 int tag_level[RICH_LAST_TAG];
469 static int enriched_cmp (const char *a, const wchar_t *b)
471 register const char *p = a;
472 register const wchar_t *q = b;
482 for ( ; *p || *q; p++, q++)
484 if ((i = ascii_tolower (*p)) - ascii_tolower (((char) *q) & 0x7f))
490 static void enriched_wrap (struct enriched_state *stte)
497 if (stte->tag_level[RICH_CENTER] || stte->tag_level[RICH_FLUSHRIGHT])
499 /* Strip trailing white space */
500 size_t y = stte->line_used - 1;
502 while (y && iswspace (stte->line[y]))
504 stte->line[y] = (wchar_t) '\0';
509 if (stte->tag_level[RICH_CENTER])
511 /* Strip leading whitespace */
514 while (stte->line[y] && iswspace (stte->line[y]))
520 for (z = y ; z <= stte->line_used; z++)
522 stte->line[z - y] = stte->line[z];
526 stte->line_used -= y;
531 extra = stte->WrapMargin - stte->line_len - stte->indent_len -
532 (stte->tag_level[RICH_INDENT_RIGHT] * IndentSize);
535 if (stte->tag_level[RICH_CENTER])
540 state_putc (' ', stte->s);
544 else if (stte->tag_level[RICH_FLUSHRIGHT])
549 state_putc (' ', stte->s);
554 state_putws ((const wchar_t*) stte->line, stte->s);
557 state_putc ('\n', stte->s);
558 stte->line[0] = (wchar_t) '\0';
561 stte->indent_len = 0;
564 state_puts (stte->s->prefix, stte->s);
565 stte->indent_len += mutt_strlen (stte->s->prefix);
568 if (stte->tag_level[RICH_EXCERPT])
570 x = stte->tag_level[RICH_EXCERPT];
575 state_puts (stte->s->prefix, stte->s);
576 stte->indent_len += mutt_strlen (stte->s->prefix);
580 state_puts ("> ", stte->s);
581 stte->indent_len += mutt_strlen ("> ");
587 stte->indent_len = 0;
588 if (stte->tag_level[RICH_INDENT])
590 x = stte->tag_level[RICH_INDENT] * IndentSize;
591 stte->indent_len += x;
594 state_putc (' ', stte->s);
600 static void enriched_flush (struct enriched_state *stte, int wrap)
602 if (!stte->tag_level[RICH_NOFILL] && (stte->line_len + stte->word_len >
603 (stte->WrapMargin - (stte->tag_level[RICH_INDENT_RIGHT] * IndentSize) -
605 enriched_wrap (stte);
609 stte->buffer[stte->buff_used] = (wchar_t) '\0';
610 stte->line_used += stte->buff_used;
611 if (stte->line_used > stte->line_max)
613 stte->line_max = stte->line_used;
614 safe_realloc (&stte->line, (stte->line_max + 1) * sizeof (wchar_t));
616 wcscat (stte->line, stte->buffer);
617 stte->line_len += stte->word_len;
626 static void enriched_putwc (wchar_t c, struct enriched_state *stte)
628 if (stte->tag_level[RICH_PARAM])
630 if (stte->tag_level[RICH_COLOR])
632 if (stte->param_used + 1 >= stte->param_len)
633 safe_realloc (&stte->param, (stte->param_len += STRING) * sizeof (wchar_t));
635 stte->param[stte->param_used++] = c;
637 return; /* nothing to do */
640 /* see if more space is needed (plus extra for possible rich characters) */
641 if (stte->buff_len < stte->buff_used + 3)
643 stte->buff_len += LONG_STRING;
644 safe_realloc (&stte->buffer, (stte->buff_len + 1) * sizeof (wchar_t));
647 if ((!stte->tag_level[RICH_NOFILL] && iswspace (c)) || c == (wchar_t) '\0')
649 if (c == (wchar_t) '\t')
650 stte->word_len += 8 - (stte->line_len + stte->word_len) % 8;
654 stte->buffer[stte->buff_used++] = c;
655 enriched_flush (stte, 0);
659 if (stte->s->flags & M_DISPLAY)
661 if (stte->tag_level[RICH_BOLD])
663 stte->buffer[stte->buff_used++] = c;
664 stte->buffer[stte->buff_used++] = (wchar_t) '\010';
665 stte->buffer[stte->buff_used++] = c;
667 else if (stte->tag_level[RICH_UNDERLINE])
670 stte->buffer[stte->buff_used++] = '_';
671 stte->buffer[stte->buff_used++] = (wchar_t) '\010';
672 stte->buffer[stte->buff_used++] = c;
674 else if (stte->tag_level[RICH_ITALIC])
676 stte->buffer[stte->buff_used++] = c;
677 stte->buffer[stte->buff_used++] = (wchar_t) '\010';
678 stte->buffer[stte->buff_used++] = '_';
682 stte->buffer[stte->buff_used++] = c;
687 stte->buffer[stte->buff_used++] = c;
693 static void enriched_puts (const char *s, struct enriched_state *stte)
697 if (stte->buff_len < stte->buff_used + mutt_strlen (s))
699 stte->buff_len += LONG_STRING;
700 safe_realloc (&stte->buffer, (stte->buff_len + 1) * sizeof (wchar_t));
705 stte->buffer[stte->buff_used++] = (wchar_t) *c;
710 static void enriched_set_flags (const wchar_t *tag, struct enriched_state *stte)
712 const wchar_t *tagptr = tag;
715 if (*tagptr == (wchar_t) '/')
718 for (i = 0, j = -1; EnrichedTags[i].tag_name; i++)
719 if (enriched_cmp (EnrichedTags[i].tag_name, tagptr) == 0)
721 j = EnrichedTags[i].index;
727 if (j == RICH_CENTER || j == RICH_FLUSHLEFT || j == RICH_FLUSHRIGHT)
728 enriched_flush (stte, 1);
730 if (*tag == (wchar_t) '/')
732 if (stte->tag_level[j]) /* make sure not to go negative */
733 stte->tag_level[j]--;
734 if ((stte->s->flags & M_DISPLAY) && j == RICH_PARAM && stte->tag_level[RICH_COLOR])
736 stte->param[stte->param_used] = (wchar_t) '\0';
737 if (!enriched_cmp("black", stte->param))
739 enriched_puts("\033[30m", stte);
741 else if (!enriched_cmp("red", stte->param))
743 enriched_puts("\033[31m", stte);
745 else if (!enriched_cmp("green", stte->param))
747 enriched_puts("\033[32m", stte);
749 else if (!enriched_cmp("yellow", stte->param))
751 enriched_puts("\033[33m", stte);
753 else if (!enriched_cmp("blue", stte->param))
755 enriched_puts("\033[34m", stte);
757 else if (!enriched_cmp("magenta", stte->param))
759 enriched_puts("\033[35m", stte);
761 else if (!enriched_cmp("cyan", stte->param))
763 enriched_puts("\033[36m", stte);
765 else if (!enriched_cmp("white", stte->param))
767 enriched_puts("\033[37m", stte);
770 if ((stte->s->flags & M_DISPLAY) && j == RICH_COLOR)
772 enriched_puts("\033[0m", stte);
775 /* flush parameter buffer when closing the tag */
778 stte->param_used = 0;
779 stte->param[0] = (wchar_t) '\0';
783 stte->tag_level[j]++;
785 if (j == RICH_EXCERPT)
786 enriched_flush(stte, 1);
790 static int text_enriched_handler (BODY *a, STATE *s)
793 TEXT, LANGLE, TAG, BOGUS_TAG, NEWLINE, ST_EOF, DONE
796 long bytes = a->length;
797 struct enriched_state stte;
800 wchar_t tag[LONG_STRING + 1];
802 memset (&stte, 0, sizeof (stte));
804 stte.WrapMargin = ((s->flags & M_DISPLAY) ? (COLS-4) : ((COLS-4)<72)?(COLS-4):72);
805 stte.line_max = stte.WrapMargin * 4;
806 stte.line = (wchar_t *) safe_calloc (1, (stte.line_max + 1) * sizeof (wchar_t));
807 stte.param = (wchar_t *) safe_calloc (1, (STRING) * sizeof (wchar_t));
809 stte.param_len = STRING;
814 state_puts (s->prefix, s);
815 stte.indent_len += mutt_strlen (s->prefix);
818 while (state != DONE)
822 if (!bytes || (wc = fgetwc (s->fpin)) == EOF)
838 if (stte.tag_level[RICH_NOFILL])
840 enriched_flush (&stte, 1);
844 enriched_putwc ((wchar_t) ' ', &stte);
850 enriched_putwc (wc, &stte);
855 if (wc == (wchar_t) '<')
857 enriched_putwc (wc, &stte);
866 /* Yes, fall through (it wasn't a <<, so this char is first in TAG) */
868 if (wc == (wchar_t) '>')
870 tag[tag_len] = (wchar_t) '\0';
871 enriched_set_flags (tag, &stte);
874 else if (tag_len < LONG_STRING) /* ignore overly long tags */
881 if (wc == (wchar_t) '>')
886 if (wc == (wchar_t) '\n')
887 enriched_flush (&stte, 1);
890 ungetwc (wc, s->fpin);
897 enriched_putwc ((wchar_t) '\0', &stte);
898 enriched_flush (&stte, 1);
902 case DONE: /* not reached, but gcc complains if this is absent */
907 state_putc ('\n', s); /* add a final newline */
909 FREE (&(stte.buffer));
911 FREE (&(stte.param));
918 #define TXTENRICHED 3
920 static int alternative_handler (BODY *a, STATE *s)
930 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
931 a->encoding == ENCUUENCODED)
935 fstat (fileno (s->fpin), &st);
936 b = mutt_new_body ();
937 b->length = (long) st.st_size;
938 b->parts = mutt_parse_multipart (s->fpin,
939 mutt_get_parameter ("boundary", a->parameter),
940 (long) st.st_size, ascii_strcasecmp ("digest", a->subtype) == 0);
947 /* First, search list of prefered types */
948 t = AlternativeOrderList;
952 int btlen; /* length of basetype */
953 int wild; /* do we have a wildcard to match all subtypes? */
955 c = strchr (t->data, '/');
958 wild = (c[1] == '*' && c[2] == 0);
964 btlen = mutt_strlen (t->data);
973 const char *bt = TYPE(b);
974 if (!ascii_strncasecmp (bt, t->data, btlen) && bt[btlen] == 0)
976 /* the basetype matches */
977 if (wild || !ascii_strcasecmp (t->data + btlen + 1, b->subtype))
987 /* Next, look for an autoviewable type */
996 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
997 if (mutt_is_autoview (b, buf))
999 rfc1524_entry *entry = rfc1524_new_entry ();
1001 if (rfc1524_mailcap_lookup (b, buf, entry, M_AUTOVIEW))
1005 rfc1524_free_entry (&entry);
1011 /* Then, look for a text entry */
1020 if (b->type == TYPETEXT)
1022 if (! ascii_strcasecmp ("plain", b->subtype) && type <= TXTPLAIN)
1027 else if (! ascii_strcasecmp ("enriched", b->subtype) && type <= TXTENRICHED)
1032 else if (! ascii_strcasecmp ("html", b->subtype) && type <= TXTHTML)
1042 /* Finally, look for other possibilities */
1051 if (mutt_can_decode (b))
1059 if (s->flags & M_DISPLAY && !option (OPTWEED))
1061 fseeko (s->fpin, choice->hdr_offset, 0);
1062 mutt_copy_bytes(s->fpin, s->fpout, choice->offset-choice->hdr_offset);
1064 mutt_body_handler (choice, s);
1066 else if (s->flags & M_DISPLAY)
1068 /* didn't find anything that we could display! */
1069 state_mark_attach (s);
1070 state_puts(_("[-- Error: Could not display any parts of Multipart/Alternative! --]\n"), s);
1080 /* handles message/rfc822 body parts */
1081 static int message_handler (BODY *a, STATE *s)
1088 off_start = ftello (s->fpin);
1089 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1090 a->encoding == ENCUUENCODED)
1092 fstat (fileno (s->fpin), &st);
1093 b = mutt_new_body ();
1094 b->length = (LOFF_T) st.st_size;
1095 b->parts = mutt_parse_messageRFC822 (s->fpin, b);
1102 mutt_copy_hdr (s->fpin, s->fpout, off_start, b->parts->offset,
1103 (((s->flags & M_WEED) || ((s->flags & (M_DISPLAY|M_PRINTING)) && option (OPTWEED))) ? (CH_WEED | CH_REORDER) : 0) |
1104 (s->prefix ? CH_PREFIX : 0) | CH_DECODE | CH_FROM, s->prefix);
1107 state_puts (s->prefix, s);
1108 state_putc ('\n', s);
1110 rc = mutt_body_handler (b->parts, s);
1113 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1114 a->encoding == ENCUUENCODED)
1115 mutt_free_body (&b);
1120 /* returns 1 if decoding the attachment will produce output */
1121 int mutt_can_decode (BODY *a)
1125 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
1126 if (mutt_is_autoview (a, type))
1127 return (rfc1524_mailcap_lookup (a, type, NULL, M_AUTOVIEW));
1128 else if (a->type == TYPETEXT)
1130 else if (a->type == TYPEMESSAGE)
1132 else if (a->type == TYPEMULTIPART)
1138 if (ascii_strcasecmp (a->subtype, "signed") == 0 ||
1139 ascii_strcasecmp (a->subtype, "encrypted") == 0)
1143 for (p = a->parts; p; p = p->next)
1145 if (mutt_can_decode (p))
1150 else if (WithCrypto && a->type == TYPEAPPLICATION)
1152 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp(a))
1154 if ((WithCrypto & APPLICATION_SMIME) && mutt_is_application_smime(a))
1161 static int multipart_handler (BODY *a, STATE *s)
1169 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1170 a->encoding == ENCUUENCODED)
1172 fstat (fileno (s->fpin), &st);
1173 b = mutt_new_body ();
1174 b->length = (long) st.st_size;
1175 b->parts = mutt_parse_multipart (s->fpin,
1176 mutt_get_parameter ("boundary", a->parameter),
1177 (long) st.st_size, ascii_strcasecmp ("digest", a->subtype) == 0);
1182 for (p = b->parts, count = 1; p; p = p->next, count++)
1184 if (s->flags & M_DISPLAY)
1186 state_mark_attach (s);
1187 state_printf (s, _("[-- Attachment #%d"), count);
1188 if (p->description || p->filename || p->form_name)
1190 state_puts (": ", s);
1191 state_puts (p->description ? p->description :
1192 p->filename ? p->filename : p->form_name, s);
1194 state_puts (" --]\n", s);
1196 mutt_pretty_size (length, sizeof (length), p->length);
1198 state_mark_attach (s);
1199 state_printf (s, _("[-- Type: %s/%s, Encoding: %s, Size: %s --]\n"),
1200 TYPE (p), p->subtype, ENCODING (p->encoding), length);
1201 if (!option (OPTWEED))
1203 fseeko (s->fpin, p->hdr_offset, 0);
1204 mutt_copy_bytes(s->fpin, s->fpout, p->offset-p->hdr_offset);
1207 state_putc ('\n', s);
1211 if (p->description && mutt_can_decode (p))
1212 state_printf (s, "Content-Description: %s\n", p->description);
1215 state_printf(s, "%s: \n", p->form_name);
1218 rc = mutt_body_handler (p, s);
1219 state_putc ('\n', s);
1223 mutt_error (_("One or more parts of this message could not be displayed"));
1224 dprint (1, (debugfile, "Failed on attachment #%d, type %s/%s.\n", count, TYPE(p), NONULL (p->subtype)));
1227 if ((s->flags & M_REPLYING)
1228 && (option (OPTINCLUDEONLYFIRST)) && (s->flags & M_FIRSTDONE))
1232 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1233 a->encoding == ENCUUENCODED)
1234 mutt_free_body (&b);
1236 /* make failure of a single part non-fatal */
1242 static int autoview_handler (BODY *a, STATE *s)
1244 rfc1524_entry *entry = rfc1524_new_entry ();
1245 char buffer[LONG_STRING];
1247 char command[LONG_STRING];
1248 char tempfile[_POSIX_PATH_MAX] = "";
1257 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
1258 rfc1524_mailcap_lookup (a, type, entry, M_AUTOVIEW);
1260 fname = safe_strdup (a->filename);
1261 mutt_sanitize_filename (fname, 1);
1262 rfc1524_expand_filename (entry->nametemplate, fname, tempfile, sizeof (tempfile));
1267 strfcpy (command, entry->command, sizeof (command));
1269 /* rfc1524_expand_command returns 0 if the file is required */
1270 piped = rfc1524_expand_command (a, tempfile, type, command, sizeof (command));
1272 if (s->flags & M_DISPLAY)
1274 state_mark_attach (s);
1275 state_printf (s, _("[-- Autoview using %s --]\n"), command);
1276 mutt_message(_("Invoking autoview command: %s"),command);
1279 if ((fpin = safe_fopen (tempfile, "w+")) == NULL)
1281 mutt_perror ("fopen");
1282 rfc1524_free_entry (&entry);
1286 mutt_copy_bytes (s->fpin, fpin, a->length);
1290 safe_fclose (&fpin);
1291 thepid = mutt_create_filter (command, NULL, &fpout, &fperr);
1298 thepid = mutt_create_filter_fd (command, NULL, &fpout, &fperr,
1299 fileno(fpin), -1, -1);
1304 mutt_perror _("Can't create filter");
1305 if (s->flags & M_DISPLAY)
1307 state_mark_attach (s);
1308 state_printf (s, _("[-- Can't run %s. --]\n"), command);
1316 while (fgets (buffer, sizeof(buffer), fpout) != NULL)
1318 state_puts (s->prefix, s);
1319 state_puts (buffer, s);
1321 /* check for data on stderr */
1322 if (fgets (buffer, sizeof(buffer), fperr))
1324 if (s->flags & M_DISPLAY)
1326 state_mark_attach (s);
1327 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1330 state_puts (s->prefix, s);
1331 state_puts (buffer, s);
1332 while (fgets (buffer, sizeof(buffer), fperr) != NULL)
1334 state_puts (s->prefix, s);
1335 state_puts (buffer, s);
1341 mutt_copy_stream (fpout, s->fpout);
1342 /* Check for stderr messages */
1343 if (fgets (buffer, sizeof(buffer), fperr))
1345 if (s->flags & M_DISPLAY)
1347 state_mark_attach (s);
1348 state_printf (s, _("[-- Autoview stderr of %s --]\n"),
1352 state_puts (buffer, s);
1353 mutt_copy_stream (fperr, s->fpout);
1358 safe_fclose (&fpout);
1359 safe_fclose (&fperr);
1361 mutt_wait_filter (thepid);
1363 safe_fclose (&fpin);
1365 mutt_unlink (tempfile);
1367 if (s->flags & M_DISPLAY)
1368 mutt_clear_error ();
1370 rfc1524_free_entry (&entry);
1375 static int external_body_handler (BODY *b, STATE *s)
1377 const char *access_type;
1378 const char *expiration;
1381 access_type = mutt_get_parameter ("access-type", b->parameter);
1384 if (s->flags & M_DISPLAY)
1386 state_mark_attach (s);
1387 state_puts (_("[-- Error: message/external-body has no access-type parameter --]\n"), s);
1394 expiration = mutt_get_parameter ("expiration", b->parameter);
1396 expire = mutt_parse_date (expiration, NULL);
1400 if (!ascii_strcasecmp (access_type, "x-mutt-deleted"))
1402 if (s->flags & (M_DISPLAY|M_PRINTING))
1405 char pretty_size[10];
1407 state_mark_attach (s);
1408 state_printf (s, _("[-- This %s/%s attachment "),
1409 TYPE(b->parts), b->parts->subtype);
1410 length = mutt_get_parameter ("length", b->parameter);
1413 mutt_pretty_size (pretty_size, sizeof (pretty_size),
1414 strtol (length, NULL, 10));
1415 state_printf (s, _("(size %s bytes) "), pretty_size);
1417 state_puts (_("has been deleted --]\n"), s);
1421 state_mark_attach (s);
1422 state_printf (s, _("[-- on %s --]\n"), expiration);
1424 if (b->parts->filename)
1426 state_mark_attach (s);
1427 state_printf (s, _("[-- name: %s --]\n"), b->parts->filename);
1430 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1431 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1435 else if(expiration && expire < time(NULL))
1437 if (s->flags & M_DISPLAY)
1439 state_mark_attach (s);
1440 state_printf (s, _("[-- This %s/%s attachment is not included, --]\n"),
1441 TYPE(b->parts), b->parts->subtype);
1442 state_attach_puts (_("[-- and the indicated external source has --]\n"
1443 "[-- expired. --]\n"), s);
1445 mutt_copy_hdr(s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1446 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1452 if (s->flags & M_DISPLAY)
1454 state_mark_attach (s);
1456 _("[-- This %s/%s attachment is not included, --]\n"),
1457 TYPE (b->parts), b->parts->subtype);
1458 state_mark_attach (s);
1460 _("[-- and the indicated access-type %s is unsupported --]\n"),
1462 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1463 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1471 void mutt_decode_attachment (BODY *b, STATE *s)
1473 int istext = mutt_is_text_part (b);
1474 iconv_t cd = (iconv_t)(-1);
1476 if (istext && s->flags & M_CHARCONV)
1478 char *charset = mutt_get_parameter ("charset", b->parameter);
1479 if (!charset && AssumedCharset && *AssumedCharset)
1480 charset = mutt_get_default_charset ();
1481 if (charset && Charset)
1482 cd = mutt_iconv_open (Charset, charset, M_ICONV_HOOK_FROM);
1484 else if (istext && b->charset)
1485 cd = mutt_iconv_open (Charset, b->charset, M_ICONV_HOOK_FROM);
1487 fseeko (s->fpin, b->offset, 0);
1488 switch (b->encoding)
1490 case ENCQUOTEDPRINTABLE:
1491 mutt_decode_quoted (s, b->length, istext || ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b)), cd);
1494 mutt_decode_base64 (s, b->length, istext || ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b)), cd);
1497 mutt_decode_uuencoded (s, b->length, istext || ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b)), cd);
1500 mutt_decode_xbit (s, b->length, istext || ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b)), cd);
1504 if (cd != (iconv_t)(-1))
1508 int mutt_body_handler (BODY *b, STATE *s)
1513 char tempfile[_POSIX_PATH_MAX];
1514 handler_t handler = NULL;
1516 size_t tmplength = 0;
1520 int oflags = s->flags;
1522 /* first determine which handler to use to process this part */
1524 snprintf (type, sizeof (type), "%s/%s", TYPE (b), b->subtype);
1525 if (mutt_is_autoview (b, type))
1527 rfc1524_entry *entry = rfc1524_new_entry ();
1529 if (rfc1524_mailcap_lookup (b, type, entry, M_AUTOVIEW))
1531 handler = autoview_handler;
1532 s->flags &= ~M_CHARCONV;
1534 rfc1524_free_entry (&entry);
1536 else if (b->type == TYPETEXT)
1538 if (ascii_strcasecmp ("plain", b->subtype) == 0)
1540 /* avoid copying this part twice since removing the transfer-encoding is
1541 * the only operation needed.
1543 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b))
1544 handler = crypt_pgp_application_pgp_handler;
1545 else if (ascii_strcasecmp ("flowed", mutt_get_parameter ("format", b->parameter)) == 0)
1546 handler = rfc3676_handler;
1550 else if (ascii_strcasecmp ("enriched", b->subtype) == 0)
1551 handler = text_enriched_handler;
1552 else /* text body type without a handler */
1555 else if (b->type == TYPEMESSAGE)
1557 if(mutt_is_message_type(b->type, b->subtype))
1558 handler = message_handler;
1559 else if (!ascii_strcasecmp ("delivery-status", b->subtype))
1561 else if (!ascii_strcasecmp ("external-body", b->subtype))
1562 handler = external_body_handler;
1564 else if (b->type == TYPEMULTIPART)
1568 if (ascii_strcasecmp ("alternative", b->subtype) == 0)
1569 handler = alternative_handler;
1570 else if (WithCrypto && ascii_strcasecmp ("signed", b->subtype) == 0)
1572 p = mutt_get_parameter ("protocol", b->parameter);
1575 mutt_error _("Error: multipart/signed has no protocol.");
1576 else if (s->flags & M_VERIFY)
1577 handler = mutt_signed_handler;
1579 else if ((WithCrypto & APPLICATION_PGP)
1580 && ascii_strcasecmp ("encrypted", b->subtype) == 0)
1582 p = mutt_get_parameter ("protocol", b->parameter);
1585 mutt_error _("Error: multipart/encrypted has no protocol parameter!");
1586 else if (ascii_strcasecmp ("application/pgp-encrypted", p) == 0)
1587 handler = crypt_pgp_encrypted_handler;
1591 handler = multipart_handler;
1593 if (b->encoding != ENC7BIT && b->encoding != ENC8BIT
1594 && b->encoding != ENCBINARY)
1596 dprint (1, (debugfile, "Bad encoding type %d for multipart entity, "
1597 "assuming 7 bit\n", b->encoding));
1598 b->encoding = ENC7BIT;
1601 else if (WithCrypto && b->type == TYPEAPPLICATION)
1603 if (option (OPTDONTHANDLEPGPKEYS)
1604 && !ascii_strcasecmp("pgp-keys", b->subtype))
1606 /* pass raw part through for key extraction */
1609 else if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b))
1610 handler = crypt_pgp_application_pgp_handler;
1611 else if ((WithCrypto & APPLICATION_SMIME) && mutt_is_application_smime(b))
1612 handler = crypt_smime_application_smime_handler;
1616 if (plaintext || handler)
1618 fseeko (s->fpin, b->offset, 0);
1620 /* see if we need to decode this part before processing it */
1621 if (b->encoding == ENCBASE64 || b->encoding == ENCQUOTEDPRINTABLE ||
1622 b->encoding == ENCUUENCODED || plaintext ||
1623 mutt_is_text_part (b)) /* text subtypes may
1625 * set conversion even
1626 * with 8bit encoding.
1629 int origType = b->type;
1630 char *savePrefix = NULL;
1634 /* decode to a tempfile, saving the original destination */
1636 mutt_mktemp (tempfile);
1637 if ((s->fpout = safe_fopen (tempfile, "w")) == NULL)
1639 mutt_error _("Unable to open temporary file!");
1640 dprint (1, (debugfile, "Can't open %s.\n", tempfile));
1643 /* decoding the attachment changes the size and offset, so save a copy
1644 * of the "real" values now, and restore them after processing
1646 tmplength = b->length;
1647 tmpoffset = b->offset;
1649 /* if we are decoding binary bodies, we don't want to prefix each
1650 * line with the prefix or else the data will get corrupted.
1652 savePrefix = s->prefix;
1660 mutt_decode_attachment (b, s);
1664 b->length = ftello (s->fpout);
1668 /* restore final destination and substitute the tempfile for input */
1671 s->fpin = fopen (tempfile, "r");
1674 /* restore the prefix */
1675 s->prefix = savePrefix;
1681 /* process the (decoded) body part */
1684 rc = handler (b, s);
1688 dprint (1, (debugfile, "Failed on attachment of type %s/%s.\n", TYPE(b), NONULL (b->subtype)));
1693 b->length = tmplength;
1694 b->offset = tmpoffset;
1696 /* restore the original source stream */
1701 s->flags |= M_FIRSTDONE;
1703 else if (s->flags & M_DISPLAY)
1705 state_mark_attach (s);
1706 state_printf (s, _("[-- %s/%s is unsupported "), TYPE (b), b->subtype);
1707 if (!option (OPTVIEWATTACH))
1709 if (km_expand_key (type, sizeof(type),
1710 km_find_func (MENU_PAGER, OP_VIEW_ATTACHMENTS)))
1711 fprintf (s->fpout, _("(use '%s' to view this part)"), type);
1713 fputs (_("(need 'view-attachments' bound to key!)"), s->fpout);
1715 fputs (" --]\n", s->fpout);
1719 s->flags = oflags | (s->flags & M_FIRSTDONE);
1722 dprint (1, (debugfile, "Bailing on attachment of type %s/%s.\n", TYPE(b), NONULL (b->subtype)));