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>
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.
15 #include <lib-lib/mem.h>
17 #include <lib-lib/macros.h>
18 #include <lib-lib/ascii.h>
22 #include "recvattach.h"
23 #include "mutt_curses.h"
24 #include "mutt_menu.h"
30 #include "mutt_crypt.h"
31 #include "mutt_idna.h"
33 #include <sys/types.h>
45 #include "lib/debug.h"
53 #include <sys/types.h>
56 extern char *ReleaseDate;
58 /* The folder the user last saved to. Used by ci_save_message() */
59 static char LastSaveFolder[_POSIX_PATH_MAX] = "";
61 int mutt_display_message (HEADER * cur)
63 char tempfile[_POSIX_PATH_MAX], buf[LONG_STRING];
64 int rc = 0, builtin = 0;
65 int cmflags = M_CM_DECODE | M_CM_DISPLAY | M_CM_CHARCONV;
67 FILE *fpfilterout = NULL;
72 snprintf (buf, sizeof (buf), "%s/%s", TYPE (cur->content),
73 cur->content->subtype);
75 mutt_parse_mime_message (Context, cur);
76 mutt_message_hook (Context, cur, M_MESSAGEHOOK);
78 mutt_mktemp (tempfile);
79 if ((fpout = safe_fopen (tempfile, "w")) == NULL) {
80 mutt_error _("Could not create temporary file!");
85 if (DisplayFilter && *DisplayFilter) {
88 /* mutt_endwin (NULL); */
89 filterpid = mutt_create_filter_fd (DisplayFilter, &fpout, NULL, NULL,
90 -1, fileno (fpfilterout), -1);
92 mutt_error (_("Cannot create display filter"));
93 safe_fclose (&fpfilterout);
99 if (!Pager || str_cmp (Pager, "builtin") == 0)
102 mutt_make_string (buf, sizeof (buf), NONULL (PagerFmt), Context, cur);
104 fputs ("\n\n", fpout);
107 msg = mx_open_message (Context, cur->msgno);
108 if (msg == NULL) res = -1;
111 /* see if crytpo is needed for this message. if so, we should exit curses */
112 if (WithCrypto && cur->security) {
113 if (cur->security & ENCRYPT) {
114 if (cur->security & APPLICATION_SMIME)
115 crypt_smime_getkeys (cur->env);
116 if (!crypt_valid_passphrase (cur->security))
119 cmflags |= M_CM_VERIFY;
121 else if (cur->security & SIGN) {
122 /* find out whether or not the verify signature */
123 if (query_quadoption (OPT_VERIFYSIG, _("Verify PGP signature?")) ==
125 cmflags |= M_CM_VERIFY;
130 if (cmflags & M_CM_VERIFY || cur->security & ENCRYPT) {
131 if (cur->security & APPLICATION_PGP) {
133 crypt_pgp_invoke_getkeys (cur->env->from);
135 crypt_invoke_message (APPLICATION_PGP);
138 if (cur->security & APPLICATION_SMIME)
139 crypt_invoke_message (APPLICATION_SMIME);
142 res = _mutt_copy_message (fpout, msg->fp, cur, cur->content, cmflags,
143 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
144 CH_DECODE | CH_FROM);
145 if (res == 0 && (ferror(fpout) || feof(fpout))) {
146 debug_print (1, ("_mutt_copy_message failed to detect EOF!\n"));
150 mx_close_message (&msg);
153 if ((safe_fclose (&fpout) != 0 && errno != EPIPE) || res == -1) {
154 mutt_error (_("Could not copy message"));
155 if (fpfilterout != NULL) {
156 mutt_wait_filter (filterpid);
157 safe_fclose (&fpfilterout);
160 /* this is maybe just plain wrong but it makes the pager display
161 * what we have; i.e. for the crypto stuff we only get
162 * 'Could not copy message' for invalid passphrases, no PGP output
163 * not nothing; so just display what we have...
166 mutt_unlink (tempfile);
171 if (fpfilterout != NULL && mutt_wait_filter (filterpid) != 0)
172 mutt_any_key_to_continue (NULL);
174 safe_fclose (&fpfilterout); /* XXX - check result? */
178 /* update crypto information for this message */
179 cur->security &= ~(GOODSIGN|BADSIGN);
180 cur->security |= crypt_query (cur->content);
182 /* Remove color cache for this message, in case there
183 are color patterns for both ~g and ~V */
191 && (cur->security & APPLICATION_SMIME) && (cmflags & M_CM_VERIFY)) {
192 if (cur->security & GOODSIGN) {
193 if (!crypt_smime_verify_sender (cur))
194 mutt_message (_("S/MIME signature successfully verified."));
196 mutt_error (_("S/MIME certificate owner does not match sender."));
198 else if (cur->security & PARTSIGN)
200 ("Warning: Part of this message has not been signed."));
201 else if (cur->security & SIGN || cur->security & BADSIGN)
202 mutt_error (_("S/MIME signature could NOT be verified."));
206 && (cur->security & APPLICATION_PGP) && (cmflags & M_CM_VERIFY)) {
207 if (cur->security & GOODSIGN)
208 mutt_message (_("PGP signature successfully verified."));
209 else if (cur->security & PARTSIGN)
211 ("Warning: Part of this message has not been signed."));
212 else if (cur->security & SIGN)
213 mutt_message (_("PGP signature could NOT be verified."));
216 /* Invoke the builtin pager */
220 rc = mutt_pager (NULL, tempfile, M_PAGER_MESSAGE, &info);
226 snprintf (buf, sizeof (buf), "%s %s", NONULL (Pager), tempfile);
227 if ((r = mutt_system (buf)) == -1)
228 mutt_error (_("Error running \"%s\"!"), buf);
230 keypad (stdscr, TRUE);
232 mutt_set_flag (Context, cur, M_READ, 1);
233 if (r != -1 && option (OPTPROMPTAFTER)) {
234 mutt_ungetch (mutt_any_key_to_continue _("Command: "), 0);
235 rc = km_dokey (MENU_PAGER);
244 void ci_bounce_message (HEADER * h, int *redraw)
246 char prompt[SHORT_STRING];
247 char buf[HUGE_STRING] = { 0 };
253 strfcpy (prompt, _("Bounce message to: "), sizeof (prompt));
255 strfcpy (prompt, _("Bounce tagged messages to: "), sizeof (prompt));
257 rc = mutt_get_field (prompt, buf, sizeof (buf), M_ALIAS);
259 if (option (OPTNEEDREDRAW)) {
260 unset_option (OPTNEEDREDRAW);
261 *redraw = REDRAW_FULL;
267 if (!(adr = rfc822_parse_adrlist (adr, buf))) {
268 mutt_error _("Error parsing address!");
273 adr = mutt_expand_aliases (adr);
275 if (mutt_addrlist_to_idna (adr, &err) < 0) {
276 mutt_error (_("Bad IDN: '%s'"), err);
278 rfc822_free_address (&adr);
283 rfc822_write_address (buf, sizeof (buf), adr, 1);
285 #define extra_space (15 + 7 + 2)
286 snprintf (prompt, sizeof (prompt),
287 (h ? _("Bounce message to %s") : _("Bounce messages to %s")),
290 if (mutt_strwidth (prompt) > COLS - extra_space) {
291 mutt_format_string (prompt, sizeof (prompt),
292 0, COLS - extra_space, 0, 0,
293 prompt, sizeof (prompt), 0);
294 str_cat (prompt, sizeof (prompt), "...?");
297 str_cat (prompt, sizeof (prompt), "?");
299 if (query_quadoption (OPT_BOUNCE, prompt) != M_YES) {
300 rfc822_free_address (&adr);
301 CLEARLINE (LINES - 1);
302 mutt_message (h ? _("Message not bounced.") : _("Messages not bounced."));
306 CLEARLINE (LINES - 1);
308 rc = mutt_bounce_message (NULL, h, adr);
309 rfc822_free_address (&adr);
310 /* If no error, or background, display message. */
311 if ((rc == 0) || (rc == S_BKG))
312 mutt_message (h ? _("Message bounced.") : _("Messages bounced."));
315 static void pipe_set_flags (int decode, int print, int *cmflags, int *chflags)
318 *cmflags |= M_CM_DECODE | M_CM_CHARCONV;
319 *chflags |= CH_DECODE | CH_REORDER;
321 if (option (OPTWEED)) {
323 *cmflags |= M_CM_WEED;
328 *cmflags |= M_CM_PRINTING;
332 static void pipe_msg (HEADER * h, FILE * fp, int decode, int print)
335 int chflags = CH_FROM;
337 pipe_set_flags (decode, print, &cmflags, &chflags);
339 if (WithCrypto && decode && h->security & ENCRYPT) {
340 if (!crypt_valid_passphrase (h->security))
346 mutt_parse_mime_message (Context, h);
348 mutt_copy_message (fp, Context, h, cmflags, chflags);
352 /* the following code is shared between printing and piping */
354 static int _mutt_pipe_message(HEADER * h, char *cmd, int decode, int print,
355 int split, const char *sep)
362 /* mutt_endwin (NULL);
364 is this really needed here ?
365 it makes the screen flicker on pgp and s/mime messages,
366 before asking for a passphrase...
370 mutt_message_hook (Context, h, M_MESSAGEHOOK);
372 if (WithCrypto && decode) {
373 mutt_parse_mime_message (Context, h);
374 if (h->security & ENCRYPT && !crypt_valid_passphrase (h->security))
379 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
380 mutt_perror (_("Can't create filter process"));
385 pipe_msg (h, fpout, decode, print);
387 rc = mutt_wait_filter (thepid);
389 else { /* handle tagged messages */
391 if (WithCrypto && decode) {
392 for (i = 0; i < Context->vcount; i++)
393 if (Context->hdrs[Context->v2r[i]]->tagged) {
394 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
396 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
397 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT &&
398 !crypt_valid_passphrase (Context->hdrs[Context->v2r[i]]->
405 for (i = 0; i < Context->vcount; i++) {
406 if (Context->hdrs[Context->v2r[i]]->tagged) {
407 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
410 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
411 mutt_perror (_("Can't create filter process"));
415 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
416 /* add the message separator */
419 safe_fclose (&fpout);
420 if (mutt_wait_filter (thepid) != 0)
427 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
428 mutt_perror (_("Can't create filter process"));
432 for (i = 0; i < Context->vcount; i++) {
433 if (Context->hdrs[Context->v2r[i]]->tagged) {
434 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
436 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
437 /* add the message separator */
442 safe_fclose (&fpout);
443 if (mutt_wait_filter (thepid) != 0)
448 if (rc || option (OPTWAITKEY))
449 mutt_any_key_to_continue (NULL);
453 void mutt_pipe_message (HEADER * h)
455 char buffer[LONG_STRING];
458 if (mutt_get_field (_("Pipe to command: "), buffer, sizeof (buffer), M_CMD)
462 mutt_expand_path (buffer, sizeof (buffer));
463 _mutt_pipe_message (h, buffer,
464 option (OPTPIPEDECODE),
465 0, option (OPTPIPESPLIT), PipeSep);
468 void mutt_print_message (HEADER * h)
471 if (quadoption (OPT_PRINT) && (!PrintCmd || !*PrintCmd)) {
472 mutt_message (_("No printing command has been defined."));
476 if (query_quadoption (OPT_PRINT,
477 h ? _("Print message?") : _("Print tagged messages?"))
481 if (_mutt_pipe_message (h, PrintCmd,
482 option (OPTPRINTDECODE),
483 1, option (OPTPRINTSPLIT), "\f") == 0)
484 mutt_message (h ? _("Message printed") : _("Messages printed"));
486 mutt_message (h ? _("Message could not be printed") :
487 _("Messages could not be printed"));
491 int mutt_select_sort (int reverse)
493 int method = Sort; /* save the current method in case of abort */
495 switch (mutt_multi_choice (reverse ?
497 ("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?: ")
500 ("Sort (d)ate/(f)rm/(r)ecv/(s)ubj/t(o)/(t)hread/(u)nsort/si(z)e/s(c)ore/s(p)am?: "),
502 case -1: /* abort - don't resort */
514 Sort = SORT_RECEIVED;
525 case 6: /* (t)hread */
529 case 7: /* (u)nsort */
537 case 9: /* s(c)ore */
541 case 10: /* s(p)am */
546 Sort |= SORT_REVERSE;
548 return (Sort != method ? 0 : -1); /* no need to resort if it's the same */
551 /* invoke a command in a subshell */
552 void mutt_shell_escape (void)
554 char buf[LONG_STRING];
557 if (mutt_get_field (_("Shell command: "), buf, sizeof (buf), M_CMD) == 0) {
558 if (!buf[0] && Shell)
559 strfcpy (buf, Shell, sizeof (buf));
561 CLEARLINE (LINES - 1);
564 if (mutt_system (buf) != 0 || option (OPTWAITKEY))
565 mutt_any_key_to_continue (NULL);
570 /* enter a mutt command */
571 void mutt_enter_command (void)
574 char buffer[LONG_STRING], errbuf[SHORT_STRING];
578 if (mutt_get_field (":", buffer, sizeof (buffer), M_COMMAND) != 0
582 err.dsize = sizeof (errbuf);
584 r = mutt_parse_rc_line (buffer, &token, &err);
585 p_delete(&token.data);
587 /* since errbuf could potentially contain printf() sequences in it,
588 we must call mutt_error() in this fashion so that vsprintf()
589 doesn't expect more arguments that we passed */
591 mutt_message ("%s", errbuf);
593 mutt_error ("%s", errbuf);
597 void mutt_display_address (ENVELOPE * env)
599 const char *pfx = NULL;
600 char buf[SHORT_STRING];
603 adr = mutt_get_address(env, &pfx);
609 * Note: We don't convert IDNA to local representation this time.
610 * That is intentional, so the user has an opportunity to copy &
611 * paste the on-the-wire form of the address to other, IDN-unable
616 rfc822_write_address (buf, sizeof (buf), adr, 0);
617 mutt_message ("%s: %s", pfx, buf);
620 static void set_copy_flags (HEADER * hdr, int decode, int decrypt,
621 int *cmflags, int *chflags)
624 *chflags = CH_UPDATE_LEN;
626 if (WithCrypto && !decode && decrypt && (hdr->security & ENCRYPT)) {
627 if ((WithCrypto & APPLICATION_PGP)
628 && mutt_is_multipart_encrypted (hdr->content)) {
629 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
630 *cmflags = M_CM_DECODE_PGP;
632 else if ((WithCrypto & APPLICATION_PGP)
633 && mutt_is_application_pgp (hdr->content) & ENCRYPT)
635 else if ((WithCrypto & APPLICATION_SMIME)
636 && mutt_is_application_smime (hdr->content) & ENCRYPT) {
637 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
638 *cmflags = M_CM_DECODE_SMIME;
643 *chflags = CH_XMIT | CH_MIME | CH_TXTPLAIN;
644 *cmflags = M_CM_DECODE | M_CM_CHARCONV;
646 if (!decrypt) { /* If decode doesn't kick in for decrypt, */
647 *chflags |= CH_DECODE; /* then decode RFC 2047 headers, */
649 if (option (OPTWEED)) {
650 *chflags |= CH_WEED; /* and respect $weed. */
651 *cmflags |= M_CM_WEED;
657 int _mutt_save_message (HEADER * h, CONTEXT * ctx, int delete, int decode,
659 int cmflags, chflags;
662 set_copy_flags (h, decode, decrypt, &cmflags, &chflags);
664 if (decode || decrypt)
665 mutt_parse_mime_message (Context, h);
667 if ((rc = mutt_append_message (ctx, Context, h, cmflags, chflags)) != 0)
671 mutt_set_flag (Context, h, M_DELETE, 1);
672 if (option (OPTDELETEUNTAG))
673 mutt_set_flag (Context, h, M_TAG, 0);
674 mutt_set_flag (Context, h, M_APPENDED, 1);
679 /* returns 0 if the copy/save was successful, or -1 on error/abort */
680 int mutt_save_message (HEADER * h, int delete,
681 int decode, int decrypt, int *redraw) {
682 int i, need_buffy_cleanup;
683 int need_passphrase = 0, app = 0;
684 char prompt[SHORT_STRING], buf[_POSIX_PATH_MAX];
697 snprintf (prompt, sizeof (prompt),
698 decode ? (delete ? _("Decode-save%s to mailbox") :
699 _("Decode-copy%s to mailbox")) :
700 (decrypt ? (delete ? _("Decrypt-save%s to mailbox") :
701 _("Decrypt-copy%s to mailbox")) :
702 (delete ? _("Save%s to mailbox") : _("Copy%s to mailbox"))),
703 h ? "" : _(" tagged"));
708 need_passphrase = h->security & ENCRYPT;
711 mutt_message_hook (Context, h, M_MESSAGEHOOK);
712 mutt_default_save (buf, sizeof (buf), h);
715 /* look for the first tagged message */
717 for (i = 0; i < Context->vcount; i++) {
718 if (Context->hdrs[Context->v2r[i]]->tagged) {
719 h = Context->hdrs[Context->v2r[i]];
726 mutt_message_hook (Context, h, M_MESSAGEHOOK);
727 mutt_default_save (buf, sizeof (buf), h);
729 need_passphrase = h->security & ENCRYPT;
736 mutt_pretty_mailbox (buf);
737 if (mutt_enter_fname (prompt, buf, sizeof (buf), redraw, 0) == -1)
740 if (*redraw != REDRAW_FULL) {
742 *redraw = REDRAW_INDEX | REDRAW_STATUS;
744 *redraw = REDRAW_STATUS;
750 /* This is an undocumented feature of ELM pointed out to me by Felix von
751 * Leitner <leitner@prz.fu-berlin.de>
753 if (str_cmp (buf, ".") == 0)
754 strfcpy (buf, LastSaveFolder, sizeof (buf));
756 strfcpy (LastSaveFolder, buf, sizeof (LastSaveFolder));
758 mutt_expand_path (buf, sizeof (buf));
760 /* check to make sure that this file is really the one the user wants */
761 if (mutt_save_confirm (buf, &st) != 0)
764 if (WithCrypto && need_passphrase && (decode || decrypt)
765 && !crypt_valid_passphrase (app))
768 mutt_message (_("Copying to %s..."), buf);
771 if (Context->magic == M_IMAP && !(decode || decrypt) && mx_get_magic (buf) == M_IMAP) {
772 switch (imap_copy_messages (Context, h, buf, delete)) {
777 /* non-fatal error: fall through to fetch/append */
780 /* fatal error, abort */
787 if (mx_open_mailbox (buf, M_APPEND, &ctx) != NULL) {
789 if (_mutt_save_message (h, &ctx, delete, decode, decrypt) != 0) {
790 mx_close_mailbox (&ctx, NULL);
794 for (i = 0; i < Context->vcount; i++) {
795 if (Context->hdrs[Context->v2r[i]]->tagged) {
796 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
798 if (_mutt_save_message (Context->hdrs[Context->v2r[i]], &ctx, delete,
799 decode, decrypt) != 0) {
800 mx_close_mailbox (&ctx, NULL);
807 need_buffy_cleanup = (ctx.magic == M_MBOX || ctx.magic == M_MMDF);
809 mx_close_mailbox (&ctx, NULL);
811 if (need_buffy_cleanup) {
813 tmp = buffy_find_mailbox (buf);
814 if (tmp && tmp->new <= 0)
815 buffy_update_mailbox (tmp);
817 /* fix up the times so buffy won't get confused */
818 if (st.st_mtime > st.st_atime) {
819 ut.actime = st.st_atime;
820 ut.modtime = time (NULL);
835 void mutt_version (void)
837 mutt_message (mutt_make_version (1));
840 void mutt_edit_content_type (HEADER * h, BODY * b, FILE * fp)
842 char buf[LONG_STRING];
843 char obuf[LONG_STRING];
847 char charset[STRING];
850 short charset_changed = 0;
851 short type_changed = 0;
853 cp = mutt_get_parameter ("charset", b->parameter);
854 strfcpy (charset, NONULL (cp), sizeof (charset));
856 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
857 strfcpy (obuf, buf, sizeof (obuf));
861 for (p = b->parameter; p; p = p->next) {
864 rfc822_cat (tmp, sizeof (tmp), p->value, MimeSpecials);
865 snprintf (buf + l, sizeof (buf) - l, "; %s=%s", p->attribute, tmp);
869 if (mutt_get_field ("Content-Type: ", buf, sizeof (buf), 0) != 0 ||
873 /* clean up previous junk */
874 mutt_free_parameter (&b->parameter);
875 p_delete(&b->subtype);
877 mutt_parse_content_type (buf, b);
880 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
881 type_changed = ascii_strcasecmp (tmp, obuf);
883 ascii_strcasecmp (charset, mutt_get_parameter ("charset", b->parameter));
885 /* if in send mode, check for conversion - current setting is default. */
887 if (!h && b->type == TYPETEXT && charset_changed) {
890 snprintf (tmp, sizeof (tmp), _("Convert to %s upon sending?"),
891 mutt_get_parameter ("charset", b->parameter));
892 if ((r = mutt_yesorno (tmp, !b->noconv)) != -1)
893 b->noconv = (r == M_NO);
896 /* inform the user */
898 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
900 mutt_message (_("Content-Type changed to %s."), tmp);
901 if (b->type == TYPETEXT && charset_changed) {
904 mutt_message (_("Character set changed to %s; %s."),
905 mutt_get_parameter ("charset", b->parameter),
906 b->noconv ? _("not converting") : _("converting"));
909 b->force_charset |= charset_changed ? 1 : 0;
911 if (!is_multipart (b) && b->parts)
912 mutt_free_body (&b->parts);
913 if (!mutt_is_message_type (b->type, b->subtype) && b->hdr) {
914 b->hdr->content = NULL;
915 mutt_free_header (&b->hdr);
918 if (fp && (is_multipart (b) || mutt_is_message_type (b->type, b->subtype)))
919 mutt_parse_part (fp, b);
921 if (WithCrypto && h) {
925 h->security |= crypt_query (b);
930 static int _mutt_check_traditional_pgp (HEADER * h, int *redraw)
935 h->security |= PGP_TRADITIONAL_CHECKED;
937 mutt_parse_mime_message (Context, h);
938 if ((msg = mx_open_message (Context, h->msgno)) == NULL)
940 if (crypt_pgp_check_traditional (msg->fp, h->content, 0)) {
941 h->security = crypt_query (h->content);
942 *redraw |= REDRAW_FULL;
946 h->security |= PGP_TRADITIONAL_CHECKED;
947 mx_close_message (&msg);
951 int mutt_check_traditional_pgp (HEADER * h, int *redraw)
956 if (h && !(h->security & PGP_TRADITIONAL_CHECKED))
957 rv = _mutt_check_traditional_pgp (h, redraw);
959 for (i = 0; i < Context->vcount; i++)
960 if (Context->hdrs[Context->v2r[i]]->tagged &&
961 !(Context->hdrs[Context->v2r[i]]->
962 security & PGP_TRADITIONAL_CHECKED))
964 _mutt_check_traditional_pgp (Context->hdrs[Context->v2r[i]], redraw)