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     safe_strcat (prompt, sizeof (prompt), "...?");
287   }
288   else
289     safe_strcat (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 static 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/s(p)am?: ") :
506                              _("Sort (d)ate/(f)rm/(r)ecv/(s)ubj/t(o)/(t)hread/(u)nsort/si(z)e/s(c)ore/s(p)am?: "),
507                              _("dfrsotuzcp")))
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   case 10: /* s(p)am */
549     Sort = SORT_SPAM;
550     break;
551   }
552   if (reverse)
553     Sort |= SORT_REVERSE;
554
555   return (Sort != method ? 0 : -1); /* no need to resort if it's the same */
556 }
557
558 /* invoke a command in a subshell */
559 void mutt_shell_escape (void)
560 {
561   char buf[LONG_STRING];
562
563   buf[0] = 0;
564   if (mutt_get_field (_("Shell command: "), buf, sizeof (buf), M_CMD) == 0)
565   {
566     if (!buf[0] && Shell)
567       strfcpy (buf, Shell, sizeof (buf));
568     if(buf[0])
569     {
570       CLEARLINE (LINES-1);
571       mutt_endwin (NULL);
572       fflush (stdout);
573       if (mutt_system (buf) != 0 || option (OPTWAITKEY))
574         mutt_any_key_to_continue (NULL);
575     }
576   }
577 }
578
579 /* enter a mutt command */
580 void mutt_enter_command (void)
581 {
582   BUFFER err, token;
583   char buffer[LONG_STRING], errbuf[SHORT_STRING];
584   int r;
585
586   buffer[0] = 0;
587   if (mutt_get_field (":", buffer, sizeof (buffer), M_COMMAND) != 0 || !buffer[0])
588     return;
589   err.data = errbuf;
590   err.dsize = sizeof (errbuf);
591   memset (&token, 0, sizeof (token));
592   r = mutt_parse_rc_line (buffer, &token, &err);
593   FREE (&token.data);
594   if (errbuf[0])
595   {
596     /* since errbuf could potentially contain printf() sequences in it,
597        we must call mutt_error() in this fashion so that vsprintf()
598        doesn't expect more arguments that we passed */
599     if (r == 0)
600       mutt_message ("%s", errbuf);
601     else
602       mutt_error ("%s", errbuf);
603   }
604 }
605
606 void mutt_display_address (ENVELOPE *env)
607 {
608   char *pfx = NULL;
609   char buf[SHORT_STRING];
610   ADDRESS *adr = NULL;
611
612   adr = mutt_get_address (env, &pfx);
613
614   if (!adr) return;
615   
616   /* 
617    * Note: We don't convert IDNA to local representation this time.
618    * That is intentional, so the user has an opportunity to copy &
619    * paste the on-the-wire form of the address to other, IDN-unable
620    * software. 
621    */
622   
623   buf[0] = 0;
624   rfc822_write_address (buf, sizeof (buf), adr, 0);
625   mutt_message ("%s: %s", pfx, buf);
626 }
627
628 static void set_copy_flags (HEADER *hdr, int decode, int decrypt, int *cmflags, int *chflags)
629 {
630   *cmflags = 0;
631   *chflags = CH_UPDATE_LEN;
632   
633   if (WithCrypto && !decode && decrypt && (hdr->security & ENCRYPT))
634   {
635     if ((WithCrypto & APPLICATION_PGP)
636         && mutt_is_multipart_encrypted(hdr->content))
637     {
638       *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
639       *cmflags = M_CM_DECODE_PGP;
640     }
641     else if ((WithCrypto & APPLICATION_PGP)
642               && mutt_is_application_pgp (hdr->content) & ENCRYPT)
643       decode = 1;
644     else if ((WithCrypto & APPLICATION_SMIME)
645              && mutt_is_application_smime(hdr->content) & ENCRYPT)
646     {
647       *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
648       *cmflags = M_CM_DECODE_SMIME;
649     }
650   }
651
652   if (decode)
653   {
654     *chflags = CH_XMIT | CH_MIME | CH_TXTPLAIN;
655     *cmflags = M_CM_DECODE | M_CM_CHARCONV;
656
657     if (!decrypt)       /* If decode doesn't kick in for decrypt, */
658     {
659       *chflags |= CH_DECODE;    /* then decode RFC 2047 headers, */
660
661       if (option (OPTWEED))
662       {
663         *chflags |= CH_WEED;    /* and respect $weed. */
664         *cmflags |= M_CM_WEED;
665       }
666     }
667   }
668 }
669
670 void _mutt_save_message (HEADER *h, CONTEXT *ctx, int delete, int decode, int decrypt)
671 {
672   int cmflags, chflags;
673   
674   set_copy_flags (h, decode, decrypt, &cmflags, &chflags);
675
676   if (decode || decrypt)
677     mutt_parse_mime_message (Context, h);
678
679   if (mutt_append_message (ctx, Context, h, cmflags, chflags) == 0)
680   {
681     if (delete)
682     {
683       mutt_set_flag (Context, h, M_DELETE, 1);
684       if (option (OPTDELETEUNTAG))
685         mutt_set_flag (Context, h, M_TAG, 0);
686     }
687     mutt_set_flag (Context, h, M_APPENDED, 1);
688   }
689 }
690
691 /* returns 0 if the copy/save was successful, or -1 on error/abort */
692 int mutt_save_message (HEADER *h, int delete, 
693                        int decode, int decrypt, int *redraw)
694 {
695   int i, need_buffy_cleanup;
696   int need_passphrase = 0, app=0;
697   char prompt[SHORT_STRING], buf[_POSIX_PATH_MAX];
698   CONTEXT ctx;
699   struct stat st;
700 #ifdef BUFFY_SIZE
701   BUFFY *tmp = NULL;
702 #else
703   struct utimbuf ut;
704 #endif
705
706   *redraw = 0;
707
708   
709   snprintf (prompt, sizeof (prompt),
710             decode  ? (delete ? _("Decode-save%s to mailbox") :
711                        _("Decode-copy%s to mailbox")) :
712             (decrypt ? (delete ? _("Decrypt-save%s to mailbox") :
713                         _("Decrypt-copy%s to mailbox")) :
714              (delete ? _("Save%s to mailbox") : _("Copy%s to mailbox"))),
715             h ? "" : _(" tagged"));
716   
717
718   if (h)
719   {
720     if (WithCrypto)
721     {
722       need_passphrase = h->security & ENCRYPT;
723       app = h->security;
724     }
725     mutt_message_hook (Context, h, M_MESSAGEHOOK);
726     mutt_default_save (buf, sizeof (buf), h);
727   }
728   else
729   {
730     /* look for the first tagged message */
731
732     for (i = 0; i < Context->vcount; i++)
733     {
734       if (Context->hdrs[Context->v2r[i]]->tagged)
735       {
736         h = Context->hdrs[Context->v2r[i]];
737         break;
738       }
739     }
740
741
742     if (h)
743     {
744       mutt_message_hook (Context, h, M_MESSAGEHOOK);
745       mutt_default_save (buf, sizeof (buf), h);
746       if (WithCrypto)
747       {
748         need_passphrase = h->security & ENCRYPT;
749         app = h->security;
750       }
751       h = NULL;
752     }
753   }
754
755   mutt_pretty_mailbox (buf);
756   if (mutt_enter_fname (prompt, buf, sizeof (buf), redraw, 0) == -1)
757     return (-1);
758
759   if (*redraw != REDRAW_FULL)
760   {
761     if (!h)
762       *redraw = REDRAW_INDEX | REDRAW_STATUS;
763     else
764       *redraw = REDRAW_STATUS;
765   }
766
767   if (!buf[0])
768     return (-1);
769  
770   /* This is an undocumented feature of ELM pointed out to me by Felix von
771    * Leitner <leitner@prz.fu-berlin.de>
772    */
773   if (mutt_strcmp (buf, ".") == 0)
774     strfcpy (buf, LastSaveFolder, sizeof (buf));
775   else
776     strfcpy (LastSaveFolder, buf, sizeof (LastSaveFolder));
777
778   mutt_expand_path (buf, sizeof (buf));
779
780   /* check to make sure that this file is really the one the user wants */
781   if (mutt_save_confirm (buf, &st) != 0)
782     return -1;
783
784   if (WithCrypto && need_passphrase && (decode || decrypt)
785       && !crypt_valid_passphrase(app))
786     return -1;
787   
788   mutt_message (_("Copying to %s..."), buf);
789   
790 #ifdef USE_IMAP
791   if (Context->magic == M_IMAP && 
792       !(decode || decrypt) && mx_is_imap (buf))
793   {
794     switch (imap_copy_messages (Context, h, buf, delete))
795     {
796       /* success */
797       case 0: mutt_clear_error (); return 0;
798       /* non-fatal error: fall through to fetch/append */
799       case 1: break;
800       /* fatal error, abort */
801       case -1: return -1;
802     }
803   }
804 #endif
805
806   if (mx_open_mailbox (buf, M_APPEND, &ctx) != NULL)
807   {
808     if (h)
809       _mutt_save_message(h, &ctx, delete, decode, decrypt);
810     else
811     {
812       for (i = 0; i < Context->vcount; i++)
813       {
814         if (Context->hdrs[Context->v2r[i]]->tagged)
815         {
816           mutt_message_hook (Context, Context->hdrs[Context->v2r[i]], M_MESSAGEHOOK);
817           _mutt_save_message(Context->hdrs[Context->v2r[i]],
818                              &ctx, delete, decode, decrypt);
819         }
820       }
821     }
822
823     need_buffy_cleanup = (ctx.magic == M_MBOX || ctx.magic == M_MMDF);
824
825     mx_close_mailbox (&ctx, NULL);
826
827     if (need_buffy_cleanup)
828     {
829 #ifdef BUFFY_SIZE
830       tmp = mutt_find_mailbox (buf);
831       if (tmp && !tmp->new)
832         mutt_update_mailbox (tmp);
833 #else
834       /* fix up the times so buffy won't get confused */
835       if (st.st_mtime > st.st_atime)
836       {
837         ut.actime = st.st_atime;
838         ut.modtime = time (NULL);
839         utime (buf, &ut); 
840       }
841       else
842         utime (buf, NULL);
843 #endif
844     }
845
846     mutt_clear_error ();
847     return (0);
848   }
849   
850   return -1;
851 }
852
853 int mutt_update_list_file (char *filename, char *section, char *key, char *line)
854 {
855   FILE *ifp;
856   FILE *ofp;
857   char buf[HUGE_STRING];
858   char oldfile[_POSIX_PATH_MAX];
859   char *c;
860   int ext = 0, done = 0, r = 0;
861
862   snprintf (oldfile, sizeof(oldfile), "%s.bak", filename);
863   dprint (1, (debugfile, "Renaming %s to %s\n", filename, oldfile));
864
865   /* if file not exist, create it */
866   if ((ifp = safe_fopen (filename, "a")))
867     fclose (ifp);
868   if (_mutt_rename_file (filename, oldfile, 1))
869   {
870     mutt_perror _("Unable to create backup file");
871     return (-1);
872   }
873   dprint (1, (debugfile, "Opening %s\n", oldfile));
874   if (!(ifp = safe_fopen (oldfile, "r")))
875   {
876     mutt_perror _("Unable to open backup file for reading");
877     return (-1);
878   }
879   dprint (1, (debugfile, "Opening %s\n", filename));
880   if (!(ofp = safe_fopen (filename, "w")))
881   {
882     fclose (ifp);
883     mutt_perror _("Unable to open new file for writing");
884     return (-1);
885   }
886   if (mx_lock_file (filename, fileno (ofp), 1, 0, 1))
887   {
888     fclose (ofp);
889     fclose (ifp);
890     mutt_error (_("Unable to lock %s, old file saved as %s"), filename, oldfile);
891     return (-1);
892   }
893
894   if (section)
895   {
896     while (r != EOF && !done && fgets (buf, sizeof (buf), ifp))
897     {
898       r = fputs (buf, ofp);
899       c = buf;
900       while (*c && *c != '\n') c++;
901       c[0] = 0; /* strip EOL */
902       if (!strncmp (buf, "#: ", 3) && !mutt_strcasecmp (buf+3, section))
903         done++;
904     }
905     if (r != EOF && !done)
906     {
907       snprintf (buf, sizeof(buf), "#: %s\n", section);
908       r = fputs (buf, ofp);
909     }
910     done = 0;
911   }
912
913   while (r != EOF && fgets (buf, sizeof (buf), ifp))
914   {
915     if (ext)
916     {
917       c = buf;
918       while (*c && (*c != '\r') && (*c != '\n')) c++;
919       c--;
920       if (*c != '\\') ext = 0;
921     }
922     else if ((section && !strncmp (buf, "#: ", 3)))
923     {
924       if (!done && line)
925       {
926         fputs (line, ofp);
927         fputc ('\n', ofp);
928       }
929       r = fputs (buf, ofp);
930       done++;
931       break;
932     }
933     else if (key && !strncmp (buf, key, strlen(key)) &&
934             (!*key || buf[strlen(key)] == ' '))
935     {
936       c = buf;
937       ext = 0;
938       while (*c && (*c != '\r') && (*c != '\n')) c++;
939       c--;
940       if (*c == '\\') ext = 1;
941       if (!done && line)
942       {
943         r = fputs (line, ofp);
944         if (*key)
945           r = fputc ('\n', ofp);
946         done++;
947       }
948     }
949     else
950     {
951       r = fputs (buf, ofp);
952     }
953   }
954
955   while (r != EOF && fgets (buf, sizeof (buf), ifp))
956     r = fputs (buf, ofp);
957
958   /* If there wasn't a line to replace, put it on the end of the file */
959   if (r != EOF && !done && line)
960   {
961     fputs (line, ofp);
962     r = fputc ('\n', ofp);
963   }
964   mx_unlock_file (filename, fileno (ofp), 0);
965   fclose (ofp);
966   fclose (ifp);
967   if (r != EOF)
968   {
969     unlink (oldfile);
970     return 0;
971   }
972   unlink (filename);
973   mutt_error (_("Cannot write new %s, old file saved as %s"), filename,
974         oldfile);
975   return (-1);
976 }
977
978
979 void mutt_version (void)
980 {
981   mutt_message ("Mutt %s (%s)", MUTT_VERSION, ReleaseDate);
982 }
983
984 void mutt_edit_content_type (HEADER *h, BODY *b, FILE *fp)
985 {
986   char buf[LONG_STRING];
987   char obuf[LONG_STRING];
988   char tmp[STRING];
989   PARAMETER *p;
990
991   char charset[STRING];
992   char *cp;
993
994   short charset_changed = 0;
995   short type_changed = 0;
996   
997   cp = mutt_get_parameter ("charset", b->parameter);
998   strfcpy (charset, NONULL (cp), sizeof (charset));
999
1000   snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
1001   strfcpy (obuf, buf, sizeof (obuf));
1002   if (b->parameter)
1003   {
1004     size_t l;
1005     
1006     for (p = b->parameter; p; p = p->next)
1007     {
1008       l = strlen (buf);
1009
1010       rfc822_cat (tmp, sizeof (tmp), p->value, MimeSpecials);
1011       snprintf (buf + l, sizeof (buf) - l, "; %s=%s", p->attribute, tmp);
1012     }
1013   }
1014   
1015   if (mutt_get_field ("Content-Type: ", buf, sizeof (buf), 0) != 0 ||
1016       buf[0] == 0)
1017     return;
1018   
1019   /* clean up previous junk */
1020   mutt_free_parameter (&b->parameter);
1021   FREE (&b->subtype);
1022   
1023   mutt_parse_content_type (buf, b);
1024
1025   
1026   snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
1027   type_changed = ascii_strcasecmp (tmp, obuf);
1028   charset_changed = ascii_strcasecmp (charset, mutt_get_parameter ("charset", b->parameter));
1029
1030   /* if in send mode, check for conversion - current setting is default. */
1031
1032   if (!h && b->type == TYPETEXT && charset_changed)
1033   {
1034     int r;
1035     snprintf (tmp, sizeof (tmp), _("Convert to %s upon sending?"),
1036               mutt_get_parameter ("charset", b->parameter));
1037     if ((r = mutt_yesorno (tmp, !b->noconv)) != -1)
1038       b->noconv = (r == M_NO);
1039   }
1040
1041   /* inform the user */
1042   
1043   snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
1044   if (type_changed)
1045     mutt_message (_("Content-Type changed to %s."), tmp);
1046   if (b->type == TYPETEXT && charset_changed)
1047   {
1048     if (type_changed)
1049       mutt_sleep (1);
1050     mutt_message (_("Character set changed to %s; %s."),
1051                   mutt_get_parameter ("charset", b->parameter),
1052                   b->noconv ? _("not converting") : _("converting"));
1053   }
1054
1055   b->force_charset |= charset_changed ? 1 : 0;
1056
1057   if (!is_multipart (b) && b->parts)
1058     mutt_free_body (&b->parts);
1059   if (!mutt_is_message_type (b->type, b->subtype) && b->hdr)
1060   {
1061     b->hdr->content = NULL;
1062     mutt_free_header (&b->hdr);
1063   }
1064
1065   if (fp && (is_multipart (b) || mutt_is_message_type (b->type, b->subtype)))
1066     mutt_parse_part (fp, b);
1067   
1068   if (WithCrypto && h)
1069   {
1070     if (h->content == b)
1071       h->security  = 0;
1072
1073     h->security |= crypt_query (b);
1074   }
1075 }
1076
1077
1078 static int _mutt_check_traditional_pgp (HEADER *h, int *redraw)
1079 {
1080   MESSAGE *msg;
1081   int rv = 0;
1082   
1083   h->security |= PGP_TRADITIONAL_CHECKED;
1084   
1085   mutt_parse_mime_message (Context, h);
1086   if ((msg = mx_open_message (Context, h->msgno)) == NULL)
1087     return 0;
1088   if (crypt_pgp_check_traditional (msg->fp, h->content, 0))
1089   {
1090     h->security = crypt_query (h->content);
1091     *redraw |= REDRAW_FULL;
1092     rv = 1;
1093   }
1094   
1095   mx_close_message (&msg);
1096   return rv;
1097 }
1098
1099 int mutt_check_traditional_pgp (HEADER *h, int *redraw)
1100 {
1101   int i;
1102   int rv = 0;
1103   if (h && !(h->security & PGP_TRADITIONAL_CHECKED))
1104     rv = _mutt_check_traditional_pgp (h, redraw);
1105   else
1106   {
1107     for (i = 0; i < Context->vcount; i++)
1108       if (Context->hdrs[Context->v2r[i]]->tagged && 
1109           !(Context->hdrs[Context->v2r[i]]->security & PGP_TRADITIONAL_CHECKED))
1110         rv = _mutt_check_traditional_pgp (Context->hdrs[Context->v2r[i]], redraw)
1111           || rv;
1112   }
1113   return rv;
1114 }
1115
1116