eb7c6dff551888e579e201389cb60fc8928221c7
[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     mutt_message (h ? _("Message not bounced.") : _("Messages not bounced."));
224     return;
225   }
226
227   rc = mutt_bounce_message (NULL, h, adr);
228   address_list_wipe(&adr);
229   /* If no error, or background, display message. */
230   if ((rc == 0) || (rc == S_BKG))
231     mutt_message (h ? _("Message bounced.") : _("Messages bounced."));
232 }
233
234 static void pipe_msg (HEADER * h, FILE * fp, int decode, int print)
235 {
236   int cmflags = 0;
237   int chflags = CH_FROM;
238
239   if (decode) {
240     cmflags |= M_CM_DECODE | M_CM_CHARCONV;
241     chflags |= CH_DECODE | CH_REORDER;
242
243     if (option(OPTWEED)) {
244       chflags |= CH_WEED;
245       cmflags |= M_CM_WEED;
246     }
247   }
248
249   if (print)
250     cmflags |= M_CM_PRINTING;
251
252   if (decode && h->security & ENCRYPT) {
253     endwin ();
254   }
255
256   if (decode)
257     mutt_parse_mime_message (Context, h);
258
259   mutt_copy_message (fp, Context, h, cmflags, chflags);
260 }
261
262 /* the following code is shared between printing and piping */
263 static int _mutt_pipe_message(HEADER * h, char *cmd, int decode, int print,
264                               int split, const char *sep)
265 {
266
267   int i, rc = 0;
268   pid_t thepid;
269   FILE *fpout;
270
271   if (h) {
272     mutt_message_hook (Context, h, M_MESSAGEHOOK);
273
274     if (decode) {
275       mutt_parse_mime_message (Context, h);
276     }
277     mutt_endwin (NULL);
278
279     if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
280       mutt_perror (_("Can't create filter process"));
281
282       return 1;
283     }
284
285     pipe_msg (h, fpout, decode, print);
286     m_fclose(&fpout);
287     rc = mutt_wait_filter (thepid);
288   } else {                        /* handle tagged messages */
289     if (decode) {
290       for (i = 0; i < Context->vcount; i++)
291         if (Context->hdrs[Context->v2r[i]]->tagged) {
292           mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
293                              M_MESSAGEHOOK);
294           mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
295         }
296     }
297
298     if (split) {
299       for (i = 0; i < Context->vcount; i++) {
300         if (Context->hdrs[Context->v2r[i]]->tagged) {
301           mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
302                              M_MESSAGEHOOK);
303           mutt_endwin (NULL);
304           if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
305             mutt_perror (_("Can't create filter process"));
306
307             return 1;
308           }
309           pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
310           /* add the message separator */
311           if (sep)
312             fputs (sep, fpout);
313           m_fclose(&fpout);
314           if (mutt_wait_filter (thepid) != 0)
315             rc = 1;
316         }
317       }
318     } else {
319       mutt_endwin (NULL);
320       if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
321         mutt_perror (_("Can't create filter process"));
322
323         return 1;
324       }
325       for (i = 0; i < Context->vcount; i++) {
326         if (Context->hdrs[Context->v2r[i]]->tagged) {
327           mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
328                              M_MESSAGEHOOK);
329           pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
330           /* add the message separator */
331           if (sep)
332             fputs (sep, fpout);
333         }
334       }
335       m_fclose(&fpout);
336       if (mutt_wait_filter (thepid) != 0)
337         rc = 1;
338     }
339   }
340
341   if (rc || option (OPTWAITKEY))
342     mutt_any_key_to_continue (NULL);
343   return rc;
344 }
345
346 void mutt_pipe_message (HEADER * h)
347 {
348   char buffer[LONG_STRING];
349
350   buffer[0] = 0;
351   if (mutt_get_field (_("Pipe to command: "), buffer, sizeof (buffer), M_CMD)
352       != 0 || !buffer[0])
353     return;
354
355   mutt_expand_path (buffer, sizeof (buffer));
356   _mutt_pipe_message (h, buffer,
357                       option (OPTPIPEDECODE),
358                       0, option (OPTPIPESPLIT), PipeSep);
359 }
360
361 void mutt_print_message (HEADER * h)
362 {
363
364   if (quadoption (OPT_PRINT) && m_strisempty(PrintCmd)) {
365     mutt_message (_("No printing command has been defined."));
366     return;
367   }
368
369   if (query_quadoption (OPT_PRINT,
370                         h ? _("Print message?") : _("Print tagged messages?"))
371       != M_YES)
372     return;
373
374   if (_mutt_pipe_message (h, PrintCmd,
375                           option (OPTPRINTDECODE),
376                           1, option (OPTPRINTSPLIT), "\f") == 0)
377     mutt_message (h ? _("Message printed") : _("Messages printed"));
378   else
379     mutt_message (h ? _("Message could not be printed") :
380                   _("Messages could not be printed"));
381 }
382
383
384 int mutt_select_sort (int reverse)
385 {
386   int method = Sort;            /* save the current method in case of abort */
387
388   switch (mutt_multi_choice (reverse ?
389                              _
390                              ("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?: ")
391                              :
392                              _
393                              ("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                              _("dfrsotuzcp"))) {
395   case -1:                     /* abort - don't resort */
396     return -1;
397
398   case 1:                      /* (d)ate */
399     Sort = SORT_DATE;
400     break;
401
402   case 2:                      /* (f)rm */
403     Sort = SORT_FROM;
404     break;
405
406   case 3:                      /* (r)ecv */
407     Sort = SORT_RECEIVED;
408     break;
409
410   case 4:                      /* (s)ubj */
411     Sort = SORT_SUBJECT;
412     break;
413
414   case 5:                      /* t(o) */
415     Sort = SORT_TO;
416     break;
417
418   case 6:                      /* (t)hread */
419     Sort = SORT_THREADS;
420     break;
421
422   case 7:                      /* (u)nsort */
423     Sort = SORT_ORDER;
424     break;
425
426   case 8:                      /* si(z)e */
427     Sort = SORT_SIZE;
428     break;
429
430   case 9:                      /* s(c)ore */
431     Sort = SORT_SCORE;
432     break;
433
434   case 10:                     /* s(p)am */
435     Sort = SORT_SPAM;
436     break;
437   }
438   if (reverse)
439     Sort |= SORT_REVERSE;
440
441   return (Sort != method ? 0 : -1);     /* no need to resort if it's the same */
442 }
443
444 /* invoke a command in a subshell */
445 void mutt_shell_escape (void)
446 {
447   char buf[LONG_STRING];
448
449   buf[0] = 0;
450   if (mutt_get_field (_("Shell command: "), buf, sizeof (buf), M_CMD) == 0) {
451     if (!buf[0])
452       m_strcpy(buf, sizeof(buf), mod_core.shell);
453     if (buf[0]) {
454       mutt_endwin (NULL);
455       fflush (stdout);
456       if (mutt_system (buf) != 0 || option (OPTWAITKEY))
457         mutt_any_key_to_continue (NULL);
458     }
459   }
460 }
461
462 /* enter a mutt command */
463 void mutt_enter_command (void)
464 {
465   BUFFER err, token;
466   char buffer[LONG_STRING], errbuf[STRING];
467   int r;
468
469   buffer[0] = 0;
470   if (mutt_get_field (":", buffer, sizeof (buffer), M_COMMAND) != 0
471       || !buffer[0])
472     return;
473   err.data = errbuf;
474   err.dsize = sizeof (errbuf);
475   p_clear(&token, 1);
476   r = mutt_parse_rc_line (buffer, &token, &err);
477   p_delete(&token.data);
478   if (errbuf[0]) {
479     /* since errbuf could potentially contain printf() sequences in it,
480        we must call mutt_error() in this fashion so that vsprintf()
481        doesn't expect more arguments that we passed */
482     if (r == 0)
483       mutt_message ("%s", errbuf);
484     else
485       mutt_error ("%s", errbuf);
486   }
487 }
488
489 void mutt_display_address (ENVELOPE * env)
490 {
491   const char *pfx = NULL;
492   char buf[STRING];
493   address_t *adr = NULL;
494
495   adr = mutt_get_address(env, &pfx);
496
497   if (!adr)
498     return;
499
500   /* 
501    * Note: We don't convert IDNA to local representation this time.
502    * That is intentional, so the user has an opportunity to copy &
503    * paste the on-the-wire form of the address to other, IDN-unable
504    * software. 
505    */
506
507   buf[0] = 0;
508   rfc822_addrcat(buf, sizeof (buf), adr, 0);
509   mutt_message ("%s: %s", pfx, buf);
510 }
511
512 static void set_copy_flags (HEADER * hdr, int decode, int decrypt,
513                             int *cmflags, int *chflags)
514 {
515   *cmflags = 0;
516   *chflags = CH_UPDATE_LEN;
517
518   if (!decode && decrypt && (hdr->security & ENCRYPT)) {
519     if (mutt_is_multipart_encrypted (hdr->content)) {
520       *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
521       *cmflags = M_CM_DECODE_PGP;
522     }
523     else if (mutt_is_application_pgp (hdr->content) & ENCRYPT)
524       decode = 1;
525     else if (mutt_is_application_smime (hdr->content) & ENCRYPT) {
526       *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
527       *cmflags = M_CM_DECODE_SMIME;
528     }
529   }
530
531   if (decode) {
532     *chflags = CH_XMIT | CH_MIME | CH_TXTPLAIN;
533     *cmflags = M_CM_DECODE | M_CM_CHARCONV;
534
535     if (!decrypt) {             /* If decode doesn't kick in for decrypt, */
536       *chflags |= CH_DECODE;    /* then decode RFC 2047 headers, */
537
538       if (option (OPTWEED)) {
539         *chflags |= CH_WEED;    /* and respect $weed. */
540         *cmflags |= M_CM_WEED;
541       }
542     }
543   }
544 }
545
546 int _mutt_save_message (HEADER * h, CONTEXT * ctx, int delete, int decode,
547                          int decrypt) {
548   int cmflags, chflags;
549   int rc;
550
551   set_copy_flags (h, decode, decrypt, &cmflags, &chflags);
552
553   if (decode || decrypt)
554     mutt_parse_mime_message (Context, h);
555
556   if ((rc = mutt_append_message (ctx, Context, h, cmflags, chflags)) != 0)
557     return rc;
558
559   if (delete) {
560     mutt_set_flag (Context, h, M_DELETE, 1);
561     if (option (OPTDELETEUNTAG))
562       mutt_set_flag (Context, h, M_TAG, 0);
563     mutt_set_flag (Context, h, M_APPENDED, 1);
564   }
565   return (0);
566 }
567
568 /* returns 0 if the copy/save was successful, or -1 on error/abort */
569 int mutt_save_message (HEADER * h, int delete,
570                        int decode, int decrypt, int *redraw) {
571   int i, need_buffy_cleanup;
572   int need_passphrase = 0, app = 0;
573   char prompt[STRING], buf[_POSIX_PATH_MAX];
574   CONTEXT ctx;
575   struct stat st;
576   struct utimbuf ut;
577
578   *redraw = 0;
579
580
581   snprintf (prompt, sizeof (prompt),
582             decode ? (delete ? _("Decode-save%s to mailbox") :
583                       _("Decode-copy%s to mailbox")) :
584             (decrypt ? (delete ? _("Decrypt-save%s to mailbox") :
585                         _("Decrypt-copy%s to mailbox")) :
586              (delete ? _("Save%s to mailbox") : _("Copy%s to mailbox"))),
587             h ? "" : _(" tagged"));
588
589
590   if (h) {
591     need_passphrase = h->security & ENCRYPT;
592     app = h->security;
593     mutt_message_hook (Context, h, M_MESSAGEHOOK);
594     mutt_default_save (buf, sizeof (buf), h);
595   }
596   else {
597     /* look for the first tagged message */
598
599     for (i = 0; i < Context->vcount; i++) {
600       if (Context->hdrs[Context->v2r[i]]->tagged) {
601         h = Context->hdrs[Context->v2r[i]];
602         break;
603       }
604     }
605
606
607     if (h) {
608       mutt_message_hook (Context, h, M_MESSAGEHOOK);
609       mutt_default_save (buf, sizeof (buf), h);
610       need_passphrase = h->security & ENCRYPT;
611       app = h->security;
612       h = NULL;
613     }
614   }
615
616   mutt_pretty_mailbox (buf);
617   if (mutt_enter_fname (prompt, buf, sizeof (buf), redraw, 0) == -1)
618     return (-1);
619
620   if (*redraw != REDRAW_FULL) {
621     if (!h)
622       *redraw = REDRAW_INDEX | REDRAW_STATUS;
623     else
624       *redraw = REDRAW_STATUS;
625   }
626
627   if (!buf[0])
628     return (-1);
629
630   /* This is an undocumented feature of ELM pointed out to me by Felix von
631    * Leitner <leitner@prz.fu-berlin.de>
632    */
633   if (m_strcmp(buf, ".") == 0)
634     m_strcpy(buf, sizeof(buf), LastSaveFolder);
635   else
636     m_strcpy(LastSaveFolder, sizeof(LastSaveFolder), buf);
637
638   mutt_expand_path (buf, sizeof (buf));
639
640   /* check to make sure that this file is really the one the user wants */
641   if (mutt_save_confirm (buf, &st) != 0)
642     return -1;
643
644   mutt_message (_("Copying to %s..."), buf);
645
646   if (Context->magic == M_IMAP && !(decode || decrypt) && mx_get_magic (buf) == M_IMAP) {
647     switch (imap_copy_messages (Context, h, buf, delete)) {
648       /* success */
649     case 0:
650       mutt_clear_error ();
651       return 0;
652       /* non-fatal error: fall through to fetch/append */
653     case 1:
654       break;
655       /* fatal error, abort */
656     case -1:
657       return -1;
658     }
659   }
660
661   if (mx_open_mailbox (buf, M_APPEND, &ctx) != NULL) {
662     if (h) {
663       if (_mutt_save_message (h, &ctx, delete, decode, decrypt) != 0) {
664         mx_close_mailbox (&ctx, NULL);
665         return (-1);
666       }
667     } else {
668       for (i = 0; i < Context->vcount; i++) {
669         if (Context->hdrs[Context->v2r[i]]->tagged) {
670           mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
671                              M_MESSAGEHOOK);
672           if (_mutt_save_message (Context->hdrs[Context->v2r[i]], &ctx, delete,
673                                   decode, decrypt) != 0) {
674             mx_close_mailbox (&ctx, NULL);
675             return (-1);
676           }
677         }
678       }
679     }
680
681     need_buffy_cleanup = (ctx.magic == M_MBOX);
682
683     mx_close_mailbox (&ctx, NULL);
684
685     if (need_buffy_cleanup) {
686       /* fix up the times so buffy won't get confused */
687       if (st.st_mtime > st.st_atime) {
688         ut.actime = st.st_atime;
689         ut.modtime = time (NULL);
690         utime (buf, &ut);
691       } else {
692         utime (buf, NULL);
693       }
694     }
695
696     mutt_clear_error ();
697     return (0);
698   }
699
700   return -1;
701 }
702
703 void mutt_version (void)
704 {
705   mutt_message (mutt_make_version());
706 }
707
708 void mutt_edit_content_type (HEADER * h, BODY * b, FILE * fp)
709 {
710   char buf[LONG_STRING];
711   char obuf[LONG_STRING];
712   char tmp[STRING];
713
714   char charset[STRING];
715   char *cp;
716
717   short charset_changed = 0;
718   short type_changed = 0;
719
720   cp = parameter_getval(b->parameter, "charset");
721   m_strcpy(charset, sizeof(charset), NONULL(cp));
722
723   snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
724   m_strcpy(obuf, sizeof(obuf), buf);
725   if (b->parameter) {
726     parameter_t *p;
727     ssize_t l;
728
729     for (p = b->parameter; p; p = p->next) {
730       l = m_strlen(buf);
731
732       rfc822_strcpy(tmp, sizeof(tmp), p->value, MimeSpecials);
733       snprintf (buf + l, sizeof (buf) - l, "; %s=%s", p->attribute, tmp);
734     }
735   }
736
737   if (mutt_get_field ("Content-Type: ", buf, sizeof (buf), 0) != 0 ||
738       buf[0] == 0)
739     return;
740
741   /* clean up previous junk */
742   parameter_list_wipe(&b->parameter);
743   p_delete(&b->subtype);
744
745   mutt_parse_content_type (buf, b);
746
747
748   snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
749   type_changed = ascii_strcasecmp (tmp, obuf);
750   charset_changed =
751     ascii_strcasecmp (charset, parameter_getval(b->parameter, "charset"));
752
753   /* if in send mode, check for conversion - current setting is default. */
754
755   if (!h && b->type == TYPETEXT && charset_changed) {
756     int r;
757
758     snprintf (tmp, sizeof (tmp), _("Convert to %s upon sending?"),
759               parameter_getval(b->parameter, "charset"));
760     if ((r = mutt_yesorno (tmp, !b->noconv)) != -1)
761       b->noconv = (r == M_NO);
762   }
763
764   /* inform the user */
765
766   snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
767   if (type_changed)
768     mutt_message (_("Content-Type changed to %s."), tmp);
769   if (b->type == TYPETEXT && charset_changed) {
770     if (type_changed)
771       mutt_sleep (1);
772     mutt_message (_("Character set changed to %s; %s."),
773                   parameter_getval(b->parameter, "charset"),
774                   b->noconv ? _("not converting") : _("converting"));
775   }
776
777   b->force_charset |= charset_changed ? 1 : 0;
778
779   if (!is_multipart(b) && b->parts)
780     body_list_wipe(&b->parts);
781   if (!mutt_is_message_type(b) && b->hdr) {
782     b->hdr->content = NULL;
783     header_delete(&b->hdr);
784   }
785
786   if (fp && (is_multipart(b) || mutt_is_message_type(b)))
787     mutt_parse_part (fp, b);
788
789   if (h) {
790     if (h->content == b)
791       h->security = 0;
792
793     h->security |= crypt_query (b);
794   }
795 }
796
797