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