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