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