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