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))
74 OldPostponed = safe_strdup (Postponed);
83 /* LastModify is useless for IMAP */
84 if (mx_is_imap (Postponed))
90 newpc = imap_status (Postponed, 0);
94 dprint (3, (debugfile, "mutt_num_postponed: %d postponed IMAP messages found.\n", PostCount));
97 dprint (3, (debugfile, "mutt_num_postponed: using old IMAP postponed count.\n"));
103 if (stat (Postponed, &st) == -1)
110 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)
125 if (LastModify < st.st_mtime)
127 LastModify = st.st_mtime;
129 if (access (Postponed, R_OK | F_OK) != 0)
130 return (PostCount = 0);
131 if (mx_open_mailbox (Postponed, M_NOSORT | M_QUIET, &ctx) == NULL)
134 PostCount = ctx.msgcount;
135 mx_fastclose_mailbox (&ctx);
141 void mutt_update_num_postponed (void)
143 UpdateNumPostponed = 1;
146 static void post_entry (char *s, size_t slen, MUTTMENU *menu, int entry)
148 CONTEXT *ctx = (CONTEXT *) menu->data;
150 _mutt_make_string (s, slen, NONULL (HdrFmt), ctx, ctx->hdrs[entry],
151 M_FORMAT_ARROWCURSOR);
154 static HEADER *select_msg (void)
158 char helpstr[LONG_STRING];
161 menu = mutt_new_menu (MENU_POST);
162 menu->make_entry = post_entry;
163 menu->max = PostContext->msgcount;
164 menu->title = _("Postponed Messages");
165 menu->data = PostContext;
166 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_POST, PostponeHelp);
168 /* The postponed mailbox is setup to have sorting disabled, but the global
169 * Sort variable may indicate something different. Sorting has to be
170 * disabled while the postpone menu is being displayed. */
176 switch (i = mutt_menuLoop (menu))
180 mutt_set_flag (PostContext, PostContext->hdrs[menu->current], M_DELETE, (i == OP_DELETE) ? 1 : 0);
181 PostCount = PostContext->msgcount - PostContext->deleted;
182 if (option (OPTRESOLVE) && menu->current < menu->max - 1)
184 menu->oldcurrent = menu->current;
186 if (menu->current >= menu->top + menu->pagelen)
188 menu->top = menu->current;
189 menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
192 menu->redraw |= REDRAW_MOTION_RESYNCH;
195 menu->redraw = REDRAW_CURRENT;
198 case OP_GENERIC_SELECT_ENTRY:
210 mutt_menuDestroy (&menu);
211 return (r > -1 ? PostContext->hdrs[r] : NULL);
215 * ctx Context info, used when recalling a message to which
217 * hdr envelope/attachment info for recalled message
218 * cur if message was a reply, `cur' is set to the message which
219 * `hdr' is in reply to
220 * fcc fcc for the recalled message
221 * fcclen max length of fcc
224 * -1 error/no messages
226 * SENDREPLY recalled message is a reply
228 int mutt_get_postponed (CONTEXT *ctx, HEADER *hdr, HEADER **cur, char *fcc, size_t fcclen)
231 int code = SENDPOSTPONED;
241 if ((PostContext = mx_open_mailbox (Postponed, M_NOSORT, NULL)) == NULL)
244 mutt_error _("No postponed messages.");
248 if (! PostContext->msgcount)
251 mx_close_mailbox (PostContext, NULL);
253 mutt_error _("No postponed messages.");
257 if (PostContext->msgcount == 1)
259 /* only one message, so just use that one. */
260 h = PostContext->hdrs[0];
262 else if ((h = select_msg ()) == NULL)
264 mx_close_mailbox (PostContext, NULL);
269 if (mutt_prepare_template (NULL, PostContext, hdr, h, 0) < 0)
271 mx_fastclose_mailbox (PostContext);
276 /* finished with this message, so delete it. */
277 mutt_set_flag (PostContext, h, M_DELETE, 1);
279 /* update the count for the status display */
280 PostCount = PostContext->msgcount - PostContext->deleted;
282 /* avoid the "purge deleted messages" prompt */
283 opt_delete = quadoption (OPT_DELETE);
284 set_quadoption (OPT_DELETE, M_YES);
285 mx_close_mailbox (PostContext, NULL);
286 set_quadoption (OPT_DELETE, opt_delete);
290 for (tmp = hdr->env->userhdrs; tmp; )
292 if (ascii_strncasecmp ("X-Mutt-References:", tmp->data, 18) == 0)
296 /* if a mailbox is currently open, look to see if the orignal message
297 the user attempted to reply to is in this mailbox */
301 ctx->id_hash = mutt_make_id_hash (ctx);
302 *cur = hash_find (ctx->id_hash, p);
305 /* Remove the X-Mutt-References: header field. */
308 last->next = tmp->next;
310 hdr->env->userhdrs = tmp->next;
312 mutt_free_list (&tmp);
317 else if (ascii_strncasecmp ("X-Mutt-Fcc:", tmp->data, 11) == 0)
321 strfcpy (fcc, p, fcclen);
322 mutt_pretty_mailbox (fcc, fcclen);
324 /* remove the X-Mutt-Fcc: header field */
327 last->next = tmp->next;
329 hdr->env->userhdrs = tmp->next;
331 mutt_free_list (&tmp);
334 else if ((WithCrypto & APPLICATION_PGP)
335 && (mutt_strncmp ("Pgp:", tmp->data, 4) == 0 /* this is generated
336 * by old mutt versions
338 || mutt_strncmp ("X-Mutt-PGP:", tmp->data, 11) == 0))
340 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1,
342 hdr->security |= APPLICATION_PGP;
344 /* remove the pgp field */
347 last->next = tmp->next;
349 hdr->env->userhdrs = tmp->next;
351 mutt_free_list (&tmp);
354 else if ((WithCrypto & APPLICATION_SMIME)
355 && mutt_strncmp ("X-Mutt-SMIME:", tmp->data, 13) == 0)
357 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1,
359 hdr->security |= APPLICATION_SMIME;
361 /* remove the smime field */
364 last->next = tmp->next;
366 hdr->env->userhdrs = tmp->next;
368 mutt_free_list (&tmp);
373 else if (mutt_strncmp ("X-Mutt-Mix:", tmp->data, 11) == 0)
376 mutt_free_list (&hdr->chain);
378 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);
407 int mutt_parse_crypt_hdr (char *p, int set_signas, int crypt_app)
409 char smime_cryptalg[LONG_STRING] = "\0";
410 char sign_as[LONG_STRING] = "\0", *q;
435 *p && *p != '>' && q < sign_as + sizeof (sign_as) - 1;
441 mutt_error _("Illegal crypto header");
449 /* This used to be the micalg parameter.
451 * It's no longer needed, so we just skip the parameter in order
452 * to be able to recall old messages.
458 for (p += 2; *p && *p != '>'; p++)
462 mutt_error _("Illegal crypto header");
476 for(p += 2; *p && *p != '>' && q < smime_cryptalg + sizeof(smime_cryptalg) - 1;
482 mutt_error _("Illegal S/MIME header");
496 mutt_error _("Illegal crypto header");
502 /* the cryptalg field must not be empty */
503 if ((WithCrypto & APPLICATION_SMIME) && *smime_cryptalg)
504 mutt_str_replace (&SmimeCryptAlg, smime_cryptalg);
506 /* Set {Smime,Pgp}SignAs, if desired. */
508 if ((WithCrypto & APPLICATION_PGP) && (crypt_app == APPLICATION_PGP)
509 && (set_signas || *sign_as))
510 mutt_str_replace (&PgpSignAs, sign_as);
512 if ((WithCrypto & APPLICATION_SMIME) && (crypt_app == APPLICATION_SMIME)
513 && (set_signas || *sign_as))
514 mutt_str_replace (&SmimeDefaultKey, sign_as);
521 int mutt_prepare_template (FILE *fp, CONTEXT *ctx, HEADER *newhdr, HEADER *hdr,
525 char file[_POSIX_PATH_MAX];
532 memset (&s, 0, sizeof (s));
534 if (!fp && (msg = mx_open_message (ctx, hdr->msgno)) == NULL)
537 if (!fp) fp = msg->fp;
541 /* parse the message header and MIME structure */
543 fseeko (fp, hdr->offset, 0);
544 newhdr->offset = hdr->offset;
545 newhdr->env = mutt_read_rfc822_header (fp, newhdr, 1, weed);
546 newhdr->content->length = hdr->content->length;
547 mutt_parse_part (fp, newhdr->content);
549 FREE (&newhdr->env->message_id);
550 FREE (&newhdr->env->mail_followup_to); /* really? */
552 /* decrypt pgp/mime encoded messages */
554 if ((WithCrypto & (APPLICATION_PGP|APPLICATION_SMIME) & hdr->security)
555 && mutt_is_multipart_encrypted (newhdr->content))
557 int ccap = WithCrypto & (APPLICATION_PGP|APPLICATION_SMIME) & hdr->security;
558 newhdr->security |= ENCRYPT | ccap;
559 if (!crypt_valid_passphrase (ccap))
562 mutt_message _("Decrypting message...");
563 if (((ccap & APPLICATION_PGP) && crypt_pgp_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
564 || ((ccap & APPLICATION_SMIME) && crypt_smime_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
568 mx_close_message (&msg);
569 mutt_free_envelope (&newhdr->env);
570 mutt_free_body (&newhdr->content);
571 mutt_error _("Decryption failed.");
575 mutt_free_body (&newhdr->content);
582 * remove a potential multipart/signed layer - useful when
586 if (WithCrypto && mutt_is_multipart_signed (newhdr->content))
588 newhdr->security |= SIGN;
589 if ((WithCrypto & APPLICATION_PGP)
590 && ascii_strcasecmp (mutt_get_parameter ("protocol", newhdr->content->parameter), "application/pgp-signature") == 0)
591 newhdr->security |= APPLICATION_PGP;
592 else if ((WithCrypto & APPLICATION_SMIME))
593 newhdr->security |= APPLICATION_SMIME;
595 /* destroy the signature */
596 mutt_free_body (&newhdr->content->parts->next);
597 newhdr->content = mutt_remove_multipart (newhdr->content);
602 * We don't need no primary multipart.
603 * Note: We _do_ preserve messages!
605 * XXX - we don't handle multipart/alternative in any
606 * smart way when sending messages. However, one may
607 * consider this a feature.
611 if (newhdr->content->type == TYPEMULTIPART)
612 newhdr->content = mutt_remove_multipart (newhdr->content);
616 /* create temporary files for all attachments */
617 for (b = newhdr->content; b; b = b->next)
620 /* what follows is roughly a receive-mode variant of
621 * mutt_get_tmp_attachment () from muttlib.c
627 strfcpy (file, b->filename, sizeof (file));
628 b->d_filename = safe_strdup (b->filename);
632 /* avoid Content-Disposition: header with temporary filename */
636 /* set up state flags */
640 if (b->type == TYPETEXT)
642 if (!ascii_strcasecmp ("yes", mutt_get_parameter ("x-mutt-noconv", b->parameter)))
646 s.flags |= M_CHARCONV;
650 mutt_delete_parameter ("x-mutt-noconv", &b->parameter);
653 mutt_adv_mktemp (file, sizeof(file));
654 if ((s.fpout = safe_fopen (file, "w")) == NULL)
658 if ((WithCrypto & APPLICATION_PGP)
659 && (mutt_is_application_pgp (b) & (ENCRYPT|SIGN)))
662 mutt_body_handler (b, &s);
664 newhdr->security |= mutt_is_application_pgp (newhdr->content);
667 mutt_str_replace (&b->subtype, "plain");
668 mutt_delete_parameter ("x-action", &b->parameter);
671 mutt_decode_attachment (b, &s);
673 if (safe_fclose (&s.fpout) != 0)
676 mutt_str_replace (&b->filename, file);
679 mutt_stamp_attachment (b);
681 mutt_free_body (&b->parts);
682 if (b->hdr) b->hdr->content = NULL; /* avoid dangling pointer */
685 /* Fix encryption flags. */
687 /* No inline if multipart. */
688 if (WithCrypto && (newhdr->security & INLINE) && newhdr->content->next)
689 newhdr->security &= ~INLINE;
691 /* Do we even support multiple mechanisms? */
692 newhdr->security &= WithCrypto | ~(APPLICATION_PGP|APPLICATION_SMIME);
694 /* Theoretically, both could be set. Take the one the user wants to set by default. */
695 if ((newhdr->security & APPLICATION_PGP) && (newhdr->security & APPLICATION_SMIME))
697 if (option (OPTSMIMEISDEFAULT))
698 newhdr->security &= ~APPLICATION_PGP;
700 newhdr->security &= ~APPLICATION_SMIME;
708 if (bfp != fp) safe_fclose (&bfp);
709 if (msg) mx_close_message (&msg);
713 mutt_free_envelope (&newhdr->env);
714 mutt_free_body (&newhdr->content);