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/lib-lib.h>
23 #include <lib-mime/mime.h>
24 #include <lib-mime/rfc3676.h>
26 #include <lib-ui/curses.h>
29 #include "recvattach.h"
34 #include <lib-crypt/crypt.h>
40 typedef int handler_f (BODY *, STATE *);
41 typedef handler_f *handler_t;
43 static void mutt_decode_xbit (STATE * s, long len, int istext, iconv_t cd)
52 while ((c = fgetc (s->fpin)) != EOF && len--) {
53 if (c == '\r' && len) {
54 if ((ch = fgetc (s->fpin)) == '\n') {
63 if (l == sizeof (bufi))
64 mutt_convert_to_state (cd, bufi, &l, s);
67 mutt_convert_to_state (cd, bufi, &l, s);
68 mutt_convert_to_state (cd, 0, 0, s);
70 state_reset_prefix (s);
73 mutt_copy_bytes (s->fpin, s->fpout, len);
76 static int qp_decode_triple (char *s, char *d)
79 if (*s == '=' && !(*(s + 1)))
82 /* quoted-printable triple */
84 isxdigit ((unsigned char) *(s + 1)) &&
85 isxdigit ((unsigned char) *(s + 2))) {
86 *d = (hexval (*(s + 1)) << 4) | hexval (*(s + 2));
94 static void qp_decode_line (char *dest, char *src, ssize_t * l, int last)
102 /* decode the line */
104 for (d = dest, s = src; *s;) {
105 switch ((kind = qp_decode_triple (s, &c))) {
109 break; /* qp triple */
112 break; /* single character */
116 break; /* soft line break */
120 if (!soft && last == '\n')
128 * Decode an attachment encoded with quoted-printable.
130 * Why doesn't this overflow any buffers? First, it's guaranteed
131 * that the length of a line grows when you _en_-code it to
132 * quoted-printable. That means that we always can store the
133 * result in a buffer of at most the _same_ size.
135 * Now, we don't special-case if the line we read with fgets()
136 * isn't terminated. We don't care about this, since STRING > 78,
137 * so corrupted input will just be corrupted a bit more. That
138 * implies that STRING+1 bytes are always sufficient to store the
139 * result of qp_decode_line.
141 * Finally, at soft line breaks, some part of a multibyte character
142 * may have been left over by mutt_convert_to_state(). This shouldn't
143 * be more than 6 characters, so STRING + 7 should be sufficient
144 * memory to store the decoded data.
146 * Just to make sure that I didn't make some off-by-one error
147 * above, we just use STRING*2 for the target buffer's size.
151 void mutt_decode_quoted (STATE * s, long len, int istext, iconv_t cd)
154 char decline[2 * STRING];
156 ssize_t linelen; /* number of input bytes in `line' */
159 int last; /* store the last character in the input line */
162 state_set_prefix (s);
168 * It's ok to use a fixed size buffer for input, even if the line turns
169 * out to be longer than this. Just process the line in chunks. This
170 * really shouldn't happen according the MIME spec, since Q-P encoded
171 * lines are at most 76 characters, but we should be liberal about what
174 if (fgets (line, MIN ((ssize_t) sizeof (line), len + 1), s->fpin) == NULL)
177 linelen = m_strlen(line);
181 * inspect the last character we read so we can tell if we got the
184 last = linelen ? line[linelen - 1] : 0;
186 /* chop trailing whitespace if we got the full line */
188 while (linelen > 0 && ISSPACE (line[linelen - 1]))
193 /* decode and do character set conversion */
194 qp_decode_line (decline + l, line, &l3, last);
196 mutt_convert_to_state (cd, decline, &l, s);
199 mutt_convert_to_state (cd, 0, 0, s);
200 state_reset_prefix (s);
203 void mutt_decode_base64 (STATE * s, long len, int istext, iconv_t cd)
206 int c1, c2, c3, c4, ch, cr = 0, i;
207 char bufi[BUFI_SIZE];
213 state_set_prefix (s);
216 for (i = 0; i < 4 && len > 0; len--) {
217 if ((ch = fgetc (s->fpin)) == EOF)
219 if (base64val(ch) >= 0 || ch == '=')
226 c1 = base64val(buf[0]);
227 c2 = base64val(buf[1]);
228 ch = (c1 << 2) | (c2 >> 4);
230 if (cr && ch != '\n')
235 if (istext && ch == '\r')
242 c3 = base64val(buf[2]);
243 ch = ((c2 & 0xf) << 4) | (c3 >> 2);
245 if (cr && ch != '\n')
250 if (istext && ch == '\r')
257 c4 = base64val(buf[3]);
258 ch = ((c3 & 0x3) << 6) | c4;
260 if (cr && ch != '\n')
264 if (istext && ch == '\r')
269 if (l + 8 >= ssizeof (bufi))
270 mutt_convert_to_state (cd, bufi, &l, s);
276 mutt_convert_to_state (cd, bufi, &l, s);
277 mutt_convert_to_state (cd, 0, 0, s);
279 state_reset_prefix (s);
282 unsigned char decode_byte (char ch)
289 void mutt_decode_uuencoded (STATE * s, long len, int istext, iconv_t cd)
291 char tmps[SHORT_STRING];
292 char linelen, c, l, out;
294 char bufi[BUFI_SIZE];
298 state_set_prefix (s);
301 if ((fgets (tmps, sizeof (tmps), s->fpin)) == NULL)
303 len -= m_strlen(tmps);
304 if ((!m_strncmp(tmps, "begin", 5)) && ISSPACE (tmps[5]))
308 if ((fgets (tmps, sizeof (tmps), s->fpin)) == NULL)
310 len -= m_strlen(tmps);
311 if (!m_strncmp(tmps, "end", 3))
314 linelen = decode_byte (*pt);
316 for (c = 0; c < linelen;) {
317 for (l = 2; l <= 6; l += 2) {
318 out = decode_byte (*pt) << l;
320 out |= (decode_byte (*pt) >> (6 - l));
326 mutt_convert_to_state (cd, bufi, &k, s);
331 mutt_convert_to_state (cd, bufi, &k, s);
332 mutt_convert_to_state (cd, 0, 0, s);
334 state_reset_prefix (s);
337 /* ----------------------------------------------------------------------------
338 * A (not so) minimal implementation of RFC1563.
341 #define IndentSize (4)
343 enum { RICH_PARAM = 0, RICH_BOLD, RICH_UNDERLINE, RICH_ITALIC, RICH_NOFILL,
344 RICH_INDENT, RICH_INDENT_RIGHT, RICH_EXCERPT, RICH_CENTER, RICH_FLUSHLEFT,
345 RICH_FLUSHRIGHT, RICH_COLOR, RICH_LAST_TAG
349 const char *tag_name;
353 "param", RICH_PARAM}, {
354 "bold", RICH_BOLD}, {
355 "italic", RICH_ITALIC}, {
356 "underline", RICH_UNDERLINE}, {
357 "nofill", RICH_NOFILL}, {
358 "excerpt", RICH_EXCERPT}, {
359 "indent", RICH_INDENT}, {
360 "indentright", RICH_INDENT_RIGHT}, {
361 "center", RICH_CENTER}, {
362 "flushleft", RICH_FLUSHLEFT}, {
363 "flushright", RICH_FLUSHRIGHT}, {
364 "flushboth", RICH_FLUSHLEFT}, {
365 "color", RICH_COLOR}, {
366 "x-color", RICH_COLOR}, {
370 struct enriched_state {
383 int tag_level[RICH_LAST_TAG];
388 static void enriched_wrap (struct enriched_state *stte)
393 if (stte->line_len) {
394 if (stte->tag_level[RICH_CENTER] || stte->tag_level[RICH_FLUSHRIGHT]) {
395 /* Strip trailing white space */
396 ssize_t y = stte->line_used - 1;
398 while (y && ISSPACE (stte->line[y])) {
399 stte->line[y] = '\0';
404 if (stte->tag_level[RICH_CENTER]) {
405 /* Strip leading whitespace */
408 while (stte->line[y] && ISSPACE (stte->line[y]))
413 for (z = y; z <= stte->line_used; z++) {
414 stte->line[z - y] = stte->line[z];
418 stte->line_used -= y;
423 extra = stte->WrapMargin - stte->line_len - stte->indent_len -
424 (stte->tag_level[RICH_INDENT_RIGHT] * IndentSize);
426 if (stte->tag_level[RICH_CENTER]) {
429 state_putc (' ', stte->s);
433 else if (stte->tag_level[RICH_FLUSHRIGHT]) {
436 state_putc (' ', stte->s);
441 state_puts (stte->line, stte->s);
444 state_putc ('\n', stte->s);
445 stte->line[0] = '\0';
448 stte->indent_len = 0;
449 if (stte->s->prefix) {
450 state_puts (stte->s->prefix, stte->s);
451 stte->indent_len += m_strlen(stte->s->prefix);
454 if (stte->tag_level[RICH_EXCERPT]) {
455 x = stte->tag_level[RICH_EXCERPT];
457 if (stte->s->prefix) {
458 state_puts (stte->s->prefix, stte->s);
459 stte->indent_len += m_strlen(stte->s->prefix);
462 state_puts ("> ", stte->s);
463 stte->indent_len += m_strlen("> ");
469 stte->indent_len = 0;
470 if (stte->tag_level[RICH_INDENT]) {
471 x = stte->tag_level[RICH_INDENT] * IndentSize;
472 stte->indent_len += x;
474 state_putc (' ', stte->s);
480 static void enriched_flush (struct enriched_state *stte, int wrap)
482 if (!stte->tag_level[RICH_NOFILL] && (stte->line_len + stte->word_len >
484 (stte->tag_level[RICH_INDENT_RIGHT] *
485 IndentSize) - stte->indent_len)))
486 enriched_wrap (stte);
488 if (stte->buff_used) {
489 stte->buffer[stte->buff_used] = '\0';
490 stte->line_used += stte->buff_used;
491 if (stte->line_used > stte->line_max) {
492 stte->line_max = stte->line_used;
493 p_realloc(&stte->line, stte->line_max + 1);
495 strcat (stte->line, stte->buffer); /* __STRCAT_CHECKED__ */
496 stte->line_len += stte->word_len;
501 enriched_wrap (stte);
505 static void enriched_putc (int c, struct enriched_state *stte)
507 if (stte->tag_level[RICH_PARAM]) {
508 if (stte->tag_level[RICH_COLOR]) {
509 if (stte->param_used + 1 >= stte->param_len)
510 p_realloc(&stte->param, (stte->param_len += STRING));
512 stte->param[stte->param_used++] = c;
514 return; /* nothing to do */
517 /* see if more space is needed (plus extra for possible rich characters) */
518 if (stte->buff_len < stte->buff_used + 3) {
519 stte->buff_len += LONG_STRING;
520 p_realloc(&stte->buffer, stte->buff_len + 1);
523 if ((!stte->tag_level[RICH_NOFILL] && ISSPACE (c)) || c == '\0') {
525 stte->word_len += 8 - (stte->line_len + stte->word_len) % 8;
529 stte->buffer[stte->buff_used++] = c;
530 enriched_flush (stte, 0);
533 if (stte->s->flags & M_DISPLAY) {
534 if (stte->tag_level[RICH_BOLD]) {
535 stte->buffer[stte->buff_used++] = c;
536 stte->buffer[stte->buff_used++] = '\010';
537 stte->buffer[stte->buff_used++] = c;
539 else if (stte->tag_level[RICH_UNDERLINE]) {
541 stte->buffer[stte->buff_used++] = '_';
542 stte->buffer[stte->buff_used++] = '\010';
543 stte->buffer[stte->buff_used++] = c;
545 else if (stte->tag_level[RICH_ITALIC]) {
546 stte->buffer[stte->buff_used++] = c;
547 stte->buffer[stte->buff_used++] = '\010';
548 stte->buffer[stte->buff_used++] = '_';
551 stte->buffer[stte->buff_used++] = c;
555 stte->buffer[stte->buff_used++] = c;
561 static void enriched_puts (const char *s, struct enriched_state *stte)
565 if (stte->buff_len < stte->buff_used + m_strlen(s)) {
566 stte->buff_len += LONG_STRING;
567 p_realloc(&stte->buffer, stte->buff_len + 1);
572 stte->buffer[stte->buff_used++] = *p++;
576 static void enriched_set_flags (const char *tag, struct enriched_state *stte)
578 const char *tagptr = tag;
584 for (i = 0, j = -1; EnrichedTags[i].tag_name; i++)
585 if (ascii_strcasecmp (EnrichedTags[i].tag_name, tagptr) == 0) {
586 j = EnrichedTags[i].index;
591 if (j == RICH_CENTER || j == RICH_FLUSHLEFT || j == RICH_FLUSHRIGHT)
592 enriched_flush (stte, 1);
595 if (stte->tag_level[j]) /* make sure not to go negative */
596 stte->tag_level[j]--;
597 if ((stte->s->flags & M_DISPLAY) && j == RICH_PARAM
598 && stte->tag_level[RICH_COLOR]) {
599 stte->param[stte->param_used] = '\0';
600 if (!ascii_strcasecmp (stte->param, "black")) {
601 enriched_puts ("\033[30m", stte);
603 else if (!ascii_strcasecmp (stte->param, "red")) {
604 enriched_puts ("\033[31m", stte);
606 else if (!ascii_strcasecmp (stte->param, "green")) {
607 enriched_puts ("\033[32m", stte);
609 else if (!ascii_strcasecmp (stte->param, "yellow")) {
610 enriched_puts ("\033[33m", stte);
612 else if (!ascii_strcasecmp (stte->param, "blue")) {
613 enriched_puts ("\033[34m", stte);
615 else if (!ascii_strcasecmp (stte->param, "magenta")) {
616 enriched_puts ("\033[35m", stte);
618 else if (!ascii_strcasecmp (stte->param, "cyan")) {
619 enriched_puts ("\033[36m", stte);
621 else if (!ascii_strcasecmp (stte->param, "white")) {
622 enriched_puts ("\033[37m", stte);
625 if ((stte->s->flags & M_DISPLAY) && j == RICH_COLOR) {
626 enriched_puts ("\033[0m", stte);
629 /* flush parameter buffer when closing the tag */
630 if (j == RICH_PARAM) {
631 stte->param_used = 0;
632 stte->param[0] = '\0';
636 stte->tag_level[j]++;
638 if (j == RICH_EXCERPT)
639 enriched_flush (stte, 1);
643 int text_enriched_handler (BODY * a, STATE * s)
646 TEXT, LANGLE, TAG, BOGUS_TAG, NEWLINE, ST_EOF, DONE
649 long bytes = a->length;
650 struct enriched_state stte;
653 char tag[LONG_STRING + 1];
658 ((s->flags & M_DISPLAY) ? (COLS - 4) : ((COLS - 4) <
659 72) ? (COLS - 4) : 72);
660 stte.line_max = stte.WrapMargin * 4;
661 stte.line = p_new(char, stte.line_max + 1);
662 stte.param = p_new(char, STRING);
664 stte.param_len = STRING;
668 state_puts (s->prefix, s);
669 stte.indent_len += m_strlen(s->prefix);
672 while (state != DONE) {
673 if (state != ST_EOF) {
674 if (!bytes || (c = fgetc (s->fpin)) == EOF)
688 if (stte.tag_level[RICH_NOFILL]) {
689 enriched_flush (&stte, 1);
692 enriched_putc (' ', &stte);
698 enriched_putc (c, &stte);
704 enriched_putc (c, &stte);
712 /* Yes, fall through (it wasn't a <<, so this char is first in TAG) */
716 enriched_set_flags (tag, &stte);
719 else if (tag_len < LONG_STRING) /* ignore overly long tags */
732 enriched_flush (&stte, 1);
741 enriched_putc ('\0', &stte);
742 enriched_flush (&stte, 1);
746 case DONE: /* not reached, but gcc complains if this is absent */
751 state_putc ('\n', s); /* add a final newline */
753 p_delete(&(stte.buffer));
754 p_delete(&(stte.line));
755 p_delete(&(stte.param));
762 #define TXTENRICHED 3
764 static int alternative_handler (BODY * a, STATE * s)
774 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
775 a->encoding == ENCUUENCODED) {
779 fstat (fileno (s->fpin), &st);
781 b->length = (long) st.st_size;
782 b->parts = mutt_parse_multipart(s->fpin,
783 parameter_getval(a->parameter, "boundary"),
785 !ascii_strcasecmp("digest", a->subtype));
792 /* First, search list of prefered types */
793 t = AlternativeOrderList;
794 while (t && !choice) {
796 int btlen; /* length of basetype */
797 int wild; /* do we have a wildcard to match all subtypes? */
799 c = strchr (t->data, '/');
801 wild = (c[1] == '*' && c[2] == 0);
806 btlen = m_strlen(t->data);
814 const char *bt = TYPE (b);
816 if (!ascii_strncasecmp (bt, t->data, btlen) && bt[btlen] == 0) {
817 /* the basetype matches */
818 if (wild || !ascii_strcasecmp (t->data + btlen + 1, b->subtype)) {
827 /* Next, look for an autoviewable type */
834 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
835 if (mutt_is_autoview (b, buf)) {
836 rfc1524_entry *entry = rfc1524_entry_new();
838 if (rfc1524_mailcap_lookup (b, buf, entry, M_AUTOVIEW)) {
841 rfc1524_entry_delete(&entry);
847 /* Then, look for a text entry */
854 if (b->type == TYPETEXT) {
855 if (!ascii_strcasecmp ("plain", b->subtype) && type <= TXTPLAIN) {
859 else if (!ascii_strcasecmp ("enriched", b->subtype)
860 && type <= TXTENRICHED) {
864 else if (!ascii_strcasecmp ("html", b->subtype) && type <= TXTHTML) {
873 /* Finally, look for other possibilities */
880 if (mutt_can_decode (b))
887 if (s->flags & M_DISPLAY && !option (OPTWEED)) {
888 fseeko (s->fpin, choice->hdr_offset, 0);
889 mutt_copy_bytes (s->fpin, s->fpout,
890 choice->offset - choice->hdr_offset);
892 mutt_body_handler (choice, s);
894 else if (s->flags & M_DISPLAY) {
895 /* didn't find anything that we could display! */
896 state_mark_attach (s);
897 state_puts (_("[-- Error: Could not display any parts of Multipart/Alternative! --]\n"), s);
907 /* handles message/rfc822 body parts */
908 static int message_handler (BODY * a, STATE * s)
915 off_start = ftello (s->fpin);
916 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
917 a->encoding == ENCUUENCODED) {
918 fstat (fileno (s->fpin), &st);
920 b->length = (long) st.st_size;
921 b->parts = mutt_parse_messageRFC822 (s->fpin, b);
927 mutt_copy_hdr (s->fpin, s->fpout, off_start, b->parts->offset,
928 (((s->flags & M_WEED)
929 || ((s->flags & (M_DISPLAY | M_PRINTING))
930 && option (OPTWEED))) ? (CH_WEED | CH_REORDER) : 0) |
931 (s->prefix ? CH_PREFIX : 0) | CH_DECODE | CH_FROM,
935 state_puts (s->prefix, s);
936 state_putc ('\n', s);
938 rc = mutt_body_handler (b->parts, s);
941 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
942 a->encoding == ENCUUENCODED)
948 /* returns 1 if decoding the attachment will produce output */
949 int mutt_can_decode (BODY * a)
953 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
954 if (mutt_is_autoview (a, type))
955 return (rfc1524_mailcap_lookup (a, type, NULL, M_AUTOVIEW));
956 else if (a->type == TYPETEXT)
958 else if (a->type == TYPEMESSAGE)
960 else if (a->type == TYPEMULTIPART) {
963 if (ascii_strcasecmp (a->subtype, "signed") == 0 ||
964 ascii_strcasecmp (a->subtype, "encrypted") == 0)
967 for (p = a->parts; p; p = p->next) {
968 if (mutt_can_decode (p))
973 else if (a->type == TYPEAPPLICATION) {
974 if (mutt_is_application_pgp(a))
976 if (mutt_is_application_smime (a))
983 static int multipart_handler (BODY * a, STATE * s)
991 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
992 a->encoding == ENCUUENCODED) {
993 fstat (fileno (s->fpin), &st);
995 b->length = (long) st.st_size;
996 b->parts = mutt_parse_multipart(s->fpin,
997 parameter_getval(a->parameter, "boundary"),
999 !ascii_strcasecmp("digest", a->subtype));
1004 for (p = b->parts, count = 1; p; p = p->next, count++) {
1005 if (s->flags & M_DISPLAY) {
1006 state_mark_attach (s);
1007 state_printf (s, _("[-- Attachment #%d"), count);
1008 if (p->description || p->filename || p->form_name) {
1009 state_puts (": ", s);
1010 state_puts (p->description ? p->description :
1011 p->filename ? p->filename : p->form_name, s);
1013 state_puts (" --]\n", s);
1015 mutt_pretty_size (length, sizeof (length), p->length);
1017 state_mark_attach (s);
1018 state_printf (s, _("[-- Type: %s/%s, Encoding: %s, Size: %s --]\n"),
1019 TYPE (p), p->subtype, ENCODING (p->encoding), length);
1020 if (!option (OPTWEED)) {
1021 fseeko (s->fpin, p->hdr_offset, 0);
1022 mutt_copy_bytes (s->fpin, s->fpout, p->offset - p->hdr_offset);
1025 state_putc ('\n', s);
1028 if (p->description && mutt_can_decode (p))
1029 state_printf (s, "Content-Description: %s\n", p->description);
1032 state_printf (s, "%s: \n", p->form_name);
1035 rc = mutt_body_handler (p, s);
1036 state_putc ('\n', s);
1037 if (rc || ((s->flags & M_REPLYING)
1038 && (option (OPTINCLUDEONLYFIRST)) && (s->flags & M_FIRSTDONE)))
1042 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1043 a->encoding == ENCUUENCODED)
1049 static int autoview_handler (BODY * a, STATE * s)
1051 rfc1524_entry *entry = rfc1524_entry_new();
1052 char buffer[LONG_STRING];
1054 char command[LONG_STRING];
1055 char tempfile[_POSIX_PATH_MAX] = "";
1064 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
1065 rfc1524_mailcap_lookup (a, type, entry, M_AUTOVIEW);
1067 fname = m_strdup(a->filename);
1068 mutt_sanitize_filename (fname, 1);
1069 rfc1524_expand_filename (entry->nametemplate, fname, tempfile,
1073 if (entry->command) {
1074 m_strcpy(command, sizeof(command), entry->command);
1076 /* rfc1524_expand_command returns 0 if the file is required */
1078 rfc1524_expand_command (a, tempfile, type, command, sizeof (command));
1080 if (s->flags & M_DISPLAY) {
1081 state_mark_attach (s);
1082 state_printf (s, _("[-- Autoview using %s --]\n"), command);
1083 mutt_message (_("Invoking autoview command: %s"), command);
1086 if ((fpin = safe_fopen (tempfile, "w+")) == NULL) {
1087 mutt_perror ("fopen");
1088 rfc1524_entry_delete(&entry);
1092 mutt_copy_bytes (s->fpin, fpin, a->length);
1095 safe_fclose (&fpin);
1096 thepid = mutt_create_filter (command, NULL, &fpout, &fperr);
1102 thepid = mutt_create_filter_fd (command, NULL, &fpout, &fperr,
1103 fileno (fpin), -1, -1);
1107 mutt_perror (_("Can't create filter"));
1109 if (s->flags & M_DISPLAY) {
1110 state_mark_attach (s);
1111 state_printf (s, _("[-- Can't run %s. --]\n"), command);
1118 while (fgets (buffer, sizeof (buffer), fpout) != NULL) {
1119 state_puts (s->prefix, s);
1120 state_puts (buffer, s);
1122 /* check for data on stderr */
1123 if (fgets (buffer, sizeof (buffer), fperr)) {
1124 if (s->flags & M_DISPLAY) {
1125 state_mark_attach (s);
1126 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1129 state_puts (s->prefix, s);
1130 state_puts (buffer, s);
1131 while (fgets (buffer, sizeof (buffer), fperr) != NULL) {
1132 state_puts (s->prefix, s);
1133 state_puts (buffer, s);
1138 mutt_copy_stream (fpout, s->fpout);
1139 /* Check for stderr messages */
1140 if (fgets (buffer, sizeof (buffer), fperr)) {
1141 if (s->flags & M_DISPLAY) {
1142 state_mark_attach (s);
1143 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1146 state_puts (buffer, s);
1147 mutt_copy_stream (fperr, s->fpout);
1152 safe_fclose (&fpout);
1153 safe_fclose (&fperr);
1155 mutt_wait_filter (thepid);
1157 safe_fclose (&fpin);
1159 mutt_unlink (tempfile);
1161 if (s->flags & M_DISPLAY)
1162 mutt_clear_error ();
1164 rfc1524_entry_delete(&entry);
1168 static int external_body_handler (BODY * b, STATE * s)
1170 const char *access_type;
1171 const char *expiration;
1174 access_type = parameter_getval(b->parameter, "access-type");
1176 if (s->flags & M_DISPLAY) {
1177 state_mark_attach (s);
1178 state_puts (_("[-- Error: message/external-body has no access-type parameter --]\n"), s);
1183 expiration = parameter_getval(b->parameter, "expiration");
1185 expire = mutt_parse_date (expiration, NULL);
1189 if (!ascii_strcasecmp (access_type, "x-mutt-deleted")) {
1190 if (s->flags & (M_DISPLAY | M_PRINTING)) {
1192 char pretty_size[10];
1194 state_mark_attach (s);
1195 state_printf (s, _("[-- This %s/%s attachment "),
1196 TYPE (b->parts), b->parts->subtype);
1197 length = parameter_getval(b->parameter, "length");
1199 mutt_pretty_size (pretty_size, sizeof (pretty_size),
1200 strtol (length, NULL, 10));
1201 state_printf (s, _("(size %s bytes) "), pretty_size);
1203 state_puts (_("has been deleted --]\n"), s);
1206 state_mark_attach (s);
1207 state_printf (s, _("[-- on %s --]\n"), expiration);
1209 if (b->parts->filename) {
1210 state_mark_attach (s);
1211 state_printf (s, _("[-- name: %s --]\n"), b->parts->filename);
1214 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1215 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1219 else if (expiration && expire < time (NULL)) {
1220 if (s->flags & M_DISPLAY) {
1221 state_mark_attach (s);
1222 state_printf (s, _("[-- This %s/%s attachment is not included, --]\n"),
1223 TYPE (b->parts), b->parts->subtype);
1224 state_attach_puts (_("[-- and the indicated external source has --]\n"
1225 "[-- expired. --]\n"), s);
1227 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1228 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1233 if (s->flags & M_DISPLAY) {
1234 state_mark_attach (s);
1236 _("[-- This %s/%s attachment is not included, --]\n"),
1237 TYPE (b->parts), b->parts->subtype);
1238 state_mark_attach (s);
1241 ("[-- and the indicated access-type %s is unsupported --]\n"),
1243 mutt_copy_hdr (s->fpin, s->fpout, ftello (s->fpin), b->parts->offset,
1244 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1251 void mutt_decode_attachment (BODY * b, STATE * s)
1253 int istext = mutt_is_text_part (b);
1254 iconv_t cd = MUTT_ICONV_ERROR;
1259 if (s->flags & M_CHARCONV) {
1260 const char *charset = parameter_getval(b->parameter, "charset");
1262 if (!option (OPTSTRICTMIME) && !charset)
1263 charset = charset_getfirst(AssumedCharset);
1264 if (charset && Charset)
1265 cd = mutt_iconv_open (Charset, charset, M_ICONV_HOOK_FROM);
1268 if (b->file_charset)
1269 cd = mutt_iconv_open (Charset, b->file_charset, M_ICONV_HOOK_FROM);
1273 fseeko (s->fpin, b->offset, 0);
1274 switch (b->encoding) {
1275 case ENCQUOTEDPRINTABLE:
1276 mutt_decode_quoted(s, b->length,
1277 istext || mutt_is_application_pgp(b), cd);
1280 mutt_decode_base64(s, b->length,
1281 istext || mutt_is_application_pgp(b), cd);
1284 mutt_decode_uuencoded(s, b->length,
1285 istext || mutt_is_application_pgp(b), cd);
1288 mutt_decode_xbit(s, b->length,
1289 istext || mutt_is_application_pgp(b), cd);
1293 if (cd != MUTT_ICONV_ERROR)
1297 int mutt_body_handler (BODY * b, STATE * s)
1302 char tempfile[_POSIX_PATH_MAX];
1303 handler_t handler = NULL;
1305 ssize_t tmplength = 0;
1309 int oflags = s->flags;
1311 /* first determine which handler to use to process this part */
1313 snprintf (type, sizeof (type), "%s/%s", TYPE (b), b->subtype);
1314 if (mutt_is_autoview (b, type)) {
1315 rfc1524_entry *entry = rfc1524_entry_new();
1317 if (rfc1524_mailcap_lookup (b, type, entry, M_AUTOVIEW)) {
1318 handler = autoview_handler;
1319 s->flags &= ~M_CHARCONV;
1321 rfc1524_entry_delete(&entry);
1323 else if (b->type == TYPETEXT) {
1324 if (ascii_strcasecmp ("plain", b->subtype) == 0) {
1325 /* avoid copying this part twice since removing the transfer-encoding is
1326 * the only operation needed.
1328 if (mutt_is_application_pgp (b))
1329 handler = crypt_pgp_application_pgp_handler;
1331 if (!ascii_strcasecmp("flowed", parameter_getval(b->parameter, "format")))
1332 handler = rfc3676_handler;
1336 else if (ascii_strcasecmp ("enriched", b->subtype) == 0)
1337 handler = text_enriched_handler;
1338 else /* text body type without a handler */
1341 else if (b->type == TYPEMESSAGE) {
1342 if (mutt_is_message_type (b->type, b->subtype))
1343 handler = message_handler;
1344 else if (!ascii_strcasecmp ("delivery-status", b->subtype))
1346 else if (!ascii_strcasecmp ("external-body", b->subtype))
1347 handler = external_body_handler;
1349 else if (b->type == TYPEMULTIPART) {
1352 if (ascii_strcasecmp ("alternative", b->subtype) == 0)
1353 handler = alternative_handler;
1354 else if (ascii_strcasecmp ("signed", b->subtype) == 0) {
1355 p = parameter_getval(b->parameter, "protocol");
1358 mutt_error (_("Error: multipart/signed has no protocol."));
1360 else if (s->flags & M_VERIFY)
1361 handler = mutt_signed_handler;
1363 else if (m_strcasecmp("encrypted", b->subtype) == 0) {
1364 p = parameter_getval(b->parameter, "protocol");
1368 ("Error: multipart/encrypted has no protocol parameter!"));
1370 else if (ascii_strcasecmp ("application/pgp-encrypted", p) == 0)
1371 handler = crypt_pgp_encrypted_handler;
1375 handler = multipart_handler;
1377 else if (b->type == TYPEAPPLICATION) {
1378 if (mutt_is_application_pgp (b))
1379 handler = crypt_pgp_application_pgp_handler;
1380 if (mutt_is_application_smime (b))
1381 handler = crypt_smime_application_smime_handler;
1385 if (plaintext || handler) {
1386 fseeko (s->fpin, b->offset, 0);
1388 /* see if we need to decode this part before processing it */
1389 if (b->encoding == ENCBASE64 || b->encoding == ENCQUOTEDPRINTABLE || b->encoding == ENCUUENCODED || plaintext || mutt_is_text_part (b)) { /* text subtypes may
1391 * set conversion even
1392 * with 8bit encoding.
1394 int origType = b->type;
1395 char *savePrefix = NULL;
1398 /* decode to a tempfile, saving the original destination */
1400 mutt_mktemp (tempfile);
1401 if ((s->fpout = safe_fopen (tempfile, "w")) == NULL) {
1402 mutt_error _("Unable to open temporary file!");
1406 /* decoding the attachment changes the size and offset, so save a copy
1407 * of the "real" values now, and restore them after processing
1409 tmplength = b->length;
1410 tmpoffset = b->offset;
1412 /* if we are decoding binary bodies, we don't want to prefix each
1413 * line with the prefix or else the data will get corrupted.
1415 savePrefix = s->prefix;
1423 mutt_decode_attachment (b, s);
1426 b->length = ftello (s->fpout);
1430 /* restore final destination and substitute the tempfile for input */
1433 s->fpin = safe_fopen (tempfile, "r");
1436 /* restore the prefix */
1437 s->prefix = savePrefix;
1443 /* process the (decoded) body part */
1445 rc = handler (b, s);
1448 b->length = tmplength;
1449 b->offset = tmpoffset;
1451 /* restore the original source stream */
1456 s->flags |= M_FIRSTDONE;
1458 else if (s->flags & M_DISPLAY) {
1459 state_mark_attach (s);
1460 state_printf (s, _("[-- %s/%s is unsupported "), TYPE (b), b->subtype);
1461 if (!option (OPTVIEWATTACH)) {
1463 (type, sizeof (type),
1464 km_find_func (MENU_PAGER, OP_VIEW_ATTACHMENTS)))
1465 fprintf (s->fpout, _("(use '%s' to view this part)"), type);
1467 fputs (_("(need 'view-attachments' bound to key!)"), s->fpout);
1469 fputs (" --]\n", s->fpout);
1473 s->flags = oflags | (s->flags & M_FIRSTDONE);