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')
195 /* neither \r nor \n as part of line-terminating CRLF
196 * may be qp-encoded, so remove \r and \n-terminate;
197 * see RfC2045, sect. 6.7, (1): General 8bit representation */
198 if (kind == 0 && c == '\r')
209 * Decode an attachment encoded with quoted-printable.
211 * Why doesn't this overflow any buffers? First, it's guaranteed
212 * that the length of a line grows when you _en_-code it to
213 * quoted-printable. That means that we always can store the
214 * result in a buffer of at most the _same_ size.
216 * Now, we don't special-case if the line we read with fgets()
217 * isn't terminated. We don't care about this, since STRING > 78,
218 * so corrupted input will just be corrupted a bit more. That
219 * implies that STRING+1 bytes are always sufficient to store the
220 * result of qp_decode_line.
222 * Finally, at soft line breaks, some part of a multibyte character
223 * may have been left over by mutt_convert_to_state(). This shouldn't
224 * be more than 6 characters, so STRING + 7 should be sufficient
225 * memory to store the decoded data.
227 * Just to make sure that I didn't make some off-by-one error
228 * above, we just use STRING*2 for the target buffer's size.
232 static void mutt_decode_quoted (STATE *s, long len, int istext, iconv_t cd)
235 char decline[2*STRING];
237 size_t linelen; /* number of input bytes in `line' */
240 int last; /* store the last character in the input line */
250 * It's ok to use a fixed size buffer for input, even if the line turns
251 * out to be longer than this. Just process the line in chunks. This
252 * really shouldn't happen according the MIME spec, since Q-P encoded
253 * lines are at most 76 characters, but we should be liberal about what
256 if (fgets (line, MIN ((ssize_t)sizeof (line), len + 1), s->fpin) == NULL)
259 linelen = strlen(line);
263 * inspect the last character we read so we can tell if we got the
266 last = linelen ? line[linelen - 1] : 0;
268 /* chop trailing whitespace if we got the full line */
271 while (linelen > 0 && ISSPACE (line[linelen-1]))
276 /* decode and do character set conversion */
277 qp_decode_line (decline + l, line, &l3, last);
279 mutt_convert_to_state (cd, decline, &l, s);
282 mutt_convert_to_state (cd, 0, 0, s);
283 state_reset_prefix(s);
286 void mutt_decode_base64 (STATE *s, long len, int istext, iconv_t cd)
289 int c1, c2, c3, c4, ch, cr = 0, i;
290 char bufi[BUFI_SIZE];
300 for (i = 0 ; i < 4 && len > 0 ; len--)
302 if ((ch = fgetc (s->fpin)) == EOF)
304 if (ch >= 0 && ch < 128 && (base64val(ch) != -1 || ch == '='))
309 dprint (2, (debugfile, "%s:%d [mutt_decode_base64()]: "
310 "didn't get a multiple of 4 chars.\n", __FILE__, __LINE__));
314 c1 = base64val (buf[0]);
315 c2 = base64val (buf[1]);
316 ch = (c1 << 2) | (c2 >> 4);
318 if (cr && ch != '\n')
323 if (istext && ch == '\r')
330 c3 = base64val (buf[2]);
331 ch = ((c2 & 0xf) << 4) | (c3 >> 2);
333 if (cr && ch != '\n')
338 if (istext && ch == '\r')
343 if (buf[3] == '=') break;
344 c4 = base64val (buf[3]);
345 ch = ((c3 & 0x3) << 6) | c4;
347 if (cr && ch != '\n')
351 if (istext && ch == '\r')
356 if (l + 8 >= sizeof (bufi))
357 mutt_convert_to_state (cd, bufi, &l, s);
360 if (cr) bufi[l++] = '\r';
362 mutt_convert_to_state (cd, bufi, &l, s);
363 mutt_convert_to_state (cd, 0, 0, s);
365 state_reset_prefix(s);
368 static unsigned char decode_byte (char ch)
375 static void mutt_decode_uuencoded (STATE *s, long len, int istext, iconv_t cd)
377 char tmps[SHORT_STRING];
378 char linelen, c, l, out;
380 char bufi[BUFI_SIZE];
388 if ((fgets(tmps, sizeof(tmps), s->fpin)) == NULL)
390 len -= mutt_strlen(tmps);
391 if ((!mutt_strncmp (tmps, "begin", 5)) && ISSPACE (tmps[5]))
396 if ((fgets(tmps, sizeof(tmps), s->fpin)) == NULL)
398 len -= mutt_strlen(tmps);
399 if (!mutt_strncmp (tmps, "end", 3))
402 linelen = decode_byte (*pt);
404 for (c = 0; c < linelen;)
406 for (l = 2; l <= 6; l += 2)
408 out = decode_byte (*pt) << l;
410 out |= (decode_byte (*pt) >> (6 - l));
416 mutt_convert_to_state (cd, bufi, &k, s);
421 mutt_convert_to_state (cd, bufi, &k, s);
422 mutt_convert_to_state (cd, 0, 0, s);
424 state_reset_prefix(s);
427 /* ----------------------------------------------------------------------------
428 * A (not so) minimal implementation of RFC1563.
431 #define IndentSize (4)
433 enum { RICH_PARAM=0, RICH_BOLD, RICH_UNDERLINE, RICH_ITALIC, RICH_NOFILL,
434 RICH_INDENT, RICH_INDENT_RIGHT, RICH_EXCERPT, RICH_CENTER, RICH_FLUSHLEFT,
435 RICH_FLUSHRIGHT, RICH_COLOR, RICH_LAST_TAG };
438 const wchar_t *tag_name;
441 { L"param", RICH_PARAM },
442 { L"bold", RICH_BOLD },
443 { L"italic", RICH_ITALIC },
444 { L"underline", RICH_UNDERLINE },
445 { L"nofill", RICH_NOFILL },
446 { L"excerpt", RICH_EXCERPT },
447 { L"indent", RICH_INDENT },
448 { L"indentright", RICH_INDENT_RIGHT },
449 { L"center", RICH_CENTER },
450 { L"flushleft", RICH_FLUSHLEFT },
451 { L"flushright", RICH_FLUSHRIGHT },
452 { L"flushboth", RICH_FLUSHLEFT },
453 { L"color", RICH_COLOR },
454 { L"x-color", RICH_COLOR },
458 struct enriched_state
472 int tag_level[RICH_LAST_TAG];
477 static void enriched_wrap (struct enriched_state *stte)
484 if (stte->tag_level[RICH_CENTER] || stte->tag_level[RICH_FLUSHRIGHT])
486 /* Strip trailing white space */
487 size_t y = stte->line_used - 1;
489 while (y && iswspace (stte->line[y]))
491 stte->line[y] = (wchar_t) '\0';
496 if (stte->tag_level[RICH_CENTER])
498 /* Strip leading whitespace */
501 while (stte->line[y] && iswspace (stte->line[y]))
507 for (z = y ; z <= stte->line_used; z++)
509 stte->line[z - y] = stte->line[z];
513 stte->line_used -= y;
518 extra = stte->WrapMargin - stte->line_len - stte->indent_len -
519 (stte->tag_level[RICH_INDENT_RIGHT] * IndentSize);
522 if (stte->tag_level[RICH_CENTER])
527 state_putc (' ', stte->s);
531 else if (stte->tag_level[RICH_FLUSHRIGHT])
536 state_putc (' ', stte->s);
541 state_putws ((const wchar_t*) stte->line, stte->s);
544 state_putc ('\n', stte->s);
545 stte->line[0] = (wchar_t) '\0';
548 stte->indent_len = 0;
551 state_puts (stte->s->prefix, stte->s);
552 stte->indent_len += mutt_strlen (stte->s->prefix);
555 if (stte->tag_level[RICH_EXCERPT])
557 x = stte->tag_level[RICH_EXCERPT];
562 state_puts (stte->s->prefix, stte->s);
563 stte->indent_len += mutt_strlen (stte->s->prefix);
567 state_puts ("> ", stte->s);
568 stte->indent_len += mutt_strlen ("> ");
574 stte->indent_len = 0;
575 if (stte->tag_level[RICH_INDENT])
577 x = stte->tag_level[RICH_INDENT] * IndentSize;
578 stte->indent_len += x;
581 state_putc (' ', stte->s);
587 static void enriched_flush (struct enriched_state *stte, int wrap)
589 if (!stte->tag_level[RICH_NOFILL] && (stte->line_len + stte->word_len >
590 (stte->WrapMargin - (stte->tag_level[RICH_INDENT_RIGHT] * IndentSize) -
592 enriched_wrap (stte);
596 stte->buffer[stte->buff_used] = (wchar_t) '\0';
597 stte->line_used += stte->buff_used;
598 if (stte->line_used > stte->line_max)
600 stte->line_max = stte->line_used;
601 safe_realloc (&stte->line, (stte->line_max + 1) * sizeof (wchar_t));
603 wcscat (stte->line, stte->buffer);
604 stte->line_len += stte->word_len;
610 fflush (stte->s->fpout);
614 static void enriched_putwc (wchar_t c, struct enriched_state *stte)
616 if (stte->tag_level[RICH_PARAM])
618 if (stte->tag_level[RICH_COLOR])
620 if (stte->param_used + 1 >= stte->param_len)
621 safe_realloc (&stte->param, (stte->param_len += STRING) * sizeof (wchar_t));
623 stte->param[stte->param_used++] = c;
625 return; /* nothing to do */
628 /* see if more space is needed (plus extra for possible rich characters) */
629 if (stte->buff_len < stte->buff_used + 3)
631 stte->buff_len += LONG_STRING;
632 safe_realloc (&stte->buffer, (stte->buff_len + 1) * sizeof (wchar_t));
635 if ((!stte->tag_level[RICH_NOFILL] && iswspace (c)) || c == (wchar_t) '\0')
637 if (c == (wchar_t) '\t')
638 stte->word_len += 8 - (stte->line_len + stte->word_len) % 8;
642 stte->buffer[stte->buff_used++] = c;
643 enriched_flush (stte, 0);
647 if (stte->s->flags & M_DISPLAY)
649 if (stte->tag_level[RICH_BOLD])
651 stte->buffer[stte->buff_used++] = c;
652 stte->buffer[stte->buff_used++] = (wchar_t) '\010';
653 stte->buffer[stte->buff_used++] = c;
655 else if (stte->tag_level[RICH_UNDERLINE])
658 stte->buffer[stte->buff_used++] = '_';
659 stte->buffer[stte->buff_used++] = (wchar_t) '\010';
660 stte->buffer[stte->buff_used++] = c;
662 else if (stte->tag_level[RICH_ITALIC])
664 stte->buffer[stte->buff_used++] = c;
665 stte->buffer[stte->buff_used++] = (wchar_t) '\010';
666 stte->buffer[stte->buff_used++] = '_';
670 stte->buffer[stte->buff_used++] = c;
675 stte->buffer[stte->buff_used++] = c;
681 static void enriched_puts (const char *s, struct enriched_state *stte)
685 if (stte->buff_len < stte->buff_used + mutt_strlen (s))
687 stte->buff_len += LONG_STRING;
688 safe_realloc (&stte->buffer, (stte->buff_len + 1) * sizeof (wchar_t));
693 stte->buffer[stte->buff_used++] = (wchar_t) *c;
698 static void enriched_set_flags (const wchar_t *tag, struct enriched_state *stte)
700 const wchar_t *tagptr = tag;
703 if (*tagptr == (wchar_t) '/')
706 for (i = 0, j = -1; EnrichedTags[i].tag_name; i++)
707 if (wcscasecmp (EnrichedTags[i].tag_name, tagptr) == 0)
709 j = EnrichedTags[i].index;
715 if (j == RICH_CENTER || j == RICH_FLUSHLEFT || j == RICH_FLUSHRIGHT)
716 enriched_flush (stte, 1);
718 if (*tag == (wchar_t) '/')
720 if (stte->tag_level[j]) /* make sure not to go negative */
721 stte->tag_level[j]--;
722 if ((stte->s->flags & M_DISPLAY) && j == RICH_PARAM && stte->tag_level[RICH_COLOR])
724 stte->param[stte->param_used] = (wchar_t) '\0';
725 if (!wcscasecmp(L"black", stte->param))
727 enriched_puts("\033[30m", stte);
729 else if (!wcscasecmp(L"red", stte->param))
731 enriched_puts("\033[31m", stte);
733 else if (!wcscasecmp(L"green", stte->param))
735 enriched_puts("\033[32m", stte);
737 else if (!wcscasecmp(L"yellow", stte->param))
739 enriched_puts("\033[33m", stte);
741 else if (!wcscasecmp(L"blue", stte->param))
743 enriched_puts("\033[34m", stte);
745 else if (!wcscasecmp(L"magenta", stte->param))
747 enriched_puts("\033[35m", stte);
749 else if (!wcscasecmp(L"cyan", stte->param))
751 enriched_puts("\033[36m", stte);
753 else if (!wcscasecmp(L"white", stte->param))
755 enriched_puts("\033[37m", stte);
758 if ((stte->s->flags & M_DISPLAY) && j == RICH_COLOR)
760 enriched_puts("\033[0m", stte);
763 /* flush parameter buffer when closing the tag */
766 stte->param_used = 0;
767 stte->param[0] = (wchar_t) '\0';
771 stte->tag_level[j]++;
773 if (j == RICH_EXCERPT)
774 enriched_flush(stte, 1);
778 static int text_enriched_handler (BODY *a, STATE *s)
781 TEXT, LANGLE, TAG, BOGUS_TAG, NEWLINE, ST_EOF, DONE
784 long bytes = a->length;
785 struct enriched_state stte;
788 wchar_t tag[LONG_STRING + 1];
790 memset (&stte, 0, sizeof (stte));
792 stte.WrapMargin = ((s->flags & M_DISPLAY) ? (COLS-4) : ((COLS-4)<72)?(COLS-4):72);
793 stte.line_max = stte.WrapMargin * 4;
794 stte.line = (wchar_t *) safe_calloc (1, (stte.line_max + 1) * sizeof (wchar_t));
795 stte.param = (wchar_t *) safe_calloc (1, (STRING) * sizeof (wchar_t));
797 stte.param_len = STRING;
802 state_puts (s->prefix, s);
803 stte.indent_len += mutt_strlen (s->prefix);
806 while (state != DONE)
810 if (!bytes || (wc = fgetwc (s->fpin)) == EOF)
826 if (stte.tag_level[RICH_NOFILL])
828 enriched_flush (&stte, 1);
832 enriched_putwc ((wchar_t) ' ', &stte);
838 enriched_putwc (wc, &stte);
843 if (wc == (wchar_t) '<')
845 enriched_putwc (wc, &stte);
854 /* Yes, fall through (it wasn't a <<, so this char is first in TAG) */
856 if (wc == (wchar_t) '>')
858 tag[tag_len] = (wchar_t) '\0';
859 enriched_set_flags (tag, &stte);
862 else if (tag_len < LONG_STRING) /* ignore overly long tags */
869 if (wc == (wchar_t) '>')
874 if (wc == (wchar_t) '\n')
875 enriched_flush (&stte, 1);
878 ungetwc (wc, s->fpin);
885 enriched_putwc ((wchar_t) '\0', &stte);
886 enriched_flush (&stte, 1);
890 case DONE: /* not reached, but gcc complains if this is absent */
895 state_putc ('\n', s); /* add a final newline */
897 FREE (&(stte.buffer));
899 FREE (&(stte.param));
906 #define TXTENRICHED 3
908 static int alternative_handler (BODY *a, STATE *s)
918 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
919 a->encoding == ENCUUENCODED)
923 fstat (fileno (s->fpin), &st);
924 b = mutt_new_body ();
925 b->length = (long) st.st_size;
926 b->parts = mutt_parse_multipart (s->fpin,
927 mutt_get_parameter ("boundary", a->parameter),
928 (long) st.st_size, ascii_strcasecmp ("digest", a->subtype) == 0);
935 /* First, search list of prefered types */
936 t = AlternativeOrderList;
940 int btlen; /* length of basetype */
941 int wild; /* do we have a wildcard to match all subtypes? */
943 c = strchr (t->data, '/');
946 wild = (c[1] == '*' && c[2] == 0);
952 btlen = mutt_strlen (t->data);
961 const char *bt = TYPE(b);
962 if (!ascii_strncasecmp (bt, t->data, btlen) && bt[btlen] == 0)
964 /* the basetype matches */
965 if (wild || !ascii_strcasecmp (t->data + btlen + 1, b->subtype))
975 /* Next, look for an autoviewable type */
984 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
985 if (mutt_is_autoview (b, buf))
987 rfc1524_entry *entry = rfc1524_new_entry ();
989 if (rfc1524_mailcap_lookup (b, buf, entry, M_AUTOVIEW))
993 rfc1524_free_entry (&entry);
999 /* Then, look for a text entry */
1008 if (b->type == TYPETEXT)
1010 if (! ascii_strcasecmp ("plain", b->subtype) && type <= TXTPLAIN)
1015 else if (! ascii_strcasecmp ("enriched", b->subtype) && type <= TXTENRICHED)
1020 else if (! ascii_strcasecmp ("html", b->subtype) && type <= TXTHTML)
1030 /* Finally, look for other possibilities */
1039 if (mutt_can_decode (b))
1047 if (s->flags & M_DISPLAY && !option (OPTWEED))
1049 fseeko (s->fpin, choice->hdr_offset, 0);
1050 mutt_copy_bytes(s->fpin, s->fpout, choice->offset-choice->hdr_offset);
1052 mutt_body_handler (choice, s);
1054 else if (s->flags & M_DISPLAY)
1056 /* didn't find anything that we could display! */
1057 state_mark_attach (s);
1058 state_puts(_("[-- Error: Could not display any parts of Multipart/Alternative! --]\n"), s);
1068 /* handles message/rfc822 body parts */
1069 static int message_handler (BODY *a, STATE *s)
1076 off_start = ftello (s->fpin);
1077 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1078 a->encoding == ENCUUENCODED)
1080 fstat (fileno (s->fpin), &st);
1081 b = mutt_new_body ();
1082 b->length = (LOFF_T) st.st_size;
1083 b->parts = mutt_parse_messageRFC822 (s->fpin, b);
1090 mutt_copy_hdr (s->fpin, s->fpout, off_start, b->parts->offset,
1091 (((s->flags & M_WEED) || ((s->flags & (M_DISPLAY|M_PRINTING)) && option (OPTWEED))) ? (CH_WEED | CH_REORDER) : 0) |
1092 (s->prefix ? CH_PREFIX : 0) | CH_DECODE | CH_FROM |
1093 (s->flags & M_DISPLAY) ? CH_DISPLAY : 0, s->prefix);
1096 state_puts (s->prefix, s);
1097 state_putc ('\n', s);
1099 rc = mutt_body_handler (b->parts, s);
1102 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1103 a->encoding == ENCUUENCODED)
1104 mutt_free_body (&b);
1109 /* returns 1 if decoding the attachment will produce output */
1110 int mutt_can_decode (BODY *a)
1114 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
1115 if (mutt_is_autoview (a, type))
1116 return (rfc1524_mailcap_lookup (a, type, NULL, M_AUTOVIEW));
1117 else if (a->type == TYPETEXT)
1119 else if (a->type == TYPEMESSAGE)
1121 else if (a->type == TYPEMULTIPART)
1127 if (ascii_strcasecmp (a->subtype, "signed") == 0 ||
1128 ascii_strcasecmp (a->subtype, "encrypted") == 0)
1132 for (p = a->parts; p; p = p->next)
1134 if (mutt_can_decode (p))
1139 else if (WithCrypto && a->type == TYPEAPPLICATION)
1141 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp(a))
1143 if ((WithCrypto & APPLICATION_SMIME) && mutt_is_application_smime(a))
1150 static int multipart_handler (BODY *a, STATE *s)
1158 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1159 a->encoding == ENCUUENCODED)
1161 fstat (fileno (s->fpin), &st);
1162 b = mutt_new_body ();
1163 b->length = (long) st.st_size;
1164 b->parts = mutt_parse_multipart (s->fpin,
1165 mutt_get_parameter ("boundary", a->parameter),
1166 (long) st.st_size, ascii_strcasecmp ("digest", a->subtype) == 0);
1171 for (p = b->parts, count = 1; p; p = p->next, count++)
1173 if (s->flags & M_DISPLAY)
1175 state_mark_attach (s);
1176 state_printf (s, _("[-- Attachment #%d"), count);
1177 if (p->description || p->filename || p->form_name)
1179 state_puts (": ", s);
1180 state_puts (p->description ? p->description :
1181 p->filename ? p->filename : p->form_name, s);
1183 state_puts (" --]\n", s);
1185 mutt_pretty_size (length, sizeof (length), p->length);
1187 state_mark_attach (s);
1188 state_printf (s, _("[-- Type: %s/%s, Encoding: %s, Size: %s --]\n"),
1189 TYPE (p), p->subtype, ENCODING (p->encoding), length);
1190 if (!option (OPTWEED))
1192 fseeko (s->fpin, p->hdr_offset, 0);
1193 mutt_copy_bytes(s->fpin, s->fpout, p->offset-p->hdr_offset);
1196 state_putc ('\n', s);
1199 rc = mutt_body_handler (p, s);
1200 state_putc ('\n', s);
1204 mutt_error (_("One or more parts of this message could not be displayed"));
1205 dprint (1, (debugfile, "Failed on attachment #%d, type %s/%s.\n", count, TYPE(p), NONULL (p->subtype)));
1208 if ((s->flags & M_REPLYING)
1209 && (option (OPTINCLUDEONLYFIRST)) && (s->flags & M_FIRSTDONE))
1213 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1214 a->encoding == ENCUUENCODED)
1215 mutt_free_body (&b);
1217 /* make failure of a single part non-fatal */
1223 static int autoview_handler (BODY *a, STATE *s)
1225 rfc1524_entry *entry = rfc1524_new_entry ();
1226 char buffer[LONG_STRING];
1228 char command[LONG_STRING];
1229 char tempfile[_POSIX_PATH_MAX] = "";
1238 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
1239 rfc1524_mailcap_lookup (a, type, entry, M_AUTOVIEW);
1241 fname = safe_strdup (a->filename);
1242 mutt_sanitize_filename (fname, 1);
1243 rfc1524_expand_filename (entry->nametemplate, fname, tempfile, sizeof (tempfile));
1248 strfcpy (command, entry->command, sizeof (command));
1250 /* rfc1524_expand_command returns 0 if the file is required */
1251 piped = rfc1524_expand_command (a, tempfile, type, command, sizeof (command));
1253 if (s->flags & M_DISPLAY)
1255 state_mark_attach (s);
1256 state_printf (s, _("[-- Autoview using %s --]\n"), command);
1257 mutt_message(_("Invoking autoview command: %s"),command);
1260 if ((fpin = safe_fopen (tempfile, "w+")) == NULL)
1262 mutt_perror ("fopen");
1263 rfc1524_free_entry (&entry);
1267 mutt_copy_bytes (s->fpin, fpin, a->length);
1271 safe_fclose (&fpin);
1272 thepid = mutt_create_filter (command, NULL, &fpout, &fperr);
1279 thepid = mutt_create_filter_fd (command, NULL, &fpout, &fperr,
1280 fileno(fpin), -1, -1);
1285 mutt_perror _("Can't create filter");
1286 if (s->flags & M_DISPLAY)
1288 state_mark_attach (s);
1289 state_printf (s, _("[-- Can't run %s. --]\n"), command);
1297 while (fgets (buffer, sizeof(buffer), fpout) != NULL)
1299 state_puts (s->prefix, s);
1300 state_puts (buffer, s);
1302 /* check for data on stderr */
1303 if (fgets (buffer, sizeof(buffer), fperr))
1305 if (s->flags & M_DISPLAY)
1307 state_mark_attach (s);
1308 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1311 state_puts (s->prefix, s);
1312 state_puts (buffer, s);
1313 while (fgets (buffer, sizeof(buffer), fperr) != NULL)
1315 state_puts (s->prefix, s);
1316 state_puts (buffer, s);
1322 mutt_copy_stream (fpout, s->fpout);
1323 /* Check for stderr messages */
1324 if (fgets (buffer, sizeof(buffer), fperr))
1326 if (s->flags & M_DISPLAY)
1328 state_mark_attach (s);
1329 state_printf (s, _("[-- Autoview stderr of %s --]\n"),
1333 state_puts (buffer, s);
1334 mutt_copy_stream (fperr, s->fpout);
1339 safe_fclose (&fpout);
1340 safe_fclose (&fperr);
1342 mutt_wait_filter (thepid);
1344 safe_fclose (&fpin);
1346 mutt_unlink (tempfile);
1348 if (s->flags & M_DISPLAY)
1349 mutt_clear_error ();
1351 rfc1524_free_entry (&entry);
1356 static int external_body_handler (BODY *b, STATE *s)
1358 const char *access_type;
1359 const char *expiration;
1362 access_type = mutt_get_parameter ("access-type", b->parameter);
1365 if (s->flags & M_DISPLAY)
1367 state_mark_attach (s);
1368 state_puts (_("[-- Error: message/external-body has no access-type parameter --]\n"), s);
1375 expiration = mutt_get_parameter ("expiration", b->parameter);
1377 expire = mutt_parse_date (expiration, NULL);
1381 if (!ascii_strcasecmp (access_type, "x-mutt-deleted"))
1383 if (s->flags & (M_DISPLAY|M_PRINTING))
1386 char pretty_size[10];
1388 state_mark_attach (s);
1389 state_printf (s, _("[-- This %s/%s attachment "),
1390 TYPE(b->parts), b->parts->subtype);
1391 length = mutt_get_parameter ("length", b->parameter);
1394 mutt_pretty_size (pretty_size, sizeof (pretty_size),
1395 strtol (length, NULL, 10));
1396 state_printf (s, _("(size %s bytes) "), pretty_size);
1398 state_puts (_("has been deleted --]\n"), s);
1402 state_mark_attach (s);
1403 state_printf (s, _("[-- on %s --]\n"), expiration);
1405 if (b->parts->filename)
1407 state_mark_attach (s);
1408 state_printf (s, _("[-- name: %s --]\n"), b->parts->filename);
1411 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1412 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1416 else if(expiration && expire < time(NULL))
1418 if (s->flags & M_DISPLAY)
1420 state_mark_attach (s);
1421 state_printf (s, _("[-- This %s/%s attachment is not included, --]\n"),
1422 TYPE(b->parts), b->parts->subtype);
1423 state_attach_puts (_("[-- and the indicated external source has --]\n"
1424 "[-- expired. --]\n"), s);
1426 mutt_copy_hdr(s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1427 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1428 CH_DECODE | CH_DISPLAY, NULL);
1433 if (s->flags & M_DISPLAY)
1435 state_mark_attach (s);
1437 _("[-- This %s/%s attachment is not included, --]\n"),
1438 TYPE (b->parts), b->parts->subtype);
1439 state_mark_attach (s);
1441 _("[-- and the indicated access-type %s is unsupported --]\n"),
1443 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1444 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1445 CH_DECODE | CH_DISPLAY, NULL);
1452 void mutt_decode_attachment (BODY *b, STATE *s)
1454 int istext = mutt_is_text_part (b);
1455 iconv_t cd = (iconv_t)(-1);
1457 if (istext && s->flags & M_CHARCONV)
1459 char *charset = mutt_get_parameter ("charset", b->parameter);
1460 if (!charset && AssumedCharset && *AssumedCharset)
1461 charset = mutt_get_default_charset ();
1462 if (charset && Charset)
1463 cd = mutt_iconv_open (Charset, charset, M_ICONV_HOOK_FROM);
1465 else if (istext && b->charset)
1466 cd = mutt_iconv_open (Charset, b->charset, M_ICONV_HOOK_FROM);
1468 fseeko (s->fpin, b->offset, 0);
1469 switch (b->encoding)
1471 case ENCQUOTEDPRINTABLE:
1472 mutt_decode_quoted (s, b->length, istext || ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b)), cd);
1475 mutt_decode_base64 (s, b->length, istext || ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b)), cd);
1478 mutt_decode_uuencoded (s, b->length, istext || ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b)), cd);
1481 mutt_decode_xbit (s, b->length, istext || ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b)), cd);
1485 if (cd != (iconv_t)(-1))
1489 /* when generating format=flowed ($text_flowed is set) from format=fixed,
1490 * strip all trailing spaces to improve interoperability;
1491 * if $text_flowed is unset, simply verbatim copy input
1493 static int text_plain_handler (BODY *b, STATE *s)
1495 char buf[LONG_STRING];
1498 while (fgets (buf, sizeof (buf), s->fpin))
1500 l = mutt_strlen (buf);
1501 if (l > 0 && buf[l-1] == '\n')
1503 if (option (OPTTEXTFLOWED))
1505 while (l > 0 && buf[l-1] == ' ')
1509 state_puts (s->prefix, s);
1510 state_puts (buf, s);
1511 state_putc ('\n', s);
1517 int mutt_body_handler (BODY *b, STATE *s)
1522 char tempfile[_POSIX_PATH_MAX];
1523 handler_t handler = NULL;
1525 size_t tmplength = 0;
1529 int oflags = s->flags;
1531 /* first determine which handler to use to process this part */
1533 snprintf (type, sizeof (type), "%s/%s", TYPE (b), b->subtype);
1534 if (mutt_is_autoview (b, type))
1536 rfc1524_entry *entry = rfc1524_new_entry ();
1538 if (rfc1524_mailcap_lookup (b, type, entry, M_AUTOVIEW))
1540 handler = autoview_handler;
1541 s->flags &= ~M_CHARCONV;
1543 rfc1524_free_entry (&entry);
1545 else if (b->type == TYPETEXT)
1547 if (ascii_strcasecmp ("plain", b->subtype) == 0)
1549 /* avoid copying this part twice since removing the transfer-encoding is
1550 * the only operation needed.
1552 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b))
1553 handler = crypt_pgp_application_pgp_handler;
1554 else if (ascii_strcasecmp ("flowed", mutt_get_parameter ("format", b->parameter)) == 0)
1555 handler = rfc3676_handler;
1557 handler = text_plain_handler;
1559 else if (ascii_strcasecmp ("enriched", b->subtype) == 0)
1560 handler = text_enriched_handler;
1561 else /* text body type without a handler */
1564 else if (b->type == TYPEMESSAGE)
1566 if(mutt_is_message_type(b->type, b->subtype))
1567 handler = message_handler;
1568 else if (!ascii_strcasecmp ("delivery-status", b->subtype))
1570 else if (!ascii_strcasecmp ("external-body", b->subtype))
1571 handler = external_body_handler;
1573 else if (b->type == TYPEMULTIPART)
1577 if (ascii_strcasecmp ("alternative", b->subtype) == 0)
1578 handler = alternative_handler;
1579 else if (WithCrypto && ascii_strcasecmp ("signed", b->subtype) == 0)
1581 p = mutt_get_parameter ("protocol", b->parameter);
1584 mutt_error _("Error: multipart/signed has no protocol.");
1585 else if (s->flags & M_VERIFY)
1586 handler = mutt_signed_handler;
1588 else if ((WithCrypto & APPLICATION_PGP)
1589 && ascii_strcasecmp ("encrypted", b->subtype) == 0)
1591 p = mutt_get_parameter ("protocol", b->parameter);
1594 mutt_error _("Error: multipart/encrypted has no protocol parameter!");
1595 else if (ascii_strcasecmp ("application/pgp-encrypted", p) == 0)
1596 handler = crypt_pgp_encrypted_handler;
1600 handler = multipart_handler;
1602 if (b->encoding != ENC7BIT && b->encoding != ENC8BIT
1603 && b->encoding != ENCBINARY)
1605 dprint (1, (debugfile, "Bad encoding type %d for multipart entity, "
1606 "assuming 7 bit\n", b->encoding));
1607 b->encoding = ENC7BIT;
1610 else if (WithCrypto && b->type == TYPEAPPLICATION)
1612 if (option (OPTDONTHANDLEPGPKEYS)
1613 && !ascii_strcasecmp("pgp-keys", b->subtype))
1615 /* pass raw part through for key extraction */
1618 else if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b))
1619 handler = crypt_pgp_application_pgp_handler;
1620 else if ((WithCrypto & APPLICATION_SMIME) && mutt_is_application_smime(b))
1621 handler = crypt_smime_application_smime_handler;
1624 /* only respect disposition == attachment if we're not
1625 displaying from the attachment menu (i.e. pager) */
1626 if ((!option (OPTHONORDISP) || (b->disposition != DISPATTACH ||
1627 option(OPTVIEWATTACH))) &&
1628 (plaintext || handler))
1630 fseeko (s->fpin, b->offset, 0);
1632 /* see if we need to decode this part before processing it */
1633 if (b->encoding == ENCBASE64 || b->encoding == ENCQUOTEDPRINTABLE ||
1634 b->encoding == ENCUUENCODED || plaintext ||
1635 mutt_is_text_part (b)) /* text subtypes may
1637 * set conversion even
1638 * with 8bit encoding.
1641 int origType = b->type;
1642 char *savePrefix = NULL;
1646 /* decode to a tempfile, saving the original destination */
1648 mutt_mktemp (tempfile);
1649 if ((s->fpout = safe_fopen (tempfile, "w")) == NULL)
1651 mutt_error _("Unable to open temporary file!");
1652 dprint (1, (debugfile, "Can't open %s.\n", tempfile));
1655 /* decoding the attachment changes the size and offset, so save a copy
1656 * of the "real" values now, and restore them after processing
1658 tmplength = b->length;
1659 tmpoffset = b->offset;
1661 /* if we are decoding binary bodies, we don't want to prefix each
1662 * line with the prefix or else the data will get corrupted.
1664 savePrefix = s->prefix;
1672 mutt_decode_attachment (b, s);
1676 b->length = ftello (s->fpout);
1678 safe_fclose (&s->fpout);
1680 /* restore final destination and substitute the tempfile for input */
1683 s->fpin = fopen (tempfile, "r");
1686 /* restore the prefix */
1687 s->prefix = savePrefix;
1693 /* process the (decoded) body part */
1696 rc = handler (b, s);
1700 dprint (1, (debugfile, "Failed on attachment of type %s/%s.\n", TYPE(b), NONULL (b->subtype)));
1705 b->length = tmplength;
1706 b->offset = tmpoffset;
1708 /* restore the original source stream */
1709 safe_fclose (&s->fpin);
1713 s->flags |= M_FIRSTDONE;
1715 /* print hint to use attachment menu for disposition == attachment
1716 if we're not already being called from there */
1717 else if ((s->flags & M_DISPLAY) || (b->disposition == DISPATTACH &&
1718 !option (OPTVIEWATTACH) &&
1719 option (OPTHONORDISP) &&
1720 (plaintext || handler)))
1722 state_mark_attach (s);
1723 if (option (OPTHONORDISP) && b->disposition == DISPATTACH)
1724 fputs (_("[-- This is an attachment "), s->fpout);
1726 state_printf (s, _("[-- %s/%s is unsupported "), TYPE (b), b->subtype);
1727 if (!option (OPTVIEWATTACH))
1729 if (km_expand_key (type, sizeof(type),
1730 km_find_func (MENU_PAGER, OP_VIEW_ATTACHMENTS)))
1731 fprintf (s->fpout, _("(use '%s' to view this part)"), type);
1733 fputs (_("(need 'view-attachments' bound to key!)"), s->fpout);
1735 fputs (" --]\n", s->fpout);
1739 s->flags = oflags | (s->flags & M_FIRSTDONE);
1742 dprint (1, (debugfile, "Bailing on attachment of type %s/%s.\n", TYPE(b), NONULL (b->subtype)));