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/lib-ui.h>
18 #include <lib-mx/mx.h>
20 #include <lib-sys/unix.h>
28 #include <imap/imap.h>
30 static short PostCount = 0;
31 static CONTEXT *PostContext = NULL;
32 static short UpdateNumPostponed = 0;
34 /* Return the number of postponed messages.
35 * if force is 0, use a cached value if it is costly to get a fresh
36 * count (IMAP) - else check.
38 int mutt_num_postponed (int force)
43 static time_t LastModify = 0;
44 static char *OldPostponed = NULL;
46 if (UpdateNumPostponed) {
47 UpdateNumPostponed = 0;
51 if (Postponed != OldPostponed) {
52 OldPostponed = Postponed;
60 /* LastModify is useless for IMAP */
61 if (imap_is_magic (Postponed, NULL) == M_IMAP) {
65 newpc = imap_mailbox_check (Postponed, 0);
73 if (stat (Postponed, &st) == -1) {
79 if (S_ISDIR (st.st_mode)) {
80 /* if we have a maildir mailbox, we need to stat the "new" dir */
82 char buf[_POSIX_PATH_MAX];
84 snprintf (buf, sizeof (buf), "%s/new", Postponed);
85 if (access (buf, F_OK) == 0 && stat (buf, &st) == -1) {
92 if (LastModify < st.st_mtime) {
93 LastModify = st.st_mtime;
95 if (access (Postponed, R_OK | F_OK) != 0)
96 return (PostCount = 0);
97 if (mx_open_mailbox (Postponed, M_NOSORT | M_QUIET, &ctx) == NULL)
100 PostCount = ctx.msgcount;
101 mx_fastclose_mailbox (&ctx);
107 void mutt_update_num_postponed (void)
109 UpdateNumPostponed = 1;
112 static void post_entry (char *s, ssize_t slen, MUTTMENU * menu, int entry)
114 CONTEXT *ctx = (CONTEXT *)menu->data;
115 _mutt_make_string(s, slen, NONULL (HdrFmt), ctx, ctx->hdrs[entry], 0);
118 static HEADER *select_msg (void)
121 int i, done = 0, r = -1;
124 menu = mutt_new_menu ();
125 menu->make_entry = post_entry;
126 menu->menu = MENU_POST;
127 menu->max = PostContext->msgcount;
128 menu->title = _("Postponed Messages");
129 menu->data = PostContext;
131 /* The postponed mailbox is setup to have sorting disabled, but the global
132 * Sort variable may indicate something different. Sorting has to be
133 * disabled while the postpone menu is being displayed. */
138 switch (i = mutt_menuLoop (menu)) {
141 mutt_set_flag (PostContext, PostContext->hdrs[menu->current], M_DELETE,
142 (i == OP_DELETE) ? 1 : 0);
143 PostCount = PostContext->msgcount - PostContext->deleted;
144 if (option (OPTRESOLVE) && menu->current < menu->max - 1) {
145 menu->oldcurrent = menu->current;
147 if (menu->current >= menu->top + menu->pagelen) {
148 menu->top = menu->current;
149 menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
152 menu->redraw |= REDRAW_MOTION_RESYNCH;
155 menu->redraw = REDRAW_CURRENT;
158 case OP_GENERIC_SELECT_ENTRY:
170 mutt_menuDestroy (&menu);
171 return (r > -1 ? PostContext->hdrs[r] : NULL);
175 * ctx Context info, used when recalling a message to which
177 * hdr envelope/attachment info for recalled message
178 * cur if message was a reply, `cur' is set to the message which
179 * `hdr' is in reply to
180 * fcc fcc for the recalled message
181 * fcclen max length of fcc
184 * -1 error/no messages
186 * SENDREPLY recalled message is a reply
188 int mutt_get_postponed (CONTEXT * ctx, HEADER * hdr, HEADER ** cur, char *fcc,
192 int code = SENDPOSTPONED;
194 string_list_t *last = NULL;
202 if ((PostContext = mx_open_mailbox (Postponed, M_NOSORT, NULL)) == NULL) {
204 mutt_error _("No postponed messages.");
209 if (!PostContext->msgcount) {
211 mx_close_mailbox (PostContext, NULL);
212 p_delete(&PostContext);
213 mutt_error _("No postponed messages.");
218 if (PostContext->msgcount == 1) {
219 /* only one message, so just use that one. */
220 h = PostContext->hdrs[0];
222 else if ((h = select_msg ()) == NULL) {
223 mx_close_mailbox (PostContext, NULL);
224 p_delete(&PostContext);
228 if (mutt_prepare_template (NULL, PostContext, hdr, h, 0) < 0) {
229 mx_fastclose_mailbox (PostContext);
230 p_delete(&PostContext);
234 /* finished with this message, so delete it. */
235 mutt_set_flag (PostContext, h, M_DELETE, 1);
237 /* and consider it saved, so that it won't be moved to the trash folder */
238 mutt_set_flag (PostContext, h, M_APPENDED, 1);
240 /* update the count for the status display */
241 PostCount = PostContext->msgcount - PostContext->deleted;
243 /* avoid the "purge deleted messages" prompt */
244 opt_delete = quadoption (OPT_DELETE);
245 set_quadoption (OPT_DELETE, M_YES);
246 mx_close_mailbox (PostContext, NULL);
247 set_quadoption (OPT_DELETE, opt_delete);
249 p_delete(&PostContext);
251 for (tmp = hdr->env->userhdrs; tmp;) {
252 if (ascii_strncasecmp ("X-Mutt-References:", tmp->data, 18) == 0) {
254 /* if a mailbox is currently open, look to see if the orignal message
255 the user attempted to reply to is in this mailbox */
256 p = vskipspaces(tmp->data + 18);
258 ctx->id_hash = mutt_make_id_hash (ctx);
259 *cur = hash_find (ctx->id_hash, p);
262 /* Remove the X-Mutt-References: header field. */
265 last->next = tmp->next;
267 hdr->env->userhdrs = tmp->next;
269 string_list_wipe(&tmp);
274 else if (ascii_strncasecmp ("X-Mutt-Fcc:", tmp->data, 11) == 0) {
275 p = vskipspaces(tmp->data + 11);
276 m_strcpy(fcc, fcclen, p);
277 mutt_pretty_mailbox (fcc);
279 /* remove the X-Mutt-Fcc: header field */
282 last->next = tmp->next;
284 hdr->env->userhdrs = tmp->next;
286 string_list_wipe(&tmp);
289 else if (m_strncmp("X-Mutt-PGP:", tmp->data, 11) == 0) {
290 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
291 hdr->security |= APPLICATION_PGP;
293 /* remove the pgp field */
296 last->next = tmp->next;
298 hdr->env->userhdrs = tmp->next;
300 string_list_wipe(&tmp);
303 else if (m_strncmp("X-Mutt-SMIME:", tmp->data, 13) == 0) {
304 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
305 hdr->security |= APPLICATION_SMIME;
307 /* remove the smime field */
310 last->next = tmp->next;
312 hdr->env->userhdrs = tmp->next;
314 string_list_wipe(&tmp);
325 int mutt_parse_crypt_hdr (char *p, int set_signas)
328 char pgp_sign_as[LONG_STRING] = "\0", *q;
329 char smime_cryptalg[LONG_STRING] = "\0";
331 for (p = vskipspaces(p); *p; p++) {
343 if (*(p + 1) == '<') {
345 *p && *p != '>' && q < pgp_sign_as + sizeof (pgp_sign_as) - 1;
349 mutt_error _("Illegal PGP header");
358 /* This used to be the micalg parameter.
360 * It's no longer needed, so we just skip the parameter in order
361 * to be able to recall old messages.
365 if (*(p + 1) == '<') {
366 for (p += 2; *p && *p != '>'; p++);
368 mutt_error _("Illegal PGP header");
381 if (*(p + 1) == '<') {
384 && q < smime_cryptalg + sizeof (smime_cryptalg) - 1;
388 mutt_error _("Illegal S/MIME header");
403 mutt_error _("Illegal PGP header");
409 /* the cryptalg field must not be empty */
411 m_strreplace(&SmimeCryptAlg, smime_cryptalg);
413 if (set_signas || *pgp_sign_as)
414 m_strreplace(&PgpSignAs, pgp_sign_as);
421 int mutt_prepare_template (FILE * fp, CONTEXT * ctx, HEADER * newhdr,
422 HEADER * hdr, short weed)
425 char file[_POSIX_PATH_MAX];
434 if (!fp && (msg = mx_open_message (ctx, hdr->msgno)) == NULL)
442 /* parse the message header and MIME structure */
444 fseeko (fp, hdr->offset, 0);
445 newhdr->offset = hdr->offset;
446 newhdr->env = mutt_read_rfc822_header (fp, newhdr, 1, weed);
447 newhdr->content->length = hdr->content->length;
448 mutt_parse_part (fp, newhdr->content);
450 p_delete(&newhdr->env->message_id);
451 p_delete(&newhdr->env->mail_followup_to); /* really? */
453 /* decrypt pgp/mime encoded messages */
455 if ((APPLICATION_PGP | APPLICATION_SMIME) & hdr->security
456 && mutt_is_multipart_encrypted (newhdr->content))
458 int ccap = (APPLICATION_PGP | APPLICATION_SMIME) & hdr->security;
459 newhdr->security |= ENCRYPT | ccap;
460 mutt_message _("Decrypting message...");
462 if (((ccap & APPLICATION_PGP)
463 && crypt_pgp_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
464 || ((ccap & APPLICATION_SMIME)
465 && crypt_smime_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
468 mx_close_message (&msg);
469 envelope_delete(&newhdr->env);
470 body_list_wipe(&newhdr->content);
471 mutt_error _("Decryption failed.");
476 body_list_wipe(&newhdr->content);
483 * remove a potential multipart/signed layer - useful when
487 if (mutt_is_multipart_signed (newhdr->content)) {
488 newhdr->security |= SIGN;
489 if (ascii_strcasecmp(parameter_getval(newhdr->content->parameter, "protocol"),
490 "application/pgp-signature") == 0)
491 newhdr->security |= APPLICATION_PGP;
493 newhdr->security |= APPLICATION_SMIME;
495 /* destroy the signature */
496 body_list_wipe(&newhdr->content->parts->next);
497 newhdr->content = mutt_remove_multipart (newhdr->content);
502 * We don't need no primary multipart.
503 * Note: We _do_ preserve messages!
505 * XXX - we don't handle multipart/alternative in any
506 * smart way when sending messages. However, one may
507 * consider this a feature.
511 if (newhdr->content->type == TYPEMULTIPART)
512 newhdr->content = mutt_remove_multipart (newhdr->content);
516 /* create temporary files for all attachments */
517 for (b = newhdr->content; b; b = b->next) {
519 /* what follows is roughly a receive-mode variant of
520 * mutt_get_tmp_attachment () from muttlib.c
525 m_strcpy(file, sizeof(file), b->filename);
526 b->d_filename = m_strdup(b->filename);
529 /* avoid Content-Disposition: header with temporary filename */
533 /* set up state flags */
537 if (b->type == TYPETEXT) {
538 b->noconv = !ascii_strcasecmp("yes", parameter_getval(b->parameter,
541 s.flags |= M_CHARCONV;
543 parameter_delval(&b->parameter, "x-mutt-noconv");
546 s.fpout = m_tempfile(file, sizeof(file), NONULL(mod_core.tmpdir), file);
550 if (mutt_is_application_pgp (b) & (ENCRYPT | SIGN)) {
552 mutt_body_handler (b, &s);
554 newhdr->security |= mutt_is_application_pgp (newhdr->content);
557 m_strreplace(&b->subtype, "plain");
558 parameter_delval(&b->parameter, "x-action");
561 mutt_decode_attachment (b, &s);
563 if (m_fclose(&s.fpout) != 0)
566 m_strreplace(&b->filename, file);
569 mutt_stamp_attachment (b);
571 body_list_wipe(&b->parts);
573 b->hdr->content = NULL; /* avoid dangling pointer */
576 /* Fix encryption flags. */
578 /* No inline if multipart. */
579 if ((newhdr->security & INLINE) && newhdr->content->next)
580 newhdr->security &= ~INLINE;
582 /* Theoretically, both could be set. Take the one the user wants to set by default. */
583 if ((newhdr->security & APPLICATION_PGP)
584 && (newhdr->security & APPLICATION_SMIME)) {
585 if (mod_crypt.smime_is_default)
586 newhdr->security &= ~APPLICATION_PGP;
588 newhdr->security &= ~APPLICATION_SMIME;
599 mx_close_message (&msg);
602 envelope_delete(&newhdr->env);
603 body_list_wipe(&newhdr->content);