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