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/lib-ui.h>
16 #include <lib-ui/menu.h>
17 #include <lib-mx/mx.h>
19 #include <lib-sys/unix.h>
27 #include <imap/imap.h>
29 static short PostCount = 0;
30 static CONTEXT *PostContext = NULL;
31 static short UpdateNumPostponed = 0;
33 /* Return the number of postponed messages.
34 * if force is 0, use a cached value if it is costly to get a fresh
35 * count (IMAP) - else check.
37 int mutt_num_postponed (int force)
42 static time_t LastModify = 0;
43 static char *OldPostponed = NULL;
45 if (UpdateNumPostponed) {
46 UpdateNumPostponed = 0;
50 if (Postponed != OldPostponed) {
51 OldPostponed = Postponed;
59 /* LastModify is useless for IMAP */
60 if (imap_is_magic (Postponed, NULL) == M_IMAP) {
64 newpc = imap_mailbox_check (Postponed, 0);
72 if (stat (Postponed, &st) == -1) {
78 if (S_ISDIR (st.st_mode)) {
79 /* if we have a maildir mailbox, we need to stat the "new" dir */
81 char buf[_POSIX_PATH_MAX];
83 snprintf (buf, sizeof (buf), "%s/new", Postponed);
84 if (access (buf, F_OK) == 0 && stat (buf, &st) == -1) {
91 if (LastModify < st.st_mtime) {
92 LastModify = st.st_mtime;
94 if (access (Postponed, R_OK | F_OK) != 0)
96 if (mx_open_mailbox (Postponed, M_NOSORT | M_QUIET, &ctx) == NULL)
99 PostCount = ctx.msgcount;
100 mx_fastclose_mailbox (&ctx);
106 void mutt_update_num_postponed (void)
108 UpdateNumPostponed = 1;
111 static void post_entry (char *s, ssize_t slen, MUTTMENU * menu, int entry)
113 CONTEXT *ctx = (CONTEXT *)menu->data;
114 _mutt_make_string(s, slen, NONULL (HdrFmt), ctx, ctx->hdrs[entry], 0);
117 static HEADER *select_msg (void)
120 int i, done = 0, r = -1;
123 menu = mutt_new_menu ();
124 menu->make_entry = post_entry;
125 menu->menu = MENU_POST;
126 menu->max = PostContext->msgcount;
127 menu->title = _("Postponed Messages");
128 menu->data = PostContext;
130 /* The postponed mailbox is setup to have sorting disabled, but the global
131 * Sort variable may indicate something different. Sorting has to be
132 * disabled while the postpone menu is being displayed. */
137 switch (i = mutt_menuLoop (menu)) {
140 mutt_set_flag (PostContext, PostContext->hdrs[menu->current], M_DELETE,
141 (i == OP_DELETE) ? 1 : 0);
142 PostCount = PostContext->msgcount - PostContext->deleted;
143 if (option (OPTRESOLVE) && menu->current < menu->max - 1) {
144 menu->oldcurrent = menu->current;
146 if (menu->current >= menu->top + menu->pagelen) {
147 menu->top = menu->current;
148 menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
151 menu->redraw |= REDRAW_MOTION_RESYNCH;
154 menu->redraw = REDRAW_CURRENT;
157 case OP_GENERIC_SELECT_ENTRY:
169 mutt_menuDestroy (&menu);
170 return r > -1 ? PostContext->hdrs[r] : NULL;
174 * ctx Context info, used when recalling a message to which
176 * hdr envelope/attachment info for recalled message
177 * cur if message was a reply, `cur' is set to the message which
178 * `hdr' is in reply to
179 * fcc fcc for the recalled message
180 * fcclen max length of fcc
183 * -1 error/no messages
185 * SENDREPLY recalled message is a reply
187 int mutt_get_postponed (CONTEXT * ctx, HEADER * hdr, HEADER ** cur, char *fcc,
191 int code = SENDPOSTPONED;
193 string_list_t *last = NULL;
201 if ((PostContext = mx_open_mailbox (Postponed, M_NOSORT, NULL)) == NULL) {
203 mutt_error _("No postponed messages.");
208 if (!PostContext->msgcount) {
210 mx_close_mailbox (PostContext, NULL);
211 p_delete(&PostContext);
212 mutt_error _("No postponed messages.");
217 if (PostContext->msgcount == 1) {
218 /* only one message, so just use that one. */
219 h = PostContext->hdrs[0];
221 else if ((h = select_msg ()) == NULL) {
222 mx_close_mailbox (PostContext, NULL);
223 p_delete(&PostContext);
227 if (mutt_prepare_template (NULL, PostContext, hdr, h, 0) < 0) {
228 mx_fastclose_mailbox (PostContext);
229 p_delete(&PostContext);
233 /* finished with this message, so delete it. */
234 mutt_set_flag (PostContext, h, M_DELETE, 1);
236 /* and consider it saved, so that it won't be moved to the trash folder */
237 mutt_set_flag (PostContext, h, M_APPENDED, 1);
239 /* update the count for the status display */
240 PostCount = PostContext->msgcount - PostContext->deleted;
242 /* avoid the "purge deleted messages" prompt */
243 opt_delete = quadoption (OPT_DELETE);
244 set_quadoption (OPT_DELETE, M_YES);
245 mx_close_mailbox (PostContext, NULL);
246 set_quadoption (OPT_DELETE, opt_delete);
248 p_delete(&PostContext);
250 for (tmp = hdr->env->userhdrs; tmp;) {
251 if (ascii_strncasecmp ("X-Mutt-References:", tmp->data, 18) == 0) {
253 /* if a mailbox is currently open, look to see if the orignal message
254 the user attempted to reply to is in this mailbox */
255 p = vskipspaces(tmp->data + 18);
257 ctx->id_hash = mutt_make_id_hash (ctx);
258 *cur = hash_find (ctx->id_hash, p);
261 /* Remove the X-Mutt-References: header field. */
264 last->next = tmp->next;
266 hdr->env->userhdrs = tmp->next;
268 string_list_wipe(&tmp);
273 else if (ascii_strncasecmp ("X-Mutt-Fcc:", tmp->data, 11) == 0) {
274 p = vskipspaces(tmp->data + 11);
275 m_strcpy(fcc, fcclen, p);
276 mutt_pretty_mailbox (fcc);
278 /* remove the X-Mutt-Fcc: header field */
281 last->next = tmp->next;
283 hdr->env->userhdrs = tmp->next;
285 string_list_wipe(&tmp);
288 else if (m_strncmp("X-Mutt-PGP:", tmp->data, 11) == 0) {
289 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
290 hdr->security |= APPLICATION_PGP;
292 /* remove the pgp field */
295 last->next = tmp->next;
297 hdr->env->userhdrs = tmp->next;
299 string_list_wipe(&tmp);
302 else if (m_strncmp("X-Mutt-SMIME:", tmp->data, 13) == 0) {
303 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
304 hdr->security |= APPLICATION_SMIME;
306 /* remove the smime field */
309 last->next = tmp->next;
311 hdr->env->userhdrs = tmp->next;
313 string_list_wipe(&tmp);
324 int mutt_parse_crypt_hdr (char *p, int set_signas)
327 char pgp_sign_as[LONG_STRING] = "\0", *q;
328 char smime_cryptalg[LONG_STRING] = "\0";
330 for (p = vskipspaces(p); *p; p++) {
342 if (*(p + 1) == '<') {
344 *p && *p != '>' && q < pgp_sign_as + sizeof (pgp_sign_as) - 1;
348 mutt_error _("Illegal PGP header");
357 /* This used to be the micalg parameter.
359 * It's no longer needed, so we just skip the parameter in order
360 * to be able to recall old messages.
364 if (*(p + 1) == '<') {
365 for (p += 2; *p && *p != '>'; p++);
367 mutt_error _("Illegal PGP header");
380 if (*(p + 1) == '<') {
383 && q < smime_cryptalg + sizeof (smime_cryptalg) - 1;
387 mutt_error _("Illegal S/MIME header");
402 mutt_error _("Illegal PGP header");
408 /* the cryptalg field must not be empty */
410 m_strreplace(&SmimeCryptAlg, smime_cryptalg);
412 if (set_signas || *pgp_sign_as)
413 m_strreplace(&PgpSignAs, pgp_sign_as);
420 int mutt_prepare_template (FILE * fp, CONTEXT * ctx, HEADER * newhdr,
421 HEADER * hdr, short weed)
424 char file[_POSIX_PATH_MAX];
433 if (!fp && (msg = mx_open_message (ctx, hdr->msgno)) == NULL)
441 /* parse the message header and MIME structure */
443 fseeko (fp, hdr->offset, 0);
444 newhdr->offset = hdr->offset;
445 newhdr->env = mutt_read_rfc822_header (fp, newhdr, 1, weed);
446 newhdr->content->length = hdr->content->length;
447 mutt_parse_part (fp, newhdr->content);
449 p_delete(&newhdr->env->message_id);
450 p_delete(&newhdr->env->mail_followup_to); /* really? */
452 /* decrypt pgp/mime encoded messages */
454 if ((APPLICATION_PGP | APPLICATION_SMIME) & hdr->security
455 && mutt_is_multipart_encrypted (newhdr->content))
457 int ccap = (APPLICATION_PGP | APPLICATION_SMIME) & hdr->security;
458 newhdr->security |= ENCRYPT | ccap;
459 mutt_message _("Decrypting message...");
461 if (((ccap & APPLICATION_PGP)
462 && crypt_pgp_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
463 || ((ccap & APPLICATION_SMIME)
464 && crypt_smime_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
467 mx_close_message (&msg);
468 envelope_delete(&newhdr->env);
469 body_list_wipe(&newhdr->content);
470 mutt_error _("Decryption failed.");
475 body_list_wipe(&newhdr->content);
482 * remove a potential multipart/signed layer - useful when
486 if (mutt_is_multipart_signed (newhdr->content)) {
487 newhdr->security |= SIGN;
488 if (ascii_strcasecmp(parameter_getval(newhdr->content->parameter, "protocol"),
489 "application/pgp-signature") == 0)
490 newhdr->security |= APPLICATION_PGP;
492 newhdr->security |= APPLICATION_SMIME;
494 /* destroy the signature */
495 body_list_wipe(&newhdr->content->parts->next);
496 newhdr->content = mutt_remove_multipart (newhdr->content);
501 * We don't need no primary multipart.
502 * Note: We _do_ preserve messages!
504 * XXX - we don't handle multipart/alternative in any
505 * smart way when sending messages. However, one may
506 * consider this a feature.
510 if (newhdr->content->type == TYPEMULTIPART)
511 newhdr->content = mutt_remove_multipart (newhdr->content);
515 /* create temporary files for all attachments */
516 for (b = newhdr->content; b; b = b->next) {
518 /* what follows is roughly a receive-mode variant of
519 * mutt_get_tmp_attachment () from muttlib.c
524 m_strcpy(file, sizeof(file), b->filename);
525 b->d_filename = m_strdup(b->filename);
528 /* avoid Content-Disposition: header with temporary filename */
532 /* set up state flags */
536 if (b->type == TYPETEXT) {
537 b->noconv = !ascii_strcasecmp("yes", parameter_getval(b->parameter,
540 s.flags |= M_CHARCONV;
542 parameter_delval(&b->parameter, "x-mutt-noconv");
545 s.fpout = m_tempfile(file, sizeof(file), NONULL(mod_core.tmpdir), file);
549 if (mutt_is_application_pgp (b) & (ENCRYPT | SIGN)) {
551 mutt_body_handler (b, &s);
553 newhdr->security |= mutt_is_application_pgp (newhdr->content);
556 m_strreplace(&b->subtype, "plain");
557 parameter_delval(&b->parameter, "x-action");
560 mutt_decode_attachment (b, &s);
562 if (m_fclose(&s.fpout) != 0)
565 m_strreplace(&b->filename, file);
568 mutt_stamp_attachment (b);
570 body_list_wipe(&b->parts);
572 b->hdr->content = NULL; /* avoid dangling pointer */
575 /* Fix encryption flags. */
577 /* No inline if multipart. */
578 if ((newhdr->security & INLINE) && newhdr->content->next)
579 newhdr->security &= ~INLINE;
581 /* Theoretically, both could be set. Take the one the user wants to set by default. */
582 if ((newhdr->security & APPLICATION_PGP)
583 && (newhdr->security & APPLICATION_SMIME)) {
584 if (mod_crypt.smime_is_default)
585 newhdr->security &= ~APPLICATION_PGP;
587 newhdr->security &= ~APPLICATION_SMIME;
598 mx_close_message (&msg);
601 envelope_delete(&newhdr->env);
602 body_list_wipe(&newhdr->content);