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>
29 #include <lib-ui/curses.h>
32 #include "recvattach.h"
39 #include <lib-crypt/crypt.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, ssize_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 ssize_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 == '=')
231 c1 = base64val(buf[0]);
232 c2 = base64val(buf[1]);
233 ch = (c1 << 2) | (c2 >> 4);
235 if (cr && ch != '\n')
240 if (istext && ch == '\r')
247 c3 = base64val(buf[2]);
248 ch = ((c2 & 0xf) << 4) | (c3 >> 2);
250 if (cr && ch != '\n')
255 if (istext && ch == '\r')
262 c4 = base64val(buf[3]);
263 ch = ((c3 & 0x3) << 6) | c4;
265 if (cr && ch != '\n')
269 if (istext && ch == '\r')
274 if (l + 8 >= sizeof (bufi))
275 mutt_convert_to_state (cd, bufi, &l, s);
281 mutt_convert_to_state (cd, bufi, &l, s);
282 mutt_convert_to_state (cd, 0, 0, s);
284 state_reset_prefix (s);
287 unsigned char decode_byte (char ch)
294 void mutt_decode_uuencoded (STATE * s, long len, int istext, iconv_t cd)
296 char tmps[SHORT_STRING];
297 char linelen, c, l, out;
299 char bufi[BUFI_SIZE];
303 state_set_prefix (s);
306 if ((fgets (tmps, sizeof (tmps), s->fpin)) == NULL)
308 len -= m_strlen(tmps);
309 if ((!m_strncmp(tmps, "begin", 5)) && ISSPACE (tmps[5]))
313 if ((fgets (tmps, sizeof (tmps), s->fpin)) == NULL)
315 len -= m_strlen(tmps);
316 if (!m_strncmp(tmps, "end", 3))
319 linelen = decode_byte (*pt);
321 for (c = 0; c < linelen;) {
322 for (l = 2; l <= 6; l += 2) {
323 out = decode_byte (*pt) << l;
325 out |= (decode_byte (*pt) >> (6 - l));
331 mutt_convert_to_state (cd, bufi, &k, s);
336 mutt_convert_to_state (cd, bufi, &k, s);
337 mutt_convert_to_state (cd, 0, 0, s);
339 state_reset_prefix (s);
342 /* ----------------------------------------------------------------------------
343 * A (not so) minimal implementation of RFC1563.
346 #define IndentSize (4)
348 enum { RICH_PARAM = 0, RICH_BOLD, RICH_UNDERLINE, RICH_ITALIC, RICH_NOFILL,
349 RICH_INDENT, RICH_INDENT_RIGHT, RICH_EXCERPT, RICH_CENTER, RICH_FLUSHLEFT,
350 RICH_FLUSHRIGHT, RICH_COLOR, RICH_LAST_TAG
354 const char *tag_name;
358 "param", RICH_PARAM}, {
359 "bold", RICH_BOLD}, {
360 "italic", RICH_ITALIC}, {
361 "underline", RICH_UNDERLINE}, {
362 "nofill", RICH_NOFILL}, {
363 "excerpt", RICH_EXCERPT}, {
364 "indent", RICH_INDENT}, {
365 "indentright", RICH_INDENT_RIGHT}, {
366 "center", RICH_CENTER}, {
367 "flushleft", RICH_FLUSHLEFT}, {
368 "flushright", RICH_FLUSHRIGHT}, {
369 "flushboth", RICH_FLUSHLEFT}, {
370 "color", RICH_COLOR}, {
371 "x-color", RICH_COLOR}, {
375 struct enriched_state {
388 int tag_level[RICH_LAST_TAG];
393 static void enriched_wrap (struct enriched_state *stte)
398 if (stte->line_len) {
399 if (stte->tag_level[RICH_CENTER] || stte->tag_level[RICH_FLUSHRIGHT]) {
400 /* Strip trailing white space */
401 ssize_t y = stte->line_used - 1;
403 while (y && ISSPACE (stte->line[y])) {
404 stte->line[y] = '\0';
409 if (stte->tag_level[RICH_CENTER]) {
410 /* Strip leading whitespace */
413 while (stte->line[y] && ISSPACE (stte->line[y]))
418 for (z = y; z <= stte->line_used; z++) {
419 stte->line[z - y] = stte->line[z];
423 stte->line_used -= y;
428 extra = stte->WrapMargin - stte->line_len - stte->indent_len -
429 (stte->tag_level[RICH_INDENT_RIGHT] * IndentSize);
431 if (stte->tag_level[RICH_CENTER]) {
434 state_putc (' ', stte->s);
438 else if (stte->tag_level[RICH_FLUSHRIGHT]) {
441 state_putc (' ', stte->s);
446 state_puts (stte->line, stte->s);
449 state_putc ('\n', stte->s);
450 stte->line[0] = '\0';
453 stte->indent_len = 0;
454 if (stte->s->prefix) {
455 state_puts (stte->s->prefix, stte->s);
456 stte->indent_len += m_strlen(stte->s->prefix);
459 if (stte->tag_level[RICH_EXCERPT]) {
460 x = stte->tag_level[RICH_EXCERPT];
462 if (stte->s->prefix) {
463 state_puts (stte->s->prefix, stte->s);
464 stte->indent_len += m_strlen(stte->s->prefix);
467 state_puts ("> ", stte->s);
468 stte->indent_len += m_strlen("> ");
474 stte->indent_len = 0;
475 if (stte->tag_level[RICH_INDENT]) {
476 x = stte->tag_level[RICH_INDENT] * IndentSize;
477 stte->indent_len += x;
479 state_putc (' ', stte->s);
485 static void enriched_flush (struct enriched_state *stte, int wrap)
487 if (!stte->tag_level[RICH_NOFILL] && (stte->line_len + stte->word_len >
489 (stte->tag_level[RICH_INDENT_RIGHT] *
490 IndentSize) - stte->indent_len)))
491 enriched_wrap (stte);
493 if (stte->buff_used) {
494 stte->buffer[stte->buff_used] = '\0';
495 stte->line_used += stte->buff_used;
496 if (stte->line_used > stte->line_max) {
497 stte->line_max = stte->line_used;
498 p_realloc(&stte->line, stte->line_max + 1);
500 strcat (stte->line, stte->buffer); /* __STRCAT_CHECKED__ */
501 stte->line_len += stte->word_len;
506 enriched_wrap (stte);
510 static void enriched_putc (int c, struct enriched_state *stte)
512 if (stte->tag_level[RICH_PARAM]) {
513 if (stte->tag_level[RICH_COLOR]) {
514 if (stte->param_used + 1 >= stte->param_len)
515 p_realloc(&stte->param, (stte->param_len += STRING));
517 stte->param[stte->param_used++] = c;
519 return; /* nothing to do */
522 /* see if more space is needed (plus extra for possible rich characters) */
523 if (stte->buff_len < stte->buff_used + 3) {
524 stte->buff_len += LONG_STRING;
525 p_realloc(&stte->buffer, stte->buff_len + 1);
528 if ((!stte->tag_level[RICH_NOFILL] && ISSPACE (c)) || c == '\0') {
530 stte->word_len += 8 - (stte->line_len + stte->word_len) % 8;
534 stte->buffer[stte->buff_used++] = c;
535 enriched_flush (stte, 0);
538 if (stte->s->flags & M_DISPLAY) {
539 if (stte->tag_level[RICH_BOLD]) {
540 stte->buffer[stte->buff_used++] = c;
541 stte->buffer[stte->buff_used++] = '\010';
542 stte->buffer[stte->buff_used++] = c;
544 else if (stte->tag_level[RICH_UNDERLINE]) {
546 stte->buffer[stte->buff_used++] = '_';
547 stte->buffer[stte->buff_used++] = '\010';
548 stte->buffer[stte->buff_used++] = c;
550 else if (stte->tag_level[RICH_ITALIC]) {
551 stte->buffer[stte->buff_used++] = c;
552 stte->buffer[stte->buff_used++] = '\010';
553 stte->buffer[stte->buff_used++] = '_';
556 stte->buffer[stte->buff_used++] = c;
560 stte->buffer[stte->buff_used++] = c;
566 static void enriched_puts (const char *s, struct enriched_state *stte)
570 if (stte->buff_len < stte->buff_used + m_strlen(s)) {
571 stte->buff_len += LONG_STRING;
572 p_realloc(&stte->buffer, stte->buff_len + 1);
577 stte->buffer[stte->buff_used++] = *p++;
581 static void enriched_set_flags (const char *tag, struct enriched_state *stte)
583 const char *tagptr = tag;
589 for (i = 0, j = -1; EnrichedTags[i].tag_name; i++)
590 if (ascii_strcasecmp (EnrichedTags[i].tag_name, tagptr) == 0) {
591 j = EnrichedTags[i].index;
596 if (j == RICH_CENTER || j == RICH_FLUSHLEFT || j == RICH_FLUSHRIGHT)
597 enriched_flush (stte, 1);
600 if (stte->tag_level[j]) /* make sure not to go negative */
601 stte->tag_level[j]--;
602 if ((stte->s->flags & M_DISPLAY) && j == RICH_PARAM
603 && stte->tag_level[RICH_COLOR]) {
604 stte->param[stte->param_used] = '\0';
605 if (!ascii_strcasecmp (stte->param, "black")) {
606 enriched_puts ("\033[30m", stte);
608 else if (!ascii_strcasecmp (stte->param, "red")) {
609 enriched_puts ("\033[31m", stte);
611 else if (!ascii_strcasecmp (stte->param, "green")) {
612 enriched_puts ("\033[32m", stte);
614 else if (!ascii_strcasecmp (stte->param, "yellow")) {
615 enriched_puts ("\033[33m", stte);
617 else if (!ascii_strcasecmp (stte->param, "blue")) {
618 enriched_puts ("\033[34m", stte);
620 else if (!ascii_strcasecmp (stte->param, "magenta")) {
621 enriched_puts ("\033[35m", stte);
623 else if (!ascii_strcasecmp (stte->param, "cyan")) {
624 enriched_puts ("\033[36m", stte);
626 else if (!ascii_strcasecmp (stte->param, "white")) {
627 enriched_puts ("\033[37m", stte);
630 if ((stte->s->flags & M_DISPLAY) && j == RICH_COLOR) {
631 enriched_puts ("\033[0m", stte);
634 /* flush parameter buffer when closing the tag */
635 if (j == RICH_PARAM) {
636 stte->param_used = 0;
637 stte->param[0] = '\0';
641 stte->tag_level[j]++;
643 if (j == RICH_EXCERPT)
644 enriched_flush (stte, 1);
648 int text_enriched_handler (BODY * a, STATE * s)
651 TEXT, LANGLE, TAG, BOGUS_TAG, NEWLINE, ST_EOF, DONE
654 long bytes = a->length;
655 struct enriched_state stte;
658 char tag[LONG_STRING + 1];
663 ((s->flags & M_DISPLAY) ? (COLS - 4) : ((COLS - 4) <
664 72) ? (COLS - 4) : 72);
665 stte.line_max = stte.WrapMargin * 4;
666 stte.line = p_new(char, stte.line_max + 1);
667 stte.param = p_new(char, STRING);
669 stte.param_len = STRING;
673 state_puts (s->prefix, s);
674 stte.indent_len += m_strlen(s->prefix);
677 while (state != DONE) {
678 if (state != ST_EOF) {
679 if (!bytes || (c = fgetc (s->fpin)) == EOF)
693 if (stte.tag_level[RICH_NOFILL]) {
694 enriched_flush (&stte, 1);
697 enriched_putc (' ', &stte);
703 enriched_putc (c, &stte);
709 enriched_putc (c, &stte);
717 /* Yes, fall through (it wasn't a <<, so this char is first in TAG) */
721 enriched_set_flags (tag, &stte);
724 else if (tag_len < LONG_STRING) /* ignore overly long tags */
737 enriched_flush (&stte, 1);
746 enriched_putc ('\0', &stte);
747 enriched_flush (&stte, 1);
751 case DONE: /* not reached, but gcc complains if this is absent */
756 state_putc ('\n', s); /* add a final newline */
758 p_delete(&(stte.buffer));
759 p_delete(&(stte.line));
760 p_delete(&(stte.param));
767 #define TXTENRICHED 3
769 static int alternative_handler (BODY * a, STATE * s)
779 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
780 a->encoding == ENCUUENCODED) {
784 fstat (fileno (s->fpin), &st);
785 b = mutt_new_body ();
786 b->length = (long) st.st_size;
787 b->parts = mutt_parse_multipart (s->fpin,
788 mutt_get_parameter ("boundary",
791 ascii_strcasecmp ("digest",
799 /* First, search list of prefered types */
800 t = AlternativeOrderList;
801 while (t && !choice) {
803 int btlen; /* length of basetype */
804 int wild; /* do we have a wildcard to match all subtypes? */
806 c = strchr (t->data, '/');
808 wild = (c[1] == '*' && c[2] == 0);
813 btlen = m_strlen(t->data);
821 const char *bt = TYPE (b);
823 if (!ascii_strncasecmp (bt, t->data, btlen) && bt[btlen] == 0) {
824 /* the basetype matches */
825 if (wild || !ascii_strcasecmp (t->data + btlen + 1, b->subtype)) {
834 /* Next, look for an autoviewable type */
841 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
842 if (mutt_is_autoview (b, buf)) {
843 rfc1524_entry *entry = rfc1524_new_entry ();
845 if (rfc1524_mailcap_lookup (b, buf, entry, M_AUTOVIEW)) {
848 rfc1524_free_entry (&entry);
854 /* Then, look for a text entry */
861 if (b->type == TYPETEXT) {
862 if (!ascii_strcasecmp ("plain", b->subtype) && type <= TXTPLAIN) {
866 else if (!ascii_strcasecmp ("enriched", b->subtype)
867 && type <= TXTENRICHED) {
871 else if (!ascii_strcasecmp ("html", b->subtype) && type <= TXTHTML) {
880 /* Finally, look for other possibilities */
887 if (mutt_can_decode (b))
894 if (s->flags & M_DISPLAY && !option (OPTWEED)) {
895 fseeko (s->fpin, choice->hdr_offset, 0);
896 mutt_copy_bytes (s->fpin, s->fpout,
897 choice->offset - choice->hdr_offset);
899 mutt_body_handler (choice, s);
901 else if (s->flags & M_DISPLAY) {
902 /* didn't find anything that we could display! */
903 state_mark_attach (s);
904 state_puts (_("[-- Error: Could not display any parts of Multipart/Alternative! --]\n"), s);
914 /* handles message/rfc822 body parts */
915 static int message_handler (BODY * a, STATE * s)
922 off_start = ftello (s->fpin);
923 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
924 a->encoding == ENCUUENCODED) {
925 fstat (fileno (s->fpin), &st);
926 b = mutt_new_body ();
927 b->length = (long) st.st_size;
928 b->parts = mutt_parse_messageRFC822 (s->fpin, b);
934 mutt_copy_hdr (s->fpin, s->fpout, off_start, b->parts->offset,
935 (((s->flags & M_WEED)
936 || ((s->flags & (M_DISPLAY | M_PRINTING))
937 && option (OPTWEED))) ? (CH_WEED | CH_REORDER) : 0) |
938 (s->prefix ? CH_PREFIX : 0) | CH_DECODE | CH_FROM,
942 state_puts (s->prefix, s);
943 state_putc ('\n', s);
945 rc = mutt_body_handler (b->parts, s);
948 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
949 a->encoding == ENCUUENCODED)
955 /* returns 1 if decoding the attachment will produce output */
956 int mutt_can_decode (BODY * a)
960 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
961 if (mutt_is_autoview (a, type))
962 return (rfc1524_mailcap_lookup (a, type, NULL, M_AUTOVIEW));
963 else if (a->type == TYPETEXT)
965 else if (a->type == TYPEMESSAGE)
967 else if (a->type == TYPEMULTIPART) {
970 if (ascii_strcasecmp (a->subtype, "signed") == 0 ||
971 ascii_strcasecmp (a->subtype, "encrypted") == 0)
974 for (p = a->parts; p; p = p->next) {
975 if (mutt_can_decode (p))
980 else if (a->type == TYPEAPPLICATION) {
981 if (mutt_is_application_pgp(a))
983 if (mutt_is_application_smime (a))
990 static int multipart_handler (BODY * a, STATE * s)
998 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
999 a->encoding == ENCUUENCODED) {
1000 fstat (fileno (s->fpin), &st);
1001 b = mutt_new_body ();
1002 b->length = (long) st.st_size;
1003 b->parts = mutt_parse_multipart (s->fpin,
1004 mutt_get_parameter ("boundary",
1007 ascii_strcasecmp ("digest",
1013 for (p = b->parts, count = 1; p; p = p->next, count++) {
1014 if (s->flags & M_DISPLAY) {
1015 state_mark_attach (s);
1016 state_printf (s, _("[-- Attachment #%d"), count);
1017 if (p->description || p->filename || p->form_name) {
1018 state_puts (": ", s);
1019 state_puts (p->description ? p->description :
1020 p->filename ? p->filename : p->form_name, s);
1022 state_puts (" --]\n", s);
1024 mutt_pretty_size (length, sizeof (length), p->length);
1026 state_mark_attach (s);
1027 state_printf (s, _("[-- Type: %s/%s, Encoding: %s, Size: %s --]\n"),
1028 TYPE (p), p->subtype, ENCODING (p->encoding), length);
1029 if (!option (OPTWEED)) {
1030 fseeko (s->fpin, p->hdr_offset, 0);
1031 mutt_copy_bytes (s->fpin, s->fpout, p->offset - p->hdr_offset);
1034 state_putc ('\n', s);
1037 if (p->description && mutt_can_decode (p))
1038 state_printf (s, "Content-Description: %s\n", p->description);
1041 state_printf (s, "%s: \n", p->form_name);
1044 rc = mutt_body_handler (p, s);
1045 state_putc ('\n', s);
1046 if (rc || ((s->flags & M_REPLYING)
1047 && (option (OPTINCLUDEONLYFIRST)) && (s->flags & M_FIRSTDONE)))
1051 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1052 a->encoding == ENCUUENCODED)
1053 mutt_free_body (&b);
1058 static int autoview_handler (BODY * a, STATE * s)
1060 rfc1524_entry *entry = rfc1524_new_entry ();
1061 char buffer[LONG_STRING];
1063 char command[LONG_STRING];
1064 char tempfile[_POSIX_PATH_MAX] = "";
1073 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
1074 rfc1524_mailcap_lookup (a, type, entry, M_AUTOVIEW);
1076 fname = m_strdup(a->filename);
1077 mutt_sanitize_filename (fname, 1);
1078 rfc1524_expand_filename (entry->nametemplate, fname, tempfile,
1082 if (entry->command) {
1083 m_strcpy(command, sizeof(command), entry->command);
1085 /* rfc1524_expand_command returns 0 if the file is required */
1087 rfc1524_expand_command (a, tempfile, type, command, sizeof (command));
1089 if (s->flags & M_DISPLAY) {
1090 state_mark_attach (s);
1091 state_printf (s, _("[-- Autoview using %s --]\n"), command);
1092 mutt_message (_("Invoking autoview command: %s"), command);
1095 if ((fpin = safe_fopen (tempfile, "w+")) == NULL) {
1096 mutt_perror ("fopen");
1097 rfc1524_free_entry (&entry);
1101 mutt_copy_bytes (s->fpin, fpin, a->length);
1104 safe_fclose (&fpin);
1105 thepid = mutt_create_filter (command, NULL, &fpout, &fperr);
1111 thepid = mutt_create_filter_fd (command, NULL, &fpout, &fperr,
1112 fileno (fpin), -1, -1);
1116 mutt_perror (_("Can't create filter"));
1118 if (s->flags & M_DISPLAY) {
1119 state_mark_attach (s);
1120 state_printf (s, _("[-- Can't run %s. --]\n"), command);
1127 while (fgets (buffer, sizeof (buffer), fpout) != NULL) {
1128 state_puts (s->prefix, s);
1129 state_puts (buffer, s);
1131 /* check for data on stderr */
1132 if (fgets (buffer, sizeof (buffer), fperr)) {
1133 if (s->flags & M_DISPLAY) {
1134 state_mark_attach (s);
1135 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1138 state_puts (s->prefix, s);
1139 state_puts (buffer, s);
1140 while (fgets (buffer, sizeof (buffer), fperr) != NULL) {
1141 state_puts (s->prefix, s);
1142 state_puts (buffer, s);
1147 mutt_copy_stream (fpout, s->fpout);
1148 /* Check for stderr messages */
1149 if (fgets (buffer, sizeof (buffer), fperr)) {
1150 if (s->flags & M_DISPLAY) {
1151 state_mark_attach (s);
1152 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1155 state_puts (buffer, s);
1156 mutt_copy_stream (fperr, s->fpout);
1161 safe_fclose (&fpout);
1162 safe_fclose (&fperr);
1164 mutt_wait_filter (thepid);
1166 safe_fclose (&fpin);
1168 mutt_unlink (tempfile);
1170 if (s->flags & M_DISPLAY)
1171 mutt_clear_error ();
1173 rfc1524_free_entry (&entry);
1177 static int external_body_handler (BODY * b, STATE * s)
1179 const char *access_type;
1180 const char *expiration;
1183 access_type = mutt_get_parameter ("access-type", b->parameter);
1185 if (s->flags & M_DISPLAY) {
1186 state_mark_attach (s);
1187 state_puts (_("[-- Error: message/external-body has no access-type parameter --]\n"), s);
1192 expiration = mutt_get_parameter ("expiration", b->parameter);
1194 expire = mutt_parse_date (expiration, NULL);
1198 if (!ascii_strcasecmp (access_type, "x-mutt-deleted")) {
1199 if (s->flags & (M_DISPLAY | M_PRINTING)) {
1201 char pretty_size[10];
1203 state_mark_attach (s);
1204 state_printf (s, _("[-- This %s/%s attachment "),
1205 TYPE (b->parts), b->parts->subtype);
1206 length = mutt_get_parameter ("length", b->parameter);
1208 mutt_pretty_size (pretty_size, sizeof (pretty_size),
1209 strtol (length, NULL, 10));
1210 state_printf (s, _("(size %s bytes) "), pretty_size);
1212 state_puts (_("has been deleted --]\n"), s);
1215 state_mark_attach (s);
1216 state_printf (s, _("[-- on %s --]\n"), expiration);
1218 if (b->parts->filename) {
1219 state_mark_attach (s);
1220 state_printf (s, _("[-- name: %s --]\n"), b->parts->filename);
1223 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1224 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1228 else if (expiration && expire < time (NULL)) {
1229 if (s->flags & M_DISPLAY) {
1230 state_mark_attach (s);
1231 state_printf (s, _("[-- This %s/%s attachment is not included, --]\n"),
1232 TYPE (b->parts), b->parts->subtype);
1233 state_attach_puts (_("[-- and the indicated external source has --]\n"
1234 "[-- expired. --]\n"), s);
1236 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1237 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1242 if (s->flags & M_DISPLAY) {
1243 state_mark_attach (s);
1245 _("[-- This %s/%s attachment is not included, --]\n"),
1246 TYPE (b->parts), b->parts->subtype);
1247 state_mark_attach (s);
1250 ("[-- and the indicated access-type %s is unsupported --]\n"),
1252 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1253 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1260 void mutt_decode_attachment (BODY * b, STATE * s)
1262 int istext = mutt_is_text_part (b);
1263 iconv_t cd = (iconv_t) (-1);
1268 if (s->flags & M_CHARCONV) {
1269 const char *charset = mutt_get_parameter ("charset", b->parameter);
1271 if (!option (OPTSTRICTMIME) && !charset)
1272 charset = mutt_get_first_charset (AssumedCharset);
1273 if (charset && Charset)
1274 cd = mutt_iconv_open (Charset, charset, M_ICONV_HOOK_FROM);
1277 if (b->file_charset)
1278 cd = mutt_iconv_open (Charset, b->file_charset, M_ICONV_HOOK_FROM);
1282 fseeko (s->fpin, b->offset, 0);
1283 switch (b->encoding) {
1284 case ENCQUOTEDPRINTABLE:
1285 mutt_decode_quoted(s, b->length,
1286 istext || mutt_is_application_pgp(b), cd);
1289 mutt_decode_base64(s, b->length,
1290 istext || mutt_is_application_pgp(b), cd);
1293 mutt_decode_uuencoded(s, b->length,
1294 istext || mutt_is_application_pgp(b), cd);
1297 mutt_decode_xbit(s, b->length,
1298 istext || mutt_is_application_pgp(b), cd);
1302 if (cd != (iconv_t) (-1))
1306 int mutt_body_handler (BODY * b, STATE * s)
1311 char tempfile[_POSIX_PATH_MAX];
1312 handler_t handler = NULL;
1314 ssize_t tmplength = 0;
1318 int oflags = s->flags;
1320 /* first determine which handler to use to process this part */
1322 snprintf (type, sizeof (type), "%s/%s", TYPE (b), b->subtype);
1323 if (mutt_is_autoview (b, type)) {
1324 rfc1524_entry *entry = rfc1524_new_entry ();
1326 if (rfc1524_mailcap_lookup (b, type, entry, M_AUTOVIEW)) {
1327 handler = autoview_handler;
1328 s->flags &= ~M_CHARCONV;
1330 rfc1524_free_entry (&entry);
1332 else if (b->type == TYPETEXT) {
1333 if (ascii_strcasecmp ("plain", b->subtype) == 0) {
1334 /* avoid copying this part twice since removing the transfer-encoding is
1335 * the only operation needed.
1337 if (mutt_is_application_pgp (b))
1338 handler = crypt_pgp_application_pgp_handler;
1340 if (ascii_strcasecmp
1341 ("flowed", mutt_get_parameter ("format", b->parameter)) == 0)
1342 handler = rfc3676_handler;
1346 else if (ascii_strcasecmp ("enriched", b->subtype) == 0)
1347 handler = text_enriched_handler;
1348 else /* text body type without a handler */
1351 else if (b->type == TYPEMESSAGE) {
1352 if (mutt_is_message_type (b->type, b->subtype))
1353 handler = message_handler;
1354 else if (!ascii_strcasecmp ("delivery-status", b->subtype))
1356 else if (!ascii_strcasecmp ("external-body", b->subtype))
1357 handler = external_body_handler;
1359 else if (b->type == TYPEMULTIPART) {
1362 if (ascii_strcasecmp ("alternative", b->subtype) == 0)
1363 handler = alternative_handler;
1364 else if (ascii_strcasecmp ("signed", b->subtype) == 0) {
1365 p = mutt_get_parameter ("protocol", b->parameter);
1368 mutt_error (_("Error: multipart/signed has no protocol."));
1370 else if (s->flags & M_VERIFY)
1371 handler = mutt_signed_handler;
1373 else if (m_strcasecmp("encrypted", b->subtype) == 0) {
1374 p = mutt_get_parameter ("protocol", b->parameter);
1378 ("Error: multipart/encrypted has no protocol parameter!"));
1380 else if (ascii_strcasecmp ("application/pgp-encrypted", p) == 0)
1381 handler = crypt_pgp_encrypted_handler;
1385 handler = multipart_handler;
1387 else if (b->type == TYPEAPPLICATION) {
1388 if (mutt_is_application_pgp (b))
1389 handler = crypt_pgp_application_pgp_handler;
1390 if (mutt_is_application_smime (b))
1391 handler = crypt_smime_application_smime_handler;
1395 if (plaintext || handler) {
1396 fseeko (s->fpin, b->offset, 0);
1398 /* see if we need to decode this part before processing it */
1399 if (b->encoding == ENCBASE64 || b->encoding == ENCQUOTEDPRINTABLE || b->encoding == ENCUUENCODED || plaintext || mutt_is_text_part (b)) { /* text subtypes may
1401 * set conversion even
1402 * with 8bit encoding.
1404 int origType = b->type;
1405 char *savePrefix = NULL;
1408 /* decode to a tempfile, saving the original destination */
1410 mutt_mktemp (tempfile);
1411 if ((s->fpout = safe_fopen (tempfile, "w")) == NULL) {
1412 mutt_error _("Unable to open temporary file!");
1416 /* decoding the attachment changes the size and offset, so save a copy
1417 * of the "real" values now, and restore them after processing
1419 tmplength = b->length;
1420 tmpoffset = b->offset;
1422 /* if we are decoding binary bodies, we don't want to prefix each
1423 * line with the prefix or else the data will get corrupted.
1425 savePrefix = s->prefix;
1433 mutt_decode_attachment (b, s);
1436 b->length = ftello (s->fpout);
1440 /* restore final destination and substitute the tempfile for input */
1443 s->fpin = safe_fopen (tempfile, "r");
1446 /* restore the prefix */
1447 s->prefix = savePrefix;
1453 /* process the (decoded) body part */
1455 rc = handler (b, s);
1458 b->length = tmplength;
1459 b->offset = tmpoffset;
1461 /* restore the original source stream */
1466 s->flags |= M_FIRSTDONE;
1468 else if (s->flags & M_DISPLAY) {
1469 state_mark_attach (s);
1470 state_printf (s, _("[-- %s/%s is unsupported "), TYPE (b), b->subtype);
1471 if (!option (OPTVIEWATTACH)) {
1473 (type, sizeof (type),
1474 km_find_func (MENU_PAGER, OP_VIEW_ATTACHMENTS)))
1475 fprintf (s->fpout, _("(use '%s' to view this part)"), type);
1477 fputs (_("(need 'view-attachments' bound to key!)"), s->fpout);
1479 fputs (" --]\n", s->fpout);
1483 s->flags = oflags | (s->flags & M_FIRSTDONE);