2 * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
27 #include "mutt_curses.h"
33 #include "mutt_crypt.h"
36 #define BUFI_SIZE 1000
37 #define BUFO_SIZE 2000
40 typedef void handler_f (BODY *, STATE *);
41 typedef handler_f *handler_t;
43 int Index_hex[128] = {
44 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
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 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-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,
50 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
51 -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,-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,62, -1,-1,-1,63,
58 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
59 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
60 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
61 -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
62 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
65 static void state_prefix_put (const char *d, size_t dlen, STATE *s)
69 state_prefix_putc (*d++, s);
71 fwrite (d, dlen, 1, s->fpout);
74 void mutt_convert_to_state(iconv_t cd, char *bufi, size_t *l, STATE *s)
83 if (cd != (iconv_t)(-1))
85 ob = bufo, obl = sizeof (bufo);
86 iconv (cd, 0, 0, &ob, &obl);
88 state_prefix_put (bufo, ob - bufo, s);
90 if (Quotebuf[0] != '\0')
91 state_prefix_putc ('\n', s);
95 if (cd == (iconv_t)(-1))
97 state_prefix_put (bufi, *l, s);
105 ob = bufo, obl = sizeof (bufo);
106 mutt_iconv (cd, &ib, &ibl, &ob, &obl, 0, "?");
109 state_prefix_put (bufo, ob - bufo, s);
111 memmove (bufi, ib, ibl);
115 void mutt_decode_xbit (STATE *s, long len, int istext, iconv_t cd)
118 char bufi[BUFI_SIZE];
125 while ((c = fgetc(s->fpin)) != EOF && len--)
129 if((ch = fgetc(s->fpin)) == '\n')
139 if (l == sizeof (bufi))
140 mutt_convert_to_state (cd, bufi, &l, s);
143 mutt_convert_to_state (cd, bufi, &l, s);
144 mutt_convert_to_state (cd, 0, 0, s);
146 state_reset_prefix (s);
149 mutt_copy_bytes (s->fpin, s->fpout, len);
152 static int qp_decode_triple (char *s, char *d)
154 /* soft line break */
155 if (*s == '=' && !(*(s+1)))
158 /* quoted-printable triple */
160 isxdigit ((unsigned char) *(s+1)) &&
161 isxdigit ((unsigned char) *(s+2)))
163 *d = (hexval (*(s+1)) << 4) | hexval (*(s+2));
171 static void qp_decode_line (char *dest, char *src, size_t *l,
180 /* decode the line */
182 for (d = dest, s = src; *s;)
184 switch ((kind = qp_decode_triple (s, &c)))
186 case 0: *d++ = c; s += 3; break; /* qp triple */
187 case -1: *d++ = *s++; break; /* single character */
188 case 1: soft = 1; s++; break; /* soft line break */
192 if (!soft && last == '\n')
200 * Decode an attachment encoded with quoted-printable.
202 * Why doesn't this overflow any buffers? First, it's guaranteed
203 * that the length of a line grows when you _en_-code it to
204 * quoted-printable. That means that we always can store the
205 * result in a buffer of at most the _same_ size.
207 * Now, we don't special-case if the line we read with fgets()
208 * isn't terminated. We don't care about this, since STRING > 78,
209 * so corrupted input will just be corrupted a bit more. That
210 * implies that STRING+1 bytes are always sufficient to store the
211 * result of qp_decode_line.
213 * Finally, at soft line breaks, some part of a multibyte character
214 * may have been left over by mutt_convert_to_state(). This shouldn't
215 * be more than 6 characters, so STRING + 7 should be sufficient
216 * memory to store the decoded data.
218 * Just to make sure that I didn't make some off-by-one error
219 * above, we just use STRING*2 for the target buffer's size.
223 void mutt_decode_quoted (STATE *s, long len, int istext, iconv_t cd)
226 char decline[2*STRING];
228 size_t linelen; /* number of input bytes in `line' */
231 int last; /* store the last character in the input line */
241 * It's ok to use a fixed size buffer for input, even if the line turns
242 * out to be longer than this. Just process the line in chunks. This
243 * really shouldn't happen according the MIME spec, since Q-P encoded
244 * lines are at most 76 characters, but we should be liberal about what
247 if (fgets (line, MIN ((ssize_t)sizeof (line), len + 1), s->fpin) == NULL)
250 linelen = strlen(line);
254 * inspect the last character we read so we can tell if we got the
257 last = linelen ? line[linelen - 1] : 0;
259 /* chop trailing whitespace if we got the full line */
262 while (linelen > 0 && ISSPACE (line[linelen-1]))
267 /* decode and do character set conversion */
268 qp_decode_line (decline + l, line, &l3, last);
270 mutt_convert_to_state (cd, decline, &l, s);
273 mutt_convert_to_state (cd, 0, 0, s);
274 state_reset_prefix(s);
277 void mutt_decode_base64 (STATE *s, long len, int istext, iconv_t cd)
280 int c1, c2, c3, c4, ch, cr = 0, i;
281 char bufi[BUFI_SIZE];
291 for (i = 0 ; i < 4 && len > 0 ; len--)
293 if ((ch = fgetc (s->fpin)) == EOF)
295 if (ch >= 0 && ch < 128 && (base64val(ch) != -1 || ch == '='))
300 dprint (2, (debugfile, "%s:%d [mutt_decode_base64()]: "
301 "didn't get a multiple of 4 chars.\n", __FILE__, __LINE__));
305 c1 = base64val (buf[0]);
306 c2 = base64val (buf[1]);
307 ch = (c1 << 2) | (c2 >> 4);
309 if (cr && ch != '\n')
314 if (istext && ch == '\r')
321 c3 = base64val (buf[2]);
322 ch = ((c2 & 0xf) << 4) | (c3 >> 2);
324 if (cr && ch != '\n')
329 if (istext && ch == '\r')
334 if (buf[3] == '=') break;
335 c4 = base64val (buf[3]);
336 ch = ((c3 & 0x3) << 6) | c4;
338 if (cr && ch != '\n')
342 if (istext && ch == '\r')
347 if (l + 8 >= sizeof (bufi))
348 mutt_convert_to_state (cd, bufi, &l, s);
351 if (cr) bufi[l++] = '\r';
353 mutt_convert_to_state (cd, bufi, &l, s);
354 mutt_convert_to_state (cd, 0, 0, s);
356 state_reset_prefix(s);
359 unsigned char decode_byte (char ch)
366 void mutt_decode_uuencoded (STATE *s, long len, int istext, iconv_t cd)
368 char tmps[SHORT_STRING];
369 char linelen, c, l, out;
371 char bufi[BUFI_SIZE];
379 if ((fgets(tmps, sizeof(tmps), s->fpin)) == NULL)
381 len -= mutt_strlen(tmps);
382 if ((!mutt_strncmp (tmps, "begin", 5)) && ISSPACE (tmps[5]))
387 if ((fgets(tmps, sizeof(tmps), s->fpin)) == NULL)
389 len -= mutt_strlen(tmps);
390 if (!mutt_strncmp (tmps, "end", 3))
393 linelen = decode_byte (*pt);
395 for (c = 0; c < linelen;)
397 for (l = 2; l <= 6; l += 2)
399 out = decode_byte (*pt) << l;
401 out |= (decode_byte (*pt) >> (6 - l));
407 mutt_convert_to_state (cd, bufi, &k, s);
412 mutt_convert_to_state (cd, bufi, &k, s);
413 mutt_convert_to_state (cd, 0, 0, s);
415 state_reset_prefix(s);
418 /* ----------------------------------------------------------------------------
419 * A (not so) minimal implementation of RFC1563.
422 #define IndentSize (4)
424 enum { RICH_PARAM=0, RICH_BOLD, RICH_UNDERLINE, RICH_ITALIC, RICH_NOFILL,
425 RICH_INDENT, RICH_INDENT_RIGHT, RICH_EXCERPT, RICH_CENTER, RICH_FLUSHLEFT,
426 RICH_FLUSHRIGHT, RICH_COLOR, RICH_LAST_TAG };
429 const char *tag_name;
432 { "param", RICH_PARAM },
433 { "bold", RICH_BOLD },
434 { "italic", RICH_ITALIC },
435 { "underline", RICH_UNDERLINE },
436 { "nofill", RICH_NOFILL },
437 { "excerpt", RICH_EXCERPT },
438 { "indent", RICH_INDENT },
439 { "indentright", RICH_INDENT_RIGHT },
440 { "center", RICH_CENTER },
441 { "flushleft", RICH_FLUSHLEFT },
442 { "flushright", RICH_FLUSHRIGHT },
443 { "flushboth", RICH_FLUSHLEFT },
444 { "color", RICH_COLOR },
445 { "x-color", RICH_COLOR },
449 struct enriched_state
463 int tag_level[RICH_LAST_TAG];
468 static void enriched_wrap (struct enriched_state *stte)
475 if (stte->tag_level[RICH_CENTER] || stte->tag_level[RICH_FLUSHRIGHT])
477 /* Strip trailing white space */
478 size_t y = stte->line_used - 1;
480 while (y && ISSPACE (stte->line[y]))
482 stte->line[y] = '\0';
487 if (stte->tag_level[RICH_CENTER])
489 /* Strip leading whitespace */
492 while (stte->line[y] && ISSPACE (stte->line[y]))
498 for (z = y ; z <= stte->line_used; z++)
500 stte->line[z - y] = stte->line[z];
504 stte->line_used -= y;
509 extra = stte->WrapMargin - stte->line_len - stte->indent_len -
510 (stte->tag_level[RICH_INDENT_RIGHT] * IndentSize);
513 if (stte->tag_level[RICH_CENTER])
518 state_putc (' ', stte->s);
522 else if (stte->tag_level[RICH_FLUSHRIGHT])
527 state_putc (' ', stte->s);
532 state_puts (stte->line, stte->s);
535 state_putc ('\n', stte->s);
536 stte->line[0] = '\0';
539 stte->indent_len = 0;
542 state_puts (stte->s->prefix, stte->s);
543 stte->indent_len += mutt_strlen (stte->s->prefix);
546 if (stte->tag_level[RICH_EXCERPT])
548 x = stte->tag_level[RICH_EXCERPT];
553 state_puts (stte->s->prefix, stte->s);
554 stte->indent_len += mutt_strlen (stte->s->prefix);
558 state_puts ("> ", stte->s);
559 stte->indent_len += mutt_strlen ("> ");
565 stte->indent_len = 0;
566 if (stte->tag_level[RICH_INDENT])
568 x = stte->tag_level[RICH_INDENT] * IndentSize;
569 stte->indent_len += x;
572 state_putc (' ', stte->s);
578 static void enriched_flush (struct enriched_state *stte, int wrap)
580 if (!stte->tag_level[RICH_NOFILL] && (stte->line_len + stte->word_len >
581 (stte->WrapMargin - (stte->tag_level[RICH_INDENT_RIGHT] * IndentSize) -
583 enriched_wrap (stte);
587 stte->buffer[stte->buff_used] = '\0';
588 stte->line_used += stte->buff_used;
589 if (stte->line_used > stte->line_max)
591 stte->line_max = stte->line_used;
592 safe_realloc (&stte->line, stte->line_max + 1);
594 strcat (stte->line, stte->buffer); /* __STRCAT_CHECKED__ */
595 stte->line_len += stte->word_len;
604 static void enriched_putc (int c, struct enriched_state *stte)
606 if (stte->tag_level[RICH_PARAM])
608 if (stte->tag_level[RICH_COLOR])
610 if (stte->param_used + 1 >= stte->param_len)
611 safe_realloc (&stte->param, (stte->param_len += STRING));
613 stte->param[stte->param_used++] = c;
615 return; /* nothing to do */
618 /* see if more space is needed (plus extra for possible rich characters) */
619 if (stte->buff_len < stte->buff_used + 3)
621 stte->buff_len += LONG_STRING;
622 safe_realloc (&stte->buffer, stte->buff_len + 1);
625 if ((!stte->tag_level[RICH_NOFILL] && ISSPACE (c)) || c == '\0' )
628 stte->word_len += 8 - (stte->line_len + stte->word_len) % 8;
632 stte->buffer[stte->buff_used++] = c;
633 enriched_flush (stte, 0);
637 if (stte->s->flags & M_DISPLAY)
639 if (stte->tag_level[RICH_BOLD])
641 stte->buffer[stte->buff_used++] = c;
642 stte->buffer[stte->buff_used++] = '\010';
643 stte->buffer[stte->buff_used++] = c;
645 else if (stte->tag_level[RICH_UNDERLINE])
648 stte->buffer[stte->buff_used++] = '_';
649 stte->buffer[stte->buff_used++] = '\010';
650 stte->buffer[stte->buff_used++] = c;
652 else if (stte->tag_level[RICH_ITALIC])
654 stte->buffer[stte->buff_used++] = c;
655 stte->buffer[stte->buff_used++] = '\010';
656 stte->buffer[stte->buff_used++] = '_';
660 stte->buffer[stte->buff_used++] = c;
665 stte->buffer[stte->buff_used++] = c;
671 static void enriched_puts (char *s, struct enriched_state *stte)
675 if (stte->buff_len < stte->buff_used + mutt_strlen(s))
677 stte->buff_len += LONG_STRING;
678 safe_realloc (&stte->buffer, stte->buff_len + 1);
683 stte->buffer[stte->buff_used++] = *c;
688 static void enriched_set_flags (const char *tag, struct enriched_state *stte)
690 const char *tagptr = tag;
696 for (i = 0, j = -1; EnrichedTags[i].tag_name; i++)
697 if (ascii_strcasecmp (EnrichedTags[i].tag_name,tagptr) == 0)
699 j = EnrichedTags[i].index;
705 if (j == RICH_CENTER || j == RICH_FLUSHLEFT || j == RICH_FLUSHRIGHT)
706 enriched_flush (stte, 1);
710 if (stte->tag_level[j]) /* make sure not to go negative */
711 stte->tag_level[j]--;
712 if ((stte->s->flags & M_DISPLAY) && j == RICH_PARAM && stte->tag_level[RICH_COLOR])
714 stte->param[stte->param_used] = '\0';
715 if (!ascii_strcasecmp(stte->param, "black"))
717 enriched_puts("\033[30m", stte);
719 else if (!ascii_strcasecmp(stte->param, "red"))
721 enriched_puts("\033[31m", stte);
723 else if (!ascii_strcasecmp(stte->param, "green"))
725 enriched_puts("\033[32m", stte);
727 else if (!ascii_strcasecmp(stte->param, "yellow"))
729 enriched_puts("\033[33m", stte);
731 else if (!ascii_strcasecmp(stte->param, "blue"))
733 enriched_puts("\033[34m", stte);
735 else if (!ascii_strcasecmp(stte->param, "magenta"))
737 enriched_puts("\033[35m", stte);
739 else if (!ascii_strcasecmp(stte->param, "cyan"))
741 enriched_puts("\033[36m", stte);
743 else if (!ascii_strcasecmp(stte->param, "white"))
745 enriched_puts("\033[37m", stte);
748 if ((stte->s->flags & M_DISPLAY) && j == RICH_COLOR)
750 enriched_puts("\033[0m", stte);
753 /* flush parameter buffer when closing the tag */
756 stte->param_used = 0;
757 stte->param[0] = '\0';
761 stte->tag_level[j]++;
763 if (j == RICH_EXCERPT)
764 enriched_flush(stte, 1);
768 void text_enriched_handler (BODY *a, STATE *s)
771 TEXT, LANGLE, TAG, BOGUS_TAG, NEWLINE, ST_EOF, DONE
774 long bytes = a->length;
775 struct enriched_state stte;
778 char tag[LONG_STRING + 1];
780 memset (&stte, 0, sizeof (stte));
782 stte.WrapMargin = ((s->flags & M_DISPLAY) ? (COLS-4) : ((COLS-4)<72)?(COLS-4):72);
783 stte.line_max = stte.WrapMargin * 4;
784 stte.line = (char *) safe_calloc (1, stte.line_max + 1);
785 stte.param = (char *) safe_calloc (1, STRING);
787 stte.param_len = STRING;
792 state_puts (s->prefix, s);
793 stte.indent_len += mutt_strlen (s->prefix);
796 while (state != DONE)
800 if (!bytes || (c = fgetc (s->fpin)) == EOF)
816 if (stte.tag_level[RICH_NOFILL])
818 enriched_flush (&stte, 1);
822 enriched_putc (' ', &stte);
828 enriched_putc (c, &stte);
835 enriched_putc (c, &stte);
844 /* Yes, fall through (it wasn't a <<, so this char is first in TAG) */
849 enriched_set_flags (tag, &stte);
852 else if (tag_len < LONG_STRING) /* ignore overly long tags */
865 enriched_flush (&stte, 1);
875 enriched_putc ('\0', &stte);
876 enriched_flush (&stte, 1);
880 case DONE: /* not reached, but gcc complains if this is absent */
885 state_putc ('\n', s); /* add a final newline */
887 FREE (&(stte.buffer));
889 FREE (&(stte.param));
893 * An implementation of RFC 2646.
895 * NOTE: This still has to be made UTF-8 aware.
899 #define FLOWED_MAX 77
901 static void flowed_quote (STATE *s, int level)
907 if (option (OPTTEXTFLOWED))
910 state_puts (s->prefix, s);
913 for (i = 0; i < level; i++)
917 static int flowed_maybe_quoted (char *cont)
919 return regexec ((regex_t *) QuoteRegexp.rx, cont, 0, NULL, 0) == 0;
922 static void flowed_stuff (STATE *s, char *cont, int level)
924 if (!option (OPTTEXTFLOWED) && !(s->flags & M_DISPLAY))
927 if (s->flags & M_DISPLAY)
930 * Hack: If we are in the beginning of the line and there is
931 * some text on the line which looks like it's quoted, turn off
932 * ANSI colors, so quote coloring doesn't affect this line.
934 if (*cont && !level && !mutt_strcmp (Pager, "builtin") && flowed_maybe_quoted (cont))
935 state_puts ("\033[0m",s);
937 else if ((!(s->flags & M_PRINTING)) &&
938 ((*cont == ' ') || (*cont == '>') || (!level && !mutt_strncmp (cont, "From ", 5))))
942 static char *flowed_skip_indent (char *prefix, char *cont)
944 for (; *cont == ' ' || *cont == '\t'; cont++)
950 static int flowed_visual_strlen (char *l, int i)
956 j += 8 - ((i + j) % 8);
965 static void text_plain_flowed_handler (BODY *a, STATE *s)
967 char line[LONG_STRING];
968 char indent[LONG_STRING];
982 int bytes = a->length;
983 int actually_wrap = 0;
996 if ((flowed_max = FLOWED_MAX) > COLS - 3)
997 flowed_max = COLS - 3;
998 if (flowed_max > COLS - WrapMargin)
999 flowed_max = COLS - WrapMargin;
1000 if (flowed_max <= 0)
1003 flowed_max = COLS - WrapMargin;
1004 if (flowed_max <= 0)
1007 fprintf(stderr,"flowed_max = %d\n",flowed_max);
1009 while (bytes > 0 && fgets (line, sizeof (line), s->fpin))
1011 bytes -= strlen (line);
1017 * If the last line wasn't fully read, this is the
1018 * tail of some line.
1020 actually_wrap = !last_full;
1022 if ((t = strrchr (line, '\r')) || (t = strrchr (line, '\n')))
1027 else if ((t = strrchr (line, ' ')) || (t = strrchr (line, '\t')))
1030 * Bad: We have a line of more than LONG_STRING characters.
1031 * (Which SHOULD NOT happen, since lines SHOULD be <= 79
1034 * Try to simulate a soft line break at a word boundary.
1035 * Handle the rest of the line next time.
1037 * Give up when we have a single word which is longer than
1038 * LONG_STRING characters. It will just be split into parts,
1039 * with a hard line break in between.
1049 fseek (s->fpin, -l, SEEK_CUR);
1056 last_quoted = quoted;
1061 * We are in the beginning of a new line. Determine quote level
1062 * and indentation prefix
1064 for (quoted = 0; line[quoted] == '>'; quoted++)
1067 cont = line + quoted;
1069 /* undo space stuffing */
1073 /* If there is an indentation, record it. */
1074 cont = flowed_skip_indent (indent, cont);
1075 i_add = flowed_visual_strlen (indent, quoted + add);
1080 * This is just the tail of some over-long line. Keep
1081 * indentation and quote levels. Don't unstuff.
1086 /* If we have a change in quoting depth, wrap. */
1088 if (col && last_quoted != quoted && last_quoted >= 0)
1090 state_putc ('\n', s);
1104 /* try to find a point for word wrapping */
1107 l = flowed_visual_strlen (cont, quoted + i_add + add + col);
1108 rl = mutt_strlen (cont);
1109 if (quoted + i_add + add + col + l > flowed_max)
1113 for (tmpcol = quoted + i_add + add + col, t = cont;
1114 *t && tmpcol < flowed_max; t++)
1116 if (*t == ' ' || *t == '\t')
1119 tmpcol = (tmpcol & ~7) + 8;
1131 /* We seem to be desperate. Get me a new line, and retry. */
1132 if (!tail && (quoted + add + col + i_add + l > flowed_max) && col)
1134 state_putc ('\n', s);
1139 /* Detect soft line breaks. */
1140 if (!soft && ascii_strcmp (cont, "-- "))
1142 lc = strrchr (cont, ' ');
1143 if (lc && lc[1] == '\0')
1148 * If we are in the beginning of an output line, do quoting
1151 * We have to temporarily assemble the line since display
1152 * stuffing (i.e., turning off quote coloring) may depend on
1153 * the line's actual content. You never know what people put
1154 * into their regular expressions.
1158 char tmp[LONG_STRING];
1159 snprintf (tmp, sizeof (tmp), "%s%s", indent, cont);
1161 flowed_quote (s, quoted);
1162 flowed_stuff (s, tmp, quoted + add);
1164 state_puts (indent, s);
1167 /* output the text */
1168 state_puts (cont, s);
1169 col += flowed_visual_strlen (cont, quoted + i_add + add + col);
1171 /* possibly indicate a soft line break */
1174 state_putc (' ', s);
1179 * Wrap if this display line corresponds to a
1180 * text line. Don't wrap if we changed the line.
1182 if (!soft || (!actually_wrap && full))
1184 state_putc ('\n', s);
1192 state_putc ('\n', s);
1197 static int get_quote_level(char * line) {
1199 for (quoted = 0; line[quoted] == '>'; quoted++);
1203 static void print_flowed_line(char * line, STATE *s,int ql) {
1205 char * pos, * oldpos;
1207 int len = strlen(line);
1210 fprintf(stderr,"prefix = `%s'\n",s->prefix);
1212 width = COLS - WrapMargin - ql - 1;
1213 if (option(OPTSTUFFQUOTED))
1218 /* fprintf(stderr,"print_flowed_line will print `%s' with ql = %d\n",line,ql); */
1220 if (strlen(line)==0) {
1222 state_puts(s->prefix,s);
1223 for (i=0;i<ql;++i) state_putc('>',s);
1224 if (option(OPTSTUFFQUOTED))
1232 if (ql>0 && isblank(*oldpos)) ++oldpos;
1234 /* fprintf(stderr,"oldpos = %p line+len = %p\n",oldpos,line+len); */
1236 for (;oldpos<line+len;pos+=width) {
1237 /* fprintf(stderr,"outer for loop\n"); */
1238 if (pos<line+len) { /* only search a new position when we're not over the end of the string w/ pos */
1239 /* fprintf(stderr,"if 1\n"); */
1241 /* fprintf(stderr,"if 2: good luck! found a space\n"); */
1245 /* fprintf(stderr,"if 2: else\n"); */
1247 while (pos>=oldpos && !isspace(*pos)) {
1248 /* fprintf(stderr,"pos(%p) > oldpos(%p)\n",pos,oldpos); */
1252 /* fprintf(stderr,"wow, no space found, searching the other direction\n"); */
1254 while (pos < line+len && *pos && !isspace(*pos)) {
1255 /* fprintf(stderr,"pos(%p) < line+len(%p)\n",pos,line+len); */
1258 /* fprintf(stderr,"found a space pos = %p\n",pos); */
1264 /* fprintf(stderr,"if 1 else\n"); */
1267 state_puts(s->prefix,s);
1270 if (option(OPTSTUFFQUOTED) && (ql>0 || s->prefix)) state_putc(' ',s);
1271 state_puts(oldpos,s);
1272 /* fprintf(stderr,"print_flowed_line: `%s'\n",oldpos); */
1278 /*state_puts(line,s);
1279 state_putc('\n',s);*/
1282 static void text_plain_flowed_handler (BODY *a, STATE *s)
1284 int bytes = a->length;
1285 char buf[LONG_STRING];
1286 char * curline = strdup("");
1288 unsigned int curline_len = 1;
1289 unsigned int quotelevel = 0, newql = 0;
1290 int append_next_line = 0;
1293 while (bytes > 0 && fgets(buf,sizeof(buf),s->fpin)) {
1295 /* fprintf(stderr,"read `%s'",buf); */
1296 bytes -= strlen(buf);
1298 newql = get_quote_level(buf);
1300 if ((t=strrchr(buf,'\n')) || (t=strrchr(buf,'\r'))) {
1302 if (strlen(curline)>0 && curline[strlen(curline)-1]==' ' && newql==quotelevel && strcmp(curline+quotelevel,"-- ")!=0) {
1303 if (buf[newql]==' ')
1304 curline[strlen(curline)-1] = '\0';
1306 curline = realloc(curline,curline_len+strlen(buf));
1307 if (curline_len == 1) *curline = '\0';
1308 curline_len+=strlen(buf);
1309 safe_strncat(curline,curline_len,buf+newql,strlen(buf+newql));
1314 print_flowed_line(curline,s,quotelevel);
1318 curline = realloc(curline,curline_len+strlen(buf));
1319 if (curline_len == 1) *curline = '\0';
1320 curline_len+=strlen(buf);
1321 safe_strncat(curline,curline_len,buf,strlen(buf));
1325 append_next_line = 1;
1326 /* @todo: add handling of very long lines */
1330 print_flowed_line(curline,s,quotelevel);
1339 #define TXTENRICHED 3
1341 static void alternative_handler (BODY *a, STATE *s)
1343 BODY *choice = NULL;
1350 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1351 a->encoding == ENCUUENCODED)
1355 fstat (fileno (s->fpin), &st);
1356 b = mutt_new_body ();
1357 b->length = (long) st.st_size;
1358 b->parts = mutt_parse_multipart (s->fpin,
1359 mutt_get_parameter ("boundary", a->parameter),
1360 (long) st.st_size, ascii_strcasecmp ("digest", a->subtype) == 0);
1367 /* First, search list of prefered types */
1368 t = AlternativeOrderList;
1369 while (t && !choice)
1372 int btlen; /* length of basetype */
1373 int wild; /* do we have a wildcard to match all subtypes? */
1375 c = strchr (t->data, '/');
1378 wild = (c[1] == '*' && c[2] == 0);
1379 btlen = c - t->data;
1384 btlen = mutt_strlen (t->data);
1393 const char *bt = TYPE(b);
1394 if (!ascii_strncasecmp (bt, t->data, btlen) && bt[btlen] == 0)
1396 /* the basetype matches */
1397 if (wild || !ascii_strcasecmp (t->data + btlen + 1, b->subtype))
1407 /* Next, look for an autoviewable type */
1416 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
1417 if (mutt_is_autoview (b, buf))
1419 rfc1524_entry *entry = rfc1524_new_entry ();
1421 if (rfc1524_mailcap_lookup (b, buf, entry, M_AUTOVIEW))
1425 rfc1524_free_entry (&entry);
1431 /* Then, look for a text entry */
1440 if (b->type == TYPETEXT)
1442 if (! ascii_strcasecmp ("plain", b->subtype) && type <= TXTPLAIN)
1447 else if (! ascii_strcasecmp ("enriched", b->subtype) && type <= TXTENRICHED)
1452 else if (! ascii_strcasecmp ("html", b->subtype) && type <= TXTHTML)
1462 /* Finally, look for other possibilities */
1471 if (mutt_can_decode (b))
1479 if (s->flags & M_DISPLAY && !option (OPTWEED))
1481 fseek (s->fpin, choice->hdr_offset, 0);
1482 mutt_copy_bytes(s->fpin, s->fpout, choice->offset-choice->hdr_offset);
1484 mutt_body_handler (choice, s);
1486 else if (s->flags & M_DISPLAY)
1488 /* didn't find anything that we could display! */
1489 state_mark_attach (s);
1490 state_puts(_("[-- Error: Could not display any parts of Multipart/Alternative! --]\n"), s);
1497 /* handles message/rfc822 body parts */
1498 void message_handler (BODY *a, STATE *s)
1504 off_start = ftell (s->fpin);
1505 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1506 a->encoding == ENCUUENCODED)
1508 fstat (fileno (s->fpin), &st);
1509 b = mutt_new_body ();
1510 b->length = (long) st.st_size;
1511 b->parts = mutt_parse_messageRFC822 (s->fpin, b);
1518 mutt_copy_hdr (s->fpin, s->fpout, off_start, b->parts->offset,
1519 (((s->flags & M_WEED) || ((s->flags & (M_DISPLAY|M_PRINTING)) && option (OPTWEED))) ? (CH_WEED | CH_REORDER) : 0) |
1520 (s->prefix ? CH_PREFIX : 0) | CH_DECODE | CH_FROM, s->prefix);
1523 state_puts (s->prefix, s);
1524 state_putc ('\n', s);
1526 mutt_body_handler (b->parts, s);
1529 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1530 a->encoding == ENCUUENCODED)
1531 mutt_free_body (&b);
1534 /* returns 1 if decoding the attachment will produce output */
1535 int mutt_can_decode (BODY *a)
1539 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
1540 if (mutt_is_autoview (a, type))
1541 return (rfc1524_mailcap_lookup (a, type, NULL, M_AUTOVIEW));
1542 else if (a->type == TYPETEXT)
1544 else if (a->type == TYPEMESSAGE)
1546 else if (a->type == TYPEMULTIPART)
1552 if (ascii_strcasecmp (a->subtype, "signed") == 0 ||
1553 ascii_strcasecmp (a->subtype, "encrypted") == 0)
1557 for (p = a->parts; p; p = p->next)
1559 if (mutt_can_decode (p))
1564 else if (WithCrypto && a->type == TYPEAPPLICATION)
1566 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp(a))
1568 if ((WithCrypto & APPLICATION_SMIME) && mutt_is_application_smime(a))
1575 void multipart_handler (BODY *a, STATE *s)
1582 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1583 a->encoding == ENCUUENCODED)
1585 fstat (fileno (s->fpin), &st);
1586 b = mutt_new_body ();
1587 b->length = (long) st.st_size;
1588 b->parts = mutt_parse_multipart (s->fpin,
1589 mutt_get_parameter ("boundary", a->parameter),
1590 (long) st.st_size, ascii_strcasecmp ("digest", a->subtype) == 0);
1595 for (p = b->parts, count = 1; p; p = p->next, count++)
1597 if (s->flags & M_DISPLAY)
1599 state_mark_attach (s);
1600 state_printf (s, _("[-- Attachment #%d"), count);
1601 if (p->description || p->filename || p->form_name)
1603 state_puts (": ", s);
1604 state_puts (p->description ? p->description :
1605 p->filename ? p->filename : p->form_name, s);
1607 state_puts (" --]\n", s);
1609 mutt_pretty_size (length, sizeof (length), p->length);
1611 state_mark_attach (s);
1612 state_printf (s, _("[-- Type: %s/%s, Encoding: %s, Size: %s --]\n"),
1613 TYPE (p), p->subtype, ENCODING (p->encoding), length);
1614 if (!option (OPTWEED))
1616 fseek (s->fpin, p->hdr_offset, 0);
1617 mutt_copy_bytes(s->fpin, s->fpout, p->offset-p->hdr_offset);
1620 state_putc ('\n', s);
1624 if (p->description && mutt_can_decode (p))
1625 state_printf (s, "Content-Description: %s\n", p->description);
1628 state_printf(s, "%s: \n", p->form_name);
1631 mutt_body_handler (p, s);
1632 state_putc ('\n', s);
1633 if ((s->flags & M_REPLYING)
1634 && (option (OPTINCLUDEONLYFIRST)) && (s->flags & M_FIRSTDONE))
1638 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1639 a->encoding == ENCUUENCODED)
1640 mutt_free_body (&b);
1643 void autoview_handler (BODY *a, STATE *s)
1645 rfc1524_entry *entry = rfc1524_new_entry ();
1646 char buffer[LONG_STRING];
1648 char command[LONG_STRING];
1649 char tempfile[_POSIX_PATH_MAX] = "";
1657 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
1658 rfc1524_mailcap_lookup (a, type, entry, M_AUTOVIEW);
1660 fname = safe_strdup (a->filename);
1661 mutt_sanitize_filename (fname, 1);
1662 rfc1524_expand_filename (entry->nametemplate, fname, tempfile, sizeof (tempfile));
1667 strfcpy (command, entry->command, sizeof (command));
1669 /* rfc1524_expand_command returns 0 if the file is required */
1670 piped = rfc1524_expand_command (a, tempfile, type, command, sizeof (command));
1672 if (s->flags & M_DISPLAY)
1674 state_mark_attach (s);
1675 state_printf (s, _("[-- Autoview using %s --]\n"), command);
1676 mutt_message(_("Invoking autoview command: %s"),command);
1679 if ((fpin = safe_fopen (tempfile, "w+")) == NULL)
1681 mutt_perror ("fopen");
1682 rfc1524_free_entry (&entry);
1686 mutt_copy_bytes (s->fpin, fpin, a->length);
1690 safe_fclose (&fpin);
1691 thepid = mutt_create_filter (command, NULL, &fpout, &fperr);
1698 thepid = mutt_create_filter_fd (command, NULL, &fpout, &fperr,
1699 fileno(fpin), -1, -1);
1704 mutt_perror _("Can't create filter");
1705 if (s->flags & M_DISPLAY)
1707 state_mark_attach (s);
1708 state_printf (s, _("[-- Can't run %s. --]\n"), command);
1715 while (fgets (buffer, sizeof(buffer), fpout) != NULL)
1717 state_puts (s->prefix, s);
1718 state_puts (buffer, s);
1720 /* check for data on stderr */
1721 if (fgets (buffer, sizeof(buffer), fperr))
1723 if (s->flags & M_DISPLAY)
1725 state_mark_attach (s);
1726 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1729 state_puts (s->prefix, s);
1730 state_puts (buffer, s);
1731 while (fgets (buffer, sizeof(buffer), fperr) != NULL)
1733 state_puts (s->prefix, s);
1734 state_puts (buffer, s);
1740 mutt_copy_stream (fpout, s->fpout);
1741 /* Check for stderr messages */
1742 if (fgets (buffer, sizeof(buffer), fperr))
1744 if (s->flags & M_DISPLAY)
1746 state_mark_attach (s);
1747 state_printf (s, _("[-- Autoview stderr of %s --]\n"),
1751 state_puts (buffer, s);
1752 mutt_copy_stream (fperr, s->fpout);
1757 safe_fclose (&fpout);
1758 safe_fclose (&fperr);
1760 mutt_wait_filter (thepid);
1762 safe_fclose (&fpin);
1764 mutt_unlink (tempfile);
1766 if (s->flags & M_DISPLAY)
1767 mutt_clear_error ();
1769 rfc1524_free_entry (&entry);
1772 static void external_body_handler (BODY *b, STATE *s)
1774 const char *access_type;
1775 const char *expiration;
1778 access_type = mutt_get_parameter ("access-type", b->parameter);
1781 if (s->flags & M_DISPLAY)
1783 state_mark_attach (s);
1784 state_puts (_("[-- Error: message/external-body has no access-type parameter --]\n"), s);
1789 expiration = mutt_get_parameter ("expiration", b->parameter);
1791 expire = mutt_parse_date (expiration, NULL);
1795 if (!ascii_strcasecmp (access_type, "x-mutt-deleted"))
1797 if (s->flags & (M_DISPLAY|M_PRINTING))
1800 char pretty_size[10];
1802 state_mark_attach (s);
1803 state_printf (s, _("[-- This %s/%s attachment "),
1804 TYPE(b->parts), b->parts->subtype);
1805 length = mutt_get_parameter ("length", b->parameter);
1808 mutt_pretty_size (pretty_size, sizeof (pretty_size),
1809 strtol (length, NULL, 10));
1810 state_printf (s, _("(size %s bytes) "), pretty_size);
1812 state_puts (_("has been deleted --]\n"), s);
1816 state_mark_attach (s);
1817 state_printf (s, _("[-- on %s --]\n"), expiration);
1819 if (b->parts->filename)
1821 state_mark_attach (s);
1822 state_printf (s, _("[-- name: %s --]\n"), b->parts->filename);
1825 mutt_copy_hdr (s->fpin, s->fpout, ftell (s->fpin), b->parts->offset,
1826 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1830 else if(expiration && expire < time(NULL))
1832 if (s->flags & M_DISPLAY)
1834 state_mark_attach (s);
1835 state_printf (s, _("[-- This %s/%s attachment is not included, --]\n"),
1836 TYPE(b->parts), b->parts->subtype);
1837 state_attach_puts (_("[-- and the indicated external source has --]\n"
1838 "[-- expired. --]\n"), s);
1840 mutt_copy_hdr(s->fpin, s->fpout, ftell (s->fpin), b->parts->offset,
1841 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1847 if (s->flags & M_DISPLAY)
1849 state_mark_attach (s);
1851 _("[-- This %s/%s attachment is not included, --]\n"),
1852 TYPE (b->parts), b->parts->subtype);
1853 state_mark_attach (s);
1855 _("[-- and the indicated access-type %s is unsupported --]\n"),
1857 mutt_copy_hdr (s->fpin, s->fpout, ftell (s->fpin), b->parts->offset,
1858 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1864 void mutt_decode_attachment (BODY *b, STATE *s)
1866 int istext = mutt_is_text_part (b);
1867 iconv_t cd = (iconv_t)(-1);
1871 if (istext && s->flags & M_CHARCONV)
1873 char *charset = mutt_get_parameter ("charset", b->parameter);
1874 if (charset && Charset)
1875 cd = mutt_iconv_open (Charset, charset, M_ICONV_HOOK_FROM);
1878 fseek (s->fpin, b->offset, 0);
1879 switch (b->encoding)
1881 case ENCQUOTEDPRINTABLE:
1882 mutt_decode_quoted (s, b->length, istext, cd);
1885 mutt_decode_base64 (s, b->length, istext, cd);
1888 mutt_decode_uuencoded (s, b->length, istext, cd);
1891 mutt_decode_xbit (s, b->length, istext, cd);
1895 if (cd != (iconv_t)(-1))
1899 void mutt_body_handler (BODY *b, STATE *s)
1904 char tempfile[_POSIX_PATH_MAX];
1905 handler_t handler = NULL;
1907 size_t tmplength = 0;
1910 int oflags = s->flags;
1912 /* first determine which handler to use to process this part */
1914 snprintf (type, sizeof (type), "%s/%s", TYPE (b), b->subtype);
1915 if (mutt_is_autoview (b, type))
1917 rfc1524_entry *entry = rfc1524_new_entry ();
1919 if (rfc1524_mailcap_lookup (b, type, entry, M_AUTOVIEW))
1921 handler = autoview_handler;
1922 s->flags &= ~M_CHARCONV;
1924 rfc1524_free_entry (&entry);
1926 else if (b->type == TYPETEXT)
1928 if (ascii_strcasecmp ("plain", b->subtype) == 0)
1930 /* avoid copying this part twice since removing the transfer-encoding is
1931 * the only operation needed.
1933 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b))
1934 handler = crypt_pgp_application_pgp_handler;
1935 else if (ascii_strcasecmp ("flowed", mutt_get_parameter ("format", b->parameter)) == 0)
1936 handler = text_plain_flowed_handler;
1940 else if (ascii_strcasecmp ("enriched", b->subtype) == 0)
1941 handler = text_enriched_handler;
1942 else /* text body type without a handler */
1945 else if (b->type == TYPEMESSAGE)
1947 if(mutt_is_message_type(b->type, b->subtype))
1948 handler = message_handler;
1949 else if (!ascii_strcasecmp ("delivery-status", b->subtype))
1951 else if (!ascii_strcasecmp ("external-body", b->subtype))
1952 handler = external_body_handler;
1954 else if (b->type == TYPEMULTIPART)
1958 if (ascii_strcasecmp ("alternative", b->subtype) == 0)
1959 handler = alternative_handler;
1960 else if (WithCrypto && ascii_strcasecmp ("signed", b->subtype) == 0)
1962 p = mutt_get_parameter ("protocol", b->parameter);
1965 mutt_error _("Error: multipart/signed has no protocol.");
1966 else if (s->flags & M_VERIFY)
1967 handler = mutt_signed_handler;
1969 else if ((WithCrypto & APPLICATION_PGP)
1970 && mutt_strcasecmp ("encrypted", b->subtype) == 0)
1972 p = mutt_get_parameter ("protocol", b->parameter);
1975 mutt_error _("Error: multipart/encrypted has no protocol parameter!");
1976 else if (ascii_strcasecmp ("application/pgp-encrypted", p) == 0)
1977 handler = crypt_pgp_encrypted_handler;
1981 handler = multipart_handler;
1983 else if (WithCrypto && b->type == TYPEAPPLICATION)
1985 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b))
1986 handler = crypt_pgp_application_pgp_handler;
1987 if ((WithCrypto & APPLICATION_SMIME) && mutt_is_application_smime(b))
1988 handler = crypt_smime_application_smime_handler;
1992 if (plaintext || handler)
1994 fseek (s->fpin, b->offset, 0);
1996 /* see if we need to decode this part before processing it */
1997 if (b->encoding == ENCBASE64 || b->encoding == ENCQUOTEDPRINTABLE ||
1998 b->encoding == ENCUUENCODED || plaintext ||
1999 mutt_is_text_part (b)) /* text subtypes may
2001 * set conversion even
2002 * with 8bit encoding.
2005 int origType = b->type;
2006 char *savePrefix = NULL;
2010 /* decode to a tempfile, saving the original destination */
2012 mutt_mktemp (tempfile);
2013 if ((s->fpout = safe_fopen (tempfile, "w")) == NULL)
2015 mutt_error _("Unable to open temporary file!");
2018 /* decoding the attachment changes the size and offset, so save a copy
2019 * of the "real" values now, and restore them after processing
2021 tmplength = b->length;
2022 tmpoffset = b->offset;
2024 /* if we are decoding binary bodies, we don't want to prefix each
2025 * line with the prefix or else the data will get corrupted.
2027 savePrefix = s->prefix;
2035 mutt_decode_attachment (b, s);
2039 b->length = ftell (s->fpout);
2043 /* restore final destination and substitute the tempfile for input */
2046 s->fpin = safe_fopen (tempfile, "r");
2049 /* restore the prefix */
2050 s->prefix = savePrefix;
2056 /* process the (decoded) body part */
2063 b->length = tmplength;
2064 b->offset = tmpoffset;
2066 /* restore the original source stream */
2071 s->flags |= M_FIRSTDONE;
2073 else if (s->flags & M_DISPLAY)
2075 state_mark_attach (s);
2076 state_printf (s, _("[-- %s/%s is unsupported "), TYPE (b), b->subtype);
2077 if (!option (OPTVIEWATTACH))
2079 if (km_expand_key (type, sizeof(type),
2080 km_find_func (MENU_PAGER, OP_VIEW_ATTACHMENTS)))
2081 fprintf (s->fpout, _("(use '%s' to view this part)"), type);
2083 fputs (_("(need 'view-attachments' bound to key!)"), s->fpout);
2085 fputs (" --]\n", s->fpout);
2089 s->flags = oflags | (s->flags & M_FIRSTDONE);