7dcb21bd7e46f27ec0d3eeb3af1d7189fea63460
[apps/madmutt.git] / commands.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4  * Copyright (C) 2000 Thomas Roessler <roessler@does-not-exist.org>
5  *
6  * This file is part of mutt-ng, see http://www.muttng.org/.
7  * It's licensed under the GNU General Public License,
8  * please see the file GPL in the top level source directory.
9  */
10
11 #include <lib-lib/lib-lib.h>
12 #include <utime.h>
13
14 #include <lib-mime/mime.h>
15
16 #include <lib-sys/exit.h>
17 #include <lib-sys/unix.h>
18
19 #include <lib-lua/lib-lua.h>
20 #include <lib-ui/lib-ui.h>
21 #include <lib-ui/enter.h>
22 #include <lib-ui/menu.h>
23 #include <lib-mx/mx.h>
24
25 #include "mutt.h"
26 #include "alias.h"
27 #include "recvattach.h"
28 #include "sort.h"
29 #include "copy.h"
30 #include "pager.h"
31 #include "crypt.h"
32 #include "mutt_idna.h"
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36
37 #include <imap/imap.h>
38
39 /* The folder the user last saved to.  Used by ci_save_message() */
40 static char LastSaveFolder[_POSIX_PATH_MAX] = "";
41
42 int mutt_display_message (HEADER * cur)
43 {
44   char tempfile[_POSIX_PATH_MAX], buf[LONG_STRING];
45   int cmflags = M_CM_DECODE | M_CM_DISPLAY | M_CM_CHARCONV;
46   FILE *fpout = NULL;
47   FILE *fpfilterout = NULL;
48   MESSAGE *msg = NULL;
49   pid_t filterpid = -1;
50   int res = 0;
51
52   snprintf (buf, sizeof (buf), "%s/%s", TYPE (cur->content),
53             cur->content->subtype);
54
55   mutt_parse_mime_message (Context, cur);
56   mutt_message_hook (Context, cur, M_MESSAGEHOOK);
57
58   fpout = m_tempfile(tempfile, sizeof(tempfile), NONULL(mod_core.tmpdir), NULL);
59   if (!fpout) {
60     mutt_error _("Could not create temporary file!");
61     return 0;
62   }
63
64   if (DisplayFilter && *DisplayFilter) {
65     fpfilterout = fpout;
66     fpout = NULL;
67     filterpid = mutt_create_filter_fd (DisplayFilter, &fpout, NULL, NULL,
68                                        -1, fileno (fpfilterout), -1);
69     if (filterpid < 0) {
70       mutt_error (_("Cannot create display filter"));
71       m_fclose(&fpfilterout);
72       unlink (tempfile);
73       return 0;
74     }
75   }
76
77   msg = mx_open_message (Context, cur->msgno);
78   if (msg == NULL) res = -1;
79
80   if (res != -1) {
81     /* see if crytpo is needed for this message.  if so, we should exit curses */
82     if (cur->security) {
83       if (cur->security & ENCRYPT) {
84         cmflags |= M_CM_VERIFY;
85       }
86       else if (cur->security & SIGN) {
87         /* find out whether or not the verify signature */
88         if (query_quadoption2(mod_crypt.verify_sig, _("Verify PGP signature?")) ==
89             M_YES) {
90           cmflags |= M_CM_VERIFY;
91         }
92       }
93     }
94
95     if (cmflags & M_CM_VERIFY || cur->security & ENCRYPT) {
96       crypt_invoke_message(cur->security);
97     }
98
99     res = _mutt_copy_message (fpout, msg->fp, cur, cur->content, cmflags,
100                              (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
101                              CH_DECODE | CH_FROM);
102     if (res == 0 && (ferror(fpout) || feof(fpout))) {
103       res = -1;
104     }
105
106     mx_close_message (&msg);
107   }
108
109   if ((m_fclose(&fpout) != 0 && errno != EPIPE) || res == -1) {
110     mutt_error (_("Could not copy message"));
111     if (fpfilterout != NULL) {
112       mutt_wait_filter (filterpid);
113       m_fclose(&fpfilterout);
114     }
115   }
116
117   if (fpfilterout != NULL && mutt_wait_filter (filterpid) != 0)
118     mutt_any_key_to_continue (NULL);
119
120   m_fclose(&fpfilterout);   /* XXX - check result? */
121
122
123   /* update crypto information for this message */
124   cur->security &= ~(GOODSIGN|BADSIGN);
125   cur->security |= crypt_query (cur->content);
126
127   /* Remove color cache for this message, in case there
128      are color patterns for both ~g and ~V */
129   cur->pair = 0;
130
131   if ((cur->security & APPLICATION_SMIME) && (cmflags & M_CM_VERIFY)) {
132     if (cur->security & GOODSIGN) {
133       if (!crypt_smime_verify_sender (cur))
134         mutt_message (_("S/MIME signature successfully verified."));
135       else
136         mutt_error (_("S/MIME certificate owner does not match sender."));
137     }
138     else if (cur->security & PARTSIGN)
139       mutt_message (_
140                     ("Warning: Part of this message has not been signed."));
141     else if (cur->security & SIGN || cur->security & BADSIGN)
142       mutt_error (_("S/MIME signature could NOT be verified."));
143   }
144
145   if ((cur->security & APPLICATION_PGP) && (cmflags & M_CM_VERIFY)) {
146     if (cur->security & GOODSIGN)
147       mutt_message (_("PGP signature successfully verified."));
148     else if (cur->security & PARTSIGN)
149       mutt_message (_
150                     ("Warning: Part of this message has not been signed."));
151     else if (cur->security & SIGN)
152       mutt_message (_("PGP signature could NOT be verified."));
153   }
154
155   {
156     pager_t info;
157
158     p_clear(&info, 1);
159     info.hdr = cur;
160     info.ctx = Context;
161     return mutt_pager (NULL, tempfile, M_PAGER_MESSAGE, &info);
162   }
163 }
164
165 void ci_bounce_message (HEADER * h, int *redraw)
166 {
167   char prompt[STRING];
168   char buf[HUGE_STRING] = { 0 };
169   address_t *adr = NULL;
170   char *err = NULL;
171   int rc;
172
173   if (h)
174     m_strcpy(prompt, sizeof(prompt), _("Bounce message to: "));
175   else
176     m_strcpy(prompt, sizeof(prompt), _("Bounce tagged messages to: "));
177
178   rc = mutt_get_field (prompt, buf, sizeof (buf), M_ALIAS);
179
180   if (option (OPTNEEDREDRAW)) {
181     unset_option (OPTNEEDREDRAW);
182     *redraw = REDRAW_FULL;
183   }
184
185   if (rc || !buf[0])
186     return;
187
188   if (!(adr = rfc822_parse_adrlist (adr, buf))) {
189     mutt_error _("Error parsing address!");
190
191     return;
192   }
193
194   adr = mutt_expand_aliases (adr);
195
196   if (mutt_addrlist_to_idna (adr, &err) < 0) {
197     mutt_error (_("Bad IDN: '%s'"), err);
198     p_delete(&err);
199     address_list_wipe(&adr);
200     return;
201   }
202
203   buf[0] = 0;
204   rfc822_addrcat(buf, sizeof (buf), adr, 1);
205
206   snprintf (prompt, sizeof (prompt),
207             (h ? _("Bounce message to %s") : _("Bounce messages to %s")),
208             buf);
209
210 #define extra_space (15 + 7 + 2)
211   if (m_strwidth(prompt) > getmaxx(main_w) - extra_space) {
212     mutt_format_string(prompt, sizeof(prompt), 0,
213                        getmaxx(main_w) - extra_space, 0, 0, prompt,
214                        sizeof(prompt), 0);
215     m_strcat(prompt, sizeof(prompt), "...?");
216   } else {
217     m_strcat(prompt, sizeof(prompt), "?");
218   }
219 #undef extra_space
220
221   if (query_quadoption (OPT_BOUNCE, prompt) != M_YES) {
222     address_list_wipe(&adr);
223     CLEARLINE(stdscr, LINES - 1);
224     mutt_message (h ? _("Message not bounced.") : _("Messages not bounced."));
225     return;
226   }
227
228   CLEARLINE(stdscr, LINES - 1);
229
230   rc = mutt_bounce_message (NULL, h, adr);
231   address_list_wipe(&adr);
232   /* If no error, or background, display message. */
233   if ((rc == 0) || (rc == S_BKG))
234     mutt_message (h ? _("Message bounced.") : _("Messages bounced."));
235 }
236
237 static void pipe_msg (HEADER * h, FILE * fp, int decode, int print)
238 {
239   int cmflags = 0;
240   int chflags = CH_FROM;
241
242   if (decode) {
243     cmflags |= M_CM_DECODE | M_CM_CHARCONV;
244     chflags |= CH_DECODE | CH_REORDER;
245
246     if (option(OPTWEED)) {
247       chflags |= CH_WEED;
248       cmflags |= M_CM_WEED;
249     }
250   }
251
252   if (print)
253     cmflags |= M_CM_PRINTING;
254
255   if (decode && h->security & ENCRYPT) {
256     endwin ();
257   }
258
259   if (decode)
260     mutt_parse_mime_message (Context, h);
261
262   mutt_copy_message (fp, Context, h, cmflags, chflags);
263 }
264
265 /* the following code is shared between printing and piping */
266 static int _mutt_pipe_message(HEADER * h, char *cmd, int decode, int print,
267                               int split, const char *sep)
268 {
269
270   int i, rc = 0;
271   pid_t thepid;
272   FILE *fpout;
273
274   if (h) {
275     mutt_message_hook (Context, h, M_MESSAGEHOOK);
276
277     if (decode) {
278       mutt_parse_mime_message (Context, h);
279     }
280     mutt_endwin (NULL);
281
282     if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
283       mutt_perror (_("Can't create filter process"));
284
285       return 1;
286     }
287
288     pipe_msg (h, fpout, decode, print);
289     m_fclose(&fpout);
290     rc = mutt_wait_filter (thepid);
291   } else {                        /* handle tagged messages */
292     if (decode) {
293       for (i = 0; i < Context->vcount; i++)
294         if (Context->hdrs[Context->v2r[i]]->tagged) {
295           mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
296                              M_MESSAGEHOOK);
297           mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
298         }
299     }
300
301     if (split) {
302       for (i = 0; i < Context->vcount; i++) {
303         if (Context->hdrs[Context->v2r[i]]->tagged) {
304           mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
305                              M_MESSAGEHOOK);
306           mutt_endwin (NULL);
307           if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
308             mutt_perror (_("Can't create filter process"));
309
310             return 1;
311           }
312           pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
313           /* add the message separator */
314           if (sep)
315             fputs (sep, fpout);
316           m_fclose(&fpout);
317           if (mutt_wait_filter (thepid) != 0)
318             rc = 1;
319         }
320       }
321     } else {
322       mutt_endwin (NULL);
323       if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
324         mutt_perror (_("Can't create filter process"));
325
326         return 1;
327       }
328       for (i = 0; i < Context->vcount; i++) {
329         if (Context->hdrs[Context->v2r[i]]->tagged) {
330           mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
331                              M_MESSAGEHOOK);
332           pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
333           /* add the message separator */
334           if (sep)
335             fputs (sep, fpout);
336         }
337       }
338       m_fclose(&fpout);
339       if (mutt_wait_filter (thepid) != 0)
340         rc = 1;
341     }
342   }
343
344   if (rc || option (OPTWAITKEY))
345     mutt_any_key_to_continue (NULL);
346   return rc;
347 }
348
349 void mutt_pipe_message (HEADER * h)
350 {
351   char buffer[LONG_STRING];
352
353   buffer[0] = 0;
354   if (mutt_get_field (_("Pipe to command: "), buffer, sizeof (buffer), M_CMD)
355       != 0 || !buffer[0])
356     return;
357
358   mutt_expand_path (buffer, sizeof (buffer));
359   _mutt_pipe_message (h, buffer,
360                       option (OPTPIPEDECODE),
361                       0, option (OPTPIPESPLIT), PipeSep);
362 }
363
364 void mutt_print_message (HEADER * h)
365 {
366
367   if (quadoption (OPT_PRINT) && m_strisempty(PrintCmd)) {
368     mutt_message (_("No printing command has been defined."));
369     return;
370   }
371
372   if (query_quadoption (OPT_PRINT,
373                         h ? _("Print message?") : _("Print tagged messages?"))
374       != M_YES)
375     return;
376
377   if (_mutt_pipe_message (h, PrintCmd,
378                           option (OPTPRINTDECODE),
379                           1, option (OPTPRINTSPLIT), "\f") == 0)
380     mutt_message (h ? _("Message printed") : _("Messages printed"));
381   else
382     mutt_message (h ? _("Message could not be printed") :
383                   _("Messages could not be printed"));
384 }
385
386
387 int mutt_select_sort (int reverse)
388 {
389   int method = Sort;            /* save the current method in case of abort */
390
391   switch (mutt_multi_choice (reverse ?
392                              _
393                              ("Rev-Sort (d)ate/(f)rm/(r)ecv/(s)ubj/t(o)/(t)hread/(u)nsort/si(z)e/s(c)ore/s(p)am?: ")
394                              :
395                              _
396                              ("Sort (d)ate/(f)rm/(r)ecv/(s)ubj/t(o)/(t)hread/(u)nsort/si(z)e/s(c)ore/s(p)am?: "),
397                              _("dfrsotuzcp"))) {
398   case -1:                     /* abort - don't resort */
399     return -1;
400
401   case 1:                      /* (d)ate */
402     Sort = SORT_DATE;
403     break;
404
405   case 2:                      /* (f)rm */
406     Sort = SORT_FROM;
407     break;
408
409   case 3:                      /* (r)ecv */
410     Sort = SORT_RECEIVED;
411     break;
412
413   case 4:                      /* (s)ubj */
414     Sort = SORT_SUBJECT;
415     break;
416
417   case 5:                      /* t(o) */
418     Sort = SORT_TO;
419     break;
420
421   case 6:                      /* (t)hread */
422     Sort = SORT_THREADS;
423     break;
424
425   case 7:                      /* (u)nsort */
426     Sort = SORT_ORDER;
427     break;
428
429   case 8:                      /* si(z)e */
430     Sort = SORT_SIZE;
431     break;
432
433   case 9:                      /* s(c)ore */
434     Sort = SORT_SCORE;
435     break;
436
437   case 10:                     /* s(p)am */
438     Sort = SORT_SPAM;
439     break;
440   }
441   if (reverse)
442     Sort |= SORT_REVERSE;
443
444   return (Sort != method ? 0 : -1);     /* no need to resort if it's the same */
445 }
446
447 /* invoke a command in a subshell */
448 void mutt_shell_escape (void)
449 {
450   char buf[LONG_STRING];
451
452   buf[0] = 0;
453   if (mutt_get_field (_("Shell command: "), buf, sizeof (buf), M_CMD) == 0) {
454     if (!buf[0])
455       m_strcpy(buf, sizeof(buf), mod_core.shell);
456     if (buf[0]) {
457       CLEARLINE(stdscr, LINES - 1);
458       mutt_endwin (NULL);
459       fflush (stdout);
460       if (mutt_system (buf) != 0 || option (OPTWAITKEY))
461         mutt_any_key_to_continue (NULL);
462     }
463   }
464 }
465
466 /* enter a mutt command */
467 void mutt_enter_command (void)
468 {
469   BUFFER err, token;
470   char buffer[LONG_STRING], errbuf[STRING];
471   int r;
472
473   buffer[0] = 0;
474   if (mutt_get_field (":", buffer, sizeof (buffer), M_COMMAND) != 0
475       || !buffer[0])
476     return;
477   err.data = errbuf;
478   err.dsize = sizeof (errbuf);
479   p_clear(&token, 1);
480   r = mutt_parse_rc_line (buffer, &token, &err);
481   p_delete(&token.data);
482   if (errbuf[0]) {
483     /* since errbuf could potentially contain printf() sequences in it,
484        we must call mutt_error() in this fashion so that vsprintf()
485        doesn't expect more arguments that we passed */
486     if (r == 0)
487       mutt_message ("%s", errbuf);
488     else
489       mutt_error ("%s", errbuf);
490   }
491 }
492
493 void mutt_display_address (ENVELOPE * env)
494 {
495   const char *pfx = NULL;
496   char buf[STRING];
497   address_t *adr = NULL;
498
499   adr = mutt_get_address(env, &pfx);
500
501   if (!adr)
502     return;
503
504   /* 
505    * Note: We don't convert IDNA to local representation this time.
506    * That is intentional, so the user has an opportunity to copy &
507    * paste the on-the-wire form of the address to other, IDN-unable
508    * software. 
509    */
510
511   buf[0] = 0;
512   rfc822_addrcat(buf, sizeof (buf), adr, 0);
513   mutt_message ("%s: %s", pfx, buf);
514 }
515
516 static void set_copy_flags (HEADER * hdr, int decode, int decrypt,
517                             int *cmflags, int *chflags)
518 {
519   *cmflags = 0;
520   *chflags = CH_UPDATE_LEN;
521
522   if (!decode && decrypt && (hdr->security & ENCRYPT)) {
523     if (mutt_is_multipart_encrypted (hdr->content)) {
524       *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
525       *cmflags = M_CM_DECODE_PGP;
526     }
527     else if (mutt_is_application_pgp (hdr->content) & ENCRYPT)
528       decode = 1;
529     else if (mutt_is_application_smime (hdr->content) & ENCRYPT) {
530       *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
531       *cmflags = M_CM_DECODE_SMIME;
532     }
533   }
534
535   if (decode) {
536     *chflags = CH_XMIT | CH_MIME | CH_TXTPLAIN;
537     *cmflags = M_CM_DECODE | M_CM_CHARCONV;
538
539     if (!decrypt) {             /* If decode doesn't kick in for decrypt, */
540       *chflags |= CH_DECODE;    /* then decode RFC 2047 headers, */
541
542       if (option (OPTWEED)) {
543         *chflags |= CH_WEED;    /* and respect $weed. */
544         *cmflags |= M_CM_WEED;
545       }
546     }
547   }
548 }
549
550 int _mutt_save_message (HEADER * h, CONTEXT * ctx, int delete, int decode,
551                          int decrypt) {
552   int cmflags, chflags;
553   int rc;
554
555   set_copy_flags (h, decode, decrypt, &cmflags, &chflags);
556
557   if (decode || decrypt)
558     mutt_parse_mime_message (Context, h);
559
560   if ((rc = mutt_append_message (ctx, Context, h, cmflags, chflags)) != 0)
561     return rc;
562
563   if (delete) {
564     mutt_set_flag (Context, h, M_DELETE, 1);
565     if (option (OPTDELETEUNTAG))
566       mutt_set_flag (Context, h, M_TAG, 0);
567     mutt_set_flag (Context, h, M_APPENDED, 1);
568   }
569   return (0);
570 }
571
572 /* returns 0 if the copy/save was successful, or -1 on error/abort */
573 int mutt_save_message (HEADER * h, int delete,
574                        int decode, int decrypt, int *redraw) {
575   int i, need_buffy_cleanup;
576   int need_passphrase = 0, app = 0;
577   char prompt[STRING], buf[_POSIX_PATH_MAX];
578   CONTEXT ctx;
579   struct stat st;
580   struct utimbuf ut;
581
582   *redraw = 0;
583
584
585   snprintf (prompt, sizeof (prompt),
586             decode ? (delete ? _("Decode-save%s to mailbox") :
587                       _("Decode-copy%s to mailbox")) :
588             (decrypt ? (delete ? _("Decrypt-save%s to mailbox") :
589                         _("Decrypt-copy%s to mailbox")) :
590              (delete ? _("Save%s to mailbox") : _("Copy%s to mailbox"))),
591             h ? "" : _(" tagged"));
592
593
594   if (h) {
595     need_passphrase = h->security & ENCRYPT;
596     app = h->security;
597     mutt_message_hook (Context, h, M_MESSAGEHOOK);
598     mutt_default_save (buf, sizeof (buf), h);
599   }
600   else {
601     /* look for the first tagged message */
602
603     for (i = 0; i < Context->vcount; i++) {
604       if (Context->hdrs[Context->v2r[i]]->tagged) {
605         h = Context->hdrs[Context->v2r[i]];
606         break;
607       }
608     }
609
610
611     if (h) {
612       mutt_message_hook (Context, h, M_MESSAGEHOOK);
613       mutt_default_save (buf, sizeof (buf), h);
614       need_passphrase = h->security & ENCRYPT;
615       app = h->security;
616       h = NULL;
617     }
618   }
619
620   mutt_pretty_mailbox (buf);
621   if (mutt_enter_fname (prompt, buf, sizeof (buf), redraw, 0) == -1)
622     return (-1);
623
624   if (*redraw != REDRAW_FULL) {
625     if (!h)
626       *redraw = REDRAW_INDEX | REDRAW_STATUS;
627     else
628       *redraw = REDRAW_STATUS;
629   }
630
631   if (!buf[0])
632     return (-1);
633
634   /* This is an undocumented feature of ELM pointed out to me by Felix von
635    * Leitner <leitner@prz.fu-berlin.de>
636    */
637   if (m_strcmp(buf, ".") == 0)
638     m_strcpy(buf, sizeof(buf), LastSaveFolder);
639   else
640     m_strcpy(LastSaveFolder, sizeof(LastSaveFolder), buf);
641
642   mutt_expand_path (buf, sizeof (buf));
643
644   /* check to make sure that this file is really the one the user wants */
645   if (mutt_save_confirm (buf, &st) != 0)
646     return -1;
647
648   mutt_message (_("Copying to %s..."), buf);
649
650   if (Context->magic == M_IMAP && !(decode || decrypt) && mx_get_magic (buf) == M_IMAP) {
651     switch (imap_copy_messages (Context, h, buf, delete)) {
652       /* success */
653     case 0:
654       mutt_clear_error ();
655       return 0;
656       /* non-fatal error: fall through to fetch/append */
657     case 1:
658       break;
659       /* fatal error, abort */
660     case -1:
661       return -1;
662     }
663   }
664
665   if (mx_open_mailbox (buf, M_APPEND, &ctx) != NULL) {
666     if (h) {
667       if (_mutt_save_message (h, &ctx, delete, decode, decrypt) != 0) {
668         mx_close_mailbox (&ctx, NULL);
669         return (-1);
670       }
671     } else {
672       for (i = 0; i < Context->vcount; i++) {
673         if (Context->hdrs[Context->v2r[i]]->tagged) {
674           mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
675                              M_MESSAGEHOOK);
676           if (_mutt_save_message (Context->hdrs[Context->v2r[i]], &ctx, delete,
677                                   decode, decrypt) != 0) {
678             mx_close_mailbox (&ctx, NULL);
679             return (-1);
680           }
681         }
682       }
683     }
684
685     need_buffy_cleanup = (ctx.magic == M_MBOX);
686
687     mx_close_mailbox (&ctx, NULL);
688
689     if (need_buffy_cleanup) {
690       /* fix up the times so buffy won't get confused */
691       if (st.st_mtime > st.st_atime) {
692         ut.actime = st.st_atime;
693         ut.modtime = time (NULL);
694         utime (buf, &ut);
695       } else {
696         utime (buf, NULL);
697       }
698     }
699
700     mutt_clear_error ();
701     return (0);
702   }
703
704   return -1;
705 }
706
707 void mutt_version (void)
708 {
709   mutt_message (mutt_make_version());
710 }
711
712 void mutt_edit_content_type (HEADER * h, BODY * b, FILE * fp)
713 {
714   char buf[LONG_STRING];
715   char obuf[LONG_STRING];
716   char tmp[STRING];
717
718   char charset[STRING];
719   char *cp;
720
721   short charset_changed = 0;
722   short type_changed = 0;
723
724   cp = parameter_getval(b->parameter, "charset");
725   m_strcpy(charset, sizeof(charset), NONULL(cp));
726
727   snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
728   m_strcpy(obuf, sizeof(obuf), buf);
729   if (b->parameter) {
730     parameter_t *p;
731     ssize_t l;
732
733     for (p = b->parameter; p; p = p->next) {
734       l = m_strlen(buf);
735
736       rfc822_strcpy(tmp, sizeof(tmp), p->value, MimeSpecials);
737       snprintf (buf + l, sizeof (buf) - l, "; %s=%s", p->attribute, tmp);
738     }
739   }
740
741   if (mutt_get_field ("Content-Type: ", buf, sizeof (buf), 0) != 0 ||
742       buf[0] == 0)
743     return;
744
745   /* clean up previous junk */
746   parameter_list_wipe(&b->parameter);
747   p_delete(&b->subtype);
748
749   mutt_parse_content_type (buf, b);
750
751
752   snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
753   type_changed = ascii_strcasecmp (tmp, obuf);
754   charset_changed =
755     ascii_strcasecmp (charset, parameter_getval(b->parameter, "charset"));
756
757   /* if in send mode, check for conversion - current setting is default. */
758
759   if (!h && b->type == TYPETEXT && charset_changed) {
760     int r;
761
762     snprintf (tmp, sizeof (tmp), _("Convert to %s upon sending?"),
763               parameter_getval(b->parameter, "charset"));
764     if ((r = mutt_yesorno (tmp, !b->noconv)) != -1)
765       b->noconv = (r == M_NO);
766   }
767
768   /* inform the user */
769
770   snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
771   if (type_changed)
772     mutt_message (_("Content-Type changed to %s."), tmp);
773   if (b->type == TYPETEXT && charset_changed) {
774     if (type_changed)
775       mutt_sleep (1);
776     mutt_message (_("Character set changed to %s; %s."),
777                   parameter_getval(b->parameter, "charset"),
778                   b->noconv ? _("not converting") : _("converting"));
779   }
780
781   b->force_charset |= charset_changed ? 1 : 0;
782
783   if (!is_multipart(b) && b->parts)
784     body_list_wipe(&b->parts);
785   if (!mutt_is_message_type(b) && b->hdr) {
786     b->hdr->content = NULL;
787     header_delete(&b->hdr);
788   }
789
790   if (fp && (is_multipart(b) || mutt_is_message_type(b)))
791     mutt_parse_part (fp, b);
792
793   if (h) {
794     if (h->content == b)
795       h->security = 0;
796
797     h->security |= crypt_query (b);
798   }
799 }
800
801