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