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>
19 #include <lib-lib/file.h>
24 #include "mutt_menu.h"
33 #include "imap/mx_imap.h"
35 #include "mutt_crypt.h"
37 #include "lib/debug.h"
44 static struct mapping_t PostponeHelp[] = {
45 {N_("Exit"), OP_EXIT},
46 {N_("Del"), OP_DELETE},
47 {N_("Undel"), OP_UNDELETE},
48 {N_("Help"), OP_HELP},
54 static short PostCount = 0;
55 static CONTEXT *PostContext = NULL;
56 static short UpdateNumPostponed = 0;
58 /* Return the number of postponed messages.
59 * if force is 0, use a cached value if it is costly to get a fresh
60 * count (IMAP) - else check.
62 int mutt_num_postponed (int force)
67 static time_t LastModify = 0;
68 static char *OldPostponed = NULL;
70 if (UpdateNumPostponed) {
71 UpdateNumPostponed = 0;
75 if (Postponed != OldPostponed) {
76 OldPostponed = Postponed;
85 /* LastModify is useless for IMAP */
86 if (imap_is_magic (Postponed, NULL) == M_IMAP) {
90 newpc = imap_mailbox_check (Postponed, 0);
93 debug_print (2, ("%d postponed IMAP messages found.\n", PostCount));
96 debug_print (2, ("using old IMAP postponed count.\n"));
102 if (stat (Postponed, &st) == -1) {
108 if (S_ISDIR (st.st_mode)) {
109 /* if we have a maildir mailbox, we need to stat the "new" dir */
111 char buf[_POSIX_PATH_MAX];
113 snprintf (buf, sizeof (buf), "%s/new", Postponed);
114 if (access (buf, F_OK) == 0 && stat (buf, &st) == -1) {
121 if (LastModify < st.st_mtime) {
123 int optnews = option (OPTNEWS);
125 LastModify = st.st_mtime;
127 if (access (Postponed, R_OK | F_OK) != 0)
128 return (PostCount = 0);
131 unset_option (OPTNEWS);
133 if (mx_open_mailbox (Postponed, M_NOSORT | M_QUIET, &ctx) == NULL)
136 PostCount = ctx.msgcount;
137 mx_fastclose_mailbox (&ctx);
140 set_option (OPTNEWS);
147 void mutt_update_num_postponed (void)
149 UpdateNumPostponed = 1;
152 static void post_entry (char *s, size_t slen, MUTTMENU * menu, int entry)
154 CONTEXT *ctx = (CONTEXT *) menu->data;
156 _mutt_make_string (s, slen, NONULL (HdrFmt), ctx, ctx->hdrs[entry],
157 M_FORMAT_ARROWCURSOR);
160 static HEADER *select_msg (void)
163 int i, done = 0, r = -1;
164 char helpstr[SHORT_STRING];
167 menu = mutt_new_menu ();
168 menu->make_entry = post_entry;
169 menu->menu = MENU_POST;
170 menu->max = PostContext->msgcount;
171 menu->title = _("Postponed Messages");
172 menu->data = PostContext;
174 mutt_compile_help (helpstr, sizeof (helpstr), MENU_POST, PostponeHelp);
176 /* The postponed mailbox is setup to have sorting disabled, but the global
177 * Sort variable may indicate something different. Sorting has to be
178 * disabled while the postpone menu is being displayed. */
183 switch (i = mutt_menuLoop (menu)) {
186 mutt_set_flag (PostContext, PostContext->hdrs[menu->current], M_DELETE,
187 (i == OP_DELETE) ? 1 : 0);
188 PostCount = PostContext->msgcount - PostContext->deleted;
189 if (option (OPTRESOLVE) && menu->current < menu->max - 1) {
190 menu->oldcurrent = menu->current;
192 if (menu->current >= menu->top + menu->pagelen) {
193 menu->top = menu->current;
194 menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
197 menu->redraw |= REDRAW_MOTION_RESYNCH;
200 menu->redraw = REDRAW_CURRENT;
203 case OP_GENERIC_SELECT_ENTRY:
215 mutt_menuDestroy (&menu);
216 return (r > -1 ? PostContext->hdrs[r] : NULL);
220 * ctx Context info, used when recalling a message to which
222 * hdr envelope/attachment info for recalled message
223 * cur if message was a reply, `cur' is set to the message which
224 * `hdr' is in reply to
225 * fcc fcc for the recalled message
226 * fcclen max length of fcc
229 * -1 error/no messages
231 * SENDREPLY recalled message is a reply
233 int mutt_get_postponed (CONTEXT * ctx, HEADER * hdr, HEADER ** cur, char *fcc,
237 int code = SENDPOSTPONED;
247 if ((PostContext = mx_open_mailbox (Postponed, M_NOSORT, NULL)) == NULL) {
249 mutt_error _("No postponed messages.");
254 if (!PostContext->msgcount) {
256 mx_close_mailbox (PostContext, NULL);
257 p_delete(&PostContext);
258 mutt_error _("No postponed messages.");
263 if (PostContext->msgcount == 1) {
264 /* only one message, so just use that one. */
265 h = PostContext->hdrs[0];
267 else if ((h = select_msg ()) == NULL) {
268 mx_close_mailbox (PostContext, NULL);
269 p_delete(&PostContext);
273 if (mutt_prepare_template (NULL, PostContext, hdr, h, 0) < 0) {
274 mx_fastclose_mailbox (PostContext);
275 p_delete(&PostContext);
279 /* finished with this message, so delete it. */
280 mutt_set_flag (PostContext, h, M_DELETE, 1);
282 /* and consider it saved, so that it won't be moved to the trash folder */
283 mutt_set_flag (PostContext, h, M_APPENDED, 1);
285 /* update the count for the status display */
286 PostCount = PostContext->msgcount - PostContext->deleted;
288 /* avoid the "purge deleted messages" prompt */
289 opt_delete = quadoption (OPT_DELETE);
290 set_quadoption (OPT_DELETE, M_YES);
291 mx_close_mailbox (PostContext, NULL);
292 set_quadoption (OPT_DELETE, opt_delete);
294 p_delete(&PostContext);
296 for (tmp = hdr->env->userhdrs; tmp;) {
297 if (ascii_strncasecmp ("X-Mutt-References:", tmp->data, 18) == 0) {
299 /* if a mailbox is currently open, look to see if the orignal message
300 the user attempted to reply to is in this mailbox */
304 ctx->id_hash = mutt_make_id_hash (ctx);
305 *cur = hash_find (ctx->id_hash, p);
308 /* Remove the X-Mutt-References: header field. */
311 last->next = tmp->next;
313 hdr->env->userhdrs = tmp->next;
315 mutt_free_list (&tmp);
320 else if (ascii_strncasecmp ("X-Mutt-Fcc:", tmp->data, 11) == 0) {
323 strfcpy (fcc, p, fcclen);
324 mutt_pretty_mailbox (fcc);
326 /* remove the X-Mutt-Fcc: header field */
329 last->next = tmp->next;
331 hdr->env->userhdrs = tmp->next;
333 mutt_free_list (&tmp);
336 else if ((WithCrypto & APPLICATION_PGP)
337 && (str_ncmp ("Pgp:", tmp->data, 4) == 0 /* this is generated
338 * by old mutt versions
340 || str_ncmp ("X-Mutt-PGP:", tmp->data, 11) == 0)) {
341 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
342 hdr->security |= APPLICATION_PGP;
344 /* remove the pgp field */
347 last->next = tmp->next;
349 hdr->env->userhdrs = tmp->next;
351 mutt_free_list (&tmp);
354 else if ((WithCrypto & APPLICATION_SMIME)
355 && str_ncmp ("X-Mutt-SMIME:", tmp->data, 13) == 0) {
356 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
357 hdr->security |= APPLICATION_SMIME;
359 /* remove the smime field */
362 last->next = tmp->next;
364 hdr->env->userhdrs = tmp->next;
366 mutt_free_list (&tmp);
371 else if (str_ncmp ("X-Mutt-Mix:", tmp->data, 11) == 0) {
374 mutt_free_list (&hdr->chain);
376 t = strtok (tmp->data + 11, " \t\n");
378 hdr->chain = mutt_add_list (hdr->chain, t);
379 t = strtok (NULL, " \t\n");
384 last->next = tmp->next;
386 hdr->env->userhdrs = tmp->next;
388 mutt_free_list (&tmp);
403 int mutt_parse_crypt_hdr (char *p, int set_signas)
406 char pgp_sign_as[LONG_STRING] = "\0", *q;
407 char smime_cryptalg[LONG_STRING] = "\0";
426 if (*(p + 1) == '<') {
428 *p && *p != '>' && q < pgp_sign_as + sizeof (pgp_sign_as) - 1;
432 mutt_error _("Illegal PGP header");
441 /* This used to be the micalg parameter.
443 * It's no longer needed, so we just skip the parameter in order
444 * to be able to recall old messages.
448 if (*(p + 1) == '<') {
449 for (p += 2; *p && *p != '>'; p++);
451 mutt_error _("Illegal PGP header");
464 if (*(p + 1) == '<') {
467 && q < smime_cryptalg + sizeof (smime_cryptalg) - 1;
471 mutt_error _("Illegal S/MIME header");
486 mutt_error _("Illegal PGP header");
492 /* the cryptalg field must not be empty */
493 if ((WithCrypto & APPLICATION_SMIME) && *smime_cryptalg)
494 str_replace (&SmimeCryptAlg, smime_cryptalg);
496 if ((WithCrypto & APPLICATION_PGP) && (set_signas || *pgp_sign_as))
497 str_replace (&PgpSignAs, pgp_sign_as);
504 int mutt_prepare_template (FILE * fp, CONTEXT * ctx, HEADER * newhdr,
505 HEADER * hdr, short weed)
508 char file[_POSIX_PATH_MAX];
517 if (!fp && (msg = mx_open_message (ctx, hdr->msgno)) == NULL)
525 /* parse the message header and MIME structure */
527 fseeko (fp, hdr->offset, 0);
528 newhdr->offset = hdr->offset;
529 newhdr->env = mutt_read_rfc822_header (fp, newhdr, 1, weed);
530 newhdr->content->length = hdr->content->length;
531 mutt_parse_part (fp, newhdr->content);
533 p_delete(&newhdr->env->message_id);
534 p_delete(&newhdr->env->mail_followup_to); /* really? */
536 /* decrypt pgp/mime encoded messages */
538 if ((WithCrypto & (APPLICATION_PGP | APPLICATION_SMIME) & hdr->security)
539 && mutt_is_multipart_encrypted (newhdr->content)) {
541 WithCrypto & (APPLICATION_PGP | APPLICATION_SMIME) & hdr->security;
542 newhdr->security |= ENCRYPT | ccap;
543 if (!crypt_valid_passphrase (ccap))
546 mutt_message _("Decrypting message...");
548 if (((ccap & APPLICATION_PGP)
549 && crypt_pgp_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
550 || ((ccap & APPLICATION_SMIME)
551 && crypt_smime_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
554 mx_close_message (&msg);
555 mutt_free_envelope (&newhdr->env);
556 mutt_free_body (&newhdr->content);
557 mutt_error _("Decryption failed.");
562 mutt_free_body (&newhdr->content);
569 * remove a potential multipart/signed layer - useful when
573 if (WithCrypto && mutt_is_multipart_signed (newhdr->content)) {
574 newhdr->security |= SIGN;
575 if ((WithCrypto & APPLICATION_PGP)
577 ascii_strcasecmp (mutt_get_parameter
578 ("protocol", newhdr->content->parameter),
579 "application/pgp-signature") == 0)
580 newhdr->security |= APPLICATION_PGP;
581 else if ((WithCrypto & APPLICATION_SMIME))
582 newhdr->security |= APPLICATION_SMIME;
584 /* destroy the signature */
585 mutt_free_body (&newhdr->content->parts->next);
586 newhdr->content = mutt_remove_multipart (newhdr->content);
591 * We don't need no primary multipart.
592 * Note: We _do_ preserve messages!
594 * XXX - we don't handle multipart/alternative in any
595 * smart way when sending messages. However, one may
596 * consider this a feature.
600 if (newhdr->content->type == TYPEMULTIPART)
601 newhdr->content = mutt_remove_multipart (newhdr->content);
605 /* create temporary files for all attachments */
606 for (b = newhdr->content; b; b = b->next) {
608 /* what follows is roughly a receive-mode variant of
609 * mutt_get_tmp_attachment () from muttlib.c
614 strfcpy (file, b->filename, sizeof (file));
615 b->d_filename = m_strdup(b->filename);
618 /* avoid Content-Disposition: header with temporary filename */
622 /* set up state flags */
626 if (b->type == TYPETEXT) {
627 if (!ascii_strcasecmp
628 ("yes", mutt_get_parameter ("x-mutt-noconv", b->parameter)))
631 s.flags |= M_CHARCONV;
635 mutt_delete_parameter ("x-mutt-noconv", &b->parameter);
638 mutt_adv_mktemp (NULL, file, sizeof (file));
639 if ((s.fpout = safe_fopen (file, "w")) == NULL)
643 if ((WithCrypto & APPLICATION_PGP)
644 && (mutt_is_application_pgp (b) & (ENCRYPT | SIGN))) {
646 mutt_body_handler (b, &s);
648 newhdr->security |= mutt_is_application_pgp (newhdr->content);
651 str_replace (&b->subtype, "plain");
652 mutt_delete_parameter ("x-action", &b->parameter);
655 mutt_decode_attachment (b, &s);
657 if (safe_fclose (&s.fpout) != 0)
660 str_replace (&b->filename, file);
663 mutt_stamp_attachment (b);
665 mutt_free_body (&b->parts);
667 b->hdr->content = NULL; /* avoid dangling pointer */
670 /* Fix encryption flags. */
672 /* No inline if multipart. */
673 if (WithCrypto && (newhdr->security & INLINE) && newhdr->content->next)
674 newhdr->security &= ~INLINE;
676 /* Do we even support multiple mechanisms? */
677 newhdr->security &= WithCrypto | ~(APPLICATION_PGP | APPLICATION_SMIME);
679 /* Theoretically, both could be set. Take the one the user wants to set by default. */
680 if ((newhdr->security & APPLICATION_PGP)
681 && (newhdr->security & APPLICATION_SMIME)) {
682 if (option (OPTSMIMEISDEFAULT))
683 newhdr->security &= ~APPLICATION_PGP;
685 newhdr->security &= ~APPLICATION_SMIME;
696 mx_close_message (&msg);
699 mutt_free_envelope (&newhdr->env);
700 mutt_free_body (&newhdr->content);