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>
27 #include <lib-mime/mime.h>
30 #include "recvattach.h"
32 #include "mutt_curses.h"
38 #include <lib-crypt/crypt.h>
43 #include "lib/debug.h"
45 typedef int handler_f (BODY *, STATE *);
46 typedef handler_f *handler_t;
48 static void mutt_decode_xbit (STATE * s, long len, int istext, iconv_t cd)
57 while ((c = fgetc (s->fpin)) != EOF && len--) {
58 if (c == '\r' && len) {
59 if ((ch = fgetc (s->fpin)) == '\n') {
68 if (l == sizeof (bufi))
69 mutt_convert_to_state (cd, bufi, &l, s);
72 mutt_convert_to_state (cd, bufi, &l, s);
73 mutt_convert_to_state (cd, 0, 0, s);
75 state_reset_prefix (s);
78 mutt_copy_bytes (s->fpin, s->fpout, len);
81 static int qp_decode_triple (char *s, char *d)
84 if (*s == '=' && !(*(s + 1)))
87 /* quoted-printable triple */
89 isxdigit ((unsigned char) *(s + 1)) &&
90 isxdigit ((unsigned char) *(s + 2))) {
91 *d = (hexval (*(s + 1)) << 4) | hexval (*(s + 2));
99 static void qp_decode_line (char *dest, char *src, size_t * l, int last)
107 /* decode the line */
109 for (d = dest, s = src; *s;) {
110 switch ((kind = qp_decode_triple (s, &c))) {
114 break; /* qp triple */
117 break; /* single character */
121 break; /* soft line break */
125 if (!soft && last == '\n')
133 * Decode an attachment encoded with quoted-printable.
135 * Why doesn't this overflow any buffers? First, it's guaranteed
136 * that the length of a line grows when you _en_-code it to
137 * quoted-printable. That means that we always can store the
138 * result in a buffer of at most the _same_ size.
140 * Now, we don't special-case if the line we read with fgets()
141 * isn't terminated. We don't care about this, since STRING > 78,
142 * so corrupted input will just be corrupted a bit more. That
143 * implies that STRING+1 bytes are always sufficient to store the
144 * result of qp_decode_line.
146 * Finally, at soft line breaks, some part of a multibyte character
147 * may have been left over by mutt_convert_to_state(). This shouldn't
148 * be more than 6 characters, so STRING + 7 should be sufficient
149 * memory to store the decoded data.
151 * Just to make sure that I didn't make some off-by-one error
152 * above, we just use STRING*2 for the target buffer's size.
156 void mutt_decode_quoted (STATE * s, long len, int istext, iconv_t cd)
159 char decline[2 * STRING];
161 size_t linelen; /* number of input bytes in `line' */
164 int last; /* store the last character in the input line */
167 state_set_prefix (s);
173 * It's ok to use a fixed size buffer for input, even if the line turns
174 * out to be longer than this. Just process the line in chunks. This
175 * really shouldn't happen according the MIME spec, since Q-P encoded
176 * lines are at most 76 characters, but we should be liberal about what
179 if (fgets (line, MIN ((ssize_t) sizeof (line), len + 1), s->fpin) == NULL)
182 linelen = m_strlen(line);
186 * inspect the last character we read so we can tell if we got the
189 last = linelen ? line[linelen - 1] : 0;
191 /* chop trailing whitespace if we got the full line */
193 while (linelen > 0 && ISSPACE (line[linelen - 1]))
198 /* decode and do character set conversion */
199 qp_decode_line (decline + l, line, &l3, last);
201 mutt_convert_to_state (cd, decline, &l, s);
204 mutt_convert_to_state (cd, 0, 0, s);
205 state_reset_prefix (s);
208 void mutt_decode_base64 (STATE * s, long len, int istext, iconv_t cd)
211 int c1, c2, c3, c4, ch, cr = 0, i;
212 char bufi[BUFI_SIZE];
218 state_set_prefix (s);
221 for (i = 0; i < 4 && len > 0; len--) {
222 if ((ch = fgetc (s->fpin)) == EOF)
224 if (base64val(ch) >= 0 || ch == '=')
228 debug_print (2, ("didn't get a multiple of 4 chars.\n"));
232 c1 = base64val(buf[0]);
233 c2 = base64val(buf[1]);
234 ch = (c1 << 2) | (c2 >> 4);
236 if (cr && ch != '\n')
241 if (istext && ch == '\r')
248 c3 = base64val(buf[2]);
249 ch = ((c2 & 0xf) << 4) | (c3 >> 2);
251 if (cr && ch != '\n')
256 if (istext && ch == '\r')
263 c4 = base64val(buf[3]);
264 ch = ((c3 & 0x3) << 6) | c4;
266 if (cr && ch != '\n')
270 if (istext && ch == '\r')
275 if (l + 8 >= sizeof (bufi))
276 mutt_convert_to_state (cd, bufi, &l, s);
282 mutt_convert_to_state (cd, bufi, &l, s);
283 mutt_convert_to_state (cd, 0, 0, s);
285 state_reset_prefix (s);
288 unsigned char decode_byte (char ch)
295 void mutt_decode_uuencoded (STATE * s, long len, int istext, iconv_t cd)
297 char tmps[SHORT_STRING];
298 char linelen, c, l, out;
300 char bufi[BUFI_SIZE];
304 state_set_prefix (s);
307 if ((fgets (tmps, sizeof (tmps), s->fpin)) == NULL)
309 len -= m_strlen(tmps);
310 if ((!m_strncmp(tmps, "begin", 5)) && ISSPACE (tmps[5]))
314 if ((fgets (tmps, sizeof (tmps), s->fpin)) == NULL)
316 len -= m_strlen(tmps);
317 if (!m_strncmp(tmps, "end", 3))
320 linelen = decode_byte (*pt);
322 for (c = 0; c < linelen;) {
323 for (l = 2; l <= 6; l += 2) {
324 out = decode_byte (*pt) << l;
326 out |= (decode_byte (*pt) >> (6 - l));
332 mutt_convert_to_state (cd, bufi, &k, s);
337 mutt_convert_to_state (cd, bufi, &k, s);
338 mutt_convert_to_state (cd, 0, 0, s);
340 state_reset_prefix (s);
343 /* ----------------------------------------------------------------------------
344 * A (not so) minimal implementation of RFC1563.
347 #define IndentSize (4)
349 enum { RICH_PARAM = 0, RICH_BOLD, RICH_UNDERLINE, RICH_ITALIC, RICH_NOFILL,
350 RICH_INDENT, RICH_INDENT_RIGHT, RICH_EXCERPT, RICH_CENTER, RICH_FLUSHLEFT,
351 RICH_FLUSHRIGHT, RICH_COLOR, RICH_LAST_TAG
355 const char *tag_name;
359 "param", RICH_PARAM}, {
360 "bold", RICH_BOLD}, {
361 "italic", RICH_ITALIC}, {
362 "underline", RICH_UNDERLINE}, {
363 "nofill", RICH_NOFILL}, {
364 "excerpt", RICH_EXCERPT}, {
365 "indent", RICH_INDENT}, {
366 "indentright", RICH_INDENT_RIGHT}, {
367 "center", RICH_CENTER}, {
368 "flushleft", RICH_FLUSHLEFT}, {
369 "flushright", RICH_FLUSHRIGHT}, {
370 "flushboth", RICH_FLUSHLEFT}, {
371 "color", RICH_COLOR}, {
372 "x-color", RICH_COLOR}, {
376 struct enriched_state {
389 int tag_level[RICH_LAST_TAG];
394 static void enriched_wrap (struct enriched_state *stte)
399 if (stte->line_len) {
400 if (stte->tag_level[RICH_CENTER] || stte->tag_level[RICH_FLUSHRIGHT]) {
401 /* Strip trailing white space */
402 size_t y = stte->line_used - 1;
404 while (y && ISSPACE (stte->line[y])) {
405 stte->line[y] = '\0';
410 if (stte->tag_level[RICH_CENTER]) {
411 /* Strip leading whitespace */
414 while (stte->line[y] && ISSPACE (stte->line[y]))
419 for (z = y; z <= stte->line_used; z++) {
420 stte->line[z - y] = stte->line[z];
424 stte->line_used -= y;
429 extra = stte->WrapMargin - stte->line_len - stte->indent_len -
430 (stte->tag_level[RICH_INDENT_RIGHT] * IndentSize);
432 if (stte->tag_level[RICH_CENTER]) {
435 state_putc (' ', stte->s);
439 else if (stte->tag_level[RICH_FLUSHRIGHT]) {
442 state_putc (' ', stte->s);
447 state_puts (stte->line, stte->s);
450 state_putc ('\n', stte->s);
451 stte->line[0] = '\0';
454 stte->indent_len = 0;
455 if (stte->s->prefix) {
456 state_puts (stte->s->prefix, stte->s);
457 stte->indent_len += m_strlen(stte->s->prefix);
460 if (stte->tag_level[RICH_EXCERPT]) {
461 x = stte->tag_level[RICH_EXCERPT];
463 if (stte->s->prefix) {
464 state_puts (stte->s->prefix, stte->s);
465 stte->indent_len += m_strlen(stte->s->prefix);
468 state_puts ("> ", stte->s);
469 stte->indent_len += m_strlen("> ");
475 stte->indent_len = 0;
476 if (stte->tag_level[RICH_INDENT]) {
477 x = stte->tag_level[RICH_INDENT] * IndentSize;
478 stte->indent_len += x;
480 state_putc (' ', stte->s);
486 static void enriched_flush (struct enriched_state *stte, int wrap)
488 if (!stte->tag_level[RICH_NOFILL] && (stte->line_len + stte->word_len >
490 (stte->tag_level[RICH_INDENT_RIGHT] *
491 IndentSize) - stte->indent_len)))
492 enriched_wrap (stte);
494 if (stte->buff_used) {
495 stte->buffer[stte->buff_used] = '\0';
496 stte->line_used += stte->buff_used;
497 if (stte->line_used > stte->line_max) {
498 stte->line_max = stte->line_used;
499 p_realloc(&stte->line, stte->line_max + 1);
501 strcat (stte->line, stte->buffer); /* __STRCAT_CHECKED__ */
502 stte->line_len += stte->word_len;
507 enriched_wrap (stte);
511 static void enriched_putc (int c, struct enriched_state *stte)
513 if (stte->tag_level[RICH_PARAM]) {
514 if (stte->tag_level[RICH_COLOR]) {
515 if (stte->param_used + 1 >= stte->param_len)
516 p_realloc(&stte->param, (stte->param_len += STRING));
518 stte->param[stte->param_used++] = c;
520 return; /* nothing to do */
523 /* see if more space is needed (plus extra for possible rich characters) */
524 if (stte->buff_len < stte->buff_used + 3) {
525 stte->buff_len += LONG_STRING;
526 p_realloc(&stte->buffer, stte->buff_len + 1);
529 if ((!stte->tag_level[RICH_NOFILL] && ISSPACE (c)) || c == '\0') {
531 stte->word_len += 8 - (stte->line_len + stte->word_len) % 8;
535 stte->buffer[stte->buff_used++] = c;
536 enriched_flush (stte, 0);
539 if (stte->s->flags & M_DISPLAY) {
540 if (stte->tag_level[RICH_BOLD]) {
541 stte->buffer[stte->buff_used++] = c;
542 stte->buffer[stte->buff_used++] = '\010';
543 stte->buffer[stte->buff_used++] = c;
545 else if (stte->tag_level[RICH_UNDERLINE]) {
547 stte->buffer[stte->buff_used++] = '_';
548 stte->buffer[stte->buff_used++] = '\010';
549 stte->buffer[stte->buff_used++] = c;
551 else if (stte->tag_level[RICH_ITALIC]) {
552 stte->buffer[stte->buff_used++] = c;
553 stte->buffer[stte->buff_used++] = '\010';
554 stte->buffer[stte->buff_used++] = '_';
557 stte->buffer[stte->buff_used++] = c;
561 stte->buffer[stte->buff_used++] = c;
567 static void enriched_puts (const char *s, struct enriched_state *stte)
571 if (stte->buff_len < stte->buff_used + m_strlen(s)) {
572 stte->buff_len += LONG_STRING;
573 p_realloc(&stte->buffer, stte->buff_len + 1);
578 stte->buffer[stte->buff_used++] = *p++;
582 static void enriched_set_flags (const char *tag, struct enriched_state *stte)
584 const char *tagptr = tag;
590 for (i = 0, j = -1; EnrichedTags[i].tag_name; i++)
591 if (ascii_strcasecmp (EnrichedTags[i].tag_name, tagptr) == 0) {
592 j = EnrichedTags[i].index;
597 if (j == RICH_CENTER || j == RICH_FLUSHLEFT || j == RICH_FLUSHRIGHT)
598 enriched_flush (stte, 1);
601 if (stte->tag_level[j]) /* make sure not to go negative */
602 stte->tag_level[j]--;
603 if ((stte->s->flags & M_DISPLAY) && j == RICH_PARAM
604 && stte->tag_level[RICH_COLOR]) {
605 stte->param[stte->param_used] = '\0';
606 if (!ascii_strcasecmp (stte->param, "black")) {
607 enriched_puts ("\033[30m", stte);
609 else if (!ascii_strcasecmp (stte->param, "red")) {
610 enriched_puts ("\033[31m", stte);
612 else if (!ascii_strcasecmp (stte->param, "green")) {
613 enriched_puts ("\033[32m", stte);
615 else if (!ascii_strcasecmp (stte->param, "yellow")) {
616 enriched_puts ("\033[33m", stte);
618 else if (!ascii_strcasecmp (stte->param, "blue")) {
619 enriched_puts ("\033[34m", stte);
621 else if (!ascii_strcasecmp (stte->param, "magenta")) {
622 enriched_puts ("\033[35m", stte);
624 else if (!ascii_strcasecmp (stte->param, "cyan")) {
625 enriched_puts ("\033[36m", stte);
627 else if (!ascii_strcasecmp (stte->param, "white")) {
628 enriched_puts ("\033[37m", stte);
631 if ((stte->s->flags & M_DISPLAY) && j == RICH_COLOR) {
632 enriched_puts ("\033[0m", stte);
635 /* flush parameter buffer when closing the tag */
636 if (j == RICH_PARAM) {
637 stte->param_used = 0;
638 stte->param[0] = '\0';
642 stte->tag_level[j]++;
644 if (j == RICH_EXCERPT)
645 enriched_flush (stte, 1);
649 int text_enriched_handler (BODY * a, STATE * s)
652 TEXT, LANGLE, TAG, BOGUS_TAG, NEWLINE, ST_EOF, DONE
655 long bytes = a->length;
656 struct enriched_state stte;
659 char tag[LONG_STRING + 1];
664 ((s->flags & M_DISPLAY) ? (COLS - 4) : ((COLS - 4) <
665 72) ? (COLS - 4) : 72);
666 stte.line_max = stte.WrapMargin * 4;
667 stte.line = p_new(char, stte.line_max + 1);
668 stte.param = p_new(char, STRING);
670 stte.param_len = STRING;
674 state_puts (s->prefix, s);
675 stte.indent_len += m_strlen(s->prefix);
678 while (state != DONE) {
679 if (state != ST_EOF) {
680 if (!bytes || (c = fgetc (s->fpin)) == EOF)
694 if (stte.tag_level[RICH_NOFILL]) {
695 enriched_flush (&stte, 1);
698 enriched_putc (' ', &stte);
704 enriched_putc (c, &stte);
710 enriched_putc (c, &stte);
718 /* Yes, fall through (it wasn't a <<, so this char is first in TAG) */
722 enriched_set_flags (tag, &stte);
725 else if (tag_len < LONG_STRING) /* ignore overly long tags */
738 enriched_flush (&stte, 1);
747 enriched_putc ('\0', &stte);
748 enriched_flush (&stte, 1);
752 case DONE: /* not reached, but gcc complains if this is absent */
757 state_putc ('\n', s); /* add a final newline */
759 p_delete(&(stte.buffer));
760 p_delete(&(stte.line));
761 p_delete(&(stte.param));
768 #define TXTENRICHED 3
770 static int alternative_handler (BODY * a, STATE * s)
780 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
781 a->encoding == ENCUUENCODED) {
785 fstat (fileno (s->fpin), &st);
786 b = mutt_new_body ();
787 b->length = (long) st.st_size;
788 b->parts = mutt_parse_multipart (s->fpin,
789 mutt_get_parameter ("boundary",
792 ascii_strcasecmp ("digest",
800 /* First, search list of prefered types */
801 t = AlternativeOrderList;
802 while (t && !choice) {
804 int btlen; /* length of basetype */
805 int wild; /* do we have a wildcard to match all subtypes? */
807 c = strchr (t->data, '/');
809 wild = (c[1] == '*' && c[2] == 0);
814 btlen = m_strlen(t->data);
822 const char *bt = TYPE (b);
824 if (!ascii_strncasecmp (bt, t->data, btlen) && bt[btlen] == 0) {
825 /* the basetype matches */
826 if (wild || !ascii_strcasecmp (t->data + btlen + 1, b->subtype)) {
835 /* Next, look for an autoviewable type */
842 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
843 if (mutt_is_autoview (b, buf)) {
844 rfc1524_entry *entry = rfc1524_new_entry ();
846 if (rfc1524_mailcap_lookup (b, buf, entry, M_AUTOVIEW)) {
849 rfc1524_free_entry (&entry);
855 /* Then, look for a text entry */
862 if (b->type == TYPETEXT) {
863 if (!ascii_strcasecmp ("plain", b->subtype) && type <= TXTPLAIN) {
867 else if (!ascii_strcasecmp ("enriched", b->subtype)
868 && type <= TXTENRICHED) {
872 else if (!ascii_strcasecmp ("html", b->subtype) && type <= TXTHTML) {
881 /* Finally, look for other possibilities */
888 if (mutt_can_decode (b))
895 if (s->flags & M_DISPLAY && !option (OPTWEED)) {
896 fseeko (s->fpin, choice->hdr_offset, 0);
897 mutt_copy_bytes (s->fpin, s->fpout,
898 choice->offset - choice->hdr_offset);
900 mutt_body_handler (choice, s);
902 else if (s->flags & M_DISPLAY) {
903 /* didn't find anything that we could display! */
904 state_mark_attach (s);
905 state_puts (_("[-- Error: Could not display any parts of Multipart/Alternative! --]\n"), s);
915 /* handles message/rfc822 body parts */
916 static int message_handler (BODY * a, STATE * s)
923 off_start = ftello (s->fpin);
924 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
925 a->encoding == ENCUUENCODED) {
926 fstat (fileno (s->fpin), &st);
927 b = mutt_new_body ();
928 b->length = (long) st.st_size;
929 b->parts = mutt_parse_messageRFC822 (s->fpin, b);
935 mutt_copy_hdr (s->fpin, s->fpout, off_start, b->parts->offset,
936 (((s->flags & M_WEED)
937 || ((s->flags & (M_DISPLAY | M_PRINTING))
938 && option (OPTWEED))) ? (CH_WEED | CH_REORDER) : 0) |
939 (s->prefix ? CH_PREFIX : 0) | CH_DECODE | CH_FROM,
943 state_puts (s->prefix, s);
944 state_putc ('\n', s);
946 rc = mutt_body_handler (b->parts, s);
949 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
950 a->encoding == ENCUUENCODED)
956 /* returns 1 if decoding the attachment will produce output */
957 int mutt_can_decode (BODY * a)
961 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
962 if (mutt_is_autoview (a, type))
963 return (rfc1524_mailcap_lookup (a, type, NULL, M_AUTOVIEW));
964 else if (a->type == TYPETEXT)
966 else if (a->type == TYPEMESSAGE)
968 else if (a->type == TYPEMULTIPART) {
971 if (ascii_strcasecmp (a->subtype, "signed") == 0 ||
972 ascii_strcasecmp (a->subtype, "encrypted") == 0)
975 for (p = a->parts; p; p = p->next) {
976 if (mutt_can_decode (p))
981 else if (a->type == TYPEAPPLICATION) {
982 if (mutt_is_application_pgp(a))
984 if (mutt_is_application_smime (a))
991 static int multipart_handler (BODY * a, STATE * s)
999 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1000 a->encoding == ENCUUENCODED) {
1001 fstat (fileno (s->fpin), &st);
1002 b = mutt_new_body ();
1003 b->length = (long) st.st_size;
1004 b->parts = mutt_parse_multipart (s->fpin,
1005 mutt_get_parameter ("boundary",
1008 ascii_strcasecmp ("digest",
1014 for (p = b->parts, count = 1; p; p = p->next, count++) {
1015 if (s->flags & M_DISPLAY) {
1016 state_mark_attach (s);
1017 state_printf (s, _("[-- Attachment #%d"), count);
1018 if (p->description || p->filename || p->form_name) {
1019 state_puts (": ", s);
1020 state_puts (p->description ? p->description :
1021 p->filename ? p->filename : p->form_name, s);
1023 state_puts (" --]\n", s);
1025 mutt_pretty_size (length, sizeof (length), p->length);
1027 state_mark_attach (s);
1028 state_printf (s, _("[-- Type: %s/%s, Encoding: %s, Size: %s --]\n"),
1029 TYPE (p), p->subtype, ENCODING (p->encoding), length);
1030 if (!option (OPTWEED)) {
1031 fseeko (s->fpin, p->hdr_offset, 0);
1032 mutt_copy_bytes (s->fpin, s->fpout, p->offset - p->hdr_offset);
1035 state_putc ('\n', s);
1038 if (p->description && mutt_can_decode (p))
1039 state_printf (s, "Content-Description: %s\n", p->description);
1042 state_printf (s, "%s: \n", p->form_name);
1045 rc = mutt_body_handler (p, s);
1046 state_putc ('\n', s);
1047 if (rc || ((s->flags & M_REPLYING)
1048 && (option (OPTINCLUDEONLYFIRST)) && (s->flags & M_FIRSTDONE)))
1052 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1053 a->encoding == ENCUUENCODED)
1054 mutt_free_body (&b);
1059 static int autoview_handler (BODY * a, STATE * s)
1061 rfc1524_entry *entry = rfc1524_new_entry ();
1062 char buffer[LONG_STRING];
1064 char command[LONG_STRING];
1065 char tempfile[_POSIX_PATH_MAX] = "";
1074 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
1075 rfc1524_mailcap_lookup (a, type, entry, M_AUTOVIEW);
1077 fname = m_strdup(a->filename);
1078 mutt_sanitize_filename (fname, 1);
1079 rfc1524_expand_filename (entry->nametemplate, fname, tempfile,
1083 if (entry->command) {
1084 m_strcpy(command, sizeof(command), entry->command);
1086 /* rfc1524_expand_command returns 0 if the file is required */
1088 rfc1524_expand_command (a, tempfile, type, command, sizeof (command));
1090 if (s->flags & M_DISPLAY) {
1091 state_mark_attach (s);
1092 state_printf (s, _("[-- Autoview using %s --]\n"), command);
1093 mutt_message (_("Invoking autoview command: %s"), command);
1096 if ((fpin = safe_fopen (tempfile, "w+")) == NULL) {
1097 mutt_perror ("fopen");
1098 rfc1524_free_entry (&entry);
1102 mutt_copy_bytes (s->fpin, fpin, a->length);
1105 safe_fclose (&fpin);
1106 thepid = mutt_create_filter (command, NULL, &fpout, &fperr);
1112 thepid = mutt_create_filter_fd (command, NULL, &fpout, &fperr,
1113 fileno (fpin), -1, -1);
1117 mutt_perror (_("Can't create filter"));
1119 if (s->flags & M_DISPLAY) {
1120 state_mark_attach (s);
1121 state_printf (s, _("[-- Can't run %s. --]\n"), command);
1128 while (fgets (buffer, sizeof (buffer), fpout) != NULL) {
1129 state_puts (s->prefix, s);
1130 state_puts (buffer, s);
1132 /* check for data on stderr */
1133 if (fgets (buffer, sizeof (buffer), fperr)) {
1134 if (s->flags & M_DISPLAY) {
1135 state_mark_attach (s);
1136 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1139 state_puts (s->prefix, s);
1140 state_puts (buffer, s);
1141 while (fgets (buffer, sizeof (buffer), fperr) != NULL) {
1142 state_puts (s->prefix, s);
1143 state_puts (buffer, s);
1148 mutt_copy_stream (fpout, s->fpout);
1149 /* Check for stderr messages */
1150 if (fgets (buffer, sizeof (buffer), fperr)) {
1151 if (s->flags & M_DISPLAY) {
1152 state_mark_attach (s);
1153 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1156 state_puts (buffer, s);
1157 mutt_copy_stream (fperr, s->fpout);
1162 safe_fclose (&fpout);
1163 safe_fclose (&fperr);
1165 mutt_wait_filter (thepid);
1167 safe_fclose (&fpin);
1169 mutt_unlink (tempfile);
1171 if (s->flags & M_DISPLAY)
1172 mutt_clear_error ();
1174 rfc1524_free_entry (&entry);
1178 static int external_body_handler (BODY * b, STATE * s)
1180 const char *access_type;
1181 const char *expiration;
1184 access_type = mutt_get_parameter ("access-type", b->parameter);
1186 if (s->flags & M_DISPLAY) {
1187 state_mark_attach (s);
1188 state_puts (_("[-- Error: message/external-body has no access-type parameter --]\n"), s);
1193 expiration = mutt_get_parameter ("expiration", b->parameter);
1195 expire = mutt_parse_date (expiration, NULL);
1199 if (!ascii_strcasecmp (access_type, "x-mutt-deleted")) {
1200 if (s->flags & (M_DISPLAY | M_PRINTING)) {
1202 char pretty_size[10];
1204 state_mark_attach (s);
1205 state_printf (s, _("[-- This %s/%s attachment "),
1206 TYPE (b->parts), b->parts->subtype);
1207 length = mutt_get_parameter ("length", b->parameter);
1209 mutt_pretty_size (pretty_size, sizeof (pretty_size),
1210 strtol (length, NULL, 10));
1211 state_printf (s, _("(size %s bytes) "), pretty_size);
1213 state_puts (_("has been deleted --]\n"), s);
1216 state_mark_attach (s);
1217 state_printf (s, _("[-- on %s --]\n"), expiration);
1219 if (b->parts->filename) {
1220 state_mark_attach (s);
1221 state_printf (s, _("[-- name: %s --]\n"), b->parts->filename);
1224 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1225 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1229 else if (expiration && expire < time (NULL)) {
1230 if (s->flags & M_DISPLAY) {
1231 state_mark_attach (s);
1232 state_printf (s, _("[-- This %s/%s attachment is not included, --]\n"),
1233 TYPE (b->parts), b->parts->subtype);
1234 state_attach_puts (_("[-- and the indicated external source has --]\n"
1235 "[-- expired. --]\n"), s);
1237 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1238 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1243 if (s->flags & M_DISPLAY) {
1244 state_mark_attach (s);
1246 _("[-- This %s/%s attachment is not included, --]\n"),
1247 TYPE (b->parts), b->parts->subtype);
1248 state_mark_attach (s);
1251 ("[-- and the indicated access-type %s is unsupported --]\n"),
1253 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1254 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1261 void mutt_decode_attachment (BODY * b, STATE * s)
1263 int istext = mutt_is_text_part (b);
1264 iconv_t cd = (iconv_t) (-1);
1269 if (s->flags & M_CHARCONV) {
1270 const char *charset = mutt_get_parameter ("charset", b->parameter);
1272 if (!option (OPTSTRICTMIME) && !charset)
1273 charset = mutt_get_first_charset (AssumedCharset);
1274 if (charset && Charset)
1275 cd = mutt_iconv_open (Charset, charset, M_ICONV_HOOK_FROM);
1278 if (b->file_charset)
1279 cd = mutt_iconv_open (Charset, b->file_charset, M_ICONV_HOOK_FROM);
1283 fseeko (s->fpin, b->offset, 0);
1284 switch (b->encoding) {
1285 case ENCQUOTEDPRINTABLE:
1286 mutt_decode_quoted(s, b->length,
1287 istext || mutt_is_application_pgp(b), cd);
1290 mutt_decode_base64(s, b->length,
1291 istext || mutt_is_application_pgp(b), cd);
1294 mutt_decode_uuencoded(s, b->length,
1295 istext || mutt_is_application_pgp(b), cd);
1298 mutt_decode_xbit(s, b->length,
1299 istext || mutt_is_application_pgp(b), cd);
1303 if (cd != (iconv_t) (-1))
1307 int mutt_body_handler (BODY * b, STATE * s)
1312 char tempfile[_POSIX_PATH_MAX];
1313 handler_t handler = NULL;
1315 size_t tmplength = 0;
1319 int oflags = s->flags;
1321 /* first determine which handler to use to process this part */
1323 snprintf (type, sizeof (type), "%s/%s", TYPE (b), b->subtype);
1324 if (mutt_is_autoview (b, type)) {
1325 rfc1524_entry *entry = rfc1524_new_entry ();
1327 if (rfc1524_mailcap_lookup (b, type, entry, M_AUTOVIEW)) {
1328 handler = autoview_handler;
1329 s->flags &= ~M_CHARCONV;
1331 rfc1524_free_entry (&entry);
1333 else if (b->type == TYPETEXT) {
1334 if (ascii_strcasecmp ("plain", b->subtype) == 0) {
1335 /* avoid copying this part twice since removing the transfer-encoding is
1336 * the only operation needed.
1338 if (mutt_is_application_pgp (b))
1339 handler = crypt_pgp_application_pgp_handler;
1341 if (ascii_strcasecmp
1342 ("flowed", mutt_get_parameter ("format", b->parameter)) == 0)
1343 handler = rfc3676_handler;
1347 else if (ascii_strcasecmp ("enriched", b->subtype) == 0)
1348 handler = text_enriched_handler;
1349 else /* text body type without a handler */
1352 else if (b->type == TYPEMESSAGE) {
1353 if (mutt_is_message_type (b->type, b->subtype))
1354 handler = message_handler;
1355 else if (!ascii_strcasecmp ("delivery-status", b->subtype))
1357 else if (!ascii_strcasecmp ("external-body", b->subtype))
1358 handler = external_body_handler;
1360 else if (b->type == TYPEMULTIPART) {
1363 if (ascii_strcasecmp ("alternative", b->subtype) == 0)
1364 handler = alternative_handler;
1365 else if (ascii_strcasecmp ("signed", b->subtype) == 0) {
1366 p = mutt_get_parameter ("protocol", b->parameter);
1369 mutt_error (_("Error: multipart/signed has no protocol."));
1371 else if (s->flags & M_VERIFY)
1372 handler = mutt_signed_handler;
1374 else if (m_strcasecmp("encrypted", b->subtype) == 0) {
1375 p = mutt_get_parameter ("protocol", b->parameter);
1379 ("Error: multipart/encrypted has no protocol parameter!"));
1381 else if (ascii_strcasecmp ("application/pgp-encrypted", p) == 0)
1382 handler = crypt_pgp_encrypted_handler;
1386 handler = multipart_handler;
1388 else if (b->type == TYPEAPPLICATION) {
1389 if (mutt_is_application_pgp (b))
1390 handler = crypt_pgp_application_pgp_handler;
1391 if (mutt_is_application_smime (b))
1392 handler = crypt_smime_application_smime_handler;
1396 if (plaintext || handler) {
1397 fseeko (s->fpin, b->offset, 0);
1399 /* see if we need to decode this part before processing it */
1400 if (b->encoding == ENCBASE64 || b->encoding == ENCQUOTEDPRINTABLE || b->encoding == ENCUUENCODED || plaintext || mutt_is_text_part (b)) { /* text subtypes may
1402 * set conversion even
1403 * with 8bit encoding.
1405 int origType = b->type;
1406 char *savePrefix = NULL;
1409 /* decode to a tempfile, saving the original destination */
1411 mutt_mktemp (tempfile);
1412 if ((s->fpout = safe_fopen (tempfile, "w")) == NULL) {
1413 mutt_error _("Unable to open temporary file!");
1417 /* decoding the attachment changes the size and offset, so save a copy
1418 * of the "real" values now, and restore them after processing
1420 tmplength = b->length;
1421 tmpoffset = b->offset;
1423 /* if we are decoding binary bodies, we don't want to prefix each
1424 * line with the prefix or else the data will get corrupted.
1426 savePrefix = s->prefix;
1434 mutt_decode_attachment (b, s);
1437 b->length = ftello (s->fpout);
1441 /* restore final destination and substitute the tempfile for input */
1444 s->fpin = safe_fopen (tempfile, "r");
1447 /* restore the prefix */
1448 s->prefix = savePrefix;
1454 /* process the (decoded) body part */
1456 rc = handler (b, s);
1459 b->length = tmplength;
1460 b->offset = tmpoffset;
1462 /* restore the original source stream */
1467 s->flags |= M_FIRSTDONE;
1469 else if (s->flags & M_DISPLAY) {
1470 state_mark_attach (s);
1471 state_printf (s, _("[-- %s/%s is unsupported "), TYPE (b), b->subtype);
1472 if (!option (OPTVIEWATTACH)) {
1474 (type, sizeof (type),
1475 km_find_func (MENU_PAGER, OP_VIEW_ATTACHMENTS)))
1476 fprintf (s->fpout, _("(use '%s' to view this part)"), type);
1478 fputs (_("(need 'view-attachments' bound to key!)"), s->fpout);
1480 fputs (" --]\n", s->fpout);
1484 s->flags = oflags | (s->flags & M_FIRSTDONE);