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.
21 #include "mutt_menu.h"
31 #include "mutt_crypt.h"
38 static struct mapping_t PostponeHelp[] = {
39 { N_("Exit"), OP_EXIT },
40 { N_("Del"), OP_DELETE },
41 { N_("Undel"), OP_UNDELETE },
42 { N_("Help"), OP_HELP },
48 static short PostCount = 0;
49 static CONTEXT *PostContext = NULL;
50 static short UpdateNumPostponed = 0;
52 /* Return the number of postponed messages.
53 * if force is 0, use a cached value if it is costly to get a fresh
54 * count (IMAP) - else check.
56 int mutt_num_postponed (int force)
61 static time_t LastModify = 0;
62 static char *OldPostponed = NULL;
64 if (UpdateNumPostponed)
66 UpdateNumPostponed = 0;
70 if (Postponed != OldPostponed)
72 OldPostponed = Postponed;
81 /* LastModify is useless for IMAP */
82 if (mx_is_imap (Postponed))
88 newpc = imap_mailbox_check (Postponed, 0);
92 dprint (2, (debugfile, "mutt_num_postponed: %d postponed IMAP messages found.\n", PostCount));
95 dprint (2, (debugfile, "mutt_num_postponed: using old IMAP postponed count.\n"));
101 if (stat (Postponed, &st) == -1)
108 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)
123 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)
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;
176 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_POST, PostponeHelp);
178 /* The postponed mailbox is setup to have sorting disabled, but the global
179 * Sort variable may indicate something different. Sorting has to be
180 * disabled while the postpone menu is being displayed. */
186 switch (i = mutt_menuLoop (menu))
190 mutt_set_flag (PostContext, PostContext->hdrs[menu->current], M_DELETE, (i == OP_DELETE) ? 1 : 0);
191 PostCount = PostContext->msgcount - PostContext->deleted;
192 if (option (OPTRESOLVE) && menu->current < menu->max - 1)
194 menu->oldcurrent = menu->current;
196 if (menu->current >= menu->top + menu->pagelen)
198 menu->top = menu->current;
199 menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
202 menu->redraw |= REDRAW_MOTION_RESYNCH;
205 menu->redraw = REDRAW_CURRENT;
208 case OP_GENERIC_SELECT_ENTRY:
220 mutt_menuDestroy (&menu);
221 return (r > -1 ? PostContext->hdrs[r] : NULL);
225 * ctx Context info, used when recalling a message to which
227 * hdr envelope/attachment info for recalled message
228 * cur if message was a reply, `cur' is set to the message which
229 * `hdr' is in reply to
230 * fcc fcc for the recalled message
231 * fcclen max length of fcc
234 * -1 error/no messages
236 * SENDREPLY recalled message is a reply
238 int mutt_get_postponed (CONTEXT *ctx, HEADER *hdr, HEADER **cur, char *fcc, size_t fcclen)
241 int code = SENDPOSTPONED;
251 if ((PostContext = mx_open_mailbox (Postponed, M_NOSORT, NULL)) == NULL)
254 mutt_error _("No postponed messages.");
258 if (! PostContext->msgcount)
261 mx_close_mailbox (PostContext, NULL);
263 mutt_error _("No postponed messages.");
267 if (PostContext->msgcount == 1)
269 /* only one message, so just use that one. */
270 h = PostContext->hdrs[0];
272 else if ((h = select_msg ()) == NULL)
274 mx_close_mailbox (PostContext, NULL);
279 if (mutt_prepare_template (NULL, PostContext, hdr, h, 0) < 0)
281 mx_fastclose_mailbox (PostContext);
286 /* finished with this message, so delete it. */
287 mutt_set_flag (PostContext, h, M_DELETE, 1);
289 /* and consider it saved, so that it won't be moved to the trash folder */
290 mutt_set_flag (PostContext, h, M_APPENDED, 1);
292 /* update the count for the status display */
293 PostCount = PostContext->msgcount - PostContext->deleted;
295 /* avoid the "purge deleted messages" prompt */
296 opt_delete = quadoption (OPT_DELETE);
297 set_quadoption (OPT_DELETE, M_YES);
298 mx_close_mailbox (PostContext, NULL);
299 set_quadoption (OPT_DELETE, opt_delete);
303 for (tmp = hdr->env->userhdrs; tmp; )
305 if (ascii_strncasecmp ("X-Mutt-References:", tmp->data, 18) == 0)
309 /* if a mailbox is currently open, look to see if the orignal message
310 the user attempted to reply to is in this mailbox */
314 ctx->id_hash = mutt_make_id_hash (ctx);
315 *cur = hash_find (ctx->id_hash, p);
318 /* Remove the X-Mutt-References: header field. */
321 last->next = tmp->next;
323 hdr->env->userhdrs = tmp->next;
325 mutt_free_list (&tmp);
330 else if (ascii_strncasecmp ("X-Mutt-Fcc:", tmp->data, 11) == 0)
334 strfcpy (fcc, p, fcclen);
335 mutt_pretty_mailbox (fcc);
337 /* remove the X-Mutt-Fcc: header field */
340 last->next = tmp->next;
342 hdr->env->userhdrs = tmp->next;
344 mutt_free_list (&tmp);
347 else if ((WithCrypto & APPLICATION_PGP)
348 && (mutt_strncmp ("Pgp:", tmp->data, 4) == 0 /* this is generated
349 * by old mutt versions
351 || mutt_strncmp ("X-Mutt-PGP:", tmp->data, 11) == 0))
353 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
354 hdr->security |= APPLICATION_PGP;
356 /* remove the pgp field */
359 last->next = tmp->next;
361 hdr->env->userhdrs = tmp->next;
363 mutt_free_list (&tmp);
366 else if ((WithCrypto & APPLICATION_SMIME)
367 && mutt_strncmp ("X-Mutt-SMIME:", tmp->data, 13) == 0)
369 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
370 hdr->security |= APPLICATION_SMIME;
372 /* remove the smime field */
375 last->next = tmp->next;
377 hdr->env->userhdrs = tmp->next;
379 mutt_free_list (&tmp);
384 else if (mutt_strncmp ("X-Mutt-Mix:", tmp->data, 11) == 0)
387 mutt_free_list (&hdr->chain);
389 t = strtok (tmp->data + 11, " \t\n");
392 hdr->chain = mutt_add_list (hdr->chain, t);
393 t = strtok (NULL, " \t\n");
398 last->next = tmp->next;
400 hdr->env->userhdrs = tmp->next;
402 mutt_free_list (&tmp);
418 int mutt_parse_crypt_hdr (char *p, int set_signas)
421 char pgp_sign_as[LONG_STRING] = "\0", *q;
422 char smime_cryptalg[LONG_STRING] = "\0";
446 *p && *p != '>' && q < pgp_sign_as + sizeof (pgp_sign_as) - 1;
452 mutt_error _("Illegal PGP header");
460 /* This used to be the micalg parameter.
462 * It's no longer needed, so we just skip the parameter in order
463 * to be able to recall old messages.
469 for (p += 2; *p && *p != '>'; p++)
473 mutt_error _("Illegal PGP header");
487 for(p += 2; *p && *p != '>' && q < smime_cryptalg + sizeof(smime_cryptalg) - 1;
493 mutt_error _("Illegal S/MIME header");
507 mutt_error _("Illegal PGP header");
513 /* the cryptalg field must not be empty */
514 if ((WithCrypto & APPLICATION_SMIME) && *smime_cryptalg)
515 mutt_str_replace (&SmimeCryptAlg, smime_cryptalg);
517 if ((WithCrypto & APPLICATION_PGP) && (set_signas || *pgp_sign_as))
518 mutt_str_replace (&PgpSignAs, pgp_sign_as);
525 int mutt_prepare_template (FILE *fp, CONTEXT *ctx, HEADER *newhdr, HEADER *hdr,
529 char file[_POSIX_PATH_MAX];
536 memset (&s, 0, sizeof (s));
538 if (!fp && (msg = mx_open_message (ctx, hdr->msgno)) == NULL)
541 if (!fp) fp = msg->fp;
545 /* parse the message header and MIME structure */
547 fseek (fp, hdr->offset, 0);
548 newhdr->offset = hdr->offset;
549 newhdr->env = mutt_read_rfc822_header (fp, newhdr, 1, weed);
550 newhdr->content->length = hdr->content->length;
551 mutt_parse_part (fp, newhdr->content);
553 FREE (&newhdr->env->message_id);
554 FREE (&newhdr->env->mail_followup_to); /* really? */
556 /* decrypt pgp/mime encoded messages */
558 if ((WithCrypto & (APPLICATION_PGP|APPLICATION_SMIME) & hdr->security)
559 && mutt_is_multipart_encrypted (newhdr->content))
561 int ccap = WithCrypto & (APPLICATION_PGP|APPLICATION_SMIME) & hdr->security;
562 newhdr->security |= ENCRYPT | ccap;
563 if (!crypt_valid_passphrase (ccap))
566 mutt_message _("Decrypting message...");
567 if (((ccap & APPLICATION_PGP) && crypt_pgp_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
568 || ((ccap & APPLICATION_SMIME) && crypt_smime_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
572 mx_close_message (&msg);
573 mutt_free_envelope (&newhdr->env);
574 mutt_free_body (&newhdr->content);
575 mutt_error _("Decryption failed.");
579 mutt_free_body (&newhdr->content);
586 * remove a potential multipart/signed layer - useful when
590 if (WithCrypto && mutt_is_multipart_signed (newhdr->content))
592 newhdr->security |= SIGN;
593 if ((WithCrypto & APPLICATION_PGP)
594 && ascii_strcasecmp (mutt_get_parameter ("protocol", newhdr->content->parameter), "application/pgp-signature") == 0)
595 newhdr->security |= APPLICATION_PGP;
596 else if ((WithCrypto & APPLICATION_SMIME))
597 newhdr->security |= APPLICATION_SMIME;
599 /* destroy the signature */
600 mutt_free_body (&newhdr->content->parts->next);
601 newhdr->content = mutt_remove_multipart (newhdr->content);
606 * We don't need no primary multipart.
607 * Note: We _do_ preserve messages!
609 * XXX - we don't handle multipart/alternative in any
610 * smart way when sending messages. However, one may
611 * consider this a feature.
615 if (newhdr->content->type == TYPEMULTIPART)
616 newhdr->content = mutt_remove_multipart (newhdr->content);
620 /* create temporary files for all attachments */
621 for (b = newhdr->content; b; b = b->next)
624 /* what follows is roughly a receive-mode variant of
625 * mutt_get_tmp_attachment () from muttlib.c
631 strfcpy (file, b->filename, sizeof (file));
632 b->d_filename = safe_strdup (b->filename);
636 /* avoid Content-Disposition: header with temporary filename */
640 /* set up state flags */
644 if (b->type == TYPETEXT)
646 if (!ascii_strcasecmp ("yes", mutt_get_parameter ("x-mutt-noconv", b->parameter)))
650 s.flags |= M_CHARCONV;
654 mutt_delete_parameter ("x-mutt-noconv", &b->parameter);
657 mutt_adv_mktemp (file, sizeof(file));
658 if ((s.fpout = safe_fopen (file, "w")) == NULL)
662 if ((WithCrypto & APPLICATION_PGP)
663 && (mutt_is_application_pgp (b) & (ENCRYPT|SIGN)))
666 mutt_body_handler (b, &s);
668 newhdr->security |= mutt_is_application_pgp (newhdr->content);
671 mutt_str_replace (&b->subtype, "plain");
672 mutt_delete_parameter ("x-action", &b->parameter);
675 mutt_decode_attachment (b, &s);
677 if (safe_fclose (&s.fpout) != 0)
680 mutt_str_replace (&b->filename, file);
683 mutt_stamp_attachment (b);
685 mutt_free_body (&b->parts);
686 if (b->hdr) b->hdr->content = NULL; /* avoid dangling pointer */
689 /* Fix encryption flags. */
691 /* No inline if multipart. */
692 if (WithCrypto && (newhdr->security & INLINE) && newhdr->content->next)
693 newhdr->security &= ~INLINE;
695 /* Do we even support multiple mechanisms? */
696 newhdr->security &= WithCrypto | ~(APPLICATION_PGP|APPLICATION_SMIME);
698 /* Theoretically, both could be set. Take the one the user wants to set by default. */
699 if ((newhdr->security & APPLICATION_PGP) && (newhdr->security & APPLICATION_SMIME))
701 if (option (OPTSMIMEISDEFAULT))
702 newhdr->security &= ~APPLICATION_PGP;
704 newhdr->security &= ~APPLICATION_SMIME;
712 if (bfp != fp) fclose (bfp);
713 if (msg) mx_close_message (&msg);
717 mutt_free_envelope (&newhdr->env);
718 mutt_free_body (&newhdr->content);