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>
37 #include <lib-crypt/crypt.h>
39 #include <imap/imap.h>
40 #include <imap/mx_imap.h>
42 static struct mapping_t PostponeHelp[] = {
43 {N_("Exit"), OP_EXIT},
44 {N_("Del"), OP_DELETE},
45 {N_("Undel"), OP_UNDELETE},
46 {N_("Help"), OP_HELP},
52 static short PostCount = 0;
53 static CONTEXT *PostContext = NULL;
54 static short UpdateNumPostponed = 0;
56 /* Return the number of postponed messages.
57 * if force is 0, use a cached value if it is costly to get a fresh
58 * count (IMAP) - else check.
60 int mutt_num_postponed (int force)
65 static time_t LastModify = 0;
66 static char *OldPostponed = NULL;
68 if (UpdateNumPostponed) {
69 UpdateNumPostponed = 0;
73 if (Postponed != OldPostponed) {
74 OldPostponed = Postponed;
82 /* LastModify is useless for IMAP */
83 if (imap_is_magic (Postponed, NULL) == M_IMAP) {
87 newpc = imap_mailbox_check (Postponed, 0);
95 if (stat (Postponed, &st) == -1) {
101 if (S_ISDIR (st.st_mode)) {
102 /* if we have a maildir mailbox, we need to stat the "new" dir */
104 char buf[_POSIX_PATH_MAX];
106 snprintf (buf, sizeof (buf), "%s/new", Postponed);
107 if (access (buf, F_OK) == 0 && stat (buf, &st) == -1) {
114 if (LastModify < st.st_mtime) {
116 int optnews = option (OPTNEWS);
118 LastModify = st.st_mtime;
120 if (access (Postponed, R_OK | F_OK) != 0)
121 return (PostCount = 0);
124 unset_option (OPTNEWS);
126 if (mx_open_mailbox (Postponed, M_NOSORT | M_QUIET, &ctx) == NULL)
129 PostCount = ctx.msgcount;
130 mx_fastclose_mailbox (&ctx);
133 set_option (OPTNEWS);
140 void mutt_update_num_postponed (void)
142 UpdateNumPostponed = 1;
145 static void post_entry (char *s, ssize_t slen, MUTTMENU * menu, int entry)
147 CONTEXT *ctx = (CONTEXT *) menu->data;
149 _mutt_make_string (s, slen, NONULL (HdrFmt), ctx, ctx->hdrs[entry],
150 M_FORMAT_ARROWCURSOR);
153 static HEADER *select_msg (void)
156 int i, done = 0, r = -1;
157 char helpstr[SHORT_STRING];
160 menu = mutt_new_menu ();
161 menu->make_entry = post_entry;
162 menu->menu = MENU_POST;
163 menu->max = PostContext->msgcount;
164 menu->title = _("Postponed Messages");
165 menu->data = PostContext;
167 mutt_compile_help (helpstr, sizeof (helpstr), MENU_POST, PostponeHelp);
169 /* The postponed mailbox is setup to have sorting disabled, but the global
170 * Sort variable may indicate something different. Sorting has to be
171 * disabled while the postpone menu is being displayed. */
176 switch (i = mutt_menuLoop (menu)) {
179 mutt_set_flag (PostContext, PostContext->hdrs[menu->current], M_DELETE,
180 (i == OP_DELETE) ? 1 : 0);
181 PostCount = PostContext->msgcount - PostContext->deleted;
182 if (option (OPTRESOLVE) && menu->current < menu->max - 1) {
183 menu->oldcurrent = menu->current;
185 if (menu->current >= menu->top + menu->pagelen) {
186 menu->top = menu->current;
187 menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
190 menu->redraw |= REDRAW_MOTION_RESYNCH;
193 menu->redraw = REDRAW_CURRENT;
196 case OP_GENERIC_SELECT_ENTRY:
208 mutt_menuDestroy (&menu);
209 return (r > -1 ? PostContext->hdrs[r] : NULL);
213 * ctx Context info, used when recalling a message to which
215 * hdr envelope/attachment info for recalled message
216 * cur if message was a reply, `cur' is set to the message which
217 * `hdr' is in reply to
218 * fcc fcc for the recalled message
219 * fcclen max length of fcc
222 * -1 error/no messages
224 * SENDREPLY recalled message is a reply
226 int mutt_get_postponed (CONTEXT * ctx, HEADER * hdr, HEADER ** cur, char *fcc,
230 int code = SENDPOSTPONED;
232 string_list_t *last = NULL;
240 if ((PostContext = mx_open_mailbox (Postponed, M_NOSORT, NULL)) == NULL) {
242 mutt_error _("No postponed messages.");
247 if (!PostContext->msgcount) {
249 mx_close_mailbox (PostContext, NULL);
250 p_delete(&PostContext);
251 mutt_error _("No postponed messages.");
256 if (PostContext->msgcount == 1) {
257 /* only one message, so just use that one. */
258 h = PostContext->hdrs[0];
260 else if ((h = select_msg ()) == NULL) {
261 mx_close_mailbox (PostContext, NULL);
262 p_delete(&PostContext);
266 if (mutt_prepare_template (NULL, PostContext, hdr, h, 0) < 0) {
267 mx_fastclose_mailbox (PostContext);
268 p_delete(&PostContext);
272 /* finished with this message, so delete it. */
273 mutt_set_flag (PostContext, h, M_DELETE, 1);
275 /* and consider it saved, so that it won't be moved to the trash folder */
276 mutt_set_flag (PostContext, h, M_APPENDED, 1);
278 /* update the count for the status display */
279 PostCount = PostContext->msgcount - PostContext->deleted;
281 /* avoid the "purge deleted messages" prompt */
282 opt_delete = quadoption (OPT_DELETE);
283 set_quadoption (OPT_DELETE, M_YES);
284 mx_close_mailbox (PostContext, NULL);
285 set_quadoption (OPT_DELETE, opt_delete);
287 p_delete(&PostContext);
289 for (tmp = hdr->env->userhdrs; tmp;) {
290 if (ascii_strncasecmp ("X-Mutt-References:", tmp->data, 18) == 0) {
292 /* if a mailbox is currently open, look to see if the orignal message
293 the user attempted to reply to is in this mailbox */
294 p = vskipspaces(tmp->data + 18);
296 ctx->id_hash = mutt_make_id_hash (ctx);
297 *cur = hash_find (ctx->id_hash, p);
300 /* Remove the X-Mutt-References: header field. */
303 last->next = tmp->next;
305 hdr->env->userhdrs = tmp->next;
307 string_list_wipe(&tmp);
312 else if (ascii_strncasecmp ("X-Mutt-Fcc:", tmp->data, 11) == 0) {
313 p = vskipspaces(tmp->data + 11);
314 m_strcpy(fcc, fcclen, p);
315 mutt_pretty_mailbox (fcc);
317 /* remove the X-Mutt-Fcc: header field */
320 last->next = tmp->next;
322 hdr->env->userhdrs = tmp->next;
324 string_list_wipe(&tmp);
327 else if ((m_strncmp("Pgp:", tmp->data, 4) == 0 /* this is generated
328 * by old mutt versions
330 || m_strncmp("X-Mutt-PGP:", tmp->data, 11) == 0)) {
331 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
332 hdr->security |= APPLICATION_PGP;
334 /* remove the pgp field */
337 last->next = tmp->next;
339 hdr->env->userhdrs = tmp->next;
341 string_list_wipe(&tmp);
344 else if (m_strncmp("X-Mutt-SMIME:", tmp->data, 13) == 0) {
345 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
346 hdr->security |= APPLICATION_SMIME;
348 /* remove the smime field */
351 last->next = tmp->next;
353 hdr->env->userhdrs = tmp->next;
355 string_list_wipe(&tmp);
360 else if (m_strncmp("X-Mutt-Mix:", tmp->data, 11) == 0) {
363 string_list_wipe(&hdr->chain);
365 t = strtok (tmp->data + 11, " \t\n");
367 hdr->chain = mutt_add_list (hdr->chain, t);
368 t = strtok (NULL, " \t\n");
373 last->next = tmp->next;
375 hdr->env->userhdrs = tmp->next;
377 string_list_wipe(&tmp);
392 int mutt_parse_crypt_hdr (char *p, int set_signas)
395 char pgp_sign_as[LONG_STRING] = "\0", *q;
396 char smime_cryptalg[LONG_STRING] = "\0";
398 for (p = vskipspaces(p); *p; p++) {
410 if (*(p + 1) == '<') {
412 *p && *p != '>' && q < pgp_sign_as + sizeof (pgp_sign_as) - 1;
416 mutt_error _("Illegal PGP header");
425 /* This used to be the micalg parameter.
427 * It's no longer needed, so we just skip the parameter in order
428 * to be able to recall old messages.
432 if (*(p + 1) == '<') {
433 for (p += 2; *p && *p != '>'; p++);
435 mutt_error _("Illegal PGP header");
448 if (*(p + 1) == '<') {
451 && q < smime_cryptalg + sizeof (smime_cryptalg) - 1;
455 mutt_error _("Illegal S/MIME header");
470 mutt_error _("Illegal PGP header");
476 /* the cryptalg field must not be empty */
478 m_strreplace(&SmimeCryptAlg, smime_cryptalg);
480 if (set_signas || *pgp_sign_as)
481 m_strreplace(&PgpSignAs, pgp_sign_as);
488 int mutt_prepare_template (FILE * fp, CONTEXT * ctx, HEADER * newhdr,
489 HEADER * hdr, short weed)
492 char file[_POSIX_PATH_MAX];
501 if (!fp && (msg = mx_open_message (ctx, hdr->msgno)) == NULL)
509 /* parse the message header and MIME structure */
511 fseeko (fp, hdr->offset, 0);
512 newhdr->offset = hdr->offset;
513 newhdr->env = mutt_read_rfc822_header (fp, newhdr, 1, weed);
514 newhdr->content->length = hdr->content->length;
515 mutt_parse_part (fp, newhdr->content);
517 p_delete(&newhdr->env->message_id);
518 p_delete(&newhdr->env->mail_followup_to); /* really? */
520 /* decrypt pgp/mime encoded messages */
522 if ((APPLICATION_PGP | APPLICATION_SMIME) & hdr->security
523 && mutt_is_multipart_encrypted (newhdr->content))
525 int ccap = (APPLICATION_PGP | APPLICATION_SMIME) & hdr->security;
526 newhdr->security |= ENCRYPT | ccap;
527 if (!crypt_valid_passphrase (ccap))
530 mutt_message _("Decrypting message...");
532 if (((ccap & APPLICATION_PGP)
533 && crypt_pgp_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
534 || ((ccap & APPLICATION_SMIME)
535 && crypt_smime_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
538 mx_close_message (&msg);
539 envelope_delete(&newhdr->env);
540 mutt_free_body (&newhdr->content);
541 mutt_error _("Decryption failed.");
546 mutt_free_body (&newhdr->content);
553 * remove a potential multipart/signed layer - useful when
557 if (mutt_is_multipart_signed (newhdr->content)) {
558 newhdr->security |= SIGN;
559 if (ascii_strcasecmp (mutt_get_parameter
560 ("protocol", newhdr->content->parameter),
561 "application/pgp-signature") == 0)
562 newhdr->security |= APPLICATION_PGP;
564 newhdr->security |= APPLICATION_SMIME;
566 /* destroy the signature */
567 mutt_free_body (&newhdr->content->parts->next);
568 newhdr->content = mutt_remove_multipart (newhdr->content);
573 * We don't need no primary multipart.
574 * Note: We _do_ preserve messages!
576 * XXX - we don't handle multipart/alternative in any
577 * smart way when sending messages. However, one may
578 * consider this a feature.
582 if (newhdr->content->type == TYPEMULTIPART)
583 newhdr->content = mutt_remove_multipart (newhdr->content);
587 /* create temporary files for all attachments */
588 for (b = newhdr->content; b; b = b->next) {
590 /* what follows is roughly a receive-mode variant of
591 * mutt_get_tmp_attachment () from muttlib.c
596 m_strcpy(file, sizeof(file), b->filename);
597 b->d_filename = m_strdup(b->filename);
600 /* avoid Content-Disposition: header with temporary filename */
604 /* set up state flags */
608 if (b->type == TYPETEXT) {
609 if (!ascii_strcasecmp
610 ("yes", mutt_get_parameter ("x-mutt-noconv", b->parameter)))
613 s.flags |= M_CHARCONV;
617 mutt_delete_parameter ("x-mutt-noconv", &b->parameter);
620 mutt_adv_mktemp (NULL, file, sizeof (file));
621 if ((s.fpout = safe_fopen (file, "w")) == NULL)
625 if (mutt_is_application_pgp (b) & (ENCRYPT | SIGN)) {
627 mutt_body_handler (b, &s);
629 newhdr->security |= mutt_is_application_pgp (newhdr->content);
632 m_strreplace(&b->subtype, "plain");
633 mutt_delete_parameter ("x-action", &b->parameter);
636 mutt_decode_attachment (b, &s);
638 if (safe_fclose (&s.fpout) != 0)
641 m_strreplace(&b->filename, file);
644 mutt_stamp_attachment (b);
646 mutt_free_body (&b->parts);
648 b->hdr->content = NULL; /* avoid dangling pointer */
651 /* Fix encryption flags. */
653 /* No inline if multipart. */
654 if ((newhdr->security & INLINE) && newhdr->content->next)
655 newhdr->security &= ~INLINE;
657 /* Theoretically, both could be set. Take the one the user wants to set by default. */
658 if ((newhdr->security & APPLICATION_PGP)
659 && (newhdr->security & APPLICATION_SMIME)) {
660 if (option (OPTSMIMEISDEFAULT))
661 newhdr->security &= ~APPLICATION_PGP;
663 newhdr->security &= ~APPLICATION_SMIME;
674 mx_close_message (&msg);
677 envelope_delete(&newhdr->env);
678 mutt_free_body (&newhdr->content);