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