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/menu.h>
22 #include <lib-mx/mx.h>
26 #include "recvattach.h"
31 #include "mutt_idna.h"
32 #include <sys/types.h>
36 #include <imap/imap.h>
38 /* The folder the user last saved to. Used by ci_save_message() */
39 static char LastSaveFolder[_POSIX_PATH_MAX] = "";
41 int mutt_display_message (HEADER * cur)
43 char tempfile[_POSIX_PATH_MAX], buf[LONG_STRING];
44 int cmflags = M_CM_DECODE | M_CM_DISPLAY | M_CM_CHARCONV;
46 FILE *fpfilterout = NULL;
51 snprintf (buf, sizeof (buf), "%s/%s", TYPE (cur->content),
52 cur->content->subtype);
54 mutt_parse_mime_message (Context, cur);
55 mutt_message_hook (Context, cur, M_MESSAGEHOOK);
57 fpout = m_tempfile(tempfile, sizeof(tempfile), NONULL(mod_core.tmpdir), NULL);
59 mutt_error _("Could not create temporary file!");
63 if (DisplayFilter && *DisplayFilter) {
66 filterpid = mutt_create_filter_fd (DisplayFilter, &fpout, NULL, NULL,
67 -1, fileno (fpfilterout), -1);
69 mutt_error (_("Cannot create display filter"));
70 m_fclose(&fpfilterout);
76 msg = mx_open_message (Context, cur->msgno);
77 if (msg == NULL) res = -1;
80 /* see if crytpo is needed for this message. if so, we should exit curses */
82 if (cur->security & ENCRYPT) {
83 cmflags |= M_CM_VERIFY;
85 else if (cur->security & SIGN) {
86 /* find out whether or not the verify signature */
87 if (query_quadoption2(mod_crypt.verify_sig, _("Verify PGP signature?")) ==
89 cmflags |= M_CM_VERIFY;
94 if (cmflags & M_CM_VERIFY || cur->security & ENCRYPT) {
95 crypt_invoke_message(cur->security);
98 res = _mutt_copy_message (fpout, msg->fp, cur, cur->content, cmflags,
99 (option (OPTWEED) ? (CH_WEED | CH_REORDER) : 0) |
100 CH_DECODE | CH_FROM);
101 if (res == 0 && (ferror(fpout) || feof(fpout))) {
105 mx_close_message (&msg);
108 if ((m_fclose(&fpout) != 0 && errno != EPIPE) || res == -1) {
109 mutt_error (_("Could not copy message"));
110 if (fpfilterout != NULL) {
111 mutt_wait_filter (filterpid);
112 m_fclose(&fpfilterout);
116 if (fpfilterout != NULL && mutt_wait_filter (filterpid) != 0)
117 mutt_any_key_to_continue (NULL);
119 m_fclose(&fpfilterout); /* XXX - check result? */
122 /* update crypto information for this message */
123 cur->security &= ~(GOODSIGN|BADSIGN);
124 cur->security |= crypt_query (cur->content);
126 /* Remove color cache for this message, in case there
127 are color patterns for both ~g and ~V */
130 if ((cur->security & APPLICATION_SMIME) && (cmflags & M_CM_VERIFY)) {
131 if (cur->security & GOODSIGN) {
132 if (!crypt_smime_verify_sender (cur))
133 mutt_message (_("S/MIME signature successfully verified."));
135 mutt_error (_("S/MIME certificate owner does not match sender."));
137 else if (cur->security & PARTSIGN)
139 ("Warning: Part of this message has not been signed."));
140 else if (cur->security & SIGN || cur->security & BADSIGN)
141 mutt_error (_("S/MIME signature could NOT be verified."));
144 if ((cur->security & APPLICATION_PGP) && (cmflags & M_CM_VERIFY)) {
145 if (cur->security & GOODSIGN)
146 mutt_message (_("PGP signature successfully verified."));
147 else if (cur->security & PARTSIGN)
149 ("Warning: Part of this message has not been signed."));
150 else if (cur->security & SIGN)
151 mutt_message (_("PGP signature could NOT be verified."));
160 return mutt_pager (NULL, tempfile, M_PAGER_MESSAGE, &info);
164 void ci_bounce_message (HEADER * h, int *redraw)
167 char buf[HUGE_STRING] = { 0 };
168 address_t *adr = NULL;
173 m_strcpy(prompt, sizeof(prompt), _("Bounce message to: "));
175 m_strcpy(prompt, sizeof(prompt), _("Bounce tagged messages to: "));
177 rc = mutt_get_field (prompt, buf, sizeof (buf), M_ALIAS);
179 if (option (OPTNEEDREDRAW)) {
180 unset_option (OPTNEEDREDRAW);
181 *redraw = REDRAW_FULL;
187 if (!(adr = rfc822_parse_adrlist (adr, buf))) {
188 mutt_error _("Error parsing address!");
193 adr = mutt_expand_aliases (adr);
195 if (mutt_addrlist_to_idna (adr, &err) < 0) {
196 mutt_error (_("Bad IDN: '%s'"), err);
198 address_list_wipe(&adr);
203 rfc822_addrcat(buf, sizeof (buf), adr, 1);
205 snprintf (prompt, sizeof (prompt),
206 (h ? _("Bounce message to %s") : _("Bounce messages to %s")),
209 #define extra_space (15 + 7 + 2)
210 if (m_strwidth(prompt) > getmaxx(main_w) - extra_space) {
211 mutt_format_string(prompt, sizeof(prompt), 0,
212 getmaxx(main_w) - extra_space, 0, 0, prompt,
214 m_strcat(prompt, sizeof(prompt), "...?");
216 m_strcat(prompt, sizeof(prompt), "?");
220 if (query_quadoption (OPT_BOUNCE, prompt) != M_YES) {
221 address_list_wipe(&adr);
222 mutt_message (h ? _("Message not bounced.") : _("Messages not bounced."));
226 rc = mutt_bounce_message (NULL, h, adr);
227 address_list_wipe(&adr);
228 /* If no error, or background, display message. */
229 if ((rc == 0) || (rc == S_BKG))
230 mutt_message (h ? _("Message bounced.") : _("Messages bounced."));
233 static void pipe_msg (HEADER * h, FILE * fp, int decode, int print)
236 int chflags = CH_FROM;
239 cmflags |= M_CM_DECODE | M_CM_CHARCONV;
240 chflags |= CH_DECODE | CH_REORDER;
242 if (option(OPTWEED)) {
244 cmflags |= M_CM_WEED;
249 cmflags |= M_CM_PRINTING;
251 if (decode && h->security & ENCRYPT) {
256 mutt_parse_mime_message (Context, h);
258 mutt_copy_message (fp, Context, h, cmflags, chflags);
261 /* the following code is shared between printing and piping */
262 static int _mutt_pipe_message(HEADER * h, char *cmd, int decode, int print,
263 int split, const char *sep)
271 mutt_message_hook (Context, h, M_MESSAGEHOOK);
274 mutt_parse_mime_message (Context, h);
278 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
279 mutt_perror (_("Can't create filter process"));
284 pipe_msg (h, fpout, decode, print);
286 rc = mutt_wait_filter (thepid);
287 } else { /* handle tagged messages */
289 for (i = 0; i < Context->vcount; i++)
290 if (Context->hdrs[Context->v2r[i]]->tagged) {
291 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
293 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
298 for (i = 0; i < Context->vcount; i++) {
299 if (Context->hdrs[Context->v2r[i]]->tagged) {
300 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
303 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
304 mutt_perror (_("Can't create filter process"));
308 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
309 /* add the message separator */
313 if (mutt_wait_filter (thepid) != 0)
319 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
320 mutt_perror (_("Can't create filter process"));
324 for (i = 0; i < Context->vcount; i++) {
325 if (Context->hdrs[Context->v2r[i]]->tagged) {
326 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
328 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
329 /* add the message separator */
335 if (mutt_wait_filter (thepid) != 0)
340 if (rc || option (OPTWAITKEY))
341 mutt_any_key_to_continue (NULL);
345 void mutt_pipe_message (HEADER * h)
347 char buffer[LONG_STRING];
350 if (mutt_get_field (_("Pipe to command: "), buffer, sizeof (buffer), M_CMD)
354 mutt_expand_path (buffer, sizeof (buffer));
355 _mutt_pipe_message (h, buffer,
356 option (OPTPIPEDECODE),
357 0, option (OPTPIPESPLIT), PipeSep);
360 void mutt_print_message (HEADER * h)
363 if (quadoption (OPT_PRINT) && m_strisempty(PrintCmd)) {
364 mutt_message (_("No printing command has been defined."));
368 if (query_quadoption (OPT_PRINT,
369 h ? _("Print message?") : _("Print tagged messages?"))
373 if (_mutt_pipe_message (h, PrintCmd,
374 option (OPTPRINTDECODE),
375 1, option (OPTPRINTSPLIT), "\f") == 0)
376 mutt_message (h ? _("Message printed") : _("Messages printed"));
378 mutt_message (h ? _("Message could not be printed") :
379 _("Messages could not be printed"));
383 int mutt_select_sort (int reverse)
385 int method = Sort; /* save the current method in case of abort */
387 switch (mutt_multi_choice (reverse ?
389 ("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?: ")
392 ("Sort (d)ate/(f)rm/(r)ecv/(s)ubj/t(o)/(t)hread/(u)nsort/si(z)e/s(c)ore/s(p)am?: "),
394 case -1: /* abort - don't resort */
406 Sort = SORT_RECEIVED;
417 case 6: /* (t)hread */
421 case 7: /* (u)nsort */
429 case 9: /* s(c)ore */
433 case 10: /* s(p)am */
438 Sort |= SORT_REVERSE;
440 return (Sort != method ? 0 : -1); /* no need to resort if it's the same */
443 /* invoke a command in a subshell */
444 void mutt_shell_escape (void)
446 char buf[LONG_STRING];
449 if (mutt_get_field (_("Shell command: "), buf, sizeof (buf), M_CMD) == 0) {
451 m_strcpy(buf, sizeof(buf), mod_core.shell);
455 if (mutt_system (buf) != 0 || option (OPTWAITKEY))
456 mutt_any_key_to_continue (NULL);
461 /* enter a mutt command */
462 void mutt_enter_command (void)
465 char buffer[LONG_STRING], errbuf[STRING];
469 if (mutt_get_field (":", buffer, sizeof (buffer), M_COMMAND) != 0
473 err.dsize = sizeof (errbuf);
475 r = mutt_parse_rc_line (buffer, &token, &err);
476 p_delete(&token.data);
478 /* since errbuf could potentially contain printf() sequences in it,
479 we must call mutt_error() in this fashion so that vsprintf()
480 doesn't expect more arguments that we passed */
482 mutt_message ("%s", errbuf);
484 mutt_error ("%s", errbuf);
488 void mutt_display_address (ENVELOPE * env)
490 const char *pfx = NULL;
492 address_t *adr = NULL;
494 adr = mutt_get_address(env, &pfx);
500 * Note: We don't convert IDNA to local representation this time.
501 * That is intentional, so the user has an opportunity to copy &
502 * paste the on-the-wire form of the address to other, IDN-unable
507 rfc822_addrcat(buf, sizeof (buf), adr, 0);
508 mutt_message ("%s: %s", pfx, buf);
511 static void set_copy_flags (HEADER * hdr, int decode, int decrypt,
512 int *cmflags, int *chflags)
515 *chflags = CH_UPDATE_LEN;
517 if (!decode && decrypt && (hdr->security & ENCRYPT)) {
518 if (mutt_is_multipart_encrypted (hdr->content)) {
519 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
520 *cmflags = M_CM_DECODE_PGP;
522 else if (mutt_is_application_pgp (hdr->content) & ENCRYPT)
524 else if (mutt_is_application_smime (hdr->content) & ENCRYPT) {
525 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
526 *cmflags = M_CM_DECODE_SMIME;
531 *chflags = CH_XMIT | CH_MIME | CH_TXTPLAIN;
532 *cmflags = M_CM_DECODE | M_CM_CHARCONV;
534 if (!decrypt) { /* If decode doesn't kick in for decrypt, */
535 *chflags |= CH_DECODE; /* then decode RFC 2047 headers, */
537 if (option (OPTWEED)) {
538 *chflags |= CH_WEED; /* and respect $weed. */
539 *cmflags |= M_CM_WEED;
545 int _mutt_save_message (HEADER * h, CONTEXT * ctx, int delete, int decode,
547 int cmflags, chflags;
550 set_copy_flags (h, decode, decrypt, &cmflags, &chflags);
552 if (decode || decrypt)
553 mutt_parse_mime_message (Context, h);
555 if ((rc = mutt_append_message (ctx, Context, h, cmflags, chflags)) != 0)
559 mutt_set_flag (Context, h, M_DELETE, 1);
560 if (option (OPTDELETEUNTAG))
561 mutt_set_flag (Context, h, M_TAG, 0);
562 mutt_set_flag (Context, h, M_APPENDED, 1);
567 /* returns 0 if the copy/save was successful, or -1 on error/abort */
568 int mutt_save_message (HEADER * h, int delete,
569 int decode, int decrypt, int *redraw) {
570 int i, need_buffy_cleanup;
571 int need_passphrase = 0, app = 0;
572 char prompt[STRING], buf[_POSIX_PATH_MAX];
580 snprintf (prompt, sizeof (prompt),
581 decode ? (delete ? _("Decode-save%s to mailbox") :
582 _("Decode-copy%s to mailbox")) :
583 (decrypt ? (delete ? _("Decrypt-save%s to mailbox") :
584 _("Decrypt-copy%s to mailbox")) :
585 (delete ? _("Save%s to mailbox") : _("Copy%s to mailbox"))),
586 h ? "" : _(" tagged"));
590 need_passphrase = h->security & ENCRYPT;
592 mutt_message_hook (Context, h, M_MESSAGEHOOK);
593 mutt_default_save (buf, sizeof (buf), h);
596 /* look for the first tagged message */
598 for (i = 0; i < Context->vcount; i++) {
599 if (Context->hdrs[Context->v2r[i]]->tagged) {
600 h = Context->hdrs[Context->v2r[i]];
607 mutt_message_hook (Context, h, M_MESSAGEHOOK);
608 mutt_default_save (buf, sizeof (buf), h);
609 need_passphrase = h->security & ENCRYPT;
615 mutt_pretty_mailbox (buf);
616 if (mutt_enter_fname (prompt, buf, sizeof (buf), redraw, 0) == -1)
619 if (*redraw != REDRAW_FULL) {
621 *redraw = REDRAW_INDEX | REDRAW_STATUS;
623 *redraw = REDRAW_STATUS;
629 /* This is an undocumented feature of ELM pointed out to me by Felix von
630 * Leitner <leitner@prz.fu-berlin.de>
632 if (m_strcmp(buf, ".") == 0)
633 m_strcpy(buf, sizeof(buf), LastSaveFolder);
635 m_strcpy(LastSaveFolder, sizeof(LastSaveFolder), buf);
637 mutt_expand_path (buf, sizeof (buf));
639 /* check to make sure that this file is really the one the user wants */
640 if (mutt_save_confirm (buf, &st) != 0)
643 mutt_message (_("Copying to %s..."), buf);
645 if (Context->magic == M_IMAP && !(decode || decrypt) && mx_get_magic (buf) == M_IMAP) {
646 switch (imap_copy_messages (Context, h, buf, delete)) {
651 /* non-fatal error: fall through to fetch/append */
654 /* fatal error, abort */
660 if (mx_open_mailbox (buf, M_APPEND, &ctx) != NULL) {
662 if (_mutt_save_message (h, &ctx, delete, decode, decrypt) != 0) {
663 mx_close_mailbox (&ctx, NULL);
667 for (i = 0; i < Context->vcount; i++) {
668 if (Context->hdrs[Context->v2r[i]]->tagged) {
669 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
671 if (_mutt_save_message (Context->hdrs[Context->v2r[i]], &ctx, delete,
672 decode, decrypt) != 0) {
673 mx_close_mailbox (&ctx, NULL);
680 need_buffy_cleanup = (ctx.magic == M_MBOX);
682 mx_close_mailbox (&ctx, NULL);
684 if (need_buffy_cleanup) {
685 /* fix up the times so buffy won't get confused */
686 if (st.st_mtime > st.st_atime) {
687 ut.actime = st.st_atime;
688 ut.modtime = time (NULL);
702 void mutt_version (void)
704 mutt_message (mutt_make_version());
707 void mutt_edit_content_type (HEADER * h, BODY * b, FILE * fp)
709 char buf[LONG_STRING];
710 char obuf[LONG_STRING];
713 char charset[STRING];
716 short charset_changed = 0;
717 short type_changed = 0;
719 cp = parameter_getval(b->parameter, "charset");
720 m_strcpy(charset, sizeof(charset), NONULL(cp));
722 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
723 m_strcpy(obuf, sizeof(obuf), buf);
728 for (p = b->parameter; p; p = p->next) {
731 rfc822_strcpy(tmp, sizeof(tmp), p->value, MimeSpecials);
732 snprintf (buf + l, sizeof (buf) - l, "; %s=%s", p->attribute, tmp);
736 if (mutt_get_field ("Content-Type: ", buf, sizeof (buf), 0) != 0 ||
740 /* clean up previous junk */
741 parameter_list_wipe(&b->parameter);
742 p_delete(&b->subtype);
744 mutt_parse_content_type (buf, b);
747 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
748 type_changed = ascii_strcasecmp (tmp, obuf);
750 ascii_strcasecmp (charset, parameter_getval(b->parameter, "charset"));
752 /* if in send mode, check for conversion - current setting is default. */
754 if (!h && b->type == TYPETEXT && charset_changed) {
757 snprintf (tmp, sizeof (tmp), _("Convert to %s upon sending?"),
758 parameter_getval(b->parameter, "charset"));
759 if ((r = mutt_yesorno (tmp, !b->noconv)) != -1)
760 b->noconv = (r == M_NO);
763 /* inform the user */
765 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
767 mutt_message (_("Content-Type changed to %s."), tmp);
768 if (b->type == TYPETEXT && charset_changed) {
771 mutt_message (_("Character set changed to %s; %s."),
772 parameter_getval(b->parameter, "charset"),
773 b->noconv ? _("not converting") : _("converting"));
776 b->force_charset |= charset_changed ? 1 : 0;
778 if (!is_multipart(b) && b->parts)
779 body_list_wipe(&b->parts);
780 if (!mutt_is_message_type(b) && b->hdr) {
781 b->hdr->content = NULL;
782 header_delete(&b->hdr);
785 if (fp && (is_multipart(b) || mutt_is_message_type(b)))
786 mutt_parse_part (fp, b);
792 h->security |= crypt_query (b);