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>
27 #include <lib-mime/mime.h>
29 #include <lib-ui/enter.h>
30 #include <lib-ui/menu.h>
38 #include <lib-crypt/crypt.h>
40 #include <imap/imap.h>
41 #include <imap/mx_imap.h>
43 static struct mapping_t PostponeHelp[] = {
44 {N_("Exit"), OP_EXIT},
45 {N_("Del"), OP_DELETE},
46 {N_("Undel"), OP_UNDELETE},
47 {N_("Help"), OP_HELP},
53 static short PostCount = 0;
54 static CONTEXT *PostContext = NULL;
55 static short UpdateNumPostponed = 0;
57 /* Return the number of postponed messages.
58 * if force is 0, use a cached value if it is costly to get a fresh
59 * count (IMAP) - else check.
61 int mutt_num_postponed (int force)
66 static time_t LastModify = 0;
67 static char *OldPostponed = NULL;
69 if (UpdateNumPostponed) {
70 UpdateNumPostponed = 0;
74 if (Postponed != OldPostponed) {
75 OldPostponed = Postponed;
83 /* LastModify is useless for IMAP */
84 if (imap_is_magic (Postponed, NULL) == M_IMAP) {
88 newpc = imap_mailbox_check (Postponed, 0);
96 if (stat (Postponed, &st) == -1) {
102 if (S_ISDIR (st.st_mode)) {
103 /* if we have a maildir mailbox, we need to stat the "new" dir */
105 char buf[_POSIX_PATH_MAX];
107 snprintf (buf, sizeof (buf), "%s/new", Postponed);
108 if (access (buf, F_OK) == 0 && stat (buf, &st) == -1) {
115 if (LastModify < st.st_mtime) {
117 int optnews = option (OPTNEWS);
119 LastModify = st.st_mtime;
121 if (access (Postponed, R_OK | F_OK) != 0)
122 return (PostCount = 0);
125 unset_option (OPTNEWS);
127 if (mx_open_mailbox (Postponed, M_NOSORT | M_QUIET, &ctx) == NULL)
130 PostCount = ctx.msgcount;
131 mx_fastclose_mailbox (&ctx);
134 set_option (OPTNEWS);
141 void mutt_update_num_postponed (void)
143 UpdateNumPostponed = 1;
146 static void post_entry (char *s, ssize_t slen, MUTTMENU * menu, int entry)
148 CONTEXT *ctx = (CONTEXT *) menu->data;
150 _mutt_make_string (s, slen, NONULL (HdrFmt), ctx, ctx->hdrs[entry],
151 M_FORMAT_ARROWCURSOR);
154 static HEADER *select_msg (void)
157 int i, done = 0, r = -1;
158 char helpstr[SHORT_STRING];
161 menu = mutt_new_menu ();
162 menu->make_entry = post_entry;
163 menu->menu = MENU_POST;
164 menu->max = PostContext->msgcount;
165 menu->title = _("Postponed Messages");
166 menu->data = PostContext;
168 mutt_compile_help (helpstr, sizeof (helpstr), MENU_POST, PostponeHelp);
170 /* The postponed mailbox is setup to have sorting disabled, but the global
171 * Sort variable may indicate something different. Sorting has to be
172 * disabled while the postpone menu is being displayed. */
177 switch (i = mutt_menuLoop (menu)) {
180 mutt_set_flag (PostContext, PostContext->hdrs[menu->current], M_DELETE,
181 (i == OP_DELETE) ? 1 : 0);
182 PostCount = PostContext->msgcount - PostContext->deleted;
183 if (option (OPTRESOLVE) && menu->current < menu->max - 1) {
184 menu->oldcurrent = menu->current;
186 if (menu->current >= menu->top + menu->pagelen) {
187 menu->top = menu->current;
188 menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
191 menu->redraw |= REDRAW_MOTION_RESYNCH;
194 menu->redraw = REDRAW_CURRENT;
197 case OP_GENERIC_SELECT_ENTRY:
209 mutt_menuDestroy (&menu);
210 return (r > -1 ? PostContext->hdrs[r] : NULL);
214 * ctx Context info, used when recalling a message to which
216 * hdr envelope/attachment info for recalled message
217 * cur if message was a reply, `cur' is set to the message which
218 * `hdr' is in reply to
219 * fcc fcc for the recalled message
220 * fcclen max length of fcc
223 * -1 error/no messages
225 * SENDREPLY recalled message is a reply
227 int mutt_get_postponed (CONTEXT * ctx, HEADER * hdr, HEADER ** cur, char *fcc,
231 int code = SENDPOSTPONED;
233 string_list_t *last = NULL;
241 if ((PostContext = mx_open_mailbox (Postponed, M_NOSORT, NULL)) == NULL) {
243 mutt_error _("No postponed messages.");
248 if (!PostContext->msgcount) {
250 mx_close_mailbox (PostContext, NULL);
251 p_delete(&PostContext);
252 mutt_error _("No postponed messages.");
257 if (PostContext->msgcount == 1) {
258 /* only one message, so just use that one. */
259 h = PostContext->hdrs[0];
261 else if ((h = select_msg ()) == NULL) {
262 mx_close_mailbox (PostContext, NULL);
263 p_delete(&PostContext);
267 if (mutt_prepare_template (NULL, PostContext, hdr, h, 0) < 0) {
268 mx_fastclose_mailbox (PostContext);
269 p_delete(&PostContext);
273 /* finished with this message, so delete it. */
274 mutt_set_flag (PostContext, h, M_DELETE, 1);
276 /* and consider it saved, so that it won't be moved to the trash folder */
277 mutt_set_flag (PostContext, h, M_APPENDED, 1);
279 /* update the count for the status display */
280 PostCount = PostContext->msgcount - PostContext->deleted;
282 /* avoid the "purge deleted messages" prompt */
283 opt_delete = quadoption (OPT_DELETE);
284 set_quadoption (OPT_DELETE, M_YES);
285 mx_close_mailbox (PostContext, NULL);
286 set_quadoption (OPT_DELETE, opt_delete);
288 p_delete(&PostContext);
290 for (tmp = hdr->env->userhdrs; tmp;) {
291 if (ascii_strncasecmp ("X-Mutt-References:", tmp->data, 18) == 0) {
293 /* if a mailbox is currently open, look to see if the orignal message
294 the user attempted to reply to is in this mailbox */
295 p = vskipspaces(tmp->data + 18);
297 ctx->id_hash = mutt_make_id_hash (ctx);
298 *cur = hash_find (ctx->id_hash, p);
301 /* Remove the X-Mutt-References: header field. */
304 last->next = tmp->next;
306 hdr->env->userhdrs = tmp->next;
308 string_list_wipe(&tmp);
313 else if (ascii_strncasecmp ("X-Mutt-Fcc:", tmp->data, 11) == 0) {
314 p = vskipspaces(tmp->data + 11);
315 m_strcpy(fcc, fcclen, p);
316 mutt_pretty_mailbox (fcc);
318 /* remove the X-Mutt-Fcc: header field */
321 last->next = tmp->next;
323 hdr->env->userhdrs = tmp->next;
325 string_list_wipe(&tmp);
328 else if ((m_strncmp("Pgp:", tmp->data, 4) == 0 /* this is generated
329 * by old mutt versions
331 || m_strncmp("X-Mutt-PGP:", tmp->data, 11) == 0)) {
332 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
333 hdr->security |= APPLICATION_PGP;
335 /* remove the pgp field */
338 last->next = tmp->next;
340 hdr->env->userhdrs = tmp->next;
342 string_list_wipe(&tmp);
345 else if (m_strncmp("X-Mutt-SMIME:", tmp->data, 13) == 0) {
346 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
347 hdr->security |= APPLICATION_SMIME;
349 /* remove the smime field */
352 last->next = tmp->next;
354 hdr->env->userhdrs = tmp->next;
356 string_list_wipe(&tmp);
361 else if (m_strncmp("X-Mutt-Mix:", tmp->data, 11) == 0) {
364 string_list_wipe(&hdr->chain);
366 t = strtok (tmp->data + 11, " \t\n");
368 hdr->chain = mutt_add_list (hdr->chain, t);
369 t = strtok (NULL, " \t\n");
374 last->next = tmp->next;
376 hdr->env->userhdrs = tmp->next;
378 string_list_wipe(&tmp);
393 int mutt_parse_crypt_hdr (char *p, int set_signas)
396 char pgp_sign_as[LONG_STRING] = "\0", *q;
397 char smime_cryptalg[LONG_STRING] = "\0";
399 for (p = vskipspaces(p); *p; p++) {
411 if (*(p + 1) == '<') {
413 *p && *p != '>' && q < pgp_sign_as + sizeof (pgp_sign_as) - 1;
417 mutt_error _("Illegal PGP header");
426 /* This used to be the micalg parameter.
428 * It's no longer needed, so we just skip the parameter in order
429 * to be able to recall old messages.
433 if (*(p + 1) == '<') {
434 for (p += 2; *p && *p != '>'; p++);
436 mutt_error _("Illegal PGP header");
449 if (*(p + 1) == '<') {
452 && q < smime_cryptalg + sizeof (smime_cryptalg) - 1;
456 mutt_error _("Illegal S/MIME header");
471 mutt_error _("Illegal PGP header");
477 /* the cryptalg field must not be empty */
479 m_strreplace(&SmimeCryptAlg, smime_cryptalg);
481 if (set_signas || *pgp_sign_as)
482 m_strreplace(&PgpSignAs, pgp_sign_as);
489 int mutt_prepare_template (FILE * fp, CONTEXT * ctx, HEADER * newhdr,
490 HEADER * hdr, short weed)
493 char file[_POSIX_PATH_MAX];
502 if (!fp && (msg = mx_open_message (ctx, hdr->msgno)) == NULL)
510 /* parse the message header and MIME structure */
512 fseeko (fp, hdr->offset, 0);
513 newhdr->offset = hdr->offset;
514 newhdr->env = mutt_read_rfc822_header (fp, newhdr, 1, weed);
515 newhdr->content->length = hdr->content->length;
516 mutt_parse_part (fp, newhdr->content);
518 p_delete(&newhdr->env->message_id);
519 p_delete(&newhdr->env->mail_followup_to); /* really? */
521 /* decrypt pgp/mime encoded messages */
523 if ((APPLICATION_PGP | APPLICATION_SMIME) & hdr->security
524 && mutt_is_multipart_encrypted (newhdr->content))
526 int ccap = (APPLICATION_PGP | APPLICATION_SMIME) & hdr->security;
527 newhdr->security |= ENCRYPT | ccap;
528 if (!crypt_valid_passphrase (ccap))
531 mutt_message _("Decrypting message...");
533 if (((ccap & APPLICATION_PGP)
534 && crypt_pgp_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
535 || ((ccap & APPLICATION_SMIME)
536 && crypt_smime_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
539 mx_close_message (&msg);
540 envelope_delete(&newhdr->env);
541 mutt_free_body (&newhdr->content);
542 mutt_error _("Decryption failed.");
547 mutt_free_body (&newhdr->content);
554 * remove a potential multipart/signed layer - useful when
558 if (mutt_is_multipart_signed (newhdr->content)) {
559 newhdr->security |= SIGN;
560 if (ascii_strcasecmp (mutt_get_parameter
561 ("protocol", newhdr->content->parameter),
562 "application/pgp-signature") == 0)
563 newhdr->security |= APPLICATION_PGP;
565 newhdr->security |= APPLICATION_SMIME;
567 /* destroy the signature */
568 mutt_free_body (&newhdr->content->parts->next);
569 newhdr->content = mutt_remove_multipart (newhdr->content);
574 * We don't need no primary multipart.
575 * Note: We _do_ preserve messages!
577 * XXX - we don't handle multipart/alternative in any
578 * smart way when sending messages. However, one may
579 * consider this a feature.
583 if (newhdr->content->type == TYPEMULTIPART)
584 newhdr->content = mutt_remove_multipart (newhdr->content);
588 /* create temporary files for all attachments */
589 for (b = newhdr->content; b; b = b->next) {
591 /* what follows is roughly a receive-mode variant of
592 * mutt_get_tmp_attachment () from muttlib.c
597 m_strcpy(file, sizeof(file), b->filename);
598 b->d_filename = m_strdup(b->filename);
601 /* avoid Content-Disposition: header with temporary filename */
605 /* set up state flags */
609 if (b->type == TYPETEXT) {
610 if (!ascii_strcasecmp
611 ("yes", mutt_get_parameter ("x-mutt-noconv", b->parameter)))
614 s.flags |= M_CHARCONV;
618 mutt_delete_parameter ("x-mutt-noconv", &b->parameter);
621 mutt_adv_mktemp (NULL, file, sizeof (file));
622 if ((s.fpout = safe_fopen (file, "w")) == NULL)
626 if (mutt_is_application_pgp (b) & (ENCRYPT | SIGN)) {
628 mutt_body_handler (b, &s);
630 newhdr->security |= mutt_is_application_pgp (newhdr->content);
633 m_strreplace(&b->subtype, "plain");
634 mutt_delete_parameter ("x-action", &b->parameter);
637 mutt_decode_attachment (b, &s);
639 if (safe_fclose (&s.fpout) != 0)
642 m_strreplace(&b->filename, file);
645 mutt_stamp_attachment (b);
647 mutt_free_body (&b->parts);
649 b->hdr->content = NULL; /* avoid dangling pointer */
652 /* Fix encryption flags. */
654 /* No inline if multipart. */
655 if ((newhdr->security & INLINE) && newhdr->content->next)
656 newhdr->security &= ~INLINE;
658 /* Theoretically, both could be set. Take the one the user wants to set by default. */
659 if ((newhdr->security & APPLICATION_PGP)
660 && (newhdr->security & APPLICATION_SMIME)) {
661 if (option (OPTSMIMEISDEFAULT))
662 newhdr->security &= ~APPLICATION_PGP;
664 newhdr->security &= ~APPLICATION_SMIME;
675 mx_close_message (&msg);
678 envelope_delete(&newhdr->env);
679 mutt_free_body (&newhdr->content);