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"
31 #include "mutt_crypt.h"
39 #include "lib/debug.h"
41 typedef int handler_f (BODY *, STATE *);
42 typedef handler_f *handler_t;
44 int Index_hex[128] = {
45 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
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 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
49 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
50 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
51 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
52 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
56 -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, 62, -1, -1, -1, 63,
59 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
60 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
61 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
62 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
63 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1
66 void mutt_decode_xbit (STATE * s, long len, int istext, iconv_t cd)
75 while ((c = fgetc (s->fpin)) != EOF && len--) {
76 if (c == '\r' && len) {
77 if ((ch = fgetc (s->fpin)) == '\n') {
86 if (l == sizeof (bufi))
87 mutt_convert_to_state (cd, bufi, &l, s);
90 mutt_convert_to_state (cd, bufi, &l, s);
91 mutt_convert_to_state (cd, 0, 0, s);
93 state_reset_prefix (s);
96 mutt_copy_bytes (s->fpin, s->fpout, len);
99 static int qp_decode_triple (char *s, char *d)
101 /* soft line break */
102 if (*s == '=' && !(*(s + 1)))
105 /* quoted-printable triple */
107 isxdigit ((unsigned char) *(s + 1)) &&
108 isxdigit ((unsigned char) *(s + 2))) {
109 *d = (hexval (*(s + 1)) << 4) | hexval (*(s + 2));
117 static void qp_decode_line (char *dest, char *src, size_t * l, int last)
125 /* decode the line */
127 for (d = dest, s = src; *s;) {
128 switch ((kind = qp_decode_triple (s, &c))) {
132 break; /* qp triple */
135 break; /* single character */
139 break; /* soft line break */
143 if (!soft && last == '\n')
151 * Decode an attachment encoded with quoted-printable.
153 * Why doesn't this overflow any buffers? First, it's guaranteed
154 * that the length of a line grows when you _en_-code it to
155 * quoted-printable. That means that we always can store the
156 * result in a buffer of at most the _same_ size.
158 * Now, we don't special-case if the line we read with fgets()
159 * isn't terminated. We don't care about this, since STRING > 78,
160 * so corrupted input will just be corrupted a bit more. That
161 * implies that STRING+1 bytes are always sufficient to store the
162 * result of qp_decode_line.
164 * Finally, at soft line breaks, some part of a multibyte character
165 * may have been left over by mutt_convert_to_state(). This shouldn't
166 * be more than 6 characters, so STRING + 7 should be sufficient
167 * memory to store the decoded data.
169 * Just to make sure that I didn't make some off-by-one error
170 * above, we just use STRING*2 for the target buffer's size.
174 void mutt_decode_quoted (STATE * s, long len, int istext, iconv_t cd)
177 char decline[2 * STRING];
179 size_t linelen; /* number of input bytes in `line' */
182 int last; /* store the last character in the input line */
185 state_set_prefix (s);
191 * It's ok to use a fixed size buffer for input, even if the line turns
192 * out to be longer than this. Just process the line in chunks. This
193 * really shouldn't happen according the MIME spec, since Q-P encoded
194 * lines are at most 76 characters, but we should be liberal about what
197 if (fgets (line, MIN ((ssize_t) sizeof (line), len + 1), s->fpin) == NULL)
200 linelen = str_len (line);
204 * inspect the last character we read so we can tell if we got the
207 last = linelen ? line[linelen - 1] : 0;
209 /* chop trailing whitespace if we got the full line */
211 while (linelen > 0 && ISSPACE (line[linelen - 1]))
216 /* decode and do character set conversion */
217 qp_decode_line (decline + l, line, &l3, last);
219 mutt_convert_to_state (cd, decline, &l, s);
222 mutt_convert_to_state (cd, 0, 0, s);
223 state_reset_prefix (s);
226 void mutt_decode_base64 (STATE * s, long len, int istext, iconv_t cd)
229 int c1, c2, c3, c4, ch, cr = 0, i;
230 char bufi[BUFI_SIZE];
236 state_set_prefix (s);
239 for (i = 0; i < 4 && len > 0; len--) {
240 if ((ch = fgetc (s->fpin)) == EOF)
242 if (ch >= 0 && ch < 128 && (base64val (ch) != -1 || ch == '='))
246 debug_print (2, ("didn't get a multiple of 4 chars.\n"));
250 c1 = base64val (buf[0]);
251 c2 = base64val (buf[1]);
252 ch = (c1 << 2) | (c2 >> 4);
254 if (cr && ch != '\n')
259 if (istext && ch == '\r')
266 c3 = base64val (buf[2]);
267 ch = ((c2 & 0xf) << 4) | (c3 >> 2);
269 if (cr && ch != '\n')
274 if (istext && ch == '\r')
281 c4 = base64val (buf[3]);
282 ch = ((c3 & 0x3) << 6) | c4;
284 if (cr && ch != '\n')
288 if (istext && ch == '\r')
293 if (l + 8 >= sizeof (bufi))
294 mutt_convert_to_state (cd, bufi, &l, s);
300 mutt_convert_to_state (cd, bufi, &l, s);
301 mutt_convert_to_state (cd, 0, 0, s);
303 state_reset_prefix (s);
306 unsigned char decode_byte (char ch)
313 void mutt_decode_uuencoded (STATE * s, long len, int istext, iconv_t cd)
315 char tmps[SHORT_STRING];
316 char linelen, c, l, out;
318 char bufi[BUFI_SIZE];
322 state_set_prefix (s);
325 if ((fgets (tmps, sizeof (tmps), s->fpin)) == NULL)
327 len -= str_len (tmps);
328 if ((!str_ncmp (tmps, "begin", 5)) && ISSPACE (tmps[5]))
332 if ((fgets (tmps, sizeof (tmps), s->fpin)) == NULL)
334 len -= str_len (tmps);
335 if (!str_ncmp (tmps, "end", 3))
338 linelen = decode_byte (*pt);
340 for (c = 0; c < linelen;) {
341 for (l = 2; l <= 6; l += 2) {
342 out = decode_byte (*pt) << l;
344 out |= (decode_byte (*pt) >> (6 - l));
350 mutt_convert_to_state (cd, bufi, &k, s);
355 mutt_convert_to_state (cd, bufi, &k, s);
356 mutt_convert_to_state (cd, 0, 0, s);
358 state_reset_prefix (s);
361 /* ----------------------------------------------------------------------------
362 * A (not so) minimal implementation of RFC1563.
365 #define IndentSize (4)
367 enum { RICH_PARAM = 0, RICH_BOLD, RICH_UNDERLINE, RICH_ITALIC, RICH_NOFILL,
368 RICH_INDENT, RICH_INDENT_RIGHT, RICH_EXCERPT, RICH_CENTER, RICH_FLUSHLEFT,
369 RICH_FLUSHRIGHT, RICH_COLOR, RICH_LAST_TAG
373 const char *tag_name;
377 "param", RICH_PARAM}, {
378 "bold", RICH_BOLD}, {
379 "italic", RICH_ITALIC}, {
380 "underline", RICH_UNDERLINE}, {
381 "nofill", RICH_NOFILL}, {
382 "excerpt", RICH_EXCERPT}, {
383 "indent", RICH_INDENT}, {
384 "indentright", RICH_INDENT_RIGHT}, {
385 "center", RICH_CENTER}, {
386 "flushleft", RICH_FLUSHLEFT}, {
387 "flushright", RICH_FLUSHRIGHT}, {
388 "flushboth", RICH_FLUSHLEFT}, {
389 "color", RICH_COLOR}, {
390 "x-color", RICH_COLOR}, {
394 struct enriched_state {
407 int tag_level[RICH_LAST_TAG];
412 static void enriched_wrap (struct enriched_state *stte)
417 if (stte->line_len) {
418 if (stte->tag_level[RICH_CENTER] || stte->tag_level[RICH_FLUSHRIGHT]) {
419 /* Strip trailing white space */
420 size_t y = stte->line_used - 1;
422 while (y && ISSPACE (stte->line[y])) {
423 stte->line[y] = '\0';
428 if (stte->tag_level[RICH_CENTER]) {
429 /* Strip leading whitespace */
432 while (stte->line[y] && ISSPACE (stte->line[y]))
437 for (z = y; z <= stte->line_used; z++) {
438 stte->line[z - y] = stte->line[z];
442 stte->line_used -= y;
447 extra = stte->WrapMargin - stte->line_len - stte->indent_len -
448 (stte->tag_level[RICH_INDENT_RIGHT] * IndentSize);
450 if (stte->tag_level[RICH_CENTER]) {
453 state_putc (' ', stte->s);
457 else if (stte->tag_level[RICH_FLUSHRIGHT]) {
460 state_putc (' ', stte->s);
465 state_puts (stte->line, stte->s);
468 state_putc ('\n', stte->s);
469 stte->line[0] = '\0';
472 stte->indent_len = 0;
473 if (stte->s->prefix) {
474 state_puts (stte->s->prefix, stte->s);
475 stte->indent_len += str_len (stte->s->prefix);
478 if (stte->tag_level[RICH_EXCERPT]) {
479 x = stte->tag_level[RICH_EXCERPT];
481 if (stte->s->prefix) {
482 state_puts (stte->s->prefix, stte->s);
483 stte->indent_len += str_len (stte->s->prefix);
486 state_puts ("> ", stte->s);
487 stte->indent_len += str_len ("> ");
493 stte->indent_len = 0;
494 if (stte->tag_level[RICH_INDENT]) {
495 x = stte->tag_level[RICH_INDENT] * IndentSize;
496 stte->indent_len += x;
498 state_putc (' ', stte->s);
504 static void enriched_flush (struct enriched_state *stte, int wrap)
506 if (!stte->tag_level[RICH_NOFILL] && (stte->line_len + stte->word_len >
508 (stte->tag_level[RICH_INDENT_RIGHT] *
509 IndentSize) - stte->indent_len)))
510 enriched_wrap (stte);
512 if (stte->buff_used) {
513 stte->buffer[stte->buff_used] = '\0';
514 stte->line_used += stte->buff_used;
515 if (stte->line_used > stte->line_max) {
516 stte->line_max = stte->line_used;
517 mem_realloc (&stte->line, stte->line_max + 1);
519 strcat (stte->line, stte->buffer); /* __STRCAT_CHECKED__ */
520 stte->line_len += stte->word_len;
525 enriched_wrap (stte);
529 static void enriched_putc (int c, struct enriched_state *stte)
531 if (stte->tag_level[RICH_PARAM]) {
532 if (stte->tag_level[RICH_COLOR]) {
533 if (stte->param_used + 1 >= stte->param_len)
534 mem_realloc (&stte->param, (stte->param_len += STRING));
536 stte->param[stte->param_used++] = c;
538 return; /* nothing to do */
541 /* see if more space is needed (plus extra for possible rich characters) */
542 if (stte->buff_len < stte->buff_used + 3) {
543 stte->buff_len += LONG_STRING;
544 mem_realloc (&stte->buffer, stte->buff_len + 1);
547 if ((!stte->tag_level[RICH_NOFILL] && ISSPACE (c)) || c == '\0') {
549 stte->word_len += 8 - (stte->line_len + stte->word_len) % 8;
553 stte->buffer[stte->buff_used++] = c;
554 enriched_flush (stte, 0);
557 if (stte->s->flags & M_DISPLAY) {
558 if (stte->tag_level[RICH_BOLD]) {
559 stte->buffer[stte->buff_used++] = c;
560 stte->buffer[stte->buff_used++] = '\010';
561 stte->buffer[stte->buff_used++] = c;
563 else if (stte->tag_level[RICH_UNDERLINE]) {
565 stte->buffer[stte->buff_used++] = '_';
566 stte->buffer[stte->buff_used++] = '\010';
567 stte->buffer[stte->buff_used++] = c;
569 else if (stte->tag_level[RICH_ITALIC]) {
570 stte->buffer[stte->buff_used++] = c;
571 stte->buffer[stte->buff_used++] = '\010';
572 stte->buffer[stte->buff_used++] = '_';
575 stte->buffer[stte->buff_used++] = c;
579 stte->buffer[stte->buff_used++] = c;
585 static void enriched_puts (char *s, struct enriched_state *stte)
589 if (stte->buff_len < stte->buff_used + str_len (s)) {
590 stte->buff_len += LONG_STRING;
591 mem_realloc (&stte->buffer, stte->buff_len + 1);
595 stte->buffer[stte->buff_used++] = *c;
600 static void enriched_set_flags (const char *tag, struct enriched_state *stte)
602 const char *tagptr = tag;
608 for (i = 0, j = -1; EnrichedTags[i].tag_name; i++)
609 if (ascii_strcasecmp (EnrichedTags[i].tag_name, tagptr) == 0) {
610 j = EnrichedTags[i].index;
615 if (j == RICH_CENTER || j == RICH_FLUSHLEFT || j == RICH_FLUSHRIGHT)
616 enriched_flush (stte, 1);
619 if (stte->tag_level[j]) /* make sure not to go negative */
620 stte->tag_level[j]--;
621 if ((stte->s->flags & M_DISPLAY) && j == RICH_PARAM
622 && stte->tag_level[RICH_COLOR]) {
623 stte->param[stte->param_used] = '\0';
624 if (!ascii_strcasecmp (stte->param, "black")) {
625 enriched_puts ("\033[30m", stte);
627 else if (!ascii_strcasecmp (stte->param, "red")) {
628 enriched_puts ("\033[31m", stte);
630 else if (!ascii_strcasecmp (stte->param, "green")) {
631 enriched_puts ("\033[32m", stte);
633 else if (!ascii_strcasecmp (stte->param, "yellow")) {
634 enriched_puts ("\033[33m", stte);
636 else if (!ascii_strcasecmp (stte->param, "blue")) {
637 enriched_puts ("\033[34m", stte);
639 else if (!ascii_strcasecmp (stte->param, "magenta")) {
640 enriched_puts ("\033[35m", stte);
642 else if (!ascii_strcasecmp (stte->param, "cyan")) {
643 enriched_puts ("\033[36m", stte);
645 else if (!ascii_strcasecmp (stte->param, "white")) {
646 enriched_puts ("\033[37m", stte);
649 if ((stte->s->flags & M_DISPLAY) && j == RICH_COLOR) {
650 enriched_puts ("\033[0m", stte);
653 /* flush parameter buffer when closing the tag */
654 if (j == RICH_PARAM) {
655 stte->param_used = 0;
656 stte->param[0] = '\0';
660 stte->tag_level[j]++;
662 if (j == RICH_EXCERPT)
663 enriched_flush (stte, 1);
667 int text_enriched_handler (BODY * a, STATE * s)
670 TEXT, LANGLE, TAG, BOGUS_TAG, NEWLINE, ST_EOF, DONE
673 long bytes = a->length;
674 struct enriched_state stte;
677 char tag[LONG_STRING + 1];
679 memset (&stte, 0, sizeof (stte));
682 ((s->flags & M_DISPLAY) ? (COLS - 4) : ((COLS - 4) <
683 72) ? (COLS - 4) : 72);
684 stte.line_max = stte.WrapMargin * 4;
685 stte.line = (char *) mem_calloc (1, stte.line_max + 1);
686 stte.param = (char *) mem_calloc (1, STRING);
688 stte.param_len = STRING;
692 state_puts (s->prefix, s);
693 stte.indent_len += str_len (s->prefix);
696 while (state != DONE) {
697 if (state != ST_EOF) {
698 if (!bytes || (c = fgetc (s->fpin)) == EOF)
712 if (stte.tag_level[RICH_NOFILL]) {
713 enriched_flush (&stte, 1);
716 enriched_putc (' ', &stte);
722 enriched_putc (c, &stte);
728 enriched_putc (c, &stte);
736 /* Yes, fall through (it wasn't a <<, so this char is first in TAG) */
740 enriched_set_flags (tag, &stte);
743 else if (tag_len < LONG_STRING) /* ignore overly long tags */
756 enriched_flush (&stte, 1);
765 enriched_putc ('\0', &stte);
766 enriched_flush (&stte, 1);
770 case DONE: /* not reached, but gcc complains if this is absent */
775 state_putc ('\n', s); /* add a final newline */
777 mem_free (&(stte.buffer));
778 mem_free (&(stte.line));
779 mem_free (&(stte.param));
785 * An implementation of RFC 2646.
787 * NOTE: This still has to be made UTF-8 aware.
791 #define FLOWED_MAX 77
793 static int get_quote_level (char *line)
797 for (quoted = 0; line[quoted] == '>'; quoted++);
801 static void print_flowed_line (char *line, STATE * s, int ql)
805 int len = str_len (line);
808 if (MaxLineLength > 0) {
809 width = MaxLineLength - WrapMargin - ql - 1;
810 if (option (OPTSTUFFQUOTED))
813 width = MaxLineLength;
816 if (option (OPTMBOXPANE))
817 width = COLS - SidebarWidth - WrapMargin - ql - 1;
819 width = COLS - WrapMargin - ql - 1;
821 if (option (OPTSTUFFQUOTED))
827 /* fprintf(stderr,"print_flowed_line will print `%s' with ql = %d\n",line,ql); */
829 if (str_len (line) == 0) {
830 if (option (OPTQUOTEEMPTY)) {
832 state_puts(s->prefix,s);
833 for (i=0;i<ql;++i) state_putc('>',s);
834 if (option(OPTSTUFFQUOTED))
844 /* fprintf(stderr,"oldpos = %p line+len = %p\n",oldpos,line+len); */
846 for (; oldpos < line + len; pos += width) {
847 /* fprintf(stderr,"outer for loop\n"); */
848 if (pos < line + len) { /* only search a new position when we're not over the end of the string w/ pos */
849 /* fprintf(stderr,"if 1\n"); */
851 /* fprintf(stderr,"if 2: good luck! found a space\n"); */
856 /* fprintf(stderr,"if 2: else\n"); */
859 while (pos >= oldpos && !isspace (*pos)) {
860 /* fprintf(stderr,"pos(%p) > oldpos(%p)\n",pos,oldpos); */
864 /* fprintf(stderr,"wow, no space found, searching the other direction\n"); */
866 while (pos < line + len && *pos && !isspace (*pos)) {
867 /* fprintf(stderr,"pos(%p) < line+len(%p)\n",pos,line+len); */
870 /* fprintf(stderr,"found a space pos = %p\n",pos); */
877 /* fprintf(stderr,"if 1 else\n"); */
880 state_puts (s->prefix, s);
881 for (i = 0; i < ql; ++i)
883 if (option (OPTSTUFFQUOTED) && (ql > 0 || s->prefix))
885 state_puts (oldpos, s);
886 /* fprintf(stderr,"print_flowed_line: `%s'\n",oldpos); */
887 if (pos < line + len)
889 state_putc ('\n', s);
892 /*state_puts(line,s);
893 state_putc('\n',s); */
896 static int text_plain_flowed_handler (BODY * a, STATE * s)
898 int bytes = a->length;
899 char buf[LONG_STRING];
900 char *curline = str_dup ("");
902 unsigned int curline_len = 1;
903 unsigned int quotelevel = 0, newql = 0;
904 int buf_off, buf_len;
906 while (bytes > 0 && fgets (buf, sizeof (buf), s->fpin)) {
907 buf_len = str_len (buf);
910 newql = get_quote_level (buf);
912 /* a change of quoting level in a paragraph - shouldn't happen,
913 * but has to be handled - see RFC 3676, sec. 4.5.
915 if (newql != quotelevel && curline && *curline) {
916 print_flowed_line (curline, s, quotelevel);
922 /* XXX - If a line is longer than buf (shouldn't happen), it is split.
923 * This will almost always cause an unintended line break, and
924 * possibly a change in quoting level. But that's better than not
925 * displaying it at all.
927 if ((t = strrchr (buf, '\n')) || (t = strrchr (buf, '\r'))) {
932 if (buf[buf_off] == ' ')
935 /* signature separator also flushes the previous paragraph */
936 if (strcmp(buf + buf_off, "-- ") == 0 && curline && *curline) {
937 print_flowed_line (curline, s, quotelevel);
942 curline = realloc (curline, curline_len + buf_len - buf_off);
943 strcpy (curline + curline_len - 1, buf + buf_off);
944 curline_len += buf_len - buf_off;
946 /* if this was a fixed line the paragraph is finished */
947 if (buf_len == 0 || buf[buf_len - 1] != ' ' || strcmp(buf + buf_off, "-- ") == 0) {
948 print_flowed_line (curline, s, quotelevel);
960 #define TXTENRICHED 3
962 static int alternative_handler (BODY * a, STATE * s)
972 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
973 a->encoding == ENCUUENCODED) {
977 fstat (fileno (s->fpin), &st);
978 b = mutt_new_body ();
979 b->length = (long) st.st_size;
980 b->parts = mutt_parse_multipart (s->fpin,
981 mutt_get_parameter ("boundary",
984 ascii_strcasecmp ("digest",
992 /* First, search list of prefered types */
993 t = AlternativeOrderList;
994 while (t && !choice) {
996 int btlen; /* length of basetype */
997 int wild; /* do we have a wildcard to match all subtypes? */
999 c = strchr (t->data, '/');
1001 wild = (c[1] == '*' && c[2] == 0);
1002 btlen = c - t->data;
1006 btlen = str_len (t->data);
1014 const char *bt = TYPE (b);
1016 if (!ascii_strncasecmp (bt, t->data, btlen) && bt[btlen] == 0) {
1017 /* the basetype matches */
1018 if (wild || !ascii_strcasecmp (t->data + btlen + 1, b->subtype)) {
1027 /* Next, look for an autoviewable type */
1034 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
1035 if (mutt_is_autoview (b, buf)) {
1036 rfc1524_entry *entry = rfc1524_new_entry ();
1038 if (rfc1524_mailcap_lookup (b, buf, entry, M_AUTOVIEW)) {
1041 rfc1524_free_entry (&entry);
1047 /* Then, look for a text entry */
1054 if (b->type == TYPETEXT) {
1055 if (!ascii_strcasecmp ("plain", b->subtype) && type <= TXTPLAIN) {
1059 else if (!ascii_strcasecmp ("enriched", b->subtype)
1060 && type <= TXTENRICHED) {
1064 else if (!ascii_strcasecmp ("html", b->subtype) && type <= TXTHTML) {
1073 /* Finally, look for other possibilities */
1080 if (mutt_can_decode (b))
1087 if (s->flags & M_DISPLAY && !option (OPTWEED)) {
1088 fseek (s->fpin, choice->hdr_offset, 0);
1089 mutt_copy_bytes (s->fpin, s->fpout,
1090 choice->offset - choice->hdr_offset);
1092 mutt_body_handler (choice, s);
1094 else if (s->flags & M_DISPLAY) {
1095 /* didn't find anything that we could display! */
1096 state_mark_attach (s);
1097 state_puts (_("[-- Error: Could not display any parts of Multipart/Alternative! --]\n"), s);
1102 mutt_free_body (&a);
1107 /* handles message/rfc822 body parts */
1108 static int message_handler (BODY * a, STATE * s)
1115 off_start = ftell (s->fpin);
1116 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1117 a->encoding == ENCUUENCODED) {
1118 fstat (fileno (s->fpin), &st);
1119 b = mutt_new_body ();
1120 b->length = (long) st.st_size;
1121 b->parts = mutt_parse_messageRFC822 (s->fpin, b);
1127 mutt_copy_hdr (s->fpin, s->fpout, off_start, b->parts->offset,
1128 (((s->flags & M_WEED)
1129 || ((s->flags & (M_DISPLAY | M_PRINTING))
1130 && option (OPTWEED))) ? (CH_WEED | CH_REORDER) : 0) |
1131 (s->prefix ? CH_PREFIX : 0) | CH_DECODE | CH_FROM,
1135 state_puts (s->prefix, s);
1136 state_putc ('\n', s);
1138 rc = mutt_body_handler (b->parts, s);
1141 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1142 a->encoding == ENCUUENCODED)
1143 mutt_free_body (&b);
1148 /* returns 1 if decoding the attachment will produce output */
1149 int mutt_can_decode (BODY * a)
1153 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
1154 if (mutt_is_autoview (a, type))
1155 return (rfc1524_mailcap_lookup (a, type, NULL, M_AUTOVIEW));
1156 else if (a->type == TYPETEXT)
1158 else if (a->type == TYPEMESSAGE)
1160 else if (a->type == TYPEMULTIPART) {
1164 if (ascii_strcasecmp (a->subtype, "signed") == 0 ||
1165 ascii_strcasecmp (a->subtype, "encrypted") == 0)
1169 for (p = a->parts; p; p = p->next) {
1170 if (mutt_can_decode (p))
1175 else if (WithCrypto && a->type == TYPEAPPLICATION) {
1176 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (a))
1178 if ((WithCrypto & APPLICATION_SMIME) && mutt_is_application_smime (a))
1185 static int multipart_handler (BODY * a, STATE * s)
1193 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1194 a->encoding == ENCUUENCODED) {
1195 fstat (fileno (s->fpin), &st);
1196 b = mutt_new_body ();
1197 b->length = (long) st.st_size;
1198 b->parts = mutt_parse_multipart (s->fpin,
1199 mutt_get_parameter ("boundary",
1202 ascii_strcasecmp ("digest",
1208 for (p = b->parts, count = 1; p; p = p->next, count++) {
1209 if (s->flags & M_DISPLAY) {
1210 state_mark_attach (s);
1211 state_printf (s, _("[-- Attachment #%d"), count);
1212 if (p->description || p->filename || p->form_name) {
1213 state_puts (": ", s);
1214 state_puts (p->description ? p->description :
1215 p->filename ? p->filename : p->form_name, s);
1217 state_puts (" --]\n", s);
1219 mutt_pretty_size (length, sizeof (length), p->length);
1221 state_mark_attach (s);
1222 state_printf (s, _("[-- Type: %s/%s, Encoding: %s, Size: %s --]\n"),
1223 TYPE (p), p->subtype, ENCODING (p->encoding), length);
1224 if (!option (OPTWEED)) {
1225 fseek (s->fpin, p->hdr_offset, 0);
1226 mutt_copy_bytes (s->fpin, s->fpout, p->offset - p->hdr_offset);
1229 state_putc ('\n', s);
1232 if (p->description && mutt_can_decode (p))
1233 state_printf (s, "Content-Description: %s\n", p->description);
1236 state_printf (s, "%s: \n", p->form_name);
1239 rc = mutt_body_handler (p, s);
1240 state_putc ('\n', s);
1241 if (rc || ((s->flags & M_REPLYING)
1242 && (option (OPTINCLUDEONLYFIRST)) && (s->flags & M_FIRSTDONE)))
1246 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1247 a->encoding == ENCUUENCODED)
1248 mutt_free_body (&b);
1253 static int autoview_handler (BODY * a, STATE * s)
1255 rfc1524_entry *entry = rfc1524_new_entry ();
1256 char buffer[LONG_STRING];
1258 char command[LONG_STRING];
1259 char tempfile[_POSIX_PATH_MAX] = "";
1268 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
1269 rfc1524_mailcap_lookup (a, type, entry, M_AUTOVIEW);
1271 fname = str_dup (a->filename);
1272 mutt_sanitize_filename (fname, 1);
1273 rfc1524_expand_filename (entry->nametemplate, fname, tempfile,
1277 if (entry->command) {
1278 strfcpy (command, entry->command, sizeof (command));
1280 /* rfc1524_expand_command returns 0 if the file is required */
1282 rfc1524_expand_command (a, tempfile, type, command, sizeof (command));
1284 if (s->flags & M_DISPLAY) {
1285 state_mark_attach (s);
1286 state_printf (s, _("[-- Autoview using %s --]\n"), command);
1287 mutt_message (_("Invoking autoview command: %s"), command);
1290 if ((fpin = safe_fopen (tempfile, "w+")) == NULL) {
1291 mutt_perror ("fopen");
1292 rfc1524_free_entry (&entry);
1296 mutt_copy_bytes (s->fpin, fpin, a->length);
1299 safe_fclose (&fpin);
1300 thepid = mutt_create_filter (command, NULL, &fpout, &fperr);
1306 thepid = mutt_create_filter_fd (command, NULL, &fpout, &fperr,
1307 fileno (fpin), -1, -1);
1311 mutt_perror (_("Can't create filter"));
1313 if (s->flags & M_DISPLAY) {
1314 state_mark_attach (s);
1315 state_printf (s, _("[-- Can't run %s. --]\n"), command);
1322 while (fgets (buffer, sizeof (buffer), fpout) != NULL) {
1323 state_puts (s->prefix, s);
1324 state_puts (buffer, s);
1326 /* check for data on stderr */
1327 if (fgets (buffer, sizeof (buffer), fperr)) {
1328 if (s->flags & M_DISPLAY) {
1329 state_mark_attach (s);
1330 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1333 state_puts (s->prefix, s);
1334 state_puts (buffer, s);
1335 while (fgets (buffer, sizeof (buffer), fperr) != NULL) {
1336 state_puts (s->prefix, s);
1337 state_puts (buffer, s);
1342 mutt_copy_stream (fpout, s->fpout);
1343 /* Check for stderr messages */
1344 if (fgets (buffer, sizeof (buffer), fperr)) {
1345 if (s->flags & M_DISPLAY) {
1346 state_mark_attach (s);
1347 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1350 state_puts (buffer, s);
1351 mutt_copy_stream (fperr, s->fpout);
1356 safe_fclose (&fpout);
1357 safe_fclose (&fperr);
1359 mutt_wait_filter (thepid);
1361 safe_fclose (&fpin);
1363 mutt_unlink (tempfile);
1365 if (s->flags & M_DISPLAY)
1366 mutt_clear_error ();
1368 rfc1524_free_entry (&entry);
1372 static int external_body_handler (BODY * b, STATE * s)
1374 const char *access_type;
1375 const char *expiration;
1378 access_type = mutt_get_parameter ("access-type", b->parameter);
1380 if (s->flags & M_DISPLAY) {
1381 state_mark_attach (s);
1382 state_puts (_("[-- Error: message/external-body has no access-type parameter --]\n"), s);
1387 expiration = mutt_get_parameter ("expiration", b->parameter);
1389 expire = mutt_parse_date (expiration, NULL);
1393 if (!ascii_strcasecmp (access_type, "x-mutt-deleted")) {
1394 if (s->flags & (M_DISPLAY | M_PRINTING)) {
1396 char pretty_size[10];
1398 state_mark_attach (s);
1399 state_printf (s, _("[-- This %s/%s attachment "),
1400 TYPE (b->parts), b->parts->subtype);
1401 length = mutt_get_parameter ("length", b->parameter);
1403 mutt_pretty_size (pretty_size, sizeof (pretty_size),
1404 strtol (length, NULL, 10));
1405 state_printf (s, _("(size %s bytes) "), pretty_size);
1407 state_puts (_("has been deleted --]\n"), s);
1410 state_mark_attach (s);
1411 state_printf (s, _("[-- on %s --]\n"), expiration);
1413 if (b->parts->filename) {
1414 state_mark_attach (s);
1415 state_printf (s, _("[-- name: %s --]\n"), b->parts->filename);
1418 mutt_copy_hdr (s->fpin, s->fpout, ftell (s->fpin), b->parts->offset,
1419 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1423 else if (expiration && expire < time (NULL)) {
1424 if (s->flags & M_DISPLAY) {
1425 state_mark_attach (s);
1426 state_printf (s, _("[-- This %s/%s attachment is not included, --]\n"),
1427 TYPE (b->parts), b->parts->subtype);
1428 state_attach_puts (_("[-- and the indicated external source has --]\n"
1429 "[-- expired. --]\n"), s);
1431 mutt_copy_hdr (s->fpin, s->fpout, ftell (s->fpin), b->parts->offset,
1432 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1437 if (s->flags & M_DISPLAY) {
1438 state_mark_attach (s);
1440 _("[-- This %s/%s attachment is not included, --]\n"),
1441 TYPE (b->parts), b->parts->subtype);
1442 state_mark_attach (s);
1445 ("[-- and the indicated access-type %s is unsupported --]\n"),
1447 mutt_copy_hdr (s->fpin, s->fpout, ftell (s->fpin), b->parts->offset,
1448 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1455 void mutt_decode_attachment (BODY * b, STATE * s)
1457 int istext = mutt_is_text_part (b);
1458 iconv_t cd = (iconv_t) (-1);
1463 if (s->flags & M_CHARCONV) {
1464 char *charset = mutt_get_parameter ("charset", b->parameter);
1466 if (!option (OPTSTRICTMIME) && !charset)
1467 charset = mutt_get_first_charset (AssumedCharset);
1468 if (charset && Charset)
1469 cd = mutt_iconv_open (Charset, charset, M_ICONV_HOOK_FROM);
1472 if (b->file_charset)
1473 cd = mutt_iconv_open (Charset, b->file_charset, M_ICONV_HOOK_FROM);
1477 fseek (s->fpin, b->offset, 0);
1478 switch (b->encoding) {
1479 case ENCQUOTEDPRINTABLE:
1480 mutt_decode_quoted (s, b->length, istext, cd);
1483 mutt_decode_base64 (s, b->length, istext, cd);
1486 mutt_decode_uuencoded (s, b->length, istext, cd);
1489 mutt_decode_xbit (s, b->length, istext, cd);
1493 if (cd != (iconv_t) (-1))
1497 int mutt_body_handler (BODY * b, STATE * s)
1502 char tempfile[_POSIX_PATH_MAX];
1503 handler_t handler = NULL;
1505 size_t tmplength = 0;
1509 int oflags = s->flags;
1511 /* first determine which handler to use to process this part */
1513 snprintf (type, sizeof (type), "%s/%s", TYPE (b), b->subtype);
1514 if (mutt_is_autoview (b, type)) {
1515 rfc1524_entry *entry = rfc1524_new_entry ();
1517 if (rfc1524_mailcap_lookup (b, type, entry, M_AUTOVIEW)) {
1518 handler = autoview_handler;
1519 s->flags &= ~M_CHARCONV;
1521 rfc1524_free_entry (&entry);
1523 else if (b->type == TYPETEXT) {
1524 if (ascii_strcasecmp ("plain", b->subtype) == 0) {
1525 /* avoid copying this part twice since removing the transfer-encoding is
1526 * the only operation needed.
1528 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b))
1529 handler = crypt_pgp_application_pgp_handler;
1531 if (ascii_strcasecmp
1532 ("flowed", mutt_get_parameter ("format", b->parameter)) == 0)
1533 handler = text_plain_flowed_handler;
1537 else if (ascii_strcasecmp ("enriched", b->subtype) == 0)
1538 handler = text_enriched_handler;
1539 else /* text body type without a handler */
1542 else if (b->type == TYPEMESSAGE) {
1543 if (mutt_is_message_type (b->type, b->subtype))
1544 handler = message_handler;
1545 else if (!ascii_strcasecmp ("delivery-status", b->subtype))
1547 else if (!ascii_strcasecmp ("external-body", b->subtype))
1548 handler = external_body_handler;
1550 else if (b->type == TYPEMULTIPART) {
1553 if (ascii_strcasecmp ("alternative", b->subtype) == 0)
1554 handler = alternative_handler;
1555 else if (WithCrypto && ascii_strcasecmp ("signed", b->subtype) == 0) {
1556 p = mutt_get_parameter ("protocol", b->parameter);
1559 mutt_error (_("Error: multipart/signed has no protocol."));
1561 else if (s->flags & M_VERIFY)
1562 handler = mutt_signed_handler;
1564 else if ((WithCrypto & APPLICATION_PGP)
1565 && str_casecmp ("encrypted", b->subtype) == 0) {
1566 p = mutt_get_parameter ("protocol", b->parameter);
1570 ("Error: multipart/encrypted has no protocol parameter!"));
1572 else if (ascii_strcasecmp ("application/pgp-encrypted", p) == 0)
1573 handler = crypt_pgp_encrypted_handler;
1577 handler = multipart_handler;
1579 else if (WithCrypto && b->type == TYPEAPPLICATION) {
1580 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b))
1581 handler = crypt_pgp_application_pgp_handler;
1582 if ((WithCrypto & APPLICATION_SMIME) && mutt_is_application_smime (b))
1583 handler = crypt_smime_application_smime_handler;
1587 if (plaintext || handler) {
1588 fseek (s->fpin, b->offset, 0);
1590 /* see if we need to decode this part before processing it */
1591 if (b->encoding == ENCBASE64 || b->encoding == ENCQUOTEDPRINTABLE || b->encoding == ENCUUENCODED || plaintext || mutt_is_text_part (b)) { /* text subtypes may
1593 * set conversion even
1594 * with 8bit encoding.
1596 int origType = b->type;
1597 char *savePrefix = NULL;
1600 /* decode to a tempfile, saving the original destination */
1602 mutt_mktemp (tempfile);
1603 if ((s->fpout = safe_fopen (tempfile, "w")) == NULL) {
1604 mutt_error _("Unable to open temporary file!");
1608 /* decoding the attachment changes the size and offset, so save a copy
1609 * of the "real" values now, and restore them after processing
1611 tmplength = b->length;
1612 tmpoffset = b->offset;
1614 /* if we are decoding binary bodies, we don't want to prefix each
1615 * line with the prefix or else the data will get corrupted.
1617 savePrefix = s->prefix;
1625 mutt_decode_attachment (b, s);
1628 b->length = ftell (s->fpout);
1632 /* restore final destination and substitute the tempfile for input */
1635 s->fpin = safe_fopen (tempfile, "r");
1638 /* restore the prefix */
1639 s->prefix = savePrefix;
1645 /* process the (decoded) body part */
1647 rc = handler (b, s);
1650 b->length = tmplength;
1651 b->offset = tmpoffset;
1653 /* restore the original source stream */
1658 s->flags |= M_FIRSTDONE;
1660 else if (s->flags & M_DISPLAY) {
1661 state_mark_attach (s);
1662 state_printf (s, _("[-- %s/%s is unsupported "), TYPE (b), b->subtype);
1663 if (!option (OPTVIEWATTACH)) {
1665 (type, sizeof (type),
1666 km_find_func (MENU_PAGER, OP_VIEW_ATTACHMENTS)))
1667 fprintf (s->fpout, _("(use '%s' to view this part)"), type);
1669 fputs (_("(need 'view-attachments' bound to key!)"), s->fpout);
1671 fputs (" --]\n", s->fpout);
1675 s->flags = oflags | (s->flags & M_FIRSTDONE);