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.
22 #include "mutt_curses.h"
28 #include "mutt_crypt.h"
36 #include "lib/debug.h"
38 typedef void handler_f (BODY *, STATE *);
39 typedef handler_f *handler_t;
41 int Index_hex[128] = {
42 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
43 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
44 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
45 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
46 -1, 10, 11, 12, 13, 14, 15, -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 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
49 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
53 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
54 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
55 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
56 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
57 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
58 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
59 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
60 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1
63 void mutt_decode_xbit (STATE * s, long len, int istext, iconv_t cd)
72 while ((c = fgetc (s->fpin)) != EOF && len--) {
73 if (c == '\r' && len) {
74 if ((ch = fgetc (s->fpin)) == '\n') {
83 if (l == sizeof (bufi))
84 mutt_convert_to_state (cd, bufi, &l, s);
87 mutt_convert_to_state (cd, bufi, &l, s);
88 mutt_convert_to_state (cd, 0, 0, s);
90 state_reset_prefix (s);
93 mutt_copy_bytes (s->fpin, s->fpout, len);
96 static int qp_decode_triple (char *s, char *d)
99 if (*s == '=' && !(*(s + 1)))
102 /* quoted-printable triple */
104 isxdigit ((unsigned char) *(s + 1)) &&
105 isxdigit ((unsigned char) *(s + 2))) {
106 *d = (hexval (*(s + 1)) << 4) | hexval (*(s + 2));
114 static void qp_decode_line (char *dest, char *src, size_t * l, int last)
122 /* decode the line */
124 for (d = dest, s = src; *s;) {
125 switch ((kind = qp_decode_triple (s, &c))) {
129 break; /* qp triple */
132 break; /* single character */
136 break; /* soft line break */
140 if (!soft && last == '\n')
148 * Decode an attachment encoded with quoted-printable.
150 * Why doesn't this overflow any buffers? First, it's guaranteed
151 * that the length of a line grows when you _en_-code it to
152 * quoted-printable. That means that we always can store the
153 * result in a buffer of at most the _same_ size.
155 * Now, we don't special-case if the line we read with fgets()
156 * isn't terminated. We don't care about this, since STRING > 78,
157 * so corrupted input will just be corrupted a bit more. That
158 * implies that STRING+1 bytes are always sufficient to store the
159 * result of qp_decode_line.
161 * Finally, at soft line breaks, some part of a multibyte character
162 * may have been left over by mutt_convert_to_state(). This shouldn't
163 * be more than 6 characters, so STRING + 7 should be sufficient
164 * memory to store the decoded data.
166 * Just to make sure that I didn't make some off-by-one error
167 * above, we just use STRING*2 for the target buffer's size.
171 void mutt_decode_quoted (STATE * s, long len, int istext, iconv_t cd)
174 char decline[2 * STRING];
176 size_t linelen; /* number of input bytes in `line' */
179 int last; /* store the last character in the input line */
182 state_set_prefix (s);
188 * It's ok to use a fixed size buffer for input, even if the line turns
189 * out to be longer than this. Just process the line in chunks. This
190 * really shouldn't happen according the MIME spec, since Q-P encoded
191 * lines are at most 76 characters, but we should be liberal about what
194 if (fgets (line, MIN ((ssize_t) sizeof (line), len + 1), s->fpin) == NULL)
197 linelen = str_len (line);
201 * inspect the last character we read so we can tell if we got the
204 last = linelen ? line[linelen - 1] : 0;
206 /* chop trailing whitespace if we got the full line */
208 while (linelen > 0 && ISSPACE (line[linelen - 1]))
213 /* decode and do character set conversion */
214 qp_decode_line (decline + l, line, &l3, last);
216 mutt_convert_to_state (cd, decline, &l, s);
219 mutt_convert_to_state (cd, 0, 0, s);
220 state_reset_prefix (s);
223 void mutt_decode_base64 (STATE * s, long len, int istext, iconv_t cd)
226 int c1, c2, c3, c4, ch, cr = 0, i;
227 char bufi[BUFI_SIZE];
233 state_set_prefix (s);
236 for (i = 0; i < 4 && len > 0; len--) {
237 if ((ch = fgetc (s->fpin)) == EOF)
239 if (ch >= 0 && ch < 128 && (base64val (ch) != -1 || ch == '='))
243 debug_print (2, ("didn't get a multiple of 4 chars.\n"));
247 c1 = base64val (buf[0]);
248 c2 = base64val (buf[1]);
249 ch = (c1 << 2) | (c2 >> 4);
251 if (cr && ch != '\n')
256 if (istext && ch == '\r')
263 c3 = base64val (buf[2]);
264 ch = ((c2 & 0xf) << 4) | (c3 >> 2);
266 if (cr && ch != '\n')
271 if (istext && ch == '\r')
278 c4 = base64val (buf[3]);
279 ch = ((c3 & 0x3) << 6) | c4;
281 if (cr && ch != '\n')
285 if (istext && ch == '\r')
290 if (l + 8 >= sizeof (bufi))
291 mutt_convert_to_state (cd, bufi, &l, s);
297 mutt_convert_to_state (cd, bufi, &l, s);
298 mutt_convert_to_state (cd, 0, 0, s);
300 state_reset_prefix (s);
303 unsigned char decode_byte (char ch)
310 void mutt_decode_uuencoded (STATE * s, long len, int istext, iconv_t cd)
312 char tmps[SHORT_STRING];
313 char linelen, c, l, out;
315 char bufi[BUFI_SIZE];
319 state_set_prefix (s);
322 if ((fgets (tmps, sizeof (tmps), s->fpin)) == NULL)
324 len -= str_len (tmps);
325 if ((!str_ncmp (tmps, "begin", 5)) && ISSPACE (tmps[5]))
329 if ((fgets (tmps, sizeof (tmps), s->fpin)) == NULL)
331 len -= str_len (tmps);
332 if (!str_ncmp (tmps, "end", 3))
335 linelen = decode_byte (*pt);
337 for (c = 0; c < linelen;) {
338 for (l = 2; l <= 6; l += 2) {
339 out = decode_byte (*pt) << l;
341 out |= (decode_byte (*pt) >> (6 - l));
347 mutt_convert_to_state (cd, bufi, &k, s);
352 mutt_convert_to_state (cd, bufi, &k, s);
353 mutt_convert_to_state (cd, 0, 0, s);
355 state_reset_prefix (s);
358 /* ----------------------------------------------------------------------------
359 * A (not so) minimal implementation of RFC1563.
362 #define IndentSize (4)
364 enum { RICH_PARAM = 0, RICH_BOLD, RICH_UNDERLINE, RICH_ITALIC, RICH_NOFILL,
365 RICH_INDENT, RICH_INDENT_RIGHT, RICH_EXCERPT, RICH_CENTER, RICH_FLUSHLEFT,
366 RICH_FLUSHRIGHT, RICH_COLOR, RICH_LAST_TAG
370 const char *tag_name;
374 "param", RICH_PARAM}, {
375 "bold", RICH_BOLD}, {
376 "italic", RICH_ITALIC}, {
377 "underline", RICH_UNDERLINE}, {
378 "nofill", RICH_NOFILL}, {
379 "excerpt", RICH_EXCERPT}, {
380 "indent", RICH_INDENT}, {
381 "indentright", RICH_INDENT_RIGHT}, {
382 "center", RICH_CENTER}, {
383 "flushleft", RICH_FLUSHLEFT}, {
384 "flushright", RICH_FLUSHRIGHT}, {
385 "flushboth", RICH_FLUSHLEFT}, {
386 "color", RICH_COLOR}, {
387 "x-color", RICH_COLOR}, {
391 struct enriched_state {
404 int tag_level[RICH_LAST_TAG];
409 static void enriched_wrap (struct enriched_state *stte)
414 if (stte->line_len) {
415 if (stte->tag_level[RICH_CENTER] || stte->tag_level[RICH_FLUSHRIGHT]) {
416 /* Strip trailing white space */
417 size_t y = stte->line_used - 1;
419 while (y && ISSPACE (stte->line[y])) {
420 stte->line[y] = '\0';
425 if (stte->tag_level[RICH_CENTER]) {
426 /* Strip leading whitespace */
429 while (stte->line[y] && ISSPACE (stte->line[y]))
434 for (z = y; z <= stte->line_used; z++) {
435 stte->line[z - y] = stte->line[z];
439 stte->line_used -= y;
444 extra = stte->WrapMargin - stte->line_len - stte->indent_len -
445 (stte->tag_level[RICH_INDENT_RIGHT] * IndentSize);
447 if (stte->tag_level[RICH_CENTER]) {
450 state_putc (' ', stte->s);
454 else if (stte->tag_level[RICH_FLUSHRIGHT]) {
457 state_putc (' ', stte->s);
462 state_puts (stte->line, stte->s);
465 state_putc ('\n', stte->s);
466 stte->line[0] = '\0';
469 stte->indent_len = 0;
470 if (stte->s->prefix) {
471 state_puts (stte->s->prefix, stte->s);
472 stte->indent_len += str_len (stte->s->prefix);
475 if (stte->tag_level[RICH_EXCERPT]) {
476 x = stte->tag_level[RICH_EXCERPT];
478 if (stte->s->prefix) {
479 state_puts (stte->s->prefix, stte->s);
480 stte->indent_len += str_len (stte->s->prefix);
483 state_puts ("> ", stte->s);
484 stte->indent_len += str_len ("> ");
490 stte->indent_len = 0;
491 if (stte->tag_level[RICH_INDENT]) {
492 x = stte->tag_level[RICH_INDENT] * IndentSize;
493 stte->indent_len += x;
495 state_putc (' ', stte->s);
501 static void enriched_flush (struct enriched_state *stte, int wrap)
503 if (!stte->tag_level[RICH_NOFILL] && (stte->line_len + stte->word_len >
505 (stte->tag_level[RICH_INDENT_RIGHT] *
506 IndentSize) - stte->indent_len)))
507 enriched_wrap (stte);
509 if (stte->buff_used) {
510 stte->buffer[stte->buff_used] = '\0';
511 stte->line_used += stte->buff_used;
512 if (stte->line_used > stte->line_max) {
513 stte->line_max = stte->line_used;
514 mem_realloc (&stte->line, stte->line_max + 1);
516 strcat (stte->line, stte->buffer); /* __STRCAT_CHECKED__ */
517 stte->line_len += stte->word_len;
522 enriched_wrap (stte);
526 static void enriched_putc (int c, struct enriched_state *stte)
528 if (stte->tag_level[RICH_PARAM]) {
529 if (stte->tag_level[RICH_COLOR]) {
530 if (stte->param_used + 1 >= stte->param_len)
531 mem_realloc (&stte->param, (stte->param_len += STRING));
533 stte->param[stte->param_used++] = c;
535 return; /* nothing to do */
538 /* see if more space is needed (plus extra for possible rich characters) */
539 if (stte->buff_len < stte->buff_used + 3) {
540 stte->buff_len += LONG_STRING;
541 mem_realloc (&stte->buffer, stte->buff_len + 1);
544 if ((!stte->tag_level[RICH_NOFILL] && ISSPACE (c)) || c == '\0') {
546 stte->word_len += 8 - (stte->line_len + stte->word_len) % 8;
550 stte->buffer[stte->buff_used++] = c;
551 enriched_flush (stte, 0);
554 if (stte->s->flags & M_DISPLAY) {
555 if (stte->tag_level[RICH_BOLD]) {
556 stte->buffer[stte->buff_used++] = c;
557 stte->buffer[stte->buff_used++] = '\010';
558 stte->buffer[stte->buff_used++] = c;
560 else if (stte->tag_level[RICH_UNDERLINE]) {
562 stte->buffer[stte->buff_used++] = '_';
563 stte->buffer[stte->buff_used++] = '\010';
564 stte->buffer[stte->buff_used++] = c;
566 else if (stte->tag_level[RICH_ITALIC]) {
567 stte->buffer[stte->buff_used++] = c;
568 stte->buffer[stte->buff_used++] = '\010';
569 stte->buffer[stte->buff_used++] = '_';
572 stte->buffer[stte->buff_used++] = c;
576 stte->buffer[stte->buff_used++] = c;
582 static void enriched_puts (char *s, struct enriched_state *stte)
586 if (stte->buff_len < stte->buff_used + str_len (s)) {
587 stte->buff_len += LONG_STRING;
588 mem_realloc (&stte->buffer, stte->buff_len + 1);
592 stte->buffer[stte->buff_used++] = *c;
597 static void enriched_set_flags (const char *tag, struct enriched_state *stte)
599 const char *tagptr = tag;
605 for (i = 0, j = -1; EnrichedTags[i].tag_name; i++)
606 if (ascii_strcasecmp (EnrichedTags[i].tag_name, tagptr) == 0) {
607 j = EnrichedTags[i].index;
612 if (j == RICH_CENTER || j == RICH_FLUSHLEFT || j == RICH_FLUSHRIGHT)
613 enriched_flush (stte, 1);
616 if (stte->tag_level[j]) /* make sure not to go negative */
617 stte->tag_level[j]--;
618 if ((stte->s->flags & M_DISPLAY) && j == RICH_PARAM
619 && stte->tag_level[RICH_COLOR]) {
620 stte->param[stte->param_used] = '\0';
621 if (!ascii_strcasecmp (stte->param, "black")) {
622 enriched_puts ("\033[30m", stte);
624 else if (!ascii_strcasecmp (stte->param, "red")) {
625 enriched_puts ("\033[31m", stte);
627 else if (!ascii_strcasecmp (stte->param, "green")) {
628 enriched_puts ("\033[32m", stte);
630 else if (!ascii_strcasecmp (stte->param, "yellow")) {
631 enriched_puts ("\033[33m", stte);
633 else if (!ascii_strcasecmp (stte->param, "blue")) {
634 enriched_puts ("\033[34m", stte);
636 else if (!ascii_strcasecmp (stte->param, "magenta")) {
637 enriched_puts ("\033[35m", stte);
639 else if (!ascii_strcasecmp (stte->param, "cyan")) {
640 enriched_puts ("\033[36m", stte);
642 else if (!ascii_strcasecmp (stte->param, "white")) {
643 enriched_puts ("\033[37m", stte);
646 if ((stte->s->flags & M_DISPLAY) && j == RICH_COLOR) {
647 enriched_puts ("\033[0m", stte);
650 /* flush parameter buffer when closing the tag */
651 if (j == RICH_PARAM) {
652 stte->param_used = 0;
653 stte->param[0] = '\0';
657 stte->tag_level[j]++;
659 if (j == RICH_EXCERPT)
660 enriched_flush (stte, 1);
664 void text_enriched_handler (BODY * a, STATE * s)
667 TEXT, LANGLE, TAG, BOGUS_TAG, NEWLINE, ST_EOF, DONE
670 long bytes = a->length;
671 struct enriched_state stte;
674 char tag[LONG_STRING + 1];
676 memset (&stte, 0, sizeof (stte));
679 ((s->flags & M_DISPLAY) ? (COLS - 4) : ((COLS - 4) <
680 72) ? (COLS - 4) : 72);
681 stte.line_max = stte.WrapMargin * 4;
682 stte.line = (char *) mem_calloc (1, stte.line_max + 1);
683 stte.param = (char *) mem_calloc (1, STRING);
685 stte.param_len = STRING;
689 state_puts (s->prefix, s);
690 stte.indent_len += str_len (s->prefix);
693 while (state != DONE) {
694 if (state != ST_EOF) {
695 if (!bytes || (c = fgetc (s->fpin)) == EOF)
709 if (stte.tag_level[RICH_NOFILL]) {
710 enriched_flush (&stte, 1);
713 enriched_putc (' ', &stte);
719 enriched_putc (c, &stte);
725 enriched_putc (c, &stte);
733 /* Yes, fall through (it wasn't a <<, so this char is first in TAG) */
737 enriched_set_flags (tag, &stte);
740 else if (tag_len < LONG_STRING) /* ignore overly long tags */
753 enriched_flush (&stte, 1);
762 enriched_putc ('\0', &stte);
763 enriched_flush (&stte, 1);
767 case DONE: /* not reached, but gcc complains if this is absent */
772 state_putc ('\n', s); /* add a final newline */
774 mem_free (&(stte.buffer));
775 mem_free (&(stte.line));
776 mem_free (&(stte.param));
780 * An implementation of RFC 2646.
782 * NOTE: This still has to be made UTF-8 aware.
786 #define FLOWED_MAX 77
788 static int get_quote_level (char *line)
792 for (quoted = 0; line[quoted] == '>'; quoted++);
796 static void print_flowed_line (char *line, STATE * s, int ql)
800 int len = str_len (line);
803 if (MaxLineLength > 0) {
804 width = MaxLineLength - WrapMargin - ql - 1;
805 if (option (OPTSTUFFQUOTED))
808 width = MaxLineLength;
811 if (option (OPTMBOXPANE))
812 width = COLS - SidebarWidth - WrapMargin - ql - 1;
814 width = COLS - WrapMargin - ql - 1;
816 if (option (OPTSTUFFQUOTED))
822 /* fprintf(stderr,"print_flowed_line will print `%s' with ql = %d\n",line,ql); */
824 if (str_len (line) == 0) {
825 if (option (OPTQUOTEEMPTY)) {
827 state_puts(s->prefix,s);
828 for (i=0;i<ql;++i) state_putc('>',s);
829 if (option(OPTSTUFFQUOTED))
836 pos = line + ql + width;
838 if (ql > 0 && ISBLANK (*oldpos))
841 /* fprintf(stderr,"oldpos = %p line+len = %p\n",oldpos,line+len); */
843 for (; oldpos < line + len; pos += width) {
844 /* fprintf(stderr,"outer for loop\n"); */
845 if (pos < line + len) { /* only search a new position when we're not over the end of the string w/ pos */
846 /* fprintf(stderr,"if 1\n"); */
848 /* fprintf(stderr,"if 2: good luck! found a space\n"); */
853 /* fprintf(stderr,"if 2: else\n"); */
856 while (pos >= oldpos && !isspace (*pos)) {
857 /* fprintf(stderr,"pos(%p) > oldpos(%p)\n",pos,oldpos); */
861 /* fprintf(stderr,"wow, no space found, searching the other direction\n"); */
863 while (pos < line + len && *pos && !isspace (*pos)) {
864 /* fprintf(stderr,"pos(%p) < line+len(%p)\n",pos,line+len); */
867 /* fprintf(stderr,"found a space pos = %p\n",pos); */
874 /* fprintf(stderr,"if 1 else\n"); */
877 state_puts (s->prefix, s);
878 for (i = 0; i < ql; ++i)
880 if (option (OPTSTUFFQUOTED) && (ql > 0 || s->prefix))
882 state_puts (oldpos, s);
883 /* fprintf(stderr,"print_flowed_line: `%s'\n",oldpos); */
884 if (pos < line + len)
886 state_putc ('\n', s);
889 /*state_puts(line,s);
890 state_putc('\n',s); */
893 static void text_plain_flowed_handler (BODY * a, STATE * s)
895 int bytes = a->length;
896 char buf[LONG_STRING];
897 char *curline = strdup ("");
899 unsigned int curline_len = 1;
900 unsigned int quotelevel = 0, newql = 0;
903 while (bytes > 0 && fgets (buf, sizeof (buf), s->fpin)) {
905 bytes -= str_len (buf);
907 newql = get_quote_level (buf);
909 if (bytes == 0 || ((t = strrchr (buf, '\n')) || (t = strrchr (buf, '\r')))) {
912 if (str_len (curline) > 0 && curline[str_len (curline) - 1] == ' '
913 && newql == quotelevel
914 && strcmp (curline + quotelevel, "-- ") != 0) {
915 if (buf[newql] == ' ')
916 curline[str_len (curline) - 1] = '\0';
918 curline = realloc (curline, curline_len + str_len (buf));
919 if (curline_len == 1)
921 curline_len += str_len (buf);
922 str_ncat (curline, curline_len, buf + newql,
923 str_len (buf + newql));
930 print_flowed_line (curline, s, quotelevel);
934 curline = realloc (curline, curline_len + str_len (buf));
935 if (curline_len == 1)
937 curline_len += str_len (buf);
938 str_ncat (curline, curline_len, buf, str_len (buf));
944 print_flowed_line (curline, s, quotelevel);
951 #define TXTENRICHED 3
953 static void alternative_handler (BODY * a, STATE * s)
962 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
963 a->encoding == ENCUUENCODED) {
967 fstat (fileno (s->fpin), &st);
968 b = mutt_new_body ();
969 b->length = (long) st.st_size;
970 b->parts = mutt_parse_multipart (s->fpin,
971 mutt_get_parameter ("boundary",
974 ascii_strcasecmp ("digest",
982 /* First, search list of prefered types */
983 t = AlternativeOrderList;
984 while (t && !choice) {
986 int btlen; /* length of basetype */
987 int wild; /* do we have a wildcard to match all subtypes? */
989 c = strchr (t->data, '/');
991 wild = (c[1] == '*' && c[2] == 0);
996 btlen = str_len (t->data);
1004 const char *bt = TYPE (b);
1006 if (!ascii_strncasecmp (bt, t->data, btlen) && bt[btlen] == 0) {
1007 /* the basetype matches */
1008 if (wild || !ascii_strcasecmp (t->data + btlen + 1, b->subtype)) {
1017 /* Next, look for an autoviewable type */
1024 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
1025 if (mutt_is_autoview (b, buf)) {
1026 rfc1524_entry *entry = rfc1524_new_entry ();
1028 if (rfc1524_mailcap_lookup (b, buf, entry, M_AUTOVIEW)) {
1031 rfc1524_free_entry (&entry);
1037 /* Then, look for a text entry */
1044 if (b->type == TYPETEXT) {
1045 if (!ascii_strcasecmp ("plain", b->subtype) && type <= TXTPLAIN) {
1049 else if (!ascii_strcasecmp ("enriched", b->subtype)
1050 && type <= TXTENRICHED) {
1054 else if (!ascii_strcasecmp ("html", b->subtype) && type <= TXTHTML) {
1063 /* Finally, look for other possibilities */
1070 if (mutt_can_decode (b))
1077 if (s->flags & M_DISPLAY && !option (OPTWEED)) {
1078 fseek (s->fpin, choice->hdr_offset, 0);
1079 mutt_copy_bytes (s->fpin, s->fpout,
1080 choice->offset - choice->hdr_offset);
1082 mutt_body_handler (choice, s);
1084 else if (s->flags & M_DISPLAY) {
1085 /* didn't find anything that we could display! */
1086 state_mark_attach (s);
1088 ("[-- Error: Could not display any parts of Multipart/Alternative! --]\n"),
1093 mutt_free_body (&a);
1096 /* handles message/rfc822 body parts */
1097 void message_handler (BODY * a, STATE * s)
1103 off_start = ftell (s->fpin);
1104 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1105 a->encoding == ENCUUENCODED) {
1106 fstat (fileno (s->fpin), &st);
1107 b = mutt_new_body ();
1108 b->length = (long) st.st_size;
1109 b->parts = mutt_parse_messageRFC822 (s->fpin, b);
1115 mutt_copy_hdr (s->fpin, s->fpout, off_start, b->parts->offset,
1116 (((s->flags & M_WEED)
1117 || ((s->flags & (M_DISPLAY | M_PRINTING))
1118 && option (OPTWEED))) ? (CH_WEED | CH_REORDER) : 0) |
1119 (s->prefix ? CH_PREFIX : 0) | CH_DECODE | CH_FROM,
1123 state_puts (s->prefix, s);
1124 state_putc ('\n', s);
1126 mutt_body_handler (b->parts, s);
1129 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1130 a->encoding == ENCUUENCODED)
1131 mutt_free_body (&b);
1134 /* returns 1 if decoding the attachment will produce output */
1135 int mutt_can_decode (BODY * a)
1139 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
1140 if (mutt_is_autoview (a, type))
1141 return (rfc1524_mailcap_lookup (a, type, NULL, M_AUTOVIEW));
1142 else if (a->type == TYPETEXT)
1144 else if (a->type == TYPEMESSAGE)
1146 else if (a->type == TYPEMULTIPART) {
1150 if (ascii_strcasecmp (a->subtype, "signed") == 0 ||
1151 ascii_strcasecmp (a->subtype, "encrypted") == 0)
1155 for (p = a->parts; p; p = p->next) {
1156 if (mutt_can_decode (p))
1161 else if (WithCrypto && a->type == TYPEAPPLICATION) {
1162 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (a))
1164 if ((WithCrypto & APPLICATION_SMIME) && mutt_is_application_smime (a))
1171 void multipart_handler (BODY * a, STATE * s)
1178 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1179 a->encoding == ENCUUENCODED) {
1180 fstat (fileno (s->fpin), &st);
1181 b = mutt_new_body ();
1182 b->length = (long) st.st_size;
1183 b->parts = mutt_parse_multipart (s->fpin,
1184 mutt_get_parameter ("boundary",
1187 ascii_strcasecmp ("digest",
1193 for (p = b->parts, count = 1; p; p = p->next, count++) {
1194 if (s->flags & M_DISPLAY) {
1195 state_mark_attach (s);
1196 state_printf (s, _("[-- Attachment #%d"), count);
1197 if (p->description || p->filename || p->form_name) {
1198 state_puts (": ", s);
1199 state_puts (p->description ? p->description :
1200 p->filename ? p->filename : p->form_name, s);
1202 state_puts (" --]\n", s);
1204 mutt_pretty_size (length, sizeof (length), p->length);
1206 state_mark_attach (s);
1207 state_printf (s, _("[-- Type: %s/%s, Encoding: %s, Size: %s --]\n"),
1208 TYPE (p), p->subtype, ENCODING (p->encoding), length);
1209 if (!option (OPTWEED)) {
1210 fseek (s->fpin, p->hdr_offset, 0);
1211 mutt_copy_bytes (s->fpin, s->fpout, p->offset - p->hdr_offset);
1214 state_putc ('\n', s);
1217 if (p->description && mutt_can_decode (p))
1218 state_printf (s, "Content-Description: %s\n", p->description);
1221 state_printf (s, "%s: \n", p->form_name);
1224 mutt_body_handler (p, s);
1225 state_putc ('\n', s);
1226 if ((s->flags & M_REPLYING)
1227 && (option (OPTINCLUDEONLYFIRST)) && (s->flags & M_FIRSTDONE))
1231 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1232 a->encoding == ENCUUENCODED)
1233 mutt_free_body (&b);
1236 void autoview_handler (BODY * a, STATE * s)
1238 rfc1524_entry *entry = rfc1524_new_entry ();
1239 char buffer[LONG_STRING];
1241 char command[LONG_STRING];
1242 char tempfile[_POSIX_PATH_MAX] = "";
1250 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
1251 rfc1524_mailcap_lookup (a, type, entry, M_AUTOVIEW);
1253 fname = str_dup (a->filename);
1254 mutt_sanitize_filename (fname, 1);
1255 rfc1524_expand_filename (entry->nametemplate, fname, tempfile,
1259 if (entry->command) {
1260 strfcpy (command, entry->command, sizeof (command));
1262 /* rfc1524_expand_command returns 0 if the file is required */
1264 rfc1524_expand_command (a, tempfile, type, command, sizeof (command));
1266 if (s->flags & M_DISPLAY) {
1267 state_mark_attach (s);
1268 state_printf (s, _("[-- Autoview using %s --]\n"), command);
1269 mutt_message (_("Invoking autoview command: %s"), command);
1272 if ((fpin = safe_fopen (tempfile, "w+")) == NULL) {
1273 mutt_perror ("fopen");
1274 rfc1524_free_entry (&entry);
1278 mutt_copy_bytes (s->fpin, fpin, a->length);
1281 safe_fclose (&fpin);
1282 thepid = mutt_create_filter (command, NULL, &fpout, &fperr);
1288 thepid = mutt_create_filter_fd (command, NULL, &fpout, &fperr,
1289 fileno (fpin), -1, -1);
1293 mutt_perror (_("Can't create filter"));
1295 if (s->flags & M_DISPLAY) {
1296 state_mark_attach (s);
1297 state_printf (s, _("[-- Can't run %s. --]\n"), command);
1303 while (fgets (buffer, sizeof (buffer), fpout) != NULL) {
1304 state_puts (s->prefix, s);
1305 state_puts (buffer, s);
1307 /* check for data on stderr */
1308 if (fgets (buffer, sizeof (buffer), fperr)) {
1309 if (s->flags & M_DISPLAY) {
1310 state_mark_attach (s);
1311 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1314 state_puts (s->prefix, s);
1315 state_puts (buffer, s);
1316 while (fgets (buffer, sizeof (buffer), fperr) != NULL) {
1317 state_puts (s->prefix, s);
1318 state_puts (buffer, s);
1323 mutt_copy_stream (fpout, s->fpout);
1324 /* Check for stderr messages */
1325 if (fgets (buffer, sizeof (buffer), fperr)) {
1326 if (s->flags & M_DISPLAY) {
1327 state_mark_attach (s);
1328 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1331 state_puts (buffer, s);
1332 mutt_copy_stream (fperr, s->fpout);
1337 safe_fclose (&fpout);
1338 safe_fclose (&fperr);
1340 mutt_wait_filter (thepid);
1342 safe_fclose (&fpin);
1344 mutt_unlink (tempfile);
1346 if (s->flags & M_DISPLAY)
1347 mutt_clear_error ();
1349 rfc1524_free_entry (&entry);
1352 static void external_body_handler (BODY * b, STATE * s)
1354 const char *access_type;
1355 const char *expiration;
1358 access_type = mutt_get_parameter ("access-type", b->parameter);
1360 if (s->flags & M_DISPLAY) {
1361 state_mark_attach (s);
1363 ("[-- Error: message/external-body has no access-type parameter --]\n"),
1369 expiration = mutt_get_parameter ("expiration", b->parameter);
1371 expire = mutt_parse_date (expiration, NULL);
1375 if (!ascii_strcasecmp (access_type, "x-mutt-deleted")) {
1376 if (s->flags & (M_DISPLAY | M_PRINTING)) {
1378 char pretty_size[10];
1380 state_mark_attach (s);
1381 state_printf (s, _("[-- This %s/%s attachment "),
1382 TYPE (b->parts), b->parts->subtype);
1383 length = mutt_get_parameter ("length", b->parameter);
1385 mutt_pretty_size (pretty_size, sizeof (pretty_size),
1386 strtol (length, NULL, 10));
1387 state_printf (s, _("(size %s bytes) "), pretty_size);
1389 state_puts (_("has been deleted --]\n"), s);
1392 state_mark_attach (s);
1393 state_printf (s, _("[-- on %s --]\n"), expiration);
1395 if (b->parts->filename) {
1396 state_mark_attach (s);
1397 state_printf (s, _("[-- name: %s --]\n"), b->parts->filename);
1400 mutt_copy_hdr (s->fpin, s->fpout, ftell (s->fpin), b->parts->offset,
1401 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1405 else if (expiration && expire < time (NULL)) {
1406 if (s->flags & M_DISPLAY) {
1407 state_mark_attach (s);
1408 state_printf (s, _("[-- This %s/%s attachment is not included, --]\n"),
1409 TYPE (b->parts), b->parts->subtype);
1410 state_attach_puts (_("[-- and the indicated external source has --]\n"
1411 "[-- expired. --]\n"), s);
1413 mutt_copy_hdr (s->fpin, s->fpout, ftell (s->fpin), b->parts->offset,
1414 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1419 if (s->flags & M_DISPLAY) {
1420 state_mark_attach (s);
1422 _("[-- This %s/%s attachment is not included, --]\n"),
1423 TYPE (b->parts), b->parts->subtype);
1424 state_mark_attach (s);
1427 ("[-- and the indicated access-type %s is unsupported --]\n"),
1429 mutt_copy_hdr (s->fpin, s->fpout, ftell (s->fpin), b->parts->offset,
1430 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1436 void mutt_decode_attachment (BODY * b, STATE * s)
1438 int istext = mutt_is_text_part (b);
1439 iconv_t cd = (iconv_t) (-1);
1444 if (s->flags & M_CHARCONV) {
1445 char *charset = mutt_get_parameter ("charset", b->parameter);
1447 if (!option (OPTSTRICTMIME) && !charset)
1448 charset = mutt_get_first_charset (AssumedCharset);
1449 if (charset && Charset)
1450 cd = mutt_iconv_open (Charset, charset, M_ICONV_HOOK_FROM);
1453 if (b->file_charset)
1454 cd = mutt_iconv_open (Charset, b->file_charset, M_ICONV_HOOK_FROM);
1458 fseek (s->fpin, b->offset, 0);
1459 switch (b->encoding) {
1460 case ENCQUOTEDPRINTABLE:
1461 mutt_decode_quoted (s, b->length, istext, cd);
1464 mutt_decode_base64 (s, b->length, istext, cd);
1467 mutt_decode_uuencoded (s, b->length, istext, cd);
1470 mutt_decode_xbit (s, b->length, istext, cd);
1474 if (cd != (iconv_t) (-1))
1478 void mutt_body_handler (BODY * b, STATE * s)
1483 char tempfile[_POSIX_PATH_MAX];
1484 handler_t handler = NULL;
1486 size_t tmplength = 0;
1489 int oflags = s->flags;
1491 /* first determine which handler to use to process this part */
1493 snprintf (type, sizeof (type), "%s/%s", TYPE (b), b->subtype);
1494 if (mutt_is_autoview (b, type)) {
1495 rfc1524_entry *entry = rfc1524_new_entry ();
1497 if (rfc1524_mailcap_lookup (b, type, entry, M_AUTOVIEW)) {
1498 handler = autoview_handler;
1499 s->flags &= ~M_CHARCONV;
1501 rfc1524_free_entry (&entry);
1503 else if (b->type == TYPETEXT) {
1504 if (ascii_strcasecmp ("plain", b->subtype) == 0) {
1505 /* avoid copying this part twice since removing the transfer-encoding is
1506 * the only operation needed.
1508 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b))
1509 handler = crypt_pgp_application_pgp_handler;
1511 if (ascii_strcasecmp
1512 ("flowed", mutt_get_parameter ("format", b->parameter)) == 0)
1513 handler = text_plain_flowed_handler;
1517 else if (ascii_strcasecmp ("enriched", b->subtype) == 0)
1518 handler = text_enriched_handler;
1519 else /* text body type without a handler */
1522 else if (b->type == TYPEMESSAGE) {
1523 if (mutt_is_message_type (b->type, b->subtype))
1524 handler = message_handler;
1525 else if (!ascii_strcasecmp ("delivery-status", b->subtype))
1527 else if (!ascii_strcasecmp ("external-body", b->subtype))
1528 handler = external_body_handler;
1530 else if (b->type == TYPEMULTIPART) {
1533 if (ascii_strcasecmp ("alternative", b->subtype) == 0)
1534 handler = alternative_handler;
1535 else if (WithCrypto && ascii_strcasecmp ("signed", b->subtype) == 0) {
1536 p = mutt_get_parameter ("protocol", b->parameter);
1539 mutt_error (_("Error: multipart/signed has no protocol."));
1541 else if (s->flags & M_VERIFY)
1542 handler = mutt_signed_handler;
1544 else if ((WithCrypto & APPLICATION_PGP)
1545 && str_casecmp ("encrypted", b->subtype) == 0) {
1546 p = mutt_get_parameter ("protocol", b->parameter);
1550 ("Error: multipart/encrypted has no protocol parameter!"));
1552 else if (ascii_strcasecmp ("application/pgp-encrypted", p) == 0)
1553 handler = crypt_pgp_encrypted_handler;
1557 handler = multipart_handler;
1559 else if (WithCrypto && b->type == TYPEAPPLICATION) {
1560 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b))
1561 handler = crypt_pgp_application_pgp_handler;
1562 if ((WithCrypto & APPLICATION_SMIME) && mutt_is_application_smime (b))
1563 handler = crypt_smime_application_smime_handler;
1567 if (plaintext || handler) {
1568 fseek (s->fpin, b->offset, 0);
1570 /* see if we need to decode this part before processing it */
1571 if (b->encoding == ENCBASE64 || b->encoding == ENCQUOTEDPRINTABLE || b->encoding == ENCUUENCODED || plaintext || mutt_is_text_part (b)) { /* text subtypes may
1573 * set conversion even
1574 * with 8bit encoding.
1576 int origType = b->type;
1577 char *savePrefix = NULL;
1580 /* decode to a tempfile, saving the original destination */
1582 mutt_mktemp (tempfile);
1583 if ((s->fpout = safe_fopen (tempfile, "w")) == NULL) {
1584 mutt_error _("Unable to open temporary file!");
1588 /* decoding the attachment changes the size and offset, so save a copy
1589 * of the "real" values now, and restore them after processing
1591 tmplength = b->length;
1592 tmpoffset = b->offset;
1594 /* if we are decoding binary bodies, we don't want to prefix each
1595 * line with the prefix or else the data will get corrupted.
1597 savePrefix = s->prefix;
1605 mutt_decode_attachment (b, s);
1608 b->length = ftell (s->fpout);
1612 /* restore final destination and substitute the tempfile for input */
1615 s->fpin = safe_fopen (tempfile, "r");
1618 /* restore the prefix */
1619 s->prefix = savePrefix;
1625 /* process the (decoded) body part */
1630 b->length = tmplength;
1631 b->offset = tmpoffset;
1633 /* restore the original source stream */
1638 s->flags |= M_FIRSTDONE;
1640 else if (s->flags & M_DISPLAY) {
1641 state_mark_attach (s);
1642 state_printf (s, _("[-- %s/%s is unsupported "), TYPE (b), b->subtype);
1643 if (!option (OPTVIEWATTACH)) {
1645 (type, sizeof (type),
1646 km_find_func (MENU_PAGER, OP_VIEW_ATTACHMENTS)))
1647 fprintf (s->fpout, _("(use '%s' to view this part)"), type);
1649 fputs (_("(need 'view-attachments' bound to key!)"), s->fpout);
1651 fputs (" --]\n", s->fpout);
1655 s->flags = oflags | (s->flags & M_FIRSTDONE);