push more things in the str lib.
[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/file.h>
17 #include <lib-lib/macros.h>
18 #include <lib-lib/ascii.h>
19
20 #include "mutt.h"
21 #include "enter.h"
22 #include "recvattach.h"
23 #include "mutt_curses.h"
24 #include "mutt_menu.h"
25 #include "mime.h"
26 #include "sort.h"
27 #include "copy.h"
28 #include "mx.h"
29 #include "pager.h"
30 #include "mutt_crypt.h"
31 #include "mutt_idna.h"
32 #include "rfc1524.h"
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36
37 #ifdef USE_IMAP
38 #include "imap.h"
39 #endif
40
41 #ifdef BUFFY_SIZE
42 #include "buffy.h"
43 #endif
44
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 || m_strcmp(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     p_clear(&info, 1);
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     m_strcpy(prompt, sizeof(prompt), _("Bounce message to: "));
254   else
255     m_strcpy(prompt, sizeof(prompt), _("Bounce tagged messages to: "));
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), 0, COLS - extra_space, 0, 0,
292                        prompt, sizeof(prompt), 0);
293     m_strcat(prompt, sizeof(prompt), "...?");
294   } else {
295     m_strcat(prompt, sizeof(prompt), "?");
296   }
297
298   if (query_quadoption (OPT_BOUNCE, prompt) != M_YES) {
299     rfc822_free_address (&adr);
300     CLEARLINE (LINES - 1);
301     mutt_message (h ? _("Message not bounced.") : _("Messages not bounced."));
302     return;
303   }
304
305   CLEARLINE (LINES - 1);
306
307   rc = mutt_bounce_message (NULL, h, adr);
308   rfc822_free_address (&adr);
309   /* If no error, or background, display message. */
310   if ((rc == 0) || (rc == S_BKG))
311     mutt_message (h ? _("Message bounced.") : _("Messages bounced."));
312 }
313
314 static void pipe_set_flags (int decode, int print, int *cmflags, int *chflags)
315 {
316   if (decode) {
317     *cmflags |= M_CM_DECODE | M_CM_CHARCONV;
318     *chflags |= CH_DECODE | CH_REORDER;
319
320     if (option (OPTWEED)) {
321       *chflags |= CH_WEED;
322       *cmflags |= M_CM_WEED;
323     }
324   }
325
326   if (print)
327     *cmflags |= M_CM_PRINTING;
328
329 }
330
331 static void pipe_msg (HEADER * h, FILE * fp, int decode, int print)
332 {
333   int cmflags = 0;
334   int chflags = CH_FROM;
335
336   pipe_set_flags (decode, print, &cmflags, &chflags);
337
338   if (WithCrypto && decode && h->security & ENCRYPT) {
339     if (!crypt_valid_passphrase (h->security))
340       return;
341     endwin ();
342   }
343
344   if (decode)
345     mutt_parse_mime_message (Context, h);
346
347   mutt_copy_message (fp, Context, h, cmflags, chflags);
348 }
349
350
351 /* the following code is shared between printing and piping */
352
353 static int _mutt_pipe_message(HEADER * h, char *cmd, int decode, int print,
354                               int split, const char *sep)
355 {
356
357   int i, rc = 0;
358   pid_t thepid;
359   FILE *fpout;
360
361 /*   mutt_endwin (NULL); 
362
363      is this really needed here ? 
364      it makes the screen flicker on pgp and s/mime messages,
365      before asking for a passphrase...
366                                      Oliver Ehli */
367   if (h) {
368
369     mutt_message_hook (Context, h, M_MESSAGEHOOK);
370
371     if (WithCrypto && decode) {
372       mutt_parse_mime_message (Context, h);
373       if (h->security & ENCRYPT && !crypt_valid_passphrase (h->security))
374         return 1;
375     }
376     mutt_endwin (NULL);
377
378     if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
379       mutt_perror (_("Can't create filter process"));
380
381       return 1;
382     }
383
384     pipe_msg (h, fpout, decode, print);
385     fclose (fpout);
386     rc = mutt_wait_filter (thepid);
387   }
388   else {                        /* handle tagged messages */
389
390     if (WithCrypto && decode) {
391       for (i = 0; i < Context->vcount; i++)
392         if (Context->hdrs[Context->v2r[i]]->tagged) {
393           mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
394                              M_MESSAGEHOOK);
395           mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
396           if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT &&
397               !crypt_valid_passphrase (Context->hdrs[Context->v2r[i]]->
398                                        security))
399             return 1;
400         }
401     }
402
403     if (split) {
404       for (i = 0; i < Context->vcount; i++) {
405         if (Context->hdrs[Context->v2r[i]]->tagged) {
406           mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
407                              M_MESSAGEHOOK);
408           mutt_endwin (NULL);
409           if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
410             mutt_perror (_("Can't create filter process"));
411
412             return 1;
413           }
414           pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
415           /* add the message separator */
416           if (sep)
417             fputs (sep, fpout);
418           safe_fclose (&fpout);
419           if (mutt_wait_filter (thepid) != 0)
420             rc = 1;
421         }
422       }
423     }
424     else {
425       mutt_endwin (NULL);
426       if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
427         mutt_perror (_("Can't create filter process"));
428
429         return 1;
430       }
431       for (i = 0; i < Context->vcount; i++) {
432         if (Context->hdrs[Context->v2r[i]]->tagged) {
433           mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
434                              M_MESSAGEHOOK);
435           pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
436           /* add the message separator */
437           if (sep)
438             fputs (sep, fpout);
439         }
440       }
441       safe_fclose (&fpout);
442       if (mutt_wait_filter (thepid) != 0)
443         rc = 1;
444     }
445   }
446
447   if (rc || option (OPTWAITKEY))
448     mutt_any_key_to_continue (NULL);
449   return rc;
450 }
451
452 void mutt_pipe_message (HEADER * h)
453 {
454   char buffer[LONG_STRING];
455
456   buffer[0] = 0;
457   if (mutt_get_field (_("Pipe to command: "), buffer, sizeof (buffer), M_CMD)
458       != 0 || !buffer[0])
459     return;
460
461   mutt_expand_path (buffer, sizeof (buffer));
462   _mutt_pipe_message (h, buffer,
463                       option (OPTPIPEDECODE),
464                       0, option (OPTPIPESPLIT), PipeSep);
465 }
466
467 void mutt_print_message (HEADER * h)
468 {
469
470   if (quadoption (OPT_PRINT) && (!PrintCmd || !*PrintCmd)) {
471     mutt_message (_("No printing command has been defined."));
472     return;
473   }
474
475   if (query_quadoption (OPT_PRINT,
476                         h ? _("Print message?") : _("Print tagged messages?"))
477       != M_YES)
478     return;
479
480   if (_mutt_pipe_message (h, PrintCmd,
481                           option (OPTPRINTDECODE),
482                           1, option (OPTPRINTSPLIT), "\f") == 0)
483     mutt_message (h ? _("Message printed") : _("Messages printed"));
484   else
485     mutt_message (h ? _("Message could not be printed") :
486                   _("Messages could not be printed"));
487 }
488
489
490 int mutt_select_sort (int reverse)
491 {
492   int method = Sort;            /* save the current method in case of abort */
493
494   switch (mutt_multi_choice (reverse ?
495                              _
496                              ("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?: ")
497                              :
498                              _
499                              ("Sort (d)ate/(f)rm/(r)ecv/(s)ubj/t(o)/(t)hread/(u)nsort/si(z)e/s(c)ore/s(p)am?: "),
500                              _("dfrsotuzcp"))) {
501   case -1:                     /* abort - don't resort */
502     return -1;
503
504   case 1:                      /* (d)ate */
505     Sort = SORT_DATE;
506     break;
507
508   case 2:                      /* (f)rm */
509     Sort = SORT_FROM;
510     break;
511
512   case 3:                      /* (r)ecv */
513     Sort = SORT_RECEIVED;
514     break;
515
516   case 4:                      /* (s)ubj */
517     Sort = SORT_SUBJECT;
518     break;
519
520   case 5:                      /* t(o) */
521     Sort = SORT_TO;
522     break;
523
524   case 6:                      /* (t)hread */
525     Sort = SORT_THREADS;
526     break;
527
528   case 7:                      /* (u)nsort */
529     Sort = SORT_ORDER;
530     break;
531
532   case 8:                      /* si(z)e */
533     Sort = SORT_SIZE;
534     break;
535
536   case 9:                      /* s(c)ore */
537     Sort = SORT_SCORE;
538     break;
539
540   case 10:                     /* s(p)am */
541     Sort = SORT_SPAM;
542     break;
543   }
544   if (reverse)
545     Sort |= SORT_REVERSE;
546
547   return (Sort != method ? 0 : -1);     /* no need to resort if it's the same */
548 }
549
550 /* invoke a command in a subshell */
551 void mutt_shell_escape (void)
552 {
553   char buf[LONG_STRING];
554
555   buf[0] = 0;
556   if (mutt_get_field (_("Shell command: "), buf, sizeof (buf), M_CMD) == 0) {
557     if (!buf[0] && Shell)
558       m_strcpy(buf, sizeof(buf), Shell);
559     if (buf[0]) {
560       CLEARLINE (LINES - 1);
561       mutt_endwin (NULL);
562       fflush (stdout);
563       if (mutt_system (buf) != 0 || option (OPTWAITKEY))
564         mutt_any_key_to_continue (NULL);
565     }
566   }
567 }
568
569 /* enter a mutt command */
570 void mutt_enter_command (void)
571 {
572   BUFFER err, token;
573   char buffer[LONG_STRING], errbuf[SHORT_STRING];
574   int r;
575
576   buffer[0] = 0;
577   if (mutt_get_field (":", buffer, sizeof (buffer), M_COMMAND) != 0
578       || !buffer[0])
579     return;
580   err.data = errbuf;
581   err.dsize = sizeof (errbuf);
582   p_clear(&token, 1);
583   r = mutt_parse_rc_line (buffer, &token, &err);
584   p_delete(&token.data);
585   if (errbuf[0]) {
586     /* since errbuf could potentially contain printf() sequences in it,
587        we must call mutt_error() in this fashion so that vsprintf()
588        doesn't expect more arguments that we passed */
589     if (r == 0)
590       mutt_message ("%s", errbuf);
591     else
592       mutt_error ("%s", errbuf);
593   }
594 }
595
596 void mutt_display_address (ENVELOPE * env)
597 {
598   const char *pfx = NULL;
599   char buf[SHORT_STRING];
600   ADDRESS *adr = NULL;
601
602   adr = mutt_get_address(env, &pfx);
603
604   if (!adr)
605     return;
606
607   /* 
608    * Note: We don't convert IDNA to local representation this time.
609    * That is intentional, so the user has an opportunity to copy &
610    * paste the on-the-wire form of the address to other, IDN-unable
611    * software. 
612    */
613
614   buf[0] = 0;
615   rfc822_write_address (buf, sizeof (buf), adr, 0);
616   mutt_message ("%s: %s", pfx, buf);
617 }
618
619 static void set_copy_flags (HEADER * hdr, int decode, int decrypt,
620                             int *cmflags, int *chflags)
621 {
622   *cmflags = 0;
623   *chflags = CH_UPDATE_LEN;
624
625   if (WithCrypto && !decode && decrypt && (hdr->security & ENCRYPT)) {
626     if ((WithCrypto & APPLICATION_PGP)
627         && mutt_is_multipart_encrypted (hdr->content)) {
628       *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
629       *cmflags = M_CM_DECODE_PGP;
630     }
631     else if ((WithCrypto & APPLICATION_PGP)
632              && mutt_is_application_pgp (hdr->content) & ENCRYPT)
633       decode = 1;
634     else if ((WithCrypto & APPLICATION_SMIME)
635              && mutt_is_application_smime (hdr->content) & ENCRYPT) {
636       *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
637       *cmflags = M_CM_DECODE_SMIME;
638     }
639   }
640
641   if (decode) {
642     *chflags = CH_XMIT | CH_MIME | CH_TXTPLAIN;
643     *cmflags = M_CM_DECODE | M_CM_CHARCONV;
644
645     if (!decrypt) {             /* If decode doesn't kick in for decrypt, */
646       *chflags |= CH_DECODE;    /* then decode RFC 2047 headers, */
647
648       if (option (OPTWEED)) {
649         *chflags |= CH_WEED;    /* and respect $weed. */
650         *cmflags |= M_CM_WEED;
651       }
652     }
653   }
654 }
655
656 int _mutt_save_message (HEADER * h, CONTEXT * ctx, int delete, int decode,
657                          int decrypt) {
658   int cmflags, chflags;
659   int rc;
660
661   set_copy_flags (h, decode, decrypt, &cmflags, &chflags);
662
663   if (decode || decrypt)
664     mutt_parse_mime_message (Context, h);
665
666   if ((rc = mutt_append_message (ctx, Context, h, cmflags, chflags)) != 0)
667     return rc;
668
669   if (delete) {
670     mutt_set_flag (Context, h, M_DELETE, 1);
671     if (option (OPTDELETEUNTAG))
672       mutt_set_flag (Context, h, M_TAG, 0);
673     mutt_set_flag (Context, h, M_APPENDED, 1);
674   }
675   return (0);
676 }
677
678 /* returns 0 if the copy/save was successful, or -1 on error/abort */
679 int mutt_save_message (HEADER * h, int delete,
680                        int decode, int decrypt, int *redraw) {
681   int i, need_buffy_cleanup;
682   int need_passphrase = 0, app = 0;
683   char prompt[SHORT_STRING], buf[_POSIX_PATH_MAX];
684   CONTEXT ctx;
685   struct stat st;
686
687 #ifdef BUFFY_SIZE
688   BUFFY *tmp = NULL;
689 #else
690   struct utimbuf ut;
691 #endif
692
693   *redraw = 0;
694
695
696   snprintf (prompt, sizeof (prompt),
697             decode ? (delete ? _("Decode-save%s to mailbox") :
698                       _("Decode-copy%s to mailbox")) :
699             (decrypt ? (delete ? _("Decrypt-save%s to mailbox") :
700                         _("Decrypt-copy%s to mailbox")) :
701              (delete ? _("Save%s to mailbox") : _("Copy%s to mailbox"))),
702             h ? "" : _(" tagged"));
703
704
705   if (h) {
706     if (WithCrypto) {
707       need_passphrase = h->security & ENCRYPT;
708       app = h->security;
709     }
710     mutt_message_hook (Context, h, M_MESSAGEHOOK);
711     mutt_default_save (buf, sizeof (buf), h);
712   }
713   else {
714     /* look for the first tagged message */
715
716     for (i = 0; i < Context->vcount; i++) {
717       if (Context->hdrs[Context->v2r[i]]->tagged) {
718         h = Context->hdrs[Context->v2r[i]];
719         break;
720       }
721     }
722
723
724     if (h) {
725       mutt_message_hook (Context, h, M_MESSAGEHOOK);
726       mutt_default_save (buf, sizeof (buf), h);
727       if (WithCrypto) {
728         need_passphrase = h->security & ENCRYPT;
729         app = h->security;
730       }
731       h = NULL;
732     }
733   }
734
735   mutt_pretty_mailbox (buf);
736   if (mutt_enter_fname (prompt, buf, sizeof (buf), redraw, 0) == -1)
737     return (-1);
738
739   if (*redraw != REDRAW_FULL) {
740     if (!h)
741       *redraw = REDRAW_INDEX | REDRAW_STATUS;
742     else
743       *redraw = REDRAW_STATUS;
744   }
745
746   if (!buf[0])
747     return (-1);
748
749   /* This is an undocumented feature of ELM pointed out to me by Felix von
750    * Leitner <leitner@prz.fu-berlin.de>
751    */
752   if (m_strcmp(buf, ".") == 0)
753     m_strcpy(buf, sizeof(buf), LastSaveFolder);
754   else
755     m_strcpy(LastSaveFolder, sizeof(LastSaveFolder), buf);
756
757   mutt_expand_path (buf, sizeof (buf));
758
759   /* check to make sure that this file is really the one the user wants */
760   if (mutt_save_confirm (buf, &st) != 0)
761     return -1;
762
763   if (WithCrypto && need_passphrase && (decode || decrypt)
764       && !crypt_valid_passphrase (app))
765     return -1;
766
767   mutt_message (_("Copying to %s..."), buf);
768
769 #ifdef USE_IMAP
770   if (Context->magic == M_IMAP && !(decode || decrypt) && mx_get_magic (buf) == M_IMAP) {
771     switch (imap_copy_messages (Context, h, buf, delete)) {
772       /* success */
773     case 0:
774       mutt_clear_error ();
775       return 0;
776       /* non-fatal error: fall through to fetch/append */
777     case 1:
778       break;
779       /* fatal error, abort */
780     case -1:
781       return -1;
782     }
783   }
784 #endif
785
786   if (mx_open_mailbox (buf, M_APPEND, &ctx) != NULL) {
787     if (h) {
788       if (_mutt_save_message (h, &ctx, delete, decode, decrypt) != 0) {
789         mx_close_mailbox (&ctx, NULL);
790         return (-1);
791       }
792     } else {
793       for (i = 0; i < Context->vcount; i++) {
794         if (Context->hdrs[Context->v2r[i]]->tagged) {
795           mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
796                              M_MESSAGEHOOK);
797           if (_mutt_save_message (Context->hdrs[Context->v2r[i]], &ctx, delete,
798                                   decode, decrypt) != 0) {
799             mx_close_mailbox (&ctx, NULL);
800             return (-1);
801           }
802         }
803       }
804     }
805
806     need_buffy_cleanup = (ctx.magic == M_MBOX || ctx.magic == M_MMDF);
807
808     mx_close_mailbox (&ctx, NULL);
809
810     if (need_buffy_cleanup) {
811 #ifdef BUFFY_SIZE
812       tmp = buffy_find_mailbox (buf);
813       if (tmp && tmp->new <= 0)
814         buffy_update_mailbox (tmp);
815 #else
816       /* fix up the times so buffy won't get confused */
817       if (st.st_mtime > st.st_atime) {
818         ut.actime = st.st_atime;
819         ut.modtime = time (NULL);
820         utime (buf, &ut);
821       }
822       else
823         utime (buf, NULL);
824 #endif
825     }
826
827     mutt_clear_error ();
828     return (0);
829   }
830
831   return -1;
832 }
833
834 void mutt_version (void)
835 {
836   mutt_message (mutt_make_version (1));
837 }
838
839 void mutt_edit_content_type (HEADER * h, BODY * b, FILE * fp)
840 {
841   char buf[LONG_STRING];
842   char obuf[LONG_STRING];
843   char tmp[STRING];
844   PARAMETER *p;
845
846   char charset[STRING];
847   char *cp;
848
849   short charset_changed = 0;
850   short type_changed = 0;
851
852   cp = mutt_get_parameter ("charset", b->parameter);
853   m_strcpy(charset, sizeof(charset), NONULL(cp));
854
855   snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
856   m_strcpy(obuf, sizeof(obuf), buf);
857   if (b->parameter) {
858     size_t l;
859
860     for (p = b->parameter; p; p = p->next) {
861       l = m_strlen(buf);
862
863       rfc822_cat (tmp, sizeof (tmp), p->value, MimeSpecials);
864       snprintf (buf + l, sizeof (buf) - l, "; %s=%s", p->attribute, tmp);
865     }
866   }
867
868   if (mutt_get_field ("Content-Type: ", buf, sizeof (buf), 0) != 0 ||
869       buf[0] == 0)
870     return;
871
872   /* clean up previous junk */
873   mutt_free_parameter (&b->parameter);
874   p_delete(&b->subtype);
875
876   mutt_parse_content_type (buf, b);
877
878
879   snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
880   type_changed = ascii_strcasecmp (tmp, obuf);
881   charset_changed =
882     ascii_strcasecmp (charset, mutt_get_parameter ("charset", b->parameter));
883
884   /* if in send mode, check for conversion - current setting is default. */
885
886   if (!h && b->type == TYPETEXT && charset_changed) {
887     int r;
888
889     snprintf (tmp, sizeof (tmp), _("Convert to %s upon sending?"),
890               mutt_get_parameter ("charset", b->parameter));
891     if ((r = mutt_yesorno (tmp, !b->noconv)) != -1)
892       b->noconv = (r == M_NO);
893   }
894
895   /* inform the user */
896
897   snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
898   if (type_changed)
899     mutt_message (_("Content-Type changed to %s."), tmp);
900   if (b->type == TYPETEXT && charset_changed) {
901     if (type_changed)
902       mutt_sleep (1);
903     mutt_message (_("Character set changed to %s; %s."),
904                   mutt_get_parameter ("charset", b->parameter),
905                   b->noconv ? _("not converting") : _("converting"));
906   }
907
908   b->force_charset |= charset_changed ? 1 : 0;
909
910   if (!is_multipart (b) && b->parts)
911     mutt_free_body (&b->parts);
912   if (!mutt_is_message_type (b->type, b->subtype) && b->hdr) {
913     b->hdr->content = NULL;
914     mutt_free_header (&b->hdr);
915   }
916
917   if (fp && (is_multipart (b) || mutt_is_message_type (b->type, b->subtype)))
918     mutt_parse_part (fp, b);
919
920   if (WithCrypto && h) {
921     if (h->content == b)
922       h->security = 0;
923
924     h->security |= crypt_query (b);
925   }
926 }
927
928
929 static int _mutt_check_traditional_pgp (HEADER * h, int *redraw)
930 {
931   MESSAGE *msg;
932   int rv = 0;
933
934   h->security |= PGP_TRADITIONAL_CHECKED;
935
936   mutt_parse_mime_message (Context, h);
937   if ((msg = mx_open_message (Context, h->msgno)) == NULL)
938     return 0;
939   if (crypt_pgp_check_traditional (msg->fp, h->content, 0)) {
940     h->security = crypt_query (h->content);
941     *redraw |= REDRAW_FULL;
942     rv = 1;
943   }
944
945   h->security |= PGP_TRADITIONAL_CHECKED;
946   mx_close_message (&msg);
947   return rv;
948 }
949
950 int mutt_check_traditional_pgp (HEADER * h, int *redraw)
951 {
952   int i;
953   int rv = 0;
954
955   if (h && !(h->security & PGP_TRADITIONAL_CHECKED))
956     rv = _mutt_check_traditional_pgp (h, redraw);
957   else {
958     for (i = 0; i < Context->vcount; i++)
959       if (Context->hdrs[Context->v2r[i]]->tagged &&
960           !(Context->hdrs[Context->v2r[i]]->
961             security & PGP_TRADITIONAL_CHECKED))
962         rv =
963           _mutt_check_traditional_pgp (Context->hdrs[Context->v2r[i]], redraw)
964           || rv;
965   }
966   return rv;
967 }