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.
23 #include "recvattach.h"
25 #include "mutt_curses.h"
32 #include "mutt_crypt.h"
40 #include "lib/debug.h"
42 typedef int handler_f (BODY *, STATE *);
43 typedef handler_f *handler_t;
45 int Index_hex[128] = {
46 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
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 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
50 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
51 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -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
57 -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, 62, -1, -1, -1, 63,
60 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
61 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
62 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
63 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
64 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1
67 void mutt_decode_xbit (STATE * s, long len, int istext, iconv_t cd)
76 while ((c = fgetc (s->fpin)) != EOF && len--) {
77 if (c == '\r' && len) {
78 if ((ch = fgetc (s->fpin)) == '\n') {
87 if (l == sizeof (bufi))
88 mutt_convert_to_state (cd, bufi, &l, s);
91 mutt_convert_to_state (cd, bufi, &l, s);
92 mutt_convert_to_state (cd, 0, 0, s);
94 state_reset_prefix (s);
97 mutt_copy_bytes (s->fpin, s->fpout, len);
100 static int qp_decode_triple (char *s, char *d)
102 /* soft line break */
103 if (*s == '=' && !(*(s + 1)))
106 /* quoted-printable triple */
108 isxdigit ((unsigned char) *(s + 1)) &&
109 isxdigit ((unsigned char) *(s + 2))) {
110 *d = (hexval (*(s + 1)) << 4) | hexval (*(s + 2));
118 static void qp_decode_line (char *dest, char *src, size_t * l, int last)
126 /* decode the line */
128 for (d = dest, s = src; *s;) {
129 switch ((kind = qp_decode_triple (s, &c))) {
133 break; /* qp triple */
136 break; /* single character */
140 break; /* soft line break */
144 if (!soft && last == '\n')
152 * Decode an attachment encoded with quoted-printable.
154 * Why doesn't this overflow any buffers? First, it's guaranteed
155 * that the length of a line grows when you _en_-code it to
156 * quoted-printable. That means that we always can store the
157 * result in a buffer of at most the _same_ size.
159 * Now, we don't special-case if the line we read with fgets()
160 * isn't terminated. We don't care about this, since STRING > 78,
161 * so corrupted input will just be corrupted a bit more. That
162 * implies that STRING+1 bytes are always sufficient to store the
163 * result of qp_decode_line.
165 * Finally, at soft line breaks, some part of a multibyte character
166 * may have been left over by mutt_convert_to_state(). This shouldn't
167 * be more than 6 characters, so STRING + 7 should be sufficient
168 * memory to store the decoded data.
170 * Just to make sure that I didn't make some off-by-one error
171 * above, we just use STRING*2 for the target buffer's size.
175 void mutt_decode_quoted (STATE * s, long len, int istext, iconv_t cd)
178 char decline[2 * STRING];
180 size_t linelen; /* number of input bytes in `line' */
183 int last; /* store the last character in the input line */
186 state_set_prefix (s);
192 * It's ok to use a fixed size buffer for input, even if the line turns
193 * out to be longer than this. Just process the line in chunks. This
194 * really shouldn't happen according the MIME spec, since Q-P encoded
195 * lines are at most 76 characters, but we should be liberal about what
198 if (fgets (line, MIN ((ssize_t) sizeof (line), len + 1), s->fpin) == NULL)
201 linelen = str_len (line);
205 * inspect the last character we read so we can tell if we got the
208 last = linelen ? line[linelen - 1] : 0;
210 /* chop trailing whitespace if we got the full line */
212 while (linelen > 0 && ISSPACE (line[linelen - 1]))
217 /* decode and do character set conversion */
218 qp_decode_line (decline + l, line, &l3, last);
220 mutt_convert_to_state (cd, decline, &l, s);
223 mutt_convert_to_state (cd, 0, 0, s);
224 state_reset_prefix (s);
227 void mutt_decode_base64 (STATE * s, long len, int istext, iconv_t cd)
230 int c1, c2, c3, c4, ch, cr = 0, i;
231 char bufi[BUFI_SIZE];
237 state_set_prefix (s);
240 for (i = 0; i < 4 && len > 0; len--) {
241 if ((ch = fgetc (s->fpin)) == EOF)
243 if (ch >= 0 && ch < 128 && (base64val (ch) != -1 || ch == '='))
247 debug_print (2, ("didn't get a multiple of 4 chars.\n"));
251 c1 = base64val (buf[0]);
252 c2 = base64val (buf[1]);
253 ch = (c1 << 2) | (c2 >> 4);
255 if (cr && ch != '\n')
260 if (istext && ch == '\r')
267 c3 = base64val (buf[2]);
268 ch = ((c2 & 0xf) << 4) | (c3 >> 2);
270 if (cr && ch != '\n')
275 if (istext && ch == '\r')
282 c4 = base64val (buf[3]);
283 ch = ((c3 & 0x3) << 6) | c4;
285 if (cr && ch != '\n')
289 if (istext && ch == '\r')
294 if (l + 8 >= sizeof (bufi))
295 mutt_convert_to_state (cd, bufi, &l, s);
301 mutt_convert_to_state (cd, bufi, &l, s);
302 mutt_convert_to_state (cd, 0, 0, s);
304 state_reset_prefix (s);
307 unsigned char decode_byte (char ch)
314 void mutt_decode_uuencoded (STATE * s, long len, int istext, iconv_t cd)
316 char tmps[SHORT_STRING];
317 char linelen, c, l, out;
319 char bufi[BUFI_SIZE];
323 state_set_prefix (s);
326 if ((fgets (tmps, sizeof (tmps), s->fpin)) == NULL)
328 len -= str_len (tmps);
329 if ((!str_ncmp (tmps, "begin", 5)) && ISSPACE (tmps[5]))
333 if ((fgets (tmps, sizeof (tmps), s->fpin)) == NULL)
335 len -= str_len (tmps);
336 if (!str_ncmp (tmps, "end", 3))
339 linelen = decode_byte (*pt);
341 for (c = 0; c < linelen;) {
342 for (l = 2; l <= 6; l += 2) {
343 out = decode_byte (*pt) << l;
345 out |= (decode_byte (*pt) >> (6 - l));
351 mutt_convert_to_state (cd, bufi, &k, s);
356 mutt_convert_to_state (cd, bufi, &k, s);
357 mutt_convert_to_state (cd, 0, 0, s);
359 state_reset_prefix (s);
362 /* ----------------------------------------------------------------------------
363 * A (not so) minimal implementation of RFC1563.
366 #define IndentSize (4)
368 enum { RICH_PARAM = 0, RICH_BOLD, RICH_UNDERLINE, RICH_ITALIC, RICH_NOFILL,
369 RICH_INDENT, RICH_INDENT_RIGHT, RICH_EXCERPT, RICH_CENTER, RICH_FLUSHLEFT,
370 RICH_FLUSHRIGHT, RICH_COLOR, RICH_LAST_TAG
374 const char *tag_name;
378 "param", RICH_PARAM}, {
379 "bold", RICH_BOLD}, {
380 "italic", RICH_ITALIC}, {
381 "underline", RICH_UNDERLINE}, {
382 "nofill", RICH_NOFILL}, {
383 "excerpt", RICH_EXCERPT}, {
384 "indent", RICH_INDENT}, {
385 "indentright", RICH_INDENT_RIGHT}, {
386 "center", RICH_CENTER}, {
387 "flushleft", RICH_FLUSHLEFT}, {
388 "flushright", RICH_FLUSHRIGHT}, {
389 "flushboth", RICH_FLUSHLEFT}, {
390 "color", RICH_COLOR}, {
391 "x-color", RICH_COLOR}, {
395 struct enriched_state {
408 int tag_level[RICH_LAST_TAG];
413 static void enriched_wrap (struct enriched_state *stte)
418 if (stte->line_len) {
419 if (stte->tag_level[RICH_CENTER] || stte->tag_level[RICH_FLUSHRIGHT]) {
420 /* Strip trailing white space */
421 size_t y = stte->line_used - 1;
423 while (y && ISSPACE (stte->line[y])) {
424 stte->line[y] = '\0';
429 if (stte->tag_level[RICH_CENTER]) {
430 /* Strip leading whitespace */
433 while (stte->line[y] && ISSPACE (stte->line[y]))
438 for (z = y; z <= stte->line_used; z++) {
439 stte->line[z - y] = stte->line[z];
443 stte->line_used -= y;
448 extra = stte->WrapMargin - stte->line_len - stte->indent_len -
449 (stte->tag_level[RICH_INDENT_RIGHT] * IndentSize);
451 if (stte->tag_level[RICH_CENTER]) {
454 state_putc (' ', stte->s);
458 else if (stte->tag_level[RICH_FLUSHRIGHT]) {
461 state_putc (' ', stte->s);
466 state_puts (stte->line, stte->s);
469 state_putc ('\n', stte->s);
470 stte->line[0] = '\0';
473 stte->indent_len = 0;
474 if (stte->s->prefix) {
475 state_puts (stte->s->prefix, stte->s);
476 stte->indent_len += str_len (stte->s->prefix);
479 if (stte->tag_level[RICH_EXCERPT]) {
480 x = stte->tag_level[RICH_EXCERPT];
482 if (stte->s->prefix) {
483 state_puts (stte->s->prefix, stte->s);
484 stte->indent_len += str_len (stte->s->prefix);
487 state_puts ("> ", stte->s);
488 stte->indent_len += str_len ("> ");
494 stte->indent_len = 0;
495 if (stte->tag_level[RICH_INDENT]) {
496 x = stte->tag_level[RICH_INDENT] * IndentSize;
497 stte->indent_len += x;
499 state_putc (' ', stte->s);
505 static void enriched_flush (struct enriched_state *stte, int wrap)
507 if (!stte->tag_level[RICH_NOFILL] && (stte->line_len + stte->word_len >
509 (stte->tag_level[RICH_INDENT_RIGHT] *
510 IndentSize) - stte->indent_len)))
511 enriched_wrap (stte);
513 if (stte->buff_used) {
514 stte->buffer[stte->buff_used] = '\0';
515 stte->line_used += stte->buff_used;
516 if (stte->line_used > stte->line_max) {
517 stte->line_max = stte->line_used;
518 mem_realloc (&stte->line, stte->line_max + 1);
520 strcat (stte->line, stte->buffer); /* __STRCAT_CHECKED__ */
521 stte->line_len += stte->word_len;
526 enriched_wrap (stte);
530 static void enriched_putc (int c, struct enriched_state *stte)
532 if (stte->tag_level[RICH_PARAM]) {
533 if (stte->tag_level[RICH_COLOR]) {
534 if (stte->param_used + 1 >= stte->param_len)
535 mem_realloc (&stte->param, (stte->param_len += STRING));
537 stte->param[stte->param_used++] = c;
539 return; /* nothing to do */
542 /* see if more space is needed (plus extra for possible rich characters) */
543 if (stte->buff_len < stte->buff_used + 3) {
544 stte->buff_len += LONG_STRING;
545 mem_realloc (&stte->buffer, stte->buff_len + 1);
548 if ((!stte->tag_level[RICH_NOFILL] && ISSPACE (c)) || c == '\0') {
550 stte->word_len += 8 - (stte->line_len + stte->word_len) % 8;
554 stte->buffer[stte->buff_used++] = c;
555 enriched_flush (stte, 0);
558 if (stte->s->flags & M_DISPLAY) {
559 if (stte->tag_level[RICH_BOLD]) {
560 stte->buffer[stte->buff_used++] = c;
561 stte->buffer[stte->buff_used++] = '\010';
562 stte->buffer[stte->buff_used++] = c;
564 else if (stte->tag_level[RICH_UNDERLINE]) {
566 stte->buffer[stte->buff_used++] = '_';
567 stte->buffer[stte->buff_used++] = '\010';
568 stte->buffer[stte->buff_used++] = c;
570 else if (stte->tag_level[RICH_ITALIC]) {
571 stte->buffer[stte->buff_used++] = c;
572 stte->buffer[stte->buff_used++] = '\010';
573 stte->buffer[stte->buff_used++] = '_';
576 stte->buffer[stte->buff_used++] = c;
580 stte->buffer[stte->buff_used++] = c;
586 static void enriched_puts (char *s, struct enriched_state *stte)
590 if (stte->buff_len < stte->buff_used + str_len (s)) {
591 stte->buff_len += LONG_STRING;
592 mem_realloc (&stte->buffer, stte->buff_len + 1);
596 stte->buffer[stte->buff_used++] = *c;
601 static void enriched_set_flags (const char *tag, struct enriched_state *stte)
603 const char *tagptr = tag;
609 for (i = 0, j = -1; EnrichedTags[i].tag_name; i++)
610 if (ascii_strcasecmp (EnrichedTags[i].tag_name, tagptr) == 0) {
611 j = EnrichedTags[i].index;
616 if (j == RICH_CENTER || j == RICH_FLUSHLEFT || j == RICH_FLUSHRIGHT)
617 enriched_flush (stte, 1);
620 if (stte->tag_level[j]) /* make sure not to go negative */
621 stte->tag_level[j]--;
622 if ((stte->s->flags & M_DISPLAY) && j == RICH_PARAM
623 && stte->tag_level[RICH_COLOR]) {
624 stte->param[stte->param_used] = '\0';
625 if (!ascii_strcasecmp (stte->param, "black")) {
626 enriched_puts ("\033[30m", stte);
628 else if (!ascii_strcasecmp (stte->param, "red")) {
629 enriched_puts ("\033[31m", stte);
631 else if (!ascii_strcasecmp (stte->param, "green")) {
632 enriched_puts ("\033[32m", stte);
634 else if (!ascii_strcasecmp (stte->param, "yellow")) {
635 enriched_puts ("\033[33m", stte);
637 else if (!ascii_strcasecmp (stte->param, "blue")) {
638 enriched_puts ("\033[34m", stte);
640 else if (!ascii_strcasecmp (stte->param, "magenta")) {
641 enriched_puts ("\033[35m", stte);
643 else if (!ascii_strcasecmp (stte->param, "cyan")) {
644 enriched_puts ("\033[36m", stte);
646 else if (!ascii_strcasecmp (stte->param, "white")) {
647 enriched_puts ("\033[37m", stte);
650 if ((stte->s->flags & M_DISPLAY) && j == RICH_COLOR) {
651 enriched_puts ("\033[0m", stte);
654 /* flush parameter buffer when closing the tag */
655 if (j == RICH_PARAM) {
656 stte->param_used = 0;
657 stte->param[0] = '\0';
661 stte->tag_level[j]++;
663 if (j == RICH_EXCERPT)
664 enriched_flush (stte, 1);
668 int text_enriched_handler (BODY * a, STATE * s)
671 TEXT, LANGLE, TAG, BOGUS_TAG, NEWLINE, ST_EOF, DONE
674 long bytes = a->length;
675 struct enriched_state stte;
678 char tag[LONG_STRING + 1];
680 memset (&stte, 0, sizeof (stte));
683 ((s->flags & M_DISPLAY) ? (COLS - 4) : ((COLS - 4) <
684 72) ? (COLS - 4) : 72);
685 stte.line_max = stte.WrapMargin * 4;
686 stte.line = (char *) mem_calloc (1, stte.line_max + 1);
687 stte.param = (char *) mem_calloc (1, STRING);
689 stte.param_len = STRING;
693 state_puts (s->prefix, s);
694 stte.indent_len += str_len (s->prefix);
697 while (state != DONE) {
698 if (state != ST_EOF) {
699 if (!bytes || (c = fgetc (s->fpin)) == EOF)
713 if (stte.tag_level[RICH_NOFILL]) {
714 enriched_flush (&stte, 1);
717 enriched_putc (' ', &stte);
723 enriched_putc (c, &stte);
729 enriched_putc (c, &stte);
737 /* Yes, fall through (it wasn't a <<, so this char is first in TAG) */
741 enriched_set_flags (tag, &stte);
744 else if (tag_len < LONG_STRING) /* ignore overly long tags */
757 enriched_flush (&stte, 1);
766 enriched_putc ('\0', &stte);
767 enriched_flush (&stte, 1);
771 case DONE: /* not reached, but gcc complains if this is absent */
776 state_putc ('\n', s); /* add a final newline */
778 mem_free (&(stte.buffer));
779 mem_free (&(stte.line));
780 mem_free (&(stte.param));
787 #define TXTENRICHED 3
789 static int alternative_handler (BODY * a, STATE * s)
799 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
800 a->encoding == ENCUUENCODED) {
804 fstat (fileno (s->fpin), &st);
805 b = mutt_new_body ();
806 b->length = (long) st.st_size;
807 b->parts = mutt_parse_multipart (s->fpin,
808 mutt_get_parameter ("boundary",
811 ascii_strcasecmp ("digest",
819 /* First, search list of prefered types */
820 t = AlternativeOrderList;
821 while (t && !choice) {
823 int btlen; /* length of basetype */
824 int wild; /* do we have a wildcard to match all subtypes? */
826 c = strchr (t->data, '/');
828 wild = (c[1] == '*' && c[2] == 0);
833 btlen = str_len (t->data);
841 const char *bt = TYPE (b);
843 if (!ascii_strncasecmp (bt, t->data, btlen) && bt[btlen] == 0) {
844 /* the basetype matches */
845 if (wild || !ascii_strcasecmp (t->data + btlen + 1, b->subtype)) {
854 /* Next, look for an autoviewable type */
861 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
862 if (mutt_is_autoview (b, buf)) {
863 rfc1524_entry *entry = rfc1524_new_entry ();
865 if (rfc1524_mailcap_lookup (b, buf, entry, M_AUTOVIEW)) {
868 rfc1524_free_entry (&entry);
874 /* Then, look for a text entry */
881 if (b->type == TYPETEXT) {
882 if (!ascii_strcasecmp ("plain", b->subtype) && type <= TXTPLAIN) {
886 else if (!ascii_strcasecmp ("enriched", b->subtype)
887 && type <= TXTENRICHED) {
891 else if (!ascii_strcasecmp ("html", b->subtype) && type <= TXTHTML) {
900 /* Finally, look for other possibilities */
907 if (mutt_can_decode (b))
914 if (s->flags & M_DISPLAY && !option (OPTWEED)) {
915 fseeko (s->fpin, choice->hdr_offset, 0);
916 mutt_copy_bytes (s->fpin, s->fpout,
917 choice->offset - choice->hdr_offset);
919 mutt_body_handler (choice, s);
921 else if (s->flags & M_DISPLAY) {
922 /* didn't find anything that we could display! */
923 state_mark_attach (s);
924 state_puts (_("[-- Error: Could not display any parts of Multipart/Alternative! --]\n"), s);
934 /* handles message/rfc822 body parts */
935 static int message_handler (BODY * a, STATE * s)
942 off_start = ftello (s->fpin);
943 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
944 a->encoding == ENCUUENCODED) {
945 fstat (fileno (s->fpin), &st);
946 b = mutt_new_body ();
947 b->length = (long) st.st_size;
948 b->parts = mutt_parse_messageRFC822 (s->fpin, b);
954 mutt_copy_hdr (s->fpin, s->fpout, off_start, b->parts->offset,
955 (((s->flags & M_WEED)
956 || ((s->flags & (M_DISPLAY | M_PRINTING))
957 && option (OPTWEED))) ? (CH_WEED | CH_REORDER) : 0) |
958 (s->prefix ? CH_PREFIX : 0) | CH_DECODE | CH_FROM,
962 state_puts (s->prefix, s);
963 state_putc ('\n', s);
965 rc = mutt_body_handler (b->parts, s);
968 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
969 a->encoding == ENCUUENCODED)
975 /* returns 1 if decoding the attachment will produce output */
976 int mutt_can_decode (BODY * a)
980 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
981 if (mutt_is_autoview (a, type))
982 return (rfc1524_mailcap_lookup (a, type, NULL, M_AUTOVIEW));
983 else if (a->type == TYPETEXT)
985 else if (a->type == TYPEMESSAGE)
987 else if (a->type == TYPEMULTIPART) {
991 if (ascii_strcasecmp (a->subtype, "signed") == 0 ||
992 ascii_strcasecmp (a->subtype, "encrypted") == 0)
996 for (p = a->parts; p; p = p->next) {
997 if (mutt_can_decode (p))
1002 else if (WithCrypto && a->type == TYPEAPPLICATION) {
1003 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (a))
1005 if ((WithCrypto & APPLICATION_SMIME) && mutt_is_application_smime (a))
1012 static int multipart_handler (BODY * a, STATE * s)
1020 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1021 a->encoding == ENCUUENCODED) {
1022 fstat (fileno (s->fpin), &st);
1023 b = mutt_new_body ();
1024 b->length = (long) st.st_size;
1025 b->parts = mutt_parse_multipart (s->fpin,
1026 mutt_get_parameter ("boundary",
1029 ascii_strcasecmp ("digest",
1035 for (p = b->parts, count = 1; p; p = p->next, count++) {
1036 if (s->flags & M_DISPLAY) {
1037 state_mark_attach (s);
1038 state_printf (s, _("[-- Attachment #%d"), count);
1039 if (p->description || p->filename || p->form_name) {
1040 state_puts (": ", s);
1041 state_puts (p->description ? p->description :
1042 p->filename ? p->filename : p->form_name, s);
1044 state_puts (" --]\n", s);
1046 mutt_pretty_size (length, sizeof (length), p->length);
1048 state_mark_attach (s);
1049 state_printf (s, _("[-- Type: %s/%s, Encoding: %s, Size: %s --]\n"),
1050 TYPE (p), p->subtype, ENCODING (p->encoding), length);
1051 if (!option (OPTWEED)) {
1052 fseeko (s->fpin, p->hdr_offset, 0);
1053 mutt_copy_bytes (s->fpin, s->fpout, p->offset - p->hdr_offset);
1056 state_putc ('\n', s);
1059 if (p->description && mutt_can_decode (p))
1060 state_printf (s, "Content-Description: %s\n", p->description);
1063 state_printf (s, "%s: \n", p->form_name);
1066 rc = mutt_body_handler (p, s);
1067 state_putc ('\n', s);
1068 if (rc || ((s->flags & M_REPLYING)
1069 && (option (OPTINCLUDEONLYFIRST)) && (s->flags & M_FIRSTDONE)))
1073 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1074 a->encoding == ENCUUENCODED)
1075 mutt_free_body (&b);
1080 static int autoview_handler (BODY * a, STATE * s)
1082 rfc1524_entry *entry = rfc1524_new_entry ();
1083 char buffer[LONG_STRING];
1085 char command[LONG_STRING];
1086 char tempfile[_POSIX_PATH_MAX] = "";
1095 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
1096 rfc1524_mailcap_lookup (a, type, entry, M_AUTOVIEW);
1098 fname = str_dup (a->filename);
1099 mutt_sanitize_filename (fname, 1);
1100 rfc1524_expand_filename (entry->nametemplate, fname, tempfile,
1104 if (entry->command) {
1105 strfcpy (command, entry->command, sizeof (command));
1107 /* rfc1524_expand_command returns 0 if the file is required */
1109 rfc1524_expand_command (a, tempfile, type, command, sizeof (command));
1111 if (s->flags & M_DISPLAY) {
1112 state_mark_attach (s);
1113 state_printf (s, _("[-- Autoview using %s --]\n"), command);
1114 mutt_message (_("Invoking autoview command: %s"), command);
1117 if ((fpin = safe_fopen (tempfile, "w+")) == NULL) {
1118 mutt_perror ("fopen");
1119 rfc1524_free_entry (&entry);
1123 mutt_copy_bytes (s->fpin, fpin, a->length);
1126 safe_fclose (&fpin);
1127 thepid = mutt_create_filter (command, NULL, &fpout, &fperr);
1133 thepid = mutt_create_filter_fd (command, NULL, &fpout, &fperr,
1134 fileno (fpin), -1, -1);
1138 mutt_perror (_("Can't create filter"));
1140 if (s->flags & M_DISPLAY) {
1141 state_mark_attach (s);
1142 state_printf (s, _("[-- Can't run %s. --]\n"), command);
1149 while (fgets (buffer, sizeof (buffer), fpout) != NULL) {
1150 state_puts (s->prefix, s);
1151 state_puts (buffer, s);
1153 /* check for data on stderr */
1154 if (fgets (buffer, sizeof (buffer), fperr)) {
1155 if (s->flags & M_DISPLAY) {
1156 state_mark_attach (s);
1157 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1160 state_puts (s->prefix, s);
1161 state_puts (buffer, s);
1162 while (fgets (buffer, sizeof (buffer), fperr) != NULL) {
1163 state_puts (s->prefix, s);
1164 state_puts (buffer, s);
1169 mutt_copy_stream (fpout, s->fpout);
1170 /* Check for stderr messages */
1171 if (fgets (buffer, sizeof (buffer), fperr)) {
1172 if (s->flags & M_DISPLAY) {
1173 state_mark_attach (s);
1174 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1177 state_puts (buffer, s);
1178 mutt_copy_stream (fperr, s->fpout);
1183 safe_fclose (&fpout);
1184 safe_fclose (&fperr);
1186 mutt_wait_filter (thepid);
1188 safe_fclose (&fpin);
1190 mutt_unlink (tempfile);
1192 if (s->flags & M_DISPLAY)
1193 mutt_clear_error ();
1195 rfc1524_free_entry (&entry);
1199 static int external_body_handler (BODY * b, STATE * s)
1201 const char *access_type;
1202 const char *expiration;
1205 access_type = mutt_get_parameter ("access-type", b->parameter);
1207 if (s->flags & M_DISPLAY) {
1208 state_mark_attach (s);
1209 state_puts (_("[-- Error: message/external-body has no access-type parameter --]\n"), s);
1214 expiration = mutt_get_parameter ("expiration", b->parameter);
1216 expire = mutt_parse_date (expiration, NULL);
1220 if (!ascii_strcasecmp (access_type, "x-mutt-deleted")) {
1221 if (s->flags & (M_DISPLAY | M_PRINTING)) {
1223 char pretty_size[10];
1225 state_mark_attach (s);
1226 state_printf (s, _("[-- This %s/%s attachment "),
1227 TYPE (b->parts), b->parts->subtype);
1228 length = mutt_get_parameter ("length", b->parameter);
1230 mutt_pretty_size (pretty_size, sizeof (pretty_size),
1231 strtol (length, NULL, 10));
1232 state_printf (s, _("(size %s bytes) "), pretty_size);
1234 state_puts (_("has been deleted --]\n"), s);
1237 state_mark_attach (s);
1238 state_printf (s, _("[-- on %s --]\n"), expiration);
1240 if (b->parts->filename) {
1241 state_mark_attach (s);
1242 state_printf (s, _("[-- name: %s --]\n"), b->parts->filename);
1245 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1246 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1250 else if (expiration && expire < time (NULL)) {
1251 if (s->flags & M_DISPLAY) {
1252 state_mark_attach (s);
1253 state_printf (s, _("[-- This %s/%s attachment is not included, --]\n"),
1254 TYPE (b->parts), b->parts->subtype);
1255 state_attach_puts (_("[-- and the indicated external source has --]\n"
1256 "[-- expired. --]\n"), s);
1258 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1259 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1264 if (s->flags & M_DISPLAY) {
1265 state_mark_attach (s);
1267 _("[-- This %s/%s attachment is not included, --]\n"),
1268 TYPE (b->parts), b->parts->subtype);
1269 state_mark_attach (s);
1272 ("[-- and the indicated access-type %s is unsupported --]\n"),
1274 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1275 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1282 void mutt_decode_attachment (BODY * b, STATE * s)
1284 int istext = mutt_is_text_part (b);
1285 iconv_t cd = (iconv_t) (-1);
1290 if (s->flags & M_CHARCONV) {
1291 char *charset = mutt_get_parameter ("charset", b->parameter);
1293 if (!option (OPTSTRICTMIME) && !charset)
1294 charset = mutt_get_first_charset (AssumedCharset);
1295 if (charset && Charset)
1296 cd = mutt_iconv_open (Charset, charset, M_ICONV_HOOK_FROM);
1299 if (b->file_charset)
1300 cd = mutt_iconv_open (Charset, b->file_charset, M_ICONV_HOOK_FROM);
1304 fseeko (s->fpin, b->offset, 0);
1305 switch (b->encoding) {
1306 case ENCQUOTEDPRINTABLE:
1307 mutt_decode_quoted (s, b->length, istext ||
1308 ((WithCrypto & APPLICATION_PGP) &&
1309 mutt_is_application_pgp (b)), cd);
1312 mutt_decode_base64 (s, b->length, istext ||
1313 ((WithCrypto & APPLICATION_PGP) &&
1314 mutt_is_application_pgp (b)), cd);
1317 mutt_decode_uuencoded (s, b->length, istext
1318 || ((WithCrypto & APPLICATION_PGP) &&
1319 mutt_is_application_pgp (b)), cd);
1322 mutt_decode_xbit (s, b->length, istext
1323 || ((WithCrypto & APPLICATION_PGP) &&
1324 mutt_is_application_pgp (b)), cd);
1328 if (cd != (iconv_t) (-1))
1332 int mutt_body_handler (BODY * b, STATE * s)
1337 char tempfile[_POSIX_PATH_MAX];
1338 handler_t handler = NULL;
1340 size_t tmplength = 0;
1344 int oflags = s->flags;
1346 /* first determine which handler to use to process this part */
1348 snprintf (type, sizeof (type), "%s/%s", TYPE (b), b->subtype);
1349 if (mutt_is_autoview (b, type)) {
1350 rfc1524_entry *entry = rfc1524_new_entry ();
1352 if (rfc1524_mailcap_lookup (b, type, entry, M_AUTOVIEW)) {
1353 handler = autoview_handler;
1354 s->flags &= ~M_CHARCONV;
1356 rfc1524_free_entry (&entry);
1358 else if (b->type == TYPETEXT) {
1359 if (ascii_strcasecmp ("plain", b->subtype) == 0) {
1360 /* avoid copying this part twice since removing the transfer-encoding is
1361 * the only operation needed.
1363 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b))
1364 handler = crypt_pgp_application_pgp_handler;
1366 if (ascii_strcasecmp
1367 ("flowed", mutt_get_parameter ("format", b->parameter)) == 0)
1368 handler = rfc3676_handler;
1372 else if (ascii_strcasecmp ("enriched", b->subtype) == 0)
1373 handler = text_enriched_handler;
1374 else /* text body type without a handler */
1377 else if (b->type == TYPEMESSAGE) {
1378 if (mutt_is_message_type (b->type, b->subtype))
1379 handler = message_handler;
1380 else if (!ascii_strcasecmp ("delivery-status", b->subtype))
1382 else if (!ascii_strcasecmp ("external-body", b->subtype))
1383 handler = external_body_handler;
1385 else if (b->type == TYPEMULTIPART) {
1388 if (ascii_strcasecmp ("alternative", b->subtype) == 0)
1389 handler = alternative_handler;
1390 else if (WithCrypto && ascii_strcasecmp ("signed", b->subtype) == 0) {
1391 p = mutt_get_parameter ("protocol", b->parameter);
1394 mutt_error (_("Error: multipart/signed has no protocol."));
1396 else if (s->flags & M_VERIFY)
1397 handler = mutt_signed_handler;
1399 else if ((WithCrypto & APPLICATION_PGP)
1400 && str_casecmp ("encrypted", b->subtype) == 0) {
1401 p = mutt_get_parameter ("protocol", b->parameter);
1405 ("Error: multipart/encrypted has no protocol parameter!"));
1407 else if (ascii_strcasecmp ("application/pgp-encrypted", p) == 0)
1408 handler = crypt_pgp_encrypted_handler;
1412 handler = multipart_handler;
1414 else if (WithCrypto && b->type == TYPEAPPLICATION) {
1415 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b))
1416 handler = crypt_pgp_application_pgp_handler;
1417 if ((WithCrypto & APPLICATION_SMIME) && mutt_is_application_smime (b))
1418 handler = crypt_smime_application_smime_handler;
1422 if (plaintext || handler) {
1423 fseeko (s->fpin, b->offset, 0);
1425 /* see if we need to decode this part before processing it */
1426 if (b->encoding == ENCBASE64 || b->encoding == ENCQUOTEDPRINTABLE || b->encoding == ENCUUENCODED || plaintext || mutt_is_text_part (b)) { /* text subtypes may
1428 * set conversion even
1429 * with 8bit encoding.
1431 int origType = b->type;
1432 char *savePrefix = NULL;
1435 /* decode to a tempfile, saving the original destination */
1437 mutt_mktemp (tempfile);
1438 if ((s->fpout = safe_fopen (tempfile, "w")) == NULL) {
1439 mutt_error _("Unable to open temporary file!");
1443 /* decoding the attachment changes the size and offset, so save a copy
1444 * of the "real" values now, and restore them after processing
1446 tmplength = b->length;
1447 tmpoffset = b->offset;
1449 /* if we are decoding binary bodies, we don't want to prefix each
1450 * line with the prefix or else the data will get corrupted.
1452 savePrefix = s->prefix;
1460 mutt_decode_attachment (b, s);
1463 b->length = ftello (s->fpout);
1467 /* restore final destination and substitute the tempfile for input */
1470 s->fpin = safe_fopen (tempfile, "r");
1473 /* restore the prefix */
1474 s->prefix = savePrefix;
1480 /* process the (decoded) body part */
1482 rc = handler (b, s);
1485 b->length = tmplength;
1486 b->offset = tmpoffset;
1488 /* restore the original source stream */
1493 s->flags |= M_FIRSTDONE;
1495 else if (s->flags & M_DISPLAY) {
1496 state_mark_attach (s);
1497 state_printf (s, _("[-- %s/%s is unsupported "), TYPE (b), b->subtype);
1498 if (!option (OPTVIEWATTACH)) {
1500 (type, sizeof (type),
1501 km_find_func (MENU_PAGER, OP_VIEW_ATTACHMENTS)))
1502 fprintf (s->fpout, _("(use '%s' to view this part)"), type);
1504 fputs (_("(need 'view-attachments' bound to key!)"), s->fpout);
1506 fputs (" --]\n", s->fpout);
1510 s->flags = oflags | (s->flags & M_FIRSTDONE);