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)
70 UpdateNumPostponed = 0;
74 if (Postponed != OldPostponed)
76 OldPostponed = Postponed;
85 /* LastModify is useless for IMAP */
86 if (mx_is_imap (Postponed))
92 newpc = imap_mailbox_check (Postponed, 0);
96 dprint (2, (debugfile, "mutt_num_postponed: %d postponed IMAP messages found.\n", PostCount));
99 dprint (2, (debugfile, "mutt_num_postponed: using old IMAP postponed count.\n"));
105 if (stat (Postponed, &st) == -1)
112 if (S_ISDIR (st.st_mode))
114 /* if we have a maildir mailbox, we need to stat the "new" dir */
116 char buf[_POSIX_PATH_MAX];
118 snprintf (buf, sizeof (buf), "%s/new", Postponed);
119 if (access (buf, F_OK) == 0 && stat (buf, &st) == -1)
127 if (LastModify < st.st_mtime)
130 int optnews = option (OPTNEWS);
132 LastModify = st.st_mtime;
134 if (access (Postponed, R_OK | F_OK) != 0)
135 return (PostCount = 0);
138 unset_option (OPTNEWS);
140 if (mx_open_mailbox (Postponed, M_NOSORT | M_QUIET, &ctx) == NULL)
143 PostCount = ctx.msgcount;
144 mx_fastclose_mailbox (&ctx);
147 set_option (OPTNEWS);
154 void mutt_update_num_postponed (void)
156 UpdateNumPostponed = 1;
159 static void post_entry (char *s, size_t slen, MUTTMENU *menu, int entry)
161 CONTEXT *ctx = (CONTEXT *) menu->data;
163 _mutt_make_string (s, slen, NONULL (HdrFmt), ctx, ctx->hdrs[entry],
164 M_FORMAT_ARROWCURSOR);
167 static HEADER *select_msg (void)
171 char helpstr[SHORT_STRING];
174 menu = mutt_new_menu ();
175 menu->make_entry = post_entry;
176 menu->menu = MENU_POST;
177 menu->max = PostContext->msgcount;
178 menu->title = _("Postponed Messages");
179 menu->data = PostContext;
180 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_POST, PostponeHelp);
182 /* The postponed mailbox is setup to have sorting disabled, but the global
183 * Sort variable may indicate something different. Sorting has to be
184 * disabled while the postpone menu is being displayed. */
190 switch (i = mutt_menuLoop (menu))
194 mutt_set_flag (PostContext, PostContext->hdrs[menu->current], M_DELETE, (i == OP_DELETE) ? 1 : 0);
195 PostCount = PostContext->msgcount - PostContext->deleted;
196 if (option (OPTRESOLVE) && menu->current < menu->max - 1)
198 menu->oldcurrent = menu->current;
200 if (menu->current >= menu->top + menu->pagelen)
202 menu->top = menu->current;
203 menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
206 menu->redraw |= REDRAW_MOTION_RESYNCH;
209 menu->redraw = REDRAW_CURRENT;
212 case OP_GENERIC_SELECT_ENTRY:
224 mutt_menuDestroy (&menu);
225 return (r > -1 ? PostContext->hdrs[r] : NULL);
229 * ctx Context info, used when recalling a message to which
231 * hdr envelope/attachment info for recalled message
232 * cur if message was a reply, `cur' is set to the message which
233 * `hdr' is in reply to
234 * fcc fcc for the recalled message
235 * fcclen max length of fcc
238 * -1 error/no messages
240 * SENDREPLY recalled message is a reply
242 int mutt_get_postponed (CONTEXT *ctx, HEADER *hdr, HEADER **cur, char *fcc, size_t fcclen)
245 int code = SENDPOSTPONED;
255 if ((PostContext = mx_open_mailbox (Postponed, M_NOSORT, NULL)) == NULL)
258 mutt_error _("No postponed messages.");
262 if (! PostContext->msgcount)
265 mx_close_mailbox (PostContext, NULL);
267 mutt_error _("No postponed messages.");
271 if (PostContext->msgcount == 1)
273 /* only one message, so just use that one. */
274 h = PostContext->hdrs[0];
276 else if ((h = select_msg ()) == NULL)
278 mx_close_mailbox (PostContext, NULL);
283 if (mutt_prepare_template (NULL, PostContext, hdr, h, 0) < 0)
285 mx_fastclose_mailbox (PostContext);
290 /* finished with this message, so delete it. */
291 mutt_set_flag (PostContext, h, M_DELETE, 1);
293 /* and consider it saved, so that it won't be moved to the trash folder */
294 mutt_set_flag (PostContext, h, M_APPENDED, 1);
296 /* update the count for the status display */
297 PostCount = PostContext->msgcount - PostContext->deleted;
299 /* avoid the "purge deleted messages" prompt */
300 opt_delete = quadoption (OPT_DELETE);
301 set_quadoption (OPT_DELETE, M_YES);
302 mx_close_mailbox (PostContext, NULL);
303 set_quadoption (OPT_DELETE, opt_delete);
307 for (tmp = hdr->env->userhdrs; tmp; )
309 if (ascii_strncasecmp ("X-Mutt-References:", tmp->data, 18) == 0)
313 /* if a mailbox is currently open, look to see if the orignal message
314 the user attempted to reply to is in this mailbox */
318 ctx->id_hash = mutt_make_id_hash (ctx);
319 *cur = hash_find (ctx->id_hash, p);
322 /* Remove the X-Mutt-References: header field. */
325 last->next = tmp->next;
327 hdr->env->userhdrs = tmp->next;
329 mutt_free_list (&tmp);
334 else if (ascii_strncasecmp ("X-Mutt-Fcc:", tmp->data, 11) == 0)
338 strfcpy (fcc, p, fcclen);
339 mutt_pretty_mailbox (fcc);
341 /* remove the X-Mutt-Fcc: header field */
344 last->next = tmp->next;
346 hdr->env->userhdrs = tmp->next;
348 mutt_free_list (&tmp);
351 else if ((WithCrypto & APPLICATION_PGP)
352 && (mutt_strncmp ("Pgp:", tmp->data, 4) == 0 /* this is generated
353 * by old mutt versions
355 || mutt_strncmp ("X-Mutt-PGP:", tmp->data, 11) == 0))
357 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
358 hdr->security |= APPLICATION_PGP;
360 /* remove the pgp field */
363 last->next = tmp->next;
365 hdr->env->userhdrs = tmp->next;
367 mutt_free_list (&tmp);
370 else if ((WithCrypto & APPLICATION_SMIME)
371 && mutt_strncmp ("X-Mutt-SMIME:", tmp->data, 13) == 0)
373 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
374 hdr->security |= APPLICATION_SMIME;
376 /* remove the smime field */
379 last->next = tmp->next;
381 hdr->env->userhdrs = tmp->next;
383 mutt_free_list (&tmp);
388 else if (mutt_strncmp ("X-Mutt-Mix:", tmp->data, 11) == 0)
391 mutt_free_list (&hdr->chain);
393 t = strtok (tmp->data + 11, " \t\n");
396 hdr->chain = mutt_add_list (hdr->chain, t);
397 t = strtok (NULL, " \t\n");
402 last->next = tmp->next;
404 hdr->env->userhdrs = tmp->next;
406 mutt_free_list (&tmp);
422 int mutt_parse_crypt_hdr (char *p, int set_signas)
425 char pgp_sign_as[LONG_STRING] = "\0", *q;
426 char smime_cryptalg[LONG_STRING] = "\0";
450 *p && *p != '>' && q < pgp_sign_as + sizeof (pgp_sign_as) - 1;
456 mutt_error _("Illegal PGP header");
464 /* This used to be the micalg parameter.
466 * It's no longer needed, so we just skip the parameter in order
467 * to be able to recall old messages.
473 for (p += 2; *p && *p != '>'; p++)
477 mutt_error _("Illegal PGP header");
491 for(p += 2; *p && *p != '>' && q < smime_cryptalg + sizeof(smime_cryptalg) - 1;
497 mutt_error _("Illegal S/MIME header");
511 mutt_error _("Illegal PGP header");
517 /* the cryptalg field must not be empty */
518 if ((WithCrypto & APPLICATION_SMIME) && *smime_cryptalg)
519 mutt_str_replace (&SmimeCryptAlg, smime_cryptalg);
521 if ((WithCrypto & APPLICATION_PGP) && (set_signas || *pgp_sign_as))
522 mutt_str_replace (&PgpSignAs, pgp_sign_as);
529 int mutt_prepare_template (FILE *fp, CONTEXT *ctx, HEADER *newhdr, HEADER *hdr,
533 char file[_POSIX_PATH_MAX];
540 memset (&s, 0, sizeof (s));
542 if (!fp && (msg = mx_open_message (ctx, hdr->msgno)) == NULL)
545 if (!fp) fp = msg->fp;
549 /* parse the message header and MIME structure */
551 fseek (fp, hdr->offset, 0);
552 newhdr->offset = hdr->offset;
553 newhdr->env = mutt_read_rfc822_header (fp, newhdr, 1, weed);
554 newhdr->content->length = hdr->content->length;
555 mutt_parse_part (fp, newhdr->content);
557 FREE (&newhdr->env->message_id);
558 FREE (&newhdr->env->mail_followup_to); /* really? */
560 /* decrypt pgp/mime encoded messages */
562 if ((WithCrypto & (APPLICATION_PGP|APPLICATION_SMIME) & hdr->security)
563 && mutt_is_multipart_encrypted (newhdr->content))
565 int ccap = WithCrypto & (APPLICATION_PGP|APPLICATION_SMIME) & hdr->security;
566 newhdr->security |= ENCRYPT | ccap;
567 if (!crypt_valid_passphrase (ccap))
570 mutt_message _("Decrypting message...");
571 if (((ccap & APPLICATION_PGP) && crypt_pgp_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
572 || ((ccap & APPLICATION_SMIME) && crypt_smime_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
576 mx_close_message (&msg);
577 mutt_free_envelope (&newhdr->env);
578 mutt_free_body (&newhdr->content);
579 mutt_error _("Decryption failed.");
583 mutt_free_body (&newhdr->content);
590 * remove a potential multipart/signed layer - useful when
594 if (WithCrypto && mutt_is_multipart_signed (newhdr->content))
596 newhdr->security |= SIGN;
597 if ((WithCrypto & APPLICATION_PGP)
598 && ascii_strcasecmp (mutt_get_parameter ("protocol", newhdr->content->parameter), "application/pgp-signature") == 0)
599 newhdr->security |= APPLICATION_PGP;
600 else if ((WithCrypto & APPLICATION_SMIME))
601 newhdr->security |= APPLICATION_SMIME;
603 /* destroy the signature */
604 mutt_free_body (&newhdr->content->parts->next);
605 newhdr->content = mutt_remove_multipart (newhdr->content);
610 * We don't need no primary multipart.
611 * Note: We _do_ preserve messages!
613 * XXX - we don't handle multipart/alternative in any
614 * smart way when sending messages. However, one may
615 * consider this a feature.
619 if (newhdr->content->type == TYPEMULTIPART)
620 newhdr->content = mutt_remove_multipart (newhdr->content);
624 /* create temporary files for all attachments */
625 for (b = newhdr->content; b; b = b->next)
628 /* what follows is roughly a receive-mode variant of
629 * mutt_get_tmp_attachment () from muttlib.c
635 strfcpy (file, b->filename, sizeof (file));
636 b->d_filename = safe_strdup (b->filename);
640 /* avoid Content-Disposition: header with temporary filename */
644 /* set up state flags */
648 if (b->type == TYPETEXT)
650 if (!ascii_strcasecmp ("yes", mutt_get_parameter ("x-mutt-noconv", b->parameter)))
654 s.flags |= M_CHARCONV;
658 mutt_delete_parameter ("x-mutt-noconv", &b->parameter);
661 mutt_adv_mktemp (file, sizeof(file));
662 if ((s.fpout = safe_fopen (file, "w")) == NULL)
666 if ((WithCrypto & APPLICATION_PGP)
667 && (mutt_is_application_pgp (b) & (ENCRYPT|SIGN)))
670 mutt_body_handler (b, &s);
672 newhdr->security |= mutt_is_application_pgp (newhdr->content);
675 mutt_str_replace (&b->subtype, "plain");
676 mutt_delete_parameter ("x-action", &b->parameter);
679 mutt_decode_attachment (b, &s);
681 if (safe_fclose (&s.fpout) != 0)
684 mutt_str_replace (&b->filename, file);
687 mutt_stamp_attachment (b);
689 mutt_free_body (&b->parts);
690 if (b->hdr) b->hdr->content = NULL; /* avoid dangling pointer */
693 /* Fix encryption flags. */
695 /* No inline if multipart. */
696 if (WithCrypto && (newhdr->security & INLINE) && newhdr->content->next)
697 newhdr->security &= ~INLINE;
699 /* Do we even support multiple mechanisms? */
700 newhdr->security &= WithCrypto | ~(APPLICATION_PGP|APPLICATION_SMIME);
702 /* Theoretically, both could be set. Take the one the user wants to set by default. */
703 if ((newhdr->security & APPLICATION_PGP) && (newhdr->security & APPLICATION_SMIME))
705 if (option (OPTSMIMEISDEFAULT))
706 newhdr->security &= ~APPLICATION_PGP;
708 newhdr->security &= ~APPLICATION_SMIME;
716 if (bfp != fp) fclose (bfp);
717 if (msg) mx_close_message (&msg);
721 mutt_free_envelope (&newhdr->env);
722 mutt_free_body (&newhdr->content);