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");
502 mutt_error _("Illegal PGP header");
508 /* the cryptalg field must not be empty */
509 if ((WithCrypto & APPLICATION_SMIME) && *smime_cryptalg)
510 mutt_str_replace (&SmimeCryptAlg, smime_cryptalg);
512 if ((WithCrypto & APPLICATION_PGP) && (set_signas || *pgp_sign_as))
513 mutt_str_replace (&PgpSignAs, pgp_sign_as);
520 int mutt_prepare_template (FILE *fp, CONTEXT *ctx, HEADER *newhdr, HEADER *hdr,
524 char file[_POSIX_PATH_MAX];
531 memset (&s, 0, sizeof (s));
533 if (!fp && (msg = mx_open_message (ctx, hdr->msgno)) == NULL)
536 if (!fp) fp = msg->fp;
540 /* parse the message header and MIME structure */
542 fseek (fp, hdr->offset, 0);
543 newhdr->offset = hdr->offset;
544 newhdr->env = mutt_read_rfc822_header (fp, newhdr, 1, weed);
545 newhdr->content->length = hdr->content->length;
546 mutt_parse_part (fp, newhdr->content);
548 FREE (&newhdr->env->message_id);
549 FREE (&newhdr->env->mail_followup_to); /* really? */
551 /* decrypt pgp/mime encoded messages */
552 /* XXX - what happens with S/MIME encrypted messages?!?!? - tlr, 020909*/
553 if ((WithCrypto & APPLICATION_PGP)
554 && (hdr->security & APPLICATION_PGP)
555 && mutt_is_multipart_encrypted (newhdr->content))
557 newhdr->security |= PGPENCRYPT;
558 if (!crypt_valid_passphrase(APPLICATION_PGP))
561 mutt_message _("Invoking PGP...");
562 if (crypt_pgp_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1 || b == NULL)
565 mx_close_message (&msg);
566 mutt_free_envelope (&newhdr->env);
567 mutt_free_body (&newhdr->content);
568 mutt_error _("Decryption failed.");
572 mutt_free_body (&newhdr->content);
579 * remove a potential multipart/signed layer - useful when
583 if (WithCrypto && mutt_is_multipart_signed (newhdr->content))
585 newhdr->security |= SIGN;
586 if ((WithCrypto & APPLICATION_PGP)
587 && ascii_strcasecmp (mutt_get_parameter ("protocol", newhdr->content->parameter), "application/pgp-signature") == 0)
588 newhdr->security |= APPLICATION_PGP;
589 else if ((WithCrypto & APPLICATION_SMIME))
590 newhdr->security |= APPLICATION_SMIME;
592 /* destroy the signature */
593 mutt_free_body (&newhdr->content->parts->next);
594 newhdr->content = mutt_remove_multipart (newhdr->content);
598 * We don't need no primary multipart.
599 * Note: We _do_ preserve messages!
601 * XXX - we don't handle multipart/alternative in any
602 * smart way when sending messages. However, one may
603 * consider this a feature.
607 if (newhdr->content->type == TYPEMULTIPART)
608 newhdr->content = mutt_remove_multipart (newhdr->content);
612 /* create temporary files for all attachments */
613 for (b = newhdr->content; b; b = b->next)
616 /* what follows is roughly a receive-mode variant of
617 * mutt_get_tmp_attachment () from muttlib.c
623 strfcpy (file, b->filename, sizeof (file));
624 b->d_filename = safe_strdup (b->filename);
628 /* avoid Content-Disposition: header with temporary filename */
632 /* set up state flags */
636 if (b->type == TYPETEXT)
638 if (!ascii_strcasecmp ("yes", mutt_get_parameter ("x-mutt-noconv", b->parameter)))
642 s.flags |= M_CHARCONV;
646 mutt_delete_parameter ("x-mutt-noconv", &b->parameter);
649 mutt_adv_mktemp (file, sizeof(file));
650 if ((s.fpout = safe_fopen (file, "w")) == NULL)
654 mutt_decode_attachment (b, &s);
656 if (safe_fclose (&s.fpout) != 0)
659 mutt_str_replace (&b->filename, file);
662 mutt_stamp_attachment (b);
664 mutt_free_body (&b->parts);
665 if (b->hdr) b->hdr->content = NULL; /* avoid dangling pointer */
673 if (bfp != fp) fclose (bfp);
674 if (msg) mx_close_message (&msg);
678 mutt_free_envelope (&newhdr->env);
679 mutt_free_body (&newhdr->content);