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>
33 #include <lib-sys/unix.h>
35 #include <lib-ui/curses.h>
36 #include <lib-ui/enter.h>
37 #include <lib-ui/menu.h>
41 #include "recvattach.h"
46 #include <lib-crypt/crypt.h>
47 #include "mutt_idna.h"
49 #include <sys/types.h>
53 #include <imap/imap.h>
59 /* The folder the user last saved to. Used by ci_save_message() */
60 static char LastSaveFolder[_POSIX_PATH_MAX] = "";
62 int mutt_display_message (HEADER * cur)
64 char tempfile[_POSIX_PATH_MAX], buf[LONG_STRING];
65 int rc = 0, builtin = 0;
66 int cmflags = M_CM_DECODE | M_CM_DISPLAY | M_CM_CHARCONV;
68 FILE *fpfilterout = NULL;
73 snprintf (buf, sizeof (buf), "%s/%s", TYPE (cur->content),
74 cur->content->subtype);
76 mutt_parse_mime_message (Context, cur);
77 mutt_message_hook (Context, cur, M_MESSAGEHOOK);
79 mutt_mktemp (tempfile);
80 if ((fpout = safe_fopen (tempfile, "w")) == NULL) {
81 mutt_error _("Could not create temporary file!");
86 if (DisplayFilter && *DisplayFilter) {
89 /* mutt_endwin (NULL); */
90 filterpid = mutt_create_filter_fd (DisplayFilter, &fpout, NULL, NULL,
91 -1, fileno (fpfilterout), -1);
93 mutt_error (_("Cannot create display filter"));
94 safe_fclose (&fpfilterout);
100 if (!Pager || m_strcmp(Pager, "builtin") == 0)
103 mutt_make_string (buf, sizeof (buf), NONULL (PagerFmt), Context, cur);
105 fputs ("\n\n", fpout);
108 msg = mx_open_message (Context, cur->msgno);
109 if (msg == NULL) res = -1;
112 /* see if crytpo is needed for this message. if so, we should exit curses */
114 if (cur->security & ENCRYPT) {
115 if (cur->security & APPLICATION_SMIME)
116 crypt_smime_getkeys (cur->env);
117 if (!crypt_valid_passphrase (cur->security))
120 cmflags |= M_CM_VERIFY;
122 else if (cur->security & SIGN) {
123 /* find out whether or not the verify signature */
124 if (query_quadoption (OPT_VERIFYSIG, _("Verify PGP signature?")) ==
126 cmflags |= M_CM_VERIFY;
131 if (cmflags & M_CM_VERIFY || cur->security & ENCRYPT) {
132 if (cur->security & APPLICATION_PGP) {
134 crypt_pgp_invoke_getkeys (cur->env->from);
136 crypt_invoke_message (APPLICATION_PGP);
139 if (cur->security & APPLICATION_SMIME)
140 crypt_invoke_message (APPLICATION_SMIME);
143 res = _mutt_copy_message (fpout, msg->fp, cur, cur->content, cmflags,
144 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
145 CH_DECODE | CH_FROM);
146 if (res == 0 && (ferror(fpout) || feof(fpout))) {
147 debug_print (1, ("_mutt_copy_message failed to detect EOF!\n"));
151 mx_close_message (&msg);
154 if ((safe_fclose (&fpout) != 0 && errno != EPIPE) || res == -1) {
155 mutt_error (_("Could not copy message"));
156 if (fpfilterout != NULL) {
157 mutt_wait_filter (filterpid);
158 safe_fclose (&fpfilterout);
161 /* this is maybe just plain wrong but it makes the pager display
162 * what we have; i.e. for the crypto stuff we only get
163 * 'Could not copy message' for invalid passphrases, no PGP output
164 * not nothing; so just display what we have...
167 mutt_unlink (tempfile);
172 if (fpfilterout != NULL && mutt_wait_filter (filterpid) != 0)
173 mutt_any_key_to_continue (NULL);
175 safe_fclose (&fpfilterout); /* XXX - check result? */
178 /* update crypto information for this message */
179 cur->security &= ~(GOODSIGN|BADSIGN);
180 cur->security |= crypt_query (cur->content);
182 /* Remove color cache for this message, in case there
183 are color patterns for both ~g and ~V */
189 if ((cur->security & APPLICATION_SMIME) && (cmflags & M_CM_VERIFY)) {
190 if (cur->security & GOODSIGN) {
191 if (!crypt_smime_verify_sender (cur))
192 mutt_message (_("S/MIME signature successfully verified."));
194 mutt_error (_("S/MIME certificate owner does not match sender."));
196 else if (cur->security & PARTSIGN)
198 ("Warning: Part of this message has not been signed."));
199 else if (cur->security & SIGN || cur->security & BADSIGN)
200 mutt_error (_("S/MIME signature could NOT be verified."));
203 if ((cur->security & APPLICATION_PGP) && (cmflags & M_CM_VERIFY)) {
204 if (cur->security & GOODSIGN)
205 mutt_message (_("PGP signature successfully verified."));
206 else if (cur->security & PARTSIGN)
208 ("Warning: Part of this message has not been signed."));
209 else if (cur->security & SIGN)
210 mutt_message (_("PGP signature could NOT be verified."));
213 /* Invoke the builtin pager */
217 rc = mutt_pager (NULL, tempfile, M_PAGER_MESSAGE, &info);
223 snprintf (buf, sizeof (buf), "%s %s", NONULL (Pager), tempfile);
224 if ((r = mutt_system (buf)) == -1)
225 mutt_error (_("Error running \"%s\"!"), buf);
227 keypad (stdscr, TRUE);
229 mutt_set_flag (Context, cur, M_READ, 1);
230 if (r != -1 && option (OPTPROMPTAFTER)) {
231 mutt_ungetch (mutt_any_key_to_continue _("Command: "), 0);
232 rc = km_dokey (MENU_PAGER);
241 void ci_bounce_message (HEADER * h, int *redraw)
243 char prompt[SHORT_STRING];
244 char buf[HUGE_STRING] = { 0 };
245 address_t *adr = NULL;
246 const char *err = NULL;
250 m_strcpy(prompt, sizeof(prompt), _("Bounce message to: "));
252 m_strcpy(prompt, sizeof(prompt), _("Bounce tagged messages to: "));
254 rc = mutt_get_field (prompt, buf, sizeof (buf), M_ALIAS);
256 if (option (OPTNEEDREDRAW)) {
257 unset_option (OPTNEEDREDRAW);
258 *redraw = REDRAW_FULL;
264 if (!(adr = rfc822_parse_adrlist (adr, buf))) {
265 mutt_error _("Error parsing address!");
270 adr = mutt_expand_aliases (adr);
272 if (mutt_addrlist_to_idna (adr, &err) < 0) {
273 mutt_error (_("Bad IDN: '%s'"), err);
275 address_list_wipe(&adr);
280 rfc822_write_address (buf, sizeof (buf), adr, 1);
282 #define extra_space (15 + 7 + 2)
283 snprintf (prompt, sizeof (prompt),
284 (h ? _("Bounce message to %s") : _("Bounce messages to %s")),
287 if (mutt_strwidth (prompt) > COLS - extra_space) {
288 mutt_format_string(prompt, sizeof(prompt), 0, COLS - extra_space, 0, 0,
289 prompt, sizeof(prompt), 0);
290 m_strcat(prompt, sizeof(prompt), "...?");
292 m_strcat(prompt, sizeof(prompt), "?");
295 if (query_quadoption (OPT_BOUNCE, prompt) != M_YES) {
296 address_list_wipe(&adr);
297 CLEARLINE (LINES - 1);
298 mutt_message (h ? _("Message not bounced.") : _("Messages not bounced."));
302 CLEARLINE (LINES - 1);
304 rc = mutt_bounce_message (NULL, h, adr);
305 address_list_wipe(&adr);
306 /* If no error, or background, display message. */
307 if ((rc == 0) || (rc == S_BKG))
308 mutt_message (h ? _("Message bounced.") : _("Messages bounced."));
311 static void pipe_set_flags (int decode, int print, int *cmflags, int *chflags)
314 *cmflags |= M_CM_DECODE | M_CM_CHARCONV;
315 *chflags |= CH_DECODE | CH_REORDER;
317 if (option (OPTWEED)) {
319 *cmflags |= M_CM_WEED;
324 *cmflags |= M_CM_PRINTING;
328 static void pipe_msg (HEADER * h, FILE * fp, int decode, int print)
331 int chflags = CH_FROM;
333 pipe_set_flags (decode, print, &cmflags, &chflags);
335 if (decode && h->security & ENCRYPT) {
336 if (!crypt_valid_passphrase (h->security))
342 mutt_parse_mime_message (Context, h);
344 mutt_copy_message (fp, Context, h, cmflags, chflags);
348 /* the following code is shared between printing and piping */
350 static int _mutt_pipe_message(HEADER * h, char *cmd, int decode, int print,
351 int split, const char *sep)
358 /* mutt_endwin (NULL);
360 is this really needed here ?
361 it makes the screen flicker on pgp and s/mime messages,
362 before asking for a passphrase...
366 mutt_message_hook (Context, h, M_MESSAGEHOOK);
369 mutt_parse_mime_message (Context, h);
370 if (h->security & ENCRYPT && !crypt_valid_passphrase (h->security))
375 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
376 mutt_perror (_("Can't create filter process"));
381 pipe_msg (h, fpout, decode, print);
383 rc = mutt_wait_filter (thepid);
385 else { /* handle tagged messages */
388 for (i = 0; i < Context->vcount; i++)
389 if (Context->hdrs[Context->v2r[i]]->tagged) {
390 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
392 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
393 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT &&
394 !crypt_valid_passphrase (Context->hdrs[Context->v2r[i]]->
401 for (i = 0; i < Context->vcount; i++) {
402 if (Context->hdrs[Context->v2r[i]]->tagged) {
403 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
406 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
407 mutt_perror (_("Can't create filter process"));
411 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
412 /* add the message separator */
415 safe_fclose (&fpout);
416 if (mutt_wait_filter (thepid) != 0)
423 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
424 mutt_perror (_("Can't create filter process"));
428 for (i = 0; i < Context->vcount; i++) {
429 if (Context->hdrs[Context->v2r[i]]->tagged) {
430 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
432 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
433 /* add the message separator */
438 safe_fclose (&fpout);
439 if (mutt_wait_filter (thepid) != 0)
444 if (rc || option (OPTWAITKEY))
445 mutt_any_key_to_continue (NULL);
449 void mutt_pipe_message (HEADER * h)
451 char buffer[LONG_STRING];
454 if (mutt_get_field (_("Pipe to command: "), buffer, sizeof (buffer), M_CMD)
458 mutt_expand_path (buffer, sizeof (buffer));
459 _mutt_pipe_message (h, buffer,
460 option (OPTPIPEDECODE),
461 0, option (OPTPIPESPLIT), PipeSep);
464 void mutt_print_message (HEADER * h)
467 if (quadoption (OPT_PRINT) && (!PrintCmd || !*PrintCmd)) {
468 mutt_message (_("No printing command has been defined."));
472 if (query_quadoption (OPT_PRINT,
473 h ? _("Print message?") : _("Print tagged messages?"))
477 if (_mutt_pipe_message (h, PrintCmd,
478 option (OPTPRINTDECODE),
479 1, option (OPTPRINTSPLIT), "\f") == 0)
480 mutt_message (h ? _("Message printed") : _("Messages printed"));
482 mutt_message (h ? _("Message could not be printed") :
483 _("Messages could not be printed"));
487 int mutt_select_sort (int reverse)
489 int method = Sort; /* save the current method in case of abort */
491 switch (mutt_multi_choice (reverse ?
493 ("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?: ")
496 ("Sort (d)ate/(f)rm/(r)ecv/(s)ubj/t(o)/(t)hread/(u)nsort/si(z)e/s(c)ore/s(p)am?: "),
498 case -1: /* abort - don't resort */
510 Sort = SORT_RECEIVED;
521 case 6: /* (t)hread */
525 case 7: /* (u)nsort */
533 case 9: /* s(c)ore */
537 case 10: /* s(p)am */
542 Sort |= SORT_REVERSE;
544 return (Sort != method ? 0 : -1); /* no need to resort if it's the same */
547 /* invoke a command in a subshell */
548 void mutt_shell_escape (void)
550 char buf[LONG_STRING];
553 if (mutt_get_field (_("Shell command: "), buf, sizeof (buf), M_CMD) == 0) {
554 if (!buf[0] && Shell)
555 m_strcpy(buf, sizeof(buf), Shell);
557 CLEARLINE (LINES - 1);
560 if (mutt_system (buf) != 0 || option (OPTWAITKEY))
561 mutt_any_key_to_continue (NULL);
566 /* enter a mutt command */
567 void mutt_enter_command (void)
570 char buffer[LONG_STRING], errbuf[SHORT_STRING];
574 if (mutt_get_field (":", buffer, sizeof (buffer), M_COMMAND) != 0
578 err.dsize = sizeof (errbuf);
580 r = mutt_parse_rc_line (buffer, &token, &err);
581 p_delete(&token.data);
583 /* since errbuf could potentially contain printf() sequences in it,
584 we must call mutt_error() in this fashion so that vsprintf()
585 doesn't expect more arguments that we passed */
587 mutt_message ("%s", errbuf);
589 mutt_error ("%s", errbuf);
593 void mutt_display_address (ENVELOPE * env)
595 const char *pfx = NULL;
596 char buf[SHORT_STRING];
597 address_t *adr = NULL;
599 adr = mutt_get_address(env, &pfx);
605 * Note: We don't convert IDNA to local representation this time.
606 * That is intentional, so the user has an opportunity to copy &
607 * paste the on-the-wire form of the address to other, IDN-unable
612 rfc822_write_address (buf, sizeof (buf), adr, 0);
613 mutt_message ("%s: %s", pfx, buf);
616 static void set_copy_flags (HEADER * hdr, int decode, int decrypt,
617 int *cmflags, int *chflags)
620 *chflags = CH_UPDATE_LEN;
622 if (!decode && decrypt && (hdr->security & ENCRYPT)) {
623 if (mutt_is_multipart_encrypted (hdr->content)) {
624 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
625 *cmflags = M_CM_DECODE_PGP;
627 else if (mutt_is_application_pgp (hdr->content) & ENCRYPT)
629 else if (mutt_is_application_smime (hdr->content) & ENCRYPT) {
630 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
631 *cmflags = M_CM_DECODE_SMIME;
636 *chflags = CH_XMIT | CH_MIME | CH_TXTPLAIN;
637 *cmflags = M_CM_DECODE | M_CM_CHARCONV;
639 if (!decrypt) { /* If decode doesn't kick in for decrypt, */
640 *chflags |= CH_DECODE; /* then decode RFC 2047 headers, */
642 if (option (OPTWEED)) {
643 *chflags |= CH_WEED; /* and respect $weed. */
644 *cmflags |= M_CM_WEED;
650 int _mutt_save_message (HEADER * h, CONTEXT * ctx, int delete, int decode,
652 int cmflags, chflags;
655 set_copy_flags (h, decode, decrypt, &cmflags, &chflags);
657 if (decode || decrypt)
658 mutt_parse_mime_message (Context, h);
660 if ((rc = mutt_append_message (ctx, Context, h, cmflags, chflags)) != 0)
664 mutt_set_flag (Context, h, M_DELETE, 1);
665 if (option (OPTDELETEUNTAG))
666 mutt_set_flag (Context, h, M_TAG, 0);
667 mutt_set_flag (Context, h, M_APPENDED, 1);
672 /* returns 0 if the copy/save was successful, or -1 on error/abort */
673 int mutt_save_message (HEADER * h, int delete,
674 int decode, int decrypt, int *redraw) {
675 int i, need_buffy_cleanup;
676 int need_passphrase = 0, app = 0;
677 char prompt[SHORT_STRING], buf[_POSIX_PATH_MAX];
690 snprintf (prompt, sizeof (prompt),
691 decode ? (delete ? _("Decode-save%s to mailbox") :
692 _("Decode-copy%s to mailbox")) :
693 (decrypt ? (delete ? _("Decrypt-save%s to mailbox") :
694 _("Decrypt-copy%s to mailbox")) :
695 (delete ? _("Save%s to mailbox") : _("Copy%s to mailbox"))),
696 h ? "" : _(" tagged"));
700 need_passphrase = h->security & ENCRYPT;
702 mutt_message_hook (Context, h, M_MESSAGEHOOK);
703 mutt_default_save (buf, sizeof (buf), h);
706 /* look for the first tagged message */
708 for (i = 0; i < Context->vcount; i++) {
709 if (Context->hdrs[Context->v2r[i]]->tagged) {
710 h = Context->hdrs[Context->v2r[i]];
717 mutt_message_hook (Context, h, M_MESSAGEHOOK);
718 mutt_default_save (buf, sizeof (buf), h);
719 need_passphrase = h->security & ENCRYPT;
725 mutt_pretty_mailbox (buf);
726 if (mutt_enter_fname (prompt, buf, sizeof (buf), redraw, 0) == -1)
729 if (*redraw != REDRAW_FULL) {
731 *redraw = REDRAW_INDEX | REDRAW_STATUS;
733 *redraw = REDRAW_STATUS;
739 /* This is an undocumented feature of ELM pointed out to me by Felix von
740 * Leitner <leitner@prz.fu-berlin.de>
742 if (m_strcmp(buf, ".") == 0)
743 m_strcpy(buf, sizeof(buf), LastSaveFolder);
745 m_strcpy(LastSaveFolder, sizeof(LastSaveFolder), buf);
747 mutt_expand_path (buf, sizeof (buf));
749 /* check to make sure that this file is really the one the user wants */
750 if (mutt_save_confirm (buf, &st) != 0)
753 if (need_passphrase && (decode || decrypt)
754 && !crypt_valid_passphrase (app))
757 mutt_message (_("Copying to %s..."), buf);
759 if (Context->magic == M_IMAP && !(decode || decrypt) && mx_get_magic (buf) == M_IMAP) {
760 switch (imap_copy_messages (Context, h, buf, delete)) {
765 /* non-fatal error: fall through to fetch/append */
768 /* fatal error, abort */
774 if (mx_open_mailbox (buf, M_APPEND, &ctx) != NULL) {
776 if (_mutt_save_message (h, &ctx, delete, decode, decrypt) != 0) {
777 mx_close_mailbox (&ctx, NULL);
781 for (i = 0; i < Context->vcount; i++) {
782 if (Context->hdrs[Context->v2r[i]]->tagged) {
783 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
785 if (_mutt_save_message (Context->hdrs[Context->v2r[i]], &ctx, delete,
786 decode, decrypt) != 0) {
787 mx_close_mailbox (&ctx, NULL);
794 need_buffy_cleanup = (ctx.magic == M_MBOX || ctx.magic == M_MMDF);
796 mx_close_mailbox (&ctx, NULL);
798 if (need_buffy_cleanup) {
800 tmp = buffy_find_mailbox (buf);
801 if (tmp && tmp->new <= 0)
802 buffy_update_mailbox (tmp);
804 /* fix up the times so buffy won't get confused */
805 if (st.st_mtime > st.st_atime) {
806 ut.actime = st.st_atime;
807 ut.modtime = time (NULL);
822 void mutt_version (void)
824 mutt_message (mutt_make_version (1));
827 void mutt_edit_content_type (HEADER * h, BODY * b, FILE * fp)
829 char buf[LONG_STRING];
830 char obuf[LONG_STRING];
834 char charset[STRING];
837 short charset_changed = 0;
838 short type_changed = 0;
840 cp = mutt_get_parameter ("charset", b->parameter);
841 m_strcpy(charset, sizeof(charset), NONULL(cp));
843 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
844 m_strcpy(obuf, sizeof(obuf), buf);
848 for (p = b->parameter; p; p = p->next) {
851 rfc822_strcpy(tmp, sizeof(tmp), p->value, MimeSpecials);
852 snprintf (buf + l, sizeof (buf) - l, "; %s=%s", p->attribute, tmp);
856 if (mutt_get_field ("Content-Type: ", buf, sizeof (buf), 0) != 0 ||
860 /* clean up previous junk */
861 parameter_list_wipe(&b->parameter);
862 p_delete(&b->subtype);
864 mutt_parse_content_type (buf, b);
867 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
868 type_changed = ascii_strcasecmp (tmp, obuf);
870 ascii_strcasecmp (charset, mutt_get_parameter ("charset", b->parameter));
872 /* if in send mode, check for conversion - current setting is default. */
874 if (!h && b->type == TYPETEXT && charset_changed) {
877 snprintf (tmp, sizeof (tmp), _("Convert to %s upon sending?"),
878 mutt_get_parameter ("charset", b->parameter));
879 if ((r = mutt_yesorno (tmp, !b->noconv)) != -1)
880 b->noconv = (r == M_NO);
883 /* inform the user */
885 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
887 mutt_message (_("Content-Type changed to %s."), tmp);
888 if (b->type == TYPETEXT && charset_changed) {
891 mutt_message (_("Character set changed to %s; %s."),
892 mutt_get_parameter ("charset", b->parameter),
893 b->noconv ? _("not converting") : _("converting"));
896 b->force_charset |= charset_changed ? 1 : 0;
898 if (!is_multipart(b) && b->parts)
899 mutt_free_body (&b->parts);
900 if (!mutt_is_message_type (b->type, b->subtype) && b->hdr) {
901 b->hdr->content = NULL;
902 header_delete(&b->hdr);
905 if (fp && (is_multipart(b) || mutt_is_message_type(b->type, b->subtype)))
906 mutt_parse_part (fp, b);
912 h->security |= crypt_query (b);
917 static int _mutt_check_traditional_pgp (HEADER * h, int *redraw)
922 h->security |= PGP_TRADITIONAL_CHECKED;
924 mutt_parse_mime_message (Context, h);
925 if ((msg = mx_open_message (Context, h->msgno)) == NULL)
927 if (crypt_pgp_check_traditional (msg->fp, h->content, 0)) {
928 h->security = crypt_query (h->content);
929 *redraw |= REDRAW_FULL;
933 h->security |= PGP_TRADITIONAL_CHECKED;
934 mx_close_message (&msg);
938 int mutt_check_traditional_pgp (HEADER * h, int *redraw)
943 if (h && !(h->security & PGP_TRADITIONAL_CHECKED))
944 rv = _mutt_check_traditional_pgp (h, redraw);
946 for (i = 0; i < Context->vcount; i++)
947 if (Context->hdrs[Context->v2r[i]]->tagged &&
948 !(Context->hdrs[Context->v2r[i]]->
949 security & PGP_TRADITIONAL_CHECKED))
951 _mutt_check_traditional_pgp (Context->hdrs[Context->v2r[i]], redraw)