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