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>
16 #include <lib-lib/file.h>
17 #include <lib-lib/macros.h>
18 #include <lib-lib/ascii.h>
20 #include <lib-mime/mime.h>
24 #include "recvattach.h"
25 #include "mutt_curses.h"
26 #include "mutt_menu.h"
31 #include <lib-crypt/crypt.h>
32 #include "mutt_idna.h"
34 #include <sys/types.h>
38 #include <imap/imap.h>
44 #include "lib/debug.h"
52 #include <sys/types.h>
55 /* The folder the user last saved to. Used by ci_save_message() */
56 static char LastSaveFolder[_POSIX_PATH_MAX] = "";
58 int mutt_display_message (HEADER * cur)
60 char tempfile[_POSIX_PATH_MAX], buf[LONG_STRING];
61 int rc = 0, builtin = 0;
62 int cmflags = M_CM_DECODE | M_CM_DISPLAY | M_CM_CHARCONV;
64 FILE *fpfilterout = NULL;
69 snprintf (buf, sizeof (buf), "%s/%s", TYPE (cur->content),
70 cur->content->subtype);
72 mutt_parse_mime_message (Context, cur);
73 mutt_message_hook (Context, cur, M_MESSAGEHOOK);
75 mutt_mktemp (tempfile);
76 if ((fpout = safe_fopen (tempfile, "w")) == NULL) {
77 mutt_error _("Could not create temporary file!");
82 if (DisplayFilter && *DisplayFilter) {
85 /* mutt_endwin (NULL); */
86 filterpid = mutt_create_filter_fd (DisplayFilter, &fpout, NULL, NULL,
87 -1, fileno (fpfilterout), -1);
89 mutt_error (_("Cannot create display filter"));
90 safe_fclose (&fpfilterout);
96 if (!Pager || m_strcmp(Pager, "builtin") == 0)
99 mutt_make_string (buf, sizeof (buf), NONULL (PagerFmt), Context, cur);
101 fputs ("\n\n", fpout);
104 msg = mx_open_message (Context, cur->msgno);
105 if (msg == NULL) res = -1;
108 /* see if crytpo is needed for this message. if so, we should exit curses */
110 if (cur->security & ENCRYPT) {
111 if (cur->security & APPLICATION_SMIME)
112 crypt_smime_getkeys (cur->env);
113 if (!crypt_valid_passphrase (cur->security))
116 cmflags |= M_CM_VERIFY;
118 else if (cur->security & SIGN) {
119 /* find out whether or not the verify signature */
120 if (query_quadoption (OPT_VERIFYSIG, _("Verify PGP signature?")) ==
122 cmflags |= M_CM_VERIFY;
127 if (cmflags & M_CM_VERIFY || cur->security & ENCRYPT) {
128 if (cur->security & APPLICATION_PGP) {
130 crypt_pgp_invoke_getkeys (cur->env->from);
132 crypt_invoke_message (APPLICATION_PGP);
135 if (cur->security & APPLICATION_SMIME)
136 crypt_invoke_message (APPLICATION_SMIME);
139 res = _mutt_copy_message (fpout, msg->fp, cur, cur->content, cmflags,
140 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
141 CH_DECODE | CH_FROM);
142 if (res == 0 && (ferror(fpout) || feof(fpout))) {
143 debug_print (1, ("_mutt_copy_message failed to detect EOF!\n"));
147 mx_close_message (&msg);
150 if ((safe_fclose (&fpout) != 0 && errno != EPIPE) || res == -1) {
151 mutt_error (_("Could not copy message"));
152 if (fpfilterout != NULL) {
153 mutt_wait_filter (filterpid);
154 safe_fclose (&fpfilterout);
157 /* this is maybe just plain wrong but it makes the pager display
158 * what we have; i.e. for the crypto stuff we only get
159 * 'Could not copy message' for invalid passphrases, no PGP output
160 * not nothing; so just display what we have...
163 mutt_unlink (tempfile);
168 if (fpfilterout != NULL && mutt_wait_filter (filterpid) != 0)
169 mutt_any_key_to_continue (NULL);
171 safe_fclose (&fpfilterout); /* XXX - check result? */
174 /* update crypto information for this message */
175 cur->security &= ~(GOODSIGN|BADSIGN);
176 cur->security |= crypt_query (cur->content);
178 /* Remove color cache for this message, in case there
179 are color patterns for both ~g and ~V */
185 if ((cur->security & APPLICATION_SMIME) && (cmflags & M_CM_VERIFY)) {
186 if (cur->security & GOODSIGN) {
187 if (!crypt_smime_verify_sender (cur))
188 mutt_message (_("S/MIME signature successfully verified."));
190 mutt_error (_("S/MIME certificate owner does not match sender."));
192 else if (cur->security & PARTSIGN)
194 ("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."));
199 if ((cur->security & APPLICATION_PGP) && (cmflags & M_CM_VERIFY)) {
200 if (cur->security & GOODSIGN)
201 mutt_message (_("PGP signature successfully verified."));
202 else if (cur->security & PARTSIGN)
204 ("Warning: Part of this message has not been signed."));
205 else if (cur->security & SIGN)
206 mutt_message (_("PGP signature could NOT be verified."));
209 /* Invoke the builtin pager */
213 rc = mutt_pager (NULL, tempfile, M_PAGER_MESSAGE, &info);
219 snprintf (buf, sizeof (buf), "%s %s", NONULL (Pager), tempfile);
220 if ((r = mutt_system (buf)) == -1)
221 mutt_error (_("Error running \"%s\"!"), buf);
223 keypad (stdscr, TRUE);
225 mutt_set_flag (Context, cur, M_READ, 1);
226 if (r != -1 && option (OPTPROMPTAFTER)) {
227 mutt_ungetch (mutt_any_key_to_continue _("Command: "), 0);
228 rc = km_dokey (MENU_PAGER);
237 void ci_bounce_message (HEADER * h, int *redraw)
239 char prompt[SHORT_STRING];
240 char buf[HUGE_STRING] = { 0 };
241 address_t *adr = NULL;
246 m_strcpy(prompt, sizeof(prompt), _("Bounce message to: "));
248 m_strcpy(prompt, sizeof(prompt), _("Bounce tagged messages to: "));
250 rc = mutt_get_field (prompt, buf, sizeof (buf), M_ALIAS);
252 if (option (OPTNEEDREDRAW)) {
253 unset_option (OPTNEEDREDRAW);
254 *redraw = REDRAW_FULL;
260 if (!(adr = rfc822_parse_adrlist (adr, buf))) {
261 mutt_error _("Error parsing address!");
266 adr = mutt_expand_aliases (adr);
268 if (mutt_addrlist_to_idna (adr, &err) < 0) {
269 mutt_error (_("Bad IDN: '%s'"), err);
271 address_delete (&adr);
276 rfc822_write_address (buf, sizeof (buf), adr, 1);
278 #define extra_space (15 + 7 + 2)
279 snprintf (prompt, sizeof (prompt),
280 (h ? _("Bounce message to %s") : _("Bounce messages to %s")),
283 if (mutt_strwidth (prompt) > COLS - extra_space) {
284 mutt_format_string(prompt, sizeof(prompt), 0, COLS - extra_space, 0, 0,
285 prompt, sizeof(prompt), 0);
286 m_strcat(prompt, sizeof(prompt), "...?");
288 m_strcat(prompt, sizeof(prompt), "?");
291 if (query_quadoption (OPT_BOUNCE, prompt) != M_YES) {
292 address_delete (&adr);
293 CLEARLINE (LINES - 1);
294 mutt_message (h ? _("Message not bounced.") : _("Messages not bounced."));
298 CLEARLINE (LINES - 1);
300 rc = mutt_bounce_message (NULL, h, adr);
301 address_delete (&adr);
302 /* If no error, or background, display message. */
303 if ((rc == 0) || (rc == S_BKG))
304 mutt_message (h ? _("Message bounced.") : _("Messages bounced."));
307 static void pipe_set_flags (int decode, int print, int *cmflags, int *chflags)
310 *cmflags |= M_CM_DECODE | M_CM_CHARCONV;
311 *chflags |= CH_DECODE | CH_REORDER;
313 if (option (OPTWEED)) {
315 *cmflags |= M_CM_WEED;
320 *cmflags |= M_CM_PRINTING;
324 static void pipe_msg (HEADER * h, FILE * fp, int decode, int print)
327 int chflags = CH_FROM;
329 pipe_set_flags (decode, print, &cmflags, &chflags);
331 if (decode && h->security & ENCRYPT) {
332 if (!crypt_valid_passphrase (h->security))
338 mutt_parse_mime_message (Context, h);
340 mutt_copy_message (fp, Context, h, cmflags, chflags);
344 /* the following code is shared between printing and piping */
346 static int _mutt_pipe_message(HEADER * h, char *cmd, int decode, int print,
347 int split, const char *sep)
354 /* mutt_endwin (NULL);
356 is this really needed here ?
357 it makes the screen flicker on pgp and s/mime messages,
358 before asking for a passphrase...
362 mutt_message_hook (Context, h, M_MESSAGEHOOK);
365 mutt_parse_mime_message (Context, h);
366 if (h->security & ENCRYPT && !crypt_valid_passphrase (h->security))
371 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
372 mutt_perror (_("Can't create filter process"));
377 pipe_msg (h, fpout, decode, print);
379 rc = mutt_wait_filter (thepid);
381 else { /* handle tagged messages */
384 for (i = 0; i < Context->vcount; i++)
385 if (Context->hdrs[Context->v2r[i]]->tagged) {
386 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
388 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
389 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT &&
390 !crypt_valid_passphrase (Context->hdrs[Context->v2r[i]]->
397 for (i = 0; i < Context->vcount; i++) {
398 if (Context->hdrs[Context->v2r[i]]->tagged) {
399 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
402 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
403 mutt_perror (_("Can't create filter process"));
407 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
408 /* add the message separator */
411 safe_fclose (&fpout);
412 if (mutt_wait_filter (thepid) != 0)
419 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
420 mutt_perror (_("Can't create filter process"));
424 for (i = 0; i < Context->vcount; i++) {
425 if (Context->hdrs[Context->v2r[i]]->tagged) {
426 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
428 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
429 /* add the message separator */
434 safe_fclose (&fpout);
435 if (mutt_wait_filter (thepid) != 0)
440 if (rc || option (OPTWAITKEY))
441 mutt_any_key_to_continue (NULL);
445 void mutt_pipe_message (HEADER * h)
447 char buffer[LONG_STRING];
450 if (mutt_get_field (_("Pipe to command: "), buffer, sizeof (buffer), M_CMD)
454 mutt_expand_path (buffer, sizeof (buffer));
455 _mutt_pipe_message (h, buffer,
456 option (OPTPIPEDECODE),
457 0, option (OPTPIPESPLIT), PipeSep);
460 void mutt_print_message (HEADER * h)
463 if (quadoption (OPT_PRINT) && (!PrintCmd || !*PrintCmd)) {
464 mutt_message (_("No printing command has been defined."));
468 if (query_quadoption (OPT_PRINT,
469 h ? _("Print message?") : _("Print tagged messages?"))
473 if (_mutt_pipe_message (h, PrintCmd,
474 option (OPTPRINTDECODE),
475 1, option (OPTPRINTSPLIT), "\f") == 0)
476 mutt_message (h ? _("Message printed") : _("Messages printed"));
478 mutt_message (h ? _("Message could not be printed") :
479 _("Messages could not be printed"));
483 int mutt_select_sort (int reverse)
485 int method = Sort; /* save the current method in case of abort */
487 switch (mutt_multi_choice (reverse ?
489 ("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?: ")
492 ("Sort (d)ate/(f)rm/(r)ecv/(s)ubj/t(o)/(t)hread/(u)nsort/si(z)e/s(c)ore/s(p)am?: "),
494 case -1: /* abort - don't resort */
506 Sort = SORT_RECEIVED;
517 case 6: /* (t)hread */
521 case 7: /* (u)nsort */
529 case 9: /* s(c)ore */
533 case 10: /* s(p)am */
538 Sort |= SORT_REVERSE;
540 return (Sort != method ? 0 : -1); /* no need to resort if it's the same */
543 /* invoke a command in a subshell */
544 void mutt_shell_escape (void)
546 char buf[LONG_STRING];
549 if (mutt_get_field (_("Shell command: "), buf, sizeof (buf), M_CMD) == 0) {
550 if (!buf[0] && Shell)
551 m_strcpy(buf, sizeof(buf), Shell);
553 CLEARLINE (LINES - 1);
556 if (mutt_system (buf) != 0 || option (OPTWAITKEY))
557 mutt_any_key_to_continue (NULL);
562 /* enter a mutt command */
563 void mutt_enter_command (void)
566 char buffer[LONG_STRING], errbuf[SHORT_STRING];
570 if (mutt_get_field (":", buffer, sizeof (buffer), M_COMMAND) != 0
574 err.dsize = sizeof (errbuf);
576 r = mutt_parse_rc_line (buffer, &token, &err);
577 p_delete(&token.data);
579 /* since errbuf could potentially contain printf() sequences in it,
580 we must call mutt_error() in this fashion so that vsprintf()
581 doesn't expect more arguments that we passed */
583 mutt_message ("%s", errbuf);
585 mutt_error ("%s", errbuf);
589 void mutt_display_address (ENVELOPE * env)
591 const char *pfx = NULL;
592 char buf[SHORT_STRING];
593 address_t *adr = NULL;
595 adr = mutt_get_address(env, &pfx);
601 * Note: We don't convert IDNA to local representation this time.
602 * That is intentional, so the user has an opportunity to copy &
603 * paste the on-the-wire form of the address to other, IDN-unable
608 rfc822_write_address (buf, sizeof (buf), adr, 0);
609 mutt_message ("%s: %s", pfx, buf);
612 static void set_copy_flags (HEADER * hdr, int decode, int decrypt,
613 int *cmflags, int *chflags)
616 *chflags = CH_UPDATE_LEN;
618 if (!decode && decrypt && (hdr->security & ENCRYPT)) {
619 if (mutt_is_multipart_encrypted (hdr->content)) {
620 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
621 *cmflags = M_CM_DECODE_PGP;
623 else if (mutt_is_application_pgp (hdr->content) & ENCRYPT)
625 else if (mutt_is_application_smime (hdr->content) & ENCRYPT) {
626 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
627 *cmflags = M_CM_DECODE_SMIME;
632 *chflags = CH_XMIT | CH_MIME | CH_TXTPLAIN;
633 *cmflags = M_CM_DECODE | M_CM_CHARCONV;
635 if (!decrypt) { /* If decode doesn't kick in for decrypt, */
636 *chflags |= CH_DECODE; /* then decode RFC 2047 headers, */
638 if (option (OPTWEED)) {
639 *chflags |= CH_WEED; /* and respect $weed. */
640 *cmflags |= M_CM_WEED;
646 int _mutt_save_message (HEADER * h, CONTEXT * ctx, int delete, int decode,
648 int cmflags, chflags;
651 set_copy_flags (h, decode, decrypt, &cmflags, &chflags);
653 if (decode || decrypt)
654 mutt_parse_mime_message (Context, h);
656 if ((rc = mutt_append_message (ctx, Context, h, cmflags, chflags)) != 0)
660 mutt_set_flag (Context, h, M_DELETE, 1);
661 if (option (OPTDELETEUNTAG))
662 mutt_set_flag (Context, h, M_TAG, 0);
663 mutt_set_flag (Context, h, M_APPENDED, 1);
668 /* returns 0 if the copy/save was successful, or -1 on error/abort */
669 int mutt_save_message (HEADER * h, int delete,
670 int decode, int decrypt, int *redraw) {
671 int i, need_buffy_cleanup;
672 int need_passphrase = 0, app = 0;
673 char prompt[SHORT_STRING], buf[_POSIX_PATH_MAX];
686 snprintf (prompt, sizeof (prompt),
687 decode ? (delete ? _("Decode-save%s to mailbox") :
688 _("Decode-copy%s to mailbox")) :
689 (decrypt ? (delete ? _("Decrypt-save%s to mailbox") :
690 _("Decrypt-copy%s to mailbox")) :
691 (delete ? _("Save%s to mailbox") : _("Copy%s to mailbox"))),
692 h ? "" : _(" tagged"));
696 need_passphrase = h->security & ENCRYPT;
698 mutt_message_hook (Context, h, M_MESSAGEHOOK);
699 mutt_default_save (buf, sizeof (buf), h);
702 /* look for the first tagged message */
704 for (i = 0; i < Context->vcount; i++) {
705 if (Context->hdrs[Context->v2r[i]]->tagged) {
706 h = Context->hdrs[Context->v2r[i]];
713 mutt_message_hook (Context, h, M_MESSAGEHOOK);
714 mutt_default_save (buf, sizeof (buf), h);
715 need_passphrase = h->security & ENCRYPT;
721 mutt_pretty_mailbox (buf);
722 if (mutt_enter_fname (prompt, buf, sizeof (buf), redraw, 0) == -1)
725 if (*redraw != REDRAW_FULL) {
727 *redraw = REDRAW_INDEX | REDRAW_STATUS;
729 *redraw = REDRAW_STATUS;
735 /* This is an undocumented feature of ELM pointed out to me by Felix von
736 * Leitner <leitner@prz.fu-berlin.de>
738 if (m_strcmp(buf, ".") == 0)
739 m_strcpy(buf, sizeof(buf), LastSaveFolder);
741 m_strcpy(LastSaveFolder, sizeof(LastSaveFolder), buf);
743 mutt_expand_path (buf, sizeof (buf));
745 /* check to make sure that this file is really the one the user wants */
746 if (mutt_save_confirm (buf, &st) != 0)
749 if (need_passphrase && (decode || decrypt)
750 && !crypt_valid_passphrase (app))
753 mutt_message (_("Copying to %s..."), buf);
755 if (Context->magic == M_IMAP && !(decode || decrypt) && mx_get_magic (buf) == M_IMAP) {
756 switch (imap_copy_messages (Context, h, buf, delete)) {
761 /* non-fatal error: fall through to fetch/append */
764 /* fatal error, abort */
770 if (mx_open_mailbox (buf, M_APPEND, &ctx) != NULL) {
772 if (_mutt_save_message (h, &ctx, delete, decode, decrypt) != 0) {
773 mx_close_mailbox (&ctx, NULL);
777 for (i = 0; i < Context->vcount; i++) {
778 if (Context->hdrs[Context->v2r[i]]->tagged) {
779 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
781 if (_mutt_save_message (Context->hdrs[Context->v2r[i]], &ctx, delete,
782 decode, decrypt) != 0) {
783 mx_close_mailbox (&ctx, NULL);
790 need_buffy_cleanup = (ctx.magic == M_MBOX || ctx.magic == M_MMDF);
792 mx_close_mailbox (&ctx, NULL);
794 if (need_buffy_cleanup) {
796 tmp = buffy_find_mailbox (buf);
797 if (tmp && tmp->new <= 0)
798 buffy_update_mailbox (tmp);
800 /* fix up the times so buffy won't get confused */
801 if (st.st_mtime > st.st_atime) {
802 ut.actime = st.st_atime;
803 ut.modtime = time (NULL);
818 void mutt_version (void)
820 mutt_message (mutt_make_version (1));
823 void mutt_edit_content_type (HEADER * h, BODY * b, FILE * fp)
825 char buf[LONG_STRING];
826 char obuf[LONG_STRING];
830 char charset[STRING];
833 short charset_changed = 0;
834 short type_changed = 0;
836 cp = mutt_get_parameter ("charset", b->parameter);
837 m_strcpy(charset, sizeof(charset), NONULL(cp));
839 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
840 m_strcpy(obuf, sizeof(obuf), buf);
844 for (p = b->parameter; p; p = p->next) {
847 rfc822_strcpy(tmp, sizeof(tmp), p->value, MimeSpecials);
848 snprintf (buf + l, sizeof (buf) - l, "; %s=%s", p->attribute, tmp);
852 if (mutt_get_field ("Content-Type: ", buf, sizeof (buf), 0) != 0 ||
856 /* clean up previous junk */
857 parameter_delete(&b->parameter);
858 p_delete(&b->subtype);
860 mutt_parse_content_type (buf, b);
863 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
864 type_changed = ascii_strcasecmp (tmp, obuf);
866 ascii_strcasecmp (charset, mutt_get_parameter ("charset", b->parameter));
868 /* if in send mode, check for conversion - current setting is default. */
870 if (!h && b->type == TYPETEXT && charset_changed) {
873 snprintf (tmp, sizeof (tmp), _("Convert to %s upon sending?"),
874 mutt_get_parameter ("charset", b->parameter));
875 if ((r = mutt_yesorno (tmp, !b->noconv)) != -1)
876 b->noconv = (r == M_NO);
879 /* inform the user */
881 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
883 mutt_message (_("Content-Type changed to %s."), tmp);
884 if (b->type == TYPETEXT && charset_changed) {
887 mutt_message (_("Character set changed to %s; %s."),
888 mutt_get_parameter ("charset", b->parameter),
889 b->noconv ? _("not converting") : _("converting"));
892 b->force_charset |= charset_changed ? 1 : 0;
894 if (!is_multipart (b) && b->parts)
895 mutt_free_body (&b->parts);
896 if (!mutt_is_message_type (b->type, b->subtype) && b->hdr) {
897 b->hdr->content = NULL;
898 header_delete(&b->hdr);
901 if (fp && (is_multipart (b) || mutt_is_message_type (b->type, b->subtype)))
902 mutt_parse_part (fp, b);
908 h->security |= crypt_query (b);
913 static int _mutt_check_traditional_pgp (HEADER * h, int *redraw)
918 h->security |= PGP_TRADITIONAL_CHECKED;
920 mutt_parse_mime_message (Context, h);
921 if ((msg = mx_open_message (Context, h->msgno)) == NULL)
923 if (crypt_pgp_check_traditional (msg->fp, h->content, 0)) {
924 h->security = crypt_query (h->content);
925 *redraw |= REDRAW_FULL;
929 h->security |= PGP_TRADITIONAL_CHECKED;
930 mx_close_message (&msg);
934 int mutt_check_traditional_pgp (HEADER * h, int *redraw)
939 if (h && !(h->security & PGP_TRADITIONAL_CHECKED))
940 rv = _mutt_check_traditional_pgp (h, redraw);
942 for (i = 0; i < Context->vcount; i++)
943 if (Context->hdrs[Context->v2r[i]]->tagged &&
944 !(Context->hdrs[Context->v2r[i]]->
945 security & PGP_TRADITIONAL_CHECKED))
947 _mutt_check_traditional_pgp (Context->hdrs[Context->v2r[i]], redraw)