2 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
3 * Copyright (C) 1999-2002,2004 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include "mutt_menu.h"
33 #include "mutt_crypt.h"
40 static struct mapping_t PostponeHelp[] = {
41 { N_("Exit"), OP_EXIT },
42 { N_("Del"), OP_DELETE },
43 { N_("Undel"), OP_UNDELETE },
44 { N_("Help"), OP_HELP },
50 static short PostCount = 0;
51 static CONTEXT *PostContext = NULL;
52 static short UpdateNumPostponed = 0;
54 /* Return the number of postponed messages.
55 * if force is 0, use a cached value if it is costly to get a fresh
56 * count (IMAP) - else check.
58 int mutt_num_postponed (int force)
63 static time_t LastModify = 0;
64 static char *OldPostponed = NULL;
66 if (UpdateNumPostponed)
68 UpdateNumPostponed = 0;
72 if (mutt_strcmp (Postponed, OldPostponed))
75 OldPostponed = safe_strdup (Postponed);
84 /* LastModify is useless for IMAP */
85 if (mx_is_imap (Postponed))
91 newpc = imap_status (Postponed, 0);
95 dprint (3, (debugfile, "mutt_num_postponed: %d postponed IMAP messages found.\n", PostCount));
98 dprint (3, (debugfile, "mutt_num_postponed: using old IMAP postponed count.\n"));
104 if (stat (Postponed, &st) == -1)
111 if (S_ISDIR (st.st_mode))
113 /* if we have a maildir mailbox, we need to stat the "new" dir */
115 char buf[_POSIX_PATH_MAX];
117 snprintf (buf, sizeof (buf), "%s/new", Postponed);
118 if (access (buf, F_OK) == 0 && stat (buf, &st) == -1)
126 if (LastModify < st.st_mtime)
128 LastModify = st.st_mtime;
130 if (access (Postponed, R_OK | F_OK) != 0)
131 return (PostCount = 0);
132 if (mx_open_mailbox (Postponed, M_NOSORT | M_QUIET, &ctx) == NULL)
135 PostCount = ctx.msgcount;
136 mx_fastclose_mailbox (&ctx);
142 void mutt_update_num_postponed (void)
144 UpdateNumPostponed = 1;
147 static void post_entry (char *s, size_t slen, MUTTMENU *menu, int entry)
149 CONTEXT *ctx = (CONTEXT *) menu->data;
151 _mutt_make_string (s, slen, NONULL (HdrFmt), ctx, ctx->hdrs[entry],
152 M_FORMAT_ARROWCURSOR);
155 static HEADER *select_msg (void)
159 char helpstr[LONG_STRING];
162 menu = mutt_new_menu (MENU_POST);
163 menu->make_entry = post_entry;
164 menu->max = PostContext->msgcount;
165 menu->title = _("Postponed Messages");
166 menu->data = PostContext;
167 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_POST, PostponeHelp);
169 /* The postponed mailbox is setup to have sorting disabled, but the global
170 * Sort variable may indicate something different. Sorting has to be
171 * disabled while the postpone menu is being displayed. */
177 switch (i = mutt_menuLoop (menu))
181 mutt_set_flag (PostContext, PostContext->hdrs[menu->current], M_DELETE, (i == OP_DELETE) ? 1 : 0);
182 PostCount = PostContext->msgcount - PostContext->deleted;
183 if (option (OPTRESOLVE) && menu->current < menu->max - 1)
185 menu->oldcurrent = menu->current;
187 if (menu->current >= menu->top + menu->pagelen)
189 menu->top = menu->current;
190 menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
193 menu->redraw |= REDRAW_MOTION_RESYNCH;
196 menu->redraw = REDRAW_CURRENT;
199 case OP_GENERIC_SELECT_ENTRY:
211 mutt_menuDestroy (&menu);
212 return (r > -1 ? PostContext->hdrs[r] : NULL);
216 * ctx Context info, used when recalling a message to which
218 * hdr envelope/attachment info for recalled message
219 * cur if message was a reply, `cur' is set to the message which
220 * `hdr' is in reply to
221 * fcc fcc for the recalled message
222 * fcclen max length of fcc
225 * -1 error/no messages
227 * SENDREPLY recalled message is a reply
229 int mutt_get_postponed (CONTEXT *ctx, HEADER *hdr, HEADER **cur, char *fcc, size_t fcclen)
232 int code = SENDPOSTPONED;
242 if ((PostContext = mx_open_mailbox (Postponed, M_NOSORT, NULL)) == NULL)
245 mutt_error _("No postponed messages.");
249 if (! PostContext->msgcount)
252 mx_close_mailbox (PostContext, NULL);
254 mutt_error _("No postponed messages.");
258 if (PostContext->msgcount == 1)
260 /* only one message, so just use that one. */
261 h = PostContext->hdrs[0];
263 else if ((h = select_msg ()) == NULL)
265 mx_close_mailbox (PostContext, NULL);
270 if (mutt_prepare_template (NULL, PostContext, hdr, h, 0) < 0)
272 mx_fastclose_mailbox (PostContext);
277 /* finished with this message, so delete it. */
278 mutt_set_flag (PostContext, h, M_DELETE, 1);
280 /* update the count for the status display */
281 PostCount = PostContext->msgcount - PostContext->deleted;
283 /* avoid the "purge deleted messages" prompt */
284 opt_delete = quadoption (OPT_DELETE);
285 set_quadoption (OPT_DELETE, M_YES);
286 mx_close_mailbox (PostContext, NULL);
287 set_quadoption (OPT_DELETE, opt_delete);
291 for (tmp = hdr->env->userhdrs; tmp; )
293 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)
322 strfcpy (fcc, p, fcclen);
323 mutt_pretty_mailbox (fcc, fcclen);
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 ((WithCrypto & APPLICATION_PGP)
336 && (mutt_strncmp ("Pgp:", tmp->data, 4) == 0 /* this is generated
337 * by old mutt versions
339 || mutt_strncmp ("X-Mutt-PGP:", tmp->data, 11) == 0))
341 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1,
343 hdr->security |= APPLICATION_PGP;
345 /* remove the pgp field */
348 last->next = tmp->next;
350 hdr->env->userhdrs = tmp->next;
352 mutt_free_list (&tmp);
355 else if ((WithCrypto & APPLICATION_SMIME)
356 && mutt_strncmp ("X-Mutt-SMIME:", tmp->data, 13) == 0)
358 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");
382 hdr->chain = mutt_add_list (hdr->chain, t);
383 t = strtok (NULL, " \t\n");
388 last->next = tmp->next;
390 hdr->env->userhdrs = tmp->next;
392 mutt_free_list (&tmp);
408 int mutt_parse_crypt_hdr (char *p, int set_signas, int crypt_app)
410 char smime_cryptalg[LONG_STRING] = "\0";
411 char sign_as[LONG_STRING] = "\0", *q;
436 *p && *p != '>' && q < sign_as + sizeof (sign_as) - 1;
442 mutt_error _("Illegal crypto header");
450 /* This used to be the micalg parameter.
452 * It's no longer needed, so we just skip the parameter in order
453 * to be able to recall old messages.
459 for (p += 2; *p && *p != '>'; p++)
463 mutt_error _("Illegal crypto header");
477 for(p += 2; *p && *p != '>' && q < smime_cryptalg + sizeof(smime_cryptalg) - 1;
483 mutt_error _("Illegal S/MIME header");
497 mutt_error _("Illegal crypto header");
503 /* the cryptalg field must not be empty */
504 if ((WithCrypto & APPLICATION_SMIME) && *smime_cryptalg)
505 mutt_str_replace (&SmimeCryptAlg, smime_cryptalg);
507 /* Set {Smime,Pgp}SignAs, if desired. */
509 if ((WithCrypto & APPLICATION_PGP) && (crypt_app == APPLICATION_PGP)
510 && (set_signas || *sign_as))
511 mutt_str_replace (&PgpSignAs, sign_as);
513 if ((WithCrypto & APPLICATION_SMIME) && (crypt_app == APPLICATION_SMIME)
514 && (set_signas || *sign_as))
515 mutt_str_replace (&SmimeDefaultKey, sign_as);
522 int mutt_prepare_template (FILE *fp, CONTEXT *ctx, HEADER *newhdr, HEADER *hdr,
526 char file[_POSIX_PATH_MAX];
533 memset (&s, 0, sizeof (s));
535 if (!fp && (msg = mx_open_message (ctx, hdr->msgno)) == NULL)
538 if (!fp) fp = msg->fp;
542 /* parse the message header and MIME structure */
544 fseeko (fp, hdr->offset, 0);
545 newhdr->offset = hdr->offset;
546 newhdr->env = mutt_read_rfc822_header (fp, newhdr, 1, weed);
547 newhdr->content->length = hdr->content->length;
548 mutt_parse_part (fp, newhdr->content);
550 FREE (&newhdr->env->message_id);
551 FREE (&newhdr->env->mail_followup_to); /* really? */
553 /* decrypt pgp/mime encoded messages */
555 if ((WithCrypto & (APPLICATION_PGP|APPLICATION_SMIME) & hdr->security)
556 && mutt_is_multipart_encrypted (newhdr->content))
558 int ccap = WithCrypto & (APPLICATION_PGP|APPLICATION_SMIME) & hdr->security;
559 newhdr->security |= ENCRYPT | ccap;
560 if (!crypt_valid_passphrase (ccap))
563 mutt_message _("Decrypting message...");
564 if (((ccap & APPLICATION_PGP) && crypt_pgp_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
565 || ((ccap & APPLICATION_SMIME) && crypt_smime_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
569 mx_close_message (&msg);
570 mutt_free_envelope (&newhdr->env);
571 mutt_free_body (&newhdr->content);
572 mutt_error _("Decryption failed.");
576 mutt_free_body (&newhdr->content);
583 * remove a potential multipart/signed layer - useful when
587 if (WithCrypto && mutt_is_multipart_signed (newhdr->content))
589 newhdr->security |= SIGN;
590 if ((WithCrypto & APPLICATION_PGP)
591 && ascii_strcasecmp (mutt_get_parameter ("protocol", newhdr->content->parameter), "application/pgp-signature") == 0)
592 newhdr->security |= APPLICATION_PGP;
593 else if ((WithCrypto & APPLICATION_SMIME))
594 newhdr->security |= APPLICATION_SMIME;
596 /* destroy the signature */
597 mutt_free_body (&newhdr->content->parts->next);
598 newhdr->content = mutt_remove_multipart (newhdr->content);
603 * We don't need no primary multipart.
604 * Note: We _do_ preserve messages!
606 * XXX - we don't handle multipart/alternative in any
607 * smart way when sending messages. However, one may
608 * consider this a feature.
612 if (newhdr->content->type == TYPEMULTIPART)
613 newhdr->content = mutt_remove_multipart (newhdr->content);
617 /* create temporary files for all attachments */
618 for (b = newhdr->content; b; b = b->next)
621 /* what follows is roughly a receive-mode variant of
622 * mutt_get_tmp_attachment () from muttlib.c
628 strfcpy (file, b->filename, sizeof (file));
629 b->d_filename = safe_strdup (b->filename);
633 /* avoid Content-Disposition: header with temporary filename */
637 /* set up state flags */
641 if (b->type == TYPETEXT)
643 if (!ascii_strcasecmp ("yes", mutt_get_parameter ("x-mutt-noconv", b->parameter)))
647 s.flags |= M_CHARCONV;
651 mutt_delete_parameter ("x-mutt-noconv", &b->parameter);
654 mutt_adv_mktemp (file, sizeof(file));
655 if ((s.fpout = safe_fopen (file, "w")) == NULL)
659 if ((WithCrypto & APPLICATION_PGP)
660 && (mutt_is_application_pgp (b) & (ENCRYPT|SIGN)))
663 mutt_body_handler (b, &s);
665 newhdr->security |= mutt_is_application_pgp (newhdr->content);
668 mutt_str_replace (&b->subtype, "plain");
669 mutt_delete_parameter ("x-action", &b->parameter);
672 mutt_decode_attachment (b, &s);
674 if (safe_fclose (&s.fpout) != 0)
677 mutt_str_replace (&b->filename, file);
680 mutt_stamp_attachment (b);
682 mutt_free_body (&b->parts);
683 if (b->hdr) b->hdr->content = NULL; /* avoid dangling pointer */
686 /* Fix encryption flags. */
688 /* No inline if multipart. */
689 if (WithCrypto && (newhdr->security & INLINE) && newhdr->content->next)
690 newhdr->security &= ~INLINE;
692 /* Do we even support multiple mechanisms? */
693 newhdr->security &= WithCrypto | ~(APPLICATION_PGP|APPLICATION_SMIME);
695 /* Theoretically, both could be set. Take the one the user wants to set by default. */
696 if ((newhdr->security & APPLICATION_PGP) && (newhdr->security & APPLICATION_SMIME))
698 if (option (OPTSMIMEISDEFAULT))
699 newhdr->security &= ~APPLICATION_PGP;
701 newhdr->security &= ~APPLICATION_SMIME;
709 if (bfp != fp) safe_fclose (&bfp);
710 if (msg) mx_close_message (&msg);
714 mutt_free_envelope (&newhdr->env);
715 mutt_free_body (&newhdr->content);