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/lib-ui.h>
21 #include <lib-ui/enter.h>
22 #include <lib-ui/menu.h>
23 #include <lib-mx/mx.h>
27 #include "recvattach.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 cmflags = M_CM_DECODE | M_CM_DISPLAY | M_CM_CHARCONV;
47 FILE *fpfilterout = NULL;
52 snprintf (buf, sizeof (buf), "%s/%s", TYPE (cur->content),
53 cur->content->subtype);
55 mutt_parse_mime_message (Context, cur);
56 mutt_message_hook (Context, cur, M_MESSAGEHOOK);
58 fpout = m_tempfile(tempfile, sizeof(tempfile), NONULL(mod_core.tmpdir), NULL);
60 mutt_error _("Could not create temporary file!");
64 if (DisplayFilter && *DisplayFilter) {
67 filterpid = mutt_create_filter_fd (DisplayFilter, &fpout, NULL, NULL,
68 -1, fileno (fpfilterout), -1);
70 mutt_error (_("Cannot create display filter"));
71 m_fclose(&fpfilterout);
77 msg = mx_open_message (Context, cur->msgno);
78 if (msg == NULL) res = -1;
81 /* see if crytpo is needed for this message. if so, we should exit curses */
83 if (cur->security & ENCRYPT) {
84 cmflags |= M_CM_VERIFY;
86 else if (cur->security & SIGN) {
87 /* find out whether or not the verify signature */
88 if (query_quadoption2(mod_crypt.verify_sig, _("Verify PGP signature?")) ==
90 cmflags |= M_CM_VERIFY;
95 if (cmflags & M_CM_VERIFY || cur->security & ENCRYPT) {
96 crypt_invoke_message(cur->security);
99 res = _mutt_copy_message (fpout, msg->fp, cur, cur->content, cmflags,
100 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
101 CH_DECODE | CH_FROM);
102 if (res == 0 && (ferror(fpout) || feof(fpout))) {
106 mx_close_message (&msg);
109 if ((m_fclose(&fpout) != 0 && errno != EPIPE) || res == -1) {
110 mutt_error (_("Could not copy message"));
111 if (fpfilterout != NULL) {
112 mutt_wait_filter (filterpid);
113 m_fclose(&fpfilterout);
117 if (fpfilterout != NULL && mutt_wait_filter (filterpid) != 0)
118 mutt_any_key_to_continue (NULL);
120 m_fclose(&fpfilterout); /* XXX - check result? */
123 /* update crypto information for this message */
124 cur->security &= ~(GOODSIGN|BADSIGN);
125 cur->security |= crypt_query (cur->content);
127 /* Remove color cache for this message, in case there
128 are color patterns for both ~g and ~V */
131 if ((cur->security & APPLICATION_SMIME) && (cmflags & M_CM_VERIFY)) {
132 if (cur->security & GOODSIGN) {
133 if (!crypt_smime_verify_sender (cur))
134 mutt_message (_("S/MIME signature successfully verified."));
136 mutt_error (_("S/MIME certificate owner does not match sender."));
138 else if (cur->security & PARTSIGN)
140 ("Warning: Part of this message has not been signed."));
141 else if (cur->security & SIGN || cur->security & BADSIGN)
142 mutt_error (_("S/MIME signature could NOT be verified."));
145 if ((cur->security & APPLICATION_PGP) && (cmflags & M_CM_VERIFY)) {
146 if (cur->security & GOODSIGN)
147 mutt_message (_("PGP signature successfully verified."));
148 else if (cur->security & PARTSIGN)
150 ("Warning: Part of this message has not been signed."));
151 else if (cur->security & SIGN)
152 mutt_message (_("PGP signature could NOT be verified."));
161 return mutt_pager (NULL, tempfile, M_PAGER_MESSAGE, &info);
165 void ci_bounce_message (HEADER * h, int *redraw)
168 char buf[HUGE_STRING] = { 0 };
169 address_t *adr = NULL;
174 m_strcpy(prompt, sizeof(prompt), _("Bounce message to: "));
176 m_strcpy(prompt, sizeof(prompt), _("Bounce tagged messages to: "));
178 rc = mutt_get_field (prompt, buf, sizeof (buf), M_ALIAS);
180 if (option (OPTNEEDREDRAW)) {
181 unset_option (OPTNEEDREDRAW);
182 *redraw = REDRAW_FULL;
188 if (!(adr = rfc822_parse_adrlist (adr, buf))) {
189 mutt_error _("Error parsing address!");
194 adr = mutt_expand_aliases (adr);
196 if (mutt_addrlist_to_idna (adr, &err) < 0) {
197 mutt_error (_("Bad IDN: '%s'"), err);
199 address_list_wipe(&adr);
204 rfc822_addrcat(buf, sizeof (buf), adr, 1);
206 snprintf (prompt, sizeof (prompt),
207 (h ? _("Bounce message to %s") : _("Bounce messages to %s")),
210 #define extra_space (15 + 7 + 2)
211 if (m_strwidth(prompt) > getmaxx(main_w) - extra_space) {
212 mutt_format_string(prompt, sizeof(prompt), 0,
213 getmaxx(main_w) - extra_space, 0, 0, prompt,
215 m_strcat(prompt, sizeof(prompt), "...?");
217 m_strcat(prompt, sizeof(prompt), "?");
221 if (query_quadoption (OPT_BOUNCE, prompt) != M_YES) {
222 address_list_wipe(&adr);
223 mutt_message (h ? _("Message not bounced.") : _("Messages not bounced."));
227 rc = mutt_bounce_message (NULL, h, adr);
228 address_list_wipe(&adr);
229 /* If no error, or background, display message. */
230 if ((rc == 0) || (rc == S_BKG))
231 mutt_message (h ? _("Message bounced.") : _("Messages bounced."));
234 static void pipe_msg (HEADER * h, FILE * fp, int decode, int print)
237 int chflags = CH_FROM;
240 cmflags |= M_CM_DECODE | M_CM_CHARCONV;
241 chflags |= CH_DECODE | CH_REORDER;
243 if (option(OPTWEED)) {
245 cmflags |= M_CM_WEED;
250 cmflags |= M_CM_PRINTING;
252 if (decode && h->security & ENCRYPT) {
257 mutt_parse_mime_message (Context, h);
259 mutt_copy_message (fp, Context, h, cmflags, chflags);
262 /* the following code is shared between printing and piping */
263 static int _mutt_pipe_message(HEADER * h, char *cmd, int decode, int print,
264 int split, const char *sep)
272 mutt_message_hook (Context, h, M_MESSAGEHOOK);
275 mutt_parse_mime_message (Context, h);
279 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
280 mutt_perror (_("Can't create filter process"));
285 pipe_msg (h, fpout, decode, print);
287 rc = mutt_wait_filter (thepid);
288 } else { /* handle tagged messages */
290 for (i = 0; i < Context->vcount; i++)
291 if (Context->hdrs[Context->v2r[i]]->tagged) {
292 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
294 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
299 for (i = 0; i < Context->vcount; i++) {
300 if (Context->hdrs[Context->v2r[i]]->tagged) {
301 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
304 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
305 mutt_perror (_("Can't create filter process"));
309 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
310 /* add the message separator */
314 if (mutt_wait_filter (thepid) != 0)
320 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
321 mutt_perror (_("Can't create filter process"));
325 for (i = 0; i < Context->vcount; i++) {
326 if (Context->hdrs[Context->v2r[i]]->tagged) {
327 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
329 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
330 /* add the message separator */
336 if (mutt_wait_filter (thepid) != 0)
341 if (rc || option (OPTWAITKEY))
342 mutt_any_key_to_continue (NULL);
346 void mutt_pipe_message (HEADER * h)
348 char buffer[LONG_STRING];
351 if (mutt_get_field (_("Pipe to command: "), buffer, sizeof (buffer), M_CMD)
355 mutt_expand_path (buffer, sizeof (buffer));
356 _mutt_pipe_message (h, buffer,
357 option (OPTPIPEDECODE),
358 0, option (OPTPIPESPLIT), PipeSep);
361 void mutt_print_message (HEADER * h)
364 if (quadoption (OPT_PRINT) && m_strisempty(PrintCmd)) {
365 mutt_message (_("No printing command has been defined."));
369 if (query_quadoption (OPT_PRINT,
370 h ? _("Print message?") : _("Print tagged messages?"))
374 if (_mutt_pipe_message (h, PrintCmd,
375 option (OPTPRINTDECODE),
376 1, option (OPTPRINTSPLIT), "\f") == 0)
377 mutt_message (h ? _("Message printed") : _("Messages printed"));
379 mutt_message (h ? _("Message could not be printed") :
380 _("Messages could not be printed"));
384 int mutt_select_sort (int reverse)
386 int method = Sort; /* save the current method in case of abort */
388 switch (mutt_multi_choice (reverse ?
390 ("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?: ")
393 ("Sort (d)ate/(f)rm/(r)ecv/(s)ubj/t(o)/(t)hread/(u)nsort/si(z)e/s(c)ore/s(p)am?: "),
395 case -1: /* abort - don't resort */
407 Sort = SORT_RECEIVED;
418 case 6: /* (t)hread */
422 case 7: /* (u)nsort */
430 case 9: /* s(c)ore */
434 case 10: /* s(p)am */
439 Sort |= SORT_REVERSE;
441 return (Sort != method ? 0 : -1); /* no need to resort if it's the same */
444 /* invoke a command in a subshell */
445 void mutt_shell_escape (void)
447 char buf[LONG_STRING];
450 if (mutt_get_field (_("Shell command: "), buf, sizeof (buf), M_CMD) == 0) {
452 m_strcpy(buf, sizeof(buf), mod_core.shell);
456 if (mutt_system (buf) != 0 || option (OPTWAITKEY))
457 mutt_any_key_to_continue (NULL);
462 /* enter a mutt command */
463 void mutt_enter_command (void)
466 char buffer[LONG_STRING], errbuf[STRING];
470 if (mutt_get_field (":", buffer, sizeof (buffer), M_COMMAND) != 0
474 err.dsize = sizeof (errbuf);
476 r = mutt_parse_rc_line (buffer, &token, &err);
477 p_delete(&token.data);
479 /* since errbuf could potentially contain printf() sequences in it,
480 we must call mutt_error() in this fashion so that vsprintf()
481 doesn't expect more arguments that we passed */
483 mutt_message ("%s", errbuf);
485 mutt_error ("%s", errbuf);
489 void mutt_display_address (ENVELOPE * env)
491 const char *pfx = NULL;
493 address_t *adr = NULL;
495 adr = mutt_get_address(env, &pfx);
501 * Note: We don't convert IDNA to local representation this time.
502 * That is intentional, so the user has an opportunity to copy &
503 * paste the on-the-wire form of the address to other, IDN-unable
508 rfc822_addrcat(buf, sizeof (buf), adr, 0);
509 mutt_message ("%s: %s", pfx, buf);
512 static void set_copy_flags (HEADER * hdr, int decode, int decrypt,
513 int *cmflags, int *chflags)
516 *chflags = CH_UPDATE_LEN;
518 if (!decode && decrypt && (hdr->security & ENCRYPT)) {
519 if (mutt_is_multipart_encrypted (hdr->content)) {
520 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
521 *cmflags = M_CM_DECODE_PGP;
523 else if (mutt_is_application_pgp (hdr->content) & ENCRYPT)
525 else if (mutt_is_application_smime (hdr->content) & ENCRYPT) {
526 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
527 *cmflags = M_CM_DECODE_SMIME;
532 *chflags = CH_XMIT | CH_MIME | CH_TXTPLAIN;
533 *cmflags = M_CM_DECODE | M_CM_CHARCONV;
535 if (!decrypt) { /* If decode doesn't kick in for decrypt, */
536 *chflags |= CH_DECODE; /* then decode RFC 2047 headers, */
538 if (option (OPTWEED)) {
539 *chflags |= CH_WEED; /* and respect $weed. */
540 *cmflags |= M_CM_WEED;
546 int _mutt_save_message (HEADER * h, CONTEXT * ctx, int delete, int decode,
548 int cmflags, chflags;
551 set_copy_flags (h, decode, decrypt, &cmflags, &chflags);
553 if (decode || decrypt)
554 mutt_parse_mime_message (Context, h);
556 if ((rc = mutt_append_message (ctx, Context, h, cmflags, chflags)) != 0)
560 mutt_set_flag (Context, h, M_DELETE, 1);
561 if (option (OPTDELETEUNTAG))
562 mutt_set_flag (Context, h, M_TAG, 0);
563 mutt_set_flag (Context, h, M_APPENDED, 1);
568 /* returns 0 if the copy/save was successful, or -1 on error/abort */
569 int mutt_save_message (HEADER * h, int delete,
570 int decode, int decrypt, int *redraw) {
571 int i, need_buffy_cleanup;
572 int need_passphrase = 0, app = 0;
573 char prompt[STRING], buf[_POSIX_PATH_MAX];
581 snprintf (prompt, sizeof (prompt),
582 decode ? (delete ? _("Decode-save%s to mailbox") :
583 _("Decode-copy%s to mailbox")) :
584 (decrypt ? (delete ? _("Decrypt-save%s to mailbox") :
585 _("Decrypt-copy%s to mailbox")) :
586 (delete ? _("Save%s to mailbox") : _("Copy%s to mailbox"))),
587 h ? "" : _(" tagged"));
591 need_passphrase = h->security & ENCRYPT;
593 mutt_message_hook (Context, h, M_MESSAGEHOOK);
594 mutt_default_save (buf, sizeof (buf), h);
597 /* look for the first tagged message */
599 for (i = 0; i < Context->vcount; i++) {
600 if (Context->hdrs[Context->v2r[i]]->tagged) {
601 h = Context->hdrs[Context->v2r[i]];
608 mutt_message_hook (Context, h, M_MESSAGEHOOK);
609 mutt_default_save (buf, sizeof (buf), h);
610 need_passphrase = h->security & ENCRYPT;
616 mutt_pretty_mailbox (buf);
617 if (mutt_enter_fname (prompt, buf, sizeof (buf), redraw, 0) == -1)
620 if (*redraw != REDRAW_FULL) {
622 *redraw = REDRAW_INDEX | REDRAW_STATUS;
624 *redraw = REDRAW_STATUS;
630 /* This is an undocumented feature of ELM pointed out to me by Felix von
631 * Leitner <leitner@prz.fu-berlin.de>
633 if (m_strcmp(buf, ".") == 0)
634 m_strcpy(buf, sizeof(buf), LastSaveFolder);
636 m_strcpy(LastSaveFolder, sizeof(LastSaveFolder), buf);
638 mutt_expand_path (buf, sizeof (buf));
640 /* check to make sure that this file is really the one the user wants */
641 if (mutt_save_confirm (buf, &st) != 0)
644 mutt_message (_("Copying to %s..."), buf);
646 if (Context->magic == M_IMAP && !(decode || decrypt) && mx_get_magic (buf) == M_IMAP) {
647 switch (imap_copy_messages (Context, h, buf, delete)) {
652 /* non-fatal error: fall through to fetch/append */
655 /* fatal error, abort */
661 if (mx_open_mailbox (buf, M_APPEND, &ctx) != NULL) {
663 if (_mutt_save_message (h, &ctx, delete, decode, decrypt) != 0) {
664 mx_close_mailbox (&ctx, NULL);
668 for (i = 0; i < Context->vcount; i++) {
669 if (Context->hdrs[Context->v2r[i]]->tagged) {
670 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
672 if (_mutt_save_message (Context->hdrs[Context->v2r[i]], &ctx, delete,
673 decode, decrypt) != 0) {
674 mx_close_mailbox (&ctx, NULL);
681 need_buffy_cleanup = (ctx.magic == M_MBOX);
683 mx_close_mailbox (&ctx, NULL);
685 if (need_buffy_cleanup) {
686 /* fix up the times so buffy won't get confused */
687 if (st.st_mtime > st.st_atime) {
688 ut.actime = st.st_atime;
689 ut.modtime = time (NULL);
703 void mutt_version (void)
705 mutt_message (mutt_make_version());
708 void mutt_edit_content_type (HEADER * h, BODY * b, FILE * fp)
710 char buf[LONG_STRING];
711 char obuf[LONG_STRING];
714 char charset[STRING];
717 short charset_changed = 0;
718 short type_changed = 0;
720 cp = parameter_getval(b->parameter, "charset");
721 m_strcpy(charset, sizeof(charset), NONULL(cp));
723 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
724 m_strcpy(obuf, sizeof(obuf), buf);
729 for (p = b->parameter; p; p = p->next) {
732 rfc822_strcpy(tmp, sizeof(tmp), p->value, MimeSpecials);
733 snprintf (buf + l, sizeof (buf) - l, "; %s=%s", p->attribute, tmp);
737 if (mutt_get_field ("Content-Type: ", buf, sizeof (buf), 0) != 0 ||
741 /* clean up previous junk */
742 parameter_list_wipe(&b->parameter);
743 p_delete(&b->subtype);
745 mutt_parse_content_type (buf, b);
748 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
749 type_changed = ascii_strcasecmp (tmp, obuf);
751 ascii_strcasecmp (charset, parameter_getval(b->parameter, "charset"));
753 /* if in send mode, check for conversion - current setting is default. */
755 if (!h && b->type == TYPETEXT && charset_changed) {
758 snprintf (tmp, sizeof (tmp), _("Convert to %s upon sending?"),
759 parameter_getval(b->parameter, "charset"));
760 if ((r = mutt_yesorno (tmp, !b->noconv)) != -1)
761 b->noconv = (r == M_NO);
764 /* inform the user */
766 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
768 mutt_message (_("Content-Type changed to %s."), tmp);
769 if (b->type == TYPETEXT && charset_changed) {
772 mutt_message (_("Character set changed to %s; %s."),
773 parameter_getval(b->parameter, "charset"),
774 b->noconv ? _("not converting") : _("converting"));
777 b->force_charset |= charset_changed ? 1 : 0;
779 if (!is_multipart(b) && b->parts)
780 body_list_wipe(&b->parts);
781 if (!mutt_is_message_type(b) && b->hdr) {
782 b->hdr->content = NULL;
783 header_delete(&b->hdr);
786 if (fp && (is_multipart(b) || mutt_is_message_type(b)))
787 mutt_parse_part (fp, b);
793 h->security |= crypt_query (b);