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)
87 if (cd != (iconv_t) (-1)) {
88 ob = bufo, obl = sizeof (bufo);
89 iconv (cd, 0, 0, &ob, &obl);
91 state_prefix_put (bufo, ob - bufo, s);
93 if (Quotebuf[0] != '\0')
94 state_prefix_putc ('\n', s);
98 if (cd == (iconv_t) (-1)) {
99 state_prefix_put (bufi, *l, s);
106 ob = bufo, obl = sizeof (bufo);
107 mutt_iconv (cd, &ib, &ibl, &ob, &obl, 0, "?");
110 state_prefix_put (bufo, ob - bufo, s);
112 memmove (bufi, ib, ibl);
116 void mutt_decode_xbit (STATE * s, long len, int istext, iconv_t cd)
119 char bufi[BUFI_SIZE];
123 state_set_prefix (s);
125 while ((c = fgetc (s->fpin)) != EOF && len--) {
126 if (c == '\r' && len) {
127 if ((ch = fgetc (s->fpin)) == '\n') {
132 ungetc (ch, s->fpin);
136 if (l == sizeof (bufi))
137 mutt_convert_to_state (cd, bufi, &l, s);
140 mutt_convert_to_state (cd, bufi, &l, s);
141 mutt_convert_to_state (cd, 0, 0, s);
143 state_reset_prefix (s);
146 mutt_copy_bytes (s->fpin, s->fpout, len);
149 static int qp_decode_triple (char *s, char *d)
151 /* soft line break */
152 if (*s == '=' && !(*(s + 1)))
155 /* quoted-printable triple */
157 isxdigit ((unsigned char) *(s + 1)) &&
158 isxdigit ((unsigned char) *(s + 2))) {
159 *d = (hexval (*(s + 1)) << 4) | hexval (*(s + 2));
167 static void qp_decode_line (char *dest, char *src, size_t * l, int last)
175 /* decode the line */
177 for (d = dest, s = src; *s;) {
178 switch ((kind = qp_decode_triple (s, &c))) {
182 break; /* qp triple */
185 break; /* single character */
189 break; /* soft line break */
193 if (!soft && last == '\n')
201 * Decode an attachment encoded with quoted-printable.
203 * Why doesn't this overflow any buffers? First, it's guaranteed
204 * that the length of a line grows when you _en_-code it to
205 * quoted-printable. That means that we always can store the
206 * result in a buffer of at most the _same_ size.
208 * Now, we don't special-case if the line we read with fgets()
209 * isn't terminated. We don't care about this, since STRING > 78,
210 * so corrupted input will just be corrupted a bit more. That
211 * implies that STRING+1 bytes are always sufficient to store the
212 * result of qp_decode_line.
214 * Finally, at soft line breaks, some part of a multibyte character
215 * may have been left over by mutt_convert_to_state(). This shouldn't
216 * be more than 6 characters, so STRING + 7 should be sufficient
217 * memory to store the decoded data.
219 * Just to make sure that I didn't make some off-by-one error
220 * above, we just use STRING*2 for the target buffer's size.
224 void mutt_decode_quoted (STATE * s, long len, int istext, iconv_t cd)
227 char decline[2 * STRING];
229 size_t linelen; /* number of input bytes in `line' */
232 int last; /* store the last character in the input line */
235 state_set_prefix (s);
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 */
261 while (linelen > 0 && ISSPACE (line[linelen - 1]))
266 /* decode and do character set conversion */
267 qp_decode_line (decline + l, line, &l3, last);
269 mutt_convert_to_state (cd, decline, &l, s);
272 mutt_convert_to_state (cd, 0, 0, s);
273 state_reset_prefix (s);
276 void mutt_decode_base64 (STATE * s, long len, int istext, iconv_t cd)
279 int c1, c2, c3, c4, ch, cr = 0, i;
280 char bufi[BUFI_SIZE];
286 state_set_prefix (s);
289 for (i = 0; i < 4 && len > 0; len--) {
290 if ((ch = fgetc (s->fpin)) == EOF)
292 if (ch >= 0 && ch < 128 && (base64val (ch) != -1 || ch == '='))
296 dprint (2, (debugfile, "%s:%d [mutt_decode_base64()]: "
297 "didn't get a multiple of 4 chars.\n", __FILE__, __LINE__));
301 c1 = base64val (buf[0]);
302 c2 = base64val (buf[1]);
303 ch = (c1 << 2) | (c2 >> 4);
305 if (cr && ch != '\n')
310 if (istext && ch == '\r')
317 c3 = base64val (buf[2]);
318 ch = ((c2 & 0xf) << 4) | (c3 >> 2);
320 if (cr && ch != '\n')
325 if (istext && ch == '\r')
332 c4 = base64val (buf[3]);
333 ch = ((c3 & 0x3) << 6) | c4;
335 if (cr && ch != '\n')
339 if (istext && ch == '\r')
344 if (l + 8 >= sizeof (bufi))
345 mutt_convert_to_state (cd, bufi, &l, s);
351 mutt_convert_to_state (cd, bufi, &l, s);
352 mutt_convert_to_state (cd, 0, 0, s);
354 state_reset_prefix (s);
357 unsigned char decode_byte (char ch)
364 void mutt_decode_uuencoded (STATE * s, long len, int istext, iconv_t cd)
366 char tmps[SHORT_STRING];
367 char linelen, c, l, out;
369 char bufi[BUFI_SIZE];
373 state_set_prefix (s);
376 if ((fgets (tmps, sizeof (tmps), s->fpin)) == NULL)
378 len -= mutt_strlen (tmps);
379 if ((!mutt_strncmp (tmps, "begin", 5)) && ISSPACE (tmps[5]))
383 if ((fgets (tmps, sizeof (tmps), s->fpin)) == NULL)
385 len -= mutt_strlen (tmps);
386 if (!mutt_strncmp (tmps, "end", 3))
389 linelen = decode_byte (*pt);
391 for (c = 0; c < linelen;) {
392 for (l = 2; l <= 6; l += 2) {
393 out = decode_byte (*pt) << l;
395 out |= (decode_byte (*pt) >> (6 - l));
401 mutt_convert_to_state (cd, bufi, &k, s);
406 mutt_convert_to_state (cd, bufi, &k, s);
407 mutt_convert_to_state (cd, 0, 0, s);
409 state_reset_prefix (s);
412 /* ----------------------------------------------------------------------------
413 * A (not so) minimal implementation of RFC1563.
416 #define IndentSize (4)
418 enum { RICH_PARAM = 0, RICH_BOLD, RICH_UNDERLINE, RICH_ITALIC, RICH_NOFILL,
419 RICH_INDENT, RICH_INDENT_RIGHT, RICH_EXCERPT, RICH_CENTER, RICH_FLUSHLEFT,
420 RICH_FLUSHRIGHT, RICH_COLOR, RICH_LAST_TAG
424 const char *tag_name;
428 "param", RICH_PARAM}, {
429 "bold", RICH_BOLD}, {
430 "italic", RICH_ITALIC}, {
431 "underline", RICH_UNDERLINE}, {
432 "nofill", RICH_NOFILL}, {
433 "excerpt", RICH_EXCERPT}, {
434 "indent", RICH_INDENT}, {
435 "indentright", RICH_INDENT_RIGHT}, {
436 "center", RICH_CENTER}, {
437 "flushleft", RICH_FLUSHLEFT}, {
438 "flushright", RICH_FLUSHRIGHT}, {
439 "flushboth", RICH_FLUSHLEFT}, {
440 "color", RICH_COLOR}, {
441 "x-color", RICH_COLOR}, {
445 struct enriched_state {
458 int tag_level[RICH_LAST_TAG];
463 static void enriched_wrap (struct enriched_state *stte)
468 if (stte->line_len) {
469 if (stte->tag_level[RICH_CENTER] || stte->tag_level[RICH_FLUSHRIGHT]) {
470 /* Strip trailing white space */
471 size_t y = stte->line_used - 1;
473 while (y && ISSPACE (stte->line[y])) {
474 stte->line[y] = '\0';
479 if (stte->tag_level[RICH_CENTER]) {
480 /* Strip leading whitespace */
483 while (stte->line[y] && ISSPACE (stte->line[y]))
488 for (z = y; z <= stte->line_used; z++) {
489 stte->line[z - y] = stte->line[z];
493 stte->line_used -= y;
498 extra = stte->WrapMargin - stte->line_len - stte->indent_len -
499 (stte->tag_level[RICH_INDENT_RIGHT] * IndentSize);
501 if (stte->tag_level[RICH_CENTER]) {
504 state_putc (' ', stte->s);
508 else if (stte->tag_level[RICH_FLUSHRIGHT]) {
511 state_putc (' ', stte->s);
516 state_puts (stte->line, stte->s);
519 state_putc ('\n', stte->s);
520 stte->line[0] = '\0';
523 stte->indent_len = 0;
524 if (stte->s->prefix) {
525 state_puts (stte->s->prefix, stte->s);
526 stte->indent_len += mutt_strlen (stte->s->prefix);
529 if (stte->tag_level[RICH_EXCERPT]) {
530 x = stte->tag_level[RICH_EXCERPT];
532 if (stte->s->prefix) {
533 state_puts (stte->s->prefix, stte->s);
534 stte->indent_len += mutt_strlen (stte->s->prefix);
537 state_puts ("> ", stte->s);
538 stte->indent_len += mutt_strlen ("> ");
544 stte->indent_len = 0;
545 if (stte->tag_level[RICH_INDENT]) {
546 x = stte->tag_level[RICH_INDENT] * IndentSize;
547 stte->indent_len += x;
549 state_putc (' ', stte->s);
555 static void enriched_flush (struct enriched_state *stte, int wrap)
557 if (!stte->tag_level[RICH_NOFILL] && (stte->line_len + stte->word_len >
559 (stte->tag_level[RICH_INDENT_RIGHT] *
560 IndentSize) - stte->indent_len)))
561 enriched_wrap (stte);
563 if (stte->buff_used) {
564 stte->buffer[stte->buff_used] = '\0';
565 stte->line_used += stte->buff_used;
566 if (stte->line_used > stte->line_max) {
567 stte->line_max = stte->line_used;
568 safe_realloc (&stte->line, stte->line_max + 1);
570 strcat (stte->line, stte->buffer); /* __STRCAT_CHECKED__ */
571 stte->line_len += stte->word_len;
576 enriched_wrap (stte);
580 static void enriched_putc (int c, struct enriched_state *stte)
582 if (stte->tag_level[RICH_PARAM]) {
583 if (stte->tag_level[RICH_COLOR]) {
584 if (stte->param_used + 1 >= stte->param_len)
585 safe_realloc (&stte->param, (stte->param_len += STRING));
587 stte->param[stte->param_used++] = c;
589 return; /* nothing to do */
592 /* see if more space is needed (plus extra for possible rich characters) */
593 if (stte->buff_len < stte->buff_used + 3) {
594 stte->buff_len += LONG_STRING;
595 safe_realloc (&stte->buffer, stte->buff_len + 1);
598 if ((!stte->tag_level[RICH_NOFILL] && ISSPACE (c)) || c == '\0') {
600 stte->word_len += 8 - (stte->line_len + stte->word_len) % 8;
604 stte->buffer[stte->buff_used++] = c;
605 enriched_flush (stte, 0);
608 if (stte->s->flags & M_DISPLAY) {
609 if (stte->tag_level[RICH_BOLD]) {
610 stte->buffer[stte->buff_used++] = c;
611 stte->buffer[stte->buff_used++] = '\010';
612 stte->buffer[stte->buff_used++] = c;
614 else if (stte->tag_level[RICH_UNDERLINE]) {
616 stte->buffer[stte->buff_used++] = '_';
617 stte->buffer[stte->buff_used++] = '\010';
618 stte->buffer[stte->buff_used++] = c;
620 else if (stte->tag_level[RICH_ITALIC]) {
621 stte->buffer[stte->buff_used++] = c;
622 stte->buffer[stte->buff_used++] = '\010';
623 stte->buffer[stte->buff_used++] = '_';
626 stte->buffer[stte->buff_used++] = c;
630 stte->buffer[stte->buff_used++] = c;
636 static void enriched_puts (char *s, struct enriched_state *stte)
640 if (stte->buff_len < stte->buff_used + mutt_strlen (s)) {
641 stte->buff_len += LONG_STRING;
642 safe_realloc (&stte->buffer, stte->buff_len + 1);
646 stte->buffer[stte->buff_used++] = *c;
651 static void enriched_set_flags (const char *tag, struct enriched_state *stte)
653 const char *tagptr = tag;
659 for (i = 0, j = -1; EnrichedTags[i].tag_name; i++)
660 if (ascii_strcasecmp (EnrichedTags[i].tag_name, tagptr) == 0) {
661 j = EnrichedTags[i].index;
666 if (j == RICH_CENTER || j == RICH_FLUSHLEFT || j == RICH_FLUSHRIGHT)
667 enriched_flush (stte, 1);
670 if (stte->tag_level[j]) /* make sure not to go negative */
671 stte->tag_level[j]--;
672 if ((stte->s->flags & M_DISPLAY) && j == RICH_PARAM
673 && stte->tag_level[RICH_COLOR]) {
674 stte->param[stte->param_used] = '\0';
675 if (!ascii_strcasecmp (stte->param, "black")) {
676 enriched_puts ("\033[30m", stte);
678 else if (!ascii_strcasecmp (stte->param, "red")) {
679 enriched_puts ("\033[31m", stte);
681 else if (!ascii_strcasecmp (stte->param, "green")) {
682 enriched_puts ("\033[32m", stte);
684 else if (!ascii_strcasecmp (stte->param, "yellow")) {
685 enriched_puts ("\033[33m", stte);
687 else if (!ascii_strcasecmp (stte->param, "blue")) {
688 enriched_puts ("\033[34m", stte);
690 else if (!ascii_strcasecmp (stte->param, "magenta")) {
691 enriched_puts ("\033[35m", stte);
693 else if (!ascii_strcasecmp (stte->param, "cyan")) {
694 enriched_puts ("\033[36m", stte);
696 else if (!ascii_strcasecmp (stte->param, "white")) {
697 enriched_puts ("\033[37m", stte);
700 if ((stte->s->flags & M_DISPLAY) && j == RICH_COLOR) {
701 enriched_puts ("\033[0m", stte);
704 /* flush parameter buffer when closing the tag */
705 if (j == RICH_PARAM) {
706 stte->param_used = 0;
707 stte->param[0] = '\0';
711 stte->tag_level[j]++;
713 if (j == RICH_EXCERPT)
714 enriched_flush (stte, 1);
718 void text_enriched_handler (BODY * a, STATE * s)
721 TEXT, LANGLE, TAG, BOGUS_TAG, NEWLINE, ST_EOF, DONE
724 long bytes = a->length;
725 struct enriched_state stte;
728 char tag[LONG_STRING + 1];
730 memset (&stte, 0, sizeof (stte));
733 ((s->flags & M_DISPLAY) ? (COLS - 4) : ((COLS - 4) <
734 72) ? (COLS - 4) : 72);
735 stte.line_max = stte.WrapMargin * 4;
736 stte.line = (char *) safe_calloc (1, stte.line_max + 1);
737 stte.param = (char *) safe_calloc (1, STRING);
739 stte.param_len = STRING;
743 state_puts (s->prefix, s);
744 stte.indent_len += mutt_strlen (s->prefix);
747 while (state != DONE) {
748 if (state != ST_EOF) {
749 if (!bytes || (c = fgetc (s->fpin)) == EOF)
763 if (stte.tag_level[RICH_NOFILL]) {
764 enriched_flush (&stte, 1);
767 enriched_putc (' ', &stte);
773 enriched_putc (c, &stte);
779 enriched_putc (c, &stte);
787 /* Yes, fall through (it wasn't a <<, so this char is first in TAG) */
791 enriched_set_flags (tag, &stte);
794 else if (tag_len < LONG_STRING) /* ignore overly long tags */
807 enriched_flush (&stte, 1);
816 enriched_putc ('\0', &stte);
817 enriched_flush (&stte, 1);
821 case DONE: /* not reached, but gcc complains if this is absent */
826 state_putc ('\n', s); /* add a final newline */
828 FREE (&(stte.buffer));
830 FREE (&(stte.param));
834 * An implementation of RFC 2646.
836 * NOTE: This still has to be made UTF-8 aware.
840 #define FLOWED_MAX 77
843 static int flowed_maybe_quoted (char *cont)
845 return regexec ((regex_t *) QuoteRegexp.rx, cont, 0, NULL, 0) == 0;
848 static void flowed_quote (STATE * s, int level)
853 if (option (OPTTEXTFLOWED))
856 state_puts (s->prefix, s);
859 for (i = 0; i < level; i++)
863 static void flowed_stuff (STATE * s, char *cont, int level)
865 if (!option (OPTTEXTFLOWED) && !(s->flags & M_DISPLAY))
868 if (s->flags & M_DISPLAY) {
870 * Hack: If we are in the beginning of the line and there is
871 * some text on the line which looks like it's quoted, turn off
872 * ANSI colors, so quote coloring doesn't affect this line.
874 if (*cont && !level && !mutt_strcmp (Pager, "builtin")
875 && flowed_maybe_quoted (cont))
876 state_puts ("\033[0m", s);
878 else if ((!(s->flags & M_PRINTING)) && ((*cont == ' ') || (*cont == '>')
880 && !mutt_strncmp (cont, "From ",
885 static char *flowed_skip_indent (char *prefix, char *cont)
887 for (; *cont == ' ' || *cont == '\t'; cont++)
893 static int flowed_visual_strlen (char *l, int i)
897 for (j = 0; *l; l++) {
899 j += 8 - ((i + j) % 8);
907 static void text_plain_flowed_handler (BODY * a, STATE * s)
909 char line[LONG_STRING];
910 char indent[LONG_STRING];
924 int bytes = a->length;
925 int actually_wrap = 0;
938 if ((flowed_max = FLOWED_MAX) > COLS - 3)
939 flowed_max = COLS - 3;
940 if (flowed_max > COLS - WrapMargin)
941 flowed_max = COLS - WrapMargin;
945 flowed_max = COLS - WrapMargin;
949 fprintf (stderr, "flowed_max = %d\n", flowed_max);
951 while (bytes > 0 && fgets (line, sizeof (line), s->fpin)) {
952 bytes -= strlen (line);
958 * If the last line wasn't fully read, this is the
961 actually_wrap = !last_full;
963 if ((t = strrchr (line, '\r')) || (t = strrchr (line, '\n'))) {
967 else if ((t = strrchr (line, ' ')) || (t = strrchr (line, '\t'))) {
969 * Bad: We have a line of more than LONG_STRING characters.
970 * (Which SHOULD NOT happen, since lines SHOULD be <= 79
973 * Try to simulate a soft line break at a word boundary.
974 * Handle the rest of the line next time.
976 * Give up when we have a single word which is longer than
977 * LONG_STRING characters. It will just be split into parts,
978 * with a hard line break in between.
987 fseek (s->fpin, -l, SEEK_CUR);
994 last_quoted = quoted;
998 * We are in the beginning of a new line. Determine quote level
999 * and indentation prefix
1001 for (quoted = 0; line[quoted] == '>'; quoted++);
1003 cont = line + quoted;
1005 /* undo space stuffing */
1009 /* If there is an indentation, record it. */
1010 cont = flowed_skip_indent (indent, cont);
1011 i_add = flowed_visual_strlen (indent, quoted + add);
1015 * This is just the tail of some over-long line. Keep
1016 * indentation and quote levels. Don't unstuff.
1021 /* If we have a change in quoting depth, wrap. */
1023 if (col && last_quoted != quoted && last_quoted >= 0) {
1024 state_putc ('\n', s);
1037 /* try to find a point for word wrapping */
1040 l = flowed_visual_strlen (cont, quoted + i_add + add + col);
1041 rl = mutt_strlen (cont);
1042 if (quoted + i_add + add + col + l > flowed_max) {
1045 for (tmpcol = quoted + i_add + add + col, t = cont;
1046 *t && tmpcol < flowed_max; t++) {
1047 if (*t == ' ' || *t == '\t')
1050 tmpcol = (tmpcol & ~7) + 8;
1061 /* We seem to be desperate. Get me a new line, and retry. */
1062 if (!tail && (quoted + add + col + i_add + l > flowed_max) && col) {
1063 state_putc ('\n', s);
1068 /* Detect soft line breaks. */
1069 if (!soft && ascii_strcmp (cont, "-- ")) {
1070 lc = strrchr (cont, ' ');
1071 if (lc && lc[1] == '\0')
1076 * If we are in the beginning of an output line, do quoting
1079 * We have to temporarily assemble the line since display
1080 * stuffing (i.e., turning off quote coloring) may depend on
1081 * the line's actual content. You never know what people put
1082 * into their regular expressions.
1085 char tmp[LONG_STRING];
1087 snprintf (tmp, sizeof (tmp), "%s%s", indent, cont);
1089 flowed_quote (s, quoted);
1090 flowed_stuff (s, tmp, quoted + add);
1092 state_puts (indent, s);
1095 /* output the text */
1096 state_puts (cont, s);
1097 col += flowed_visual_strlen (cont, quoted + i_add + add + col);
1099 /* possibly indicate a soft line break */
1101 state_putc (' ', s);
1106 * Wrap if this display line corresponds to a
1107 * text line. Don't wrap if we changed the line.
1109 if (!soft || (!actually_wrap && full)) {
1110 state_putc ('\n', s);
1118 state_putc ('\n', s);
1123 static int get_quote_level (char *line)
1127 for (quoted = 0; line[quoted] == '>'; quoted++);
1131 static void print_flowed_line (char *line, STATE * s, int ql)
1135 int len = strlen (line);
1138 if (MaxLineLength > 0) {
1139 width = MaxLineLength - WrapMargin - ql - 1;
1140 if (option (OPTSTUFFQUOTED))
1143 width = MaxLineLength;
1146 if (option (OPTMBOXPANE))
1147 width = COLS - SidebarWidth - WrapMargin - ql - 1;
1149 width = COLS - WrapMargin - ql - 1;
1151 if (option (OPTSTUFFQUOTED))
1157 /* fprintf(stderr,"print_flowed_line will print `%s' with ql = %d\n",line,ql); */
1159 if (strlen (line) == 0) {
1160 if (option (OPTQUOTEEMPTY)) {
1162 state_puts(s->prefix,s);
1163 for (i=0;i<ql;++i) state_putc('>',s);
1164 if (option(OPTSTUFFQUOTED))
1171 pos = line + ql + width;
1173 if (ql > 0 && ISBLANK (*oldpos))
1176 /* fprintf(stderr,"oldpos = %p line+len = %p\n",oldpos,line+len); */
1178 for (; oldpos < line + len; pos += width) {
1179 /* fprintf(stderr,"outer for loop\n"); */
1180 if (pos < line + len) { /* only search a new position when we're not over the end of the string w/ pos */
1181 /* fprintf(stderr,"if 1\n"); */
1183 /* fprintf(stderr,"if 2: good luck! found a space\n"); */
1188 /* fprintf(stderr,"if 2: else\n"); */
1191 while (pos >= oldpos && !isspace (*pos)) {
1192 /* fprintf(stderr,"pos(%p) > oldpos(%p)\n",pos,oldpos); */
1196 /* fprintf(stderr,"wow, no space found, searching the other direction\n"); */
1198 while (pos < line + len && *pos && !isspace (*pos)) {
1199 /* fprintf(stderr,"pos(%p) < line+len(%p)\n",pos,line+len); */
1202 /* fprintf(stderr,"found a space pos = %p\n",pos); */
1209 /* fprintf(stderr,"if 1 else\n"); */
1212 state_puts (s->prefix, s);
1213 for (i = 0; i < ql; ++i)
1214 state_putc ('>', s);
1215 if (option (OPTSTUFFQUOTED) && (ql > 0 || s->prefix))
1216 state_putc (' ', s);
1217 state_puts (oldpos, s);
1218 /* fprintf(stderr,"print_flowed_line: `%s'\n",oldpos); */
1219 if (pos < line + len)
1220 state_putc (' ', s);
1221 state_putc ('\n', s);
1224 /*state_puts(line,s);
1225 state_putc('\n',s); */
1228 static void text_plain_flowed_handler (BODY * a, STATE * s)
1230 int bytes = a->length;
1231 char buf[LONG_STRING];
1232 char *curline = strdup ("");
1234 unsigned int curline_len = 1;
1235 unsigned int quotelevel = 0, newql = 0;
1236 int append_next_line = 0;
1239 while (bytes > 0 && fgets (buf, sizeof (buf), s->fpin)) {
1241 /* fprintf(stderr,"read `%s'",buf); */
1242 bytes -= strlen (buf);
1244 newql = get_quote_level (buf);
1246 if ((t = strrchr (buf, '\n')) || (t = strrchr (buf, '\r'))) {
1248 if (strlen (curline) > 0 && curline[strlen (curline) - 1] == ' '
1249 && newql == quotelevel
1250 && strcmp (curline + quotelevel, "-- ") != 0) {
1251 if (buf[newql] == ' ')
1252 curline[strlen (curline) - 1] = '\0';
1254 curline = realloc (curline, curline_len + strlen (buf));
1255 if (curline_len == 1)
1257 curline_len += strlen (buf);
1258 safe_strncat (curline, curline_len, buf + newql,
1259 strlen (buf + newql));
1266 print_flowed_line (curline, s, quotelevel);
1270 curline = realloc (curline, curline_len + strlen (buf));
1271 if (curline_len == 1)
1273 curline_len += strlen (buf);
1274 safe_strncat (curline, curline_len, buf, strlen (buf));
1279 append_next_line = 1;
1280 /* @todo: add handling of very long lines */
1284 print_flowed_line (curline, s, quotelevel);
1293 #define TXTENRICHED 3
1295 static void alternative_handler (BODY * a, STATE * s)
1297 BODY *choice = NULL;
1304 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1305 a->encoding == ENCUUENCODED) {
1309 fstat (fileno (s->fpin), &st);
1310 b = mutt_new_body ();
1311 b->length = (long) st.st_size;
1312 b->parts = mutt_parse_multipart (s->fpin,
1313 mutt_get_parameter ("boundary",
1316 ascii_strcasecmp ("digest",
1324 /* First, search list of prefered types */
1325 t = AlternativeOrderList;
1326 while (t && !choice) {
1328 int btlen; /* length of basetype */
1329 int wild; /* do we have a wildcard to match all subtypes? */
1331 c = strchr (t->data, '/');
1333 wild = (c[1] == '*' && c[2] == 0);
1334 btlen = c - t->data;
1338 btlen = mutt_strlen (t->data);
1346 const char *bt = TYPE (b);
1348 if (!ascii_strncasecmp (bt, t->data, btlen) && bt[btlen] == 0) {
1349 /* the basetype matches */
1350 if (wild || !ascii_strcasecmp (t->data + btlen + 1, b->subtype)) {
1359 /* Next, look for an autoviewable type */
1366 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
1367 if (mutt_is_autoview (b, buf)) {
1368 rfc1524_entry *entry = rfc1524_new_entry ();
1370 if (rfc1524_mailcap_lookup (b, buf, entry, M_AUTOVIEW)) {
1373 rfc1524_free_entry (&entry);
1379 /* Then, look for a text entry */
1386 if (b->type == TYPETEXT) {
1387 if (!ascii_strcasecmp ("plain", b->subtype) && type <= TXTPLAIN) {
1391 else if (!ascii_strcasecmp ("enriched", b->subtype)
1392 && type <= TXTENRICHED) {
1396 else if (!ascii_strcasecmp ("html", b->subtype) && type <= TXTHTML) {
1405 /* Finally, look for other possibilities */
1412 if (mutt_can_decode (b))
1419 if (s->flags & M_DISPLAY && !option (OPTWEED)) {
1420 fseek (s->fpin, choice->hdr_offset, 0);
1421 mutt_copy_bytes (s->fpin, s->fpout,
1422 choice->offset - choice->hdr_offset);
1424 mutt_body_handler (choice, s);
1426 else if (s->flags & M_DISPLAY) {
1427 /* didn't find anything that we could display! */
1428 state_mark_attach (s);
1430 ("[-- Error: Could not display any parts of Multipart/Alternative! --]\n"),
1435 mutt_free_body (&a);
1438 /* handles message/rfc822 body parts */
1439 void message_handler (BODY * a, STATE * s)
1445 off_start = ftell (s->fpin);
1446 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1447 a->encoding == ENCUUENCODED) {
1448 fstat (fileno (s->fpin), &st);
1449 b = mutt_new_body ();
1450 b->length = (long) st.st_size;
1451 b->parts = mutt_parse_messageRFC822 (s->fpin, b);
1457 mutt_copy_hdr (s->fpin, s->fpout, off_start, b->parts->offset,
1458 (((s->flags & M_WEED)
1459 || ((s->flags & (M_DISPLAY | M_PRINTING))
1460 && option (OPTWEED))) ? (CH_WEED | CH_REORDER) : 0) |
1461 (s->prefix ? CH_PREFIX : 0) | CH_DECODE | CH_FROM,
1465 state_puts (s->prefix, s);
1466 state_putc ('\n', s);
1468 mutt_body_handler (b->parts, s);
1471 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1472 a->encoding == ENCUUENCODED)
1473 mutt_free_body (&b);
1476 /* returns 1 if decoding the attachment will produce output */
1477 int mutt_can_decode (BODY * a)
1481 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
1482 if (mutt_is_autoview (a, type))
1483 return (rfc1524_mailcap_lookup (a, type, NULL, M_AUTOVIEW));
1484 else if (a->type == TYPETEXT)
1486 else if (a->type == TYPEMESSAGE)
1488 else if (a->type == TYPEMULTIPART) {
1492 if (ascii_strcasecmp (a->subtype, "signed") == 0 ||
1493 ascii_strcasecmp (a->subtype, "encrypted") == 0)
1497 for (p = a->parts; p; p = p->next) {
1498 if (mutt_can_decode (p))
1503 else if (WithCrypto && a->type == TYPEAPPLICATION) {
1504 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (a))
1506 if ((WithCrypto & APPLICATION_SMIME) && mutt_is_application_smime (a))
1513 void multipart_handler (BODY * a, STATE * s)
1520 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1521 a->encoding == ENCUUENCODED) {
1522 fstat (fileno (s->fpin), &st);
1523 b = mutt_new_body ();
1524 b->length = (long) st.st_size;
1525 b->parts = mutt_parse_multipart (s->fpin,
1526 mutt_get_parameter ("boundary",
1529 ascii_strcasecmp ("digest",
1535 for (p = b->parts, count = 1; p; p = p->next, count++) {
1536 if (s->flags & M_DISPLAY) {
1537 state_mark_attach (s);
1538 state_printf (s, _("[-- Attachment #%d"), count);
1539 if (p->description || p->filename || p->form_name) {
1540 state_puts (": ", s);
1541 state_puts (p->description ? p->description :
1542 p->filename ? p->filename : p->form_name, s);
1544 state_puts (" --]\n", s);
1546 mutt_pretty_size (length, sizeof (length), p->length);
1548 state_mark_attach (s);
1549 state_printf (s, _("[-- Type: %s/%s, Encoding: %s, Size: %s --]\n"),
1550 TYPE (p), p->subtype, ENCODING (p->encoding), length);
1551 if (!option (OPTWEED)) {
1552 fseek (s->fpin, p->hdr_offset, 0);
1553 mutt_copy_bytes (s->fpin, s->fpout, p->offset - p->hdr_offset);
1556 state_putc ('\n', s);
1559 if (p->description && mutt_can_decode (p))
1560 state_printf (s, "Content-Description: %s\n", p->description);
1563 state_printf (s, "%s: \n", p->form_name);
1566 mutt_body_handler (p, s);
1567 state_putc ('\n', s);
1568 if ((s->flags & M_REPLYING)
1569 && (option (OPTINCLUDEONLYFIRST)) && (s->flags & M_FIRSTDONE))
1573 if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1574 a->encoding == ENCUUENCODED)
1575 mutt_free_body (&b);
1578 void autoview_handler (BODY * a, STATE * s)
1580 rfc1524_entry *entry = rfc1524_new_entry ();
1581 char buffer[LONG_STRING];
1583 char command[LONG_STRING];
1584 char tempfile[_POSIX_PATH_MAX] = "";
1592 snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
1593 rfc1524_mailcap_lookup (a, type, entry, M_AUTOVIEW);
1595 fname = safe_strdup (a->filename);
1596 mutt_sanitize_filename (fname, 1);
1597 rfc1524_expand_filename (entry->nametemplate, fname, tempfile,
1601 if (entry->command) {
1602 strfcpy (command, entry->command, sizeof (command));
1604 /* rfc1524_expand_command returns 0 if the file is required */
1606 rfc1524_expand_command (a, tempfile, type, command, sizeof (command));
1608 if (s->flags & M_DISPLAY) {
1609 state_mark_attach (s);
1610 state_printf (s, _("[-- Autoview using %s --]\n"), command);
1611 mutt_message (_("Invoking autoview command: %s"), command);
1614 if ((fpin = safe_fopen (tempfile, "w+")) == NULL) {
1615 mutt_perror ("fopen");
1616 rfc1524_free_entry (&entry);
1620 mutt_copy_bytes (s->fpin, fpin, a->length);
1623 safe_fclose (&fpin);
1624 thepid = mutt_create_filter (command, NULL, &fpout, &fperr);
1630 thepid = mutt_create_filter_fd (command, NULL, &fpout, &fperr,
1631 fileno (fpin), -1, -1);
1635 mutt_perror _("Can't create filter");
1637 if (s->flags & M_DISPLAY) {
1638 state_mark_attach (s);
1639 state_printf (s, _("[-- Can't run %s. --]\n"), command);
1645 while (fgets (buffer, sizeof (buffer), fpout) != NULL) {
1646 state_puts (s->prefix, s);
1647 state_puts (buffer, s);
1649 /* check for data on stderr */
1650 if (fgets (buffer, sizeof (buffer), fperr)) {
1651 if (s->flags & M_DISPLAY) {
1652 state_mark_attach (s);
1653 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1656 state_puts (s->prefix, s);
1657 state_puts (buffer, s);
1658 while (fgets (buffer, sizeof (buffer), fperr) != NULL) {
1659 state_puts (s->prefix, s);
1660 state_puts (buffer, s);
1665 mutt_copy_stream (fpout, s->fpout);
1666 /* Check for stderr messages */
1667 if (fgets (buffer, sizeof (buffer), fperr)) {
1668 if (s->flags & M_DISPLAY) {
1669 state_mark_attach (s);
1670 state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1673 state_puts (buffer, s);
1674 mutt_copy_stream (fperr, s->fpout);
1679 safe_fclose (&fpout);
1680 safe_fclose (&fperr);
1682 mutt_wait_filter (thepid);
1684 safe_fclose (&fpin);
1686 mutt_unlink (tempfile);
1688 if (s->flags & M_DISPLAY)
1689 mutt_clear_error ();
1691 rfc1524_free_entry (&entry);
1694 static void external_body_handler (BODY * b, STATE * s)
1696 const char *access_type;
1697 const char *expiration;
1700 access_type = mutt_get_parameter ("access-type", b->parameter);
1702 if (s->flags & M_DISPLAY) {
1703 state_mark_attach (s);
1705 ("[-- Error: message/external-body has no access-type parameter --]\n"),
1711 expiration = mutt_get_parameter ("expiration", b->parameter);
1713 expire = mutt_parse_date (expiration, NULL);
1717 if (!ascii_strcasecmp (access_type, "x-mutt-deleted")) {
1718 if (s->flags & (M_DISPLAY | M_PRINTING)) {
1720 char pretty_size[10];
1722 state_mark_attach (s);
1723 state_printf (s, _("[-- This %s/%s attachment "),
1724 TYPE (b->parts), b->parts->subtype);
1725 length = mutt_get_parameter ("length", b->parameter);
1727 mutt_pretty_size (pretty_size, sizeof (pretty_size),
1728 strtol (length, NULL, 10));
1729 state_printf (s, _("(size %s bytes) "), pretty_size);
1731 state_puts (_("has been deleted --]\n"), s);
1734 state_mark_attach (s);
1735 state_printf (s, _("[-- on %s --]\n"), expiration);
1737 if (b->parts->filename) {
1738 state_mark_attach (s);
1739 state_printf (s, _("[-- name: %s --]\n"), b->parts->filename);
1742 mutt_copy_hdr (s->fpin, s->fpout, ftell (s->fpin), b->parts->offset,
1743 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1747 else if (expiration && expire < time (NULL)) {
1748 if (s->flags & M_DISPLAY) {
1749 state_mark_attach (s);
1750 state_printf (s, _("[-- This %s/%s attachment is not included, --]\n"),
1751 TYPE (b->parts), b->parts->subtype);
1752 state_attach_puts (_("[-- and the indicated external source has --]\n"
1753 "[-- expired. --]\n"), s);
1755 mutt_copy_hdr (s->fpin, s->fpout, ftell (s->fpin), b->parts->offset,
1756 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1761 if (s->flags & M_DISPLAY) {
1762 state_mark_attach (s);
1764 _("[-- This %s/%s attachment is not included, --]\n"),
1765 TYPE (b->parts), b->parts->subtype);
1766 state_mark_attach (s);
1769 ("[-- and the indicated access-type %s is unsupported --]\n"),
1771 mutt_copy_hdr (s->fpin, s->fpout, ftell (s->fpin), b->parts->offset,
1772 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1778 void mutt_decode_attachment (BODY * b, STATE * s)
1780 int istext = mutt_is_text_part (b);
1781 iconv_t cd = (iconv_t) (-1);
1786 if (s->flags & M_CHARCONV) {
1787 char *charset = mutt_get_parameter ("charset", b->parameter);
1789 if (!option (OPTSTRICTMIME) && !charset)
1790 charset = mutt_get_first_charset (AssumedCharset);
1791 if (charset && Charset)
1792 cd = mutt_iconv_open (Charset, charset, M_ICONV_HOOK_FROM);
1795 if (b->file_charset)
1796 cd = mutt_iconv_open (Charset, b->file_charset, M_ICONV_HOOK_FROM);
1800 fseek (s->fpin, b->offset, 0);
1801 switch (b->encoding) {
1802 case ENCQUOTEDPRINTABLE:
1803 mutt_decode_quoted (s, b->length, istext, cd);
1806 mutt_decode_base64 (s, b->length, istext, cd);
1809 mutt_decode_uuencoded (s, b->length, istext, cd);
1812 mutt_decode_xbit (s, b->length, istext, cd);
1816 if (cd != (iconv_t) (-1))
1820 void mutt_body_handler (BODY * b, STATE * s)
1825 char tempfile[_POSIX_PATH_MAX];
1826 handler_t handler = NULL;
1828 size_t tmplength = 0;
1831 int oflags = s->flags;
1833 /* first determine which handler to use to process this part */
1835 snprintf (type, sizeof (type), "%s/%s", TYPE (b), b->subtype);
1836 if (mutt_is_autoview (b, type)) {
1837 rfc1524_entry *entry = rfc1524_new_entry ();
1839 if (rfc1524_mailcap_lookup (b, type, entry, M_AUTOVIEW)) {
1840 handler = autoview_handler;
1841 s->flags &= ~M_CHARCONV;
1843 rfc1524_free_entry (&entry);
1845 else if (b->type == TYPETEXT) {
1846 if (ascii_strcasecmp ("plain", b->subtype) == 0) {
1847 /* avoid copying this part twice since removing the transfer-encoding is
1848 * the only operation needed.
1850 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b))
1851 handler = crypt_pgp_application_pgp_handler;
1853 if (ascii_strcasecmp
1854 ("flowed", mutt_get_parameter ("format", b->parameter)) == 0)
1855 handler = text_plain_flowed_handler;
1859 else if (ascii_strcasecmp ("enriched", b->subtype) == 0)
1860 handler = text_enriched_handler;
1861 else /* text body type without a handler */
1864 else if (b->type == TYPEMESSAGE) {
1865 if (mutt_is_message_type (b->type, b->subtype))
1866 handler = message_handler;
1867 else if (!ascii_strcasecmp ("delivery-status", b->subtype))
1869 else if (!ascii_strcasecmp ("external-body", b->subtype))
1870 handler = external_body_handler;
1872 else if (b->type == TYPEMULTIPART) {
1875 if (ascii_strcasecmp ("alternative", b->subtype) == 0)
1876 handler = alternative_handler;
1877 else if (WithCrypto && ascii_strcasecmp ("signed", b->subtype) == 0) {
1878 p = mutt_get_parameter ("protocol", b->parameter);
1881 mutt_error (_("Error: multipart/signed has no protocol."));
1883 else if (s->flags & M_VERIFY)
1884 handler = mutt_signed_handler;
1886 else if ((WithCrypto & APPLICATION_PGP)
1887 && mutt_strcasecmp ("encrypted", b->subtype) == 0) {
1888 p = mutt_get_parameter ("protocol", b->parameter);
1892 ("Error: multipart/encrypted has no protocol parameter!"));
1894 else if (ascii_strcasecmp ("application/pgp-encrypted", p) == 0)
1895 handler = crypt_pgp_encrypted_handler;
1899 handler = multipart_handler;
1901 else if (WithCrypto && b->type == TYPEAPPLICATION) {
1902 if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b))
1903 handler = crypt_pgp_application_pgp_handler;
1904 if ((WithCrypto & APPLICATION_SMIME) && mutt_is_application_smime (b))
1905 handler = crypt_smime_application_smime_handler;
1909 if (plaintext || handler) {
1910 fseek (s->fpin, b->offset, 0);
1912 /* see if we need to decode this part before processing it */
1913 if (b->encoding == ENCBASE64 || b->encoding == ENCQUOTEDPRINTABLE || b->encoding == ENCUUENCODED || plaintext || mutt_is_text_part (b)) { /* text subtypes may
1915 * set conversion even
1916 * with 8bit encoding.
1918 int origType = b->type;
1919 char *savePrefix = NULL;
1922 /* decode to a tempfile, saving the original destination */
1924 mutt_mktemp (tempfile);
1925 if ((s->fpout = safe_fopen (tempfile, "w")) == NULL) {
1926 mutt_error _("Unable to open temporary file!");
1930 /* decoding the attachment changes the size and offset, so save a copy
1931 * of the "real" values now, and restore them after processing
1933 tmplength = b->length;
1934 tmpoffset = b->offset;
1936 /* if we are decoding binary bodies, we don't want to prefix each
1937 * line with the prefix or else the data will get corrupted.
1939 savePrefix = s->prefix;
1947 mutt_decode_attachment (b, s);
1950 b->length = ftell (s->fpout);
1954 /* restore final destination and substitute the tempfile for input */
1957 s->fpin = safe_fopen (tempfile, "r");
1960 /* restore the prefix */
1961 s->prefix = savePrefix;
1967 /* process the (decoded) body part */
1972 b->length = tmplength;
1973 b->offset = tmpoffset;
1975 /* restore the original source stream */
1980 s->flags |= M_FIRSTDONE;
1982 else if (s->flags & M_DISPLAY) {
1983 state_mark_attach (s);
1984 state_printf (s, _("[-- %s/%s is unsupported "), TYPE (b), b->subtype);
1985 if (!option (OPTVIEWATTACH)) {
1987 (type, sizeof (type),
1988 km_find_func (MENU_PAGER, OP_VIEW_ATTACHMENTS)))
1989 fprintf (s->fpout, _("(use '%s' to view this part)"), type);
1991 fputs (_("(need 'view-attachments' bound to key!)"), s->fpout);
1993 fputs (" --]\n", s->fpout);
1997 s->flags = oflags | (s->flags & M_FIRSTDONE);