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