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