4ba746c82e43c04195b0fabe4f9794dbf73ee94a
[apps/madmutt.git] / handler.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4  *
5  * This file is part of mutt-ng, see http://www.muttng.org/.
6  * It's licensed under the GNU General Public License,
7  * please see the file GPL in the top level source directory.
8  */
9
10 #if HAVE_CONFIG_H
11 # include "config.h"
12 #endif
13
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <ctype.h>
18 #include <sys/wait.h>
19 #include <sys/stat.h>
20
21 #include "mutt.h"
22 #include "mutt_curses.h"
23 #include "rfc1524.h"
24 #include "keymap.h"
25 #include "mime.h"
26 #include "copy.h"
27 #include "charset.h"
28 #include "mutt_crypt.h"
29 #include "state.h"
30 #include "attach.h"
31 #include "lib.h"
32
33 #include "lib/mem.h"
34 #include "lib/intl.h"
35 #include "lib/str.h"
36 #include "lib/debug.h"
37
38 typedef void handler_f (BODY *, STATE *);
39 typedef handler_f *handler_t;
40
41 int Index_hex[128] = {
42   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
43   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
44   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
45   0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
46   -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
47   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
48   -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
49   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
50 };
51
52 int Index_64[128] = {
53   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
54   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
55   -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
56   52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
57   -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
58   15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
59   -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
60   41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1
61 };
62
63 void mutt_decode_xbit (STATE * s, long len, int istext, iconv_t cd)
64 {
65   int c, ch;
66   char bufi[BUFI_SIZE];
67   size_t l = 0;
68
69   if (istext) {
70     state_set_prefix (s);
71
72     while ((c = fgetc (s->fpin)) != EOF && len--) {
73       if (c == '\r' && len) {
74         if ((ch = fgetc (s->fpin)) == '\n') {
75           c = ch;
76           len--;
77         }
78         else
79           ungetc (ch, s->fpin);
80       }
81
82       bufi[l++] = c;
83       if (l == sizeof (bufi))
84         mutt_convert_to_state (cd, bufi, &l, s);
85     }
86
87     mutt_convert_to_state (cd, bufi, &l, s);
88     mutt_convert_to_state (cd, 0, 0, s);
89
90     state_reset_prefix (s);
91   }
92   else
93     mutt_copy_bytes (s->fpin, s->fpout, len);
94 }
95
96 static int qp_decode_triple (char *s, char *d)
97 {
98   /* soft line break */
99   if (*s == '=' && !(*(s + 1)))
100     return 1;
101
102   /* quoted-printable triple */
103   if (*s == '=' &&
104       isxdigit ((unsigned char) *(s + 1)) &&
105       isxdigit ((unsigned char) *(s + 2))) {
106     *d = (hexval (*(s + 1)) << 4) | hexval (*(s + 2));
107     return 0;
108   }
109
110   /* something else */
111   return -1;
112 }
113
114 static void qp_decode_line (char *dest, char *src, size_t * l, int last)
115 {
116   char *d, *s;
117   char c;
118
119   int kind;
120   int soft = 0;
121
122   /* decode the line */
123
124   for (d = dest, s = src; *s;) {
125     switch ((kind = qp_decode_triple (s, &c))) {
126     case 0:
127       *d++ = c;
128       s += 3;
129       break;                    /* qp triple */
130     case -1:
131       *d++ = *s++;
132       break;                    /* single character */
133     case 1:
134       soft = 1;
135       s++;
136       break;                    /* soft line break */
137     }
138   }
139
140   if (!soft && last == '\n')
141     *d++ = '\n';
142
143   *d = '\0';
144   *l = d - dest;
145 }
146
147 /* 
148  * Decode an attachment encoded with quoted-printable.
149  * 
150  * Why doesn't this overflow any buffers?  First, it's guaranteed
151  * that the length of a line grows when you _en_-code it to
152  * quoted-printable.  That means that we always can store the
153  * result in a buffer of at most the _same_ size.
154  * 
155  * Now, we don't special-case if the line we read with fgets()
156  * isn't terminated.  We don't care about this, since STRING > 78,
157  * so corrupted input will just be corrupted a bit more.  That
158  * implies that STRING+1 bytes are always sufficient to store the
159  * result of qp_decode_line.
160  * 
161  * Finally, at soft line breaks, some part of a multibyte character
162  * may have been left over by mutt_convert_to_state().  This shouldn't
163  * be more than 6 characters, so STRING + 7 should be sufficient
164  * memory to store the decoded data.
165  * 
166  * Just to make sure that I didn't make some off-by-one error
167  * above, we just use STRING*2 for the target buffer's size.
168  * 
169  */
170
171 void mutt_decode_quoted (STATE * s, long len, int istext, iconv_t cd)
172 {
173   char line[STRING];
174   char decline[2 * STRING];
175   size_t l = 0;
176   size_t linelen;               /* number of input bytes in `line' */
177   size_t l3;
178
179   int last;                     /* store the last character in the input line */
180
181   if (istext)
182     state_set_prefix (s);
183
184   while (len > 0) {
185     last = 0;
186
187     /*
188      * It's ok to use a fixed size buffer for input, even if the line turns
189      * out to be longer than this.  Just process the line in chunks.  This
190      * really shouldn't happen according the MIME spec, since Q-P encoded
191      * lines are at most 76 characters, but we should be liberal about what
192      * we accept.
193      */
194     if (fgets (line, MIN ((ssize_t) sizeof (line), len + 1), s->fpin) == NULL)
195       break;
196
197     linelen = str_len (line);
198     len -= linelen;
199
200     /*
201      * inspect the last character we read so we can tell if we got the
202      * entire line.
203      */
204     last = linelen ? line[linelen - 1] : 0;
205
206     /* chop trailing whitespace if we got the full line */
207     if (last == '\n') {
208       while (linelen > 0 && ISSPACE (line[linelen - 1]))
209         linelen--;
210       line[linelen] = 0;
211     }
212
213     /* decode and do character set conversion */
214     qp_decode_line (decline + l, line, &l3, last);
215     l += l3;
216     mutt_convert_to_state (cd, decline, &l, s);
217   }
218
219   mutt_convert_to_state (cd, 0, 0, s);
220   state_reset_prefix (s);
221 }
222
223 void mutt_decode_base64 (STATE * s, long len, int istext, iconv_t cd)
224 {
225   char buf[5];
226   int c1, c2, c3, c4, ch, cr = 0, i;
227   char bufi[BUFI_SIZE];
228   size_t l = 0;
229
230   buf[4] = 0;
231
232   if (istext)
233     state_set_prefix (s);
234
235   while (len > 0) {
236     for (i = 0; i < 4 && len > 0; len--) {
237       if ((ch = fgetc (s->fpin)) == EOF)
238         break;
239       if (ch >= 0 && ch < 128 && (base64val (ch) != -1 || ch == '='))
240         buf[i++] = ch;
241     }
242     if (i != 4) {
243       debug_print (2, ("didn't get a multiple of 4 chars.\n"));
244       break;
245     }
246
247     c1 = base64val (buf[0]);
248     c2 = base64val (buf[1]);
249     ch = (c1 << 2) | (c2 >> 4);
250
251     if (cr && ch != '\n')
252       bufi[l++] = '\r';
253
254     cr = 0;
255
256     if (istext && ch == '\r')
257       cr = 1;
258     else
259       bufi[l++] = ch;
260
261     if (buf[2] == '=')
262       break;
263     c3 = base64val (buf[2]);
264     ch = ((c2 & 0xf) << 4) | (c3 >> 2);
265
266     if (cr && ch != '\n')
267       bufi[l++] = '\r';
268
269     cr = 0;
270
271     if (istext && ch == '\r')
272       cr = 1;
273     else
274       bufi[l++] = ch;
275
276     if (buf[3] == '=')
277       break;
278     c4 = base64val (buf[3]);
279     ch = ((c3 & 0x3) << 6) | c4;
280
281     if (cr && ch != '\n')
282       bufi[l++] = '\r';
283     cr = 0;
284
285     if (istext && ch == '\r')
286       cr = 1;
287     else
288       bufi[l++] = ch;
289
290     if (l + 8 >= sizeof (bufi))
291       mutt_convert_to_state (cd, bufi, &l, s);
292   }
293
294   if (cr)
295     bufi[l++] = '\r';
296
297   mutt_convert_to_state (cd, bufi, &l, s);
298   mutt_convert_to_state (cd, 0, 0, s);
299
300   state_reset_prefix (s);
301 }
302
303 unsigned char decode_byte (char ch)
304 {
305   if (ch == 96)
306     return 0;
307   return ch - 32;
308 }
309
310 void mutt_decode_uuencoded (STATE * s, long len, int istext, iconv_t cd)
311 {
312   char tmps[SHORT_STRING];
313   char linelen, c, l, out;
314   char *pt;
315   char bufi[BUFI_SIZE];
316   size_t k = 0;
317
318   if (istext)
319     state_set_prefix (s);
320
321   while (len > 0) {
322     if ((fgets (tmps, sizeof (tmps), s->fpin)) == NULL)
323       return;
324     len -= str_len (tmps);
325     if ((!str_ncmp (tmps, "begin", 5)) && ISSPACE (tmps[5]))
326       break;
327   }
328   while (len > 0) {
329     if ((fgets (tmps, sizeof (tmps), s->fpin)) == NULL)
330       return;
331     len -= str_len (tmps);
332     if (!str_ncmp (tmps, "end", 3))
333       break;
334     pt = tmps;
335     linelen = decode_byte (*pt);
336     pt++;
337     for (c = 0; c < linelen;) {
338       for (l = 2; l <= 6; l += 2) {
339         out = decode_byte (*pt) << l;
340         pt++;
341         out |= (decode_byte (*pt) >> (6 - l));
342         bufi[k++] = out;
343         c++;
344         if (c == linelen)
345           break;
346       }
347       mutt_convert_to_state (cd, bufi, &k, s);
348       pt++;
349     }
350   }
351
352   mutt_convert_to_state (cd, bufi, &k, s);
353   mutt_convert_to_state (cd, 0, 0, s);
354
355   state_reset_prefix (s);
356 }
357
358 /* ----------------------------------------------------------------------------
359  * A (not so) minimal implementation of RFC1563.
360  */
361
362 #define IndentSize (4)
363
364 enum { RICH_PARAM = 0, RICH_BOLD, RICH_UNDERLINE, RICH_ITALIC, RICH_NOFILL,
365   RICH_INDENT, RICH_INDENT_RIGHT, RICH_EXCERPT, RICH_CENTER, RICH_FLUSHLEFT,
366   RICH_FLUSHRIGHT, RICH_COLOR, RICH_LAST_TAG
367 };
368
369 static struct {
370   const char *tag_name;
371   int index;
372 } EnrichedTags[] = {
373   {
374   "param", RICH_PARAM}, {
375   "bold", RICH_BOLD}, {
376   "italic", RICH_ITALIC}, {
377   "underline", RICH_UNDERLINE}, {
378   "nofill", RICH_NOFILL}, {
379   "excerpt", RICH_EXCERPT}, {
380   "indent", RICH_INDENT}, {
381   "indentright", RICH_INDENT_RIGHT}, {
382   "center", RICH_CENTER}, {
383   "flushleft", RICH_FLUSHLEFT}, {
384   "flushright", RICH_FLUSHRIGHT}, {
385   "flushboth", RICH_FLUSHLEFT}, {
386   "color", RICH_COLOR}, {
387   "x-color", RICH_COLOR}, {
388   NULL, -1}
389 };
390
391 struct enriched_state {
392   char *buffer;
393   char *line;
394   char *param;
395   size_t buff_len;
396   size_t line_len;
397   size_t line_used;
398   size_t line_max;
399   size_t indent_len;
400   size_t word_len;
401   size_t buff_used;
402   size_t param_used;
403   size_t param_len;
404   int tag_level[RICH_LAST_TAG];
405   int WrapMargin;
406   STATE *s;
407 };
408
409 static void enriched_wrap (struct enriched_state *stte)
410 {
411   int x;
412   int extra;
413
414   if (stte->line_len) {
415     if (stte->tag_level[RICH_CENTER] || stte->tag_level[RICH_FLUSHRIGHT]) {
416       /* Strip trailing white space */
417       size_t y = stte->line_used - 1;
418
419       while (y && ISSPACE (stte->line[y])) {
420         stte->line[y] = '\0';
421         y--;
422         stte->line_used--;
423         stte->line_len--;
424       }
425       if (stte->tag_level[RICH_CENTER]) {
426         /* Strip leading whitespace */
427         y = 0;
428
429         while (stte->line[y] && ISSPACE (stte->line[y]))
430           y++;
431         if (y) {
432           size_t z;
433
434           for (z = y; z <= stte->line_used; z++) {
435             stte->line[z - y] = stte->line[z];
436           }
437
438           stte->line_len -= y;
439           stte->line_used -= y;
440         }
441       }
442     }
443
444     extra = stte->WrapMargin - stte->line_len - stte->indent_len -
445       (stte->tag_level[RICH_INDENT_RIGHT] * IndentSize);
446     if (extra > 0) {
447       if (stte->tag_level[RICH_CENTER]) {
448         x = extra / 2;
449         while (x) {
450           state_putc (' ', stte->s);
451           x--;
452         }
453       }
454       else if (stte->tag_level[RICH_FLUSHRIGHT]) {
455         x = extra - 1;
456         while (x) {
457           state_putc (' ', stte->s);
458           x--;
459         }
460       }
461     }
462     state_puts (stte->line, stte->s);
463   }
464
465   state_putc ('\n', stte->s);
466   stte->line[0] = '\0';
467   stte->line_len = 0;
468   stte->line_used = 0;
469   stte->indent_len = 0;
470   if (stte->s->prefix) {
471     state_puts (stte->s->prefix, stte->s);
472     stte->indent_len += str_len (stte->s->prefix);
473   }
474
475   if (stte->tag_level[RICH_EXCERPT]) {
476     x = stte->tag_level[RICH_EXCERPT];
477     while (x) {
478       if (stte->s->prefix) {
479         state_puts (stte->s->prefix, stte->s);
480         stte->indent_len += str_len (stte->s->prefix);
481       }
482       else {
483         state_puts ("> ", stte->s);
484         stte->indent_len += str_len ("> ");
485       }
486       x--;
487     }
488   }
489   else
490     stte->indent_len = 0;
491   if (stte->tag_level[RICH_INDENT]) {
492     x = stte->tag_level[RICH_INDENT] * IndentSize;
493     stte->indent_len += x;
494     while (x) {
495       state_putc (' ', stte->s);
496       x--;
497     }
498   }
499 }
500
501 static void enriched_flush (struct enriched_state *stte, int wrap)
502 {
503   if (!stte->tag_level[RICH_NOFILL] && (stte->line_len + stte->word_len >
504                                         (stte->WrapMargin -
505                                          (stte->tag_level[RICH_INDENT_RIGHT] *
506                                           IndentSize) - stte->indent_len)))
507     enriched_wrap (stte);
508
509   if (stte->buff_used) {
510     stte->buffer[stte->buff_used] = '\0';
511     stte->line_used += stte->buff_used;
512     if (stte->line_used > stte->line_max) {
513       stte->line_max = stte->line_used;
514       mem_realloc (&stte->line, stte->line_max + 1);
515     }
516     strcat (stte->line, stte->buffer);  /* __STRCAT_CHECKED__ */
517     stte->line_len += stte->word_len;
518     stte->word_len = 0;
519     stte->buff_used = 0;
520   }
521   if (wrap)
522     enriched_wrap (stte);
523 }
524
525
526 static void enriched_putc (int c, struct enriched_state *stte)
527 {
528   if (stte->tag_level[RICH_PARAM]) {
529     if (stte->tag_level[RICH_COLOR]) {
530       if (stte->param_used + 1 >= stte->param_len)
531         mem_realloc (&stte->param, (stte->param_len += STRING));
532
533       stte->param[stte->param_used++] = c;
534     }
535     return;                     /* nothing to do */
536   }
537
538   /* see if more space is needed (plus extra for possible rich characters) */
539   if (stte->buff_len < stte->buff_used + 3) {
540     stte->buff_len += LONG_STRING;
541     mem_realloc (&stte->buffer, stte->buff_len + 1);
542   }
543
544   if ((!stte->tag_level[RICH_NOFILL] && ISSPACE (c)) || c == '\0') {
545     if (c == '\t')
546       stte->word_len += 8 - (stte->line_len + stte->word_len) % 8;
547     else
548       stte->word_len++;
549
550     stte->buffer[stte->buff_used++] = c;
551     enriched_flush (stte, 0);
552   }
553   else {
554     if (stte->s->flags & M_DISPLAY) {
555       if (stte->tag_level[RICH_BOLD]) {
556         stte->buffer[stte->buff_used++] = c;
557         stte->buffer[stte->buff_used++] = '\010';
558         stte->buffer[stte->buff_used++] = c;
559       }
560       else if (stte->tag_level[RICH_UNDERLINE]) {
561
562         stte->buffer[stte->buff_used++] = '_';
563         stte->buffer[stte->buff_used++] = '\010';
564         stte->buffer[stte->buff_used++] = c;
565       }
566       else if (stte->tag_level[RICH_ITALIC]) {
567         stte->buffer[stte->buff_used++] = c;
568         stte->buffer[stte->buff_used++] = '\010';
569         stte->buffer[stte->buff_used++] = '_';
570       }
571       else {
572         stte->buffer[stte->buff_used++] = c;
573       }
574     }
575     else {
576       stte->buffer[stte->buff_used++] = c;
577     }
578     stte->word_len++;
579   }
580 }
581
582 static void enriched_puts (char *s, struct enriched_state *stte)
583 {
584   char *c;
585
586   if (stte->buff_len < stte->buff_used + str_len (s)) {
587     stte->buff_len += LONG_STRING;
588     mem_realloc (&stte->buffer, stte->buff_len + 1);
589   }
590   c = s;
591   while (*c) {
592     stte->buffer[stte->buff_used++] = *c;
593     c++;
594   }
595 }
596
597 static void enriched_set_flags (const char *tag, struct enriched_state *stte)
598 {
599   const char *tagptr = tag;
600   int i, j;
601
602   if (*tagptr == '/')
603     tagptr++;
604
605   for (i = 0, j = -1; EnrichedTags[i].tag_name; i++)
606     if (ascii_strcasecmp (EnrichedTags[i].tag_name, tagptr) == 0) {
607       j = EnrichedTags[i].index;
608       break;
609     }
610
611   if (j != -1) {
612     if (j == RICH_CENTER || j == RICH_FLUSHLEFT || j == RICH_FLUSHRIGHT)
613       enriched_flush (stte, 1);
614
615     if (*tag == '/') {
616       if (stte->tag_level[j])   /* make sure not to go negative */
617         stte->tag_level[j]--;
618       if ((stte->s->flags & M_DISPLAY) && j == RICH_PARAM
619           && stte->tag_level[RICH_COLOR]) {
620         stte->param[stte->param_used] = '\0';
621         if (!ascii_strcasecmp (stte->param, "black")) {
622           enriched_puts ("\033[30m", stte);
623         }
624         else if (!ascii_strcasecmp (stte->param, "red")) {
625           enriched_puts ("\033[31m", stte);
626         }
627         else if (!ascii_strcasecmp (stte->param, "green")) {
628           enriched_puts ("\033[32m", stte);
629         }
630         else if (!ascii_strcasecmp (stte->param, "yellow")) {
631           enriched_puts ("\033[33m", stte);
632         }
633         else if (!ascii_strcasecmp (stte->param, "blue")) {
634           enriched_puts ("\033[34m", stte);
635         }
636         else if (!ascii_strcasecmp (stte->param, "magenta")) {
637           enriched_puts ("\033[35m", stte);
638         }
639         else if (!ascii_strcasecmp (stte->param, "cyan")) {
640           enriched_puts ("\033[36m", stte);
641         }
642         else if (!ascii_strcasecmp (stte->param, "white")) {
643           enriched_puts ("\033[37m", stte);
644         }
645       }
646       if ((stte->s->flags & M_DISPLAY) && j == RICH_COLOR) {
647         enriched_puts ("\033[0m", stte);
648       }
649
650       /* flush parameter buffer when closing the tag */
651       if (j == RICH_PARAM) {
652         stte->param_used = 0;
653         stte->param[0] = '\0';
654       }
655     }
656     else
657       stte->tag_level[j]++;
658
659     if (j == RICH_EXCERPT)
660       enriched_flush (stte, 1);
661   }
662 }
663
664 void text_enriched_handler (BODY * a, STATE * s)
665 {
666   enum {
667     TEXT, LANGLE, TAG, BOGUS_TAG, NEWLINE, ST_EOF, DONE
668   } state = TEXT;
669
670   long bytes = a->length;
671   struct enriched_state stte;
672   int c = 0;
673   int tag_len = 0;
674   char tag[LONG_STRING + 1];
675
676   memset (&stte, 0, sizeof (stte));
677   stte.s = s;
678   stte.WrapMargin =
679     ((s->flags & M_DISPLAY) ? (COLS - 4) : ((COLS - 4) <
680                                             72) ? (COLS - 4) : 72);
681   stte.line_max = stte.WrapMargin * 4;
682   stte.line = (char *) mem_calloc (1, stte.line_max + 1);
683   stte.param = (char *) mem_calloc (1, STRING);
684
685   stte.param_len = STRING;
686   stte.param_used = 0;
687
688   if (s->prefix) {
689     state_puts (s->prefix, s);
690     stte.indent_len += str_len (s->prefix);
691   }
692
693   while (state != DONE) {
694     if (state != ST_EOF) {
695       if (!bytes || (c = fgetc (s->fpin)) == EOF)
696         state = ST_EOF;
697       else
698         bytes--;
699     }
700
701     switch (state) {
702     case TEXT:
703       switch (c) {
704       case '<':
705         state = LANGLE;
706         break;
707
708       case '\n':
709         if (stte.tag_level[RICH_NOFILL]) {
710           enriched_flush (&stte, 1);
711         }
712         else {
713           enriched_putc (' ', &stte);
714           state = NEWLINE;
715         }
716         break;
717
718       default:
719         enriched_putc (c, &stte);
720       }
721       break;
722
723     case LANGLE:
724       if (c == '<') {
725         enriched_putc (c, &stte);
726         state = TEXT;
727         break;
728       }
729       else {
730         tag_len = 0;
731         state = TAG;
732       }
733       /* Yes, fall through (it wasn't a <<, so this char is first in TAG) */
734     case TAG:
735       if (c == '>') {
736         tag[tag_len] = '\0';
737         enriched_set_flags (tag, &stte);
738         state = TEXT;
739       }
740       else if (tag_len < LONG_STRING)   /* ignore overly long tags */
741         tag[tag_len++] = c;
742       else
743         state = BOGUS_TAG;
744       break;
745
746     case BOGUS_TAG:
747       if (c == '>')
748         state = TEXT;
749       break;
750
751     case NEWLINE:
752       if (c == '\n')
753         enriched_flush (&stte, 1);
754       else {
755         ungetc (c, s->fpin);
756         bytes++;
757         state = TEXT;
758       }
759       break;
760
761     case ST_EOF:
762       enriched_putc ('\0', &stte);
763       enriched_flush (&stte, 1);
764       state = DONE;
765       break;
766
767     case DONE:                 /* not reached, but gcc complains if this is absent */
768       break;
769     }
770   }
771
772   state_putc ('\n', s);         /* add a final newline */
773
774   mem_free (&(stte.buffer));
775   mem_free (&(stte.line));
776   mem_free (&(stte.param));
777 }
778
779 /*
780  * An implementation of RFC 2646.
781  *
782  * NOTE: This still has to be made UTF-8 aware.
783  *
784  */
785
786 #define FLOWED_MAX 77
787
788 static int get_quote_level (char *line)
789 {
790   int quoted;
791
792   for (quoted = 0; line[quoted] == '>'; quoted++);
793   return quoted;
794 }
795
796 static void print_flowed_line (char *line, STATE * s, int ql)
797 {
798   int width;
799   char *pos, *oldpos;
800   int len = str_len (line);
801   int i;
802
803   if (MaxLineLength > 0) {
804     width = MaxLineLength - WrapMargin - ql - 1;
805     if (option (OPTSTUFFQUOTED))
806       --width;
807     if (width < 0)
808       width = MaxLineLength;
809   }
810   else {
811     if (option (OPTMBOXPANE))
812       width = COLS - SidebarWidth - WrapMargin - ql - 1;
813     else
814       width = COLS - WrapMargin - ql - 1;
815
816     if (option (OPTSTUFFQUOTED))
817       --width;
818     if (width < 0)
819       width = COLS;
820   }
821
822   /* fprintf(stderr,"print_flowed_line will print `%s' with ql = %d\n",line,ql); */
823
824   if (str_len (line) == 0) {
825     if (option (OPTQUOTEEMPTY)) {
826       if (s->prefix)
827         state_puts(s->prefix,s);
828       for (i=0;i<ql;++i) state_putc('>',s);
829       if (option(OPTSTUFFQUOTED))
830         state_putc(' ',s);
831     }
832     state_putc('\n',s);
833     return;
834   }
835
836   pos = line + ql + width;
837   oldpos = line + ql;
838   if (ql > 0 && ISBLANK (*oldpos))
839     ++oldpos;
840
841   /* fprintf(stderr,"oldpos = %p line+len = %p\n",oldpos,line+len); */
842
843   for (; oldpos < line + len; pos += width) {
844     /* fprintf(stderr,"outer for loop\n"); */
845     if (pos < line + len) {     /* only search a new position when we're not over the end of the string w/ pos */
846       /* fprintf(stderr,"if 1\n"); */
847       if (*pos == ' ') {
848         /* fprintf(stderr,"if 2: good luck! found a space\n"); */
849         *pos = '\0';
850         ++pos;
851       }
852       else {
853         /* fprintf(stderr,"if 2: else\n"); */
854         char *save = pos;
855
856         while (pos >= oldpos && !isspace (*pos)) {
857           /* fprintf(stderr,"pos(%p) > oldpos(%p)\n",pos,oldpos); */
858           --pos;
859         }
860         if (pos < oldpos) {
861           /* fprintf(stderr,"wow, no space found, searching the other direction\n"); */
862           pos = save;
863           while (pos < line + len && *pos && !isspace (*pos)) {
864             /* fprintf(stderr,"pos(%p) < line+len(%p)\n",pos,line+len); */
865             ++pos;
866           }
867           /* fprintf(stderr,"found a space pos = %p\n",pos); */
868         }
869         *pos = '\0';
870         ++pos;
871       }
872     }
873     else {
874       /* fprintf(stderr,"if 1 else\n"); */
875     }
876     if (s->prefix)
877       state_puts (s->prefix, s);
878     for (i = 0; i < ql; ++i)
879       state_putc ('>', s);
880     if (option (OPTSTUFFQUOTED) && (ql > 0 || s->prefix))
881       state_putc (' ', s);
882     state_puts (oldpos, s);
883     /* fprintf(stderr,"print_flowed_line: `%s'\n",oldpos); */
884     if (pos < line + len)
885       state_putc (' ', s);
886     state_putc ('\n', s);
887     oldpos = pos;
888   }
889   /*state_puts(line,s);
890      state_putc('\n',s); */
891 }
892
893 static void text_plain_flowed_handler (BODY * a, STATE * s)
894 {
895   int bytes = a->length;
896   char buf[LONG_STRING];
897   char *curline = strdup ("");
898   char *t;
899   unsigned int curline_len = 1;
900   unsigned int quotelevel = 0, newql = 0;
901   int first_line = 1;
902
903   while (bytes > 0 && fgets (buf, sizeof (buf), s->fpin)) {
904
905     bytes -= str_len (buf);
906
907     newql = get_quote_level (buf);
908
909     if ((t = strrchr (buf, '\n')) || (t = strrchr (buf, '\r'))) {
910       *t = '\0';
911       if (str_len (curline) > 0 && curline[str_len (curline) - 1] == ' '
912           && newql == quotelevel
913           && strcmp (curline + quotelevel, "-- ") != 0) {
914         if (buf[newql] == ' ')
915           curline[str_len (curline) - 1] = '\0';
916
917         curline = realloc (curline, curline_len + str_len (buf));
918         if (curline_len == 1)
919           *curline = '\0';
920         curline_len += str_len (buf);
921         str_ncat (curline, curline_len, buf + newql,
922                       str_len (buf + newql));
923       }
924       else {
925         if (first_line) {
926           first_line = 0;
927         }
928         else {
929           print_flowed_line (curline, s, quotelevel);
930         }
931         mem_free (&curline);
932         curline_len = 1;
933         curline = realloc (curline, curline_len + str_len (buf));
934         if (curline_len == 1)
935           *curline = '\0';
936         curline_len += str_len (buf);
937         str_ncat (curline, curline_len, buf, str_len (buf));
938         quotelevel = newql;
939       }
940     }
941     else if (bytes == 0) {
942       /* in case there's no line end it's likely the last line
943        * so append to current (if any) */
944       if (buf[newql] == ' ')
945         curline[str_len (curline) - 1] = '\0';
946       curline = realloc (curline, curline_len + str_len (buf));
947       if (curline_len == 1)
948         *curline = '\0';
949       curline_len += str_len (buf);
950       str_ncat (curline, curline_len, buf + newql,
951                     str_len (buf + newql));
952       break;
953     }
954   }
955   if (curline) {
956     print_flowed_line (curline, s, quotelevel);
957     mem_free (&curline);
958   }
959 }
960
961 #define TXTHTML     1
962 #define TXTPLAIN    2
963 #define TXTENRICHED 3
964
965 static void alternative_handler (BODY * a, STATE * s)
966 {
967   BODY *choice = NULL;
968   BODY *b;
969   LIST *t;
970   char buf[STRING];
971   int type = 0;
972   int mustfree = 0;
973
974   if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
975       a->encoding == ENCUUENCODED) {
976     struct stat st;
977
978     mustfree = 1;
979     fstat (fileno (s->fpin), &st);
980     b = mutt_new_body ();
981     b->length = (long) st.st_size;
982     b->parts = mutt_parse_multipart (s->fpin,
983                                      mutt_get_parameter ("boundary",
984                                                          a->parameter),
985                                      (long) st.st_size,
986                                      ascii_strcasecmp ("digest",
987                                                        a->subtype) == 0);
988   }
989   else
990     b = a;
991
992   a = b;
993
994   /* First, search list of prefered types */
995   t = AlternativeOrderList;
996   while (t && !choice) {
997     char *c;
998     int btlen;                  /* length of basetype */
999     int wild;                   /* do we have a wildcard to match all subtypes? */
1000
1001     c = strchr (t->data, '/');
1002     if (c) {
1003       wild = (c[1] == '*' && c[2] == 0);
1004       btlen = c - t->data;
1005     }
1006     else {
1007       wild = 1;
1008       btlen = str_len (t->data);
1009     }
1010
1011     if (a && a->parts)
1012       b = a->parts;
1013     else
1014       b = a;
1015     while (b) {
1016       const char *bt = TYPE (b);
1017
1018       if (!ascii_strncasecmp (bt, t->data, btlen) && bt[btlen] == 0) {
1019         /* the basetype matches */
1020         if (wild || !ascii_strcasecmp (t->data + btlen + 1, b->subtype)) {
1021           choice = b;
1022         }
1023       }
1024       b = b->next;
1025     }
1026     t = t->next;
1027   }
1028
1029   /* Next, look for an autoviewable type */
1030   if (!choice) {
1031     if (a && a->parts)
1032       b = a->parts;
1033     else
1034       b = a;
1035     while (b) {
1036       snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
1037       if (mutt_is_autoview (b, buf)) {
1038         rfc1524_entry *entry = rfc1524_new_entry ();
1039
1040         if (rfc1524_mailcap_lookup (b, buf, entry, M_AUTOVIEW)) {
1041           choice = b;
1042         }
1043         rfc1524_free_entry (&entry);
1044       }
1045       b = b->next;
1046     }
1047   }
1048
1049   /* Then, look for a text entry */
1050   if (!choice) {
1051     if (a && a->parts)
1052       b = a->parts;
1053     else
1054       b = a;
1055     while (b) {
1056       if (b->type == TYPETEXT) {
1057         if (!ascii_strcasecmp ("plain", b->subtype) && type <= TXTPLAIN) {
1058           choice = b;
1059           type = TXTPLAIN;
1060         }
1061         else if (!ascii_strcasecmp ("enriched", b->subtype)
1062                  && type <= TXTENRICHED) {
1063           choice = b;
1064           type = TXTENRICHED;
1065         }
1066         else if (!ascii_strcasecmp ("html", b->subtype) && type <= TXTHTML) {
1067           choice = b;
1068           type = TXTHTML;
1069         }
1070       }
1071       b = b->next;
1072     }
1073   }
1074
1075   /* Finally, look for other possibilities */
1076   if (!choice) {
1077     if (a && a->parts)
1078       b = a->parts;
1079     else
1080       b = a;
1081     while (b) {
1082       if (mutt_can_decode (b))
1083         choice = b;
1084       b = b->next;
1085     }
1086   }
1087
1088   if (choice) {
1089     if (s->flags & M_DISPLAY && !option (OPTWEED)) {
1090       fseek (s->fpin, choice->hdr_offset, 0);
1091       mutt_copy_bytes (s->fpin, s->fpout,
1092                        choice->offset - choice->hdr_offset);
1093     }
1094     mutt_body_handler (choice, s);
1095   }
1096   else if (s->flags & M_DISPLAY) {
1097     /* didn't find anything that we could display! */
1098     state_mark_attach (s);
1099     state_puts (_
1100                 ("[-- Error:  Could not display any parts of Multipart/Alternative! --]\n"),
1101                 s);
1102   }
1103
1104   if (mustfree)
1105     mutt_free_body (&a);
1106 }
1107
1108 /* handles message/rfc822 body parts */
1109 void message_handler (BODY * a, STATE * s)
1110 {
1111   struct stat st;
1112   BODY *b;
1113   long off_start;
1114
1115   off_start = ftell (s->fpin);
1116   if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1117       a->encoding == ENCUUENCODED) {
1118     fstat (fileno (s->fpin), &st);
1119     b = mutt_new_body ();
1120     b->length = (long) st.st_size;
1121     b->parts = mutt_parse_messageRFC822 (s->fpin, b);
1122   }
1123   else
1124     b = a;
1125
1126   if (b->parts) {
1127     mutt_copy_hdr (s->fpin, s->fpout, off_start, b->parts->offset,
1128                    (((s->flags & M_WEED)
1129                      || ((s->flags & (M_DISPLAY | M_PRINTING))
1130                          && option (OPTWEED))) ? (CH_WEED | CH_REORDER) : 0) |
1131                    (s->prefix ? CH_PREFIX : 0) | CH_DECODE | CH_FROM,
1132                    s->prefix);
1133
1134     if (s->prefix)
1135       state_puts (s->prefix, s);
1136     state_putc ('\n', s);
1137
1138     mutt_body_handler (b->parts, s);
1139   }
1140
1141   if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1142       a->encoding == ENCUUENCODED)
1143     mutt_free_body (&b);
1144 }
1145
1146 /* returns 1 if decoding the attachment will produce output */
1147 int mutt_can_decode (BODY * a)
1148 {
1149   char type[STRING];
1150
1151   snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
1152   if (mutt_is_autoview (a, type))
1153     return (rfc1524_mailcap_lookup (a, type, NULL, M_AUTOVIEW));
1154   else if (a->type == TYPETEXT)
1155     return (1);
1156   else if (a->type == TYPEMESSAGE)
1157     return (1);
1158   else if (a->type == TYPEMULTIPART) {
1159     BODY *p;
1160
1161     if (WithCrypto) {
1162       if (ascii_strcasecmp (a->subtype, "signed") == 0 ||
1163           ascii_strcasecmp (a->subtype, "encrypted") == 0)
1164         return (1);
1165     }
1166
1167     for (p = a->parts; p; p = p->next) {
1168       if (mutt_can_decode (p))
1169         return (1);
1170     }
1171
1172   }
1173   else if (WithCrypto && a->type == TYPEAPPLICATION) {
1174     if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (a))
1175       return (1);
1176     if ((WithCrypto & APPLICATION_SMIME) && mutt_is_application_smime (a))
1177       return (1);
1178   }
1179
1180   return (0);
1181 }
1182
1183 void multipart_handler (BODY * a, STATE * s)
1184 {
1185   BODY *b, *p;
1186   char length[5];
1187   struct stat st;
1188   int count;
1189
1190   if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1191       a->encoding == ENCUUENCODED) {
1192     fstat (fileno (s->fpin), &st);
1193     b = mutt_new_body ();
1194     b->length = (long) st.st_size;
1195     b->parts = mutt_parse_multipart (s->fpin,
1196                                      mutt_get_parameter ("boundary",
1197                                                          a->parameter),
1198                                      (long) st.st_size,
1199                                      ascii_strcasecmp ("digest",
1200                                                        a->subtype) == 0);
1201   }
1202   else
1203     b = a;
1204
1205   for (p = b->parts, count = 1; p; p = p->next, count++) {
1206     if (s->flags & M_DISPLAY) {
1207       state_mark_attach (s);
1208       state_printf (s, _("[-- Attachment #%d"), count);
1209       if (p->description || p->filename || p->form_name) {
1210         state_puts (": ", s);
1211         state_puts (p->description ? p->description :
1212                     p->filename ? p->filename : p->form_name, s);
1213       }
1214       state_puts (" --]\n", s);
1215
1216       mutt_pretty_size (length, sizeof (length), p->length);
1217
1218       state_mark_attach (s);
1219       state_printf (s, _("[-- Type: %s/%s, Encoding: %s, Size: %s --]\n"),
1220                     TYPE (p), p->subtype, ENCODING (p->encoding), length);
1221       if (!option (OPTWEED)) {
1222         fseek (s->fpin, p->hdr_offset, 0);
1223         mutt_copy_bytes (s->fpin, s->fpout, p->offset - p->hdr_offset);
1224       }
1225       else
1226         state_putc ('\n', s);
1227     }
1228     else {
1229       if (p->description && mutt_can_decode (p))
1230         state_printf (s, "Content-Description: %s\n", p->description);
1231
1232       if (p->form_name)
1233         state_printf (s, "%s: \n", p->form_name);
1234
1235     }
1236     mutt_body_handler (p, s);
1237     state_putc ('\n', s);
1238     if ((s->flags & M_REPLYING)
1239         && (option (OPTINCLUDEONLYFIRST)) && (s->flags & M_FIRSTDONE))
1240       break;
1241   }
1242
1243   if (a->encoding == ENCBASE64 || a->encoding == ENCQUOTEDPRINTABLE ||
1244       a->encoding == ENCUUENCODED)
1245     mutt_free_body (&b);
1246 }
1247
1248 void autoview_handler (BODY * a, STATE * s)
1249 {
1250   rfc1524_entry *entry = rfc1524_new_entry ();
1251   char buffer[LONG_STRING];
1252   char type[STRING];
1253   char command[LONG_STRING];
1254   char tempfile[_POSIX_PATH_MAX] = "";
1255   char *fname;
1256   FILE *fpin = NULL;
1257   FILE *fpout = NULL;
1258   FILE *fperr = NULL;
1259   int piped = FALSE;
1260   pid_t thepid;
1261
1262   snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
1263   rfc1524_mailcap_lookup (a, type, entry, M_AUTOVIEW);
1264
1265   fname = str_dup (a->filename);
1266   mutt_sanitize_filename (fname, 1);
1267   rfc1524_expand_filename (entry->nametemplate, fname, tempfile,
1268                            sizeof (tempfile));
1269   mem_free (&fname);
1270
1271   if (entry->command) {
1272     strfcpy (command, entry->command, sizeof (command));
1273
1274     /* rfc1524_expand_command returns 0 if the file is required */
1275     piped =
1276       rfc1524_expand_command (a, tempfile, type, command, sizeof (command));
1277
1278     if (s->flags & M_DISPLAY) {
1279       state_mark_attach (s);
1280       state_printf (s, _("[-- Autoview using %s --]\n"), command);
1281       mutt_message (_("Invoking autoview command: %s"), command);
1282     }
1283
1284     if ((fpin = safe_fopen (tempfile, "w+")) == NULL) {
1285       mutt_perror ("fopen");
1286       rfc1524_free_entry (&entry);
1287       return;
1288     }
1289
1290     mutt_copy_bytes (s->fpin, fpin, a->length);
1291
1292     if (!piped) {
1293       safe_fclose (&fpin);
1294       thepid = mutt_create_filter (command, NULL, &fpout, &fperr);
1295     }
1296     else {
1297       unlink (tempfile);
1298       fflush (fpin);
1299       rewind (fpin);
1300       thepid = mutt_create_filter_fd (command, NULL, &fpout, &fperr,
1301                                       fileno (fpin), -1, -1);
1302     }
1303
1304     if (thepid < 0) {
1305       mutt_perror (_("Can't create filter"));
1306
1307       if (s->flags & M_DISPLAY) {
1308         state_mark_attach (s);
1309         state_printf (s, _("[-- Can't run %s. --]\n"), command);
1310       }
1311       goto bail;
1312     }
1313
1314     if (s->prefix) {
1315       while (fgets (buffer, sizeof (buffer), fpout) != NULL) {
1316         state_puts (s->prefix, s);
1317         state_puts (buffer, s);
1318       }
1319       /* check for data on stderr */
1320       if (fgets (buffer, sizeof (buffer), fperr)) {
1321         if (s->flags & M_DISPLAY) {
1322           state_mark_attach (s);
1323           state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1324         }
1325
1326         state_puts (s->prefix, s);
1327         state_puts (buffer, s);
1328         while (fgets (buffer, sizeof (buffer), fperr) != NULL) {
1329           state_puts (s->prefix, s);
1330           state_puts (buffer, s);
1331         }
1332       }
1333     }
1334     else {
1335       mutt_copy_stream (fpout, s->fpout);
1336       /* Check for stderr messages */
1337       if (fgets (buffer, sizeof (buffer), fperr)) {
1338         if (s->flags & M_DISPLAY) {
1339           state_mark_attach (s);
1340           state_printf (s, _("[-- Autoview stderr of %s --]\n"), command);
1341         }
1342
1343         state_puts (buffer, s);
1344         mutt_copy_stream (fperr, s->fpout);
1345       }
1346     }
1347
1348   bail:
1349     safe_fclose (&fpout);
1350     safe_fclose (&fperr);
1351
1352     mutt_wait_filter (thepid);
1353     if (piped)
1354       safe_fclose (&fpin);
1355     else
1356       mutt_unlink (tempfile);
1357
1358     if (s->flags & M_DISPLAY)
1359       mutt_clear_error ();
1360   }
1361   rfc1524_free_entry (&entry);
1362 }
1363
1364 static void external_body_handler (BODY * b, STATE * s)
1365 {
1366   const char *access_type;
1367   const char *expiration;
1368   time_t expire;
1369
1370   access_type = mutt_get_parameter ("access-type", b->parameter);
1371   if (!access_type) {
1372     if (s->flags & M_DISPLAY) {
1373       state_mark_attach (s);
1374       state_puts (_
1375                   ("[-- Error: message/external-body has no access-type parameter --]\n"),
1376                   s);
1377     }
1378     return;
1379   }
1380
1381   expiration = mutt_get_parameter ("expiration", b->parameter);
1382   if (expiration)
1383     expire = mutt_parse_date (expiration, NULL);
1384   else
1385     expire = -1;
1386
1387   if (!ascii_strcasecmp (access_type, "x-mutt-deleted")) {
1388     if (s->flags & (M_DISPLAY | M_PRINTING)) {
1389       char *length;
1390       char pretty_size[10];
1391
1392       state_mark_attach (s);
1393       state_printf (s, _("[-- This %s/%s attachment "),
1394                     TYPE (b->parts), b->parts->subtype);
1395       length = mutt_get_parameter ("length", b->parameter);
1396       if (length) {
1397         mutt_pretty_size (pretty_size, sizeof (pretty_size),
1398                           strtol (length, NULL, 10));
1399         state_printf (s, _("(size %s bytes) "), pretty_size);
1400       }
1401       state_puts (_("has been deleted --]\n"), s);
1402
1403       if (expire != -1) {
1404         state_mark_attach (s);
1405         state_printf (s, _("[-- on %s --]\n"), expiration);
1406       }
1407       if (b->parts->filename) {
1408         state_mark_attach (s);
1409         state_printf (s, _("[-- name: %s --]\n"), b->parts->filename);
1410       }
1411
1412       mutt_copy_hdr (s->fpin, s->fpout, ftell (s->fpin), b->parts->offset,
1413                      (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1414                      CH_DECODE, NULL);
1415     }
1416   }
1417   else if (expiration && expire < time (NULL)) {
1418     if (s->flags & M_DISPLAY) {
1419       state_mark_attach (s);
1420       state_printf (s, _("[-- This %s/%s attachment is not included, --]\n"),
1421                     TYPE (b->parts), b->parts->subtype);
1422       state_attach_puts (_("[-- and the indicated external source has --]\n"
1423                            "[-- expired. --]\n"), s);
1424
1425       mutt_copy_hdr (s->fpin, s->fpout, ftell (s->fpin), b->parts->offset,
1426                      (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1427                      CH_DECODE, NULL);
1428     }
1429   }
1430   else {
1431     if (s->flags & M_DISPLAY) {
1432       state_mark_attach (s);
1433       state_printf (s,
1434                     _("[-- This %s/%s attachment is not included, --]\n"),
1435                     TYPE (b->parts), b->parts->subtype);
1436       state_mark_attach (s);
1437       state_printf (s,
1438                     _
1439                     ("[-- and the indicated access-type %s is unsupported --]\n"),
1440                     access_type);
1441       mutt_copy_hdr (s->fpin, s->fpout, ftell (s->fpin), b->parts->offset,
1442                      (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
1443                      CH_DECODE, NULL);
1444     }
1445   }
1446 }
1447
1448 void mutt_decode_attachment (BODY * b, STATE * s)
1449 {
1450   int istext = mutt_is_text_part (b);
1451   iconv_t cd = (iconv_t) (-1);
1452
1453   Quotebuf[0] = '\0';
1454
1455   if (istext) {
1456     if (s->flags & M_CHARCONV) {
1457       char *charset = mutt_get_parameter ("charset", b->parameter);
1458
1459       if (!option (OPTSTRICTMIME) && !charset)
1460         charset = mutt_get_first_charset (AssumedCharset);
1461       if (charset && Charset)
1462         cd = mutt_iconv_open (Charset, charset, M_ICONV_HOOK_FROM);
1463     }
1464     else {
1465       if (b->file_charset)
1466         cd = mutt_iconv_open (Charset, b->file_charset, M_ICONV_HOOK_FROM);
1467     }
1468   }
1469
1470   fseek (s->fpin, b->offset, 0);
1471   switch (b->encoding) {
1472   case ENCQUOTEDPRINTABLE:
1473     mutt_decode_quoted (s, b->length, istext, cd);
1474     break;
1475   case ENCBASE64:
1476     mutt_decode_base64 (s, b->length, istext, cd);
1477     break;
1478   case ENCUUENCODED:
1479     mutt_decode_uuencoded (s, b->length, istext, cd);
1480     break;
1481   default:
1482     mutt_decode_xbit (s, b->length, istext, cd);
1483     break;
1484   }
1485
1486   if (cd != (iconv_t) (-1))
1487     iconv_close (cd);
1488 }
1489
1490 void mutt_body_handler (BODY * b, STATE * s)
1491 {
1492   int decode = 0;
1493   int plaintext = 0;
1494   FILE *fp = NULL;
1495   char tempfile[_POSIX_PATH_MAX];
1496   handler_t handler = NULL;
1497   long tmpoffset = 0;
1498   size_t tmplength = 0;
1499   char type[STRING];
1500
1501   int oflags = s->flags;
1502
1503   /* first determine which handler to use to process this part */
1504
1505   snprintf (type, sizeof (type), "%s/%s", TYPE (b), b->subtype);
1506   if (mutt_is_autoview (b, type)) {
1507     rfc1524_entry *entry = rfc1524_new_entry ();
1508
1509     if (rfc1524_mailcap_lookup (b, type, entry, M_AUTOVIEW)) {
1510       handler = autoview_handler;
1511       s->flags &= ~M_CHARCONV;
1512     }
1513     rfc1524_free_entry (&entry);
1514   }
1515   else if (b->type == TYPETEXT) {
1516     if (ascii_strcasecmp ("plain", b->subtype) == 0) {
1517       /* avoid copying this part twice since removing the transfer-encoding is
1518        * the only operation needed.
1519        */
1520       if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b))
1521         handler = crypt_pgp_application_pgp_handler;
1522       else
1523         if (ascii_strcasecmp
1524             ("flowed", mutt_get_parameter ("format", b->parameter)) == 0)
1525         handler = text_plain_flowed_handler;
1526       else
1527         plaintext = 1;
1528     }
1529     else if (ascii_strcasecmp ("enriched", b->subtype) == 0)
1530       handler = text_enriched_handler;
1531     else                        /* text body type without a handler */
1532       plaintext = 1;
1533   }
1534   else if (b->type == TYPEMESSAGE) {
1535     if (mutt_is_message_type (b->type, b->subtype))
1536       handler = message_handler;
1537     else if (!ascii_strcasecmp ("delivery-status", b->subtype))
1538       plaintext = 1;
1539     else if (!ascii_strcasecmp ("external-body", b->subtype))
1540       handler = external_body_handler;
1541   }
1542   else if (b->type == TYPEMULTIPART) {
1543     char *p;
1544
1545     if (ascii_strcasecmp ("alternative", b->subtype) == 0)
1546       handler = alternative_handler;
1547     else if (WithCrypto && ascii_strcasecmp ("signed", b->subtype) == 0) {
1548       p = mutt_get_parameter ("protocol", b->parameter);
1549
1550       if (!p)
1551         mutt_error (_("Error: multipart/signed has no protocol."));
1552
1553       else if (s->flags & M_VERIFY)
1554         handler = mutt_signed_handler;
1555     }
1556     else if ((WithCrypto & APPLICATION_PGP)
1557              && str_casecmp ("encrypted", b->subtype) == 0) {
1558       p = mutt_get_parameter ("protocol", b->parameter);
1559
1560       if (!p)
1561         mutt_error (_
1562                     ("Error: multipart/encrypted has no protocol parameter!"));
1563
1564       else if (ascii_strcasecmp ("application/pgp-encrypted", p) == 0)
1565         handler = crypt_pgp_encrypted_handler;
1566     }
1567
1568     if (!handler)
1569       handler = multipart_handler;
1570   }
1571   else if (WithCrypto && b->type == TYPEAPPLICATION) {
1572     if ((WithCrypto & APPLICATION_PGP) && mutt_is_application_pgp (b))
1573       handler = crypt_pgp_application_pgp_handler;
1574     if ((WithCrypto & APPLICATION_SMIME) && mutt_is_application_smime (b))
1575       handler = crypt_smime_application_smime_handler;
1576   }
1577
1578
1579   if (plaintext || handler) {
1580     fseek (s->fpin, b->offset, 0);
1581
1582     /* see if we need to decode this part before processing it */
1583     if (b->encoding == ENCBASE64 || b->encoding == ENCQUOTEDPRINTABLE || b->encoding == ENCUUENCODED || plaintext || mutt_is_text_part (b)) {   /* text subtypes may
1584                                                                                                                                                  * require character
1585                                                                                                                                                  * set conversion even
1586                                                                                                                                                  * with 8bit encoding.
1587                                                                                                                                                  */
1588       int origType = b->type;
1589       char *savePrefix = NULL;
1590
1591       if (!plaintext) {
1592         /* decode to a tempfile, saving the original destination */
1593         fp = s->fpout;
1594         mutt_mktemp (tempfile);
1595         if ((s->fpout = safe_fopen (tempfile, "w")) == NULL) {
1596           mutt_error _("Unable to open temporary file!");
1597
1598           goto bail;
1599         }
1600         /* decoding the attachment changes the size and offset, so save a copy
1601          * of the "real" values now, and restore them after processing
1602          */
1603         tmplength = b->length;
1604         tmpoffset = b->offset;
1605
1606         /* if we are decoding binary bodies, we don't want to prefix each
1607          * line with the prefix or else the data will get corrupted.
1608          */
1609         savePrefix = s->prefix;
1610         s->prefix = NULL;
1611
1612         decode = 1;
1613       }
1614       else
1615         b->type = TYPETEXT;
1616
1617       mutt_decode_attachment (b, s);
1618
1619       if (decode) {
1620         b->length = ftell (s->fpout);
1621         b->offset = 0;
1622         fclose (s->fpout);
1623
1624         /* restore final destination and substitute the tempfile for input */
1625         s->fpout = fp;
1626         fp = s->fpin;
1627         s->fpin = safe_fopen (tempfile, "r");
1628         unlink (tempfile);
1629
1630         /* restore the prefix */
1631         s->prefix = savePrefix;
1632       }
1633
1634       b->type = origType;
1635     }
1636
1637     /* process the (decoded) body part */
1638     if (handler) {
1639       handler (b, s);
1640
1641       if (decode) {
1642         b->length = tmplength;
1643         b->offset = tmpoffset;
1644
1645         /* restore the original source stream */
1646         fclose (s->fpin);
1647         s->fpin = fp;
1648       }
1649     }
1650     s->flags |= M_FIRSTDONE;
1651   }
1652   else if (s->flags & M_DISPLAY) {
1653     state_mark_attach (s);
1654     state_printf (s, _("[-- %s/%s is unsupported "), TYPE (b), b->subtype);
1655     if (!option (OPTVIEWATTACH)) {
1656       if (km_expand_key
1657           (type, sizeof (type),
1658            km_find_func (MENU_PAGER, OP_VIEW_ATTACHMENTS)))
1659         fprintf (s->fpout, _("(use '%s' to view this part)"), type);
1660       else
1661         fputs (_("(need 'view-attachments' bound to key!)"), s->fpout);
1662     }
1663     fputs (" --]\n", s->fpout);
1664   }
1665
1666 bail:
1667   s->flags = oflags | (s->flags & M_FIRSTDONE);
1668 }