2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
21 #include <lib-lib/mem.h>
22 #include <lib-lib/ascii.h>
23 #include <lib-lib/str.h>
24 #include <lib-lib/macros.h>
25 #include <lib-lib/file.h>
28 #include "recvattach.h"
30 #include "mutt_curses.h"
37 #include "mutt_crypt.h"
42 #include "lib/debug.h"
44 typedef int handler_f (BODY *, STATE *);
45 typedef handler_f *handler_t;
47 int Index_hex[128] = {
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 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
51 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
52 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
53 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
54 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
55 -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, -1, -1, -1, -1, -1,
61 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
62 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
63 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
64 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
65 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
66 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1
69 void mutt_decode_xbit (STATE * s, long len, int istext, iconv_t cd)
78 while ((c = fgetc (s->fpin)) != EOF && len--) {
79 if (c == '\r' && len) {
80 if ((ch = fgetc (s->fpin)) == '\n') {
89 if (l == sizeof (bufi))
90 mutt_convert_to_state (cd, bufi, &l, s);
93 mutt_convert_to_state (cd, bufi, &l, s);
94 mutt_convert_to_state (cd, 0, 0, s);
96 state_reset_prefix (s);
99 mutt_copy_bytes (s->fpin, s->fpout, len);
102 static int qp_decode_triple (char *s, char *d)
104 /* soft line break */
105 if (*s == '=' && !(*(s + 1)))
108 /* quoted-printable triple */
110 isxdigit ((unsigned char) *(s + 1)) &&
111 isxdigit ((unsigned char) *(s + 2))) {
112 *d = (hexval (*(s + 1)) << 4) | hexval (*(s + 2));
120 static void qp_decode_line (char *dest, char *src, size_t * l, int last)
128 /* decode the line */
130 for (d = dest, s = src; *s;) {
131 switch ((kind = qp_decode_triple (s, &c))) {
135 break; /* qp triple */
138 break; /* single character */
142 break; /* soft line break */
146 if (!soft && last == '\n')
154 * Decode an attachment encoded with quoted-printable.
156 * Why doesn't this overflow any buffers? First, it's guaranteed
157 * that the length of a line grows when you _en_-code it to
158 * quoted-printable. That means that we always can store the
159 * result in a buffer of at most the _same_ size.
161 * Now, we don't special-case if the line we read with fgets()
162 * isn't terminated. We don't care about this, since STRING > 78,
163 * so corrupted input will just be corrupted a bit more. That
164 * implies that STRING+1 bytes are always sufficient to store the
165 * result of qp_decode_line.
167 * Finally, at soft line breaks, some part of a multibyte character
168 * may have been left over by mutt_convert_to_state(). This shouldn't
169 * be more than 6 characters, so STRING + 7 should be sufficient
170 * memory to store the decoded data.
172 * Just to make sure that I didn't make some off-by-one error
173 * above, we just use STRING*2 for the target buffer's size.
177 void mutt_decode_quoted (STATE * s, long len, int istext, iconv_t cd)
180 char decline[2 * STRING];
182 size_t linelen; /* number of input bytes in `line' */
185 int last; /* store the last character in the input line */
188 state_set_prefix (s);
194 * It's ok to use a fixed size buffer for input, even if the line turns
195 * out to be longer than this. Just process the line in chunks. This
196 * really shouldn't happen according the MIME spec, since Q-P encoded
197 * lines are at most 76 characters, but we should be liberal about what
200 if (fgets (line, MIN ((ssize_t) sizeof (line), len + 1), s->fpin) == NULL)
203 linelen = m_strlen(line);
207 * inspect the last character we read so we can tell if we got the
210 last = linelen ? line[linelen - 1] : 0;
212 /* chop trailing whitespace if we got the full line */
214 while (linelen > 0 && ISSPACE (line[linelen - 1]))
219 /* decode and do character set conversion */
220 qp_decode_line (decline + l, line, &l3, last);
222 mutt_convert_to_state (cd, decline, &l, s);
225 mutt_convert_to_state (cd, 0, 0, s);
226 state_reset_prefix (s);
229 void mutt_decode_base64 (STATE * s, long len, int istext, iconv_t cd)
232 int c1, c2, c3, c4, ch, cr = 0, i;
233 char bufi[BUFI_SIZE];
239 state_set_prefix (s);
242 for (i = 0; i < 4 && len > 0; len--) {
243 if ((ch = fgetc (s->fpin)) == EOF)
245 if (ch >= 0 && ch < 128 && (base64val (ch) != -1 || ch == '='))
249 debug_print (2, ("didn't get a multiple of 4 chars.\n"));
253 c1 = base64val (buf[0]);
254 c2 = base64val (buf[1]);
255 ch = (c1 << 2) | (c2 >> 4);
257 if (cr && ch != '\n')
262 if (istext && ch == '\r')
269 c3 = base64val (buf[2]);
270 ch = ((c2 & 0xf) << 4) | (c3 >> 2);
272 if (cr && ch != '\n')
277 if (istext && ch == '\r')
284 c4 = base64val (buf[3]);
285 ch = ((c3 & 0x3) << 6) | c4;
287 if (cr && ch != '\n')
291 if (istext && ch == '\r')
296 if (l + 8 >= sizeof (bufi))
297 mutt_convert_to_state (cd, bufi, &l, s);
303 mutt_convert_to_state (cd, bufi, &l, s);
304 mutt_convert_to_state (cd, 0, 0, s);
306 state_reset_prefix (s);
309 unsigned char decode_byte (char ch)
316 void mutt_decode_uuencoded (STATE * s, long len, int istext, iconv_t cd)
318 char tmps[SHORT_STRING];
319 char linelen, c, l, out;
321 char bufi[BUFI_SIZE];
325 state_set_prefix (s);
328 if ((fgets (tmps, sizeof (tmps), s->fpin)) == NULL)
330 len -= m_strlen(tmps);
331 if ((!m_strncmp(tmps, "begin", 5)) && ISSPACE (tmps[5]))
335 if ((fgets (tmps, sizeof (tmps), s->fpin)) == NULL)
337 len -= m_strlen(tmps);
338 if (!m_strncmp(tmps, "end", 3))
341 linelen = decode_byte (*pt);
343 for (c = 0; c < linelen;) {
344 for (l = 2; l <= 6; l += 2) {
345 out = decode_byte (*pt) << l;
347 out |= (decode_byte (*pt) >> (6 - l));
353 mutt_convert_to_state (cd, bufi, &k, s);
358 mutt_convert_to_state (cd, bufi, &k, s);
359 mutt_convert_to_state (cd, 0, 0, s);
361 state_reset_prefix (s);
364 /* ----------------------------------------------------------------------------
365 * A (not so) minimal implementation of RFC1563.
368 #define IndentSize (4)
370 enum { RICH_PARAM = 0, RICH_BOLD, RICH_UNDERLINE, RICH_ITALIC, RICH_NOFILL,
371 RICH_INDENT, RICH_INDENT_RIGHT, RICH_EXCERPT, RICH_CENTER, RICH_FLUSHLEFT,
372 RICH_FLUSHRIGHT, RICH_COLOR, RICH_LAST_TAG
376 const char *tag_name;
380 "param", RICH_PARAM}, {
381 "bold", RICH_BOLD}, {
382 "italic", RICH_ITALIC}, {
383 "underline", RICH_UNDERLINE}, {
384 "nofill", RICH_NOFILL}, {
385 "excerpt", RICH_EXCERPT}, {
386 "indent", RICH_INDENT}, {
387 "indentright", RICH_INDENT_RIGHT}, {
388 "center", RICH_CENTER}, {
389 "flushleft", RICH_FLUSHLEFT}, {
390 "flushright", RICH_FLUSHRIGHT}, {
391 "flushboth", RICH_FLUSHLEFT}, {
392 "color", RICH_COLOR}, {
393 "x-color", RICH_COLOR}, {
397 struct enriched_state {
410 int tag_level[RICH_LAST_TAG];
415 static void enriched_wrap (struct enriched_state *stte)
420 if (stte->line_len) {
421 if (stte->tag_level[RICH_CENTER] || stte->tag_level[RICH_FLUSHRIGHT]) {
422 /* Strip trailing white space */
423 size_t y = stte->line_used - 1;
425 while (y && ISSPACE (stte->line[y])) {
426 stte->line[y] = '\0';
431 if (stte->tag_level[RICH_CENTER]) {
432 /* Strip leading whitespace */
435 while (stte->line[y] && ISSPACE (stte->line[y]))
440 for (z = y; z <= stte->line_used; z++) {
441 stte->line[z - y] = stte->line[z];
445 stte->line_used -= y;
450 extra = stte->WrapMargin - stte->line_len - stte->indent_len -
451 (stte->tag_level[RICH_INDENT_RIGHT] * IndentSize);
453 if (stte->tag_level[RICH_CENTER]) {
456 state_putc (' ', stte->s);
460 else if (stte->tag_level[RICH_FLUSHRIGHT]) {
463 state_putc (' ', stte->s);
468 state_puts (stte->line, stte->s);
471 state_putc ('\n', stte->s);
472 stte->line[0] = '\0';
475 stte->indent_len = 0;
476 if (stte->s->prefix) {
477 state_puts (stte->s->prefix, stte->s);
478 stte->indent_len += m_strlen(stte->s->prefix);
481 if (stte->tag_level[RICH_EXCERPT]) {
482 x = stte->tag_level[RICH_EXCERPT];
484 if (stte->s->prefix) {
485 state_puts (stte->s->prefix, stte->s);
486 stte->indent_len += m_strlen(stte->s->prefix);
489 state_puts ("> ", stte->s);
490 stte->indent_len += m_strlen("> ");
496 stte->indent_len = 0;
497 if (stte->tag_level[RICH_INDENT]) {
498 x = stte->tag_level[RICH_INDENT] * IndentSize;
499 stte->indent_len += x;
501 state_putc (' ', stte->s);
507 static void enriched_flush (struct enriched_state *stte, int wrap)
509 if (!stte->tag_level[RICH_NOFILL] && (stte->line_len + stte->word_len >
511 (stte->tag_level[RICH_INDENT_RIGHT] *
512 IndentSize) - stte->indent_len)))
513 enriched_wrap (stte);
515 if (stte->buff_used) {
516 stte->buffer[stte->buff_used] = '\0';
517 stte->line_used += stte->buff_used;
518 if (stte->line_used > stte->line_max) {
519 stte->line_max = stte->line_used;
520 p_realloc(&stte->line, stte->line_max + 1);
522 strcat (stte->line, stte->buffer); /* __STRCAT_CHECKED__ */
523 stte->line_len += stte->word_len;
528 enriched_wrap (stte);
532 static void enriched_putc (int c, struct enriched_state *stte)
534 if (stte->tag_level[RICH_PARAM]) {
535 if (stte->tag_level[RICH_COLOR]) {
536 if (stte->param_used + 1 >= stte->param_len)
537 p_realloc(&stte->param, (stte->param_len += STRING));
539 stte->param[stte->param_used++] = c;
541 return; /* nothing to do */
544 /* see if more space is needed (plus extra for possible rich characters) */
545 if (stte->buff_len < stte->buff_used + 3) {
546 stte->buff_len += LONG_STRING;
547 p_realloc(&stte->buffer, stte->buff_len + 1);
550 if ((!stte->tag_level[RICH_NOFILL] && ISSPACE (c)) || c == '\0') {
552 stte->word_len += 8 - (stte->line_len + stte->word_len) % 8;
556 stte->buffer[stte->buff_used++] = c;
557 enriched_flush (stte, 0);
560 if (stte->s->flags & M_DISPLAY) {
561 if (stte->tag_level[RICH_BOLD]) {
562 stte->buffer[stte->buff_used++] = c;
563 stte->buffer[stte->buff_used++] = '\010';
564 stte->buffer[stte->buff_used++] = c;
566 else if (stte->tag_level[RICH_UNDERLINE]) {
568 stte->buffer[stte->buff_used++] = '_';
569 stte->buffer[stte->buff_used++] = '\010';
570 stte->buffer[stte->buff_used++] = c;
572 else if (stte->tag_level[RICH_ITALIC]) {
573 stte->buffer[stte->buff_used++] = c;
574 stte->buffer[stte->buff_used++] = '\010';
575 stte->buffer[stte->buff_used++] = '_';
578 stte->buffer[stte->buff_used++] = c;
582 stte->buffer[stte->buff_used++] = c;
588 static void enriched_puts (const char *s, struct enriched_state *stte)
592 if (stte->buff_len < stte->buff_used + m_strlen(s)) {
593 stte->buff_len += LONG_STRING;
594 p_realloc(&stte->buffer, stte->buff_len + 1);
599 stte->buffer[stte->buff_used++] = *p++;
603 static void enriched_set_flags (const char *tag, struct enriched_state *stte)
605 const char *tagptr = tag;
611 for (i = 0, j = -1; EnrichedTags[i].tag_name; i++)
612 if (ascii_strcasecmp (EnrichedTags[i].tag_name, tagptr) == 0) {
613 j = EnrichedTags[i].index;
618 if (j == RICH_CENTER || j == RICH_FLUSHLEFT || j == RICH_FLUSHRIGHT)
619 enriched_flush (stte, 1);
622 if (stte->tag_level[j]) /* make sure not to go negative */
623 stte->tag_level[j]--;
624 if ((stte->s->flags & M_DISPLAY) && j == RICH_PARAM
625 && stte->tag_level[RICH_COLOR]) {
626 stte->param[stte->param_used] = '\0';
627 if (!ascii_strcasecmp (stte->param, "black")) {
628 enriched_puts ("\033[30m", stte);
630 else if (!ascii_strcasecmp (stte->param, "red")) {
631 enriched_puts ("\033[31m", stte);
633 else if (!ascii_strcasecmp (stte->param, "green")) {
634 enriched_puts ("\033[32m", stte);
636 else if (!ascii_strcasecmp (stte->param, "yellow")) {
637 enriched_puts ("\033[33m", stte);
639 else if (!ascii_strcasecmp (stte->param, "blue")) {
640 enriched_puts ("\033[34m", stte);
642 else if (!ascii_strcasecmp (stte->param, "magenta")) {
643 enriched_puts ("\033[35m", stte);
645 else if (!ascii_strcasecmp (stte->param, "cyan")) {
646 enriched_puts ("\033[36m", stte);
648 else if (!ascii_strcasecmp (stte->param, "white")) {
649 enriched_puts ("\033[37m", stte);
652 if ((stte->s->flags & M_DISPLAY) && j == RICH_COLOR) {
653 enriched_puts ("\033[0m", stte);
656 /* flush parameter buffer when closing the tag */
657 if (j == RICH_PARAM) {
658 stte->param_used = 0;
659 stte->param[0] = '\0';
663 stte->tag_level[j]++;
665 if (j == RICH_EXCERPT)
666 enriched_flush (stte, 1);
670 int text_enriched_handler (BODY * a, STATE * s)
673 TEXT, LANGLE, TAG, BOGUS_TAG, NEWLINE, ST_EOF, DONE
676 long bytes = a->length;
677 struct enriched_state stte;
680 char tag[LONG_STRING + 1];
685 ((s->flags & M_DISPLAY) ? (COLS - 4) : ((COLS - 4) <
686 72) ? (COLS - 4) : 72);
687 stte.line_max = stte.WrapMargin * 4;
688 stte.line = p_new(char, stte.line_max + 1);
689 stte.param = p_new(char, STRING);
691 stte.param_len = STRING;
695 state_puts (s->prefix, s);
696 stte.indent_len += m_strlen(s->prefix);
699 while (state != DONE) {
700 if (state != ST_EOF) {
701 if (!bytes || (c = fgetc (s->fpin)) == EOF)
715 if (stte.tag_level[RICH_NOFILL]) {
716 enriched_flush (&stte, 1);
719 enriched_putc (' ', &stte);
725 enriched_putc (c, &stte);
731 enriched_putc (c, &stte);
739 /* Yes, fall through (it wasn't a <<, so this char is first in TAG) */
743 enriched_set_flags (tag, &stte);
746 else if (tag_len < LONG_STRING) /* ignore overly long tags */
759 enriched_flush (&stte, 1);
768 enriched_putc ('\0', &stte);
769 enriched_flush (&stte, 1);
773 case DONE: /* not reached, but gcc complains if this is absent */
778 state_putc ('\n', s); /* add a final newline */
780 p_delete(&(stte.buffer));
781 p_delete(&(stte.line));
782 p_delete(&(stte.param));
789 #define TXTENRICHED 3
791 static int alternative_handler (BODY * a, STATE * s)
801 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
802 a->encoding == ENCUUENCODED) {
806 fstat (fileno (s->fpin), &st);
807 b = mutt_new_body ();
808 b->length = (long) st.st_size;
809 b->parts = mutt_parse_multipart (s->fpin,
810 mutt_get_parameter ("boundary",
813 ascii_strcasecmp ("digest",
821 /* First, search list of prefered types */
822 t = AlternativeOrderList;
823 while (t && !choice) {
825 int btlen; /* length of basetype */
826 int wild; /* do we have a wildcard to match all subtypes? */
828 c = strchr (t->data, '/');
830 wild = (c[1] == '*' && c[2] == 0);
835 btlen = m_strlen(t->data);
843 const char *bt = TYPE (b);
845 if (!ascii_strncasecmp (bt, t->data, btlen) && bt[btlen] == 0) {
846 /* the basetype matches */
847 if (wild || !ascii_strcasecmp (t->data + btlen + 1, b->subtype)) {
856 /* Next, look for an autoviewable type */
863 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
864 if (mutt_is_autoview (b, buf)) {
865 rfc1524_entry *entry = rfc1524_new_entry ();
867 if (rfc1524_mailcap_lookup (b, buf, entry, M_AUTOVIEW)) {
870 rfc1524_free_entry (&entry);
876 /* Then, look for a text entry */
883 if (b->type == TYPETEXT) {
884 if (!ascii_strcasecmp ("plain", b->subtype) && type <= TXTPLAIN) {
888 else if (!ascii_strcasecmp ("enriched", b->subtype)
889 && type <= TXTENRICHED) {
893 else if (!ascii_strcasecmp ("html", b->subtype) && type <= TXTHTML) {
902 /* Finally, look for other possibilities */
909 if (mutt_can_decode (b))
916 if (s->flags & M_DISPLAY && !option (OPTWEED)) {
917 fseeko (s->fpin, choice->hdr_offset, 0);
918 mutt_copy_bytes (s->fpin, s->fpout,
919 choice->offset - choice->hdr_offset);
921 mutt_body_handler (choice, s);
923 else if (s->flags & M_DISPLAY) {
924 /* didn't find anything that we could display! */
925 state_mark_attach (s);
926 state_puts (_("[-- Error: Could not display any parts of Multipart/Alternative! --]\n"), s);
936 /* handles message/rfc822 body parts */
937 static int message_handler (BODY * a, STATE * s)
944 off_start = ftello (s->fpin);
945 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
946 a->encoding == ENCUUENCODED) {
947 fstat (fileno (s->fpin), &st);
948 b = mutt_new_body ();
949 b->length = (long) st.st_size;
950 b->parts = mutt_parse_messageRFC822 (s->fpin, b);
956 mutt_copy_hdr (s->fpin, s->fpout, off_start, b->parts->offset,
957 (((s->flags & M_WEED)
958 || ((s->flags & (M_DISPLAY | M_PRINTING))
959 && option (OPTWEED))) ? (CH_WEED | CH_REORDER) : 0) |
960 (s->prefix ? CH_PREFIX : 0) | CH_DECODE | CH_FROM,
964 state_puts (s->prefix, s);
965 state_putc ('\n', s);
967 rc = mutt_body_handler (b->parts, s);
970 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
971 a->encoding == ENCUUENCODED)
977 /* returns 1 if decoding the attachment will produce output */
978 int mutt_can_decode (BODY * a)
982 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
983 if (mutt_is_autoview (a, type))
984 return (rfc1524_mailcap_lookup (a, type, NULL, M_AUTOVIEW));
985 else if (a->type == TYPETEXT)
987 else if (a->type == TYPEMESSAGE)
989 else if (a->type == TYPEMULTIPART) {
993 if (ascii_strcasecmp (a->subtype, "signed") == 0 ||
994 ascii_strcasecmp (a->subtype, "encrypted") == 0)
998 for (p = a->parts; p; p = p->next) {
999 if (mutt_can_decode (p))
1004 else if (WithCrypto && a->type == TYPEAPPLICATION) {
1005 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (a))
1007 if ((WithCrypto & APPLICATION_SMIME) && mutt_is_application_smime (a))
1014 static int multipart_handler (BODY * a, STATE * s)
1022 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1023 a->encoding == ENCUUENCODED) {
1024 fstat (fileno (s->fpin), &st);
1025 b = mutt_new_body ();
1026 b->length = (long) st.st_size;
1027 b->parts = mutt_parse_multipart (s->fpin,
1028 mutt_get_parameter ("boundary",
1031 ascii_strcasecmp ("digest",
1037 for (p = b->parts, count = 1; p; p = p->next, count++) {
1038 if (s->flags & M_DISPLAY) {
1039 state_mark_attach (s);
1040 state_printf (s, _("[-- Attachment #%d"), count);
1041 if (p->description || p->filename || p->form_name) {
1042 state_puts (": ", s);
1043 state_puts (p->description ? p->description :
1044 p->filename ? p->filename : p->form_name, s);
1046 state_puts (" --]\n", s);
1048 mutt_pretty_size (length, sizeof (length), p->length);
1050 state_mark_attach (s);
1051 state_printf (s, _("[-- Type: %s/%s, Encoding: %s, Size: %s --]\n"),
1052 TYPE (p), p->subtype, ENCODING (p->encoding), length);
1053 if (!option (OPTWEED)) {
1054 fseeko (s->fpin, p->hdr_offset, 0);
1055 mutt_copy_bytes (s->fpin, s->fpout, p->offset - p->hdr_offset);
1058 state_putc ('\n', s);
1061 if (p->description && mutt_can_decode (p))
1062 state_printf (s, "Content-Description: %s\n", p->description);
1065 state_printf (s, "%s: \n", p->form_name);
1068 rc = mutt_body_handler (p, s);
1069 state_putc ('\n', s);
1070 if (rc || ((s->flags & M_REPLYING)
1071 && (option (OPTINCLUDEONLYFIRST)) && (s->flags & M_FIRSTDONE)))
1075 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1076 a->encoding == ENCUUENCODED)
1077 mutt_free_body (&b);
1082 static int autoview_handler (BODY * a, STATE * s)
1084 rfc1524_entry *entry = rfc1524_new_entry ();
1085 char buffer[LONG_STRING];
1087 char command[LONG_STRING];
1088 char tempfile[_POSIX_PATH_MAX] = "";
1097 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
1098 rfc1524_mailcap_lookup (a, type, entry, M_AUTOVIEW);
1100 fname = m_strdup(a->filename);
1101 mutt_sanitize_filename (fname, 1);
1102 rfc1524_expand_filename (entry->nametemplate, fname, tempfile,
1106 if (entry->command) {
1107 strfcpy (command, entry->command, sizeof (command));
1109 /* rfc1524_expand_command returns 0 if the file is required */
1111 rfc1524_expand_command (a, tempfile, type, command, sizeof (command));
1113 if (s->flags & M_DISPLAY) {
1114 state_mark_attach (s);
1115 state_printf (s, _("[-- Autoview using %s --]\n"), command);
1116 mutt_message (_("Invoking autoview command: %s"), command);
1119 if ((fpin = safe_fopen (tempfile, "w+")) == NULL) {
1120 mutt_perror ("fopen");
1121 rfc1524_free_entry (&entry);
1125 mutt_copy_bytes (s->fpin, fpin, a->length);
1128 safe_fclose (&fpin);
1129 thepid = mutt_create_filter (command, NULL, &fpout, &fperr);
1135 thepid = mutt_create_filter_fd (command, NULL, &fpout, &fperr,
1136 fileno (fpin), -1, -1);
1140 mutt_perror (_("Can't create filter"));
1142 if (s->flags & M_DISPLAY) {
1143 state_mark_attach (s);
1144 state_printf (s, _("[-- Can't run %s. --]\n"), command);
1151 while (fgets (buffer, sizeof (buffer), fpout) != NULL) {
1152 state_puts (s->prefix, s);
1153 state_puts (buffer, s);
1155 /* check for data on stderr */
1156 if (fgets (buffer, sizeof (buffer), fperr)) {
1157 if (s->flags & M_DISPLAY) {
1158 state_mark_attach (s);
1159 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1162 state_puts (s->prefix, s);
1163 state_puts (buffer, s);
1164 while (fgets (buffer, sizeof (buffer), fperr) != NULL) {
1165 state_puts (s->prefix, s);
1166 state_puts (buffer, s);
1171 mutt_copy_stream (fpout, s->fpout);
1172 /* Check for stderr messages */
1173 if (fgets (buffer, sizeof (buffer), fperr)) {
1174 if (s->flags & M_DISPLAY) {
1175 state_mark_attach (s);
1176 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1179 state_puts (buffer, s);
1180 mutt_copy_stream (fperr, s->fpout);
1185 safe_fclose (&fpout);
1186 safe_fclose (&fperr);
1188 mutt_wait_filter (thepid);
1190 safe_fclose (&fpin);
1192 mutt_unlink (tempfile);
1194 if (s->flags & M_DISPLAY)
1195 mutt_clear_error ();
1197 rfc1524_free_entry (&entry);
1201 static int external_body_handler (BODY * b, STATE * s)
1203 const char *access_type;
1204 const char *expiration;
1207 access_type = mutt_get_parameter ("access-type", b->parameter);
1209 if (s->flags & M_DISPLAY) {
1210 state_mark_attach (s);
1211 state_puts (_("[-- Error: message/external-body has no access-type parameter --]\n"), s);
1216 expiration = mutt_get_parameter ("expiration", b->parameter);
1218 expire = mutt_parse_date (expiration, NULL);
1222 if (!ascii_strcasecmp (access_type, "x-mutt-deleted")) {
1223 if (s->flags & (M_DISPLAY | M_PRINTING)) {
1225 char pretty_size[10];
1227 state_mark_attach (s);
1228 state_printf (s, _("[-- This %s/%s attachment "),
1229 TYPE (b->parts), b->parts->subtype);
1230 length = mutt_get_parameter ("length", b->parameter);
1232 mutt_pretty_size (pretty_size, sizeof (pretty_size),
1233 strtol (length, NULL, 10));
1234 state_printf (s, _("(size %s bytes) "), pretty_size);
1236 state_puts (_("has been deleted --]\n"), s);
1239 state_mark_attach (s);
1240 state_printf (s, _("[-- on %s --]\n"), expiration);
1242 if (b->parts->filename) {
1243 state_mark_attach (s);
1244 state_printf (s, _("[-- name: %s --]\n"), b->parts->filename);
1247 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1248 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1252 else if (expiration && expire < time (NULL)) {
1253 if (s->flags & M_DISPLAY) {
1254 state_mark_attach (s);
1255 state_printf (s, _("[-- This %s/%s attachment is not included, --]\n"),
1256 TYPE (b->parts), b->parts->subtype);
1257 state_attach_puts (_("[-- and the indicated external source has --]\n"
1258 "[-- expired. --]\n"), s);
1260 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1261 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1266 if (s->flags & M_DISPLAY) {
1267 state_mark_attach (s);
1269 _("[-- This %s/%s attachment is not included, --]\n"),
1270 TYPE (b->parts), b->parts->subtype);
1271 state_mark_attach (s);
1274 ("[-- and the indicated access-type %s is unsupported --]\n"),
1276 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1277 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1284 void mutt_decode_attachment (BODY * b, STATE * s)
1286 int istext = mutt_is_text_part (b);
1287 iconv_t cd = (iconv_t) (-1);
1292 if (s->flags & M_CHARCONV) {
1293 const char *charset = mutt_get_parameter ("charset", b->parameter);
1295 if (!option (OPTSTRICTMIME) && !charset)
1296 charset = mutt_get_first_charset (AssumedCharset);
1297 if (charset && Charset)
1298 cd = mutt_iconv_open (Charset, charset, M_ICONV_HOOK_FROM);
1301 if (b->file_charset)
1302 cd = mutt_iconv_open (Charset, b->file_charset, M_ICONV_HOOK_FROM);
1306 fseeko (s->fpin, b->offset, 0);
1307 switch (b->encoding) {
1308 case ENCQUOTEDPRINTABLE:
1309 mutt_decode_quoted (s, b->length, istext ||
1310 ((WithCrypto & APPLICATION_PGP) &&
1311 mutt_is_application_pgp (b)), cd);
1314 mutt_decode_base64 (s, b->length, istext ||
1315 ((WithCrypto & APPLICATION_PGP) &&
1316 mutt_is_application_pgp (b)), cd);
1319 mutt_decode_uuencoded (s, b->length, istext
1320 || ((WithCrypto & APPLICATION_PGP) &&
1321 mutt_is_application_pgp (b)), cd);
1324 mutt_decode_xbit (s, b->length, istext
1325 || ((WithCrypto & APPLICATION_PGP) &&
1326 mutt_is_application_pgp (b)), cd);
1330 if (cd != (iconv_t) (-1))
1334 int mutt_body_handler (BODY * b, STATE * s)
1339 char tempfile[_POSIX_PATH_MAX];
1340 handler_t handler = NULL;
1342 size_t tmplength = 0;
1346 int oflags = s->flags;
1348 /* first determine which handler to use to process this part */
1350 snprintf (type, sizeof (type), "%s/%s", TYPE (b), b->subtype);
1351 if (mutt_is_autoview (b, type)) {
1352 rfc1524_entry *entry = rfc1524_new_entry ();
1354 if (rfc1524_mailcap_lookup (b, type, entry, M_AUTOVIEW)) {
1355 handler = autoview_handler;
1356 s->flags &= ~M_CHARCONV;
1358 rfc1524_free_entry (&entry);
1360 else if (b->type == TYPETEXT) {
1361 if (ascii_strcasecmp ("plain", b->subtype) == 0) {
1362 /* avoid copying this part twice since removing the transfer-encoding is
1363 * the only operation needed.
1365 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b))
1366 handler = crypt_pgp_application_pgp_handler;
1368 if (ascii_strcasecmp
1369 ("flowed", mutt_get_parameter ("format", b->parameter)) == 0)
1370 handler = rfc3676_handler;
1374 else if (ascii_strcasecmp ("enriched", b->subtype) == 0)
1375 handler = text_enriched_handler;
1376 else /* text body type without a handler */
1379 else if (b->type == TYPEMESSAGE) {
1380 if (mutt_is_message_type (b->type, b->subtype))
1381 handler = message_handler;
1382 else if (!ascii_strcasecmp ("delivery-status", b->subtype))
1384 else if (!ascii_strcasecmp ("external-body", b->subtype))
1385 handler = external_body_handler;
1387 else if (b->type == TYPEMULTIPART) {
1390 if (ascii_strcasecmp ("alternative", b->subtype) == 0)
1391 handler = alternative_handler;
1392 else if (WithCrypto && ascii_strcasecmp ("signed", b->subtype) == 0) {
1393 p = mutt_get_parameter ("protocol", b->parameter);
1396 mutt_error (_("Error: multipart/signed has no protocol."));
1398 else if (s->flags & M_VERIFY)
1399 handler = mutt_signed_handler;
1401 else if ((WithCrypto & APPLICATION_PGP)
1402 && m_strcasecmp("encrypted", b->subtype) == 0) {
1403 p = mutt_get_parameter ("protocol", b->parameter);
1407 ("Error: multipart/encrypted has no protocol parameter!"));
1409 else if (ascii_strcasecmp ("application/pgp-encrypted", p) == 0)
1410 handler = crypt_pgp_encrypted_handler;
1414 handler = multipart_handler;
1416 else if (WithCrypto && b->type == TYPEAPPLICATION) {
1417 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b))
1418 handler = crypt_pgp_application_pgp_handler;
1419 if ((WithCrypto & APPLICATION_SMIME) && mutt_is_application_smime (b))
1420 handler = crypt_smime_application_smime_handler;
1424 if (plaintext || handler) {
1425 fseeko (s->fpin, b->offset, 0);
1427 /* see if we need to decode this part before processing it */
1428 if (b->encoding == ENCBASE64 || b->encoding == ENCQUOTEDPRINTABLE || b->encoding == ENCUUENCODED || plaintext || mutt_is_text_part (b)) { /* text subtypes may
1430 * set conversion even
1431 * with 8bit encoding.
1433 int origType = b->type;
1434 char *savePrefix = NULL;
1437 /* decode to a tempfile, saving the original destination */
1439 mutt_mktemp (tempfile);
1440 if ((s->fpout = safe_fopen (tempfile, "w")) == NULL) {
1441 mutt_error _("Unable to open temporary file!");
1445 /* decoding the attachment changes the size and offset, so save a copy
1446 * of the "real" values now, and restore them after processing
1448 tmplength = b->length;
1449 tmpoffset = b->offset;
1451 /* if we are decoding binary bodies, we don't want to prefix each
1452 * line with the prefix or else the data will get corrupted.
1454 savePrefix = s->prefix;
1462 mutt_decode_attachment (b, s);
1465 b->length = ftello (s->fpout);
1469 /* restore final destination and substitute the tempfile for input */
1472 s->fpin = safe_fopen (tempfile, "r");
1475 /* restore the prefix */
1476 s->prefix = savePrefix;
1482 /* process the (decoded) body part */
1484 rc = handler (b, s);
1487 b->length = tmplength;
1488 b->offset = tmpoffset;
1490 /* restore the original source stream */
1495 s->flags |= M_FIRSTDONE;
1497 else if (s->flags & M_DISPLAY) {
1498 state_mark_attach (s);
1499 state_printf (s, _("[-- %s/%s is unsupported "), TYPE (b), b->subtype);
1500 if (!option (OPTVIEWATTACH)) {
1502 (type, sizeof (type),
1503 km_find_func (MENU_PAGER, OP_VIEW_ATTACHMENTS)))
1504 fprintf (s->fpout, _("(use '%s' to view this part)"), type);
1506 fputs (_("(need 'view-attachments' bound to key!)"), s->fpout);
1508 fputs (" --]\n", s->fpout);
1512 s->flags = oflags | (s->flags & M_FIRSTDONE);