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.
31 #include "mutt_curses.h"
37 #include "mutt_crypt.h"
41 #define BUFI_SIZE 1000
42 #define BUFO_SIZE 2000
45 typedef void handler_f (BODY *, STATE *);
46 typedef handler_f *handler_t;
48 int Index_hex[128] = {
49 -1,-1,-1,-1, -1,-1,-1,-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,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
52 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1,
53 -1,10,11,12, 13,14,15,-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,10,11,12, 13,14,15,-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
60 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
61 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
62 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
63 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
64 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
65 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
66 -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
67 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
70 static void state_prefix_put (const char *d, size_t dlen, STATE *s)
74 state_prefix_putc (*d++, s);
76 fwrite (d, dlen, 1, s->fpout);
79 void mutt_convert_to_state(iconv_t cd, char *bufi, size_t *l, STATE *s)
88 if (cd != (iconv_t)(-1))
90 ob = bufo, obl = sizeof (bufo);
91 iconv (cd, 0, 0, &ob, &obl);
93 state_prefix_put (bufo, ob - bufo, s);
95 if (Quotebuf[0] != '\0')
96 state_prefix_putc ('\n', s);
100 if (cd == (iconv_t)(-1))
102 state_prefix_put (bufi, *l, s);
110 ob = bufo, obl = sizeof (bufo);
111 mutt_iconv (cd, &ib, &ibl, &ob, &obl, 0, "?");
114 state_prefix_put (bufo, ob - bufo, s);
116 memmove (bufi, ib, ibl);
120 void mutt_decode_xbit (STATE *s, long len, int istext, iconv_t cd)
123 char bufi[BUFI_SIZE];
130 while ((c = fgetc(s->fpin)) != EOF && len--)
134 if((ch = fgetc(s->fpin)) == '\n')
144 if (l == sizeof (bufi))
145 mutt_convert_to_state (cd, bufi, &l, s);
148 mutt_convert_to_state (cd, bufi, &l, s);
149 mutt_convert_to_state (cd, 0, 0, s);
151 state_reset_prefix (s);
154 mutt_copy_bytes (s->fpin, s->fpout, len);
157 static int qp_decode_triple (char *s, char *d)
159 /* soft line break */
160 if (*s == '=' && !(*(s+1)))
163 /* quoted-printable triple */
165 isxdigit ((unsigned char) *(s+1)) &&
166 isxdigit ((unsigned char) *(s+2)))
168 *d = (hexval (*(s+1)) << 4) | hexval (*(s+2));
176 static void qp_decode_line (char *dest, char *src, size_t *l,
185 /* decode the line */
187 for (d = dest, s = src; *s;)
189 switch ((kind = qp_decode_triple (s, &c)))
191 case 0: *d++ = c; s += 3; break; /* qp triple */
192 case -1: *d++ = *s++; break; /* single character */
193 case 1: soft = 1; s++; break; /* soft line break */
197 if (!soft && last == '\n')
205 * Decode an attachment encoded with quoted-printable.
207 * Why doesn't this overflow any buffers? First, it's guaranteed
208 * that the length of a line grows when you _en_-code it to
209 * quoted-printable. That means that we always can store the
210 * result in a buffer of at most the _same_ size.
212 * Now, we don't special-case if the line we read with fgets()
213 * isn't terminated. We don't care about this, since STRING > 78,
214 * so corrupted input will just be corrupted a bit more. That
215 * implies that STRING+1 bytes are always sufficient to store the
216 * result of qp_decode_line.
218 * Finally, at soft line breaks, some part of a multibyte character
219 * may have been left over by mutt_convert_to_state(). This shouldn't
220 * be more than 6 characters, so STRING + 7 should be sufficient
221 * memory to store the decoded data.
223 * Just to make sure that I didn't make some off-by-one error
224 * above, we just use STRING*2 for the target buffer's size.
228 void mutt_decode_quoted (STATE *s, long len, int istext, iconv_t cd)
231 char decline[2*STRING];
233 size_t linelen; /* number of input bytes in `line' */
236 int last; /* store the last character in the input line */
246 * It's ok to use a fixed size buffer for input, even if the line turns
247 * out to be longer than this. Just process the line in chunks. This
248 * really shouldn't happen according the MIME spec, since Q-P encoded
249 * lines are at most 76 characters, but we should be liberal about what
252 if (fgets (line, MIN ((ssize_t)sizeof (line), len + 1), s->fpin) == NULL)
255 linelen = strlen(line);
259 * inspect the last character we read so we can tell if we got the
262 last = linelen ? line[linelen - 1] : 0;
264 /* chop trailing whitespace if we got the full line */
267 while (linelen > 0 && ISSPACE (line[linelen-1]))
272 /* decode and do character set conversion */
273 qp_decode_line (decline + l, line, &l3, last);
275 mutt_convert_to_state (cd, decline, &l, s);
278 mutt_convert_to_state (cd, 0, 0, s);
279 state_reset_prefix(s);
282 void mutt_decode_base64 (STATE *s, long len, int istext, iconv_t cd)
285 int c1, c2, c3, c4, ch, cr = 0, i;
286 char bufi[BUFI_SIZE];
296 for (i = 0 ; i < 4 && len > 0 ; len--)
298 if ((ch = fgetc (s->fpin)) == EOF)
300 if (ch >= 0 && ch < 128 && (base64val(ch) != -1 || ch == '='))
305 dprint (2, (debugfile, "%s:%d [mutt_decode_base64()]: "
306 "didn't get a multiple of 4 chars.\n", __FILE__, __LINE__));
310 c1 = base64val (buf[0]);
311 c2 = base64val (buf[1]);
312 ch = (c1 << 2) | (c2 >> 4);
314 if (cr && ch != '\n')
319 if (istext && ch == '\r')
326 c3 = base64val (buf[2]);
327 ch = ((c2 & 0xf) << 4) | (c3 >> 2);
329 if (cr && ch != '\n')
334 if (istext && ch == '\r')
339 if (buf[3] == '=') break;
340 c4 = base64val (buf[3]);
341 ch = ((c3 & 0x3) << 6) | c4;
343 if (cr && ch != '\n')
347 if (istext && ch == '\r')
352 if (l + 8 >= sizeof (bufi))
353 mutt_convert_to_state (cd, bufi, &l, s);
356 if (cr) bufi[l++] = '\r';
358 mutt_convert_to_state (cd, bufi, &l, s);
359 mutt_convert_to_state (cd, 0, 0, s);
361 state_reset_prefix(s);
364 unsigned char decode_byte (char ch)
371 void mutt_decode_uuencoded (STATE *s, long len, int istext, iconv_t cd)
373 char tmps[SHORT_STRING];
374 char linelen, c, l, out;
376 char bufi[BUFI_SIZE];
384 if ((fgets(tmps, sizeof(tmps), s->fpin)) == NULL)
386 len -= mutt_strlen(tmps);
387 if ((!mutt_strncmp (tmps, "begin", 5)) && ISSPACE (tmps[5]))
392 if ((fgets(tmps, sizeof(tmps), s->fpin)) == NULL)
394 len -= mutt_strlen(tmps);
395 if (!mutt_strncmp (tmps, "end", 3))
398 linelen = decode_byte (*pt);
400 for (c = 0; c < linelen;)
402 for (l = 2; l <= 6; l += 2)
404 out = decode_byte (*pt) << l;
406 out |= (decode_byte (*pt) >> (6 - l));
412 mutt_convert_to_state (cd, bufi, &k, s);
417 mutt_convert_to_state (cd, bufi, &k, s);
418 mutt_convert_to_state (cd, 0, 0, s);
420 state_reset_prefix(s);
423 /* ----------------------------------------------------------------------------
424 * A (not so) minimal implementation of RFC1563.
427 #define IndentSize (4)
429 enum { RICH_PARAM=0, RICH_BOLD, RICH_UNDERLINE, RICH_ITALIC, RICH_NOFILL,
430 RICH_INDENT, RICH_INDENT_RIGHT, RICH_EXCERPT, RICH_CENTER, RICH_FLUSHLEFT,
431 RICH_FLUSHRIGHT, RICH_COLOR, RICH_LAST_TAG };
434 const char *tag_name;
437 { "param", RICH_PARAM },
438 { "bold", RICH_BOLD },
439 { "italic", RICH_ITALIC },
440 { "underline", RICH_UNDERLINE },
441 { "nofill", RICH_NOFILL },
442 { "excerpt", RICH_EXCERPT },
443 { "indent", RICH_INDENT },
444 { "indentright", RICH_INDENT_RIGHT },
445 { "center", RICH_CENTER },
446 { "flushleft", RICH_FLUSHLEFT },
447 { "flushright", RICH_FLUSHRIGHT },
448 { "flushboth", RICH_FLUSHLEFT },
449 { "color", RICH_COLOR },
450 { "x-color", RICH_COLOR },
454 struct enriched_state
468 int tag_level[RICH_LAST_TAG];
473 static void enriched_wrap (struct enriched_state *stte)
480 if (stte->tag_level[RICH_CENTER] || stte->tag_level[RICH_FLUSHRIGHT])
482 /* Strip trailing white space */
483 size_t y = stte->line_used - 1;
485 while (y && ISSPACE (stte->line[y]))
487 stte->line[y] = '\0';
492 if (stte->tag_level[RICH_CENTER])
494 /* Strip leading whitespace */
497 while (stte->line[y] && ISSPACE (stte->line[y]))
503 for (z = y ; z <= stte->line_used; z++)
505 stte->line[z - y] = stte->line[z];
509 stte->line_used -= y;
514 extra = stte->WrapMargin - stte->line_len - stte->indent_len -
515 (stte->tag_level[RICH_INDENT_RIGHT] * IndentSize);
518 if (stte->tag_level[RICH_CENTER])
523 state_putc (' ', stte->s);
527 else if (stte->tag_level[RICH_FLUSHRIGHT])
532 state_putc (' ', stte->s);
537 state_puts (stte->line, stte->s);
540 state_putc ('\n', stte->s);
541 stte->line[0] = '\0';
544 stte->indent_len = 0;
547 state_puts (stte->s->prefix, stte->s);
548 stte->indent_len += mutt_strlen (stte->s->prefix);
551 if (stte->tag_level[RICH_EXCERPT])
553 x = stte->tag_level[RICH_EXCERPT];
558 state_puts (stte->s->prefix, stte->s);
559 stte->indent_len += mutt_strlen (stte->s->prefix);
563 state_puts ("> ", stte->s);
564 stte->indent_len += mutt_strlen ("> ");
570 stte->indent_len = 0;
571 if (stte->tag_level[RICH_INDENT])
573 x = stte->tag_level[RICH_INDENT] * IndentSize;
574 stte->indent_len += x;
577 state_putc (' ', stte->s);
583 static void enriched_flush (struct enriched_state *stte, int wrap)
585 if (!stte->tag_level[RICH_NOFILL] && (stte->line_len + stte->word_len >
586 (stte->WrapMargin - (stte->tag_level[RICH_INDENT_RIGHT] * IndentSize) -
588 enriched_wrap (stte);
592 stte->buffer[stte->buff_used] = '\0';
593 stte->line_used += stte->buff_used;
594 if (stte->line_used > stte->line_max)
596 stte->line_max = stte->line_used;
597 safe_realloc (&stte->line, stte->line_max + 1);
599 strcat (stte->line, stte->buffer); /* __STRCAT_CHECKED__ */
600 stte->line_len += stte->word_len;
609 static void enriched_putc (int c, struct enriched_state *stte)
611 if (stte->tag_level[RICH_PARAM])
613 if (stte->tag_level[RICH_COLOR])
615 if (stte->param_used + 1 >= stte->param_len)
616 safe_realloc (&stte->param, (stte->param_len += STRING));
618 stte->param[stte->param_used++] = c;
620 return; /* nothing to do */
623 /* see if more space is needed (plus extra for possible rich characters) */
624 if (stte->buff_len < stte->buff_used + 3)
626 stte->buff_len += LONG_STRING;
627 safe_realloc (&stte->buffer, stte->buff_len + 1);
630 if ((!stte->tag_level[RICH_NOFILL] && ISSPACE (c)) || c == '\0' )
633 stte->word_len += 8 - (stte->line_len + stte->word_len) % 8;
637 stte->buffer[stte->buff_used++] = c;
638 enriched_flush (stte, 0);
642 if (stte->s->flags & M_DISPLAY)
644 if (stte->tag_level[RICH_BOLD])
646 stte->buffer[stte->buff_used++] = c;
647 stte->buffer[stte->buff_used++] = '\010';
648 stte->buffer[stte->buff_used++] = c;
650 else if (stte->tag_level[RICH_UNDERLINE])
653 stte->buffer[stte->buff_used++] = '_';
654 stte->buffer[stte->buff_used++] = '\010';
655 stte->buffer[stte->buff_used++] = c;
657 else if (stte->tag_level[RICH_ITALIC])
659 stte->buffer[stte->buff_used++] = c;
660 stte->buffer[stte->buff_used++] = '\010';
661 stte->buffer[stte->buff_used++] = '_';
665 stte->buffer[stte->buff_used++] = c;
670 stte->buffer[stte->buff_used++] = c;
676 static void enriched_puts (char *s, struct enriched_state *stte)
680 if (stte->buff_len < stte->buff_used + mutt_strlen(s))
682 stte->buff_len += LONG_STRING;
683 safe_realloc (&stte->buffer, stte->buff_len + 1);
688 stte->buffer[stte->buff_used++] = *c;
693 static void enriched_set_flags (const char *tag, struct enriched_state *stte)
695 const char *tagptr = tag;
701 for (i = 0, j = -1; EnrichedTags[i].tag_name; i++)
702 if (ascii_strcasecmp (EnrichedTags[i].tag_name,tagptr) == 0)
704 j = EnrichedTags[i].index;
710 if (j == RICH_CENTER || j == RICH_FLUSHLEFT || j == RICH_FLUSHRIGHT)
711 enriched_flush (stte, 1);
715 if (stte->tag_level[j]) /* make sure not to go negative */
716 stte->tag_level[j]--;
717 if ((stte->s->flags & M_DISPLAY) && j == RICH_PARAM && stte->tag_level[RICH_COLOR])
719 stte->param[stte->param_used] = '\0';
720 if (!ascii_strcasecmp(stte->param, "black"))
722 enriched_puts("\033[30m", stte);
724 else if (!ascii_strcasecmp(stte->param, "red"))
726 enriched_puts("\033[31m", stte);
728 else if (!ascii_strcasecmp(stte->param, "green"))
730 enriched_puts("\033[32m", stte);
732 else if (!ascii_strcasecmp(stte->param, "yellow"))
734 enriched_puts("\033[33m", stte);
736 else if (!ascii_strcasecmp(stte->param, "blue"))
738 enriched_puts("\033[34m", stte);
740 else if (!ascii_strcasecmp(stte->param, "magenta"))
742 enriched_puts("\033[35m", stte);
744 else if (!ascii_strcasecmp(stte->param, "cyan"))
746 enriched_puts("\033[36m", stte);
748 else if (!ascii_strcasecmp(stte->param, "white"))
750 enriched_puts("\033[37m", stte);
753 if ((stte->s->flags & M_DISPLAY) && j == RICH_COLOR)
755 enriched_puts("\033[0m", stte);
758 /* flush parameter buffer when closing the tag */
761 stte->param_used = 0;
762 stte->param[0] = '\0';
766 stte->tag_level[j]++;
768 if (j == RICH_EXCERPT)
769 enriched_flush(stte, 1);
773 void text_enriched_handler (BODY *a, STATE *s)
776 TEXT, LANGLE, TAG, BOGUS_TAG, NEWLINE, ST_EOF, DONE
779 long bytes = a->length;
780 struct enriched_state stte;
783 char tag[LONG_STRING + 1];
785 memset (&stte, 0, sizeof (stte));
787 stte.WrapMargin = ((s->flags & M_DISPLAY) ? (COLS-4) : ((COLS-4)<72)?(COLS-4):72);
788 stte.line_max = stte.WrapMargin * 4;
789 stte.line = (char *) safe_calloc (1, stte.line_max + 1);
790 stte.param = (char *) safe_calloc (1, STRING);
792 stte.param_len = STRING;
797 state_puts (s->prefix, s);
798 stte.indent_len += mutt_strlen (s->prefix);
801 while (state != DONE)
805 if (!bytes || (c = fgetc (s->fpin)) == EOF)
821 if (stte.tag_level[RICH_NOFILL])
823 enriched_flush (&stte, 1);
827 enriched_putc (' ', &stte);
833 enriched_putc (c, &stte);
840 enriched_putc (c, &stte);
849 /* Yes, fall through (it wasn't a <<, so this char is first in TAG) */
854 enriched_set_flags (tag, &stte);
857 else if (tag_len < LONG_STRING) /* ignore overly long tags */
870 enriched_flush (&stte, 1);
880 enriched_putc ('\0', &stte);
881 enriched_flush (&stte, 1);
885 case DONE: /* not reached, but gcc complains if this is absent */
890 state_putc ('\n', s); /* add a final newline */
892 FREE (&(stte.buffer));
894 FREE (&(stte.param));
898 * An implementation of RFC 2646.
900 * NOTE: This still has to be made UTF-8 aware.
904 #define FLOWED_MAX 77
906 static void flowed_quote (STATE *s, int level)
912 if (option (OPTTEXTFLOWED))
915 state_puts (s->prefix, s);
918 for (i = 0; i < level; i++)
922 static int flowed_maybe_quoted (char *cont)
924 return regexec ((regex_t *) QuoteRegexp.rx, cont, 0, NULL, 0) == 0;
927 static void flowed_stuff (STATE *s, char *cont, int level)
929 if (!option (OPTTEXTFLOWED) && !(s->flags & M_DISPLAY))
932 if (s->flags & M_DISPLAY)
935 * Hack: If we are in the beginning of the line and there is
936 * some text on the line which looks like it's quoted, turn off
937 * ANSI colors, so quote coloring doesn't affect this line.
939 if (*cont && !level && !mutt_strcmp (Pager, "builtin") && flowed_maybe_quoted (cont))
940 state_puts ("\033[0m",s);
942 else if ((!(s->flags & M_PRINTING)) &&
943 ((*cont == ' ') || (*cont == '>') || (!level && !mutt_strncmp (cont, "From ", 5))))
947 static char *flowed_skip_indent (char *prefix, char *cont)
949 for (; *cont == ' ' || *cont == '\t'; cont++)
955 static int flowed_visual_strlen (char *l, int i)
961 j += 8 - ((i + j) % 8);
970 static void text_plain_flowed_handler (BODY *a, STATE *s)
972 char line[LONG_STRING];
973 char indent[LONG_STRING];
987 int bytes = a->length;
988 int actually_wrap = 0;
1001 if ((flowed_max = FLOWED_MAX) > COLS - 3)
1002 flowed_max = COLS - 3;
1003 if (flowed_max > COLS - WrapMargin)
1004 flowed_max = COLS - WrapMargin;
1005 if (flowed_max <= 0)
1008 flowed_max = COLS - WrapMargin;
1009 if (flowed_max <= 0)
1012 fprintf(stderr,"flowed_max = %d\n",flowed_max);
1014 while (bytes > 0 && fgets (line, sizeof (line), s->fpin))
1016 bytes -= strlen (line);
1022 * If the last line wasn't fully read, this is the
1023 * tail of some line.
1025 actually_wrap = !last_full;
1027 if ((t = strrchr (line, '\r')) || (t = strrchr (line, '\n')))
1032 else if ((t = strrchr (line, ' ')) || (t = strrchr (line, '\t')))
1035 * Bad: We have a line of more than LONG_STRING characters.
1036 * (Which SHOULD NOT happen, since lines SHOULD be <= 79
1039 * Try to simulate a soft line break at a word boundary.
1040 * Handle the rest of the line next time.
1042 * Give up when we have a single word which is longer than
1043 * LONG_STRING characters. It will just be split into parts,
1044 * with a hard line break in between.
1054 fseek (s->fpin, -l, SEEK_CUR);
1061 last_quoted = quoted;
1066 * We are in the beginning of a new line. Determine quote level
1067 * and indentation prefix
1069 for (quoted = 0; line[quoted] == '>'; quoted++)
1072 cont = line + quoted;
1074 /* undo space stuffing */
1078 /* If there is an indentation, record it. */
1079 cont = flowed_skip_indent (indent, cont);
1080 i_add = flowed_visual_strlen (indent, quoted + add);
1085 * This is just the tail of some over-long line. Keep
1086 * indentation and quote levels. Don't unstuff.
1091 /* If we have a change in quoting depth, wrap. */
1093 if (col && last_quoted != quoted && last_quoted >= 0)
1095 state_putc ('\n', s);
1109 /* try to find a point for word wrapping */
1112 l = flowed_visual_strlen (cont, quoted + i_add + add + col);
1113 rl = mutt_strlen (cont);
1114 if (quoted + i_add + add + col + l > flowed_max)
1118 for (tmpcol = quoted + i_add + add + col, t = cont;
1119 *t && tmpcol < flowed_max; t++)
1121 if (*t == ' ' || *t == '\t')
1124 tmpcol = (tmpcol & ~7) + 8;
1136 /* We seem to be desperate. Get me a new line, and retry. */
1137 if (!tail && (quoted + add + col + i_add + l > flowed_max) && col)
1139 state_putc ('\n', s);
1144 /* Detect soft line breaks. */
1145 if (!soft && ascii_strcmp (cont, "-- "))
1147 lc = strrchr (cont, ' ');
1148 if (lc && lc[1] == '\0')
1153 * If we are in the beginning of an output line, do quoting
1156 * We have to temporarily assemble the line since display
1157 * stuffing (i.e., turning off quote coloring) may depend on
1158 * the line's actual content. You never know what people put
1159 * into their regular expressions.
1163 char tmp[LONG_STRING];
1164 snprintf (tmp, sizeof (tmp), "%s%s", indent, cont);
1166 flowed_quote (s, quoted);
1167 flowed_stuff (s, tmp, quoted + add);
1169 state_puts (indent, s);
1172 /* output the text */
1173 state_puts (cont, s);
1174 col += flowed_visual_strlen (cont, quoted + i_add + add + col);
1176 /* possibly indicate a soft line break */
1179 state_putc (' ', s);
1184 * Wrap if this display line corresponds to a
1185 * text line. Don't wrap if we changed the line.
1187 if (!soft || (!actually_wrap && full))
1189 state_putc ('\n', s);
1197 state_putc ('\n', s);
1202 static int get_quote_level(char * line) {
1204 for (quoted = 0; line[quoted] == '>'; quoted++);
1208 static void print_flowed_line(char * line, STATE *s,int ql) {
1210 char * pos, * oldpos;
1212 int len = strlen(line);
1215 width = COLS - WrapMargin - ql - 1;
1216 if (option(OPTSTUFFQUOTED))
1221 /* fprintf(stderr,"print_flowed_line will print `%s' with ql = %d\n",line,ql); */
1223 if (strlen(line)==0) {
1225 state_puts(s->prefix,s);
1226 for (i=0;i<ql;++i) state_putc('>',s);
1227 if (option(OPTSTUFFQUOTED))
1235 if (ql>0 && ISBLANK(*oldpos)) ++oldpos;
1237 /* fprintf(stderr,"oldpos = %p line+len = %p\n",oldpos,line+len); */
1239 for (;oldpos<line+len;pos+=width) {
1240 /* fprintf(stderr,"outer for loop\n"); */
1241 if (pos<line+len) { /* only search a new position when we're not over the end of the string w/ pos */
1242 /* fprintf(stderr,"if 1\n"); */
1244 /* fprintf(stderr,"if 2: good luck! found a space\n"); */
1248 /* fprintf(stderr,"if 2: else\n"); */
1250 while (pos>=oldpos && !isspace(*pos)) {
1251 /* fprintf(stderr,"pos(%p) > oldpos(%p)\n",pos,oldpos); */
1255 /* fprintf(stderr,"wow, no space found, searching the other direction\n"); */
1257 while (pos < line+len && *pos && !isspace(*pos)) {
1258 /* fprintf(stderr,"pos(%p) < line+len(%p)\n",pos,line+len); */
1261 /* fprintf(stderr,"found a space pos = %p\n",pos); */
1267 /* fprintf(stderr,"if 1 else\n"); */
1270 state_puts(s->prefix,s);
1273 if (option(OPTSTUFFQUOTED) && (ql>0 || s->prefix)) state_putc(' ',s);
1274 state_puts(oldpos,s);
1275 /* fprintf(stderr,"print_flowed_line: `%s'\n",oldpos); */
1281 /*state_puts(line,s);
1282 state_putc('\n',s);*/
1285 static void text_plain_flowed_handler (BODY *a, STATE *s)
1287 int bytes = a->length;
1288 char buf[LONG_STRING];
1289 char * curline = strdup("");
1291 unsigned int curline_len = 1;
1292 unsigned int quotelevel = 0, newql = 0;
1293 int append_next_line = 0;
1296 while (bytes > 0 && fgets(buf,sizeof(buf),s->fpin)) {
1298 /* fprintf(stderr,"read `%s'",buf); */
1299 bytes -= strlen(buf);
1301 newql = get_quote_level(buf);
1303 if ((t=strrchr(buf,'\n')) || (t=strrchr(buf,'\r'))) {
1305 if (strlen(curline)>0 && curline[strlen(curline)-1]==' ' && newql==quotelevel && strcmp(curline+quotelevel,"-- ")!=0) {
1306 if (buf[newql]==' ')
1307 curline[strlen(curline)-1] = '\0';
1309 curline = realloc(curline,curline_len+strlen(buf));
1310 if (curline_len == 1) *curline = '\0';
1311 curline_len+=strlen(buf);
1312 safe_strncat(curline,curline_len,buf+newql,strlen(buf+newql));
1317 print_flowed_line(curline,s,quotelevel);
1321 curline = realloc(curline,curline_len+strlen(buf));
1322 if (curline_len == 1) *curline = '\0';
1323 curline_len+=strlen(buf);
1324 safe_strncat(curline,curline_len,buf,strlen(buf));
1328 append_next_line = 1;
1329 /* @todo: add handling of very long lines */
1333 print_flowed_line(curline,s,quotelevel);
1342 #define TXTENRICHED 3
1344 static void alternative_handler (BODY *a, STATE *s)
1346 BODY *choice = NULL;
1353 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1354 a->encoding == ENCUUENCODED)
1358 fstat (fileno (s->fpin), &st);
1359 b = mutt_new_body ();
1360 b->length = (long) st.st_size;
1361 b->parts = mutt_parse_multipart (s->fpin,
1362 mutt_get_parameter ("boundary", a->parameter),
1363 (long) st.st_size, ascii_strcasecmp ("digest", a->subtype) == 0);
1370 /* First, search list of prefered types */
1371 t = AlternativeOrderList;
1372 while (t && !choice)
1375 int btlen; /* length of basetype */
1376 int wild; /* do we have a wildcard to match all subtypes? */
1378 c = strchr (t->data, '/');
1381 wild = (c[1] == '*' && c[2] == 0);
1382 btlen = c - t->data;
1387 btlen = mutt_strlen (t->data);
1396 const char *bt = TYPE(b);
1397 if (!ascii_strncasecmp (bt, t->data, btlen) && bt[btlen] == 0)
1399 /* the basetype matches */
1400 if (wild || !ascii_strcasecmp (t->data + btlen + 1, b->subtype))
1410 /* Next, look for an autoviewable type */
1419 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
1420 if (mutt_is_autoview (b, buf))
1422 rfc1524_entry *entry = rfc1524_new_entry ();
1424 if (rfc1524_mailcap_lookup (b, buf, entry, M_AUTOVIEW))
1428 rfc1524_free_entry (&entry);
1434 /* Then, look for a text entry */
1443 if (b->type == TYPETEXT)
1445 if (! ascii_strcasecmp ("plain", b->subtype) && type <= TXTPLAIN)
1450 else if (! ascii_strcasecmp ("enriched", b->subtype) && type <= TXTENRICHED)
1455 else if (! ascii_strcasecmp ("html", b->subtype) && type <= TXTHTML)
1465 /* Finally, look for other possibilities */
1474 if (mutt_can_decode (b))
1482 if (s->flags & M_DISPLAY && !option (OPTWEED))
1484 fseek (s->fpin, choice->hdr_offset, 0);
1485 mutt_copy_bytes(s->fpin, s->fpout, choice->offset-choice->hdr_offset);
1487 mutt_body_handler (choice, s);
1489 else if (s->flags & M_DISPLAY)
1491 /* didn't find anything that we could display! */
1492 state_mark_attach (s);
1493 state_puts(_("[-- Error: Could not display any parts of Multipart/Alternative! --]\n"), s);
1500 /* handles message/rfc822 body parts */
1501 void message_handler (BODY *a, STATE *s)
1507 off_start = ftell (s->fpin);
1508 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1509 a->encoding == ENCUUENCODED)
1511 fstat (fileno (s->fpin), &st);
1512 b = mutt_new_body ();
1513 b->length = (long) st.st_size;
1514 b->parts = mutt_parse_messageRFC822 (s->fpin, b);
1521 mutt_copy_hdr (s->fpin, s->fpout, off_start, b->parts->offset,
1522 (((s->flags & M_WEED) || ((s->flags & (M_DISPLAY|M_PRINTING)) && option (OPTWEED))) ? (CH_WEED | CH_REORDER) : 0) |
1523 (s->prefix ? CH_PREFIX : 0) | CH_DECODE | CH_FROM, s->prefix);
1526 state_puts (s->prefix, s);
1527 state_putc ('\n', s);
1529 mutt_body_handler (b->parts, s);
1532 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1533 a->encoding == ENCUUENCODED)
1534 mutt_free_body (&b);
1537 /* returns 1 if decoding the attachment will produce output */
1538 int mutt_can_decode (BODY *a)
1542 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
1543 if (mutt_is_autoview (a, type))
1544 return (rfc1524_mailcap_lookup (a, type, NULL, M_AUTOVIEW));
1545 else if (a->type == TYPETEXT)
1547 else if (a->type == TYPEMESSAGE)
1549 else if (a->type == TYPEMULTIPART)
1555 if (ascii_strcasecmp (a->subtype, "signed") == 0 ||
1556 ascii_strcasecmp (a->subtype, "encrypted") == 0)
1560 for (p = a->parts; p; p = p->next)
1562 if (mutt_can_decode (p))
1567 else if (WithCrypto && a->type == TYPEAPPLICATION)
1569 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp(a))
1571 if ((WithCrypto & APPLICATION_SMIME) && mutt_is_application_smime(a))
1578 void multipart_handler (BODY *a, STATE *s)
1585 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1586 a->encoding == ENCUUENCODED)
1588 fstat (fileno (s->fpin), &st);
1589 b = mutt_new_body ();
1590 b->length = (long) st.st_size;
1591 b->parts = mutt_parse_multipart (s->fpin,
1592 mutt_get_parameter ("boundary", a->parameter),
1593 (long) st.st_size, ascii_strcasecmp ("digest", a->subtype) == 0);
1598 for (p = b->parts, count = 1; p; p = p->next, count++)
1600 if (s->flags & M_DISPLAY)
1602 state_mark_attach (s);
1603 state_printf (s, _("[-- Attachment #%d"), count);
1604 if (p->description || p->filename || p->form_name)
1606 state_puts (": ", s);
1607 state_puts (p->description ? p->description :
1608 p->filename ? p->filename : p->form_name, s);
1610 state_puts (" --]\n", s);
1612 mutt_pretty_size (length, sizeof (length), p->length);
1614 state_mark_attach (s);
1615 state_printf (s, _("[-- Type: %s/%s, Encoding: %s, Size: %s --]\n"),
1616 TYPE (p), p->subtype, ENCODING (p->encoding), length);
1617 if (!option (OPTWEED))
1619 fseek (s->fpin, p->hdr_offset, 0);
1620 mutt_copy_bytes(s->fpin, s->fpout, p->offset-p->hdr_offset);
1623 state_putc ('\n', s);
1627 if (p->description && mutt_can_decode (p))
1628 state_printf (s, "Content-Description: %s\n", p->description);
1631 state_printf(s, "%s: \n", p->form_name);
1634 mutt_body_handler (p, s);
1635 state_putc ('\n', s);
1636 if ((s->flags & M_REPLYING)
1637 && (option (OPTINCLUDEONLYFIRST)) && (s->flags & M_FIRSTDONE))
1641 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1642 a->encoding == ENCUUENCODED)
1643 mutt_free_body (&b);
1646 void autoview_handler (BODY *a, STATE *s)
1648 rfc1524_entry *entry = rfc1524_new_entry ();
1649 char buffer[LONG_STRING];
1651 char command[LONG_STRING];
1652 char tempfile[_POSIX_PATH_MAX] = "";
1660 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
1661 rfc1524_mailcap_lookup (a, type, entry, M_AUTOVIEW);
1663 fname = safe_strdup (a->filename);
1664 mutt_sanitize_filename (fname, 1);
1665 rfc1524_expand_filename (entry->nametemplate, fname, tempfile, sizeof (tempfile));
1670 strfcpy (command, entry->command, sizeof (command));
1672 /* rfc1524_expand_command returns 0 if the file is required */
1673 piped = rfc1524_expand_command (a, tempfile, type, command, sizeof (command));
1675 if (s->flags & M_DISPLAY)
1677 state_mark_attach (s);
1678 state_printf (s, _("[-- Autoview using %s --]\n"), command);
1679 mutt_message(_("Invoking autoview command: %s"),command);
1682 if ((fpin = safe_fopen (tempfile, "w+")) == NULL)
1684 mutt_perror ("fopen");
1685 rfc1524_free_entry (&entry);
1689 mutt_copy_bytes (s->fpin, fpin, a->length);
1693 safe_fclose (&fpin);
1694 thepid = mutt_create_filter (command, NULL, &fpout, &fperr);
1701 thepid = mutt_create_filter_fd (command, NULL, &fpout, &fperr,
1702 fileno(fpin), -1, -1);
1707 mutt_perror _("Can't create filter");
1708 if (s->flags & M_DISPLAY)
1710 state_mark_attach (s);
1711 state_printf (s, _("[-- Can't run %s. --]\n"), command);
1718 while (fgets (buffer, sizeof(buffer), fpout) != NULL)
1720 state_puts (s->prefix, s);
1721 state_puts (buffer, s);
1723 /* check for data on stderr */
1724 if (fgets (buffer, sizeof(buffer), fperr))
1726 if (s->flags & M_DISPLAY)
1728 state_mark_attach (s);
1729 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1732 state_puts (s->prefix, s);
1733 state_puts (buffer, s);
1734 while (fgets (buffer, sizeof(buffer), fperr) != NULL)
1736 state_puts (s->prefix, s);
1737 state_puts (buffer, s);
1743 mutt_copy_stream (fpout, s->fpout);
1744 /* Check for stderr messages */
1745 if (fgets (buffer, sizeof(buffer), fperr))
1747 if (s->flags & M_DISPLAY)
1749 state_mark_attach (s);
1750 state_printf (s, _("[-- Autoview stderr of %s --]\n"),
1754 state_puts (buffer, s);
1755 mutt_copy_stream (fperr, s->fpout);
1760 safe_fclose (&fpout);
1761 safe_fclose (&fperr);
1763 mutt_wait_filter (thepid);
1765 safe_fclose (&fpin);
1767 mutt_unlink (tempfile);
1769 if (s->flags & M_DISPLAY)
1770 mutt_clear_error ();
1772 rfc1524_free_entry (&entry);
1775 static void external_body_handler (BODY *b, STATE *s)
1777 const char *access_type;
1778 const char *expiration;
1781 access_type = mutt_get_parameter ("access-type", b->parameter);
1784 if (s->flags & M_DISPLAY)
1786 state_mark_attach (s);
1787 state_puts (_("[-- Error: message/external-body has no access-type parameter --]\n"), s);
1792 expiration = mutt_get_parameter ("expiration", b->parameter);
1794 expire = mutt_parse_date (expiration, NULL);
1798 if (!ascii_strcasecmp (access_type, "x-mutt-deleted"))
1800 if (s->flags & (M_DISPLAY|M_PRINTING))
1803 char pretty_size[10];
1805 state_mark_attach (s);
1806 state_printf (s, _("[-- This %s/%s attachment "),
1807 TYPE(b->parts), b->parts->subtype);
1808 length = mutt_get_parameter ("length", b->parameter);
1811 mutt_pretty_size (pretty_size, sizeof (pretty_size),
1812 strtol (length, NULL, 10));
1813 state_printf (s, _("(size %s bytes) "), pretty_size);
1815 state_puts (_("has been deleted --]\n"), s);
1819 state_mark_attach (s);
1820 state_printf (s, _("[-- on %s --]\n"), expiration);
1822 if (b->parts->filename)
1824 state_mark_attach (s);
1825 state_printf (s, _("[-- name: %s --]\n"), b->parts->filename);
1828 mutt_copy_hdr (s->fpin, s->fpout, ftell (s->fpin), b->parts->offset,
1829 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1833 else if(expiration && expire < time(NULL))
1835 if (s->flags & M_DISPLAY)
1837 state_mark_attach (s);
1838 state_printf (s, _("[-- This %s/%s attachment is not included, --]\n"),
1839 TYPE(b->parts), b->parts->subtype);
1840 state_attach_puts (_("[-- and the indicated external source has --]\n"
1841 "[-- expired. --]\n"), s);
1843 mutt_copy_hdr(s->fpin, s->fpout, ftell (s->fpin), b->parts->offset,
1844 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1850 if (s->flags & M_DISPLAY)
1852 state_mark_attach (s);
1854 _("[-- This %s/%s attachment is not included, --]\n"),
1855 TYPE (b->parts), b->parts->subtype);
1856 state_mark_attach (s);
1858 _("[-- and the indicated access-type %s is unsupported --]\n"),
1860 mutt_copy_hdr (s->fpin, s->fpout, ftell (s->fpin), b->parts->offset,
1861 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1867 void mutt_decode_attachment (BODY *b, STATE *s)
1869 int istext = mutt_is_text_part (b);
1870 iconv_t cd = (iconv_t)(-1);
1876 if(s->flags & M_CHARCONV)
1878 char *charset = mutt_get_parameter ("charset", b->parameter);
1879 if (!option (OPTSTRICTMIME) && !charset)
1880 charset = mutt_get_first_charset (AssumedCharset);
1881 if (charset && Charset)
1882 cd = mutt_iconv_open (Charset, charset, M_ICONV_HOOK_FROM);
1886 if (b->file_charset)
1887 cd = mutt_iconv_open (Charset, b->file_charset, M_ICONV_HOOK_FROM);
1891 fseek (s->fpin, b->offset, 0);
1892 switch (b->encoding)
1894 case ENCQUOTEDPRINTABLE:
1895 mutt_decode_quoted (s, b->length, istext, cd);
1898 mutt_decode_base64 (s, b->length, istext, cd);
1901 mutt_decode_uuencoded (s, b->length, istext, cd);
1904 mutt_decode_xbit (s, b->length, istext, cd);
1908 if (cd != (iconv_t)(-1))
1912 void mutt_body_handler (BODY *b, STATE *s)
1917 char tempfile[_POSIX_PATH_MAX];
1918 handler_t handler = NULL;
1920 size_t tmplength = 0;
1923 int oflags = s->flags;
1925 /* first determine which handler to use to process this part */
1927 snprintf (type, sizeof (type), "%s/%s", TYPE (b), b->subtype);
1928 if (mutt_is_autoview (b, type))
1930 rfc1524_entry *entry = rfc1524_new_entry ();
1932 if (rfc1524_mailcap_lookup (b, type, entry, M_AUTOVIEW))
1934 handler = autoview_handler;
1935 s->flags &= ~M_CHARCONV;
1937 rfc1524_free_entry (&entry);
1939 else if (b->type == TYPETEXT)
1941 if (ascii_strcasecmp ("plain", b->subtype) == 0)
1943 /* avoid copying this part twice since removing the transfer-encoding is
1944 * the only operation needed.
1946 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b))
1947 handler = crypt_pgp_application_pgp_handler;
1948 else if (ascii_strcasecmp ("flowed", mutt_get_parameter ("format", b->parameter)) == 0)
1949 handler = text_plain_flowed_handler;
1953 else if (ascii_strcasecmp ("enriched", b->subtype) == 0)
1954 handler = text_enriched_handler;
1955 else /* text body type without a handler */
1958 else if (b->type == TYPEMESSAGE)
1960 if(mutt_is_message_type(b->type, b->subtype))
1961 handler = message_handler;
1962 else if (!ascii_strcasecmp ("delivery-status", b->subtype))
1964 else if (!ascii_strcasecmp ("external-body", b->subtype))
1965 handler = external_body_handler;
1967 else if (b->type == TYPEMULTIPART)
1971 if (ascii_strcasecmp ("alternative", b->subtype) == 0)
1972 handler = alternative_handler;
1973 else if (WithCrypto && ascii_strcasecmp ("signed", b->subtype) == 0)
1975 p = mutt_get_parameter ("protocol", b->parameter);
1978 mutt_error _("Error: multipart/signed has no protocol.");
1979 else if (s->flags & M_VERIFY)
1980 handler = mutt_signed_handler;
1982 else if ((WithCrypto & APPLICATION_PGP)
1983 && mutt_strcasecmp ("encrypted", b->subtype) == 0)
1985 p = mutt_get_parameter ("protocol", b->parameter);
1988 mutt_error _("Error: multipart/encrypted has no protocol parameter!");
1989 else if (ascii_strcasecmp ("application/pgp-encrypted", p) == 0)
1990 handler = crypt_pgp_encrypted_handler;
1994 handler = multipart_handler;
1996 else if (WithCrypto && b->type == TYPEAPPLICATION)
1998 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b))
1999 handler = crypt_pgp_application_pgp_handler;
2000 if ((WithCrypto & APPLICATION_SMIME) && mutt_is_application_smime(b))
2001 handler = crypt_smime_application_smime_handler;
2005 if (plaintext || handler)
2007 fseek (s->fpin, b->offset, 0);
2009 /* see if we need to decode this part before processing it */
2010 if (b->encoding == ENCBASE64 || b->encoding == ENCQUOTEDPRINTABLE ||
2011 b->encoding == ENCUUENCODED || plaintext ||
2012 mutt_is_text_part (b)) /* text subtypes may
2014 * set conversion even
2015 * with 8bit encoding.
2018 int origType = b->type;
2019 char *savePrefix = NULL;
2023 /* decode to a tempfile, saving the original destination */
2025 mutt_mktemp (tempfile);
2026 if ((s->fpout = safe_fopen (tempfile, "w")) == NULL)
2028 mutt_error _("Unable to open temporary file!");
2031 /* decoding the attachment changes the size and offset, so save a copy
2032 * of the "real" values now, and restore them after processing
2034 tmplength = b->length;
2035 tmpoffset = b->offset;
2037 /* if we are decoding binary bodies, we don't want to prefix each
2038 * line with the prefix or else the data will get corrupted.
2040 savePrefix = s->prefix;
2048 mutt_decode_attachment (b, s);
2052 b->length = ftell (s->fpout);
2056 /* restore final destination and substitute the tempfile for input */
2059 s->fpin = safe_fopen (tempfile, "r");
2062 /* restore the prefix */
2063 s->prefix = savePrefix;
2069 /* process the (decoded) body part */
2076 b->length = tmplength;
2077 b->offset = tmpoffset;
2079 /* restore the original source stream */
2084 s->flags |= M_FIRSTDONE;
2086 else if (s->flags & M_DISPLAY)
2088 state_mark_attach (s);
2089 state_printf (s, _("[-- %s/%s is unsupported "), TYPE (b), b->subtype);
2090 if (!option (OPTVIEWATTACH))
2092 if (km_expand_key (type, sizeof(type),
2093 km_find_func (MENU_PAGER, OP_VIEW_ATTACHMENTS)))
2094 fprintf (s->fpout, _("(use '%s' to view this part)"), type);
2096 fputs (_("(need 'view-attachments' bound to key!)"), s->fpout);
2098 fputs (" --]\n", s->fpout);
2102 s->flags = oflags | (s->flags & M_FIRSTDONE);