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>
20 #include <lib-lib/mapping.h>
22 #include <lib-mime/mime.h>
27 #include "mutt_menu.h"
34 #include "imap/mx_imap.h"
36 #include "mutt_crypt.h"
38 #include "lib/debug.h"
45 static struct mapping_t PostponeHelp[] = {
46 {N_("Exit"), OP_EXIT},
47 {N_("Del"), OP_DELETE},
48 {N_("Undel"), OP_UNDELETE},
49 {N_("Help"), OP_HELP},
55 static short PostCount = 0;
56 static CONTEXT *PostContext = NULL;
57 static short UpdateNumPostponed = 0;
59 /* Return the number of postponed messages.
60 * if force is 0, use a cached value if it is costly to get a fresh
61 * count (IMAP) - else check.
63 int mutt_num_postponed (int force)
68 static time_t LastModify = 0;
69 static char *OldPostponed = NULL;
71 if (UpdateNumPostponed) {
72 UpdateNumPostponed = 0;
76 if (Postponed != OldPostponed) {
77 OldPostponed = Postponed;
86 /* LastModify is useless for IMAP */
87 if (imap_is_magic (Postponed, NULL) == M_IMAP) {
91 newpc = imap_mailbox_check (Postponed, 0);
94 debug_print (2, ("%d postponed IMAP messages found.\n", PostCount));
97 debug_print (2, ("using old IMAP postponed count.\n"));
103 if (stat (Postponed, &st) == -1) {
109 if (S_ISDIR (st.st_mode)) {
110 /* if we have a maildir mailbox, we need to stat the "new" dir */
112 char buf[_POSIX_PATH_MAX];
114 snprintf (buf, sizeof (buf), "%s/new", Postponed);
115 if (access (buf, F_OK) == 0 && stat (buf, &st) == -1) {
122 if (LastModify < st.st_mtime) {
124 int optnews = option (OPTNEWS);
126 LastModify = st.st_mtime;
128 if (access (Postponed, R_OK | F_OK) != 0)
129 return (PostCount = 0);
132 unset_option (OPTNEWS);
134 if (mx_open_mailbox (Postponed, M_NOSORT | M_QUIET, &ctx) == NULL)
137 PostCount = ctx.msgcount;
138 mx_fastclose_mailbox (&ctx);
141 set_option (OPTNEWS);
148 void mutt_update_num_postponed (void)
150 UpdateNumPostponed = 1;
153 static void post_entry (char *s, size_t slen, MUTTMENU * menu, int entry)
155 CONTEXT *ctx = (CONTEXT *) menu->data;
157 _mutt_make_string (s, slen, NONULL (HdrFmt), ctx, ctx->hdrs[entry],
158 M_FORMAT_ARROWCURSOR);
161 static HEADER *select_msg (void)
164 int i, done = 0, r = -1;
165 char helpstr[SHORT_STRING];
168 menu = mutt_new_menu ();
169 menu->make_entry = post_entry;
170 menu->menu = MENU_POST;
171 menu->max = PostContext->msgcount;
172 menu->title = _("Postponed Messages");
173 menu->data = PostContext;
175 mutt_compile_help (helpstr, sizeof (helpstr), MENU_POST, PostponeHelp);
177 /* The postponed mailbox is setup to have sorting disabled, but the global
178 * Sort variable may indicate something different. Sorting has to be
179 * disabled while the postpone menu is being displayed. */
184 switch (i = mutt_menuLoop (menu)) {
187 mutt_set_flag (PostContext, PostContext->hdrs[menu->current], M_DELETE,
188 (i == OP_DELETE) ? 1 : 0);
189 PostCount = PostContext->msgcount - PostContext->deleted;
190 if (option (OPTRESOLVE) && menu->current < menu->max - 1) {
191 menu->oldcurrent = menu->current;
193 if (menu->current >= menu->top + menu->pagelen) {
194 menu->top = menu->current;
195 menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
198 menu->redraw |= REDRAW_MOTION_RESYNCH;
201 menu->redraw = REDRAW_CURRENT;
204 case OP_GENERIC_SELECT_ENTRY:
216 mutt_menuDestroy (&menu);
217 return (r > -1 ? PostContext->hdrs[r] : NULL);
221 * ctx Context info, used when recalling a message to which
223 * hdr envelope/attachment info for recalled message
224 * cur if message was a reply, `cur' is set to the message which
225 * `hdr' is in reply to
226 * fcc fcc for the recalled message
227 * fcclen max length of fcc
230 * -1 error/no messages
232 * SENDREPLY recalled message is a reply
234 int mutt_get_postponed (CONTEXT * ctx, HEADER * hdr, HEADER ** cur, char *fcc,
238 int code = SENDPOSTPONED;
248 if ((PostContext = mx_open_mailbox (Postponed, M_NOSORT, NULL)) == NULL) {
250 mutt_error _("No postponed messages.");
255 if (!PostContext->msgcount) {
257 mx_close_mailbox (PostContext, NULL);
258 p_delete(&PostContext);
259 mutt_error _("No postponed messages.");
264 if (PostContext->msgcount == 1) {
265 /* only one message, so just use that one. */
266 h = PostContext->hdrs[0];
268 else if ((h = select_msg ()) == NULL) {
269 mx_close_mailbox (PostContext, NULL);
270 p_delete(&PostContext);
274 if (mutt_prepare_template (NULL, PostContext, hdr, h, 0) < 0) {
275 mx_fastclose_mailbox (PostContext);
276 p_delete(&PostContext);
280 /* finished with this message, so delete it. */
281 mutt_set_flag (PostContext, h, M_DELETE, 1);
283 /* and consider it saved, so that it won't be moved to the trash folder */
284 mutt_set_flag (PostContext, h, M_APPENDED, 1);
286 /* update the count for the status display */
287 PostCount = PostContext->msgcount - PostContext->deleted;
289 /* avoid the "purge deleted messages" prompt */
290 opt_delete = quadoption (OPT_DELETE);
291 set_quadoption (OPT_DELETE, M_YES);
292 mx_close_mailbox (PostContext, NULL);
293 set_quadoption (OPT_DELETE, opt_delete);
295 p_delete(&PostContext);
297 for (tmp = hdr->env->userhdrs; tmp;) {
298 if (ascii_strncasecmp ("X-Mutt-References:", tmp->data, 18) == 0) {
300 /* if a mailbox is currently open, look to see if the orignal message
301 the user attempted to reply to is in this mailbox */
302 p = vskipspaces(tmp->data + 18);
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) {
321 p = vskipspaces(tmp->data + 11);
322 m_strcpy(fcc, fcclen, p);
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 ((m_strncmp("Pgp:", tmp->data, 4) == 0 /* this is generated
336 * by old mutt versions
338 || m_strncmp("X-Mutt-PGP:", tmp->data, 11) == 0)) {
339 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
340 hdr->security |= APPLICATION_PGP;
342 /* remove the pgp field */
345 last->next = tmp->next;
347 hdr->env->userhdrs = tmp->next;
349 mutt_free_list (&tmp);
352 else if (m_strncmp("X-Mutt-SMIME:", tmp->data, 13) == 0) {
353 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
354 hdr->security |= APPLICATION_SMIME;
356 /* remove the smime field */
359 last->next = tmp->next;
361 hdr->env->userhdrs = tmp->next;
363 mutt_free_list (&tmp);
368 else if (m_strncmp("X-Mutt-Mix:", tmp->data, 11) == 0) {
371 mutt_free_list (&hdr->chain);
373 t = strtok (tmp->data + 11, " \t\n");
375 hdr->chain = mutt_add_list (hdr->chain, t);
376 t = strtok (NULL, " \t\n");
381 last->next = tmp->next;
383 hdr->env->userhdrs = tmp->next;
385 mutt_free_list (&tmp);
400 int mutt_parse_crypt_hdr (char *p, int set_signas)
403 char pgp_sign_as[LONG_STRING] = "\0", *q;
404 char smime_cryptalg[LONG_STRING] = "\0";
406 for (p = vskipspaces(p); *p; p++) {
418 if (*(p + 1) == '<') {
420 *p && *p != '>' && q < pgp_sign_as + sizeof (pgp_sign_as) - 1;
424 mutt_error _("Illegal PGP header");
433 /* This used to be the micalg parameter.
435 * It's no longer needed, so we just skip the parameter in order
436 * to be able to recall old messages.
440 if (*(p + 1) == '<') {
441 for (p += 2; *p && *p != '>'; p++);
443 mutt_error _("Illegal PGP header");
456 if (*(p + 1) == '<') {
459 && q < smime_cryptalg + sizeof (smime_cryptalg) - 1;
463 mutt_error _("Illegal S/MIME header");
478 mutt_error _("Illegal PGP header");
484 /* the cryptalg field must not be empty */
486 m_strreplace(&SmimeCryptAlg, smime_cryptalg);
488 if (set_signas || *pgp_sign_as)
489 m_strreplace(&PgpSignAs, pgp_sign_as);
496 int mutt_prepare_template (FILE * fp, CONTEXT * ctx, HEADER * newhdr,
497 HEADER * hdr, short weed)
500 char file[_POSIX_PATH_MAX];
509 if (!fp && (msg = mx_open_message (ctx, hdr->msgno)) == NULL)
517 /* parse the message header and MIME structure */
519 fseeko (fp, hdr->offset, 0);
520 newhdr->offset = hdr->offset;
521 newhdr->env = mutt_read_rfc822_header (fp, newhdr, 1, weed);
522 newhdr->content->length = hdr->content->length;
523 mutt_parse_part (fp, newhdr->content);
525 p_delete(&newhdr->env->message_id);
526 p_delete(&newhdr->env->mail_followup_to); /* really? */
528 /* decrypt pgp/mime encoded messages */
530 if ((APPLICATION_PGP | APPLICATION_SMIME) & hdr->security
531 && mutt_is_multipart_encrypted (newhdr->content))
533 int ccap = (APPLICATION_PGP | APPLICATION_SMIME) & hdr->security;
534 newhdr->security |= ENCRYPT | ccap;
535 if (!crypt_valid_passphrase (ccap))
538 mutt_message _("Decrypting message...");
540 if (((ccap & APPLICATION_PGP)
541 && crypt_pgp_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
542 || ((ccap & APPLICATION_SMIME)
543 && crypt_smime_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
546 mx_close_message (&msg);
547 mutt_free_envelope (&newhdr->env);
548 mutt_free_body (&newhdr->content);
549 mutt_error _("Decryption failed.");
554 mutt_free_body (&newhdr->content);
561 * remove a potential multipart/signed layer - useful when
565 if (mutt_is_multipart_signed (newhdr->content)) {
566 newhdr->security |= SIGN;
567 if (ascii_strcasecmp (mutt_get_parameter
568 ("protocol", newhdr->content->parameter),
569 "application/pgp-signature") == 0)
570 newhdr->security |= APPLICATION_PGP;
572 newhdr->security |= APPLICATION_SMIME;
574 /* destroy the signature */
575 mutt_free_body (&newhdr->content->parts->next);
576 newhdr->content = mutt_remove_multipart (newhdr->content);
581 * We don't need no primary multipart.
582 * Note: We _do_ preserve messages!
584 * XXX - we don't handle multipart/alternative in any
585 * smart way when sending messages. However, one may
586 * consider this a feature.
590 if (newhdr->content->type == TYPEMULTIPART)
591 newhdr->content = mutt_remove_multipart (newhdr->content);
595 /* create temporary files for all attachments */
596 for (b = newhdr->content; b; b = b->next) {
598 /* what follows is roughly a receive-mode variant of
599 * mutt_get_tmp_attachment () from muttlib.c
604 m_strcpy(file, sizeof(file), b->filename);
605 b->d_filename = m_strdup(b->filename);
608 /* avoid Content-Disposition: header with temporary filename */
612 /* set up state flags */
616 if (b->type == TYPETEXT) {
617 if (!ascii_strcasecmp
618 ("yes", mutt_get_parameter ("x-mutt-noconv", b->parameter)))
621 s.flags |= M_CHARCONV;
625 mutt_delete_parameter ("x-mutt-noconv", &b->parameter);
628 mutt_adv_mktemp (NULL, file, sizeof (file));
629 if ((s.fpout = safe_fopen (file, "w")) == NULL)
633 if (mutt_is_application_pgp (b) & (ENCRYPT | SIGN)) {
635 mutt_body_handler (b, &s);
637 newhdr->security |= mutt_is_application_pgp (newhdr->content);
640 m_strreplace(&b->subtype, "plain");
641 mutt_delete_parameter ("x-action", &b->parameter);
644 mutt_decode_attachment (b, &s);
646 if (safe_fclose (&s.fpout) != 0)
649 m_strreplace(&b->filename, file);
652 mutt_stamp_attachment (b);
654 mutt_free_body (&b->parts);
656 b->hdr->content = NULL; /* avoid dangling pointer */
659 /* Fix encryption flags. */
661 /* No inline if multipart. */
662 if ((newhdr->security & INLINE) && newhdr->content->next)
663 newhdr->security &= ~INLINE;
665 /* Theoretically, both could be set. Take the one the user wants to set by default. */
666 if ((newhdr->security & APPLICATION_PGP)
667 && (newhdr->security & APPLICATION_SMIME)) {
668 if (option (OPTSMIMEISDEFAULT))
669 newhdr->security &= ~APPLICATION_PGP;
671 newhdr->security &= ~APPLICATION_SMIME;
682 mx_close_message (&msg);
685 mutt_free_envelope (&newhdr->env);
686 mutt_free_body (&newhdr->content);