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))
841 pos = line + ql + width;
843 if (ql > 0 && ISBLANK (*oldpos))
846 /* fprintf(stderr,"oldpos = %p line+len = %p\n",oldpos,line+len); */
848 for (; oldpos < line + len; pos += width) {
849 /* fprintf(stderr,"outer for loop\n"); */
850 if (pos < line + len) { /* only search a new position when we're not over the end of the string w/ pos */
851 /* fprintf(stderr,"if 1\n"); */
853 /* fprintf(stderr,"if 2: good luck! found a space\n"); */
858 /* fprintf(stderr,"if 2: else\n"); */
861 while (pos >= oldpos && !isspace (*pos)) {
862 /* fprintf(stderr,"pos(%p) > oldpos(%p)\n",pos,oldpos); */
866 /* fprintf(stderr,"wow, no space found, searching the other direction\n"); */
868 while (pos < line + len && *pos && !isspace (*pos)) {
869 /* fprintf(stderr,"pos(%p) < line+len(%p)\n",pos,line+len); */
872 /* fprintf(stderr,"found a space pos = %p\n",pos); */
879 /* fprintf(stderr,"if 1 else\n"); */
882 state_puts (s->prefix, s);
883 for (i = 0; i < ql; ++i)
885 if (option (OPTSTUFFQUOTED) && (ql > 0 || s->prefix))
887 state_puts (oldpos, s);
888 /* fprintf(stderr,"print_flowed_line: `%s'\n",oldpos); */
889 if (pos < line + len)
891 state_putc ('\n', s);
894 /*state_puts(line,s);
895 state_putc('\n',s); */
898 static int text_plain_flowed_handler (BODY * a, STATE * s)
900 int bytes = a->length;
901 char buf[LONG_STRING];
902 char *curline = strdup ("");
904 unsigned int curline_len = 1;
905 unsigned int quotelevel = 0, newql = 0;
908 while (bytes > 0 && fgets (buf, sizeof (buf), s->fpin)) {
910 bytes -= str_len (buf);
912 newql = get_quote_level (buf);
914 if (bytes == 0 || ((t = strrchr (buf, '\n')) || (t = strrchr (buf, '\r')))) {
917 if (str_len (curline) > 0 && curline[str_len (curline) - 1] == ' '
918 && newql == quotelevel
919 && strcmp (curline + quotelevel, "-- ") != 0) {
920 if (buf[newql] == ' ')
921 curline[str_len (curline) - 1] = '\0';
923 curline = realloc (curline, curline_len + str_len (buf));
924 if (curline_len == 1)
926 curline_len += str_len (buf);
927 str_ncat (curline, curline_len, buf + newql,
928 str_len (buf + newql));
935 print_flowed_line (curline, s, quotelevel);
939 curline = realloc (curline, curline_len + str_len (buf));
940 if (curline_len == 1)
942 curline_len += str_len (buf);
943 str_ncat (curline, curline_len, buf, str_len (buf));
949 print_flowed_line (curline, s, quotelevel);
957 #define TXTENRICHED 3
959 static int alternative_handler (BODY * a, STATE * s)
969 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
970 a->encoding == ENCUUENCODED) {
974 fstat (fileno (s->fpin), &st);
975 b = mutt_new_body ();
976 b->length = (long) st.st_size;
977 b->parts = mutt_parse_multipart (s->fpin,
978 mutt_get_parameter ("boundary",
981 ascii_strcasecmp ("digest",
989 /* First, search list of prefered types */
990 t = AlternativeOrderList;
991 while (t && !choice) {
993 int btlen; /* length of basetype */
994 int wild; /* do we have a wildcard to match all subtypes? */
996 c = strchr (t->data, '/');
998 wild = (c[1] == '*' && c[2] == 0);
1003 btlen = str_len (t->data);
1011 const char *bt = TYPE (b);
1013 if (!ascii_strncasecmp (bt, t->data, btlen) && bt[btlen] == 0) {
1014 /* the basetype matches */
1015 if (wild || !ascii_strcasecmp (t->data + btlen + 1, b->subtype)) {
1024 /* Next, look for an autoviewable type */
1031 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
1032 if (mutt_is_autoview (b, buf)) {
1033 rfc1524_entry *entry = rfc1524_new_entry ();
1035 if (rfc1524_mailcap_lookup (b, buf, entry, M_AUTOVIEW)) {
1038 rfc1524_free_entry (&entry);
1044 /* Then, look for a text entry */
1051 if (b->type == TYPETEXT) {
1052 if (!ascii_strcasecmp ("plain", b->subtype) && type <= TXTPLAIN) {
1056 else if (!ascii_strcasecmp ("enriched", b->subtype)
1057 && type <= TXTENRICHED) {
1061 else if (!ascii_strcasecmp ("html", b->subtype) && type <= TXTHTML) {
1070 /* Finally, look for other possibilities */
1077 if (mutt_can_decode (b))
1084 if (s->flags & M_DISPLAY && !option (OPTWEED)) {
1085 fseek (s->fpin, choice->hdr_offset, 0);
1086 mutt_copy_bytes (s->fpin, s->fpout,
1087 choice->offset - choice->hdr_offset);
1089 mutt_body_handler (choice, s);
1091 else if (s->flags & M_DISPLAY) {
1092 /* didn't find anything that we could display! */
1093 state_mark_attach (s);
1094 state_puts (_("[-- Error: Could not display any parts of Multipart/Alternative! --]\n"), s);
1099 mutt_free_body (&a);
1104 /* handles message/rfc822 body parts */
1105 static int message_handler (BODY * a, STATE * s)
1112 off_start = ftell (s->fpin);
1113 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1114 a->encoding == ENCUUENCODED) {
1115 fstat (fileno (s->fpin), &st);
1116 b = mutt_new_body ();
1117 b->length = (long) st.st_size;
1118 b->parts = mutt_parse_messageRFC822 (s->fpin, b);
1124 mutt_copy_hdr (s->fpin, s->fpout, off_start, b->parts->offset,
1125 (((s->flags & M_WEED)
1126 || ((s->flags & (M_DISPLAY | M_PRINTING))
1127 && option (OPTWEED))) ? (CH_WEED | CH_REORDER) : 0) |
1128 (s->prefix ? CH_PREFIX : 0) | CH_DECODE | CH_FROM,
1132 state_puts (s->prefix, s);
1133 state_putc ('\n', s);
1135 rc = mutt_body_handler (b->parts, s);
1138 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1139 a->encoding == ENCUUENCODED)
1140 mutt_free_body (&b);
1145 /* returns 1 if decoding the attachment will produce output */
1146 int mutt_can_decode (BODY * a)
1150 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
1151 if (mutt_is_autoview (a, type))
1152 return (rfc1524_mailcap_lookup (a, type, NULL, M_AUTOVIEW));
1153 else if (a->type == TYPETEXT)
1155 else if (a->type == TYPEMESSAGE)
1157 else if (a->type == TYPEMULTIPART) {
1161 if (ascii_strcasecmp (a->subtype, "signed") == 0 ||
1162 ascii_strcasecmp (a->subtype, "encrypted") == 0)
1166 for (p = a->parts; p; p = p->next) {
1167 if (mutt_can_decode (p))
1172 else if (WithCrypto && a->type == TYPEAPPLICATION) {
1173 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (a))
1175 if ((WithCrypto & APPLICATION_SMIME) && mutt_is_application_smime (a))
1182 static int multipart_handler (BODY * a, STATE * s)
1190 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1191 a->encoding == ENCUUENCODED) {
1192 fstat (fileno (s->fpin), &st);
1193 b = mutt_new_body ();
1194 b->length = (long) st.st_size;
1195 b->parts = mutt_parse_multipart (s->fpin,
1196 mutt_get_parameter ("boundary",
1199 ascii_strcasecmp ("digest",
1205 for (p = b->parts, count = 1; p; p = p->next, count++) {
1206 if (s->flags & M_DISPLAY) {
1207 state_mark_attach (s);
1208 state_printf (s, _("[-- Attachment #%d"), count);
1209 if (p->description || p->filename || p->form_name) {
1210 state_puts (": ", s);
1211 state_puts (p->description ? p->description :
1212 p->filename ? p->filename : p->form_name, s);
1214 state_puts (" --]\n", s);
1216 mutt_pretty_size (length, sizeof (length), p->length);
1218 state_mark_attach (s);
1219 state_printf (s, _("[-- Type: %s/%s, Encoding: %s, Size: %s --]\n"),
1220 TYPE (p), p->subtype, ENCODING (p->encoding), length);
1221 if (!option (OPTWEED)) {
1222 fseek (s->fpin, p->hdr_offset, 0);
1223 mutt_copy_bytes (s->fpin, s->fpout, p->offset - p->hdr_offset);
1226 state_putc ('\n', s);
1229 if (p->description && mutt_can_decode (p))
1230 state_printf (s, "Content-Description: %s\n", p->description);
1233 state_printf (s, "%s: \n", p->form_name);
1236 rc = mutt_body_handler (p, s);
1237 state_putc ('\n', s);
1238 if (rc || ((s->flags & M_REPLYING)
1239 && (option (OPTINCLUDEONLYFIRST)) && (s->flags & M_FIRSTDONE)))
1243 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1244 a->encoding == ENCUUENCODED)
1245 mutt_free_body (&b);
1250 static int autoview_handler (BODY * a, STATE * s)
1252 rfc1524_entry *entry = rfc1524_new_entry ();
1253 char buffer[LONG_STRING];
1255 char command[LONG_STRING];
1256 char tempfile[_POSIX_PATH_MAX] = "";
1265 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
1266 rfc1524_mailcap_lookup (a, type, entry, M_AUTOVIEW);
1268 fname = str_dup (a->filename);
1269 mutt_sanitize_filename (fname, 1);
1270 rfc1524_expand_filename (entry->nametemplate, fname, tempfile,
1274 if (entry->command) {
1275 strfcpy (command, entry->command, sizeof (command));
1277 /* rfc1524_expand_command returns 0 if the file is required */
1279 rfc1524_expand_command (a, tempfile, type, command, sizeof (command));
1281 if (s->flags & M_DISPLAY) {
1282 state_mark_attach (s);
1283 state_printf (s, _("[-- Autoview using %s --]\n"), command);
1284 mutt_message (_("Invoking autoview command: %s"), command);
1287 if ((fpin = safe_fopen (tempfile, "w+")) == NULL) {
1288 mutt_perror ("fopen");
1289 rfc1524_free_entry (&entry);
1293 mutt_copy_bytes (s->fpin, fpin, a->length);
1296 safe_fclose (&fpin);
1297 thepid = mutt_create_filter (command, NULL, &fpout, &fperr);
1303 thepid = mutt_create_filter_fd (command, NULL, &fpout, &fperr,
1304 fileno (fpin), -1, -1);
1308 mutt_perror (_("Can't create filter"));
1310 if (s->flags & M_DISPLAY) {
1311 state_mark_attach (s);
1312 state_printf (s, _("[-- Can't run %s. --]\n"), command);
1319 while (fgets (buffer, sizeof (buffer), fpout) != NULL) {
1320 state_puts (s->prefix, s);
1321 state_puts (buffer, s);
1323 /* check for data on stderr */
1324 if (fgets (buffer, sizeof (buffer), fperr)) {
1325 if (s->flags & M_DISPLAY) {
1326 state_mark_attach (s);
1327 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1330 state_puts (s->prefix, s);
1331 state_puts (buffer, s);
1332 while (fgets (buffer, sizeof (buffer), fperr) != NULL) {
1333 state_puts (s->prefix, s);
1334 state_puts (buffer, s);
1339 mutt_copy_stream (fpout, s->fpout);
1340 /* Check for stderr messages */
1341 if (fgets (buffer, sizeof (buffer), fperr)) {
1342 if (s->flags & M_DISPLAY) {
1343 state_mark_attach (s);
1344 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1347 state_puts (buffer, s);
1348 mutt_copy_stream (fperr, s->fpout);
1353 safe_fclose (&fpout);
1354 safe_fclose (&fperr);
1356 mutt_wait_filter (thepid);
1358 safe_fclose (&fpin);
1360 mutt_unlink (tempfile);
1362 if (s->flags & M_DISPLAY)
1363 mutt_clear_error ();
1365 rfc1524_free_entry (&entry);
1369 static int external_body_handler (BODY * b, STATE * s)
1371 const char *access_type;
1372 const char *expiration;
1375 access_type = mutt_get_parameter ("access-type", b->parameter);
1377 if (s->flags & M_DISPLAY) {
1378 state_mark_attach (s);
1379 state_puts (_("[-- Error: message/external-body has no access-type parameter --]\n"), s);
1384 expiration = mutt_get_parameter ("expiration", b->parameter);
1386 expire = mutt_parse_date (expiration, NULL);
1390 if (!ascii_strcasecmp (access_type, "x-mutt-deleted")) {
1391 if (s->flags & (M_DISPLAY | M_PRINTING)) {
1393 char pretty_size[10];
1395 state_mark_attach (s);
1396 state_printf (s, _("[-- This %s/%s attachment "),
1397 TYPE (b->parts), b->parts->subtype);
1398 length = mutt_get_parameter ("length", b->parameter);
1400 mutt_pretty_size (pretty_size, sizeof (pretty_size),
1401 strtol (length, NULL, 10));
1402 state_printf (s, _("(size %s bytes) "), pretty_size);
1404 state_puts (_("has been deleted --]\n"), s);
1407 state_mark_attach (s);
1408 state_printf (s, _("[-- on %s --]\n"), expiration);
1410 if (b->parts->filename) {
1411 state_mark_attach (s);
1412 state_printf (s, _("[-- name: %s --]\n"), b->parts->filename);
1415 mutt_copy_hdr (s->fpin, s->fpout, ftell (s->fpin), b->parts->offset,
1416 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1420 else if (expiration && expire < time (NULL)) {
1421 if (s->flags & M_DISPLAY) {
1422 state_mark_attach (s);
1423 state_printf (s, _("[-- This %s/%s attachment is not included, --]\n"),
1424 TYPE (b->parts), b->parts->subtype);
1425 state_attach_puts (_("[-- and the indicated external source has --]\n"
1426 "[-- expired. --]\n"), s);
1428 mutt_copy_hdr (s->fpin, s->fpout, ftell (s->fpin), b->parts->offset,
1429 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1434 if (s->flags & M_DISPLAY) {
1435 state_mark_attach (s);
1437 _("[-- This %s/%s attachment is not included, --]\n"),
1438 TYPE (b->parts), b->parts->subtype);
1439 state_mark_attach (s);
1442 ("[-- and the indicated access-type %s is unsupported --]\n"),
1444 mutt_copy_hdr (s->fpin, s->fpout, ftell (s->fpin), b->parts->offset,
1445 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1452 void mutt_decode_attachment (BODY * b, STATE * s)
1454 int istext = mutt_is_text_part (b);
1455 iconv_t cd = (iconv_t) (-1);
1460 if (s->flags & M_CHARCONV) {
1461 char *charset = mutt_get_parameter ("charset", b->parameter);
1463 if (!option (OPTSTRICTMIME) && !charset)
1464 charset = mutt_get_first_charset (AssumedCharset);
1465 if (charset && Charset)
1466 cd = mutt_iconv_open (Charset, charset, M_ICONV_HOOK_FROM);
1469 if (b->file_charset)
1470 cd = mutt_iconv_open (Charset, b->file_charset, M_ICONV_HOOK_FROM);
1474 fseek (s->fpin, b->offset, 0);
1475 switch (b->encoding) {
1476 case ENCQUOTEDPRINTABLE:
1477 mutt_decode_quoted (s, b->length, istext, cd);
1480 mutt_decode_base64 (s, b->length, istext, cd);
1483 mutt_decode_uuencoded (s, b->length, istext, cd);
1486 mutt_decode_xbit (s, b->length, istext, cd);
1490 if (cd != (iconv_t) (-1))
1494 int mutt_body_handler (BODY * b, STATE * s)
1499 char tempfile[_POSIX_PATH_MAX];
1500 handler_t handler = NULL;
1502 size_t tmplength = 0;
1506 int oflags = s->flags;
1508 /* first determine which handler to use to process this part */
1510 snprintf (type, sizeof (type), "%s/%s", TYPE (b), b->subtype);
1511 if (mutt_is_autoview (b, type)) {
1512 rfc1524_entry *entry = rfc1524_new_entry ();
1514 if (rfc1524_mailcap_lookup (b, type, entry, M_AUTOVIEW)) {
1515 handler = autoview_handler;
1516 s->flags &= ~M_CHARCONV;
1518 rfc1524_free_entry (&entry);
1520 else if (b->type == TYPETEXT) {
1521 if (ascii_strcasecmp ("plain", b->subtype) == 0) {
1522 /* avoid copying this part twice since removing the transfer-encoding is
1523 * the only operation needed.
1525 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b))
1526 handler = crypt_pgp_application_pgp_handler;
1528 if (ascii_strcasecmp
1529 ("flowed", mutt_get_parameter ("format", b->parameter)) == 0)
1530 handler = text_plain_flowed_handler;
1534 else if (ascii_strcasecmp ("enriched", b->subtype) == 0)
1535 handler = text_enriched_handler;
1536 else /* text body type without a handler */
1539 else if (b->type == TYPEMESSAGE) {
1540 if (mutt_is_message_type (b->type, b->subtype))
1541 handler = message_handler;
1542 else if (!ascii_strcasecmp ("delivery-status", b->subtype))
1544 else if (!ascii_strcasecmp ("external-body", b->subtype))
1545 handler = external_body_handler;
1547 else if (b->type == TYPEMULTIPART) {
1550 if (ascii_strcasecmp ("alternative", b->subtype) == 0)
1551 handler = alternative_handler;
1552 else if (WithCrypto && ascii_strcasecmp ("signed", b->subtype) == 0) {
1553 p = mutt_get_parameter ("protocol", b->parameter);
1556 mutt_error (_("Error: multipart/signed has no protocol."));
1558 else if (s->flags & M_VERIFY)
1559 handler = mutt_signed_handler;
1561 else if ((WithCrypto & APPLICATION_PGP)
1562 && str_casecmp ("encrypted", b->subtype) == 0) {
1563 p = mutt_get_parameter ("protocol", b->parameter);
1567 ("Error: multipart/encrypted has no protocol parameter!"));
1569 else if (ascii_strcasecmp ("application/pgp-encrypted", p) == 0)
1570 handler = crypt_pgp_encrypted_handler;
1574 handler = multipart_handler;
1576 else if (WithCrypto && b->type == TYPEAPPLICATION) {
1577 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b))
1578 handler = crypt_pgp_application_pgp_handler;
1579 if ((WithCrypto & APPLICATION_SMIME) && mutt_is_application_smime (b))
1580 handler = crypt_smime_application_smime_handler;
1584 if (plaintext || handler) {
1585 fseek (s->fpin, b->offset, 0);
1587 /* see if we need to decode this part before processing it */
1588 if (b->encoding == ENCBASE64 || b->encoding == ENCQUOTEDPRINTABLE || b->encoding == ENCUUENCODED || plaintext || mutt_is_text_part (b)) { /* text subtypes may
1590 * set conversion even
1591 * with 8bit encoding.
1593 int origType = b->type;
1594 char *savePrefix = NULL;
1597 /* decode to a tempfile, saving the original destination */
1599 mutt_mktemp (tempfile);
1600 if ((s->fpout = safe_fopen (tempfile, "w")) == NULL) {
1601 mutt_error _("Unable to open temporary file!");
1605 /* decoding the attachment changes the size and offset, so save a copy
1606 * of the "real" values now, and restore them after processing
1608 tmplength = b->length;
1609 tmpoffset = b->offset;
1611 /* if we are decoding binary bodies, we don't want to prefix each
1612 * line with the prefix or else the data will get corrupted.
1614 savePrefix = s->prefix;
1622 mutt_decode_attachment (b, s);
1625 b->length = ftell (s->fpout);
1629 /* restore final destination and substitute the tempfile for input */
1632 s->fpin = safe_fopen (tempfile, "r");
1635 /* restore the prefix */
1636 s->prefix = savePrefix;
1642 /* process the (decoded) body part */
1644 rc = handler (b, s);
1647 b->length = tmplength;
1648 b->offset = tmpoffset;
1650 /* restore the original source stream */
1655 s->flags |= M_FIRSTDONE;
1657 else if (s->flags & M_DISPLAY) {
1658 state_mark_attach (s);
1659 state_printf (s, _("[-- %s/%s is unsupported "), TYPE (b), b->subtype);
1660 if (!option (OPTVIEWATTACH)) {
1662 (type, sizeof (type),
1663 km_find_func (MENU_PAGER, OP_VIEW_ATTACHMENTS)))
1664 fprintf (s->fpout, _("(use '%s' to view this part)"), type);
1666 fputs (_("(need 'view-attachments' bound to key!)"), s->fpout);
1668 fputs (" --]\n", s->fpout);
1672 s->flags = oflags | (s->flags & M_FIRSTDONE);