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.
21 #include <sys/types.h>
24 #include <lib-lib/mem.h>
25 #include <lib-lib/file.h>
26 #include <lib-lib/macros.h>
27 #include <lib-lib/ascii.h>
29 #include <lib-mime/mime.h>
31 #include <lib-sys/exit.h>
32 #include <lib-sys/unix.h>
34 #include <lib-ui/curses.h>
35 #include <lib-ui/enter.h>
36 #include <lib-ui/menu.h>
40 #include "recvattach.h"
45 #include <lib-crypt/crypt.h>
46 #include "mutt_idna.h"
47 #include <sys/types.h>
51 #include <imap/imap.h>
57 /* The folder the user last saved to. Used by ci_save_message() */
58 static char LastSaveFolder[_POSIX_PATH_MAX] = "";
60 int mutt_display_message (HEADER * cur)
62 char tempfile[_POSIX_PATH_MAX], buf[LONG_STRING];
63 int rc = 0, builtin = 0;
64 int cmflags = M_CM_DECODE | M_CM_DISPLAY | M_CM_CHARCONV;
66 FILE *fpfilterout = NULL;
71 snprintf (buf, sizeof (buf), "%s/%s", TYPE (cur->content),
72 cur->content->subtype);
74 mutt_parse_mime_message (Context, cur);
75 mutt_message_hook (Context, cur, M_MESSAGEHOOK);
77 mutt_mktemp (tempfile);
78 if ((fpout = safe_fopen (tempfile, "w")) == NULL) {
79 mutt_error _("Could not create temporary file!");
84 if (DisplayFilter && *DisplayFilter) {
87 /* mutt_endwin (NULL); */
88 filterpid = mutt_create_filter_fd (DisplayFilter, &fpout, NULL, NULL,
89 -1, fileno (fpfilterout), -1);
91 mutt_error (_("Cannot create display filter"));
92 safe_fclose (&fpfilterout);
98 if (!Pager || m_strcmp(Pager, "builtin") == 0)
101 mutt_make_string (buf, sizeof (buf), NONULL (PagerFmt), Context, cur);
103 fputs ("\n\n", fpout);
106 msg = mx_open_message (Context, cur->msgno);
107 if (msg == NULL) res = -1;
110 /* see if crytpo is needed for this message. if so, we should exit curses */
112 if (cur->security & ENCRYPT) {
113 if (cur->security & APPLICATION_SMIME)
114 crypt_smime_getkeys (cur->env);
115 if (!crypt_valid_passphrase (cur->security))
118 cmflags |= M_CM_VERIFY;
120 else if (cur->security & SIGN) {
121 /* find out whether or not the verify signature */
122 if (query_quadoption (OPT_VERIFYSIG, _("Verify PGP signature?")) ==
124 cmflags |= M_CM_VERIFY;
129 if (cmflags & M_CM_VERIFY || cur->security & ENCRYPT) {
130 if (cur->security & APPLICATION_PGP) {
132 crypt_pgp_invoke_getkeys (cur->env->from);
134 crypt_invoke_message (APPLICATION_PGP);
137 if (cur->security & APPLICATION_SMIME)
138 crypt_invoke_message (APPLICATION_SMIME);
141 res = _mutt_copy_message (fpout, msg->fp, cur, cur->content, cmflags,
142 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
143 CH_DECODE | CH_FROM);
144 if (res == 0 && (ferror(fpout) || feof(fpout))) {
148 mx_close_message (&msg);
151 if ((safe_fclose (&fpout) != 0 && errno != EPIPE) || res == -1) {
152 mutt_error (_("Could not copy message"));
153 if (fpfilterout != NULL) {
154 mutt_wait_filter (filterpid);
155 safe_fclose (&fpfilterout);
158 /* this is maybe just plain wrong but it makes the pager display
159 * what we have; i.e. for the crypto stuff we only get
160 * 'Could not copy message' for invalid passphrases, no PGP output
161 * not nothing; so just display what we have...
164 mutt_unlink (tempfile);
169 if (fpfilterout != NULL && mutt_wait_filter (filterpid) != 0)
170 mutt_any_key_to_continue (NULL);
172 safe_fclose (&fpfilterout); /* XXX - check result? */
175 /* update crypto information for this message */
176 cur->security &= ~(GOODSIGN|BADSIGN);
177 cur->security |= crypt_query (cur->content);
179 /* Remove color cache for this message, in case there
180 are color patterns for both ~g and ~V */
186 if ((cur->security & APPLICATION_SMIME) && (cmflags & M_CM_VERIFY)) {
187 if (cur->security & GOODSIGN) {
188 if (!crypt_smime_verify_sender (cur))
189 mutt_message (_("S/MIME signature successfully verified."));
191 mutt_error (_("S/MIME certificate owner does not match sender."));
193 else if (cur->security & PARTSIGN)
195 ("Warning: Part of this message has not been signed."));
196 else if (cur->security & SIGN || cur->security & BADSIGN)
197 mutt_error (_("S/MIME signature could NOT be verified."));
200 if ((cur->security & APPLICATION_PGP) && (cmflags & M_CM_VERIFY)) {
201 if (cur->security & GOODSIGN)
202 mutt_message (_("PGP signature successfully verified."));
203 else if (cur->security & PARTSIGN)
205 ("Warning: Part of this message has not been signed."));
206 else if (cur->security & SIGN)
207 mutt_message (_("PGP signature could NOT be verified."));
210 /* Invoke the builtin pager */
214 rc = mutt_pager (NULL, tempfile, M_PAGER_MESSAGE, &info);
220 snprintf (buf, sizeof (buf), "%s %s", NONULL (Pager), tempfile);
221 if ((r = mutt_system (buf)) == -1)
222 mutt_error (_("Error running \"%s\"!"), buf);
224 keypad (stdscr, TRUE);
226 mutt_set_flag (Context, cur, M_READ, 1);
227 if (r != -1 && option (OPTPROMPTAFTER)) {
228 mutt_ungetch (mutt_any_key_to_continue _("Command: "), 0);
229 rc = km_dokey (MENU_PAGER);
238 void ci_bounce_message (HEADER * h, int *redraw)
240 char prompt[SHORT_STRING];
241 char buf[HUGE_STRING] = { 0 };
242 address_t *adr = NULL;
243 const char *err = NULL;
247 m_strcpy(prompt, sizeof(prompt), _("Bounce message to: "));
249 m_strcpy(prompt, sizeof(prompt), _("Bounce tagged messages to: "));
251 rc = mutt_get_field (prompt, buf, sizeof (buf), M_ALIAS);
253 if (option (OPTNEEDREDRAW)) {
254 unset_option (OPTNEEDREDRAW);
255 *redraw = REDRAW_FULL;
261 if (!(adr = rfc822_parse_adrlist (adr, buf))) {
262 mutt_error _("Error parsing address!");
267 adr = mutt_expand_aliases (adr);
269 if (mutt_addrlist_to_idna (adr, &err) < 0) {
270 mutt_error (_("Bad IDN: '%s'"), err);
272 address_list_wipe(&adr);
277 rfc822_write_address (buf, sizeof (buf), adr, 1);
279 #define extra_space (15 + 7 + 2)
280 snprintf (prompt, sizeof (prompt),
281 (h ? _("Bounce message to %s") : _("Bounce messages to %s")),
284 if (mutt_strwidth (prompt) > COLS - extra_space) {
285 mutt_format_string(prompt, sizeof(prompt), 0, COLS - extra_space, 0, 0,
286 prompt, sizeof(prompt), 0);
287 m_strcat(prompt, sizeof(prompt), "...?");
289 m_strcat(prompt, sizeof(prompt), "?");
292 if (query_quadoption (OPT_BOUNCE, prompt) != M_YES) {
293 address_list_wipe(&adr);
294 CLEARLINE (LINES - 1);
295 mutt_message (h ? _("Message not bounced.") : _("Messages not bounced."));
299 CLEARLINE (LINES - 1);
301 rc = mutt_bounce_message (NULL, h, adr);
302 address_list_wipe(&adr);
303 /* If no error, or background, display message. */
304 if ((rc == 0) || (rc == S_BKG))
305 mutt_message (h ? _("Message bounced.") : _("Messages bounced."));
308 static void pipe_set_flags (int decode, int print, int *cmflags, int *chflags)
311 *cmflags |= M_CM_DECODE | M_CM_CHARCONV;
312 *chflags |= CH_DECODE | CH_REORDER;
314 if (option (OPTWEED)) {
316 *cmflags |= M_CM_WEED;
321 *cmflags |= M_CM_PRINTING;
325 static void pipe_msg (HEADER * h, FILE * fp, int decode, int print)
328 int chflags = CH_FROM;
330 pipe_set_flags (decode, print, &cmflags, &chflags);
332 if (decode && h->security & ENCRYPT) {
333 if (!crypt_valid_passphrase (h->security))
339 mutt_parse_mime_message (Context, h);
341 mutt_copy_message (fp, Context, h, cmflags, chflags);
345 /* the following code is shared between printing and piping */
347 static int _mutt_pipe_message(HEADER * h, char *cmd, int decode, int print,
348 int split, const char *sep)
355 /* mutt_endwin (NULL);
357 is this really needed here ?
358 it makes the screen flicker on pgp and s/mime messages,
359 before asking for a passphrase...
363 mutt_message_hook (Context, h, M_MESSAGEHOOK);
366 mutt_parse_mime_message (Context, h);
367 if (h->security & ENCRYPT && !crypt_valid_passphrase (h->security))
372 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
373 mutt_perror (_("Can't create filter process"));
378 pipe_msg (h, fpout, decode, print);
380 rc = mutt_wait_filter (thepid);
382 else { /* handle tagged messages */
385 for (i = 0; i < Context->vcount; i++)
386 if (Context->hdrs[Context->v2r[i]]->tagged) {
387 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
389 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
390 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT &&
391 !crypt_valid_passphrase (Context->hdrs[Context->v2r[i]]->
398 for (i = 0; i < Context->vcount; i++) {
399 if (Context->hdrs[Context->v2r[i]]->tagged) {
400 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
403 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
404 mutt_perror (_("Can't create filter process"));
408 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
409 /* add the message separator */
412 safe_fclose (&fpout);
413 if (mutt_wait_filter (thepid) != 0)
420 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
421 mutt_perror (_("Can't create filter process"));
425 for (i = 0; i < Context->vcount; i++) {
426 if (Context->hdrs[Context->v2r[i]]->tagged) {
427 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
429 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
430 /* add the message separator */
435 safe_fclose (&fpout);
436 if (mutt_wait_filter (thepid) != 0)
441 if (rc || option (OPTWAITKEY))
442 mutt_any_key_to_continue (NULL);
446 void mutt_pipe_message (HEADER * h)
448 char buffer[LONG_STRING];
451 if (mutt_get_field (_("Pipe to command: "), buffer, sizeof (buffer), M_CMD)
455 mutt_expand_path (buffer, sizeof (buffer));
456 _mutt_pipe_message (h, buffer,
457 option (OPTPIPEDECODE),
458 0, option (OPTPIPESPLIT), PipeSep);
461 void mutt_print_message (HEADER * h)
464 if (quadoption (OPT_PRINT) && (!PrintCmd || !*PrintCmd)) {
465 mutt_message (_("No printing command has been defined."));
469 if (query_quadoption (OPT_PRINT,
470 h ? _("Print message?") : _("Print tagged messages?"))
474 if (_mutt_pipe_message (h, PrintCmd,
475 option (OPTPRINTDECODE),
476 1, option (OPTPRINTSPLIT), "\f") == 0)
477 mutt_message (h ? _("Message printed") : _("Messages printed"));
479 mutt_message (h ? _("Message could not be printed") :
480 _("Messages could not be printed"));
484 int mutt_select_sort (int reverse)
486 int method = Sort; /* save the current method in case of abort */
488 switch (mutt_multi_choice (reverse ?
490 ("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?: ")
493 ("Sort (d)ate/(f)rm/(r)ecv/(s)ubj/t(o)/(t)hread/(u)nsort/si(z)e/s(c)ore/s(p)am?: "),
495 case -1: /* abort - don't resort */
507 Sort = SORT_RECEIVED;
518 case 6: /* (t)hread */
522 case 7: /* (u)nsort */
530 case 9: /* s(c)ore */
534 case 10: /* s(p)am */
539 Sort |= SORT_REVERSE;
541 return (Sort != method ? 0 : -1); /* no need to resort if it's the same */
544 /* invoke a command in a subshell */
545 void mutt_shell_escape (void)
547 char buf[LONG_STRING];
550 if (mutt_get_field (_("Shell command: "), buf, sizeof (buf), M_CMD) == 0) {
551 if (!buf[0] && Shell)
552 m_strcpy(buf, sizeof(buf), Shell);
554 CLEARLINE (LINES - 1);
557 if (mutt_system (buf) != 0 || option (OPTWAITKEY))
558 mutt_any_key_to_continue (NULL);
563 /* enter a mutt command */
564 void mutt_enter_command (void)
567 char buffer[LONG_STRING], errbuf[SHORT_STRING];
571 if (mutt_get_field (":", buffer, sizeof (buffer), M_COMMAND) != 0
575 err.dsize = sizeof (errbuf);
577 r = mutt_parse_rc_line (buffer, &token, &err);
578 p_delete(&token.data);
580 /* since errbuf could potentially contain printf() sequences in it,
581 we must call mutt_error() in this fashion so that vsprintf()
582 doesn't expect more arguments that we passed */
584 mutt_message ("%s", errbuf);
586 mutt_error ("%s", errbuf);
590 void mutt_display_address (ENVELOPE * env)
592 const char *pfx = NULL;
593 char buf[SHORT_STRING];
594 address_t *adr = NULL;
596 adr = mutt_get_address(env, &pfx);
602 * Note: We don't convert IDNA to local representation this time.
603 * That is intentional, so the user has an opportunity to copy &
604 * paste the on-the-wire form of the address to other, IDN-unable
609 rfc822_write_address (buf, sizeof (buf), adr, 0);
610 mutt_message ("%s: %s", pfx, buf);
613 static void set_copy_flags (HEADER * hdr, int decode, int decrypt,
614 int *cmflags, int *chflags)
617 *chflags = CH_UPDATE_LEN;
619 if (!decode && decrypt && (hdr->security & ENCRYPT)) {
620 if (mutt_is_multipart_encrypted (hdr->content)) {
621 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
622 *cmflags = M_CM_DECODE_PGP;
624 else if (mutt_is_application_pgp (hdr->content) & ENCRYPT)
626 else if (mutt_is_application_smime (hdr->content) & ENCRYPT) {
627 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
628 *cmflags = M_CM_DECODE_SMIME;
633 *chflags = CH_XMIT | CH_MIME | CH_TXTPLAIN;
634 *cmflags = M_CM_DECODE | M_CM_CHARCONV;
636 if (!decrypt) { /* If decode doesn't kick in for decrypt, */
637 *chflags |= CH_DECODE; /* then decode RFC 2047 headers, */
639 if (option (OPTWEED)) {
640 *chflags |= CH_WEED; /* and respect $weed. */
641 *cmflags |= M_CM_WEED;
647 int _mutt_save_message (HEADER * h, CONTEXT * ctx, int delete, int decode,
649 int cmflags, chflags;
652 set_copy_flags (h, decode, decrypt, &cmflags, &chflags);
654 if (decode || decrypt)
655 mutt_parse_mime_message (Context, h);
657 if ((rc = mutt_append_message (ctx, Context, h, cmflags, chflags)) != 0)
661 mutt_set_flag (Context, h, M_DELETE, 1);
662 if (option (OPTDELETEUNTAG))
663 mutt_set_flag (Context, h, M_TAG, 0);
664 mutt_set_flag (Context, h, M_APPENDED, 1);
669 /* returns 0 if the copy/save was successful, or -1 on error/abort */
670 int mutt_save_message (HEADER * h, int delete,
671 int decode, int decrypt, int *redraw) {
672 int i, need_buffy_cleanup;
673 int need_passphrase = 0, app = 0;
674 char prompt[SHORT_STRING], buf[_POSIX_PATH_MAX];
687 snprintf (prompt, sizeof (prompt),
688 decode ? (delete ? _("Decode-save%s to mailbox") :
689 _("Decode-copy%s to mailbox")) :
690 (decrypt ? (delete ? _("Decrypt-save%s to mailbox") :
691 _("Decrypt-copy%s to mailbox")) :
692 (delete ? _("Save%s to mailbox") : _("Copy%s to mailbox"))),
693 h ? "" : _(" tagged"));
697 need_passphrase = h->security & ENCRYPT;
699 mutt_message_hook (Context, h, M_MESSAGEHOOK);
700 mutt_default_save (buf, sizeof (buf), h);
703 /* look for the first tagged message */
705 for (i = 0; i < Context->vcount; i++) {
706 if (Context->hdrs[Context->v2r[i]]->tagged) {
707 h = Context->hdrs[Context->v2r[i]];
714 mutt_message_hook (Context, h, M_MESSAGEHOOK);
715 mutt_default_save (buf, sizeof (buf), h);
716 need_passphrase = h->security & ENCRYPT;
722 mutt_pretty_mailbox (buf);
723 if (mutt_enter_fname (prompt, buf, sizeof (buf), redraw, 0) == -1)
726 if (*redraw != REDRAW_FULL) {
728 *redraw = REDRAW_INDEX | REDRAW_STATUS;
730 *redraw = REDRAW_STATUS;
736 /* This is an undocumented feature of ELM pointed out to me by Felix von
737 * Leitner <leitner@prz.fu-berlin.de>
739 if (m_strcmp(buf, ".") == 0)
740 m_strcpy(buf, sizeof(buf), LastSaveFolder);
742 m_strcpy(LastSaveFolder, sizeof(LastSaveFolder), buf);
744 mutt_expand_path (buf, sizeof (buf));
746 /* check to make sure that this file is really the one the user wants */
747 if (mutt_save_confirm (buf, &st) != 0)
750 if (need_passphrase && (decode || decrypt)
751 && !crypt_valid_passphrase (app))
754 mutt_message (_("Copying to %s..."), buf);
756 if (Context->magic == M_IMAP && !(decode || decrypt) && mx_get_magic (buf) == M_IMAP) {
757 switch (imap_copy_messages (Context, h, buf, delete)) {
762 /* non-fatal error: fall through to fetch/append */
765 /* fatal error, abort */
771 if (mx_open_mailbox (buf, M_APPEND, &ctx) != NULL) {
773 if (_mutt_save_message (h, &ctx, delete, decode, decrypt) != 0) {
774 mx_close_mailbox (&ctx, NULL);
778 for (i = 0; i < Context->vcount; i++) {
779 if (Context->hdrs[Context->v2r[i]]->tagged) {
780 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
782 if (_mutt_save_message (Context->hdrs[Context->v2r[i]], &ctx, delete,
783 decode, decrypt) != 0) {
784 mx_close_mailbox (&ctx, NULL);
791 need_buffy_cleanup = (ctx.magic == M_MBOX || ctx.magic == M_MMDF);
793 mx_close_mailbox (&ctx, NULL);
795 if (need_buffy_cleanup) {
797 tmp = buffy_find_mailbox (buf);
798 if (tmp && tmp->new <= 0)
799 buffy_update_mailbox (tmp);
801 /* fix up the times so buffy won't get confused */
802 if (st.st_mtime > st.st_atime) {
803 ut.actime = st.st_atime;
804 ut.modtime = time (NULL);
819 void mutt_version (void)
821 mutt_message (mutt_make_version (1));
824 void mutt_edit_content_type (HEADER * h, BODY * b, FILE * fp)
826 char buf[LONG_STRING];
827 char obuf[LONG_STRING];
831 char charset[STRING];
834 short charset_changed = 0;
835 short type_changed = 0;
837 cp = mutt_get_parameter ("charset", b->parameter);
838 m_strcpy(charset, sizeof(charset), NONULL(cp));
840 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
841 m_strcpy(obuf, sizeof(obuf), buf);
845 for (p = b->parameter; p; p = p->next) {
848 rfc822_strcpy(tmp, sizeof(tmp), p->value, MimeSpecials);
849 snprintf (buf + l, sizeof (buf) - l, "; %s=%s", p->attribute, tmp);
853 if (mutt_get_field ("Content-Type: ", buf, sizeof (buf), 0) != 0 ||
857 /* clean up previous junk */
858 parameter_list_wipe(&b->parameter);
859 p_delete(&b->subtype);
861 mutt_parse_content_type (buf, b);
864 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
865 type_changed = ascii_strcasecmp (tmp, obuf);
867 ascii_strcasecmp (charset, mutt_get_parameter ("charset", b->parameter));
869 /* if in send mode, check for conversion - current setting is default. */
871 if (!h && b->type == TYPETEXT && charset_changed) {
874 snprintf (tmp, sizeof (tmp), _("Convert to %s upon sending?"),
875 mutt_get_parameter ("charset", b->parameter));
876 if ((r = mutt_yesorno (tmp, !b->noconv)) != -1)
877 b->noconv = (r == M_NO);
880 /* inform the user */
882 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
884 mutt_message (_("Content-Type changed to %s."), tmp);
885 if (b->type == TYPETEXT && charset_changed) {
888 mutt_message (_("Character set changed to %s; %s."),
889 mutt_get_parameter ("charset", b->parameter),
890 b->noconv ? _("not converting") : _("converting"));
893 b->force_charset |= charset_changed ? 1 : 0;
895 if (!is_multipart(b) && b->parts)
896 mutt_free_body (&b->parts);
897 if (!mutt_is_message_type (b->type, b->subtype) && b->hdr) {
898 b->hdr->content = NULL;
899 header_delete(&b->hdr);
902 if (fp && (is_multipart(b) || mutt_is_message_type(b->type, b->subtype)))
903 mutt_parse_part (fp, b);
909 h->security |= crypt_query (b);
914 static int _mutt_check_traditional_pgp (HEADER * h, int *redraw)
919 h->security |= PGP_TRADITIONAL_CHECKED;
921 mutt_parse_mime_message (Context, h);
922 if ((msg = mx_open_message (Context, h->msgno)) == NULL)
924 if (crypt_pgp_check_traditional (msg->fp, h->content, 0)) {
925 h->security = crypt_query (h->content);
926 *redraw |= REDRAW_FULL;
930 h->security |= PGP_TRADITIONAL_CHECKED;
931 mx_close_message (&msg);
935 int mutt_check_traditional_pgp (HEADER * h, int *redraw)
940 if (h && !(h->security & PGP_TRADITIONAL_CHECKED))
941 rv = _mutt_check_traditional_pgp (h, redraw);
943 for (i = 0; i < Context->vcount; i++)
944 if (Context->hdrs[Context->v2r[i]]->tagged &&
945 !(Context->hdrs[Context->v2r[i]]->
946 security & PGP_TRADITIONAL_CHECKED))
948 _mutt_check_traditional_pgp (Context->hdrs[Context->v2r[i]], redraw)