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.
20 #include <lib-lib/mem.h>
21 #include <lib-lib/str.h>
22 #include <lib-lib/ascii.h>
23 #include <lib-lib/macros.h>
24 #include <lib-lib/file.h>
25 #include <lib-lib/mapping.h>
26 #include <lib-lib/debug.h>
28 #include <lib-mime/mime.h>
30 #include <lib-ui/enter.h>
31 #include <lib-ui/menu.h>
39 #include <lib-crypt/crypt.h>
41 #include <imap/imap.h>
42 #include <imap/mx_imap.h>
44 static struct mapping_t PostponeHelp[] = {
45 {N_("Exit"), OP_EXIT},
46 {N_("Del"), OP_DELETE},
47 {N_("Undel"), OP_UNDELETE},
48 {N_("Help"), OP_HELP},
54 static short PostCount = 0;
55 static CONTEXT *PostContext = NULL;
56 static short UpdateNumPostponed = 0;
58 /* Return the number of postponed messages.
59 * if force is 0, use a cached value if it is costly to get a fresh
60 * count (IMAP) - else check.
62 int mutt_num_postponed (int force)
67 static time_t LastModify = 0;
68 static char *OldPostponed = NULL;
70 if (UpdateNumPostponed) {
71 UpdateNumPostponed = 0;
75 if (Postponed != OldPostponed) {
76 OldPostponed = Postponed;
84 /* LastModify is useless for IMAP */
85 if (imap_is_magic (Postponed, NULL) == M_IMAP) {
89 newpc = imap_mailbox_check (Postponed, 0);
92 debug_print (2, ("%d postponed IMAP messages found.\n", PostCount));
95 debug_print (2, ("using old IMAP postponed count.\n"));
100 if (stat (Postponed, &st) == -1) {
106 if (S_ISDIR (st.st_mode)) {
107 /* if we have a maildir mailbox, we need to stat the "new" dir */
109 char buf[_POSIX_PATH_MAX];
111 snprintf (buf, sizeof (buf), "%s/new", Postponed);
112 if (access (buf, F_OK) == 0 && stat (buf, &st) == -1) {
119 if (LastModify < st.st_mtime) {
121 int optnews = option (OPTNEWS);
123 LastModify = st.st_mtime;
125 if (access (Postponed, R_OK | F_OK) != 0)
126 return (PostCount = 0);
129 unset_option (OPTNEWS);
131 if (mx_open_mailbox (Postponed, M_NOSORT | M_QUIET, &ctx) == NULL)
134 PostCount = ctx.msgcount;
135 mx_fastclose_mailbox (&ctx);
138 set_option (OPTNEWS);
145 void mutt_update_num_postponed (void)
147 UpdateNumPostponed = 1;
150 static void post_entry (char *s, ssize_t slen, MUTTMENU * menu, int entry)
152 CONTEXT *ctx = (CONTEXT *) menu->data;
154 _mutt_make_string (s, slen, NONULL (HdrFmt), ctx, ctx->hdrs[entry],
155 M_FORMAT_ARROWCURSOR);
158 static HEADER *select_msg (void)
161 int i, done = 0, r = -1;
162 char helpstr[SHORT_STRING];
165 menu = mutt_new_menu ();
166 menu->make_entry = post_entry;
167 menu->menu = MENU_POST;
168 menu->max = PostContext->msgcount;
169 menu->title = _("Postponed Messages");
170 menu->data = PostContext;
172 mutt_compile_help (helpstr, sizeof (helpstr), MENU_POST, PostponeHelp);
174 /* The postponed mailbox is setup to have sorting disabled, but the global
175 * Sort variable may indicate something different. Sorting has to be
176 * disabled while the postpone menu is being displayed. */
181 switch (i = mutt_menuLoop (menu)) {
184 mutt_set_flag (PostContext, PostContext->hdrs[menu->current], M_DELETE,
185 (i == OP_DELETE) ? 1 : 0);
186 PostCount = PostContext->msgcount - PostContext->deleted;
187 if (option (OPTRESOLVE) && menu->current < menu->max - 1) {
188 menu->oldcurrent = menu->current;
190 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,
235 int code = SENDPOSTPONED;
237 string_list_t *last = NULL;
245 if ((PostContext = mx_open_mailbox (Postponed, M_NOSORT, NULL)) == NULL) {
247 mutt_error _("No postponed messages.");
252 if (!PostContext->msgcount) {
254 mx_close_mailbox (PostContext, NULL);
255 p_delete(&PostContext);
256 mutt_error _("No postponed messages.");
261 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) {
266 mx_close_mailbox (PostContext, NULL);
267 p_delete(&PostContext);
271 if (mutt_prepare_template (NULL, PostContext, hdr, h, 0) < 0) {
272 mx_fastclose_mailbox (PostContext);
273 p_delete(&PostContext);
277 /* finished with this message, so delete it. */
278 mutt_set_flag (PostContext, h, M_DELETE, 1);
280 /* and consider it saved, so that it won't be moved to the trash folder */
281 mutt_set_flag (PostContext, h, M_APPENDED, 1);
283 /* update the count for the status display */
284 PostCount = PostContext->msgcount - PostContext->deleted;
286 /* avoid the "purge deleted messages" prompt */
287 opt_delete = quadoption (OPT_DELETE);
288 set_quadoption (OPT_DELETE, M_YES);
289 mx_close_mailbox (PostContext, NULL);
290 set_quadoption (OPT_DELETE, opt_delete);
292 p_delete(&PostContext);
294 for (tmp = hdr->env->userhdrs; tmp;) {
295 if (ascii_strncasecmp ("X-Mutt-References:", tmp->data, 18) == 0) {
297 /* if a mailbox is currently open, look to see if the orignal message
298 the user attempted to reply to is in this mailbox */
299 p = vskipspaces(tmp->data + 18);
301 ctx->id_hash = mutt_make_id_hash (ctx);
302 *cur = hash_find (ctx->id_hash, p);
305 /* Remove the X-Mutt-References: header field. */
308 last->next = tmp->next;
310 hdr->env->userhdrs = tmp->next;
312 string_list_wipe(&tmp);
317 else if (ascii_strncasecmp ("X-Mutt-Fcc:", tmp->data, 11) == 0) {
318 p = vskipspaces(tmp->data + 11);
319 m_strcpy(fcc, fcclen, p);
320 mutt_pretty_mailbox (fcc);
322 /* remove the X-Mutt-Fcc: header field */
325 last->next = tmp->next;
327 hdr->env->userhdrs = tmp->next;
329 string_list_wipe(&tmp);
332 else if ((m_strncmp("Pgp:", tmp->data, 4) == 0 /* this is generated
333 * by old mutt versions
335 || m_strncmp("X-Mutt-PGP:", tmp->data, 11) == 0)) {
336 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
337 hdr->security |= APPLICATION_PGP;
339 /* remove the pgp field */
342 last->next = tmp->next;
344 hdr->env->userhdrs = tmp->next;
346 string_list_wipe(&tmp);
349 else if (m_strncmp("X-Mutt-SMIME:", tmp->data, 13) == 0) {
350 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
351 hdr->security |= APPLICATION_SMIME;
353 /* remove the smime field */
356 last->next = tmp->next;
358 hdr->env->userhdrs = tmp->next;
360 string_list_wipe(&tmp);
365 else if (m_strncmp("X-Mutt-Mix:", tmp->data, 11) == 0) {
368 string_list_wipe(&hdr->chain);
370 t = strtok (tmp->data + 11, " \t\n");
372 hdr->chain = mutt_add_list (hdr->chain, t);
373 t = strtok (NULL, " \t\n");
378 last->next = tmp->next;
380 hdr->env->userhdrs = tmp->next;
382 string_list_wipe(&tmp);
397 int mutt_parse_crypt_hdr (char *p, int set_signas)
400 char pgp_sign_as[LONG_STRING] = "\0", *q;
401 char smime_cryptalg[LONG_STRING] = "\0";
403 for (p = vskipspaces(p); *p; p++) {
415 if (*(p + 1) == '<') {
417 *p && *p != '>' && q < pgp_sign_as + sizeof (pgp_sign_as) - 1;
421 mutt_error _("Illegal PGP header");
430 /* This used to be the micalg parameter.
432 * It's no longer needed, so we just skip the parameter in order
433 * to be able to recall old messages.
437 if (*(p + 1) == '<') {
438 for (p += 2; *p && *p != '>'; p++);
440 mutt_error _("Illegal PGP header");
453 if (*(p + 1) == '<') {
456 && q < smime_cryptalg + sizeof (smime_cryptalg) - 1;
460 mutt_error _("Illegal S/MIME header");
475 mutt_error _("Illegal PGP header");
481 /* the cryptalg field must not be empty */
483 m_strreplace(&SmimeCryptAlg, smime_cryptalg);
485 if (set_signas || *pgp_sign_as)
486 m_strreplace(&PgpSignAs, pgp_sign_as);
493 int mutt_prepare_template (FILE * fp, CONTEXT * ctx, HEADER * newhdr,
494 HEADER * hdr, short weed)
497 char file[_POSIX_PATH_MAX];
506 if (!fp && (msg = mx_open_message (ctx, hdr->msgno)) == NULL)
514 /* parse the message header and MIME structure */
516 fseeko (fp, hdr->offset, 0);
517 newhdr->offset = hdr->offset;
518 newhdr->env = mutt_read_rfc822_header (fp, newhdr, 1, weed);
519 newhdr->content->length = hdr->content->length;
520 mutt_parse_part (fp, newhdr->content);
522 p_delete(&newhdr->env->message_id);
523 p_delete(&newhdr->env->mail_followup_to); /* really? */
525 /* decrypt pgp/mime encoded messages */
527 if ((APPLICATION_PGP | APPLICATION_SMIME) & hdr->security
528 && mutt_is_multipart_encrypted (newhdr->content))
530 int ccap = (APPLICATION_PGP | APPLICATION_SMIME) & hdr->security;
531 newhdr->security |= ENCRYPT | ccap;
532 if (!crypt_valid_passphrase (ccap))
535 mutt_message _("Decrypting message...");
537 if (((ccap & APPLICATION_PGP)
538 && crypt_pgp_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
539 || ((ccap & APPLICATION_SMIME)
540 && crypt_smime_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
543 mx_close_message (&msg);
544 envelope_delete(&newhdr->env);
545 mutt_free_body (&newhdr->content);
546 mutt_error _("Decryption failed.");
551 mutt_free_body (&newhdr->content);
558 * remove a potential multipart/signed layer - useful when
562 if (mutt_is_multipart_signed (newhdr->content)) {
563 newhdr->security |= SIGN;
564 if (ascii_strcasecmp (mutt_get_parameter
565 ("protocol", newhdr->content->parameter),
566 "application/pgp-signature") == 0)
567 newhdr->security |= APPLICATION_PGP;
569 newhdr->security |= APPLICATION_SMIME;
571 /* destroy the signature */
572 mutt_free_body (&newhdr->content->parts->next);
573 newhdr->content = mutt_remove_multipart (newhdr->content);
578 * We don't need no primary multipart.
579 * Note: We _do_ preserve messages!
581 * XXX - we don't handle multipart/alternative in any
582 * smart way when sending messages. However, one may
583 * consider this a feature.
587 if (newhdr->content->type == TYPEMULTIPART)
588 newhdr->content = mutt_remove_multipart (newhdr->content);
592 /* create temporary files for all attachments */
593 for (b = newhdr->content; b; b = b->next) {
595 /* what follows is roughly a receive-mode variant of
596 * mutt_get_tmp_attachment () from muttlib.c
601 m_strcpy(file, sizeof(file), b->filename);
602 b->d_filename = m_strdup(b->filename);
605 /* avoid Content-Disposition: header with temporary filename */
609 /* set up state flags */
613 if (b->type == TYPETEXT) {
614 if (!ascii_strcasecmp
615 ("yes", mutt_get_parameter ("x-mutt-noconv", b->parameter)))
618 s.flags |= M_CHARCONV;
622 mutt_delete_parameter ("x-mutt-noconv", &b->parameter);
625 mutt_adv_mktemp (NULL, file, sizeof (file));
626 if ((s.fpout = safe_fopen (file, "w")) == NULL)
630 if (mutt_is_application_pgp (b) & (ENCRYPT | SIGN)) {
632 mutt_body_handler (b, &s);
634 newhdr->security |= mutt_is_application_pgp (newhdr->content);
637 m_strreplace(&b->subtype, "plain");
638 mutt_delete_parameter ("x-action", &b->parameter);
641 mutt_decode_attachment (b, &s);
643 if (safe_fclose (&s.fpout) != 0)
646 m_strreplace(&b->filename, file);
649 mutt_stamp_attachment (b);
651 mutt_free_body (&b->parts);
653 b->hdr->content = NULL; /* avoid dangling pointer */
656 /* Fix encryption flags. */
658 /* No inline if multipart. */
659 if ((newhdr->security & INLINE) && newhdr->content->next)
660 newhdr->security &= ~INLINE;
662 /* Theoretically, both could be set. Take the one the user wants to set by default. */
663 if ((newhdr->security & APPLICATION_PGP)
664 && (newhdr->security & APPLICATION_SMIME)) {
665 if (option (OPTSMIMEISDEFAULT))
666 newhdr->security &= ~APPLICATION_PGP;
668 newhdr->security &= ~APPLICATION_SMIME;
679 mx_close_message (&msg);
682 envelope_delete(&newhdr->env);
683 mutt_free_body (&newhdr->content);