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>
28 #include <lib-lib/debug.h>
30 #include <lib-mime/mime.h>
32 #include <lib-sys/exit.h>
34 #include <lib-ui/curses.h>
35 #include <lib-ui/enter.h>
36 #include <lib-ui/menu.h>
39 #include "recvattach.h"
44 #include <lib-crypt/crypt.h>
45 #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))) {
145 debug_print (1, ("_mutt_copy_message failed to detect EOF!\n"));
149 mx_close_message (&msg);
152 if ((safe_fclose (&fpout) != 0 && errno != EPIPE) || res == -1) {
153 mutt_error (_("Could not copy message"));
154 if (fpfilterout != NULL) {
155 mutt_wait_filter (filterpid);
156 safe_fclose (&fpfilterout);
159 /* this is maybe just plain wrong but it makes the pager display
160 * what we have; i.e. for the crypto stuff we only get
161 * 'Could not copy message' for invalid passphrases, no PGP output
162 * not nothing; so just display what we have...
165 mutt_unlink (tempfile);
170 if (fpfilterout != NULL && mutt_wait_filter (filterpid) != 0)
171 mutt_any_key_to_continue (NULL);
173 safe_fclose (&fpfilterout); /* XXX - check result? */
176 /* update crypto information for this message */
177 cur->security &= ~(GOODSIGN|BADSIGN);
178 cur->security |= crypt_query (cur->content);
180 /* Remove color cache for this message, in case there
181 are color patterns for both ~g and ~V */
187 if ((cur->security & APPLICATION_SMIME) && (cmflags & M_CM_VERIFY)) {
188 if (cur->security & GOODSIGN) {
189 if (!crypt_smime_verify_sender (cur))
190 mutt_message (_("S/MIME signature successfully verified."));
192 mutt_error (_("S/MIME certificate owner does not match sender."));
194 else if (cur->security & PARTSIGN)
196 ("Warning: Part of this message has not been signed."));
197 else if (cur->security & SIGN || cur->security & BADSIGN)
198 mutt_error (_("S/MIME signature could NOT be verified."));
201 if ((cur->security & APPLICATION_PGP) && (cmflags & M_CM_VERIFY)) {
202 if (cur->security & GOODSIGN)
203 mutt_message (_("PGP signature successfully verified."));
204 else if (cur->security & PARTSIGN)
206 ("Warning: Part of this message has not been signed."));
207 else if (cur->security & SIGN)
208 mutt_message (_("PGP signature could NOT be verified."));
211 /* Invoke the builtin pager */
215 rc = mutt_pager (NULL, tempfile, M_PAGER_MESSAGE, &info);
221 snprintf (buf, sizeof (buf), "%s %s", NONULL (Pager), tempfile);
222 if ((r = mutt_system (buf)) == -1)
223 mutt_error (_("Error running \"%s\"!"), buf);
225 keypad (stdscr, TRUE);
227 mutt_set_flag (Context, cur, M_READ, 1);
228 if (r != -1 && option (OPTPROMPTAFTER)) {
229 mutt_ungetch (mutt_any_key_to_continue _("Command: "), 0);
230 rc = km_dokey (MENU_PAGER);
239 void ci_bounce_message (HEADER * h, int *redraw)
241 char prompt[SHORT_STRING];
242 char buf[HUGE_STRING] = { 0 };
243 address_t *adr = NULL;
248 m_strcpy(prompt, sizeof(prompt), _("Bounce message to: "));
250 m_strcpy(prompt, sizeof(prompt), _("Bounce tagged messages to: "));
252 rc = mutt_get_field (prompt, buf, sizeof (buf), M_ALIAS);
254 if (option (OPTNEEDREDRAW)) {
255 unset_option (OPTNEEDREDRAW);
256 *redraw = REDRAW_FULL;
262 if (!(adr = rfc822_parse_adrlist (adr, buf))) {
263 mutt_error _("Error parsing address!");
268 adr = mutt_expand_aliases (adr);
270 if (mutt_addrlist_to_idna (adr, &err) < 0) {
271 mutt_error (_("Bad IDN: '%s'"), err);
273 address_delete (&adr);
278 rfc822_write_address (buf, sizeof (buf), adr, 1);
280 #define extra_space (15 + 7 + 2)
281 snprintf (prompt, sizeof (prompt),
282 (h ? _("Bounce message to %s") : _("Bounce messages to %s")),
285 if (mutt_strwidth (prompt) > COLS - extra_space) {
286 mutt_format_string(prompt, sizeof(prompt), 0, COLS - extra_space, 0, 0,
287 prompt, sizeof(prompt), 0);
288 m_strcat(prompt, sizeof(prompt), "...?");
290 m_strcat(prompt, sizeof(prompt), "?");
293 if (query_quadoption (OPT_BOUNCE, prompt) != M_YES) {
294 address_delete (&adr);
295 CLEARLINE (LINES - 1);
296 mutt_message (h ? _("Message not bounced.") : _("Messages not bounced."));
300 CLEARLINE (LINES - 1);
302 rc = mutt_bounce_message (NULL, h, adr);
303 address_delete (&adr);
304 /* If no error, or background, display message. */
305 if ((rc == 0) || (rc == S_BKG))
306 mutt_message (h ? _("Message bounced.") : _("Messages bounced."));
309 static void pipe_set_flags (int decode, int print, int *cmflags, int *chflags)
312 *cmflags |= M_CM_DECODE | M_CM_CHARCONV;
313 *chflags |= CH_DECODE | CH_REORDER;
315 if (option (OPTWEED)) {
317 *cmflags |= M_CM_WEED;
322 *cmflags |= M_CM_PRINTING;
326 static void pipe_msg (HEADER * h, FILE * fp, int decode, int print)
329 int chflags = CH_FROM;
331 pipe_set_flags (decode, print, &cmflags, &chflags);
333 if (decode && h->security & ENCRYPT) {
334 if (!crypt_valid_passphrase (h->security))
340 mutt_parse_mime_message (Context, h);
342 mutt_copy_message (fp, Context, h, cmflags, chflags);
346 /* the following code is shared between printing and piping */
348 static int _mutt_pipe_message(HEADER * h, char *cmd, int decode, int print,
349 int split, const char *sep)
356 /* mutt_endwin (NULL);
358 is this really needed here ?
359 it makes the screen flicker on pgp and s/mime messages,
360 before asking for a passphrase...
364 mutt_message_hook (Context, h, M_MESSAGEHOOK);
367 mutt_parse_mime_message (Context, h);
368 if (h->security & ENCRYPT && !crypt_valid_passphrase (h->security))
373 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
374 mutt_perror (_("Can't create filter process"));
379 pipe_msg (h, fpout, decode, print);
381 rc = mutt_wait_filter (thepid);
383 else { /* handle tagged messages */
386 for (i = 0; i < Context->vcount; i++)
387 if (Context->hdrs[Context->v2r[i]]->tagged) {
388 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
390 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
391 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT &&
392 !crypt_valid_passphrase (Context->hdrs[Context->v2r[i]]->
399 for (i = 0; i < Context->vcount; i++) {
400 if (Context->hdrs[Context->v2r[i]]->tagged) {
401 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
404 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
405 mutt_perror (_("Can't create filter process"));
409 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
410 /* add the message separator */
413 safe_fclose (&fpout);
414 if (mutt_wait_filter (thepid) != 0)
421 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
422 mutt_perror (_("Can't create filter process"));
426 for (i = 0; i < Context->vcount; i++) {
427 if (Context->hdrs[Context->v2r[i]]->tagged) {
428 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
430 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
431 /* add the message separator */
436 safe_fclose (&fpout);
437 if (mutt_wait_filter (thepid) != 0)
442 if (rc || option (OPTWAITKEY))
443 mutt_any_key_to_continue (NULL);
447 void mutt_pipe_message (HEADER * h)
449 char buffer[LONG_STRING];
452 if (mutt_get_field (_("Pipe to command: "), buffer, sizeof (buffer), M_CMD)
456 mutt_expand_path (buffer, sizeof (buffer));
457 _mutt_pipe_message (h, buffer,
458 option (OPTPIPEDECODE),
459 0, option (OPTPIPESPLIT), PipeSep);
462 void mutt_print_message (HEADER * h)
465 if (quadoption (OPT_PRINT) && (!PrintCmd || !*PrintCmd)) {
466 mutt_message (_("No printing command has been defined."));
470 if (query_quadoption (OPT_PRINT,
471 h ? _("Print message?") : _("Print tagged messages?"))
475 if (_mutt_pipe_message (h, PrintCmd,
476 option (OPTPRINTDECODE),
477 1, option (OPTPRINTSPLIT), "\f") == 0)
478 mutt_message (h ? _("Message printed") : _("Messages printed"));
480 mutt_message (h ? _("Message could not be printed") :
481 _("Messages could not be printed"));
485 int mutt_select_sort (int reverse)
487 int method = Sort; /* save the current method in case of abort */
489 switch (mutt_multi_choice (reverse ?
491 ("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?: ")
494 ("Sort (d)ate/(f)rm/(r)ecv/(s)ubj/t(o)/(t)hread/(u)nsort/si(z)e/s(c)ore/s(p)am?: "),
496 case -1: /* abort - don't resort */
508 Sort = SORT_RECEIVED;
519 case 6: /* (t)hread */
523 case 7: /* (u)nsort */
531 case 9: /* s(c)ore */
535 case 10: /* s(p)am */
540 Sort |= SORT_REVERSE;
542 return (Sort != method ? 0 : -1); /* no need to resort if it's the same */
545 /* invoke a command in a subshell */
546 void mutt_shell_escape (void)
548 char buf[LONG_STRING];
551 if (mutt_get_field (_("Shell command: "), buf, sizeof (buf), M_CMD) == 0) {
552 if (!buf[0] && Shell)
553 m_strcpy(buf, sizeof(buf), Shell);
555 CLEARLINE (LINES - 1);
558 if (mutt_system (buf) != 0 || option (OPTWAITKEY))
559 mutt_any_key_to_continue (NULL);
564 /* enter a mutt command */
565 void mutt_enter_command (void)
568 char buffer[LONG_STRING], errbuf[SHORT_STRING];
572 if (mutt_get_field (":", buffer, sizeof (buffer), M_COMMAND) != 0
576 err.dsize = sizeof (errbuf);
578 r = mutt_parse_rc_line (buffer, &token, &err);
579 p_delete(&token.data);
581 /* since errbuf could potentially contain printf() sequences in it,
582 we must call mutt_error() in this fashion so that vsprintf()
583 doesn't expect more arguments that we passed */
585 mutt_message ("%s", errbuf);
587 mutt_error ("%s", errbuf);
591 void mutt_display_address (ENVELOPE * env)
593 const char *pfx = NULL;
594 char buf[SHORT_STRING];
595 address_t *adr = NULL;
597 adr = mutt_get_address(env, &pfx);
603 * Note: We don't convert IDNA to local representation this time.
604 * That is intentional, so the user has an opportunity to copy &
605 * paste the on-the-wire form of the address to other, IDN-unable
610 rfc822_write_address (buf, sizeof (buf), adr, 0);
611 mutt_message ("%s: %s", pfx, buf);
614 static void set_copy_flags (HEADER * hdr, int decode, int decrypt,
615 int *cmflags, int *chflags)
618 *chflags = CH_UPDATE_LEN;
620 if (!decode && decrypt && (hdr->security & ENCRYPT)) {
621 if (mutt_is_multipart_encrypted (hdr->content)) {
622 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
623 *cmflags = M_CM_DECODE_PGP;
625 else if (mutt_is_application_pgp (hdr->content) & ENCRYPT)
627 else if (mutt_is_application_smime (hdr->content) & ENCRYPT) {
628 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
629 *cmflags = M_CM_DECODE_SMIME;
634 *chflags = CH_XMIT | CH_MIME | CH_TXTPLAIN;
635 *cmflags = M_CM_DECODE | M_CM_CHARCONV;
637 if (!decrypt) { /* If decode doesn't kick in for decrypt, */
638 *chflags |= CH_DECODE; /* then decode RFC 2047 headers, */
640 if (option (OPTWEED)) {
641 *chflags |= CH_WEED; /* and respect $weed. */
642 *cmflags |= M_CM_WEED;
648 int _mutt_save_message (HEADER * h, CONTEXT * ctx, int delete, int decode,
650 int cmflags, chflags;
653 set_copy_flags (h, decode, decrypt, &cmflags, &chflags);
655 if (decode || decrypt)
656 mutt_parse_mime_message (Context, h);
658 if ((rc = mutt_append_message (ctx, Context, h, cmflags, chflags)) != 0)
662 mutt_set_flag (Context, h, M_DELETE, 1);
663 if (option (OPTDELETEUNTAG))
664 mutt_set_flag (Context, h, M_TAG, 0);
665 mutt_set_flag (Context, h, M_APPENDED, 1);
670 /* returns 0 if the copy/save was successful, or -1 on error/abort */
671 int mutt_save_message (HEADER * h, int delete,
672 int decode, int decrypt, int *redraw) {
673 int i, need_buffy_cleanup;
674 int need_passphrase = 0, app = 0;
675 char prompt[SHORT_STRING], buf[_POSIX_PATH_MAX];
688 snprintf (prompt, sizeof (prompt),
689 decode ? (delete ? _("Decode-save%s to mailbox") :
690 _("Decode-copy%s to mailbox")) :
691 (decrypt ? (delete ? _("Decrypt-save%s to mailbox") :
692 _("Decrypt-copy%s to mailbox")) :
693 (delete ? _("Save%s to mailbox") : _("Copy%s to mailbox"))),
694 h ? "" : _(" tagged"));
698 need_passphrase = h->security & ENCRYPT;
700 mutt_message_hook (Context, h, M_MESSAGEHOOK);
701 mutt_default_save (buf, sizeof (buf), h);
704 /* look for the first tagged message */
706 for (i = 0; i < Context->vcount; i++) {
707 if (Context->hdrs[Context->v2r[i]]->tagged) {
708 h = Context->hdrs[Context->v2r[i]];
715 mutt_message_hook (Context, h, M_MESSAGEHOOK);
716 mutt_default_save (buf, sizeof (buf), h);
717 need_passphrase = h->security & ENCRYPT;
723 mutt_pretty_mailbox (buf);
724 if (mutt_enter_fname (prompt, buf, sizeof (buf), redraw, 0) == -1)
727 if (*redraw != REDRAW_FULL) {
729 *redraw = REDRAW_INDEX | REDRAW_STATUS;
731 *redraw = REDRAW_STATUS;
737 /* This is an undocumented feature of ELM pointed out to me by Felix von
738 * Leitner <leitner@prz.fu-berlin.de>
740 if (m_strcmp(buf, ".") == 0)
741 m_strcpy(buf, sizeof(buf), LastSaveFolder);
743 m_strcpy(LastSaveFolder, sizeof(LastSaveFolder), buf);
745 mutt_expand_path (buf, sizeof (buf));
747 /* check to make sure that this file is really the one the user wants */
748 if (mutt_save_confirm (buf, &st) != 0)
751 if (need_passphrase && (decode || decrypt)
752 && !crypt_valid_passphrase (app))
755 mutt_message (_("Copying to %s..."), buf);
757 if (Context->magic == M_IMAP && !(decode || decrypt) && mx_get_magic (buf) == M_IMAP) {
758 switch (imap_copy_messages (Context, h, buf, delete)) {
763 /* non-fatal error: fall through to fetch/append */
766 /* fatal error, abort */
772 if (mx_open_mailbox (buf, M_APPEND, &ctx) != NULL) {
774 if (_mutt_save_message (h, &ctx, delete, decode, decrypt) != 0) {
775 mx_close_mailbox (&ctx, NULL);
779 for (i = 0; i < Context->vcount; i++) {
780 if (Context->hdrs[Context->v2r[i]]->tagged) {
781 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
783 if (_mutt_save_message (Context->hdrs[Context->v2r[i]], &ctx, delete,
784 decode, decrypt) != 0) {
785 mx_close_mailbox (&ctx, NULL);
792 need_buffy_cleanup = (ctx.magic == M_MBOX || ctx.magic == M_MMDF);
794 mx_close_mailbox (&ctx, NULL);
796 if (need_buffy_cleanup) {
798 tmp = buffy_find_mailbox (buf);
799 if (tmp && tmp->new <= 0)
800 buffy_update_mailbox (tmp);
802 /* fix up the times so buffy won't get confused */
803 if (st.st_mtime > st.st_atime) {
804 ut.actime = st.st_atime;
805 ut.modtime = time (NULL);
820 void mutt_version (void)
822 mutt_message (mutt_make_version (1));
825 void mutt_edit_content_type (HEADER * h, BODY * b, FILE * fp)
827 char buf[LONG_STRING];
828 char obuf[LONG_STRING];
832 char charset[STRING];
835 short charset_changed = 0;
836 short type_changed = 0;
838 cp = mutt_get_parameter ("charset", b->parameter);
839 m_strcpy(charset, sizeof(charset), NONULL(cp));
841 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
842 m_strcpy(obuf, sizeof(obuf), buf);
846 for (p = b->parameter; p; p = p->next) {
849 rfc822_strcpy(tmp, sizeof(tmp), p->value, MimeSpecials);
850 snprintf (buf + l, sizeof (buf) - l, "; %s=%s", p->attribute, tmp);
854 if (mutt_get_field ("Content-Type: ", buf, sizeof (buf), 0) != 0 ||
858 /* clean up previous junk */
859 parameter_delete(&b->parameter);
860 p_delete(&b->subtype);
862 mutt_parse_content_type (buf, b);
865 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
866 type_changed = ascii_strcasecmp (tmp, obuf);
868 ascii_strcasecmp (charset, mutt_get_parameter ("charset", b->parameter));
870 /* if in send mode, check for conversion - current setting is default. */
872 if (!h && b->type == TYPETEXT && charset_changed) {
875 snprintf (tmp, sizeof (tmp), _("Convert to %s upon sending?"),
876 mutt_get_parameter ("charset", b->parameter));
877 if ((r = mutt_yesorno (tmp, !b->noconv)) != -1)
878 b->noconv = (r == M_NO);
881 /* inform the user */
883 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
885 mutt_message (_("Content-Type changed to %s."), tmp);
886 if (b->type == TYPETEXT && charset_changed) {
889 mutt_message (_("Character set changed to %s; %s."),
890 mutt_get_parameter ("charset", b->parameter),
891 b->noconv ? _("not converting") : _("converting"));
894 b->force_charset |= charset_changed ? 1 : 0;
896 if (!is_multipart(b) && b->parts)
897 mutt_free_body (&b->parts);
898 if (!mutt_is_message_type (b->type, b->subtype) && b->hdr) {
899 b->hdr->content = NULL;
900 header_delete(&b->hdr);
903 if (fp && (is_multipart(b) || mutt_is_message_type(b->type, b->subtype)))
904 mutt_parse_part (fp, b);
910 h->security |= crypt_query (b);
915 static int _mutt_check_traditional_pgp (HEADER * h, int *redraw)
920 h->security |= PGP_TRADITIONAL_CHECKED;
922 mutt_parse_mime_message (Context, h);
923 if ((msg = mx_open_message (Context, h->msgno)) == NULL)
925 if (crypt_pgp_check_traditional (msg->fp, h->content, 0)) {
926 h->security = crypt_query (h->content);
927 *redraw |= REDRAW_FULL;
931 h->security |= PGP_TRADITIONAL_CHECKED;
932 mx_close_message (&msg);
936 int mutt_check_traditional_pgp (HEADER * h, int *redraw)
941 if (h && !(h->security & PGP_TRADITIONAL_CHECKED))
942 rv = _mutt_check_traditional_pgp (h, redraw);
944 for (i = 0; i < Context->vcount; i++)
945 if (Context->hdrs[Context->v2r[i]]->tagged &&
946 !(Context->hdrs[Context->v2r[i]]->
947 security & PGP_TRADITIONAL_CHECKED))
949 _mutt_check_traditional_pgp (Context->hdrs[Context->v2r[i]], redraw)