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/curses.h>
19 #include <lib-sys/unix.h>
26 #include <lib-crypt/crypt.h>
28 #include <imap/imap.h>
29 #include <imap/mx_imap.h>
31 static struct mapping_t PostponeHelp[] = {
32 {N_("Exit"), OP_EXIT},
33 {N_("Del"), OP_DELETE},
34 {N_("Undel"), OP_UNDELETE},
35 {N_("Help"), OP_HELP},
41 static short PostCount = 0;
42 static CONTEXT *PostContext = NULL;
43 static short UpdateNumPostponed = 0;
45 /* Return the number of postponed messages.
46 * if force is 0, use a cached value if it is costly to get a fresh
47 * count (IMAP) - else check.
49 int mutt_num_postponed (int force)
54 static time_t LastModify = 0;
55 static char *OldPostponed = NULL;
57 if (UpdateNumPostponed) {
58 UpdateNumPostponed = 0;
62 if (Postponed != OldPostponed) {
63 OldPostponed = Postponed;
71 /* LastModify is useless for IMAP */
72 if (imap_is_magic (Postponed, NULL) == M_IMAP) {
76 newpc = imap_mailbox_check (Postponed, 0);
84 if (stat (Postponed, &st) == -1) {
90 if (S_ISDIR (st.st_mode)) {
91 /* if we have a maildir mailbox, we need to stat the "new" dir */
93 char buf[_POSIX_PATH_MAX];
95 snprintf (buf, sizeof (buf), "%s/new", Postponed);
96 if (access (buf, F_OK) == 0 && stat (buf, &st) == -1) {
103 if (LastModify < st.st_mtime) {
105 int optnews = option (OPTNEWS);
107 LastModify = st.st_mtime;
109 if (access (Postponed, R_OK | F_OK) != 0)
110 return (PostCount = 0);
113 unset_option (OPTNEWS);
115 if (mx_open_mailbox (Postponed, M_NOSORT | M_QUIET, &ctx) == NULL)
118 PostCount = ctx.msgcount;
119 mx_fastclose_mailbox (&ctx);
122 set_option (OPTNEWS);
129 void mutt_update_num_postponed (void)
131 UpdateNumPostponed = 1;
134 static void post_entry (char *s, ssize_t slen, MUTTMENU * menu, int entry)
136 CONTEXT *ctx = (CONTEXT *) menu->data;
138 _mutt_make_string (s, slen, NONULL (HdrFmt), ctx, ctx->hdrs[entry],
139 M_FORMAT_ARROWCURSOR);
142 static HEADER *select_msg (void)
145 int i, done = 0, r = -1;
146 char helpstr[SHORT_STRING];
149 menu = mutt_new_menu ();
150 menu->make_entry = post_entry;
151 menu->menu = MENU_POST;
152 menu->max = PostContext->msgcount;
153 menu->title = _("Postponed Messages");
154 menu->data = PostContext;
156 mutt_compile_help (helpstr, sizeof (helpstr), MENU_POST, PostponeHelp);
158 /* The postponed mailbox is setup to have sorting disabled, but the global
159 * Sort variable may indicate something different. Sorting has to be
160 * disabled while the postpone menu is being displayed. */
165 switch (i = mutt_menuLoop (menu)) {
168 mutt_set_flag (PostContext, PostContext->hdrs[menu->current], M_DELETE,
169 (i == OP_DELETE) ? 1 : 0);
170 PostCount = PostContext->msgcount - PostContext->deleted;
171 if (option (OPTRESOLVE) && menu->current < menu->max - 1) {
172 menu->oldcurrent = menu->current;
174 if (menu->current >= menu->top + menu->pagelen) {
175 menu->top = menu->current;
176 menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
179 menu->redraw |= REDRAW_MOTION_RESYNCH;
182 menu->redraw = REDRAW_CURRENT;
185 case OP_GENERIC_SELECT_ENTRY:
197 mutt_menuDestroy (&menu);
198 return (r > -1 ? PostContext->hdrs[r] : NULL);
202 * ctx Context info, used when recalling a message to which
204 * hdr envelope/attachment info for recalled message
205 * cur if message was a reply, `cur' is set to the message which
206 * `hdr' is in reply to
207 * fcc fcc for the recalled message
208 * fcclen max length of fcc
211 * -1 error/no messages
213 * SENDREPLY recalled message is a reply
215 int mutt_get_postponed (CONTEXT * ctx, HEADER * hdr, HEADER ** cur, char *fcc,
219 int code = SENDPOSTPONED;
221 string_list_t *last = NULL;
229 if ((PostContext = mx_open_mailbox (Postponed, M_NOSORT, NULL)) == NULL) {
231 mutt_error _("No postponed messages.");
236 if (!PostContext->msgcount) {
238 mx_close_mailbox (PostContext, NULL);
239 p_delete(&PostContext);
240 mutt_error _("No postponed messages.");
245 if (PostContext->msgcount == 1) {
246 /* only one message, so just use that one. */
247 h = PostContext->hdrs[0];
249 else if ((h = select_msg ()) == NULL) {
250 mx_close_mailbox (PostContext, NULL);
251 p_delete(&PostContext);
255 if (mutt_prepare_template (NULL, PostContext, hdr, h, 0) < 0) {
256 mx_fastclose_mailbox (PostContext);
257 p_delete(&PostContext);
261 /* finished with this message, so delete it. */
262 mutt_set_flag (PostContext, h, M_DELETE, 1);
264 /* and consider it saved, so that it won't be moved to the trash folder */
265 mutt_set_flag (PostContext, h, M_APPENDED, 1);
267 /* update the count for the status display */
268 PostCount = PostContext->msgcount - PostContext->deleted;
270 /* avoid the "purge deleted messages" prompt */
271 opt_delete = quadoption (OPT_DELETE);
272 set_quadoption (OPT_DELETE, M_YES);
273 mx_close_mailbox (PostContext, NULL);
274 set_quadoption (OPT_DELETE, opt_delete);
276 p_delete(&PostContext);
278 for (tmp = hdr->env->userhdrs; tmp;) {
279 if (ascii_strncasecmp ("X-Mutt-References:", tmp->data, 18) == 0) {
281 /* if a mailbox is currently open, look to see if the orignal message
282 the user attempted to reply to is in this mailbox */
283 p = vskipspaces(tmp->data + 18);
285 ctx->id_hash = mutt_make_id_hash (ctx);
286 *cur = hash_find (ctx->id_hash, p);
289 /* Remove the X-Mutt-References: header field. */
292 last->next = tmp->next;
294 hdr->env->userhdrs = tmp->next;
296 string_list_wipe(&tmp);
301 else if (ascii_strncasecmp ("X-Mutt-Fcc:", tmp->data, 11) == 0) {
302 p = vskipspaces(tmp->data + 11);
303 m_strcpy(fcc, fcclen, p);
304 mutt_pretty_mailbox (fcc);
306 /* remove the X-Mutt-Fcc: header field */
309 last->next = tmp->next;
311 hdr->env->userhdrs = tmp->next;
313 string_list_wipe(&tmp);
316 else if ((m_strncmp("Pgp:", tmp->data, 4) == 0 /* this is generated
317 * by old mutt versions
319 || m_strncmp("X-Mutt-PGP:", tmp->data, 11) == 0)) {
320 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
321 hdr->security |= APPLICATION_PGP;
323 /* remove the pgp field */
326 last->next = tmp->next;
328 hdr->env->userhdrs = tmp->next;
330 string_list_wipe(&tmp);
333 else if (m_strncmp("X-Mutt-SMIME:", tmp->data, 13) == 0) {
334 hdr->security = mutt_parse_crypt_hdr (strchr (tmp->data, ':') + 1, 1);
335 hdr->security |= APPLICATION_SMIME;
337 /* remove the smime field */
340 last->next = tmp->next;
342 hdr->env->userhdrs = tmp->next;
344 string_list_wipe(&tmp);
349 else if (m_strncmp("X-Mutt-Mix:", tmp->data, 11) == 0) {
352 string_list_wipe(&hdr->chain);
354 t = strtok (tmp->data + 11, " \t\n");
356 hdr->chain = mutt_add_list (hdr->chain, t);
357 t = strtok (NULL, " \t\n");
362 last->next = tmp->next;
364 hdr->env->userhdrs = tmp->next;
366 string_list_wipe(&tmp);
381 int mutt_parse_crypt_hdr (char *p, int set_signas)
384 char pgp_sign_as[LONG_STRING] = "\0", *q;
385 char smime_cryptalg[LONG_STRING] = "\0";
387 for (p = vskipspaces(p); *p; p++) {
399 if (*(p + 1) == '<') {
401 *p && *p != '>' && q < pgp_sign_as + sizeof (pgp_sign_as) - 1;
405 mutt_error _("Illegal PGP header");
414 /* This used to be the micalg parameter.
416 * It's no longer needed, so we just skip the parameter in order
417 * to be able to recall old messages.
421 if (*(p + 1) == '<') {
422 for (p += 2; *p && *p != '>'; p++);
424 mutt_error _("Illegal PGP header");
437 if (*(p + 1) == '<') {
440 && q < smime_cryptalg + sizeof (smime_cryptalg) - 1;
444 mutt_error _("Illegal S/MIME header");
459 mutt_error _("Illegal PGP header");
465 /* the cryptalg field must not be empty */
467 m_strreplace(&SmimeCryptAlg, smime_cryptalg);
469 if (set_signas || *pgp_sign_as)
470 m_strreplace(&PgpSignAs, pgp_sign_as);
477 int mutt_prepare_template (FILE * fp, CONTEXT * ctx, HEADER * newhdr,
478 HEADER * hdr, short weed)
481 char file[_POSIX_PATH_MAX];
490 if (!fp && (msg = mx_open_message (ctx, hdr->msgno)) == NULL)
498 /* parse the message header and MIME structure */
500 fseeko (fp, hdr->offset, 0);
501 newhdr->offset = hdr->offset;
502 newhdr->env = mutt_read_rfc822_header (fp, newhdr, 1, weed);
503 newhdr->content->length = hdr->content->length;
504 mutt_parse_part (fp, newhdr->content);
506 p_delete(&newhdr->env->message_id);
507 p_delete(&newhdr->env->mail_followup_to); /* really? */
509 /* decrypt pgp/mime encoded messages */
511 if ((APPLICATION_PGP | APPLICATION_SMIME) & hdr->security
512 && mutt_is_multipart_encrypted (newhdr->content))
514 int ccap = (APPLICATION_PGP | APPLICATION_SMIME) & hdr->security;
515 newhdr->security |= ENCRYPT | ccap;
516 if (!crypt_valid_passphrase (ccap))
519 mutt_message _("Decrypting message...");
521 if (((ccap & APPLICATION_PGP)
522 && crypt_pgp_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
523 || ((ccap & APPLICATION_SMIME)
524 && crypt_smime_decrypt_mime (fp, &bfp, newhdr->content, &b) == -1)
527 mx_close_message (&msg);
528 envelope_delete(&newhdr->env);
529 body_list_wipe(&newhdr->content);
530 mutt_error _("Decryption failed.");
535 body_list_wipe(&newhdr->content);
542 * remove a potential multipart/signed layer - useful when
546 if (mutt_is_multipart_signed (newhdr->content)) {
547 newhdr->security |= SIGN;
548 if (ascii_strcasecmp(parameter_getval(newhdr->content->parameter, "protocol"),
549 "application/pgp-signature") == 0)
550 newhdr->security |= APPLICATION_PGP;
552 newhdr->security |= APPLICATION_SMIME;
554 /* destroy the signature */
555 body_list_wipe(&newhdr->content->parts->next);
556 newhdr->content = mutt_remove_multipart (newhdr->content);
561 * We don't need no primary multipart.
562 * Note: We _do_ preserve messages!
564 * XXX - we don't handle multipart/alternative in any
565 * smart way when sending messages. However, one may
566 * consider this a feature.
570 if (newhdr->content->type == TYPEMULTIPART)
571 newhdr->content = mutt_remove_multipart (newhdr->content);
575 /* create temporary files for all attachments */
576 for (b = newhdr->content; b; b = b->next) {
578 /* what follows is roughly a receive-mode variant of
579 * mutt_get_tmp_attachment () from muttlib.c
584 m_strcpy(file, sizeof(file), b->filename);
585 b->d_filename = m_strdup(b->filename);
588 /* avoid Content-Disposition: header with temporary filename */
592 /* set up state flags */
596 if (b->type == TYPETEXT) {
597 if (!ascii_strcasecmp
598 ("yes", parameter_getval(b->parameter, "x-mutt-noconv")))
601 s.flags |= M_CHARCONV;
605 parameter_delval(&b->parameter, "x-mutt-noconv");
608 mutt_adv_mktemp (NULL, file, sizeof (file));
609 if ((s.fpout = safe_fopen (file, "w")) == NULL)
613 if (mutt_is_application_pgp (b) & (ENCRYPT | SIGN)) {
615 mutt_body_handler (b, &s);
617 newhdr->security |= mutt_is_application_pgp (newhdr->content);
620 m_strreplace(&b->subtype, "plain");
621 parameter_delval(&b->parameter, "x-action");
624 mutt_decode_attachment (b, &s);
626 if (safe_fclose (&s.fpout) != 0)
629 m_strreplace(&b->filename, file);
632 mutt_stamp_attachment (b);
634 body_list_wipe(&b->parts);
636 b->hdr->content = NULL; /* avoid dangling pointer */
639 /* Fix encryption flags. */
641 /* No inline if multipart. */
642 if ((newhdr->security & INLINE) && newhdr->content->next)
643 newhdr->security &= ~INLINE;
645 /* Theoretically, both could be set. Take the one the user wants to set by default. */
646 if ((newhdr->security & APPLICATION_PGP)
647 && (newhdr->security & APPLICATION_SMIME)) {
648 if (option (OPTSMIMEISDEFAULT))
649 newhdr->security &= ~APPLICATION_PGP;
651 newhdr->security &= ~APPLICATION_SMIME;
662 mx_close_message (&msg);
665 envelope_delete(&newhdr->env);
666 body_list_wipe(&newhdr->content);