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 CLEARLINE(main_w, LINES - 1);
224 mutt_message (h ? _("Message not bounced.") : _("Messages not bounced."));
228 CLEARLINE(main_w, LINES - 1);
230 rc = mutt_bounce_message (NULL, h, adr);
231 address_list_wipe(&adr);
232 /* If no error, or background, display message. */
233 if ((rc == 0) || (rc == S_BKG))
234 mutt_message (h ? _("Message bounced.") : _("Messages bounced."));
237 static void pipe_msg (HEADER * h, FILE * fp, int decode, int print)
240 int chflags = CH_FROM;
243 cmflags |= M_CM_DECODE | M_CM_CHARCONV;
244 chflags |= CH_DECODE | CH_REORDER;
246 if (option(OPTWEED)) {
248 cmflags |= M_CM_WEED;
253 cmflags |= M_CM_PRINTING;
255 if (decode && h->security & ENCRYPT) {
260 mutt_parse_mime_message (Context, h);
262 mutt_copy_message (fp, Context, h, cmflags, chflags);
265 /* the following code is shared between printing and piping */
266 static int _mutt_pipe_message(HEADER * h, char *cmd, int decode, int print,
267 int split, const char *sep)
275 mutt_message_hook (Context, h, M_MESSAGEHOOK);
278 mutt_parse_mime_message (Context, h);
282 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
283 mutt_perror (_("Can't create filter process"));
288 pipe_msg (h, fpout, decode, print);
290 rc = mutt_wait_filter (thepid);
291 } else { /* handle tagged messages */
293 for (i = 0; i < Context->vcount; i++)
294 if (Context->hdrs[Context->v2r[i]]->tagged) {
295 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
297 mutt_parse_mime_message (Context, Context->hdrs[Context->v2r[i]]);
302 for (i = 0; i < Context->vcount; i++) {
303 if (Context->hdrs[Context->v2r[i]]->tagged) {
304 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
307 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
308 mutt_perror (_("Can't create filter process"));
312 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
313 /* add the message separator */
317 if (mutt_wait_filter (thepid) != 0)
323 if ((thepid = mutt_create_filter (cmd, &fpout, NULL, NULL)) < 0) {
324 mutt_perror (_("Can't create filter process"));
328 for (i = 0; i < Context->vcount; i++) {
329 if (Context->hdrs[Context->v2r[i]]->tagged) {
330 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
332 pipe_msg (Context->hdrs[Context->v2r[i]], fpout, decode, print);
333 /* add the message separator */
339 if (mutt_wait_filter (thepid) != 0)
344 if (rc || option (OPTWAITKEY))
345 mutt_any_key_to_continue (NULL);
349 void mutt_pipe_message (HEADER * h)
351 char buffer[LONG_STRING];
354 if (mutt_get_field (_("Pipe to command: "), buffer, sizeof (buffer), M_CMD)
358 mutt_expand_path (buffer, sizeof (buffer));
359 _mutt_pipe_message (h, buffer,
360 option (OPTPIPEDECODE),
361 0, option (OPTPIPESPLIT), PipeSep);
364 void mutt_print_message (HEADER * h)
367 if (quadoption (OPT_PRINT) && m_strisempty(PrintCmd)) {
368 mutt_message (_("No printing command has been defined."));
372 if (query_quadoption (OPT_PRINT,
373 h ? _("Print message?") : _("Print tagged messages?"))
377 if (_mutt_pipe_message (h, PrintCmd,
378 option (OPTPRINTDECODE),
379 1, option (OPTPRINTSPLIT), "\f") == 0)
380 mutt_message (h ? _("Message printed") : _("Messages printed"));
382 mutt_message (h ? _("Message could not be printed") :
383 _("Messages could not be printed"));
387 int mutt_select_sort (int reverse)
389 int method = Sort; /* save the current method in case of abort */
391 switch (mutt_multi_choice (reverse ?
393 ("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?: ")
396 ("Sort (d)ate/(f)rm/(r)ecv/(s)ubj/t(o)/(t)hread/(u)nsort/si(z)e/s(c)ore/s(p)am?: "),
398 case -1: /* abort - don't resort */
410 Sort = SORT_RECEIVED;
421 case 6: /* (t)hread */
425 case 7: /* (u)nsort */
433 case 9: /* s(c)ore */
437 case 10: /* s(p)am */
442 Sort |= SORT_REVERSE;
444 return (Sort != method ? 0 : -1); /* no need to resort if it's the same */
447 /* invoke a command in a subshell */
448 void mutt_shell_escape (void)
450 char buf[LONG_STRING];
453 if (mutt_get_field (_("Shell command: "), buf, sizeof (buf), M_CMD) == 0) {
455 m_strcpy(buf, sizeof(buf), mod_core.shell);
457 CLEARLINE(main_w, LINES - 1);
460 if (mutt_system (buf) != 0 || option (OPTWAITKEY))
461 mutt_any_key_to_continue (NULL);
466 /* enter a mutt command */
467 void mutt_enter_command (void)
470 char buffer[LONG_STRING], errbuf[STRING];
474 if (mutt_get_field (":", buffer, sizeof (buffer), M_COMMAND) != 0
478 err.dsize = sizeof (errbuf);
480 r = mutt_parse_rc_line (buffer, &token, &err);
481 p_delete(&token.data);
483 /* since errbuf could potentially contain printf() sequences in it,
484 we must call mutt_error() in this fashion so that vsprintf()
485 doesn't expect more arguments that we passed */
487 mutt_message ("%s", errbuf);
489 mutt_error ("%s", errbuf);
493 void mutt_display_address (ENVELOPE * env)
495 const char *pfx = NULL;
497 address_t *adr = NULL;
499 adr = mutt_get_address(env, &pfx);
505 * Note: We don't convert IDNA to local representation this time.
506 * That is intentional, so the user has an opportunity to copy &
507 * paste the on-the-wire form of the address to other, IDN-unable
512 rfc822_addrcat(buf, sizeof (buf), adr, 0);
513 mutt_message ("%s: %s", pfx, buf);
516 static void set_copy_flags (HEADER * hdr, int decode, int decrypt,
517 int *cmflags, int *chflags)
520 *chflags = CH_UPDATE_LEN;
522 if (!decode && decrypt && (hdr->security & ENCRYPT)) {
523 if (mutt_is_multipart_encrypted (hdr->content)) {
524 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
525 *cmflags = M_CM_DECODE_PGP;
527 else if (mutt_is_application_pgp (hdr->content) & ENCRYPT)
529 else if (mutt_is_application_smime (hdr->content) & ENCRYPT) {
530 *chflags = CH_NONEWLINE | CH_XMIT | CH_MIME;
531 *cmflags = M_CM_DECODE_SMIME;
536 *chflags = CH_XMIT | CH_MIME | CH_TXTPLAIN;
537 *cmflags = M_CM_DECODE | M_CM_CHARCONV;
539 if (!decrypt) { /* If decode doesn't kick in for decrypt, */
540 *chflags |= CH_DECODE; /* then decode RFC 2047 headers, */
542 if (option (OPTWEED)) {
543 *chflags |= CH_WEED; /* and respect $weed. */
544 *cmflags |= M_CM_WEED;
550 int _mutt_save_message (HEADER * h, CONTEXT * ctx, int delete, int decode,
552 int cmflags, chflags;
555 set_copy_flags (h, decode, decrypt, &cmflags, &chflags);
557 if (decode || decrypt)
558 mutt_parse_mime_message (Context, h);
560 if ((rc = mutt_append_message (ctx, Context, h, cmflags, chflags)) != 0)
564 mutt_set_flag (Context, h, M_DELETE, 1);
565 if (option (OPTDELETEUNTAG))
566 mutt_set_flag (Context, h, M_TAG, 0);
567 mutt_set_flag (Context, h, M_APPENDED, 1);
572 /* returns 0 if the copy/save was successful, or -1 on error/abort */
573 int mutt_save_message (HEADER * h, int delete,
574 int decode, int decrypt, int *redraw) {
575 int i, need_buffy_cleanup;
576 int need_passphrase = 0, app = 0;
577 char prompt[STRING], buf[_POSIX_PATH_MAX];
585 snprintf (prompt, sizeof (prompt),
586 decode ? (delete ? _("Decode-save%s to mailbox") :
587 _("Decode-copy%s to mailbox")) :
588 (decrypt ? (delete ? _("Decrypt-save%s to mailbox") :
589 _("Decrypt-copy%s to mailbox")) :
590 (delete ? _("Save%s to mailbox") : _("Copy%s to mailbox"))),
591 h ? "" : _(" tagged"));
595 need_passphrase = h->security & ENCRYPT;
597 mutt_message_hook (Context, h, M_MESSAGEHOOK);
598 mutt_default_save (buf, sizeof (buf), h);
601 /* look for the first tagged message */
603 for (i = 0; i < Context->vcount; i++) {
604 if (Context->hdrs[Context->v2r[i]]->tagged) {
605 h = Context->hdrs[Context->v2r[i]];
612 mutt_message_hook (Context, h, M_MESSAGEHOOK);
613 mutt_default_save (buf, sizeof (buf), h);
614 need_passphrase = h->security & ENCRYPT;
620 mutt_pretty_mailbox (buf);
621 if (mutt_enter_fname (prompt, buf, sizeof (buf), redraw, 0) == -1)
624 if (*redraw != REDRAW_FULL) {
626 *redraw = REDRAW_INDEX | REDRAW_STATUS;
628 *redraw = REDRAW_STATUS;
634 /* This is an undocumented feature of ELM pointed out to me by Felix von
635 * Leitner <leitner@prz.fu-berlin.de>
637 if (m_strcmp(buf, ".") == 0)
638 m_strcpy(buf, sizeof(buf), LastSaveFolder);
640 m_strcpy(LastSaveFolder, sizeof(LastSaveFolder), buf);
642 mutt_expand_path (buf, sizeof (buf));
644 /* check to make sure that this file is really the one the user wants */
645 if (mutt_save_confirm (buf, &st) != 0)
648 mutt_message (_("Copying to %s..."), buf);
650 if (Context->magic == M_IMAP && !(decode || decrypt) && mx_get_magic (buf) == M_IMAP) {
651 switch (imap_copy_messages (Context, h, buf, delete)) {
656 /* non-fatal error: fall through to fetch/append */
659 /* fatal error, abort */
665 if (mx_open_mailbox (buf, M_APPEND, &ctx) != NULL) {
667 if (_mutt_save_message (h, &ctx, delete, decode, decrypt) != 0) {
668 mx_close_mailbox (&ctx, NULL);
672 for (i = 0; i < Context->vcount; i++) {
673 if (Context->hdrs[Context->v2r[i]]->tagged) {
674 mutt_message_hook (Context, Context->hdrs[Context->v2r[i]],
676 if (_mutt_save_message (Context->hdrs[Context->v2r[i]], &ctx, delete,
677 decode, decrypt) != 0) {
678 mx_close_mailbox (&ctx, NULL);
685 need_buffy_cleanup = (ctx.magic == M_MBOX);
687 mx_close_mailbox (&ctx, NULL);
689 if (need_buffy_cleanup) {
690 /* fix up the times so buffy won't get confused */
691 if (st.st_mtime > st.st_atime) {
692 ut.actime = st.st_atime;
693 ut.modtime = time (NULL);
707 void mutt_version (void)
709 mutt_message (mutt_make_version());
712 void mutt_edit_content_type (HEADER * h, BODY * b, FILE * fp)
714 char buf[LONG_STRING];
715 char obuf[LONG_STRING];
718 char charset[STRING];
721 short charset_changed = 0;
722 short type_changed = 0;
724 cp = parameter_getval(b->parameter, "charset");
725 m_strcpy(charset, sizeof(charset), NONULL(cp));
727 snprintf (buf, sizeof (buf), "%s/%s", TYPE (b), b->subtype);
728 m_strcpy(obuf, sizeof(obuf), buf);
733 for (p = b->parameter; p; p = p->next) {
736 rfc822_strcpy(tmp, sizeof(tmp), p->value, MimeSpecials);
737 snprintf (buf + l, sizeof (buf) - l, "; %s=%s", p->attribute, tmp);
741 if (mutt_get_field ("Content-Type: ", buf, sizeof (buf), 0) != 0 ||
745 /* clean up previous junk */
746 parameter_list_wipe(&b->parameter);
747 p_delete(&b->subtype);
749 mutt_parse_content_type (buf, b);
752 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
753 type_changed = ascii_strcasecmp (tmp, obuf);
755 ascii_strcasecmp (charset, parameter_getval(b->parameter, "charset"));
757 /* if in send mode, check for conversion - current setting is default. */
759 if (!h && b->type == TYPETEXT && charset_changed) {
762 snprintf (tmp, sizeof (tmp), _("Convert to %s upon sending?"),
763 parameter_getval(b->parameter, "charset"));
764 if ((r = mutt_yesorno (tmp, !b->noconv)) != -1)
765 b->noconv = (r == M_NO);
768 /* inform the user */
770 snprintf (tmp, sizeof (tmp), "%s/%s", TYPE (b), NONULL (b->subtype));
772 mutt_message (_("Content-Type changed to %s."), tmp);
773 if (b->type == TYPETEXT && charset_changed) {
776 mutt_message (_("Character set changed to %s; %s."),
777 parameter_getval(b->parameter, "charset"),
778 b->noconv ? _("not converting") : _("converting"));
781 b->force_charset |= charset_changed ? 1 : 0;
783 if (!is_multipart(b) && b->parts)
784 body_list_wipe(&b->parts);
785 if (!mutt_is_message_type(b) && b->hdr) {
786 b->hdr->content = NULL;
787 header_delete(&b->hdr);
790 if (fp && (is_multipart(b) || mutt_is_message_type(b)))
791 mutt_parse_part (fp, b);
797 h->security |= crypt_query (b);