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 cmflags |= M_CM_VERIFY;
98 else if (cur->security & SIGN) {
99 /* find out whether or not the verify signature */
100 if (query_quadoption (OPT_VERIFYSIG, _("Verify PGP signature?")) ==
102 cmflags |= M_CM_VERIFY;
107 if (cmflags & M_CM_VERIFY || cur->security & ENCRYPT) {
108 if (cur->security & APPLICATION_PGP) {
110 crypt_pgp_invoke_getkeys (cur->env->from);
112 crypt_invoke_message (APPLICATION_PGP);
115 if (cur->security & APPLICATION_SMIME)
116 crypt_invoke_message (APPLICATION_SMIME);
119 res = _mutt_copy_message (fpout, msg->fp, cur, cur->content, cmflags,
120 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
121 CH_DECODE | CH_FROM);
122 if (res == 0 && (ferror(fpout) || feof(fpout))) {
126 mx_close_message (&msg);
129 if ((m_fclose(&fpout) != 0 && errno != EPIPE) || res == -1) {
130 mutt_error (_("Could not copy message"));
131 if (fpfilterout != NULL) {
132 mutt_wait_filter (filterpid);
133 m_fclose(&fpfilterout);
137 if (fpfilterout != NULL && mutt_wait_filter (filterpid) != 0)
138 mutt_any_key_to_continue (NULL);
140 m_fclose(&fpfilterout); /* XXX - check result? */
143 /* update crypto information for this message */
144 cur->security &= ~(GOODSIGN|BADSIGN);
145 cur->security |= crypt_query (cur->content);
147 /* Remove color cache for this message, in case there
148 are color patterns for both ~g and ~V */
154 if ((cur->security & APPLICATION_SMIME) && (cmflags & M_CM_VERIFY)) {
155 if (cur->security & GOODSIGN) {
156 if (!crypt_smime_verify_sender (cur))
157 mutt_message (_("S/MIME signature successfully verified."));
159 mutt_error (_("S/MIME certificate owner does not match sender."));
161 else if (cur->security & PARTSIGN)
163 ("Warning: Part of this message has not been signed."));
164 else if (cur->security & SIGN || cur->security & BADSIGN)
165 mutt_error (_("S/MIME signature could NOT be verified."));
168 if ((cur->security & APPLICATION_PGP) && (cmflags & M_CM_VERIFY)) {
169 if (cur->security & GOODSIGN)
170 mutt_message (_("PGP signature successfully verified."));
171 else if (cur->security & PARTSIGN)
173 ("Warning: Part of this message has not been signed."));
174 else if (cur->security & SIGN)
175 mutt_message (_("PGP signature could NOT be verified."));
178 /* Invoke the builtin pager */
182 rc = mutt_pager (NULL, tempfile, M_PAGER_MESSAGE, &info);
188 snprintf (buf, sizeof (buf), "%s %s", NONULL (Pager), tempfile);
189 if ((r = mutt_system (buf)) == -1)
190 mutt_error (_("Error running \"%s\"!"), buf);
192 keypad (stdscr, TRUE);
194 mutt_set_flag (Context, cur, M_READ, 1);
195 if (r != -1 && option (OPTPROMPTAFTER)) {
196 mutt_ungetch (mutt_any_key_to_continue _("Command: "), 0);
197 rc = km_dokey (MENU_PAGER);
206 void ci_bounce_message (HEADER * h, int *redraw)
209 char buf[HUGE_STRING] = { 0 };
210 address_t *adr = NULL;
215 m_strcpy(prompt, sizeof(prompt), _("Bounce message to: "));
217 m_strcpy(prompt, sizeof(prompt), _("Bounce tagged messages to: "));
219 rc = mutt_get_field (prompt, buf, sizeof (buf), M_ALIAS);
221 if (option (OPTNEEDREDRAW)) {
222 unset_option (OPTNEEDREDRAW);
223 *redraw = REDRAW_FULL;
229 if (!(adr = rfc822_parse_adrlist (adr, buf))) {
230 mutt_error _("Error parsing address!");
235 adr = mutt_expand_aliases (adr);
237 if (mutt_addrlist_to_idna (adr, &err) < 0) {
238 mutt_error (_("Bad IDN: '%s'"), err);
240 address_list_wipe(&adr);
245 rfc822_addrcat(buf, sizeof (buf), adr, 1);
247 #define extra_space (15 + 7 + 2)
248 snprintf (prompt, sizeof (prompt),
249 (h ? _("Bounce message to %s") : _("Bounce messages to %s")),
252 if (m_strwidth(prompt) > COLS - extra_space) {
253 mutt_format_string(prompt, sizeof(prompt), 0, COLS - extra_space, 0, 0,
254 prompt, sizeof(prompt), 0);
255 m_strcat(prompt, sizeof(prompt), "...?");
257 m_strcat(prompt, sizeof(prompt), "?");
260 if (query_quadoption (OPT_BOUNCE, prompt) != M_YES) {
261 address_list_wipe(&adr);
262 CLEARLINE (LINES - 1);
263 mutt_message (h ? _("Message not bounced.") : _("Messages not bounced."));
267 CLEARLINE (LINES - 1);
269 rc = mutt_bounce_message (NULL, h, adr);
270 address_list_wipe(&adr);
271 /* If no error, or background, display message. */
272 if ((rc == 0) || (rc == S_BKG))
273 mutt_message (h ? _("Message bounced.") : _("Messages bounced."));
276 static void pipe_set_flags (int decode, int print, int *cmflags, int *chflags)
279 *cmflags |= M_CM_DECODE | M_CM_CHARCONV;
280 *chflags |= CH_DECODE | CH_REORDER;
282 if (option (OPTWEED)) {
284 *cmflags |= M_CM_WEED;
289 *cmflags |= M_CM_PRINTING;
293 static void pipe_msg (HEADER * h, FILE * fp, int decode, int print)
296 int chflags = CH_FROM;
298 pipe_set_flags (decode, print, &cmflags, &chflags);
300 if (decode && h->security & ENCRYPT) {
305 mutt_parse_mime_message (Context, h);
307 mutt_copy_message (fp, Context, h, cmflags, chflags);
311 /* the following code is shared between printing and piping */
313 static int _mutt_pipe_message(HEADER * h, char *cmd, int decode, int print,
314 int split, const char *sep)
321 /* mutt_endwin (NULL);
323 is this really needed here ?
324 it makes the screen flicker on pgp and s/mime messages,
325 before asking for a passphrase...
329 mutt_message_hook (Context, h, M_MESSAGEHOOK);
332 mutt_parse_mime_message (Context, h);
336 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
337 mutt_perror (_("Can't create filter process"));
342 pipe_msg (h, fpout, decode, print);
344 rc = mutt_wait_filter (thepid);
346 else { /* handle tagged messages */
349 for (i = 0; i < Context->vcount; i++)
350 if (Context->hdrs[Context->v2r[i]]->tagged) {
351 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
353 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
358 for (i = 0; i < Context->vcount; i++) {
359 if (Context->hdrs[Context->v2r[i]]->tagged) {
360 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
363 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
364 mutt_perror (_("Can't create filter process"));
368 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
369 /* add the message separator */
373 if (mutt_wait_filter (thepid) != 0)
380 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
381 mutt_perror (_("Can't create filter process"));
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 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
390 /* add the message separator */
396 if (mutt_wait_filter (thepid) != 0)
401 if (rc || option (OPTWAITKEY))
402 mutt_any_key_to_continue (NULL);
406 void mutt_pipe_message (HEADER * h)
408 char buffer[LONG_STRING];
411 if (mutt_get_field (_("Pipe to command: "), buffer, sizeof (buffer), M_CMD)
415 mutt_expand_path (buffer, sizeof (buffer));
416 _mutt_pipe_message (h, buffer,
417 option (OPTPIPEDECODE),
418 0, option (OPTPIPESPLIT), PipeSep);
421 void mutt_print_message (HEADER * h)
424 if (quadoption (OPT_PRINT) && m_strisempty(PrintCmd)) {
425 mutt_message (_("No printing command has been defined."));
429 if (query_quadoption (OPT_PRINT,
430 h ? _("Print message?") : _("Print tagged messages?"))
434 if (_mutt_pipe_message (h, PrintCmd,
435 option (OPTPRINTDECODE),
436 1, option (OPTPRINTSPLIT), "\f") == 0)
437 mutt_message (h ? _("Message printed") : _("Messages printed"));
439 mutt_message (h ? _("Message could not be printed") :
440 _("Messages could not be printed"));
444 int mutt_select_sort (int reverse)
446 int method = Sort; /* save the current method in case of abort */
448 switch (mutt_multi_choice (reverse ?
450 ("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?: ")
453 ("Sort (d)ate/(f)rm/(r)ecv/(s)ubj/t(o)/(t)hread/(u)nsort/si(z)e/s(c)ore/s(p)am?: "),
455 case -1: /* abort - don't resort */
467 Sort = SORT_RECEIVED;
478 case 6: /* (t)hread */
482 case 7: /* (u)nsort */
490 case 9: /* s(c)ore */
494 case 10: /* s(p)am */
499 Sort |= SORT_REVERSE;
501 return (Sort != method ? 0 : -1); /* no need to resort if it's the same */
504 /* invoke a command in a subshell */
505 void mutt_shell_escape (void)
507 char buf[LONG_STRING];
510 if (mutt_get_field (_("Shell command: "), buf, sizeof (buf), M_CMD) == 0) {
512 m_strcpy(buf, sizeof(buf), MCore.shell);
514 CLEARLINE (LINES - 1);
517 if (mutt_system (buf) != 0 || option (OPTWAITKEY))
518 mutt_any_key_to_continue (NULL);
523 /* enter a mutt command */
524 void mutt_enter_command (void)
527 char buffer[LONG_STRING], errbuf[STRING];
531 if (mutt_get_field (":", buffer, sizeof (buffer), M_COMMAND) != 0
535 err.dsize = sizeof (errbuf);
537 r = mutt_parse_rc_line (buffer, &token, &err);
538 p_delete(&token.data);
540 /* since errbuf could potentially contain printf() sequences in it,
541 we must call mutt_error() in this fashion so that vsprintf()
542 doesn't expect more arguments that we passed */
544 mutt_message ("%s", errbuf);
546 mutt_error ("%s", errbuf);
550 void mutt_display_address (ENVELOPE * env)
552 const char *pfx = NULL;
554 address_t *adr = NULL;
556 adr = mutt_get_address(env, &pfx);
562 * Note: We don't convert IDNA to local representation this time.
563 * That is intentional, so the user has an opportunity to copy &
564 * paste the on-the-wire form of the address to other, IDN-unable
569 rfc822_addrcat(buf, sizeof (buf), adr, 0);
570 mutt_message ("%s: %s", pfx, buf);
573 static void set_copy_flags (HEADER * hdr, int decode, int decrypt,
574 int *cmflags, int *chflags)
577 *chflags = CH_UPDATE_LEN;
579 if (!decode && decrypt && (hdr->security & ENCRYPT)) {
580 if (mutt_is_multipart_encrypted (hdr->content)) {
581 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
582 *cmflags = M_CM_DECODE_PGP;
584 else if (mutt_is_application_pgp (hdr->content) & ENCRYPT)
586 else if (mutt_is_application_smime (hdr->content) & ENCRYPT) {
587 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
588 *cmflags = M_CM_DECODE_SMIME;
593 *chflags = CH_XMIT | CH_MIME | CH_TXTPLAIN;
594 *cmflags = M_CM_DECODE | M_CM_CHARCONV;
596 if (!decrypt) { /* If decode doesn't kick in for decrypt, */
597 *chflags |= CH_DECODE; /* then decode RFC 2047 headers, */
599 if (option (OPTWEED)) {
600 *chflags |= CH_WEED; /* and respect $weed. */
601 *cmflags |= M_CM_WEED;
607 int _mutt_save_message (HEADER * h, CONTEXT * ctx, int delete, int decode,
609 int cmflags, chflags;
612 set_copy_flags (h, decode, decrypt, &cmflags, &chflags);
614 if (decode || decrypt)
615 mutt_parse_mime_message (Context, h);
617 if ((rc = mutt_append_message (ctx, Context, h, cmflags, chflags)) != 0)
621 mutt_set_flag (Context, h, M_DELETE, 1);
622 if (option (OPTDELETEUNTAG))
623 mutt_set_flag (Context, h, M_TAG, 0);
624 mutt_set_flag (Context, h, M_APPENDED, 1);
629 /* returns 0 if the copy/save was successful, or -1 on error/abort */
630 int mutt_save_message (HEADER * h, int delete,
631 int decode, int decrypt, int *redraw) {
632 int i, need_buffy_cleanup;
633 int need_passphrase = 0, app = 0;
634 char prompt[STRING], buf[_POSIX_PATH_MAX];
642 snprintf (prompt, sizeof (prompt),
643 decode ? (delete ? _("Decode-save%s to mailbox") :
644 _("Decode-copy%s to mailbox")) :
645 (decrypt ? (delete ? _("Decrypt-save%s to mailbox") :
646 _("Decrypt-copy%s to mailbox")) :
647 (delete ? _("Save%s to mailbox") : _("Copy%s to mailbox"))),
648 h ? "" : _(" tagged"));
652 need_passphrase = h->security & ENCRYPT;
654 mutt_message_hook (Context, h, M_MESSAGEHOOK);
655 mutt_default_save (buf, sizeof (buf), h);
658 /* look for the first tagged message */
660 for (i = 0; i < Context->vcount; i++) {
661 if (Context->hdrs[Context->v2r[i]]->tagged) {
662 h = Context->hdrs[Context->v2r[i]];
669 mutt_message_hook (Context, h, M_MESSAGEHOOK);
670 mutt_default_save (buf, sizeof (buf), h);
671 need_passphrase = h->security & ENCRYPT;
677 mutt_pretty_mailbox (buf);
678 if (mutt_enter_fname (prompt, buf, sizeof (buf), redraw, 0) == -1)
681 if (*redraw != REDRAW_FULL) {
683 *redraw = REDRAW_INDEX | REDRAW_STATUS;
685 *redraw = REDRAW_STATUS;
691 /* This is an undocumented feature of ELM pointed out to me by Felix von
692 * Leitner <leitner@prz.fu-berlin.de>
694 if (m_strcmp(buf, ".") == 0)
695 m_strcpy(buf, sizeof(buf), LastSaveFolder);
697 m_strcpy(LastSaveFolder, sizeof(LastSaveFolder), buf);
699 mutt_expand_path (buf, sizeof (buf));
701 /* check to make sure that this file is really the one the user wants */
702 if (mutt_save_confirm (buf, &st) != 0)
705 mutt_message (_("Copying to %s..."), buf);
707 if (Context->magic == M_IMAP && !(decode || decrypt) && mx_get_magic (buf) == M_IMAP) {
708 switch (imap_copy_messages (Context, h, buf, delete)) {
713 /* non-fatal error: fall through to fetch/append */
716 /* fatal error, abort */
722 if (mx_open_mailbox (buf, M_APPEND, &ctx) != NULL) {
724 if (_mutt_save_message (h, &ctx, delete, decode, decrypt) != 0) {
725 mx_close_mailbox (&ctx, NULL);
729 for (i = 0; i < Context->vcount; i++) {
730 if (Context->hdrs[Context->v2r[i]]->tagged) {
731 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
733 if (_mutt_save_message (Context->hdrs[Context->v2r[i]], &ctx, delete,
734 decode, decrypt) != 0) {
735 mx_close_mailbox (&ctx, NULL);
742 need_buffy_cleanup = (ctx.magic == M_MBOX || ctx.magic == M_MMDF);
744 mx_close_mailbox (&ctx, NULL);
746 if (need_buffy_cleanup) {
747 /* fix up the times so buffy won't get confused */
748 if (st.st_mtime > st.st_atime) {
749 ut.actime = st.st_atime;
750 ut.modtime = time (NULL);
764 void mutt_version (void)
766 mutt_message (mutt_make_version());
769 void mutt_edit_content_type (HEADER * h, BODY * b, FILE * fp)
771 char buf[LONG_STRING];
772 char obuf[LONG_STRING];
775 char charset[STRING];
778 short charset_changed = 0;
779 short type_changed = 0;
781 cp = parameter_getval(b->parameter, "charset");
782 m_strcpy(charset, sizeof(charset), NONULL(cp));
784 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
785 m_strcpy(obuf, sizeof(obuf), buf);
790 for (p = b->parameter; p; p = p->next) {
793 rfc822_strcpy(tmp, sizeof(tmp), p->value, MimeSpecials);
794 snprintf (buf + l, sizeof (buf) - l, "; %s=%s", p->attribute, tmp);
798 if (mutt_get_field ("Content-Type: ", buf, sizeof (buf), 0) != 0 ||
802 /* clean up previous junk */
803 parameter_list_wipe(&b->parameter);
804 p_delete(&b->subtype);
806 mutt_parse_content_type (buf, b);
809 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
810 type_changed = ascii_strcasecmp (tmp, obuf);
812 ascii_strcasecmp (charset, parameter_getval(b->parameter, "charset"));
814 /* if in send mode, check for conversion - current setting is default. */
816 if (!h && b->type == TYPETEXT && charset_changed) {
819 snprintf (tmp, sizeof (tmp), _("Convert to %s upon sending?"),
820 parameter_getval(b->parameter, "charset"));
821 if ((r = mutt_yesorno (tmp, !b->noconv)) != -1)
822 b->noconv = (r == M_NO);
825 /* inform the user */
827 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
829 mutt_message (_("Content-Type changed to %s."), tmp);
830 if (b->type == TYPETEXT && charset_changed) {
833 mutt_message (_("Character set changed to %s; %s."),
834 parameter_getval(b->parameter, "charset"),
835 b->noconv ? _("not converting") : _("converting"));
838 b->force_charset |= charset_changed ? 1 : 0;
840 if (!is_multipart(b) && b->parts)
841 body_list_wipe(&b->parts);
842 if (!mutt_is_message_type(b) && b->hdr) {
843 b->hdr->content = NULL;
844 header_delete(&b->hdr);
847 if (fp && (is_multipart(b) || mutt_is_message_type(b)))
848 mutt_parse_part (fp, b);
854 h->security |= crypt_query (b);
859 static int _mutt_check_traditional_pgp (HEADER * h, int *redraw)
864 h->security |= PGP_TRADITIONAL_CHECKED;
866 mutt_parse_mime_message (Context, h);
867 if ((msg = mx_open_message (Context, h->msgno)) == NULL)
869 if (crypt_pgp_check_traditional (msg->fp, h->content, 0)) {
870 h->security = crypt_query (h->content);
871 *redraw |= REDRAW_FULL;
875 h->security |= PGP_TRADITIONAL_CHECKED;
876 mx_close_message (&msg);
880 int mutt_check_traditional_pgp (HEADER * h, int *redraw)
885 if (h && !(h->security & PGP_TRADITIONAL_CHECKED))
886 rv = _mutt_check_traditional_pgp (h, redraw);
888 for (i = 0; i < Context->vcount; i++)
889 if (Context->hdrs[Context->v2r[i]]->tagged &&
890 !(Context->hdrs[Context->v2r[i]]->
891 security & PGP_TRADITIONAL_CHECKED))
893 _mutt_check_traditional_pgp (Context->hdrs[Context->v2r[i]], redraw)