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