2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
4 * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
6 * This file is part of mutt-ng, see http://www.muttng.org/.
7 * It's licensed under the GNU General Public License,
8 * please see the file GPL in the top level source directory.
16 #include "mutt_menu.h"
25 #include "mutt_crypt.h"
36 static struct mapping_t PostponeHelp[] = {
37 {N_("Exit"), OP_EXIT},
38 {N_("Del"), OP_DELETE},
39 {N_("Undel"), OP_UNDELETE},
40 {N_("Help"), OP_HELP},
46 static short PostCount = 0;
47 static CONTEXT *PostContext = NULL;
48 static short UpdateNumPostponed = 0;
50 /* Return the number of postponed messages.
51 * if force is 0, use a cached value if it is costly to get a fresh
52 * count (IMAP) - else check.
54 int mutt_num_postponed (int force)
59 static time_t LastModify = 0;
60 static char *OldPostponed = NULL;
62 if (UpdateNumPostponed) {
63 UpdateNumPostponed = 0;
67 if (Postponed != OldPostponed) {
68 OldPostponed = Postponed;
77 /* LastModify is useless for IMAP */
78 if (mx_get_magic (Postponed) == M_IMAP) {
82 newpc = imap_mailbox_check (Postponed, 0);
87 "mutt_num_postponed: %d postponed IMAP messages found.\n",
93 "mutt_num_postponed: using old IMAP postponed count.\n"));
99 if (stat (Postponed, &st) == -1) {
105 if (S_ISDIR (st.st_mode)) {
106 /* if we have a maildir mailbox, we need to stat the "new" dir */
108 char buf[_POSIX_PATH_MAX];
110 snprintf (buf, sizeof (buf), "%s/new", Postponed);
111 if (access (buf, F_OK) == 0 && stat (buf, &st) == -1) {
118 if (LastModify < st.st_mtime) {
120 int optnews = option (OPTNEWS);
122 LastModify = st.st_mtime;
124 if (access (Postponed, R_OK | F_OK) != 0)
125 return (PostCount = 0);
128 unset_option (OPTNEWS);
130 if (mx_open_mailbox (Postponed, M_NOSORT | M_QUIET, &ctx) == NULL)
133 PostCount = ctx.msgcount;
134 mx_fastclose_mailbox (&ctx);
137 set_option (OPTNEWS);
144 void mutt_update_num_postponed (void)
146 UpdateNumPostponed = 1;
149 static void post_entry (char *s, size_t slen, MUTTMENU * menu, int entry)
151 CONTEXT *ctx = (CONTEXT *) menu->data;
153 _mutt_make_string (s, slen, NONULL (HdrFmt), ctx, ctx->hdrs[entry],
154 M_FORMAT_ARROWCURSOR);
157 static HEADER *select_msg (void)
160 int i, done = 0, r = -1;
161 char helpstr[SHORT_STRING];
164 menu = mutt_new_menu ();
165 menu->make_entry = post_entry;
166 menu->menu = MENU_POST;
167 menu->max = PostContext->msgcount;
168 menu->title = _("Postponed Messages");
169 menu->data = PostContext;
171 mutt_compile_help (helpstr, sizeof (helpstr), MENU_POST, PostponeHelp);
173 /* The postponed mailbox is setup to have sorting disabled, but the global
174 * Sort variable may indicate something different. Sorting has to be
175 * disabled while the postpone menu is being displayed. */
180 switch (i = mutt_menuLoop (menu)) {
183 mutt_set_flag (PostContext, PostContext->hdrs[menu->current], M_DELETE,
184 (i == OP_DELETE) ? 1 : 0);
185 PostCount = PostContext->msgcount - PostContext->deleted;
186 if (option (OPTRESOLVE) && menu->current < menu->max - 1) {
187 menu->oldcurrent = menu->current;
189 if (menu->current >= menu->top + menu->pagelen) {
190 menu->top = menu->current;
191 menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
194 menu->redraw |= REDRAW_MOTION_RESYNCH;
197 menu->redraw = REDRAW_CURRENT;
200 case OP_GENERIC_SELECT_ENTRY:
212 mutt_menuDestroy (&menu);
213 return (r > -1 ? PostContext->hdrs[r] : NULL);
217 * ctx Context info, used when recalling a message to which
219 * hdr envelope/attachment info for recalled message
220 * cur if message was a reply, `cur' is set to the message which
221 * `hdr' is in reply to
222 * fcc fcc for the recalled message
223 * fcclen max length of fcc
226 * -1 error/no messages
228 * SENDREPLY recalled message is a reply
230 int mutt_get_postponed (CONTEXT * ctx, HEADER * hdr, HEADER ** cur, char *fcc,
234 int code = SENDPOSTPONED;
244 if ((PostContext = mx_open_mailbox (Postponed, M_NOSORT, NULL)) == NULL) {
246 mutt_error _("No postponed messages.");
251 if (!PostContext->msgcount) {
253 mx_close_mailbox (PostContext, NULL);
255 mutt_error _("No postponed messages.");
260 if (PostContext->msgcount == 1) {
261 /* only one message, so just use that one. */
262 h = PostContext->hdrs[0];
264 else if ((h = select_msg ()) == NULL) {
265 mx_close_mailbox (PostContext, NULL);
270 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 /* and consider it saved, so that it won't be moved to the trash folder */
280 mutt_set_flag (PostContext, h, M_APPENDED, 1);
282 /* update the count for the status display */
283 PostCount = PostContext->msgcount - PostContext->deleted;
285 /* avoid the "purge deleted messages" prompt */
286 opt_delete = quadoption (OPT_DELETE);
287 set_quadoption (OPT_DELETE, M_YES);
288 mx_close_mailbox (PostContext, NULL);
289 set_quadoption (OPT_DELETE, opt_delete);
293 for (tmp = hdr->env->userhdrs; tmp;) {
294 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) {
320 strfcpy (fcc, p, fcclen);
321 mutt_pretty_mailbox (fcc);
323 /* remove the X-Mutt-Fcc: header field */
326 last->next = tmp->next;
328 hdr->env->userhdrs = tmp->next;
330 mutt_free_list (&tmp);
333 else if ((WithCrypto & APPLICATION_PGP)
334 && (safe_strncmp ("Pgp:", tmp->data, 4) == 0 /* this is generated
335 * by old mutt versions
337 || safe_strncmp ("X-Mutt-PGP:", tmp->data, 11) == 0)) {
338 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
339 hdr->security |= APPLICATION_PGP;
341 /* remove the pgp field */
344 last->next = tmp->next;
346 hdr->env->userhdrs = tmp->next;
348 mutt_free_list (&tmp);
351 else if ((WithCrypto & APPLICATION_SMIME)
352 && safe_strncmp ("X-Mutt-SMIME:", tmp->data, 13) == 0) {
353 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
354 hdr->security |= APPLICATION_SMIME;
356 /* remove the smime field */
359 last->next = tmp->next;
361 hdr->env->userhdrs = tmp->next;
363 mutt_free_list (&tmp);
368 else if (safe_strncmp ("X-Mutt-Mix:", tmp->data, 11) == 0) {
371 mutt_free_list (&hdr->chain);
373 t = strtok (tmp->data + 11, " \t\n");
375 hdr->chain = mutt_add_list (hdr->chain, t);
376 t = strtok (NULL, " \t\n");
381 last->next = tmp->next;
383 hdr->env->userhdrs = tmp->next;
385 mutt_free_list (&tmp);
400 int mutt_parse_crypt_hdr (char *p, int set_signas)
403 char pgp_sign_as[LONG_STRING] = "\0", *q;
404 char smime_cryptalg[LONG_STRING] = "\0";
423 if (*(p + 1) == '<') {
425 *p && *p != '>' && q < pgp_sign_as + sizeof (pgp_sign_as) - 1;
429 mutt_error _("Illegal PGP header");
438 /* This used to be the micalg parameter.
440 * It's no longer needed, so we just skip the parameter in order
441 * to be able to recall old messages.
445 if (*(p + 1) == '<') {
446 for (p += 2; *p && *p != '>'; p++);
448 mutt_error _("Illegal PGP header");
461 if (*(p + 1) == '<') {
464 && q < smime_cryptalg + sizeof (smime_cryptalg) - 1;
468 mutt_error _("Illegal S/MIME header");
483 mutt_error _("Illegal PGP header");
489 /* the cryptalg field must not be empty */
490 if ((WithCrypto & APPLICATION_SMIME) && *smime_cryptalg)
491 str_replace (&SmimeCryptAlg, smime_cryptalg);
493 if ((WithCrypto & APPLICATION_PGP) && (set_signas || *pgp_sign_as))
494 str_replace (&PgpSignAs, pgp_sign_as);
501 int mutt_prepare_template (FILE * fp, CONTEXT * ctx, HEADER * newhdr,
502 HEADER * hdr, short weed)
505 char file[_POSIX_PATH_MAX];
512 memset (&s, 0, sizeof (s));
514 if (!fp && (msg = mx_open_message (ctx, hdr->msgno)) == NULL)
522 /* parse the message header and MIME structure */
524 fseek (fp, hdr->offset, 0);
525 newhdr->offset = hdr->offset;
526 newhdr->env = mutt_read_rfc822_header (fp, newhdr, 1, weed);
527 newhdr->content->length = hdr->content->length;
528 mutt_parse_part (fp, newhdr->content);
530 FREE (&newhdr->env->message_id);
531 FREE (&newhdr->env->mail_followup_to); /* really? */
533 /* decrypt pgp/mime encoded messages */
535 if ((WithCrypto & (APPLICATION_PGP | APPLICATION_SMIME) & hdr->security)
536 && mutt_is_multipart_encrypted (newhdr->content)) {
538 WithCrypto & (APPLICATION_PGP | APPLICATION_SMIME) & hdr->security;
539 newhdr->security |= ENCRYPT | ccap;
540 if (!crypt_valid_passphrase (ccap))
543 mutt_message _("Decrypting message...");
545 if (((ccap & APPLICATION_PGP)
546 && crypt_pgp_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
547 || ((ccap & APPLICATION_SMIME)
548 && crypt_smime_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
551 mx_close_message (&msg);
552 mutt_free_envelope (&newhdr->env);
553 mutt_free_body (&newhdr->content);
554 mutt_error _("Decryption failed.");
559 mutt_free_body (&newhdr->content);
566 * remove a potential multipart/signed layer - useful when
570 if (WithCrypto && mutt_is_multipart_signed (newhdr->content)) {
571 newhdr->security |= SIGN;
572 if ((WithCrypto & APPLICATION_PGP)
574 ascii_strcasecmp (mutt_get_parameter
575 ("protocol", newhdr->content->parameter),
576 "application/pgp-signature") == 0)
577 newhdr->security |= APPLICATION_PGP;
578 else if ((WithCrypto & APPLICATION_SMIME))
579 newhdr->security |= APPLICATION_SMIME;
581 /* destroy the signature */
582 mutt_free_body (&newhdr->content->parts->next);
583 newhdr->content = mutt_remove_multipart (newhdr->content);
588 * We don't need no primary multipart.
589 * Note: We _do_ preserve messages!
591 * XXX - we don't handle multipart/alternative in any
592 * smart way when sending messages. However, one may
593 * consider this a feature.
597 if (newhdr->content->type == TYPEMULTIPART)
598 newhdr->content = mutt_remove_multipart (newhdr->content);
602 /* create temporary files for all attachments */
603 for (b = newhdr->content; b; b = b->next) {
605 /* what follows is roughly a receive-mode variant of
606 * mutt_get_tmp_attachment () from muttlib.c
611 strfcpy (file, b->filename, sizeof (file));
612 b->d_filename = safe_strdup (b->filename);
615 /* avoid Content-Disposition: header with temporary filename */
619 /* set up state flags */
623 if (b->type == TYPETEXT) {
624 if (!ascii_strcasecmp
625 ("yes", mutt_get_parameter ("x-mutt-noconv", b->parameter)))
628 s.flags |= M_CHARCONV;
632 mutt_delete_parameter ("x-mutt-noconv", &b->parameter);
635 mutt_adv_mktemp (file, sizeof (file));
636 if ((s.fpout = safe_fopen (file, "w")) == NULL)
640 if ((WithCrypto & APPLICATION_PGP)
641 && (mutt_is_application_pgp (b) & (ENCRYPT | SIGN))) {
643 mutt_body_handler (b, &s);
645 newhdr->security |= mutt_is_application_pgp (newhdr->content);
648 str_replace (&b->subtype, "plain");
649 mutt_delete_parameter ("x-action", &b->parameter);
652 mutt_decode_attachment (b, &s);
654 if (safe_fclose (&s.fpout) != 0)
657 str_replace (&b->filename, file);
660 mutt_stamp_attachment (b);
662 mutt_free_body (&b->parts);
664 b->hdr->content = NULL; /* avoid dangling pointer */
667 /* Fix encryption flags. */
669 /* No inline if multipart. */
670 if (WithCrypto && (newhdr->security & INLINE) && newhdr->content->next)
671 newhdr->security &= ~INLINE;
673 /* Do we even support multiple mechanisms? */
674 newhdr->security &= WithCrypto | ~(APPLICATION_PGP | APPLICATION_SMIME);
676 /* Theoretically, both could be set. Take the one the user wants to set by default. */
677 if ((newhdr->security & APPLICATION_PGP)
678 && (newhdr->security & APPLICATION_SMIME)) {
679 if (option (OPTSMIMEISDEFAULT))
680 newhdr->security &= ~APPLICATION_PGP;
682 newhdr->security &= ~APPLICATION_SMIME;
693 mx_close_message (&msg);
696 mutt_free_envelope (&newhdr->env);
697 mutt_free_body (&newhdr->content);