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.
11 #include <lib-lib/lib-lib.h>
14 #include <lib-mime/mime.h>
16 #include <lib-sys/exit.h>
17 #include <lib-sys/unix.h>
19 #include <lib-lua/lib-lua.h>
20 #include <lib-ui/curses.h>
21 #include <lib-ui/enter.h>
22 #include <lib-ui/menu.h>
23 #include <lib-mx/mx.h>
27 #include "recvattach.h"
31 #include <lib-crypt/crypt.h>
32 #include "mutt_idna.h"
33 #include <sys/types.h>
37 #include <imap/imap.h>
39 /* The folder the user last saved to. Used by ci_save_message() */
40 static char LastSaveFolder[_POSIX_PATH_MAX] = "";
42 int mutt_display_message (HEADER * cur)
44 char tempfile[_POSIX_PATH_MAX], buf[LONG_STRING];
45 int rc = 0, builtin = 0;
46 int cmflags = M_CM_DECODE | M_CM_DISPLAY | M_CM_CHARCONV;
48 FILE *fpfilterout = NULL;
53 snprintf (buf, sizeof (buf), "%s/%s", TYPE (cur->content),
54 cur->content->subtype);
56 mutt_parse_mime_message (Context, cur);
57 mutt_message_hook (Context, cur, M_MESSAGEHOOK);
59 fpout = m_tempfile(tempfile, sizeof(tempfile), NONULL(MCore.tmpdir), NULL);
61 mutt_error _("Could not create temporary file!");
65 if (DisplayFilter && *DisplayFilter) {
68 /* mutt_endwin (NULL); */
69 filterpid = mutt_create_filter_fd (DisplayFilter, &fpout, NULL, NULL,
70 -1, fileno (fpfilterout), -1);
72 mutt_error (_("Cannot create display filter"));
73 m_fclose(&fpfilterout);
79 if (!Pager || m_strcmp(Pager, "builtin") == 0)
82 mutt_make_string (buf, sizeof (buf), NONULL (PagerFmt), Context, cur);
84 fputs ("\n\n", fpout);
87 msg = mx_open_message (Context, cur->msgno);
88 if (msg == NULL) res = -1;
91 /* see if crytpo is needed for this message. if so, we should exit curses */
93 if (cur->security & ENCRYPT) {
94 if (cur->security & APPLICATION_SMIME)
95 crypt_smime_getkeys (cur->env);
96 if (!crypt_valid_passphrase (cur->security))
99 cmflags |= M_CM_VERIFY;
101 else if (cur->security & SIGN) {
102 /* find out whether or not the verify signature */
103 if (query_quadoption (OPT_VERIFYSIG, _("Verify PGP signature?")) ==
105 cmflags |= M_CM_VERIFY;
110 if (cmflags & M_CM_VERIFY || cur->security & ENCRYPT) {
111 if (cur->security & APPLICATION_PGP) {
113 crypt_pgp_invoke_getkeys (cur->env->from);
115 crypt_invoke_message (APPLICATION_PGP);
118 if (cur->security & APPLICATION_SMIME)
119 crypt_invoke_message (APPLICATION_SMIME);
122 res = _mutt_copy_message (fpout, msg->fp, cur, cur->content, cmflags,
123 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
124 CH_DECODE | CH_FROM);
125 if (res == 0 && (ferror(fpout) || feof(fpout))) {
129 mx_close_message (&msg);
132 if ((m_fclose(&fpout) != 0 && errno != EPIPE) || res == -1) {
133 mutt_error (_("Could not copy message"));
134 if (fpfilterout != NULL) {
135 mutt_wait_filter (filterpid);
136 m_fclose(&fpfilterout);
140 if (fpfilterout != NULL && mutt_wait_filter (filterpid) != 0)
141 mutt_any_key_to_continue (NULL);
143 m_fclose(&fpfilterout); /* XXX - check result? */
146 /* update crypto information for this message */
147 cur->security &= ~(GOODSIGN|BADSIGN);
148 cur->security |= crypt_query (cur->content);
150 /* Remove color cache for this message, in case there
151 are color patterns for both ~g and ~V */
157 if ((cur->security & APPLICATION_SMIME) && (cmflags & M_CM_VERIFY)) {
158 if (cur->security & GOODSIGN) {
159 if (!crypt_smime_verify_sender (cur))
160 mutt_message (_("S/MIME signature successfully verified."));
162 mutt_error (_("S/MIME certificate owner does not match sender."));
164 else if (cur->security & PARTSIGN)
166 ("Warning: Part of this message has not been signed."));
167 else if (cur->security & SIGN || cur->security & BADSIGN)
168 mutt_error (_("S/MIME signature could NOT be verified."));
171 if ((cur->security & APPLICATION_PGP) && (cmflags & M_CM_VERIFY)) {
172 if (cur->security & GOODSIGN)
173 mutt_message (_("PGP signature successfully verified."));
174 else if (cur->security & PARTSIGN)
176 ("Warning: Part of this message has not been signed."));
177 else if (cur->security & SIGN)
178 mutt_message (_("PGP signature could NOT be verified."));
181 /* Invoke the builtin pager */
185 rc = mutt_pager (NULL, tempfile, M_PAGER_MESSAGE, &info);
191 snprintf (buf, sizeof (buf), "%s %s", NONULL (Pager), tempfile);
192 if ((r = mutt_system (buf)) == -1)
193 mutt_error (_("Error running \"%s\"!"), buf);
195 keypad (stdscr, TRUE);
197 mutt_set_flag (Context, cur, M_READ, 1);
198 if (r != -1 && option (OPTPROMPTAFTER)) {
199 mutt_ungetch (mutt_any_key_to_continue _("Command: "), 0);
200 rc = km_dokey (MENU_PAGER);
209 void ci_bounce_message (HEADER * h, int *redraw)
212 char buf[HUGE_STRING] = { 0 };
213 address_t *adr = NULL;
218 m_strcpy(prompt, sizeof(prompt), _("Bounce message to: "));
220 m_strcpy(prompt, sizeof(prompt), _("Bounce tagged messages to: "));
222 rc = mutt_get_field (prompt, buf, sizeof (buf), M_ALIAS);
224 if (option (OPTNEEDREDRAW)) {
225 unset_option (OPTNEEDREDRAW);
226 *redraw = REDRAW_FULL;
232 if (!(adr = rfc822_parse_adrlist (adr, buf))) {
233 mutt_error _("Error parsing address!");
238 adr = mutt_expand_aliases (adr);
240 if (mutt_addrlist_to_idna (adr, &err) < 0) {
241 mutt_error (_("Bad IDN: '%s'"), err);
243 address_list_wipe(&adr);
248 rfc822_addrcat(buf, sizeof (buf), adr, 1);
250 #define extra_space (15 + 7 + 2)
251 snprintf (prompt, sizeof (prompt),
252 (h ? _("Bounce message to %s") : _("Bounce messages to %s")),
255 if (m_strwidth(prompt) > COLS - extra_space) {
256 mutt_format_string(prompt, sizeof(prompt), 0, COLS - extra_space, 0, 0,
257 prompt, sizeof(prompt), 0);
258 m_strcat(prompt, sizeof(prompt), "...?");
260 m_strcat(prompt, sizeof(prompt), "?");
263 if (query_quadoption (OPT_BOUNCE, prompt) != M_YES) {
264 address_list_wipe(&adr);
265 CLEARLINE (LINES - 1);
266 mutt_message (h ? _("Message not bounced.") : _("Messages not bounced."));
270 CLEARLINE (LINES - 1);
272 rc = mutt_bounce_message (NULL, h, adr);
273 address_list_wipe(&adr);
274 /* If no error, or background, display message. */
275 if ((rc == 0) || (rc == S_BKG))
276 mutt_message (h ? _("Message bounced.") : _("Messages bounced."));
279 static void pipe_set_flags (int decode, int print, int *cmflags, int *chflags)
282 *cmflags |= M_CM_DECODE | M_CM_CHARCONV;
283 *chflags |= CH_DECODE | CH_REORDER;
285 if (option (OPTWEED)) {
287 *cmflags |= M_CM_WEED;
292 *cmflags |= M_CM_PRINTING;
296 static void pipe_msg (HEADER * h, FILE * fp, int decode, int print)
299 int chflags = CH_FROM;
301 pipe_set_flags (decode, print, &cmflags, &chflags);
303 if (decode && h->security & ENCRYPT) {
304 if (!crypt_valid_passphrase (h->security))
310 mutt_parse_mime_message (Context, h);
312 mutt_copy_message (fp, Context, h, cmflags, chflags);
316 /* the following code is shared between printing and piping */
318 static int _mutt_pipe_message(HEADER * h, char *cmd, int decode, int print,
319 int split, const char *sep)
326 /* mutt_endwin (NULL);
328 is this really needed here ?
329 it makes the screen flicker on pgp and s/mime messages,
330 before asking for a passphrase...
334 mutt_message_hook (Context, h, M_MESSAGEHOOK);
337 mutt_parse_mime_message (Context, h);
338 if (h->security & ENCRYPT && !crypt_valid_passphrase (h->security))
343 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
344 mutt_perror (_("Can't create filter process"));
349 pipe_msg (h, fpout, decode, print);
351 rc = mutt_wait_filter (thepid);
353 else { /* handle tagged messages */
356 for (i = 0; i < Context->vcount; i++)
357 if (Context->hdrs[Context->v2r[i]]->tagged) {
358 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
360 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
361 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT &&
362 !crypt_valid_passphrase (Context->hdrs[Context->v2r[i]]->
369 for (i = 0; i < Context->vcount; i++) {
370 if (Context->hdrs[Context->v2r[i]]->tagged) {
371 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
374 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
375 mutt_perror (_("Can't create filter process"));
379 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
380 /* add the message separator */
384 if (mutt_wait_filter (thepid) != 0)
391 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
392 mutt_perror (_("Can't create filter process"));
396 for (i = 0; i < Context->vcount; i++) {
397 if (Context->hdrs[Context->v2r[i]]->tagged) {
398 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
400 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
401 /* add the message separator */
407 if (mutt_wait_filter (thepid) != 0)
412 if (rc || option (OPTWAITKEY))
413 mutt_any_key_to_continue (NULL);
417 void mutt_pipe_message (HEADER * h)
419 char buffer[LONG_STRING];
422 if (mutt_get_field (_("Pipe to command: "), buffer, sizeof (buffer), M_CMD)
426 mutt_expand_path (buffer, sizeof (buffer));
427 _mutt_pipe_message (h, buffer,
428 option (OPTPIPEDECODE),
429 0, option (OPTPIPESPLIT), PipeSep);
432 void mutt_print_message (HEADER * h)
435 if (quadoption (OPT_PRINT) && m_strisempty(PrintCmd)) {
436 mutt_message (_("No printing command has been defined."));
440 if (query_quadoption (OPT_PRINT,
441 h ? _("Print message?") : _("Print tagged messages?"))
445 if (_mutt_pipe_message (h, PrintCmd,
446 option (OPTPRINTDECODE),
447 1, option (OPTPRINTSPLIT), "\f") == 0)
448 mutt_message (h ? _("Message printed") : _("Messages printed"));
450 mutt_message (h ? _("Message could not be printed") :
451 _("Messages could not be printed"));
455 int mutt_select_sort (int reverse)
457 int method = Sort; /* save the current method in case of abort */
459 switch (mutt_multi_choice (reverse ?
461 ("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?: ")
464 ("Sort (d)ate/(f)rm/(r)ecv/(s)ubj/t(o)/(t)hread/(u)nsort/si(z)e/s(c)ore/s(p)am?: "),
466 case -1: /* abort - don't resort */
478 Sort = SORT_RECEIVED;
489 case 6: /* (t)hread */
493 case 7: /* (u)nsort */
501 case 9: /* s(c)ore */
505 case 10: /* s(p)am */
510 Sort |= SORT_REVERSE;
512 return (Sort != method ? 0 : -1); /* no need to resort if it's the same */
515 /* invoke a command in a subshell */
516 void mutt_shell_escape (void)
518 char buf[LONG_STRING];
521 if (mutt_get_field (_("Shell command: "), buf, sizeof (buf), M_CMD) == 0) {
523 m_strcpy(buf, sizeof(buf), MCore.shell);
525 CLEARLINE (LINES - 1);
528 if (mutt_system (buf) != 0 || option (OPTWAITKEY))
529 mutt_any_key_to_continue (NULL);
534 /* enter a mutt command */
535 void mutt_enter_command (void)
538 char buffer[LONG_STRING], errbuf[STRING];
542 if (mutt_get_field (":", buffer, sizeof (buffer), M_COMMAND) != 0
546 err.dsize = sizeof (errbuf);
548 r = mutt_parse_rc_line (buffer, &token, &err);
549 p_delete(&token.data);
551 /* since errbuf could potentially contain printf() sequences in it,
552 we must call mutt_error() in this fashion so that vsprintf()
553 doesn't expect more arguments that we passed */
555 mutt_message ("%s", errbuf);
557 mutt_error ("%s", errbuf);
561 void mutt_display_address (ENVELOPE * env)
563 const char *pfx = NULL;
565 address_t *adr = NULL;
567 adr = mutt_get_address(env, &pfx);
573 * Note: We don't convert IDNA to local representation this time.
574 * That is intentional, so the user has an opportunity to copy &
575 * paste the on-the-wire form of the address to other, IDN-unable
580 rfc822_addrcat(buf, sizeof (buf), adr, 0);
581 mutt_message ("%s: %s", pfx, buf);
584 static void set_copy_flags (HEADER * hdr, int decode, int decrypt,
585 int *cmflags, int *chflags)
588 *chflags = CH_UPDATE_LEN;
590 if (!decode && decrypt && (hdr->security & ENCRYPT)) {
591 if (mutt_is_multipart_encrypted (hdr->content)) {
592 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
593 *cmflags = M_CM_DECODE_PGP;
595 else if (mutt_is_application_pgp (hdr->content) & ENCRYPT)
597 else if (mutt_is_application_smime (hdr->content) & ENCRYPT) {
598 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
599 *cmflags = M_CM_DECODE_SMIME;
604 *chflags = CH_XMIT | CH_MIME | CH_TXTPLAIN;
605 *cmflags = M_CM_DECODE | M_CM_CHARCONV;
607 if (!decrypt) { /* If decode doesn't kick in for decrypt, */
608 *chflags |= CH_DECODE; /* then decode RFC 2047 headers, */
610 if (option (OPTWEED)) {
611 *chflags |= CH_WEED; /* and respect $weed. */
612 *cmflags |= M_CM_WEED;
618 int _mutt_save_message (HEADER * h, CONTEXT * ctx, int delete, int decode,
620 int cmflags, chflags;
623 set_copy_flags (h, decode, decrypt, &cmflags, &chflags);
625 if (decode || decrypt)
626 mutt_parse_mime_message (Context, h);
628 if ((rc = mutt_append_message (ctx, Context, h, cmflags, chflags)) != 0)
632 mutt_set_flag (Context, h, M_DELETE, 1);
633 if (option (OPTDELETEUNTAG))
634 mutt_set_flag (Context, h, M_TAG, 0);
635 mutt_set_flag (Context, h, M_APPENDED, 1);
640 /* returns 0 if the copy/save was successful, or -1 on error/abort */
641 int mutt_save_message (HEADER * h, int delete,
642 int decode, int decrypt, int *redraw) {
643 int i, need_buffy_cleanup;
644 int need_passphrase = 0, app = 0;
645 char prompt[STRING], buf[_POSIX_PATH_MAX];
653 snprintf (prompt, sizeof (prompt),
654 decode ? (delete ? _("Decode-save%s to mailbox") :
655 _("Decode-copy%s to mailbox")) :
656 (decrypt ? (delete ? _("Decrypt-save%s to mailbox") :
657 _("Decrypt-copy%s to mailbox")) :
658 (delete ? _("Save%s to mailbox") : _("Copy%s to mailbox"))),
659 h ? "" : _(" tagged"));
663 need_passphrase = h->security & ENCRYPT;
665 mutt_message_hook (Context, h, M_MESSAGEHOOK);
666 mutt_default_save (buf, sizeof (buf), h);
669 /* look for the first tagged message */
671 for (i = 0; i < Context->vcount; i++) {
672 if (Context->hdrs[Context->v2r[i]]->tagged) {
673 h = Context->hdrs[Context->v2r[i]];
680 mutt_message_hook (Context, h, M_MESSAGEHOOK);
681 mutt_default_save (buf, sizeof (buf), h);
682 need_passphrase = h->security & ENCRYPT;
688 mutt_pretty_mailbox (buf);
689 if (mutt_enter_fname (prompt, buf, sizeof (buf), redraw, 0) == -1)
692 if (*redraw != REDRAW_FULL) {
694 *redraw = REDRAW_INDEX | REDRAW_STATUS;
696 *redraw = REDRAW_STATUS;
702 /* This is an undocumented feature of ELM pointed out to me by Felix von
703 * Leitner <leitner@prz.fu-berlin.de>
705 if (m_strcmp(buf, ".") == 0)
706 m_strcpy(buf, sizeof(buf), LastSaveFolder);
708 m_strcpy(LastSaveFolder, sizeof(LastSaveFolder), buf);
710 mutt_expand_path (buf, sizeof (buf));
712 /* check to make sure that this file is really the one the user wants */
713 if (mutt_save_confirm (buf, &st) != 0)
716 if (need_passphrase && (decode || decrypt)
717 && !crypt_valid_passphrase (app))
720 mutt_message (_("Copying to %s..."), buf);
722 if (Context->magic == M_IMAP && !(decode || decrypt) && mx_get_magic (buf) == M_IMAP) {
723 switch (imap_copy_messages (Context, h, buf, delete)) {
728 /* non-fatal error: fall through to fetch/append */
731 /* fatal error, abort */
737 if (mx_open_mailbox (buf, M_APPEND, &ctx) != NULL) {
739 if (_mutt_save_message (h, &ctx, delete, decode, decrypt) != 0) {
740 mx_close_mailbox (&ctx, NULL);
744 for (i = 0; i < Context->vcount; i++) {
745 if (Context->hdrs[Context->v2r[i]]->tagged) {
746 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
748 if (_mutt_save_message (Context->hdrs[Context->v2r[i]], &ctx, delete,
749 decode, decrypt) != 0) {
750 mx_close_mailbox (&ctx, NULL);
757 need_buffy_cleanup = (ctx.magic == M_MBOX || ctx.magic == M_MMDF);
759 mx_close_mailbox (&ctx, NULL);
761 if (need_buffy_cleanup) {
762 /* fix up the times so buffy won't get confused */
763 if (st.st_mtime > st.st_atime) {
764 ut.actime = st.st_atime;
765 ut.modtime = time (NULL);
779 void mutt_version (void)
781 mutt_message (mutt_make_version());
784 void mutt_edit_content_type (HEADER * h, BODY * b, FILE * fp)
786 char buf[LONG_STRING];
787 char obuf[LONG_STRING];
790 char charset[STRING];
793 short charset_changed = 0;
794 short type_changed = 0;
796 cp = parameter_getval(b->parameter, "charset");
797 m_strcpy(charset, sizeof(charset), NONULL(cp));
799 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
800 m_strcpy(obuf, sizeof(obuf), buf);
805 for (p = b->parameter; p; p = p->next) {
808 rfc822_strcpy(tmp, sizeof(tmp), p->value, MimeSpecials);
809 snprintf (buf + l, sizeof (buf) - l, "; %s=%s", p->attribute, tmp);
813 if (mutt_get_field ("Content-Type: ", buf, sizeof (buf), 0) != 0 ||
817 /* clean up previous junk */
818 parameter_list_wipe(&b->parameter);
819 p_delete(&b->subtype);
821 mutt_parse_content_type (buf, b);
824 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
825 type_changed = ascii_strcasecmp (tmp, obuf);
827 ascii_strcasecmp (charset, parameter_getval(b->parameter, "charset"));
829 /* if in send mode, check for conversion - current setting is default. */
831 if (!h && b->type == TYPETEXT && charset_changed) {
834 snprintf (tmp, sizeof (tmp), _("Convert to %s upon sending?"),
835 parameter_getval(b->parameter, "charset"));
836 if ((r = mutt_yesorno (tmp, !b->noconv)) != -1)
837 b->noconv = (r == M_NO);
840 /* inform the user */
842 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
844 mutt_message (_("Content-Type changed to %s."), tmp);
845 if (b->type == TYPETEXT && charset_changed) {
848 mutt_message (_("Character set changed to %s; %s."),
849 parameter_getval(b->parameter, "charset"),
850 b->noconv ? _("not converting") : _("converting"));
853 b->force_charset |= charset_changed ? 1 : 0;
855 if (!is_multipart(b) && b->parts)
856 body_list_wipe(&b->parts);
857 if (!mutt_is_message_type(b) && b->hdr) {
858 b->hdr->content = NULL;
859 header_delete(&b->hdr);
862 if (fp && (is_multipart(b) || mutt_is_message_type(b)))
863 mutt_parse_part (fp, b);
869 h->security |= crypt_query (b);
874 static int _mutt_check_traditional_pgp (HEADER * h, int *redraw)
879 h->security |= PGP_TRADITIONAL_CHECKED;
881 mutt_parse_mime_message (Context, h);
882 if ((msg = mx_open_message (Context, h->msgno)) == NULL)
884 if (crypt_pgp_check_traditional (msg->fp, h->content, 0)) {
885 h->security = crypt_query (h->content);
886 *redraw |= REDRAW_FULL;
890 h->security |= PGP_TRADITIONAL_CHECKED;
891 mx_close_message (&msg);
895 int mutt_check_traditional_pgp (HEADER * h, int *redraw)
900 if (h && !(h->security & PGP_TRADITIONAL_CHECKED))
901 rv = _mutt_check_traditional_pgp (h, redraw);
903 for (i = 0; i < Context->vcount; i++)
904 if (Context->hdrs[Context->v2r[i]]->tagged &&
905 !(Context->hdrs[Context->v2r[i]]->
906 security & PGP_TRADITIONAL_CHECKED))
908 _mutt_check_traditional_pgp (Context->hdrs[Context->v2r[i]], redraw)