2 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
3 * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
25 #include "mutt_menu.h"
35 #include "mutt_crypt.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 (mx_is_imap (Postponed)) {
88 newpc = imap_mailbox_check (Postponed, 0);
93 "mutt_num_postponed: %d postponed IMAP messages found.\n",
99 "mutt_num_postponed: using old IMAP postponed count.\n"));
105 if (stat (Postponed, &st) == -1) {
111 if (S_ISDIR (st.st_mode)) {
112 /* if we have a maildir mailbox, we need to stat the "new" dir */
114 char buf[_POSIX_PATH_MAX];
116 snprintf (buf, sizeof (buf), "%s/new", Postponed);
117 if (access (buf, F_OK) == 0 && stat (buf, &st) == -1) {
124 if (LastModify < st.st_mtime) {
126 int optnews = option (OPTNEWS);
128 LastModify = st.st_mtime;
130 if (access (Postponed, R_OK | F_OK) != 0)
131 return (PostCount = 0);
134 unset_option (OPTNEWS);
136 if (mx_open_mailbox (Postponed, M_NOSORT | M_QUIET, &ctx) == NULL)
139 PostCount = ctx.msgcount;
140 mx_fastclose_mailbox (&ctx);
143 set_option (OPTNEWS);
150 void mutt_update_num_postponed (void)
152 UpdateNumPostponed = 1;
155 static void post_entry (char *s, size_t slen, MUTTMENU * menu, int entry)
157 CONTEXT *ctx = (CONTEXT *) menu->data;
159 _mutt_make_string (s, slen, NONULL (HdrFmt), ctx, ctx->hdrs[entry],
160 M_FORMAT_ARROWCURSOR);
163 static HEADER *select_msg (void)
166 int i, done = 0, r = -1;
167 char helpstr[SHORT_STRING];
170 menu = mutt_new_menu ();
171 menu->make_entry = post_entry;
172 menu->menu = MENU_POST;
173 menu->max = PostContext->msgcount;
174 menu->title = _("Postponed Messages");
175 menu->data = PostContext;
177 mutt_compile_help (helpstr, sizeof (helpstr), MENU_POST, PostponeHelp);
179 /* The postponed mailbox is setup to have sorting disabled, but the global
180 * Sort variable may indicate something different. Sorting has to be
181 * disabled while the postpone menu is being displayed. */
186 switch (i = mutt_menuLoop (menu)) {
189 mutt_set_flag (PostContext, PostContext->hdrs[menu->current], M_DELETE,
190 (i == OP_DELETE) ? 1 : 0);
191 PostCount = PostContext->msgcount - PostContext->deleted;
192 if (option (OPTRESOLVE) && menu->current < menu->max - 1) {
193 menu->oldcurrent = menu->current;
195 if (menu->current >= menu->top + menu->pagelen) {
196 menu->top = menu->current;
197 menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
200 menu->redraw |= REDRAW_MOTION_RESYNCH;
203 menu->redraw = REDRAW_CURRENT;
206 case OP_GENERIC_SELECT_ENTRY:
218 mutt_menuDestroy (&menu);
219 return (r > -1 ? PostContext->hdrs[r] : NULL);
223 * ctx Context info, used when recalling a message to which
225 * hdr envelope/attachment info for recalled message
226 * cur if message was a reply, `cur' is set to the message which
227 * `hdr' is in reply to
228 * fcc fcc for the recalled message
229 * fcclen max length of fcc
232 * -1 error/no messages
234 * SENDREPLY recalled message is a reply
236 int mutt_get_postponed (CONTEXT * ctx, HEADER * hdr, HEADER ** cur, char *fcc,
240 int code = SENDPOSTPONED;
250 if ((PostContext = mx_open_mailbox (Postponed, M_NOSORT, NULL)) == NULL) {
252 mutt_error _("No postponed messages.");
257 if (!PostContext->msgcount) {
259 mx_close_mailbox (PostContext, NULL);
261 mutt_error _("No postponed messages.");
266 if (PostContext->msgcount == 1) {
267 /* only one message, so just use that one. */
268 h = PostContext->hdrs[0];
270 else if ((h = select_msg ()) == NULL) {
271 mx_close_mailbox (PostContext, NULL);
276 if (mutt_prepare_template (NULL, PostContext, hdr, h, 0) < 0) {
277 mx_fastclose_mailbox (PostContext);
282 /* finished with this message, so delete it. */
283 mutt_set_flag (PostContext, h, M_DELETE, 1);
285 /* and consider it saved, so that it won't be moved to the trash folder */
286 mutt_set_flag (PostContext, h, M_APPENDED, 1);
288 /* update the count for the status display */
289 PostCount = PostContext->msgcount - PostContext->deleted;
291 /* avoid the "purge deleted messages" prompt */
292 opt_delete = quadoption (OPT_DELETE);
293 set_quadoption (OPT_DELETE, M_YES);
294 mx_close_mailbox (PostContext, NULL);
295 set_quadoption (OPT_DELETE, opt_delete);
299 for (tmp = hdr->env->userhdrs; tmp;) {
300 if (ascii_strncasecmp ("X-Mutt-References:", tmp->data, 18) == 0) {
302 /* if a mailbox is currently open, look to see if the orignal message
303 the user attempted to reply to is in this mailbox */
307 ctx->id_hash = mutt_make_id_hash (ctx);
308 *cur = hash_find (ctx->id_hash, p);
311 /* Remove the X-Mutt-References: header field. */
314 last->next = tmp->next;
316 hdr->env->userhdrs = tmp->next;
318 mutt_free_list (&tmp);
323 else if (ascii_strncasecmp ("X-Mutt-Fcc:", tmp->data, 11) == 0) {
326 strfcpy (fcc, p, fcclen);
327 mutt_pretty_mailbox (fcc);
329 /* remove the X-Mutt-Fcc: header field */
332 last->next = tmp->next;
334 hdr->env->userhdrs = tmp->next;
336 mutt_free_list (&tmp);
339 else if ((WithCrypto & APPLICATION_PGP)
340 && (mutt_strncmp ("Pgp:", tmp->data, 4) == 0 /* this is generated
341 * by old mutt versions
343 || mutt_strncmp ("X-Mutt-PGP:", tmp->data, 11) == 0)) {
344 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
345 hdr->security |= APPLICATION_PGP;
347 /* remove the pgp field */
350 last->next = tmp->next;
352 hdr->env->userhdrs = tmp->next;
354 mutt_free_list (&tmp);
357 else if ((WithCrypto & APPLICATION_SMIME)
358 && mutt_strncmp ("X-Mutt-SMIME:", tmp->data, 13) == 0) {
359 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
360 hdr->security |= APPLICATION_SMIME;
362 /* remove the smime field */
365 last->next = tmp->next;
367 hdr->env->userhdrs = tmp->next;
369 mutt_free_list (&tmp);
374 else if (mutt_strncmp ("X-Mutt-Mix:", tmp->data, 11) == 0) {
377 mutt_free_list (&hdr->chain);
379 t = strtok (tmp->data + 11, " \t\n");
381 hdr->chain = mutt_add_list (hdr->chain, t);
382 t = strtok (NULL, " \t\n");
387 last->next = tmp->next;
389 hdr->env->userhdrs = tmp->next;
391 mutt_free_list (&tmp);
406 int mutt_parse_crypt_hdr (char *p, int set_signas)
409 char pgp_sign_as[LONG_STRING] = "\0", *q;
410 char smime_cryptalg[LONG_STRING] = "\0";
429 if (*(p + 1) == '<') {
431 *p && *p != '>' && q < pgp_sign_as + sizeof (pgp_sign_as) - 1;
435 mutt_error _("Illegal PGP header");
444 /* This used to be the micalg parameter.
446 * It's no longer needed, so we just skip the parameter in order
447 * to be able to recall old messages.
451 if (*(p + 1) == '<') {
452 for (p += 2; *p && *p != '>'; p++);
454 mutt_error _("Illegal PGP header");
467 if (*(p + 1) == '<') {
470 && q < smime_cryptalg + sizeof (smime_cryptalg) - 1;
474 mutt_error _("Illegal S/MIME header");
489 mutt_error _("Illegal PGP header");
495 /* the cryptalg field must not be empty */
496 if ((WithCrypto & APPLICATION_SMIME) && *smime_cryptalg)
497 mutt_str_replace (&SmimeCryptAlg, smime_cryptalg);
499 if ((WithCrypto & APPLICATION_PGP) && (set_signas || *pgp_sign_as))
500 mutt_str_replace (&PgpSignAs, pgp_sign_as);
507 int mutt_prepare_template (FILE * fp, CONTEXT * ctx, HEADER * newhdr,
508 HEADER * hdr, short weed)
511 char file[_POSIX_PATH_MAX];
518 memset (&s, 0, sizeof (s));
520 if (!fp && (msg = mx_open_message (ctx, hdr->msgno)) == NULL)
528 /* parse the message header and MIME structure */
530 fseek (fp, hdr->offset, 0);
531 newhdr->offset = hdr->offset;
532 newhdr->env = mutt_read_rfc822_header (fp, newhdr, 1, weed);
533 newhdr->content->length = hdr->content->length;
534 mutt_parse_part (fp, newhdr->content);
536 FREE (&newhdr->env->message_id);
537 FREE (&newhdr->env->mail_followup_to); /* really? */
539 /* decrypt pgp/mime encoded messages */
541 if ((WithCrypto & (APPLICATION_PGP | APPLICATION_SMIME) & hdr->security)
542 && mutt_is_multipart_encrypted (newhdr->content)) {
544 WithCrypto & (APPLICATION_PGP | APPLICATION_SMIME) & hdr->security;
545 newhdr->security |= ENCRYPT | ccap;
546 if (!crypt_valid_passphrase (ccap))
549 mutt_message _("Decrypting message...");
551 if (((ccap & APPLICATION_PGP)
552 && crypt_pgp_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
553 || ((ccap & APPLICATION_SMIME)
554 && crypt_smime_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
557 mx_close_message (&msg);
558 mutt_free_envelope (&newhdr->env);
559 mutt_free_body (&newhdr->content);
560 mutt_error _("Decryption failed.");
565 mutt_free_body (&newhdr->content);
572 * remove a potential multipart/signed layer - useful when
576 if (WithCrypto && mutt_is_multipart_signed (newhdr->content)) {
577 newhdr->security |= SIGN;
578 if ((WithCrypto & APPLICATION_PGP)
580 ascii_strcasecmp (mutt_get_parameter
581 ("protocol", newhdr->content->parameter),
582 "application/pgp-signature") == 0)
583 newhdr->security |= APPLICATION_PGP;
584 else if ((WithCrypto & APPLICATION_SMIME))
585 newhdr->security |= APPLICATION_SMIME;
587 /* destroy the signature */
588 mutt_free_body (&newhdr->content->parts->next);
589 newhdr->content = mutt_remove_multipart (newhdr->content);
594 * We don't need no primary multipart.
595 * Note: We _do_ preserve messages!
597 * XXX - we don't handle multipart/alternative in any
598 * smart way when sending messages. However, one may
599 * consider this a feature.
603 if (newhdr->content->type == TYPEMULTIPART)
604 newhdr->content = mutt_remove_multipart (newhdr->content);
608 /* create temporary files for all attachments */
609 for (b = newhdr->content; b; b = b->next) {
611 /* what follows is roughly a receive-mode variant of
612 * mutt_get_tmp_attachment () from muttlib.c
617 strfcpy (file, b->filename, sizeof (file));
618 b->d_filename = safe_strdup (b->filename);
621 /* avoid Content-Disposition: header with temporary filename */
625 /* set up state flags */
629 if (b->type == TYPETEXT) {
630 if (!ascii_strcasecmp
631 ("yes", mutt_get_parameter ("x-mutt-noconv", b->parameter)))
634 s.flags |= M_CHARCONV;
638 mutt_delete_parameter ("x-mutt-noconv", &b->parameter);
641 mutt_adv_mktemp (file, sizeof (file));
642 if ((s.fpout = safe_fopen (file, "w")) == NULL)
646 if ((WithCrypto & APPLICATION_PGP)
647 && (mutt_is_application_pgp (b) & (ENCRYPT | SIGN))) {
649 mutt_body_handler (b, &s);
651 newhdr->security |= mutt_is_application_pgp (newhdr->content);
654 mutt_str_replace (&b->subtype, "plain");
655 mutt_delete_parameter ("x-action", &b->parameter);
658 mutt_decode_attachment (b, &s);
660 if (safe_fclose (&s.fpout) != 0)
663 mutt_str_replace (&b->filename, file);
666 mutt_stamp_attachment (b);
668 mutt_free_body (&b->parts);
670 b->hdr->content = NULL; /* avoid dangling pointer */
673 /* Fix encryption flags. */
675 /* No inline if multipart. */
676 if (WithCrypto && (newhdr->security & INLINE) && newhdr->content->next)
677 newhdr->security &= ~INLINE;
679 /* Do we even support multiple mechanisms? */
680 newhdr->security &= WithCrypto | ~(APPLICATION_PGP | APPLICATION_SMIME);
682 /* Theoretically, both could be set. Take the one the user wants to set by default. */
683 if ((newhdr->security & APPLICATION_PGP)
684 && (newhdr->security & APPLICATION_SMIME)) {
685 if (option (OPTSMIMEISDEFAULT))
686 newhdr->security &= ~APPLICATION_PGP;
688 newhdr->security &= ~APPLICATION_SMIME;
699 mx_close_message (&msg);
702 mutt_free_envelope (&newhdr->env);
703 mutt_free_body (&newhdr->content);