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/lib-lib.h>
26 #include <lib-mime/mime.h>
28 #include <lib-sys/exit.h>
29 #include <lib-sys/unix.h>
31 #include <lib-ui/curses.h>
32 #include <lib-ui/enter.h>
33 #include <lib-ui/menu.h>
37 #include "recvattach.h"
42 #include <lib-crypt/crypt.h>
43 #include "mutt_idna.h"
44 #include <sys/types.h>
48 #include <imap/imap.h>
54 /* The folder the user last saved to. Used by ci_save_message() */
55 static char LastSaveFolder[_POSIX_PATH_MAX] = "";
57 int mutt_display_message (HEADER * cur)
59 char tempfile[_POSIX_PATH_MAX], buf[LONG_STRING];
60 int rc = 0, builtin = 0;
61 int cmflags = M_CM_DECODE | M_CM_DISPLAY | M_CM_CHARCONV;
63 FILE *fpfilterout = NULL;
68 snprintf (buf, sizeof (buf), "%s/%s", TYPE (cur->content),
69 cur->content->subtype);
71 mutt_parse_mime_message (Context, cur);
72 mutt_message_hook (Context, cur, M_MESSAGEHOOK);
74 mutt_mktemp (tempfile);
75 if ((fpout = safe_fopen (tempfile, "w")) == NULL) {
76 mutt_error _("Could not create temporary file!");
81 if (DisplayFilter && *DisplayFilter) {
84 /* mutt_endwin (NULL); */
85 filterpid = mutt_create_filter_fd (DisplayFilter, &fpout, NULL, NULL,
86 -1, fileno (fpfilterout), -1);
88 mutt_error (_("Cannot create display filter"));
89 safe_fclose (&fpfilterout);
95 if (!Pager || m_strcmp(Pager, "builtin") == 0)
98 mutt_make_string (buf, sizeof (buf), NONULL (PagerFmt), Context, cur);
100 fputs ("\n\n", fpout);
103 msg = mx_open_message (Context, cur->msgno);
104 if (msg == NULL) res = -1;
107 /* see if crytpo is needed for this message. if so, we should exit curses */
109 if (cur->security & ENCRYPT) {
110 if (cur->security & APPLICATION_SMIME)
111 crypt_smime_getkeys (cur->env);
112 if (!crypt_valid_passphrase (cur->security))
115 cmflags |= M_CM_VERIFY;
117 else if (cur->security & SIGN) {
118 /* find out whether or not the verify signature */
119 if (query_quadoption (OPT_VERIFYSIG, _("Verify PGP signature?")) ==
121 cmflags |= M_CM_VERIFY;
126 if (cmflags & M_CM_VERIFY || cur->security & ENCRYPT) {
127 if (cur->security & APPLICATION_PGP) {
129 crypt_pgp_invoke_getkeys (cur->env->from);
131 crypt_invoke_message (APPLICATION_PGP);
134 if (cur->security & APPLICATION_SMIME)
135 crypt_invoke_message (APPLICATION_SMIME);
138 res = _mutt_copy_message (fpout, msg->fp, cur, cur->content, cmflags,
139 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
140 CH_DECODE | CH_FROM);
141 if (res == 0 && (ferror(fpout) || feof(fpout))) {
145 mx_close_message (&msg);
148 if ((safe_fclose (&fpout) != 0 && errno != EPIPE) || res == -1) {
149 mutt_error (_("Could not copy message"));
150 if (fpfilterout != NULL) {
151 mutt_wait_filter (filterpid);
152 safe_fclose (&fpfilterout);
155 /* this is maybe just plain wrong but it makes the pager display
156 * what we have; i.e. for the crypto stuff we only get
157 * 'Could not copy message' for invalid passphrases, no PGP output
158 * not nothing; so just display what we have...
161 mutt_unlink (tempfile);
166 if (fpfilterout != NULL && mutt_wait_filter (filterpid) != 0)
167 mutt_any_key_to_continue (NULL);
169 safe_fclose (&fpfilterout); /* XXX - check result? */
172 /* update crypto information for this message */
173 cur->security &= ~(GOODSIGN|BADSIGN);
174 cur->security |= crypt_query (cur->content);
176 /* Remove color cache for this message, in case there
177 are color patterns for both ~g and ~V */
183 if ((cur->security & APPLICATION_SMIME) && (cmflags & M_CM_VERIFY)) {
184 if (cur->security & GOODSIGN) {
185 if (!crypt_smime_verify_sender (cur))
186 mutt_message (_("S/MIME signature successfully verified."));
188 mutt_error (_("S/MIME certificate owner does not match sender."));
190 else if (cur->security & PARTSIGN)
192 ("Warning: Part of this message has not been signed."));
193 else if (cur->security & SIGN || cur->security & BADSIGN)
194 mutt_error (_("S/MIME signature could NOT be verified."));
197 if ((cur->security & APPLICATION_PGP) && (cmflags & M_CM_VERIFY)) {
198 if (cur->security & GOODSIGN)
199 mutt_message (_("PGP signature successfully verified."));
200 else if (cur->security & PARTSIGN)
202 ("Warning: Part of this message has not been signed."));
203 else if (cur->security & SIGN)
204 mutt_message (_("PGP signature could NOT be verified."));
207 /* Invoke the builtin pager */
211 rc = mutt_pager (NULL, tempfile, M_PAGER_MESSAGE, &info);
217 snprintf (buf, sizeof (buf), "%s %s", NONULL (Pager), tempfile);
218 if ((r = mutt_system (buf)) == -1)
219 mutt_error (_("Error running \"%s\"!"), buf);
221 keypad (stdscr, TRUE);
223 mutt_set_flag (Context, cur, M_READ, 1);
224 if (r != -1 && option (OPTPROMPTAFTER)) {
225 mutt_ungetch (mutt_any_key_to_continue _("Command: "), 0);
226 rc = km_dokey (MENU_PAGER);
235 void ci_bounce_message (HEADER * h, int *redraw)
237 char prompt[SHORT_STRING];
238 char buf[HUGE_STRING] = { 0 };
239 address_t *adr = NULL;
244 m_strcpy(prompt, sizeof(prompt), _("Bounce message to: "));
246 m_strcpy(prompt, sizeof(prompt), _("Bounce tagged messages to: "));
248 rc = mutt_get_field (prompt, buf, sizeof (buf), M_ALIAS);
250 if (option (OPTNEEDREDRAW)) {
251 unset_option (OPTNEEDREDRAW);
252 *redraw = REDRAW_FULL;
258 if (!(adr = rfc822_parse_adrlist (adr, buf))) {
259 mutt_error _("Error parsing address!");
264 adr = mutt_expand_aliases (adr);
266 if (mutt_addrlist_to_idna (adr, &err) < 0) {
267 mutt_error (_("Bad IDN: '%s'"), err);
269 address_list_wipe(&adr);
274 rfc822_write_address (buf, sizeof (buf), adr, 1);
276 #define extra_space (15 + 7 + 2)
277 snprintf (prompt, sizeof (prompt),
278 (h ? _("Bounce message to %s") : _("Bounce messages to %s")),
281 if (mutt_strwidth (prompt) > COLS - extra_space) {
282 mutt_format_string(prompt, sizeof(prompt), 0, COLS - extra_space, 0, 0,
283 prompt, sizeof(prompt), 0);
284 m_strcat(prompt, sizeof(prompt), "...?");
286 m_strcat(prompt, sizeof(prompt), "?");
289 if (query_quadoption (OPT_BOUNCE, prompt) != M_YES) {
290 address_list_wipe(&adr);
291 CLEARLINE (LINES - 1);
292 mutt_message (h ? _("Message not bounced.") : _("Messages not bounced."));
296 CLEARLINE (LINES - 1);
298 rc = mutt_bounce_message (NULL, h, adr);
299 address_list_wipe(&adr);
300 /* If no error, or background, display message. */
301 if ((rc == 0) || (rc == S_BKG))
302 mutt_message (h ? _("Message bounced.") : _("Messages bounced."));
305 static void pipe_set_flags (int decode, int print, int *cmflags, int *chflags)
308 *cmflags |= M_CM_DECODE | M_CM_CHARCONV;
309 *chflags |= CH_DECODE | CH_REORDER;
311 if (option (OPTWEED)) {
313 *cmflags |= M_CM_WEED;
318 *cmflags |= M_CM_PRINTING;
322 static void pipe_msg (HEADER * h, FILE * fp, int decode, int print)
325 int chflags = CH_FROM;
327 pipe_set_flags (decode, print, &cmflags, &chflags);
329 if (decode && h->security & ENCRYPT) {
330 if (!crypt_valid_passphrase (h->security))
336 mutt_parse_mime_message (Context, h);
338 mutt_copy_message (fp, Context, h, cmflags, chflags);
342 /* the following code is shared between printing and piping */
344 static int _mutt_pipe_message(HEADER * h, char *cmd, int decode, int print,
345 int split, const char *sep)
352 /* mutt_endwin (NULL);
354 is this really needed here ?
355 it makes the screen flicker on pgp and s/mime messages,
356 before asking for a passphrase...
360 mutt_message_hook (Context, h, M_MESSAGEHOOK);
363 mutt_parse_mime_message (Context, h);
364 if (h->security & ENCRYPT && !crypt_valid_passphrase (h->security))
369 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
370 mutt_perror (_("Can't create filter process"));
375 pipe_msg (h, fpout, decode, print);
377 rc = mutt_wait_filter (thepid);
379 else { /* handle tagged messages */
382 for (i = 0; i < Context->vcount; i++)
383 if (Context->hdrs[Context->v2r[i]]->tagged) {
384 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
386 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
387 if (Context->hdrs[Context->v2r[i]]->security & ENCRYPT &&
388 !crypt_valid_passphrase (Context->hdrs[Context->v2r[i]]->
395 for (i = 0; i < Context->vcount; i++) {
396 if (Context->hdrs[Context->v2r[i]]->tagged) {
397 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
400 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
401 mutt_perror (_("Can't create filter process"));
405 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
406 /* add the message separator */
409 safe_fclose (&fpout);
410 if (mutt_wait_filter (thepid) != 0)
417 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
418 mutt_perror (_("Can't create filter process"));
422 for (i = 0; i < Context->vcount; i++) {
423 if (Context->hdrs[Context->v2r[i]]->tagged) {
424 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
426 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
427 /* add the message separator */
432 safe_fclose (&fpout);
433 if (mutt_wait_filter (thepid) != 0)
438 if (rc || option (OPTWAITKEY))
439 mutt_any_key_to_continue (NULL);
443 void mutt_pipe_message (HEADER * h)
445 char buffer[LONG_STRING];
448 if (mutt_get_field (_("Pipe to command: "), buffer, sizeof (buffer), M_CMD)
452 mutt_expand_path (buffer, sizeof (buffer));
453 _mutt_pipe_message (h, buffer,
454 option (OPTPIPEDECODE),
455 0, option (OPTPIPESPLIT), PipeSep);
458 void mutt_print_message (HEADER * h)
461 if (quadoption (OPT_PRINT) && (!PrintCmd || !*PrintCmd)) {
462 mutt_message (_("No printing command has been defined."));
466 if (query_quadoption (OPT_PRINT,
467 h ? _("Print message?") : _("Print tagged messages?"))
471 if (_mutt_pipe_message (h, PrintCmd,
472 option (OPTPRINTDECODE),
473 1, option (OPTPRINTSPLIT), "\f") == 0)
474 mutt_message (h ? _("Message printed") : _("Messages printed"));
476 mutt_message (h ? _("Message could not be printed") :
477 _("Messages could not be printed"));
481 int mutt_select_sort (int reverse)
483 int method = Sort; /* save the current method in case of abort */
485 switch (mutt_multi_choice (reverse ?
487 ("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?: ")
490 ("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 case -1: /* abort - don't resort */
504 Sort = SORT_RECEIVED;
515 case 6: /* (t)hread */
519 case 7: /* (u)nsort */
527 case 9: /* s(c)ore */
531 case 10: /* s(p)am */
536 Sort |= SORT_REVERSE;
538 return (Sort != method ? 0 : -1); /* no need to resort if it's the same */
541 /* invoke a command in a subshell */
542 void mutt_shell_escape (void)
544 char buf[LONG_STRING];
547 if (mutt_get_field (_("Shell command: "), buf, sizeof (buf), M_CMD) == 0) {
548 if (!buf[0] && Shell)
549 m_strcpy(buf, sizeof(buf), Shell);
551 CLEARLINE (LINES - 1);
554 if (mutt_system (buf) != 0 || option (OPTWAITKEY))
555 mutt_any_key_to_continue (NULL);
560 /* enter a mutt command */
561 void mutt_enter_command (void)
564 char buffer[LONG_STRING], errbuf[SHORT_STRING];
568 if (mutt_get_field (":", buffer, sizeof (buffer), M_COMMAND) != 0
572 err.dsize = sizeof (errbuf);
574 r = mutt_parse_rc_line (buffer, &token, &err);
575 p_delete(&token.data);
577 /* since errbuf could potentially contain printf() sequences in it,
578 we must call mutt_error() in this fashion so that vsprintf()
579 doesn't expect more arguments that we passed */
581 mutt_message ("%s", errbuf);
583 mutt_error ("%s", errbuf);
587 void mutt_display_address (ENVELOPE * env)
589 const char *pfx = NULL;
590 char buf[SHORT_STRING];
591 address_t *adr = NULL;
593 adr = mutt_get_address(env, &pfx);
599 * Note: We don't convert IDNA to local representation this time.
600 * That is intentional, so the user has an opportunity to copy &
601 * paste the on-the-wire form of the address to other, IDN-unable
606 rfc822_write_address (buf, sizeof (buf), adr, 0);
607 mutt_message ("%s: %s", pfx, buf);
610 static void set_copy_flags (HEADER * hdr, int decode, int decrypt,
611 int *cmflags, int *chflags)
614 *chflags = CH_UPDATE_LEN;
616 if (!decode && decrypt && (hdr->security & ENCRYPT)) {
617 if (mutt_is_multipart_encrypted (hdr->content)) {
618 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
619 *cmflags = M_CM_DECODE_PGP;
621 else if (mutt_is_application_pgp (hdr->content) & ENCRYPT)
623 else if (mutt_is_application_smime (hdr->content) & ENCRYPT) {
624 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
625 *cmflags = M_CM_DECODE_SMIME;
630 *chflags = CH_XMIT | CH_MIME | CH_TXTPLAIN;
631 *cmflags = M_CM_DECODE | M_CM_CHARCONV;
633 if (!decrypt) { /* If decode doesn't kick in for decrypt, */
634 *chflags |= CH_DECODE; /* then decode RFC 2047 headers, */
636 if (option (OPTWEED)) {
637 *chflags |= CH_WEED; /* and respect $weed. */
638 *cmflags |= M_CM_WEED;
644 int _mutt_save_message (HEADER * h, CONTEXT * ctx, int delete, int decode,
646 int cmflags, chflags;
649 set_copy_flags (h, decode, decrypt, &cmflags, &chflags);
651 if (decode || decrypt)
652 mutt_parse_mime_message (Context, h);
654 if ((rc = mutt_append_message (ctx, Context, h, cmflags, chflags)) != 0)
658 mutt_set_flag (Context, h, M_DELETE, 1);
659 if (option (OPTDELETEUNTAG))
660 mutt_set_flag (Context, h, M_TAG, 0);
661 mutt_set_flag (Context, h, M_APPENDED, 1);
666 /* returns 0 if the copy/save was successful, or -1 on error/abort */
667 int mutt_save_message (HEADER * h, int delete,
668 int decode, int decrypt, int *redraw) {
669 int i, need_buffy_cleanup;
670 int need_passphrase = 0, app = 0;
671 char prompt[SHORT_STRING], buf[_POSIX_PATH_MAX];
684 snprintf (prompt, sizeof (prompt),
685 decode ? (delete ? _("Decode-save%s to mailbox") :
686 _("Decode-copy%s to mailbox")) :
687 (decrypt ? (delete ? _("Decrypt-save%s to mailbox") :
688 _("Decrypt-copy%s to mailbox")) :
689 (delete ? _("Save%s to mailbox") : _("Copy%s to mailbox"))),
690 h ? "" : _(" tagged"));
694 need_passphrase = h->security & ENCRYPT;
696 mutt_message_hook (Context, h, M_MESSAGEHOOK);
697 mutt_default_save (buf, sizeof (buf), h);
700 /* look for the first tagged message */
702 for (i = 0; i < Context->vcount; i++) {
703 if (Context->hdrs[Context->v2r[i]]->tagged) {
704 h = Context->hdrs[Context->v2r[i]];
711 mutt_message_hook (Context, h, M_MESSAGEHOOK);
712 mutt_default_save (buf, sizeof (buf), h);
713 need_passphrase = h->security & ENCRYPT;
719 mutt_pretty_mailbox (buf);
720 if (mutt_enter_fname (prompt, buf, sizeof (buf), redraw, 0) == -1)
723 if (*redraw != REDRAW_FULL) {
725 *redraw = REDRAW_INDEX | REDRAW_STATUS;
727 *redraw = REDRAW_STATUS;
733 /* This is an undocumented feature of ELM pointed out to me by Felix von
734 * Leitner <leitner@prz.fu-berlin.de>
736 if (m_strcmp(buf, ".") == 0)
737 m_strcpy(buf, sizeof(buf), LastSaveFolder);
739 m_strcpy(LastSaveFolder, sizeof(LastSaveFolder), buf);
741 mutt_expand_path (buf, sizeof (buf));
743 /* check to make sure that this file is really the one the user wants */
744 if (mutt_save_confirm (buf, &st) != 0)
747 if (need_passphrase && (decode || decrypt)
748 && !crypt_valid_passphrase (app))
751 mutt_message (_("Copying to %s..."), buf);
753 if (Context->magic == M_IMAP && !(decode || decrypt) && mx_get_magic (buf) == M_IMAP) {
754 switch (imap_copy_messages (Context, h, buf, delete)) {
759 /* non-fatal error: fall through to fetch/append */
762 /* fatal error, abort */
768 if (mx_open_mailbox (buf, M_APPEND, &ctx) != NULL) {
770 if (_mutt_save_message (h, &ctx, delete, decode, decrypt) != 0) {
771 mx_close_mailbox (&ctx, NULL);
775 for (i = 0; i < Context->vcount; i++) {
776 if (Context->hdrs[Context->v2r[i]]->tagged) {
777 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
779 if (_mutt_save_message (Context->hdrs[Context->v2r[i]], &ctx, delete,
780 decode, decrypt) != 0) {
781 mx_close_mailbox (&ctx, NULL);
788 need_buffy_cleanup = (ctx.magic == M_MBOX || ctx.magic == M_MMDF);
790 mx_close_mailbox (&ctx, NULL);
792 if (need_buffy_cleanup) {
794 tmp = buffy_find_mailbox (buf);
795 if (tmp && tmp->new <= 0)
796 buffy_update_mailbox (tmp);
798 /* fix up the times so buffy won't get confused */
799 if (st.st_mtime > st.st_atime) {
800 ut.actime = st.st_atime;
801 ut.modtime = time (NULL);
816 void mutt_version (void)
818 mutt_message (mutt_make_version (1));
821 void mutt_edit_content_type (HEADER * h, BODY * b, FILE * fp)
823 char buf[LONG_STRING];
824 char obuf[LONG_STRING];
827 char charset[STRING];
830 short charset_changed = 0;
831 short type_changed = 0;
833 cp = parameter_getval(b->parameter, "charset");
834 m_strcpy(charset, sizeof(charset), NONULL(cp));
836 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
837 m_strcpy(obuf, sizeof(obuf), buf);
842 for (p = b->parameter; p; p = p->next) {
845 rfc822_strcpy(tmp, sizeof(tmp), p->value, MimeSpecials);
846 snprintf (buf + l, sizeof (buf) - l, "; %s=%s", p->attribute, tmp);
850 if (mutt_get_field ("Content-Type: ", buf, sizeof (buf), 0) != 0 ||
854 /* clean up previous junk */
855 parameter_list_wipe(&b->parameter);
856 p_delete(&b->subtype);
858 mutt_parse_content_type (buf, b);
861 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
862 type_changed = ascii_strcasecmp (tmp, obuf);
864 ascii_strcasecmp (charset, parameter_getval(b->parameter, "charset"));
866 /* if in send mode, check for conversion - current setting is default. */
868 if (!h && b->type == TYPETEXT && charset_changed) {
871 snprintf (tmp, sizeof (tmp), _("Convert to %s upon sending?"),
872 parameter_getval(b->parameter, "charset"));
873 if ((r = mutt_yesorno (tmp, !b->noconv)) != -1)
874 b->noconv = (r == M_NO);
877 /* inform the user */
879 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
881 mutt_message (_("Content-Type changed to %s."), tmp);
882 if (b->type == TYPETEXT && charset_changed) {
885 mutt_message (_("Character set changed to %s; %s."),
886 parameter_getval(b->parameter, "charset"),
887 b->noconv ? _("not converting") : _("converting"));
890 b->force_charset |= charset_changed ? 1 : 0;
892 if (!is_multipart(b) && b->parts)
893 body_list_wipe(&b->parts);
894 if (!mutt_is_message_type (b->type, b->subtype) && b->hdr) {
895 b->hdr->content = NULL;
896 header_delete(&b->hdr);
899 if (fp && (is_multipart(b) || mutt_is_message_type(b->type, b->subtype)))
900 mutt_parse_part (fp, b);
906 h->security |= crypt_query (b);
911 static int _mutt_check_traditional_pgp (HEADER * h, int *redraw)
916 h->security |= PGP_TRADITIONAL_CHECKED;
918 mutt_parse_mime_message (Context, h);
919 if ((msg = mx_open_message (Context, h->msgno)) == NULL)
921 if (crypt_pgp_check_traditional (msg->fp, h->content, 0)) {
922 h->security = crypt_query (h->content);
923 *redraw |= REDRAW_FULL;
927 h->security |= PGP_TRADITIONAL_CHECKED;
928 mx_close_message (&msg);
932 int mutt_check_traditional_pgp (HEADER * h, int *redraw)
937 if (h && !(h->security & PGP_TRADITIONAL_CHECKED))
938 rv = _mutt_check_traditional_pgp (h, redraw);
940 for (i = 0; i < Context->vcount; i++)
941 if (Context->hdrs[Context->v2r[i]]->tagged &&
942 !(Context->hdrs[Context->v2r[i]]->
943 security & PGP_TRADITIONAL_CHECKED))
945 _mutt_check_traditional_pgp (Context->hdrs[Context->v2r[i]], redraw)