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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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_status (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)
129 LastModify = st.st_mtime;
131 if (access (Postponed, R_OK | F_OK) != 0)
132 return (PostCount = 0);
133 if (mx_open_mailbox (Postponed, M_NOSORT | M_QUIET, &ctx) == NULL)
136 PostCount = ctx.msgcount;
137 mx_fastclose_mailbox (&ctx);
143 void mutt_update_num_postponed (void)
145 UpdateNumPostponed = 1;
148 static void post_entry (char *s, size_t slen, MUTTMENU *menu, int entry)
150 CONTEXT *ctx = (CONTEXT *) menu->data;
152 _mutt_make_string (s, slen, NONULL (HdrFmt), ctx, ctx->hdrs[entry],
153 M_FORMAT_ARROWCURSOR);
156 static HEADER *select_msg (void)
160 char helpstr[LONG_STRING];
163 menu = mutt_new_menu ();
164 menu->make_entry = post_entry;
165 menu->menu = MENU_POST;
166 menu->max = PostContext->msgcount;
167 menu->title = _("Postponed Messages");
168 menu->data = PostContext;
169 menu->help = mutt_compile_help (helpstr, sizeof (helpstr), MENU_POST, PostponeHelp);
171 /* The postponed mailbox is setup to have sorting disabled, but the global
172 * Sort variable may indicate something different. Sorting has to be
173 * disabled while the postpone menu is being displayed. */
179 switch (i = mutt_menuLoop (menu))
183 mutt_set_flag (PostContext, PostContext->hdrs[menu->current], M_DELETE, (i == OP_DELETE) ? 1 : 0);
184 PostCount = PostContext->msgcount - PostContext->deleted;
185 if (option (OPTRESOLVE) && menu->current < menu->max - 1)
187 menu->oldcurrent = menu->current;
189 if (menu->current >= menu->top + menu->pagelen)
191 menu->top = menu->current;
192 menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
195 menu->redraw |= REDRAW_MOTION_RESYNCH;
198 menu->redraw = REDRAW_CURRENT;
201 case OP_GENERIC_SELECT_ENTRY:
213 mutt_menuDestroy (&menu);
214 return (r > -1 ? PostContext->hdrs[r] : NULL);
218 * ctx Context info, used when recalling a message to which
220 * hdr envelope/attachment info for recalled message
221 * cur if message was a reply, `cur' is set to the message which
222 * `hdr' is in reply to
223 * fcc fcc for the recalled message
224 * fcclen max length of fcc
227 * -1 error/no messages
229 * SENDREPLY recalled message is a reply
231 int mutt_get_postponed (CONTEXT *ctx, HEADER *hdr, HEADER **cur, char *fcc, size_t fcclen)
234 int code = SENDPOSTPONED;
244 if ((PostContext = mx_open_mailbox (Postponed, M_NOSORT, NULL)) == NULL)
247 mutt_error _("No postponed messages.");
251 if (! PostContext->msgcount)
254 mx_close_mailbox (PostContext, NULL);
256 mutt_error _("No postponed messages.");
260 if (PostContext->msgcount == 1)
262 /* only one message, so just use that one. */
263 h = PostContext->hdrs[0];
265 else if ((h = select_msg ()) == NULL)
267 mx_close_mailbox (PostContext, NULL);
272 if (mutt_prepare_template (NULL, PostContext, hdr, h, 0) < 0)
274 mx_fastclose_mailbox (PostContext);
279 /* finished with this message, so delete it. */
280 mutt_set_flag (PostContext, h, M_DELETE, 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; )
295 if (ascii_strncasecmp ("X-Mutt-References:", tmp->data, 18) == 0)
299 /* if a mailbox is currently open, look to see if the orignal message
300 the user attempted to reply to is in this mailbox */
304 ctx->id_hash = mutt_make_id_hash (ctx);
305 *cur = hash_find (ctx->id_hash, p);
308 /* Remove the X-Mutt-References: header field. */
311 last->next = tmp->next;
313 hdr->env->userhdrs = tmp->next;
315 mutt_free_list (&tmp);
320 else if (ascii_strncasecmp ("X-Mutt-Fcc:", tmp->data, 11) == 0)
324 strfcpy (fcc, p, fcclen);
325 mutt_pretty_mailbox (fcc);
327 /* remove the X-Mutt-Fcc: header field */
330 last->next = tmp->next;
332 hdr->env->userhdrs = tmp->next;
334 mutt_free_list (&tmp);
337 else if ((WithCrypto & APPLICATION_PGP)
338 && (mutt_strncmp ("Pgp:", tmp->data, 4) == 0 /* this is generated
339 * by old mutt versions
341 || mutt_strncmp ("X-Mutt-PGP:", tmp->data, 11) == 0))
343 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1,
345 hdr->security |= APPLICATION_PGP;
347 /* remove the pgp field */
350 last->next = tmp->next;
352 hdr->env->userhdrs = tmp->next;
354 mutt_free_list (&tmp);
357 else if ((WithCrypto & APPLICATION_SMIME)
358 && mutt_strncmp ("X-Mutt-SMIME:", tmp->data, 13) == 0)
360 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1,
362 hdr->security |= APPLICATION_SMIME;
364 /* remove the smime field */
367 last->next = tmp->next;
369 hdr->env->userhdrs = tmp->next;
371 mutt_free_list (&tmp);
376 else if (mutt_strncmp ("X-Mutt-Mix:", tmp->data, 11) == 0)
379 mutt_free_list (&hdr->chain);
381 t = strtok (tmp->data + 11, " \t\n");
384 hdr->chain = mutt_add_list (hdr->chain, t);
385 t = strtok (NULL, " \t\n");
390 last->next = tmp->next;
392 hdr->env->userhdrs = tmp->next;
394 mutt_free_list (&tmp);
410 int mutt_parse_crypt_hdr (char *p, int set_signas, int crypt_app)
412 char smime_cryptalg[LONG_STRING] = "\0";
413 char sign_as[LONG_STRING] = "\0", *q;
438 *p && *p != '>' && q < sign_as + sizeof (sign_as) - 1;
444 mutt_error _("Illegal crypto header");
452 /* This used to be the micalg parameter.
454 * It's no longer needed, so we just skip the parameter in order
455 * to be able to recall old messages.
461 for (p += 2; *p && *p != '>'; p++)
465 mutt_error _("Illegal crypto header");
479 for(p += 2; *p && *p != '>' && q < smime_cryptalg + sizeof(smime_cryptalg) - 1;
485 mutt_error _("Illegal S/MIME header");
499 mutt_error _("Illegal crypto header");
505 /* the cryptalg field must not be empty */
506 if ((WithCrypto & APPLICATION_SMIME) && *smime_cryptalg)
507 mutt_str_replace (&SmimeCryptAlg, smime_cryptalg);
509 /* Set {Smime,Pgp}SignAs, if desired. */
511 if ((WithCrypto & APPLICATION_PGP) && (crypt_app == APPLICATION_PGP)
512 && (set_signas || *sign_as))
513 mutt_str_replace (&PgpSignAs, sign_as);
515 if ((WithCrypto & APPLICATION_SMIME) && (crypt_app == APPLICATION_SMIME)
516 && (set_signas || *sign_as))
517 mutt_str_replace (&SmimeDefaultKey, sign_as);
524 int mutt_prepare_template (FILE *fp, CONTEXT *ctx, HEADER *newhdr, HEADER *hdr,
528 char file[_POSIX_PATH_MAX];
535 memset (&s, 0, sizeof (s));
537 if (!fp && (msg = mx_open_message (ctx, hdr->msgno)) == NULL)
540 if (!fp) fp = msg->fp;
544 /* parse the message header and MIME structure */
546 fseeko (fp, hdr->offset, 0);
547 newhdr->offset = hdr->offset;
548 newhdr->env = mutt_read_rfc822_header (fp, newhdr, 1, weed);
549 newhdr->content->length = hdr->content->length;
550 mutt_parse_part (fp, newhdr->content);
552 FREE (&newhdr->env->message_id);
553 FREE (&newhdr->env->mail_followup_to); /* really? */
555 /* decrypt pgp/mime encoded messages */
557 if ((WithCrypto & (APPLICATION_PGP|APPLICATION_SMIME) & hdr->security)
558 && mutt_is_multipart_encrypted (newhdr->content))
560 int ccap = WithCrypto & (APPLICATION_PGP|APPLICATION_SMIME) & hdr->security;
561 newhdr->security |= ENCRYPT | ccap;
562 if (!crypt_valid_passphrase (ccap))
565 mutt_message _("Decrypting message...");
566 if (((ccap & APPLICATION_PGP) && crypt_pgp_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
567 || ((ccap & APPLICATION_SMIME) && crypt_smime_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
571 mx_close_message (&msg);
572 mutt_free_envelope (&newhdr->env);
573 mutt_free_body (&newhdr->content);
574 mutt_error _("Decryption failed.");
578 mutt_free_body (&newhdr->content);
585 * remove a potential multipart/signed layer - useful when
589 if (WithCrypto && mutt_is_multipart_signed (newhdr->content))
591 newhdr->security |= SIGN;
592 if ((WithCrypto & APPLICATION_PGP)
593 && ascii_strcasecmp (mutt_get_parameter ("protocol", newhdr->content->parameter), "application/pgp-signature") == 0)
594 newhdr->security |= APPLICATION_PGP;
595 else if ((WithCrypto & APPLICATION_SMIME))
596 newhdr->security |= APPLICATION_SMIME;
598 /* destroy the signature */
599 mutt_free_body (&newhdr->content->parts->next);
600 newhdr->content = mutt_remove_multipart (newhdr->content);
605 * We don't need no primary multipart.
606 * Note: We _do_ preserve messages!
608 * XXX - we don't handle multipart/alternative in any
609 * smart way when sending messages. However, one may
610 * consider this a feature.
614 if (newhdr->content->type == TYPEMULTIPART)
615 newhdr->content = mutt_remove_multipart (newhdr->content);
619 /* create temporary files for all attachments */
620 for (b = newhdr->content; b; b = b->next)
623 /* what follows is roughly a receive-mode variant of
624 * mutt_get_tmp_attachment () from muttlib.c
630 strfcpy (file, b->filename, sizeof (file));
631 b->d_filename = safe_strdup (b->filename);
635 /* avoid Content-Disposition: header with temporary filename */
639 /* set up state flags */
643 if (b->type == TYPETEXT)
645 if (!ascii_strcasecmp ("yes", mutt_get_parameter ("x-mutt-noconv", b->parameter)))
649 s.flags |= M_CHARCONV;
653 mutt_delete_parameter ("x-mutt-noconv", &b->parameter);
656 mutt_adv_mktemp (file, sizeof(file));
657 if ((s.fpout = safe_fopen (file, "w")) == NULL)
661 if ((WithCrypto & APPLICATION_PGP)
662 && (mutt_is_application_pgp (b) & (ENCRYPT|SIGN)))
665 mutt_body_handler (b, &s);
667 newhdr->security |= mutt_is_application_pgp (newhdr->content);
670 mutt_str_replace (&b->subtype, "plain");
671 mutt_delete_parameter ("x-action", &b->parameter);
674 mutt_decode_attachment (b, &s);
676 if (safe_fclose (&s.fpout) != 0)
679 mutt_str_replace (&b->filename, file);
682 mutt_stamp_attachment (b);
684 mutt_free_body (&b->parts);
685 if (b->hdr) b->hdr->content = NULL; /* avoid dangling pointer */
688 /* Fix encryption flags. */
690 /* No inline if multipart. */
691 if (WithCrypto && (newhdr->security & INLINE) && newhdr->content->next)
692 newhdr->security &= ~INLINE;
694 /* Do we even support multiple mechanisms? */
695 newhdr->security &= WithCrypto | ~(APPLICATION_PGP|APPLICATION_SMIME);
697 /* Theoretically, both could be set. Take the one the user wants to set by default. */
698 if ((newhdr->security & APPLICATION_PGP) && (newhdr->security & APPLICATION_SMIME))
700 if (option (OPTSMIMEISDEFAULT))
701 newhdr->security &= ~APPLICATION_PGP;
703 newhdr->security &= ~APPLICATION_SMIME;
711 if (bfp != fp) fclose (bfp);
712 if (msg) mx_close_message (&msg);
716 mutt_free_envelope (&newhdr->env);
717 mutt_free_body (&newhdr->content);