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