use a proper "hack" for iconv functions:
[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 #if 0
159     /* this is maybe just plain wrong but it makes the pager display
160      * what we have; i.e. for the crypto stuff we only get
161      * 'Could not copy message' for invalid passphrases, no PGP output
162      * not nothing; so just display what we have...
163      * - pdmef
164      */
165     mutt_unlink (tempfile);
166     return 0;
167 #endif
168   }
169
170   if (fpfilterout != NULL && mutt_wait_filter (filterpid) != 0)
171     mutt_any_key_to_continue (NULL);
172
173   safe_fclose (&fpfilterout);   /* XXX - check result? */
174
175
176   if (WithCrypto) {
177     /* update crypto information for this message */
178     cur->security &= ~(GOODSIGN|BADSIGN);
179     cur->security |= crypt_query (cur->content);
180
181     /* Remove color cache for this message, in case there
182        are color patterns for both ~g and ~V */
183     cur->pair = 0;
184   }
185
186   if (builtin) {
187     pager_t info;
188
189     if (WithCrypto
190         && (cur->security & APPLICATION_SMIME) && (cmflags & M_CM_VERIFY)) {
191       if (cur->security & GOODSIGN) {
192         if (!crypt_smime_verify_sender (cur))
193           mutt_message (_("S/MIME signature successfully verified."));
194         else
195           mutt_error (_("S/MIME certificate owner does not match sender."));
196       }
197       else if (cur->security & PARTSIGN)
198         mutt_message (_
199                       ("Warning: Part of this message has not been signed."));
200       else if (cur->security & SIGN || cur->security & BADSIGN)
201         mutt_error (_("S/MIME signature could NOT be verified."));
202     }
203
204     if (WithCrypto
205         && (cur->security & APPLICATION_PGP) && (cmflags & M_CM_VERIFY)) {
206       if (cur->security & GOODSIGN)
207         mutt_message (_("PGP signature successfully verified."));
208       else if (cur->security & PARTSIGN)
209         mutt_message (_
210                       ("Warning: Part of this message has not been signed."));
211       else if (cur->security & SIGN)
212         mutt_message (_("PGP signature could NOT be verified."));
213     }
214
215     /* Invoke the builtin pager */
216     memset (&info, 0, sizeof (pager_t));
217     info.hdr = cur;
218     info.ctx = Context;
219     rc = mutt_pager (NULL, tempfile, M_PAGER_MESSAGE, &info);
220   }
221   else {
222     int r;
223
224     mutt_endwin (NULL);
225     snprintf (buf, sizeof (buf), "%s %s", NONULL (Pager), tempfile);
226     if ((r = mutt_system (buf)) == -1)
227       mutt_error (_("Error running \"%s\"!"), buf);
228     unlink (tempfile);
229     keypad (stdscr, TRUE);
230     if (r != -1)
231       mutt_set_flag (Context, cur, M_READ, 1);
232     if (r != -1 && option (OPTPROMPTAFTER)) {
233       mutt_ungetch (mutt_any_key_to_continue _("Command: "), 0);
234       rc = km_dokey (MENU_PAGER);
235     }
236     else
237       rc = 0;
238   }
239
240   return rc;
241 }
242
243 void ci_bounce_message (HEADER * h, int *redraw)
244 {
245   char prompt[SHORT_STRING];
246   char buf[HUGE_STRING] = { 0 };
247   ADDRESS *adr = NULL;
248   char *err = NULL;
249   int rc;
250
251   if (h)
252     strfcpy (prompt, _("Bounce message to: "), sizeof (prompt));
253   else
254     strfcpy (prompt, _("Bounce tagged messages to: "), sizeof (prompt));
255
256   rc = mutt_get_field (prompt, buf, sizeof (buf), M_ALIAS);
257
258   if (option (OPTNEEDREDRAW)) {
259     unset_option (OPTNEEDREDRAW);
260     *redraw = REDRAW_FULL;
261   }
262
263   if (rc || !buf[0])
264     return;
265
266   if (!(adr = rfc822_parse_adrlist (adr, buf))) {
267     mutt_error _("Error parsing address!");
268
269     return;
270   }
271
272   adr = mutt_expand_aliases (adr);
273
274   if (mutt_addrlist_to_idna (adr, &err) < 0) {
275     mutt_error (_("Bad IDN: '%s'"), err);
276     mem_free (&err);
277     rfc822_free_address (&adr);
278     return;
279   }
280
281   buf[0] = 0;
282   rfc822_write_address (buf, sizeof (buf), adr, 1);
283
284 #define extra_space (15 + 7 + 2)
285   snprintf (prompt, sizeof (prompt),
286             (h ? _("Bounce message to %s") : _("Bounce messages to %s")),
287             buf);
288
289   if (mutt_strwidth (prompt) > COLS - extra_space) {
290     mutt_format_string (prompt, sizeof (prompt),
291                         0, COLS - extra_space, 0, 0,
292                         prompt, sizeof (prompt), 0);
293     str_cat (prompt, sizeof (prompt), "...?");
294   }
295   else
296     str_cat (prompt, sizeof (prompt), "?");
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,
354                                int decode, int print, int split, 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       strfcpy (buf, Shell, sizeof (buf));
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   memset (&token, 0, sizeof (token));
583   r = mutt_parse_rc_line (buffer, &token, &err);
584   mem_free (&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   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 (str_cmp (buf, ".") == 0)
753     strfcpy (buf, LastSaveFolder, sizeof (buf));
754   else
755     strfcpy (LastSaveFolder, buf, sizeof (LastSaveFolder));
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   strfcpy (charset, NONULL (cp), sizeof (charset));
854
855   snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
856   strfcpy (obuf, buf, sizeof (obuf));
857   if (b->parameter) {
858     size_t l;
859
860     for (p = b->parameter; p; p = p->next) {
861       l = str_len (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   mem_free (&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 }