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.
19 #include "mutt_menu.h"
28 #include "imap/mx_imap.h"
30 #include "mutt_crypt.h"
35 #include "lib/debug.h"
42 static struct mapping_t PostponeHelp[] = {
43 {N_("Exit"), OP_EXIT},
44 {N_("Del"), OP_DELETE},
45 {N_("Undel"), OP_UNDELETE},
46 {N_("Help"), OP_HELP},
52 static short PostCount = 0;
53 static CONTEXT *PostContext = NULL;
54 static short UpdateNumPostponed = 0;
56 /* Return the number of postponed messages.
57 * if force is 0, use a cached value if it is costly to get a fresh
58 * count (IMAP) - else check.
60 int mutt_num_postponed (int force)
65 static time_t LastModify = 0;
66 static char *OldPostponed = NULL;
68 if (UpdateNumPostponed) {
69 UpdateNumPostponed = 0;
73 if (Postponed != OldPostponed) {
74 OldPostponed = Postponed;
83 /* LastModify is useless for IMAP */
84 if (imap_is_magic (Postponed, NULL) == M_IMAP) {
88 newpc = imap_mailbox_check (Postponed, 0);
91 debug_print (2, ("%d postponed IMAP messages found.\n", PostCount));
94 debug_print (2, ("using old IMAP postponed count.\n"));
100 if (stat (Postponed, &st) == -1) {
106 if (S_ISDIR (st.st_mode)) {
107 /* if we have a maildir mailbox, we need to stat the "new" dir */
109 char buf[_POSIX_PATH_MAX];
111 snprintf (buf, sizeof (buf), "%s/new", Postponed);
112 if (access (buf, F_OK) == 0 && stat (buf, &st) == -1) {
119 if (LastModify < st.st_mtime) {
121 int optnews = option (OPTNEWS);
123 LastModify = st.st_mtime;
125 if (access (Postponed, R_OK | F_OK) != 0)
126 return (PostCount = 0);
129 unset_option (OPTNEWS);
131 if (mx_open_mailbox (Postponed, M_NOSORT | M_QUIET, &ctx) == NULL)
134 PostCount = ctx.msgcount;
135 mx_fastclose_mailbox (&ctx);
138 set_option (OPTNEWS);
145 void mutt_update_num_postponed (void)
147 UpdateNumPostponed = 1;
150 static void post_entry (char *s, size_t slen, MUTTMENU * menu, int entry)
152 CONTEXT *ctx = (CONTEXT *) menu->data;
154 _mutt_make_string (s, slen, NONULL (HdrFmt), ctx, ctx->hdrs[entry],
155 M_FORMAT_ARROWCURSOR);
158 static HEADER *select_msg (void)
161 int i, done = 0, r = -1;
162 char helpstr[SHORT_STRING];
165 menu = mutt_new_menu ();
166 menu->make_entry = post_entry;
167 menu->menu = MENU_POST;
168 menu->max = PostContext->msgcount;
169 menu->title = _("Postponed Messages");
170 menu->data = PostContext;
172 mutt_compile_help (helpstr, sizeof (helpstr), MENU_POST, PostponeHelp);
174 /* The postponed mailbox is setup to have sorting disabled, but the global
175 * Sort variable may indicate something different. Sorting has to be
176 * disabled while the postpone menu is being displayed. */
181 switch (i = mutt_menuLoop (menu)) {
184 mutt_set_flag (PostContext, PostContext->hdrs[menu->current], M_DELETE,
185 (i == OP_DELETE) ? 1 : 0);
186 PostCount = PostContext->msgcount - PostContext->deleted;
187 if (option (OPTRESOLVE) && menu->current < menu->max - 1) {
188 menu->oldcurrent = menu->current;
190 if (menu->current >= menu->top + menu->pagelen) {
191 menu->top = menu->current;
192 menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
195 menu->redraw |= REDRAW_MOTION_RESYNCH;
198 menu->redraw = REDRAW_CURRENT;
201 case OP_GENERIC_SELECT_ENTRY:
213 mutt_menuDestroy (&menu);
214 return (r > -1 ? PostContext->hdrs[r] : NULL);
218 * ctx Context info, used when recalling a message to which
220 * hdr envelope/attachment info for recalled message
221 * cur if message was a reply, `cur' is set to the message which
222 * `hdr' is in reply to
223 * fcc fcc for the recalled message
224 * fcclen max length of fcc
227 * -1 error/no messages
229 * SENDREPLY recalled message is a reply
231 int mutt_get_postponed (CONTEXT * ctx, HEADER * hdr, HEADER ** cur, char *fcc,
235 int code = SENDPOSTPONED;
245 if ((PostContext = mx_open_mailbox (Postponed, M_NOSORT, NULL)) == NULL) {
247 mutt_error _("No postponed messages.");
252 if (!PostContext->msgcount) {
254 mx_close_mailbox (PostContext, NULL);
255 mem_free (&PostContext);
256 mutt_error _("No postponed messages.");
261 if (PostContext->msgcount == 1) {
262 /* only one message, so just use that one. */
263 h = PostContext->hdrs[0];
265 else if ((h = select_msg ()) == NULL) {
266 mx_close_mailbox (PostContext, NULL);
267 mem_free (&PostContext);
271 if (mutt_prepare_template (NULL, PostContext, hdr, h, 0) < 0) {
272 mx_fastclose_mailbox (PostContext);
273 mem_free (&PostContext);
277 /* finished with this message, so delete it. */
278 mutt_set_flag (PostContext, h, M_DELETE, 1);
280 /* and consider it saved, so that it won't be moved to the trash folder */
281 mutt_set_flag (PostContext, h, M_APPENDED, 1);
283 /* update the count for the status display */
284 PostCount = PostContext->msgcount - PostContext->deleted;
286 /* avoid the "purge deleted messages" prompt */
287 opt_delete = quadoption (OPT_DELETE);
288 set_quadoption (OPT_DELETE, M_YES);
289 mx_close_mailbox (PostContext, NULL);
290 set_quadoption (OPT_DELETE, opt_delete);
292 mem_free (&PostContext);
294 for (tmp = hdr->env->userhdrs; tmp;) {
295 if (ascii_strncasecmp ("X-Mutt-References:", tmp->data, 18) == 0) {
297 /* if a mailbox is currently open, look to see if the orignal message
298 the user attempted to reply to is in this mailbox */
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) {
321 strfcpy (fcc, p, fcclen);
322 mutt_pretty_mailbox (fcc);
324 /* remove the X-Mutt-Fcc: header field */
327 last->next = tmp->next;
329 hdr->env->userhdrs = tmp->next;
331 mutt_free_list (&tmp);
334 else if ((WithCrypto & APPLICATION_PGP)
335 && (str_ncmp ("Pgp:", tmp->data, 4) == 0 /* this is generated
336 * by old mutt versions
338 || str_ncmp ("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 ((WithCrypto & APPLICATION_SMIME)
353 && str_ncmp ("X-Mutt-SMIME:", tmp->data, 13) == 0) {
354 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
355 hdr->security |= APPLICATION_SMIME;
357 /* remove the smime field */
360 last->next = tmp->next;
362 hdr->env->userhdrs = tmp->next;
364 mutt_free_list (&tmp);
369 else if (str_ncmp ("X-Mutt-Mix:", tmp->data, 11) == 0) {
372 mutt_free_list (&hdr->chain);
374 t = strtok (tmp->data + 11, " \t\n");
376 hdr->chain = mutt_add_list (hdr->chain, t);
377 t = strtok (NULL, " \t\n");
382 last->next = tmp->next;
384 hdr->env->userhdrs = tmp->next;
386 mutt_free_list (&tmp);
401 int mutt_parse_crypt_hdr (char *p, int set_signas)
404 char pgp_sign_as[LONG_STRING] = "\0", *q;
405 char smime_cryptalg[LONG_STRING] = "\0";
424 if (*(p + 1) == '<') {
426 *p && *p != '>' && q < pgp_sign_as + sizeof (pgp_sign_as) - 1;
430 mutt_error _("Illegal PGP header");
439 /* This used to be the micalg parameter.
441 * It's no longer needed, so we just skip the parameter in order
442 * to be able to recall old messages.
446 if (*(p + 1) == '<') {
447 for (p += 2; *p && *p != '>'; p++);
449 mutt_error _("Illegal PGP header");
462 if (*(p + 1) == '<') {
465 && q < smime_cryptalg + sizeof (smime_cryptalg) - 1;
469 mutt_error _("Illegal S/MIME header");
484 mutt_error _("Illegal PGP header");
490 /* the cryptalg field must not be empty */
491 if ((WithCrypto & APPLICATION_SMIME) && *smime_cryptalg)
492 str_replace (&SmimeCryptAlg, smime_cryptalg);
494 if ((WithCrypto & APPLICATION_PGP) && (set_signas || *pgp_sign_as))
495 str_replace (&PgpSignAs, pgp_sign_as);
502 int mutt_prepare_template (FILE * fp, CONTEXT * ctx, HEADER * newhdr,
503 HEADER * hdr, short weed)
506 char file[_POSIX_PATH_MAX];
513 memset (&s, 0, sizeof (s));
515 if (!fp && (msg = mx_open_message (ctx, hdr->msgno)) == NULL)
523 /* parse the message header and MIME structure */
525 fseek (fp, hdr->offset, 0);
526 newhdr->offset = hdr->offset;
527 newhdr->env = mutt_read_rfc822_header (fp, newhdr, 1, weed);
528 newhdr->content->length = hdr->content->length;
529 mutt_parse_part (fp, newhdr->content);
531 mem_free (&newhdr->env->message_id);
532 mem_free (&newhdr->env->mail_followup_to); /* really? */
534 /* decrypt pgp/mime encoded messages */
536 if ((WithCrypto & (APPLICATION_PGP | APPLICATION_SMIME) & hdr->security)
537 && mutt_is_multipart_encrypted (newhdr->content)) {
539 WithCrypto & (APPLICATION_PGP | APPLICATION_SMIME) & hdr->security;
540 newhdr->security |= ENCRYPT | ccap;
541 if (!crypt_valid_passphrase (ccap))
544 mutt_message _("Decrypting message...");
546 if (((ccap & APPLICATION_PGP)
547 && crypt_pgp_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
548 || ((ccap & APPLICATION_SMIME)
549 && crypt_smime_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
552 mx_close_message (&msg);
553 mutt_free_envelope (&newhdr->env);
554 mutt_free_body (&newhdr->content);
555 mutt_error _("Decryption failed.");
560 mutt_free_body (&newhdr->content);
567 * remove a potential multipart/signed layer - useful when
571 if (WithCrypto && mutt_is_multipart_signed (newhdr->content)) {
572 newhdr->security |= SIGN;
573 if ((WithCrypto & APPLICATION_PGP)
575 ascii_strcasecmp (mutt_get_parameter
576 ("protocol", newhdr->content->parameter),
577 "application/pgp-signature") == 0)
578 newhdr->security |= APPLICATION_PGP;
579 else if ((WithCrypto & APPLICATION_SMIME))
580 newhdr->security |= APPLICATION_SMIME;
582 /* destroy the signature */
583 mutt_free_body (&newhdr->content->parts->next);
584 newhdr->content = mutt_remove_multipart (newhdr->content);
589 * We don't need no primary multipart.
590 * Note: We _do_ preserve messages!
592 * XXX - we don't handle multipart/alternative in any
593 * smart way when sending messages. However, one may
594 * consider this a feature.
598 if (newhdr->content->type == TYPEMULTIPART)
599 newhdr->content = mutt_remove_multipart (newhdr->content);
603 /* create temporary files for all attachments */
604 for (b = newhdr->content; b; b = b->next) {
606 /* what follows is roughly a receive-mode variant of
607 * mutt_get_tmp_attachment () from muttlib.c
612 strfcpy (file, b->filename, sizeof (file));
613 b->d_filename = str_dup (b->filename);
616 /* avoid Content-Disposition: header with temporary filename */
620 /* set up state flags */
624 if (b->type == TYPETEXT) {
625 if (!ascii_strcasecmp
626 ("yes", mutt_get_parameter ("x-mutt-noconv", b->parameter)))
629 s.flags |= M_CHARCONV;
633 mutt_delete_parameter ("x-mutt-noconv", &b->parameter);
636 mutt_adv_mktemp (NULL, file, sizeof (file));
637 if ((s.fpout = safe_fopen (file, "w")) == NULL)
641 if ((WithCrypto & APPLICATION_PGP)
642 && (mutt_is_application_pgp (b) & (ENCRYPT | SIGN))) {
644 mutt_body_handler (b, &s);
646 newhdr->security |= mutt_is_application_pgp (newhdr->content);
649 str_replace (&b->subtype, "plain");
650 mutt_delete_parameter ("x-action", &b->parameter);
653 mutt_decode_attachment (b, &s);
655 if (safe_fclose (&s.fpout) != 0)
658 str_replace (&b->filename, file);
661 mutt_stamp_attachment (b);
663 mutt_free_body (&b->parts);
665 b->hdr->content = NULL; /* avoid dangling pointer */
668 /* Fix encryption flags. */
670 /* No inline if multipart. */
671 if (WithCrypto && (newhdr->security & INLINE) && newhdr->content->next)
672 newhdr->security &= ~INLINE;
674 /* Do we even support multiple mechanisms? */
675 newhdr->security &= WithCrypto | ~(APPLICATION_PGP | APPLICATION_SMIME);
677 /* Theoretically, both could be set. Take the one the user wants to set by default. */
678 if ((newhdr->security & APPLICATION_PGP)
679 && (newhdr->security & APPLICATION_SMIME)) {
680 if (option (OPTSMIMEISDEFAULT))
681 newhdr->security &= ~APPLICATION_PGP;
683 newhdr->security &= ~APPLICATION_SMIME;
694 mx_close_message (&msg);
697 mutt_free_envelope (&newhdr->env);
698 mutt_free_body (&newhdr->content);