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