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.
11 #include <lib-lib/lib-lib.h>
13 #include <lib-mime/mime.h>
15 #include <lib-ui/enter.h>
16 #include <lib-ui/menu.h>
17 #include <lib-ui/curses.h>
18 #include <lib-mx/mx.h>
20 #include <lib-sys/unix.h>
26 #include <lib-crypt/crypt.h>
28 #include <imap/imap.h>
30 static struct mapping_t PostponeHelp[] = {
31 {N_("Exit"), OP_EXIT},
32 {N_("Del"), OP_DELETE},
33 {N_("Undel"), OP_UNDELETE},
34 {N_("Help"), OP_HELP},
40 static short PostCount = 0;
41 static CONTEXT *PostContext = NULL;
42 static short UpdateNumPostponed = 0;
44 /* Return the number of postponed messages.
45 * if force is 0, use a cached value if it is costly to get a fresh
46 * count (IMAP) - else check.
48 int mutt_num_postponed (int force)
53 static time_t LastModify = 0;
54 static char *OldPostponed = NULL;
56 if (UpdateNumPostponed) {
57 UpdateNumPostponed = 0;
61 if (Postponed != OldPostponed) {
62 OldPostponed = Postponed;
70 /* LastModify is useless for IMAP */
71 if (imap_is_magic (Postponed, NULL) == M_IMAP) {
75 newpc = imap_mailbox_check (Postponed, 0);
83 if (stat (Postponed, &st) == -1) {
89 if (S_ISDIR (st.st_mode)) {
90 /* if we have a maildir mailbox, we need to stat the "new" dir */
92 char buf[_POSIX_PATH_MAX];
94 snprintf (buf, sizeof (buf), "%s/new", Postponed);
95 if (access (buf, F_OK) == 0 && stat (buf, &st) == -1) {
102 if (LastModify < st.st_mtime) {
104 int optnews = option (OPTNEWS);
106 LastModify = st.st_mtime;
108 if (access (Postponed, R_OK | F_OK) != 0)
109 return (PostCount = 0);
112 unset_option (OPTNEWS);
114 if (mx_open_mailbox (Postponed, M_NOSORT | M_QUIET, &ctx) == NULL)
117 PostCount = ctx.msgcount;
118 mx_fastclose_mailbox (&ctx);
121 set_option (OPTNEWS);
128 void mutt_update_num_postponed (void)
130 UpdateNumPostponed = 1;
133 static void post_entry (char *s, ssize_t slen, MUTTMENU * menu, int entry)
135 CONTEXT *ctx = (CONTEXT *) menu->data;
137 _mutt_make_string (s, slen, NONULL (HdrFmt), ctx, ctx->hdrs[entry],
138 option(OPTARROWCURSOR) ? M_FORMAT_ARROWCURSOR : 0);
141 static HEADER *select_msg (void)
144 int i, done = 0, r = -1;
145 char helpstr[STRING];
148 menu = mutt_new_menu ();
149 menu->make_entry = post_entry;
150 menu->menu = MENU_POST;
151 menu->max = PostContext->msgcount;
152 menu->title = _("Postponed Messages");
153 menu->data = PostContext;
155 mutt_compile_help (helpstr, sizeof (helpstr), MENU_POST, PostponeHelp);
157 /* The postponed mailbox is setup to have sorting disabled, but the global
158 * Sort variable may indicate something different. Sorting has to be
159 * disabled while the postpone menu is being displayed. */
164 switch (i = mutt_menuLoop (menu)) {
167 mutt_set_flag (PostContext, PostContext->hdrs[menu->current], M_DELETE,
168 (i == OP_DELETE) ? 1 : 0);
169 PostCount = PostContext->msgcount - PostContext->deleted;
170 if (option (OPTRESOLVE) && menu->current < menu->max - 1) {
171 menu->oldcurrent = menu->current;
173 if (menu->current >= menu->top + menu->pagelen) {
174 menu->top = menu->current;
175 menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
178 menu->redraw |= REDRAW_MOTION_RESYNCH;
181 menu->redraw = REDRAW_CURRENT;
184 case OP_GENERIC_SELECT_ENTRY:
196 mutt_menuDestroy (&menu);
197 return (r > -1 ? PostContext->hdrs[r] : NULL);
201 * ctx Context info, used when recalling a message to which
203 * hdr envelope/attachment info for recalled message
204 * cur if message was a reply, `cur' is set to the message which
205 * `hdr' is in reply to
206 * fcc fcc for the recalled message
207 * fcclen max length of fcc
210 * -1 error/no messages
212 * SENDREPLY recalled message is a reply
214 int mutt_get_postponed (CONTEXT * ctx, HEADER * hdr, HEADER ** cur, char *fcc,
218 int code = SENDPOSTPONED;
220 string_list_t *last = NULL;
228 if ((PostContext = mx_open_mailbox (Postponed, M_NOSORT, NULL)) == NULL) {
230 mutt_error _("No postponed messages.");
235 if (!PostContext->msgcount) {
237 mx_close_mailbox (PostContext, NULL);
238 p_delete(&PostContext);
239 mutt_error _("No postponed messages.");
244 if (PostContext->msgcount == 1) {
245 /* only one message, so just use that one. */
246 h = PostContext->hdrs[0];
248 else if ((h = select_msg ()) == NULL) {
249 mx_close_mailbox (PostContext, NULL);
250 p_delete(&PostContext);
254 if (mutt_prepare_template (NULL, PostContext, hdr, h, 0) < 0) {
255 mx_fastclose_mailbox (PostContext);
256 p_delete(&PostContext);
260 /* finished with this message, so delete it. */
261 mutt_set_flag (PostContext, h, M_DELETE, 1);
263 /* and consider it saved, so that it won't be moved to the trash folder */
264 mutt_set_flag (PostContext, h, M_APPENDED, 1);
266 /* update the count for the status display */
267 PostCount = PostContext->msgcount - PostContext->deleted;
269 /* avoid the "purge deleted messages" prompt */
270 opt_delete = quadoption (OPT_DELETE);
271 set_quadoption (OPT_DELETE, M_YES);
272 mx_close_mailbox (PostContext, NULL);
273 set_quadoption (OPT_DELETE, opt_delete);
275 p_delete(&PostContext);
277 for (tmp = hdr->env->userhdrs; tmp;) {
278 if (ascii_strncasecmp ("X-Mutt-References:", tmp->data, 18) == 0) {
280 /* if a mailbox is currently open, look to see if the orignal message
281 the user attempted to reply to is in this mailbox */
282 p = vskipspaces(tmp->data + 18);
284 ctx->id_hash = mutt_make_id_hash (ctx);
285 *cur = hash_find (ctx->id_hash, p);
288 /* Remove the X-Mutt-References: header field. */
291 last->next = tmp->next;
293 hdr->env->userhdrs = tmp->next;
295 string_list_wipe(&tmp);
300 else if (ascii_strncasecmp ("X-Mutt-Fcc:", tmp->data, 11) == 0) {
301 p = vskipspaces(tmp->data + 11);
302 m_strcpy(fcc, fcclen, p);
303 mutt_pretty_mailbox (fcc);
305 /* remove the X-Mutt-Fcc: header field */
308 last->next = tmp->next;
310 hdr->env->userhdrs = tmp->next;
312 string_list_wipe(&tmp);
315 else if ((m_strncmp("Pgp:", tmp->data, 4) == 0 /* this is generated
316 * by old mutt versions
318 || m_strncmp("X-Mutt-PGP:", tmp->data, 11) == 0)) {
319 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
320 hdr->security |= APPLICATION_PGP;
322 /* remove the pgp field */
325 last->next = tmp->next;
327 hdr->env->userhdrs = tmp->next;
329 string_list_wipe(&tmp);
332 else if (m_strncmp("X-Mutt-SMIME:", tmp->data, 13) == 0) {
333 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
334 hdr->security |= APPLICATION_SMIME;
336 /* remove the smime field */
339 last->next = tmp->next;
341 hdr->env->userhdrs = tmp->next;
343 string_list_wipe(&tmp);
346 else if (m_strncmp("X-Mutt-Mix:", tmp->data, 11) == 0) {
349 string_list_wipe(&hdr->chain);
351 t = strtok (tmp->data + 11, " \t\n");
353 hdr->chain = mutt_add_list (hdr->chain, t);
354 t = strtok (NULL, " \t\n");
359 last->next = tmp->next;
361 hdr->env->userhdrs = tmp->next;
363 string_list_wipe(&tmp);
376 int mutt_parse_crypt_hdr (char *p, int set_signas)
379 char pgp_sign_as[LONG_STRING] = "\0", *q;
380 char smime_cryptalg[LONG_STRING] = "\0";
382 for (p = vskipspaces(p); *p; p++) {
394 if (*(p + 1) == '<') {
396 *p && *p != '>' && q < pgp_sign_as + sizeof (pgp_sign_as) - 1;
400 mutt_error _("Illegal PGP header");
409 /* This used to be the micalg parameter.
411 * It's no longer needed, so we just skip the parameter in order
412 * to be able to recall old messages.
416 if (*(p + 1) == '<') {
417 for (p += 2; *p && *p != '>'; p++);
419 mutt_error _("Illegal PGP header");
432 if (*(p + 1) == '<') {
435 && q < smime_cryptalg + sizeof (smime_cryptalg) - 1;
439 mutt_error _("Illegal S/MIME header");
454 mutt_error _("Illegal PGP header");
460 /* the cryptalg field must not be empty */
462 m_strreplace(&SmimeCryptAlg, smime_cryptalg);
464 if (set_signas || *pgp_sign_as)
465 m_strreplace(&PgpSignAs, pgp_sign_as);
472 int mutt_prepare_template (FILE * fp, CONTEXT * ctx, HEADER * newhdr,
473 HEADER * hdr, short weed)
476 char file[_POSIX_PATH_MAX];
485 if (!fp && (msg = mx_open_message (ctx, hdr->msgno)) == NULL)
493 /* parse the message header and MIME structure */
495 fseeko (fp, hdr->offset, 0);
496 newhdr->offset = hdr->offset;
497 newhdr->env = mutt_read_rfc822_header (fp, newhdr, 1, weed);
498 newhdr->content->length = hdr->content->length;
499 mutt_parse_part (fp, newhdr->content);
501 p_delete(&newhdr->env->message_id);
502 p_delete(&newhdr->env->mail_followup_to); /* really? */
504 /* decrypt pgp/mime encoded messages */
506 if ((APPLICATION_PGP | APPLICATION_SMIME) & hdr->security
507 && mutt_is_multipart_encrypted (newhdr->content))
509 int ccap = (APPLICATION_PGP | APPLICATION_SMIME) & hdr->security;
510 newhdr->security |= ENCRYPT | ccap;
511 if (!crypt_valid_passphrase (ccap))
514 mutt_message _("Decrypting message...");
516 if (((ccap & APPLICATION_PGP)
517 && crypt_pgp_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
518 || ((ccap & APPLICATION_SMIME)
519 && crypt_smime_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
522 mx_close_message (&msg);
523 envelope_delete(&newhdr->env);
524 body_list_wipe(&newhdr->content);
525 mutt_error _("Decryption failed.");
530 body_list_wipe(&newhdr->content);
537 * remove a potential multipart/signed layer - useful when
541 if (mutt_is_multipart_signed (newhdr->content)) {
542 newhdr->security |= SIGN;
543 if (ascii_strcasecmp(parameter_getval(newhdr->content->parameter, "protocol"),
544 "application/pgp-signature") == 0)
545 newhdr->security |= APPLICATION_PGP;
547 newhdr->security |= APPLICATION_SMIME;
549 /* destroy the signature */
550 body_list_wipe(&newhdr->content->parts->next);
551 newhdr->content = mutt_remove_multipart (newhdr->content);
556 * We don't need no primary multipart.
557 * Note: We _do_ preserve messages!
559 * XXX - we don't handle multipart/alternative in any
560 * smart way when sending messages. However, one may
561 * consider this a feature.
565 if (newhdr->content->type == TYPEMULTIPART)
566 newhdr->content = mutt_remove_multipart (newhdr->content);
570 /* create temporary files for all attachments */
571 for (b = newhdr->content; b; b = b->next) {
573 /* what follows is roughly a receive-mode variant of
574 * mutt_get_tmp_attachment () from muttlib.c
579 m_strcpy(file, sizeof(file), b->filename);
580 b->d_filename = m_strdup(b->filename);
583 /* avoid Content-Disposition: header with temporary filename */
587 /* set up state flags */
591 if (b->type == TYPETEXT) {
592 b->noconv = !ascii_strcasecmp("yes", parameter_getval(b->parameter,
595 s.flags |= M_CHARCONV;
597 parameter_delval(&b->parameter, "x-mutt-noconv");
600 s.fpout = m_tempfile(file, sizeof(file), NONULL(Tempdir), file);
604 if (mutt_is_application_pgp (b) & (ENCRYPT | SIGN)) {
606 mutt_body_handler (b, &s);
608 newhdr->security |= mutt_is_application_pgp (newhdr->content);
611 m_strreplace(&b->subtype, "plain");
612 parameter_delval(&b->parameter, "x-action");
615 mutt_decode_attachment (b, &s);
617 if (m_fclose(&s.fpout) != 0)
620 m_strreplace(&b->filename, file);
623 mutt_stamp_attachment (b);
625 body_list_wipe(&b->parts);
627 b->hdr->content = NULL; /* avoid dangling pointer */
630 /* Fix encryption flags. */
632 /* No inline if multipart. */
633 if ((newhdr->security & INLINE) && newhdr->content->next)
634 newhdr->security &= ~INLINE;
636 /* Theoretically, both could be set. Take the one the user wants to set by default. */
637 if ((newhdr->security & APPLICATION_PGP)
638 && (newhdr->security & APPLICATION_SMIME)) {
639 if (option (OPTSMIMEISDEFAULT))
640 newhdr->security &= ~APPLICATION_PGP;
642 newhdr->security &= ~APPLICATION_SMIME;
653 mx_close_message (&msg);
656 envelope_delete(&newhdr->env);
657 body_list_wipe(&newhdr->content);