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