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.
16 #include "mutt_menu.h"
24 #include "imap/mx_imap.h"
26 #include "mutt_crypt.h"
31 #include "lib/debug.h"
38 static struct mapping_t PostponeHelp[] = {
39 {N_("Exit"), OP_EXIT},
40 {N_("Del"), OP_DELETE},
41 {N_("Undel"), OP_UNDELETE},
42 {N_("Help"), OP_HELP},
48 static short PostCount = 0;
49 static CONTEXT *PostContext = NULL;
50 static short UpdateNumPostponed = 0;
52 /* Return the number of postponed messages.
53 * if force is 0, use a cached value if it is costly to get a fresh
54 * count (IMAP) - else check.
56 int mutt_num_postponed (int force)
61 static time_t LastModify = 0;
62 static char *OldPostponed = NULL;
64 if (UpdateNumPostponed) {
65 UpdateNumPostponed = 0;
69 if (Postponed != OldPostponed) {
70 OldPostponed = Postponed;
79 /* LastModify is useless for IMAP */
80 if (imap_is_magic (Postponed, NULL) == M_IMAP) {
84 newpc = imap_mailbox_check (Postponed, 0);
87 debug_print (2, ("%d postponed IMAP messages found.\n", PostCount));
90 debug_print (2, ("using old IMAP postponed count.\n"));
96 if (stat (Postponed, &st) == -1) {
102 if (S_ISDIR (st.st_mode)) {
103 /* if we have a maildir mailbox, we need to stat the "new" dir */
105 char buf[_POSIX_PATH_MAX];
107 snprintf (buf, sizeof (buf), "%s/new", Postponed);
108 if (access (buf, F_OK) == 0 && stat (buf, &st) == -1) {
115 if (LastModify < st.st_mtime) {
117 int optnews = option (OPTNEWS);
119 LastModify = st.st_mtime;
121 if (access (Postponed, R_OK | F_OK) != 0)
122 return (PostCount = 0);
125 unset_option (OPTNEWS);
127 if (mx_open_mailbox (Postponed, M_NOSORT | M_QUIET, &ctx) == NULL)
130 PostCount = ctx.msgcount;
131 mx_fastclose_mailbox (&ctx);
134 set_option (OPTNEWS);
141 void mutt_update_num_postponed (void)
143 UpdateNumPostponed = 1;
146 static void post_entry (char *s, size_t slen, MUTTMENU * menu, int entry)
148 CONTEXT *ctx = (CONTEXT *) menu->data;
150 _mutt_make_string (s, slen, NONULL (HdrFmt), ctx, ctx->hdrs[entry],
151 M_FORMAT_ARROWCURSOR);
154 static HEADER *select_msg (void)
157 int i, done = 0, r = -1;
158 char helpstr[SHORT_STRING];
161 menu = mutt_new_menu ();
162 menu->make_entry = post_entry;
163 menu->menu = MENU_POST;
164 menu->max = PostContext->msgcount;
165 menu->title = _("Postponed Messages");
166 menu->data = PostContext;
168 mutt_compile_help (helpstr, sizeof (helpstr), MENU_POST, PostponeHelp);
170 /* The postponed mailbox is setup to have sorting disabled, but the global
171 * Sort variable may indicate something different. Sorting has to be
172 * disabled while the postpone menu is being displayed. */
177 switch (i = mutt_menuLoop (menu)) {
180 mutt_set_flag (PostContext, PostContext->hdrs[menu->current], M_DELETE,
181 (i == OP_DELETE) ? 1 : 0);
182 PostCount = PostContext->msgcount - PostContext->deleted;
183 if (option (OPTRESOLVE) && menu->current < menu->max - 1) {
184 menu->oldcurrent = menu->current;
186 if (menu->current >= menu->top + menu->pagelen) {
187 menu->top = menu->current;
188 menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
191 menu->redraw |= REDRAW_MOTION_RESYNCH;
194 menu->redraw = REDRAW_CURRENT;
197 case OP_GENERIC_SELECT_ENTRY:
209 mutt_menuDestroy (&menu);
210 return (r > -1 ? PostContext->hdrs[r] : NULL);
214 * ctx Context info, used when recalling a message to which
216 * hdr envelope/attachment info for recalled message
217 * cur if message was a reply, `cur' is set to the message which
218 * `hdr' is in reply to
219 * fcc fcc for the recalled message
220 * fcclen max length of fcc
223 * -1 error/no messages
225 * SENDREPLY recalled message is a reply
227 int mutt_get_postponed (CONTEXT * ctx, HEADER * hdr, HEADER ** cur, char *fcc,
231 int code = SENDPOSTPONED;
241 if ((PostContext = mx_open_mailbox (Postponed, M_NOSORT, NULL)) == NULL) {
243 mutt_error _("No postponed messages.");
248 if (!PostContext->msgcount) {
250 mx_close_mailbox (PostContext, NULL);
252 mutt_error _("No postponed messages.");
257 if (PostContext->msgcount == 1) {
258 /* only one message, so just use that one. */
259 h = PostContext->hdrs[0];
261 else if ((h = select_msg ()) == NULL) {
262 mx_close_mailbox (PostContext, NULL);
267 if (mutt_prepare_template (NULL, PostContext, hdr, h, 0) < 0) {
268 mx_fastclose_mailbox (PostContext);
273 /* finished with this message, so delete it. */
274 mutt_set_flag (PostContext, h, M_DELETE, 1);
276 /* and consider it saved, so that it won't be moved to the trash folder */
277 mutt_set_flag (PostContext, h, M_APPENDED, 1);
279 /* update the count for the status display */
280 PostCount = PostContext->msgcount - PostContext->deleted;
282 /* avoid the "purge deleted messages" prompt */
283 opt_delete = quadoption (OPT_DELETE);
284 set_quadoption (OPT_DELETE, M_YES);
285 mx_close_mailbox (PostContext, NULL);
286 set_quadoption (OPT_DELETE, opt_delete);
290 for (tmp = hdr->env->userhdrs; tmp;) {
291 if (ascii_strncasecmp ("X-Mutt-References:", tmp->data, 18) == 0) {
293 /* if a mailbox is currently open, look to see if the orignal message
294 the user attempted to reply to is in this mailbox */
298 ctx->id_hash = mutt_make_id_hash (ctx);
299 *cur = hash_find (ctx->id_hash, p);
302 /* Remove the X-Mutt-References: header field. */
305 last->next = tmp->next;
307 hdr->env->userhdrs = tmp->next;
309 mutt_free_list (&tmp);
314 else if (ascii_strncasecmp ("X-Mutt-Fcc:", tmp->data, 11) == 0) {
317 strfcpy (fcc, p, fcclen);
318 mutt_pretty_mailbox (fcc);
320 /* remove the X-Mutt-Fcc: header field */
323 last->next = tmp->next;
325 hdr->env->userhdrs = tmp->next;
327 mutt_free_list (&tmp);
330 else if ((WithCrypto & APPLICATION_PGP)
331 && (safe_strncmp ("Pgp:", tmp->data, 4) == 0 /* this is generated
332 * by old mutt versions
334 || safe_strncmp ("X-Mutt-PGP:", tmp->data, 11) == 0)) {
335 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
336 hdr->security |= APPLICATION_PGP;
338 /* remove the pgp field */
341 last->next = tmp->next;
343 hdr->env->userhdrs = tmp->next;
345 mutt_free_list (&tmp);
348 else if ((WithCrypto & APPLICATION_SMIME)
349 && safe_strncmp ("X-Mutt-SMIME:", tmp->data, 13) == 0) {
350 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
351 hdr->security |= APPLICATION_SMIME;
353 /* remove the smime field */
356 last->next = tmp->next;
358 hdr->env->userhdrs = tmp->next;
360 mutt_free_list (&tmp);
365 else if (safe_strncmp ("X-Mutt-Mix:", tmp->data, 11) == 0) {
368 mutt_free_list (&hdr->chain);
370 t = strtok (tmp->data + 11, " \t\n");
372 hdr->chain = mutt_add_list (hdr->chain, t);
373 t = strtok (NULL, " \t\n");
378 last->next = tmp->next;
380 hdr->env->userhdrs = tmp->next;
382 mutt_free_list (&tmp);
397 int mutt_parse_crypt_hdr (char *p, int set_signas)
400 char pgp_sign_as[LONG_STRING] = "\0", *q;
401 char smime_cryptalg[LONG_STRING] = "\0";
420 if (*(p + 1) == '<') {
422 *p && *p != '>' && q < pgp_sign_as + sizeof (pgp_sign_as) - 1;
426 mutt_error _("Illegal PGP header");
435 /* This used to be the micalg parameter.
437 * It's no longer needed, so we just skip the parameter in order
438 * to be able to recall old messages.
442 if (*(p + 1) == '<') {
443 for (p += 2; *p && *p != '>'; p++);
445 mutt_error _("Illegal PGP header");
458 if (*(p + 1) == '<') {
461 && q < smime_cryptalg + sizeof (smime_cryptalg) - 1;
465 mutt_error _("Illegal S/MIME header");
480 mutt_error _("Illegal PGP header");
486 /* the cryptalg field must not be empty */
487 if ((WithCrypto & APPLICATION_SMIME) && *smime_cryptalg)
488 str_replace (&SmimeCryptAlg, smime_cryptalg);
490 if ((WithCrypto & APPLICATION_PGP) && (set_signas || *pgp_sign_as))
491 str_replace (&PgpSignAs, pgp_sign_as);
498 int mutt_prepare_template (FILE * fp, CONTEXT * ctx, HEADER * newhdr,
499 HEADER * hdr, short weed)
502 char file[_POSIX_PATH_MAX];
509 memset (&s, 0, sizeof (s));
511 if (!fp && (msg = mx_open_message (ctx, hdr->msgno)) == NULL)
519 /* parse the message header and MIME structure */
521 fseek (fp, hdr->offset, 0);
522 newhdr->offset = hdr->offset;
523 newhdr->env = mutt_read_rfc822_header (fp, newhdr, 1, weed);
524 newhdr->content->length = hdr->content->length;
525 mutt_parse_part (fp, newhdr->content);
527 FREE (&newhdr->env->message_id);
528 FREE (&newhdr->env->mail_followup_to); /* really? */
530 /* decrypt pgp/mime encoded messages */
532 if ((WithCrypto & (APPLICATION_PGP | APPLICATION_SMIME) & hdr->security)
533 && mutt_is_multipart_encrypted (newhdr->content)) {
535 WithCrypto & (APPLICATION_PGP | APPLICATION_SMIME) & hdr->security;
536 newhdr->security |= ENCRYPT | ccap;
537 if (!crypt_valid_passphrase (ccap))
540 mutt_message _("Decrypting message...");
542 if (((ccap & APPLICATION_PGP)
543 && crypt_pgp_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
544 || ((ccap & APPLICATION_SMIME)
545 && crypt_smime_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
548 mx_close_message (&msg);
549 mutt_free_envelope (&newhdr->env);
550 mutt_free_body (&newhdr->content);
551 mutt_error _("Decryption failed.");
556 mutt_free_body (&newhdr->content);
563 * remove a potential multipart/signed layer - useful when
567 if (WithCrypto && mutt_is_multipart_signed (newhdr->content)) {
568 newhdr->security |= SIGN;
569 if ((WithCrypto & APPLICATION_PGP)
571 ascii_strcasecmp (mutt_get_parameter
572 ("protocol", newhdr->content->parameter),
573 "application/pgp-signature") == 0)
574 newhdr->security |= APPLICATION_PGP;
575 else if ((WithCrypto & APPLICATION_SMIME))
576 newhdr->security |= APPLICATION_SMIME;
578 /* destroy the signature */
579 mutt_free_body (&newhdr->content->parts->next);
580 newhdr->content = mutt_remove_multipart (newhdr->content);
585 * We don't need no primary multipart.
586 * Note: We _do_ preserve messages!
588 * XXX - we don't handle multipart/alternative in any
589 * smart way when sending messages. However, one may
590 * consider this a feature.
594 if (newhdr->content->type == TYPEMULTIPART)
595 newhdr->content = mutt_remove_multipart (newhdr->content);
599 /* create temporary files for all attachments */
600 for (b = newhdr->content; b; b = b->next) {
602 /* what follows is roughly a receive-mode variant of
603 * mutt_get_tmp_attachment () from muttlib.c
608 strfcpy (file, b->filename, sizeof (file));
609 b->d_filename = safe_strdup (b->filename);
612 /* avoid Content-Disposition: header with temporary filename */
616 /* set up state flags */
620 if (b->type == TYPETEXT) {
621 if (!ascii_strcasecmp
622 ("yes", mutt_get_parameter ("x-mutt-noconv", b->parameter)))
625 s.flags |= M_CHARCONV;
629 mutt_delete_parameter ("x-mutt-noconv", &b->parameter);
632 mutt_adv_mktemp (file, sizeof (file));
633 if ((s.fpout = safe_fopen (file, "w")) == NULL)
637 if ((WithCrypto & APPLICATION_PGP)
638 && (mutt_is_application_pgp (b) & (ENCRYPT | SIGN))) {
640 mutt_body_handler (b, &s);
642 newhdr->security |= mutt_is_application_pgp (newhdr->content);
645 str_replace (&b->subtype, "plain");
646 mutt_delete_parameter ("x-action", &b->parameter);
649 mutt_decode_attachment (b, &s);
651 if (safe_fclose (&s.fpout) != 0)
654 str_replace (&b->filename, file);
657 mutt_stamp_attachment (b);
659 mutt_free_body (&b->parts);
661 b->hdr->content = NULL; /* avoid dangling pointer */
664 /* Fix encryption flags. */
666 /* No inline if multipart. */
667 if (WithCrypto && (newhdr->security & INLINE) && newhdr->content->next)
668 newhdr->security &= ~INLINE;
670 /* Do we even support multiple mechanisms? */
671 newhdr->security &= WithCrypto | ~(APPLICATION_PGP | APPLICATION_SMIME);
673 /* Theoretically, both could be set. Take the one the user wants to set by default. */
674 if ((newhdr->security & APPLICATION_PGP)
675 && (newhdr->security & APPLICATION_SMIME)) {
676 if (option (OPTSMIMEISDEFAULT))
677 newhdr->security &= ~APPLICATION_PGP;
679 newhdr->security &= ~APPLICATION_SMIME;
690 mx_close_message (&msg);
693 mutt_free_envelope (&newhdr->env);
694 mutt_free_body (&newhdr->content);