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) {
94 int optnews = option (OPTNEWS);
96 LastModify = st.st_mtime;
98 if (access (Postponed, R_OK | F_OK) != 0)
99 return (PostCount = 0);
102 unset_option (OPTNEWS);
104 if (mx_open_mailbox (Postponed, M_NOSORT | M_QUIET, &ctx) == NULL)
107 PostCount = ctx.msgcount;
108 mx_fastclose_mailbox (&ctx);
111 set_option (OPTNEWS);
118 void mutt_update_num_postponed (void)
120 UpdateNumPostponed = 1;
123 static void post_entry (char *s, ssize_t slen, MUTTMENU * menu, int entry)
125 CONTEXT *ctx = (CONTEXT *)menu->data;
126 _mutt_make_string(s, slen, NONULL (HdrFmt), ctx, ctx->hdrs[entry], 0);
129 static HEADER *select_msg (void)
132 int i, done = 0, r = -1;
135 menu = mutt_new_menu ();
136 menu->make_entry = post_entry;
137 menu->menu = MENU_POST;
138 menu->max = PostContext->msgcount;
139 menu->title = _("Postponed Messages");
140 menu->data = PostContext;
142 /* The postponed mailbox is setup to have sorting disabled, but the global
143 * Sort variable may indicate something different. Sorting has to be
144 * disabled while the postpone menu is being displayed. */
149 switch (i = mutt_menuLoop (menu)) {
152 mutt_set_flag (PostContext, PostContext->hdrs[menu->current], M_DELETE,
153 (i == OP_DELETE) ? 1 : 0);
154 PostCount = PostContext->msgcount - PostContext->deleted;
155 if (option (OPTRESOLVE) && menu->current < menu->max - 1) {
156 menu->oldcurrent = menu->current;
158 if (menu->current >= menu->top + menu->pagelen) {
159 menu->top = menu->current;
160 menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
163 menu->redraw |= REDRAW_MOTION_RESYNCH;
166 menu->redraw = REDRAW_CURRENT;
169 case OP_GENERIC_SELECT_ENTRY:
181 mutt_menuDestroy (&menu);
182 return (r > -1 ? PostContext->hdrs[r] : NULL);
186 * ctx Context info, used when recalling a message to which
188 * hdr envelope/attachment info for recalled message
189 * cur if message was a reply, `cur' is set to the message which
190 * `hdr' is in reply to
191 * fcc fcc for the recalled message
192 * fcclen max length of fcc
195 * -1 error/no messages
197 * SENDREPLY recalled message is a reply
199 int mutt_get_postponed (CONTEXT * ctx, HEADER * hdr, HEADER ** cur, char *fcc,
203 int code = SENDPOSTPONED;
205 string_list_t *last = NULL;
213 if ((PostContext = mx_open_mailbox (Postponed, M_NOSORT, NULL)) == NULL) {
215 mutt_error _("No postponed messages.");
220 if (!PostContext->msgcount) {
222 mx_close_mailbox (PostContext, NULL);
223 p_delete(&PostContext);
224 mutt_error _("No postponed messages.");
229 if (PostContext->msgcount == 1) {
230 /* only one message, so just use that one. */
231 h = PostContext->hdrs[0];
233 else if ((h = select_msg ()) == NULL) {
234 mx_close_mailbox (PostContext, NULL);
235 p_delete(&PostContext);
239 if (mutt_prepare_template (NULL, PostContext, hdr, h, 0) < 0) {
240 mx_fastclose_mailbox (PostContext);
241 p_delete(&PostContext);
245 /* finished with this message, so delete it. */
246 mutt_set_flag (PostContext, h, M_DELETE, 1);
248 /* and consider it saved, so that it won't be moved to the trash folder */
249 mutt_set_flag (PostContext, h, M_APPENDED, 1);
251 /* update the count for the status display */
252 PostCount = PostContext->msgcount - PostContext->deleted;
254 /* avoid the "purge deleted messages" prompt */
255 opt_delete = quadoption (OPT_DELETE);
256 set_quadoption (OPT_DELETE, M_YES);
257 mx_close_mailbox (PostContext, NULL);
258 set_quadoption (OPT_DELETE, opt_delete);
260 p_delete(&PostContext);
262 for (tmp = hdr->env->userhdrs; tmp;) {
263 if (ascii_strncasecmp ("X-Mutt-References:", tmp->data, 18) == 0) {
265 /* if a mailbox is currently open, look to see if the orignal message
266 the user attempted to reply to is in this mailbox */
267 p = vskipspaces(tmp->data + 18);
269 ctx->id_hash = mutt_make_id_hash (ctx);
270 *cur = hash_find (ctx->id_hash, p);
273 /* Remove the X-Mutt-References: header field. */
276 last->next = tmp->next;
278 hdr->env->userhdrs = tmp->next;
280 string_list_wipe(&tmp);
285 else if (ascii_strncasecmp ("X-Mutt-Fcc:", tmp->data, 11) == 0) {
286 p = vskipspaces(tmp->data + 11);
287 m_strcpy(fcc, fcclen, p);
288 mutt_pretty_mailbox (fcc);
290 /* remove the X-Mutt-Fcc: header field */
293 last->next = tmp->next;
295 hdr->env->userhdrs = tmp->next;
297 string_list_wipe(&tmp);
300 else if (m_strncmp("X-Mutt-PGP:", tmp->data, 11) == 0) {
301 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
302 hdr->security |= APPLICATION_PGP;
304 /* remove the pgp field */
307 last->next = tmp->next;
309 hdr->env->userhdrs = tmp->next;
311 string_list_wipe(&tmp);
314 else if (m_strncmp("X-Mutt-SMIME:", tmp->data, 13) == 0) {
315 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
316 hdr->security |= APPLICATION_SMIME;
318 /* remove the smime field */
321 last->next = tmp->next;
323 hdr->env->userhdrs = tmp->next;
325 string_list_wipe(&tmp);
336 int mutt_parse_crypt_hdr (char *p, int set_signas)
339 char pgp_sign_as[LONG_STRING] = "\0", *q;
340 char smime_cryptalg[LONG_STRING] = "\0";
342 for (p = vskipspaces(p); *p; p++) {
354 if (*(p + 1) == '<') {
356 *p && *p != '>' && q < pgp_sign_as + sizeof (pgp_sign_as) - 1;
360 mutt_error _("Illegal PGP header");
369 /* This used to be the micalg parameter.
371 * It's no longer needed, so we just skip the parameter in order
372 * to be able to recall old messages.
376 if (*(p + 1) == '<') {
377 for (p += 2; *p && *p != '>'; p++);
379 mutt_error _("Illegal PGP header");
392 if (*(p + 1) == '<') {
395 && q < smime_cryptalg + sizeof (smime_cryptalg) - 1;
399 mutt_error _("Illegal S/MIME header");
414 mutt_error _("Illegal PGP header");
420 /* the cryptalg field must not be empty */
422 m_strreplace(&SmimeCryptAlg, smime_cryptalg);
424 if (set_signas || *pgp_sign_as)
425 m_strreplace(&PgpSignAs, pgp_sign_as);
432 int mutt_prepare_template (FILE * fp, CONTEXT * ctx, HEADER * newhdr,
433 HEADER * hdr, short weed)
436 char file[_POSIX_PATH_MAX];
445 if (!fp && (msg = mx_open_message (ctx, hdr->msgno)) == NULL)
453 /* parse the message header and MIME structure */
455 fseeko (fp, hdr->offset, 0);
456 newhdr->offset = hdr->offset;
457 newhdr->env = mutt_read_rfc822_header (fp, newhdr, 1, weed);
458 newhdr->content->length = hdr->content->length;
459 mutt_parse_part (fp, newhdr->content);
461 p_delete(&newhdr->env->message_id);
462 p_delete(&newhdr->env->mail_followup_to); /* really? */
464 /* decrypt pgp/mime encoded messages */
466 if ((APPLICATION_PGP | APPLICATION_SMIME) & hdr->security
467 && mutt_is_multipart_encrypted (newhdr->content))
469 int ccap = (APPLICATION_PGP | APPLICATION_SMIME) & hdr->security;
470 newhdr->security |= ENCRYPT | ccap;
471 mutt_message _("Decrypting message...");
473 if (((ccap & APPLICATION_PGP)
474 && crypt_pgp_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
475 || ((ccap & APPLICATION_SMIME)
476 && crypt_smime_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
479 mx_close_message (&msg);
480 envelope_delete(&newhdr->env);
481 body_list_wipe(&newhdr->content);
482 mutt_error _("Decryption failed.");
487 body_list_wipe(&newhdr->content);
494 * remove a potential multipart/signed layer - useful when
498 if (mutt_is_multipart_signed (newhdr->content)) {
499 newhdr->security |= SIGN;
500 if (ascii_strcasecmp(parameter_getval(newhdr->content->parameter, "protocol"),
501 "application/pgp-signature") == 0)
502 newhdr->security |= APPLICATION_PGP;
504 newhdr->security |= APPLICATION_SMIME;
506 /* destroy the signature */
507 body_list_wipe(&newhdr->content->parts->next);
508 newhdr->content = mutt_remove_multipart (newhdr->content);
513 * We don't need no primary multipart.
514 * Note: We _do_ preserve messages!
516 * XXX - we don't handle multipart/alternative in any
517 * smart way when sending messages. However, one may
518 * consider this a feature.
522 if (newhdr->content->type == TYPEMULTIPART)
523 newhdr->content = mutt_remove_multipart (newhdr->content);
527 /* create temporary files for all attachments */
528 for (b = newhdr->content; b; b = b->next) {
530 /* what follows is roughly a receive-mode variant of
531 * mutt_get_tmp_attachment () from muttlib.c
536 m_strcpy(file, sizeof(file), b->filename);
537 b->d_filename = m_strdup(b->filename);
540 /* avoid Content-Disposition: header with temporary filename */
544 /* set up state flags */
548 if (b->type == TYPETEXT) {
549 b->noconv = !ascii_strcasecmp("yes", parameter_getval(b->parameter,
552 s.flags |= M_CHARCONV;
554 parameter_delval(&b->parameter, "x-mutt-noconv");
557 s.fpout = m_tempfile(file, sizeof(file), NONULL(mod_core.tmpdir), file);
561 if (mutt_is_application_pgp (b) & (ENCRYPT | SIGN)) {
563 mutt_body_handler (b, &s);
565 newhdr->security |= mutt_is_application_pgp (newhdr->content);
568 m_strreplace(&b->subtype, "plain");
569 parameter_delval(&b->parameter, "x-action");
572 mutt_decode_attachment (b, &s);
574 if (m_fclose(&s.fpout) != 0)
577 m_strreplace(&b->filename, file);
580 mutt_stamp_attachment (b);
582 body_list_wipe(&b->parts);
584 b->hdr->content = NULL; /* avoid dangling pointer */
587 /* Fix encryption flags. */
589 /* No inline if multipart. */
590 if ((newhdr->security & INLINE) && newhdr->content->next)
591 newhdr->security &= ~INLINE;
593 /* Theoretically, both could be set. Take the one the user wants to set by default. */
594 if ((newhdr->security & APPLICATION_PGP)
595 && (newhdr->security & APPLICATION_SMIME)) {
596 if (mod_crypt.smime_is_default)
597 newhdr->security &= ~APPLICATION_PGP;
599 newhdr->security &= ~APPLICATION_SMIME;
610 mx_close_message (&msg);
613 envelope_delete(&newhdr->env);
614 body_list_wipe(&newhdr->content);