2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
4 * Copyright (C) 1999-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.
15 #include <lib-lib/mem.h>
16 #include <lib-lib/str.h>
17 #include <lib-lib/ascii.h>
18 #include <lib-lib/macros.h>
23 #include "mutt_menu.h"
32 #include "imap/mx_imap.h"
34 #include "mutt_crypt.h"
36 #include "lib/debug.h"
43 static struct mapping_t PostponeHelp[] = {
44 {N_("Exit"), OP_EXIT},
45 {N_("Del"), OP_DELETE},
46 {N_("Undel"), OP_UNDELETE},
47 {N_("Help"), OP_HELP},
53 static short PostCount = 0;
54 static CONTEXT *PostContext = NULL;
55 static short UpdateNumPostponed = 0;
57 /* Return the number of postponed messages.
58 * if force is 0, use a cached value if it is costly to get a fresh
59 * count (IMAP) - else check.
61 int mutt_num_postponed (int force)
66 static time_t LastModify = 0;
67 static char *OldPostponed = NULL;
69 if (UpdateNumPostponed) {
70 UpdateNumPostponed = 0;
74 if (Postponed != OldPostponed) {
75 OldPostponed = Postponed;
84 /* LastModify is useless for IMAP */
85 if (imap_is_magic (Postponed, NULL) == M_IMAP) {
89 newpc = imap_mailbox_check (Postponed, 0);
92 debug_print (2, ("%d postponed IMAP messages found.\n", PostCount));
95 debug_print (2, ("using old IMAP postponed count.\n"));
101 if (stat (Postponed, &st) == -1) {
107 if (S_ISDIR (st.st_mode)) {
108 /* if we have a maildir mailbox, we need to stat the "new" dir */
110 char buf[_POSIX_PATH_MAX];
112 snprintf (buf, sizeof (buf), "%s/new", Postponed);
113 if (access (buf, F_OK) == 0 && stat (buf, &st) == -1) {
120 if (LastModify < st.st_mtime) {
122 int optnews = option (OPTNEWS);
124 LastModify = st.st_mtime;
126 if (access (Postponed, R_OK | F_OK) != 0)
127 return (PostCount = 0);
130 unset_option (OPTNEWS);
132 if (mx_open_mailbox (Postponed, M_NOSORT | M_QUIET, &ctx) == NULL)
135 PostCount = ctx.msgcount;
136 mx_fastclose_mailbox (&ctx);
139 set_option (OPTNEWS);
146 void mutt_update_num_postponed (void)
148 UpdateNumPostponed = 1;
151 static void post_entry (char *s, size_t slen, MUTTMENU * menu, int entry)
153 CONTEXT *ctx = (CONTEXT *) menu->data;
155 _mutt_make_string (s, slen, NONULL (HdrFmt), ctx, ctx->hdrs[entry],
156 M_FORMAT_ARROWCURSOR);
159 static HEADER *select_msg (void)
162 int i, done = 0, r = -1;
163 char helpstr[SHORT_STRING];
166 menu = mutt_new_menu ();
167 menu->make_entry = post_entry;
168 menu->menu = MENU_POST;
169 menu->max = PostContext->msgcount;
170 menu->title = _("Postponed Messages");
171 menu->data = PostContext;
173 mutt_compile_help (helpstr, sizeof (helpstr), MENU_POST, PostponeHelp);
175 /* The postponed mailbox is setup to have sorting disabled, but the global
176 * Sort variable may indicate something different. Sorting has to be
177 * disabled while the postpone menu is being displayed. */
182 switch (i = mutt_menuLoop (menu)) {
185 mutt_set_flag (PostContext, PostContext->hdrs[menu->current], M_DELETE,
186 (i == OP_DELETE) ? 1 : 0);
187 PostCount = PostContext->msgcount - PostContext->deleted;
188 if (option (OPTRESOLVE) && menu->current < menu->max - 1) {
189 menu->oldcurrent = menu->current;
191 if (menu->current >= menu->top + menu->pagelen) {
192 menu->top = menu->current;
193 menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
196 menu->redraw |= REDRAW_MOTION_RESYNCH;
199 menu->redraw = REDRAW_CURRENT;
202 case OP_GENERIC_SELECT_ENTRY:
214 mutt_menuDestroy (&menu);
215 return (r > -1 ? PostContext->hdrs[r] : NULL);
219 * ctx Context info, used when recalling a message to which
221 * hdr envelope/attachment info for recalled message
222 * cur if message was a reply, `cur' is set to the message which
223 * `hdr' is in reply to
224 * fcc fcc for the recalled message
225 * fcclen max length of fcc
228 * -1 error/no messages
230 * SENDREPLY recalled message is a reply
232 int mutt_get_postponed (CONTEXT * ctx, HEADER * hdr, HEADER ** cur, char *fcc,
236 int code = SENDPOSTPONED;
246 if ((PostContext = mx_open_mailbox (Postponed, M_NOSORT, NULL)) == NULL) {
248 mutt_error _("No postponed messages.");
253 if (!PostContext->msgcount) {
255 mx_close_mailbox (PostContext, NULL);
256 p_delete(&PostContext);
257 mutt_error _("No postponed messages.");
262 if (PostContext->msgcount == 1) {
263 /* only one message, so just use that one. */
264 h = PostContext->hdrs[0];
266 else if ((h = select_msg ()) == NULL) {
267 mx_close_mailbox (PostContext, NULL);
268 p_delete(&PostContext);
272 if (mutt_prepare_template (NULL, PostContext, hdr, h, 0) < 0) {
273 mx_fastclose_mailbox (PostContext);
274 p_delete(&PostContext);
278 /* finished with this message, so delete it. */
279 mutt_set_flag (PostContext, h, M_DELETE, 1);
281 /* and consider it saved, so that it won't be moved to the trash folder */
282 mutt_set_flag (PostContext, h, M_APPENDED, 1);
284 /* update the count for the status display */
285 PostCount = PostContext->msgcount - PostContext->deleted;
287 /* avoid the "purge deleted messages" prompt */
288 opt_delete = quadoption (OPT_DELETE);
289 set_quadoption (OPT_DELETE, M_YES);
290 mx_close_mailbox (PostContext, NULL);
291 set_quadoption (OPT_DELETE, opt_delete);
293 p_delete(&PostContext);
295 for (tmp = hdr->env->userhdrs; tmp;) {
296 if (ascii_strncasecmp ("X-Mutt-References:", tmp->data, 18) == 0) {
298 /* if a mailbox is currently open, look to see if the orignal message
299 the user attempted to reply to is in this mailbox */
303 ctx->id_hash = mutt_make_id_hash (ctx);
304 *cur = hash_find (ctx->id_hash, p);
307 /* Remove the X-Mutt-References: header field. */
310 last->next = tmp->next;
312 hdr->env->userhdrs = tmp->next;
314 mutt_free_list (&tmp);
319 else if (ascii_strncasecmp ("X-Mutt-Fcc:", tmp->data, 11) == 0) {
322 strfcpy (fcc, p, fcclen);
323 mutt_pretty_mailbox (fcc);
325 /* remove the X-Mutt-Fcc: header field */
328 last->next = tmp->next;
330 hdr->env->userhdrs = tmp->next;
332 mutt_free_list (&tmp);
335 else if ((WithCrypto & APPLICATION_PGP)
336 && (str_ncmp ("Pgp:", tmp->data, 4) == 0 /* this is generated
337 * by old mutt versions
339 || str_ncmp ("X-Mutt-PGP:", tmp->data, 11) == 0)) {
340 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
341 hdr->security |= APPLICATION_PGP;
343 /* remove the pgp field */
346 last->next = tmp->next;
348 hdr->env->userhdrs = tmp->next;
350 mutt_free_list (&tmp);
353 else if ((WithCrypto & APPLICATION_SMIME)
354 && str_ncmp ("X-Mutt-SMIME:", tmp->data, 13) == 0) {
355 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
356 hdr->security |= APPLICATION_SMIME;
358 /* remove the smime field */
361 last->next = tmp->next;
363 hdr->env->userhdrs = tmp->next;
365 mutt_free_list (&tmp);
370 else if (str_ncmp ("X-Mutt-Mix:", tmp->data, 11) == 0) {
373 mutt_free_list (&hdr->chain);
375 t = strtok (tmp->data + 11, " \t\n");
377 hdr->chain = mutt_add_list (hdr->chain, t);
378 t = strtok (NULL, " \t\n");
383 last->next = tmp->next;
385 hdr->env->userhdrs = tmp->next;
387 mutt_free_list (&tmp);
402 int mutt_parse_crypt_hdr (char *p, int set_signas)
405 char pgp_sign_as[LONG_STRING] = "\0", *q;
406 char smime_cryptalg[LONG_STRING] = "\0";
425 if (*(p + 1) == '<') {
427 *p && *p != '>' && q < pgp_sign_as + sizeof (pgp_sign_as) - 1;
431 mutt_error _("Illegal PGP header");
440 /* This used to be the micalg parameter.
442 * It's no longer needed, so we just skip the parameter in order
443 * to be able to recall old messages.
447 if (*(p + 1) == '<') {
448 for (p += 2; *p && *p != '>'; p++);
450 mutt_error _("Illegal PGP header");
463 if (*(p + 1) == '<') {
466 && q < smime_cryptalg + sizeof (smime_cryptalg) - 1;
470 mutt_error _("Illegal S/MIME header");
485 mutt_error _("Illegal PGP header");
491 /* the cryptalg field must not be empty */
492 if ((WithCrypto & APPLICATION_SMIME) && *smime_cryptalg)
493 str_replace (&SmimeCryptAlg, smime_cryptalg);
495 if ((WithCrypto & APPLICATION_PGP) && (set_signas || *pgp_sign_as))
496 str_replace (&PgpSignAs, pgp_sign_as);
503 int mutt_prepare_template (FILE * fp, CONTEXT * ctx, HEADER * newhdr,
504 HEADER * hdr, short weed)
507 char file[_POSIX_PATH_MAX];
516 if (!fp && (msg = mx_open_message (ctx, hdr->msgno)) == NULL)
524 /* parse the message header and MIME structure */
526 fseeko (fp, hdr->offset, 0);
527 newhdr->offset = hdr->offset;
528 newhdr->env = mutt_read_rfc822_header (fp, newhdr, 1, weed);
529 newhdr->content->length = hdr->content->length;
530 mutt_parse_part (fp, newhdr->content);
532 p_delete(&newhdr->env->message_id);
533 p_delete(&newhdr->env->mail_followup_to); /* really? */
535 /* decrypt pgp/mime encoded messages */
537 if ((WithCrypto & (APPLICATION_PGP | APPLICATION_SMIME) & hdr->security)
538 && mutt_is_multipart_encrypted (newhdr->content)) {
540 WithCrypto & (APPLICATION_PGP | APPLICATION_SMIME) & hdr->security;
541 newhdr->security |= ENCRYPT | ccap;
542 if (!crypt_valid_passphrase (ccap))
545 mutt_message _("Decrypting message...");
547 if (((ccap & APPLICATION_PGP)
548 && crypt_pgp_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
549 || ((ccap & APPLICATION_SMIME)
550 && crypt_smime_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
553 mx_close_message (&msg);
554 mutt_free_envelope (&newhdr->env);
555 mutt_free_body (&newhdr->content);
556 mutt_error _("Decryption failed.");
561 mutt_free_body (&newhdr->content);
568 * remove a potential multipart/signed layer - useful when
572 if (WithCrypto && mutt_is_multipart_signed (newhdr->content)) {
573 newhdr->security |= SIGN;
574 if ((WithCrypto & APPLICATION_PGP)
576 ascii_strcasecmp (mutt_get_parameter
577 ("protocol", newhdr->content->parameter),
578 "application/pgp-signature") == 0)
579 newhdr->security |= APPLICATION_PGP;
580 else if ((WithCrypto & APPLICATION_SMIME))
581 newhdr->security |= APPLICATION_SMIME;
583 /* destroy the signature */
584 mutt_free_body (&newhdr->content->parts->next);
585 newhdr->content = mutt_remove_multipart (newhdr->content);
590 * We don't need no primary multipart.
591 * Note: We _do_ preserve messages!
593 * XXX - we don't handle multipart/alternative in any
594 * smart way when sending messages. However, one may
595 * consider this a feature.
599 if (newhdr->content->type == TYPEMULTIPART)
600 newhdr->content = mutt_remove_multipart (newhdr->content);
604 /* create temporary files for all attachments */
605 for (b = newhdr->content; b; b = b->next) {
607 /* what follows is roughly a receive-mode variant of
608 * mutt_get_tmp_attachment () from muttlib.c
613 strfcpy (file, b->filename, sizeof (file));
614 b->d_filename = m_strdup(b->filename);
617 /* avoid Content-Disposition: header with temporary filename */
621 /* set up state flags */
625 if (b->type == TYPETEXT) {
626 if (!ascii_strcasecmp
627 ("yes", mutt_get_parameter ("x-mutt-noconv", b->parameter)))
630 s.flags |= M_CHARCONV;
634 mutt_delete_parameter ("x-mutt-noconv", &b->parameter);
637 mutt_adv_mktemp (NULL, file, sizeof (file));
638 if ((s.fpout = safe_fopen (file, "w")) == NULL)
642 if ((WithCrypto & APPLICATION_PGP)
643 && (mutt_is_application_pgp (b) & (ENCRYPT | SIGN))) {
645 mutt_body_handler (b, &s);
647 newhdr->security |= mutt_is_application_pgp (newhdr->content);
650 str_replace (&b->subtype, "plain");
651 mutt_delete_parameter ("x-action", &b->parameter);
654 mutt_decode_attachment (b, &s);
656 if (safe_fclose (&s.fpout) != 0)
659 str_replace (&b->filename, file);
662 mutt_stamp_attachment (b);
664 mutt_free_body (&b->parts);
666 b->hdr->content = NULL; /* avoid dangling pointer */
669 /* Fix encryption flags. */
671 /* No inline if multipart. */
672 if (WithCrypto && (newhdr->security & INLINE) && newhdr->content->next)
673 newhdr->security &= ~INLINE;
675 /* Do we even support multiple mechanisms? */
676 newhdr->security &= WithCrypto | ~(APPLICATION_PGP | APPLICATION_SMIME);
678 /* Theoretically, both could be set. Take the one the user wants to set by default. */
679 if ((newhdr->security & APPLICATION_PGP)
680 && (newhdr->security & APPLICATION_SMIME)) {
681 if (option (OPTSMIMEISDEFAULT))
682 newhdr->security &= ~APPLICATION_PGP;
684 newhdr->security &= ~APPLICATION_SMIME;
695 mx_close_message (&msg);
698 mutt_free_envelope (&newhdr->env);
699 mutt_free_body (&newhdr->content);