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>
24 #include <lib-ui/enter.h>
25 #include <lib-ui/menu.h>
33 #include <lib-crypt/crypt.h>
35 #include <imap/imap.h>
36 #include <imap/mx_imap.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;
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"));
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, ssize_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 */
300 p = vskipspaces(tmp->data + 18);
302 ctx->id_hash = mutt_make_id_hash (ctx);
303 *cur = hash_find (ctx->id_hash, p);
306 /* Remove the X-Mutt-References: header field. */
309 last->next = tmp->next;
311 hdr->env->userhdrs = tmp->next;
313 mutt_free_list (&tmp);
318 else if (ascii_strncasecmp ("X-Mutt-Fcc:", tmp->data, 11) == 0) {
319 p = vskipspaces(tmp->data + 11);
320 m_strcpy(fcc, fcclen, p);
321 mutt_pretty_mailbox (fcc);
323 /* remove the X-Mutt-Fcc: header field */
326 last->next = tmp->next;
328 hdr->env->userhdrs = tmp->next;
330 mutt_free_list (&tmp);
333 else if ((m_strncmp("Pgp:", tmp->data, 4) == 0 /* this is generated
334 * by old mutt versions
336 || m_strncmp("X-Mutt-PGP:", tmp->data, 11) == 0)) {
337 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
338 hdr->security |= APPLICATION_PGP;
340 /* remove the pgp field */
343 last->next = tmp->next;
345 hdr->env->userhdrs = tmp->next;
347 mutt_free_list (&tmp);
350 else if (m_strncmp("X-Mutt-SMIME:", tmp->data, 13) == 0) {
351 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
352 hdr->security |= APPLICATION_SMIME;
354 /* remove the smime field */
357 last->next = tmp->next;
359 hdr->env->userhdrs = tmp->next;
361 mutt_free_list (&tmp);
366 else if (m_strncmp("X-Mutt-Mix:", tmp->data, 11) == 0) {
369 mutt_free_list (&hdr->chain);
371 t = strtok (tmp->data + 11, " \t\n");
373 hdr->chain = mutt_add_list (hdr->chain, t);
374 t = strtok (NULL, " \t\n");
379 last->next = tmp->next;
381 hdr->env->userhdrs = tmp->next;
383 mutt_free_list (&tmp);
398 int mutt_parse_crypt_hdr (char *p, int set_signas)
401 char pgp_sign_as[LONG_STRING] = "\0", *q;
402 char smime_cryptalg[LONG_STRING] = "\0";
404 for (p = vskipspaces(p); *p; p++) {
416 if (*(p + 1) == '<') {
418 *p && *p != '>' && q < pgp_sign_as + sizeof (pgp_sign_as) - 1;
422 mutt_error _("Illegal PGP header");
431 /* This used to be the micalg parameter.
433 * It's no longer needed, so we just skip the parameter in order
434 * to be able to recall old messages.
438 if (*(p + 1) == '<') {
439 for (p += 2; *p && *p != '>'; p++);
441 mutt_error _("Illegal PGP header");
454 if (*(p + 1) == '<') {
457 && q < smime_cryptalg + sizeof (smime_cryptalg) - 1;
461 mutt_error _("Illegal S/MIME header");
476 mutt_error _("Illegal PGP header");
482 /* the cryptalg field must not be empty */
484 m_strreplace(&SmimeCryptAlg, smime_cryptalg);
486 if (set_signas || *pgp_sign_as)
487 m_strreplace(&PgpSignAs, pgp_sign_as);
494 int mutt_prepare_template (FILE * fp, CONTEXT * ctx, HEADER * newhdr,
495 HEADER * hdr, short weed)
498 char file[_POSIX_PATH_MAX];
507 if (!fp && (msg = mx_open_message (ctx, hdr->msgno)) == NULL)
515 /* parse the message header and MIME structure */
517 fseeko (fp, hdr->offset, 0);
518 newhdr->offset = hdr->offset;
519 newhdr->env = mutt_read_rfc822_header (fp, newhdr, 1, weed);
520 newhdr->content->length = hdr->content->length;
521 mutt_parse_part (fp, newhdr->content);
523 p_delete(&newhdr->env->message_id);
524 p_delete(&newhdr->env->mail_followup_to); /* really? */
526 /* decrypt pgp/mime encoded messages */
528 if ((APPLICATION_PGP | APPLICATION_SMIME) & hdr->security
529 && mutt_is_multipart_encrypted (newhdr->content))
531 int ccap = (APPLICATION_PGP | APPLICATION_SMIME) & hdr->security;
532 newhdr->security |= ENCRYPT | ccap;
533 if (!crypt_valid_passphrase (ccap))
536 mutt_message _("Decrypting message...");
538 if (((ccap & APPLICATION_PGP)
539 && crypt_pgp_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
540 || ((ccap & APPLICATION_SMIME)
541 && crypt_smime_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
544 mx_close_message (&msg);
545 envelope_delete(&newhdr->env);
546 mutt_free_body (&newhdr->content);
547 mutt_error _("Decryption failed.");
552 mutt_free_body (&newhdr->content);
559 * remove a potential multipart/signed layer - useful when
563 if (mutt_is_multipart_signed (newhdr->content)) {
564 newhdr->security |= SIGN;
565 if (ascii_strcasecmp (mutt_get_parameter
566 ("protocol", newhdr->content->parameter),
567 "application/pgp-signature") == 0)
568 newhdr->security |= APPLICATION_PGP;
570 newhdr->security |= APPLICATION_SMIME;
572 /* destroy the signature */
573 mutt_free_body (&newhdr->content->parts->next);
574 newhdr->content = mutt_remove_multipart (newhdr->content);
579 * We don't need no primary multipart.
580 * Note: We _do_ preserve messages!
582 * XXX - we don't handle multipart/alternative in any
583 * smart way when sending messages. However, one may
584 * consider this a feature.
588 if (newhdr->content->type == TYPEMULTIPART)
589 newhdr->content = mutt_remove_multipart (newhdr->content);
593 /* create temporary files for all attachments */
594 for (b = newhdr->content; b; b = b->next) {
596 /* what follows is roughly a receive-mode variant of
597 * mutt_get_tmp_attachment () from muttlib.c
602 m_strcpy(file, sizeof(file), b->filename);
603 b->d_filename = m_strdup(b->filename);
606 /* avoid Content-Disposition: header with temporary filename */
610 /* set up state flags */
614 if (b->type == TYPETEXT) {
615 if (!ascii_strcasecmp
616 ("yes", mutt_get_parameter ("x-mutt-noconv", b->parameter)))
619 s.flags |= M_CHARCONV;
623 mutt_delete_parameter ("x-mutt-noconv", &b->parameter);
626 mutt_adv_mktemp (NULL, file, sizeof (file));
627 if ((s.fpout = safe_fopen (file, "w")) == NULL)
631 if (mutt_is_application_pgp (b) & (ENCRYPT | SIGN)) {
633 mutt_body_handler (b, &s);
635 newhdr->security |= mutt_is_application_pgp (newhdr->content);
638 m_strreplace(&b->subtype, "plain");
639 mutt_delete_parameter ("x-action", &b->parameter);
642 mutt_decode_attachment (b, &s);
644 if (safe_fclose (&s.fpout) != 0)
647 m_strreplace(&b->filename, file);
650 mutt_stamp_attachment (b);
652 mutt_free_body (&b->parts);
654 b->hdr->content = NULL; /* avoid dangling pointer */
657 /* Fix encryption flags. */
659 /* No inline if multipart. */
660 if ((newhdr->security & INLINE) && newhdr->content->next)
661 newhdr->security &= ~INLINE;
663 /* Theoretically, both could be set. Take the one the user wants to set by default. */
664 if ((newhdr->security & APPLICATION_PGP)
665 && (newhdr->security & APPLICATION_SMIME)) {
666 if (option (OPTSMIMEISDEFAULT))
667 newhdr->security &= ~APPLICATION_PGP;
669 newhdr->security &= ~APPLICATION_SMIME;
680 mx_close_message (&msg);
683 envelope_delete(&newhdr->env);
684 mutt_free_body (&newhdr->content);