NeoMutt  2025-12-11-58-g09398d
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
message.c
Go to the documentation of this file.
1
29
35
36#include "config.h"
37#include <limits.h>
38#include <stdbool.h>
39#include <stdint.h>
40#include <stdio.h>
41#include <string.h>
42#include <unistd.h>
43#include "private.h"
44#include "mutt/lib.h"
45#include "config/lib.h"
46#include "email/lib.h"
47#include "core/lib.h"
48#include "conn/lib.h"
49#include "gui/lib.h"
50#include "mutt.h"
51#include "message.h"
52#include "lib.h"
53#include "bcache/lib.h"
54#include "key/lib.h"
55#include "progress/lib.h"
56#include "question/lib.h"
57#include "adata.h"
58#include "edata.h"
59#include "external.h"
60#include "mdata.h"
61#include "msg_set.h"
62#include "msn.h"
63#include "mutt_logging.h"
64#include "mx.h"
65#include "protos.h"
66#ifdef ENABLE_NLS
67#include <libintl.h>
68#endif
69#ifdef USE_HCACHE
70#include "hcache/lib.h"
71#endif
72
73struct BodyCache;
74
81static struct BodyCache *imap_bcache_open(struct Mailbox *m)
82{
85
86 if (!adata || (adata->mailbox != m) || !mdata)
87 return NULL;
88
89 if (mdata->bcache)
90 return mdata->bcache;
91
92 struct Buffer *mailbox = buf_pool_get();
93 imap_cachepath(adata->delim, mdata->name, mailbox);
94
95 struct BodyCache *bc = mutt_bcache_open(&adata->conn->account, buf_string(mailbox));
96 buf_pool_release(&mailbox);
97
98 return bc;
99}
100
108static FILE *msg_cache_get(struct Mailbox *m, struct Email *e)
109{
111 struct ImapMboxData *mdata = imap_mdata_get(m);
112
113 if (!e || !adata || (adata->mailbox != m) || !mdata)
114 return NULL;
115
116 mdata->bcache = imap_bcache_open(m);
117 char id[64] = { 0 };
118 snprintf(id, sizeof(id), "%u-%u", mdata->uidvalidity, imap_edata_get(e)->uid);
119 return mutt_bcache_get(mdata->bcache, id);
120}
121
129static FILE *msg_cache_put(struct Mailbox *m, struct Email *e)
130{
132 struct ImapMboxData *mdata = imap_mdata_get(m);
133
134 if (!e || !adata || (adata->mailbox != m) || !mdata)
135 return NULL;
136
137 mdata->bcache = imap_bcache_open(m);
138 char id[64] = { 0 };
139 snprintf(id, sizeof(id), "%u-%u", mdata->uidvalidity, imap_edata_get(e)->uid);
140 return mutt_bcache_put(mdata->bcache, id);
141}
142
150static int msg_cache_commit(struct Mailbox *m, struct Email *e)
151{
153 struct ImapMboxData *mdata = imap_mdata_get(m);
154
155 if (!e || !adata || (adata->mailbox != m) || !mdata)
156 return -1;
157
158 mdata->bcache = imap_bcache_open(m);
159 char id[64] = { 0 };
160 snprintf(id, sizeof(id), "%u-%u", mdata->uidvalidity, imap_edata_get(e)->uid);
161
162 return mutt_bcache_commit(mdata->bcache, id);
163}
164
169static int imap_bcache_delete(const char *id, struct BodyCache *bcache, void *data)
170{
171 uint32_t uv = 0;
172 unsigned int uid = 0;
173 struct ImapMboxData *mdata = data;
174 if (!mdata)
175 return 0;
176
177 if (sscanf(id, "%u-%u", &uv, &uid) != 2)
178 return 0;
179
180 /* bad UID */
181 if ((uv != mdata->uidvalidity) || !mutt_hash_int_find(mdata->uid_hash, uid))
183
184 return 0;
185}
186
194static char *msg_parse_flags(struct ImapHeader *h, char *s)
195{
196 struct ImapEmailData *edata = h->edata;
197
198 /* sanity-check string */
199 size_t plen = mutt_istr_startswith(s, "FLAGS");
200 if (plen == 0)
201 {
202 mutt_debug(LL_DEBUG1, "not a FLAGS response: %s\n", s);
203 return NULL;
204 }
205 s += plen;
206 SKIPWS(s);
207 if (*s != '(')
208 {
209 mutt_debug(LL_DEBUG1, "bogus FLAGS response: %s\n", s);
210 return NULL;
211 }
212 s++;
213
214 FREE(&edata->flags_system);
215 FREE(&edata->flags_remote);
216
217 edata->deleted = false;
218 edata->flagged = false;
219 edata->replied = false;
220 edata->read = false;
221 edata->old = false;
222
223 /* start parsing */
224 while (*s && (*s != ')'))
225 {
226 if ((plen = mutt_istr_startswith(s, "\\deleted")))
227 {
228 s += plen;
229 edata->deleted = true;
230 }
231 else if ((plen = mutt_istr_startswith(s, "\\flagged")))
232 {
233 s += plen;
234 edata->flagged = true;
235 }
236 else if ((plen = mutt_istr_startswith(s, "\\answered")))
237 {
238 s += plen;
239 edata->replied = true;
240 }
241 else if ((plen = mutt_istr_startswith(s, "\\seen")))
242 {
243 s += plen;
244 edata->read = true;
245 }
246 else if ((plen = mutt_istr_startswith(s, "\\recent")))
247 {
248 s += plen;
249 }
250 else if ((plen = mutt_istr_startswith(s, "old")))
251 {
252 s += plen;
253 edata->old = cs_subset_bool(NeoMutt->sub, "mark_old");
254 }
255 else
256 {
257 char ctmp;
258 char *flag_word = s;
259 bool is_system_keyword = mutt_istr_startswith(s, "\\");
260
261 while (*s && !mutt_isspace(*s) && (*s != ')'))
262 s++;
263
264 ctmp = *s;
265 *s = '\0';
266
267 struct Buffer *buf = buf_pool_get();
268 if (is_system_keyword)
269 {
270 /* store other system flags as well (mainly \\Draft) */
271 buf_addstr(buf, edata->flags_system);
272 buf_join_str(buf, flag_word, ' ');
273 edata->flags_system = buf_strdup(buf);
274 }
275 else
276 {
277 /* store custom flags as well */
278 buf_addstr(buf, edata->flags_remote);
279 buf_join_str(buf, flag_word, ' ');
280 edata->flags_remote = buf_strdup(buf);
281 }
282 buf_pool_release(&buf);
283
284 *s = ctmp;
285 }
286 SKIPWS(s);
287 }
288
289 /* wrap up, or note bad flags response */
290 if (*s == ')')
291 {
292 s++;
293 }
294 else
295 {
296 mutt_debug(LL_DEBUG1, "Unterminated FLAGS response: %s\n", s);
297 return NULL;
298 }
299
300 return s;
301}
302
311static int msg_parse_fetch(struct ImapHeader *h, char *s)
312{
313 if (!s)
314 return -1;
315
316 char tmp[128] = { 0 };
317 char *ptmp = NULL;
318 size_t plen = 0;
319
320 while (*s)
321 {
322 SKIPWS(s);
323
324 if (mutt_istr_startswith(s, "FLAGS"))
325 {
326 s = msg_parse_flags(h, s);
327 if (!s)
328 return -1;
329 }
330 else if ((plen = mutt_istr_startswith(s, "UID")))
331 {
332 s += plen;
333 SKIPWS(s);
334 if (!mutt_str_atoui(s, &h->edata->uid))
335 return -1;
336
337 s = imap_next_word(s);
338 }
339 else if ((plen = mutt_istr_startswith(s, "INTERNALDATE")))
340 {
341 s += plen;
342 SKIPWS(s);
343 if (*s != '\"')
344 {
345 mutt_debug(LL_DEBUG1, "bogus INTERNALDATE entry: %s\n", s);
346 return -1;
347 }
348 s++;
349 ptmp = tmp;
350 while (*s && (*s != '\"') && (ptmp != (tmp + sizeof(tmp) - 1)))
351 *ptmp++ = *s++;
352 if (*s != '\"')
353 return -1;
354 s++; /* skip past the trailing " */
355 *ptmp = '\0';
357 }
358 else if ((plen = mutt_istr_startswith(s, "RFC822.SIZE")))
359 {
360 s += plen;
361 SKIPWS(s);
362 ptmp = tmp;
363 while (mutt_isdigit(*s) && (ptmp != (tmp + sizeof(tmp) - 1)))
364 *ptmp++ = *s++;
365 *ptmp = '\0';
366 if (!mutt_str_atol(tmp, &h->content_length))
367 return -1;
368 }
369 else if (mutt_istr_startswith(s, "BODY") || mutt_istr_startswith(s, "RFC822.HEADER"))
370 {
371 /* handle above, in msg_fetch_header */
372 return -2;
373 }
374 else if ((plen = mutt_istr_startswith(s, "MODSEQ")))
375 {
376 s += plen;
377 SKIPWS(s);
378 if (*s != '(')
379 {
380 mutt_debug(LL_DEBUG1, "bogus MODSEQ response: %s\n", s);
381 return -1;
382 }
383 s++;
384 while (*s && (*s != ')'))
385 s++;
386 if (*s == ')')
387 {
388 s++;
389 }
390 else
391 {
392 mutt_debug(LL_DEBUG1, "Unterminated MODSEQ response: %s\n", s);
393 return -1;
394 }
395 }
396 else if (*s == ')')
397 {
398 s++; /* end of request */
399 }
400 else if (*s)
401 {
402 /* got something i don't understand */
403 imap_error("msg_parse_fetch", s);
404 return -1;
405 }
406 }
407
408 return 0;
409}
410
423static int msg_fetch_header(struct Mailbox *m, struct ImapHeader *ih, char *buf, FILE *fp)
424{
425 int rc = -1; /* default now is that string isn't FETCH response */
426
428 if (!adata)
429 return rc;
430
431 if (buf[0] != '*')
432 return rc;
433
434 /* skip to message number */
436 if (!mutt_str_atoui(buf, &ih->edata->msn))
437 return rc;
438
439 /* find FETCH tag */
441 if (!mutt_istr_startswith(buf, "FETCH"))
442 return rc;
443
444 rc = -2; /* we've got a FETCH response, for better or worse */
445 buf = strchr(buf, '(');
446 if (!buf)
447 return rc;
448 buf++;
449
450 /* FIXME: current implementation - call msg_parse_fetch - if it returns -2,
451 * read header lines and call it again. Silly. */
452 int parse_rc = msg_parse_fetch(ih, buf);
453 if (parse_rc == 0)
454 return 0;
455 if ((parse_rc != -2) || !fp)
456 return rc;
457
458 unsigned int bytes = 0;
459 if (imap_get_literal_count(buf, &bytes) == 0)
460 {
461 imap_read_literal(fp, adata, bytes, NULL);
462
463 /* we may have other fields of the FETCH _after_ the literal
464 * (eg Domino puts FLAGS here). Nothing wrong with that, either.
465 * This all has to go - we should accept literals and nonliterals
466 * interchangeably at any time. */
468 return rc;
469
470 if (msg_parse_fetch(ih, adata->buf) == -1)
471 return rc;
472 }
473
474 rc = 0; /* success */
475
476 /* subtract headers from message size - unfortunately only the subset of
477 * headers we've requested. */
478 ih->content_length -= bytes;
479
480 return rc;
481}
482
491static int flush_buffer(char *buf, size_t *len, struct Connection *conn)
492{
493 buf[*len] = '\0';
494 int rc = mutt_socket_write_n(conn, buf, *len);
495 *len = 0;
496 return rc;
497}
498
508{
509 bool abort = false;
510
512 /* L10N: This prompt is made if the user hits Ctrl-C when opening an IMAP mailbox */
513 if (query_yesorno(_("Abort download and close mailbox?"), MUTT_YES) == MUTT_YES)
514 {
515 abort = true;
517 }
518 SigInt = false;
519
520 return abort;
521}
522
531static void imap_alloc_uid_hash(struct ImapAccountData *adata, unsigned int msn_count)
532{
533 struct ImapMboxData *mdata = adata->mailbox->mdata;
534 if (mdata && !mdata->uid_hash)
535 mdata->uid_hash = mutt_hash_int_new(MAX(6 * msn_count / 5, 30), MUTT_HASH_NO_FLAGS);
536}
537
555static unsigned int imap_fetch_msn_seqset(struct Buffer *buf, struct ImapAccountData *adata,
556 bool evalhc, unsigned int msn_begin,
557 unsigned int msn_end, unsigned int *fetch_msn_end)
558{
559 buf_reset(buf);
560 if (msn_end < msn_begin)
561 return 0;
562
563 struct ImapMboxData *mdata = adata->mailbox->mdata;
564 if (!mdata)
565 return 0;
566
567 unsigned int max_headers_per_fetch = UINT_MAX;
568 bool first_chunk = true;
569 int state = 0; /* 1: single msn, 2: range of msn */
570 unsigned int msn;
571 unsigned int range_begin = 0;
572 unsigned int range_end = 0;
573 unsigned int msn_count = 0;
574
575 const long c_imap_fetch_chunk_size = cs_subset_long(NeoMutt->sub, "imap_fetch_chunk_size");
576 if (c_imap_fetch_chunk_size > 0)
577 max_headers_per_fetch = c_imap_fetch_chunk_size;
578
579 if (!evalhc)
580 {
581 if ((msn_end - msn_begin + 1) <= max_headers_per_fetch)
582 *fetch_msn_end = msn_end;
583 else
584 *fetch_msn_end = msn_begin + max_headers_per_fetch - 1;
585 buf_printf(buf, "%u:%u", msn_begin, *fetch_msn_end);
586 return (*fetch_msn_end - msn_begin + 1);
587 }
588
589 for (msn = msn_begin; msn <= (msn_end + 1); msn++)
590 {
591 if ((msn_count < max_headers_per_fetch) && (msn <= msn_end) &&
592 !imap_msn_get(&mdata->msn, msn - 1))
593 {
594 msn_count++;
595
596 switch (state)
597 {
598 case 1: /* single: convert to a range */
599 state = 2;
601
602 case 2: /* extend range ending */
603 range_end = msn;
604 break;
605 default:
606 state = 1;
607 range_begin = msn;
608 break;
609 }
610 }
611 else if (state)
612 {
613 if (first_chunk)
614 first_chunk = false;
615 else
616 buf_addch(buf, ',');
617
618 if (state == 1)
619 buf_add_printf(buf, "%u", range_begin);
620 else if (state == 2)
621 buf_add_printf(buf, "%u:%u", range_begin, range_end);
622 state = 0;
623
624 if ((buf_len(buf) > 500) || (msn_count >= max_headers_per_fetch))
625 break;
626 }
627 }
628
629 /* The loop index goes one past to terminate the range if needed. */
630 *fetch_msn_end = msn - 1;
631
632 return msn_count;
633}
634
650static void set_changed_flag(struct Mailbox *m, struct Email *e, int local_changes,
651 bool *server_changes, enum MessageType flag_name,
652 bool old_hd_flag, bool new_hd_flag, bool h_flag)
653{
654 /* If there are local_changes, we only want to note if the server
655 * flags have changed, so we can set a reopen flag in
656 * cmd_parse_fetch(). We don't want to count a local modification
657 * to the header flag as a "change". */
658 if ((old_hd_flag == new_hd_flag) && (local_changes != 0))
659 return;
660
661 if (new_hd_flag == h_flag)
662 return;
663
664 if (server_changes)
665 *server_changes = true;
666
667 /* Local changes have priority */
668 if (local_changes == 0)
669 mutt_set_flag(m, e, flag_name, new_hd_flag, true);
670}
671
672#ifdef USE_HCACHE
691 unsigned int msn_end, unsigned int uid_next,
692 bool store_flag_updates, bool eval_condstore)
693{
694 struct Progress *progress = NULL;
695 char buf[1024] = { 0 };
696 int rc = -1;
697
698 struct Mailbox *m = adata->mailbox;
699 struct ImapMboxData *mdata = imap_mdata_get(m);
700 if (!mdata)
701 return -1;
702
703 int idx = m->msg_count;
704
705 if (m->verbose)
706 {
707 /* L10N: Comparing the cached data with the IMAP server's data */
708 progress = progress_new(MUTT_PROGRESS_READ, msn_end);
709 progress_set_message(progress, _("Evaluating cache..."));
710 }
711
712 /* If we are using CONDSTORE's "FETCH CHANGEDSINCE", then we keep
713 * the flags in the header cache, and update them further below.
714 * Otherwise, we fetch the current state of the flags here. */
715 snprintf(buf, sizeof(buf), "UID FETCH 1:%u (UID%s)", uid_next - 1,
716 eval_condstore ? "" : " FLAGS");
717
718 imap_cmd_start(adata, buf);
719
721 int mfhrc = 0;
722 struct ImapHeader h = { 0 };
723 for (int msgno = 1; rc == IMAP_RES_CONTINUE; msgno++)
724 {
726 goto fail;
727
728 progress_update(progress, msgno, -1);
729
730 memset(&h, 0, sizeof(h));
731 h.edata = imap_edata_new();
732 do
733 {
734 rc = imap_cmd_step(adata);
735 if (rc != IMAP_RES_CONTINUE)
736 break;
737
738 mfhrc = msg_fetch_header(m, &h, adata->buf, NULL);
739 if (mfhrc < 0)
740 continue;
741
742 if (!h.edata->uid)
743 {
744 mutt_debug(LL_DEBUG2, "skipping hcache FETCH response for message number %d missing a UID\n",
745 h.edata->msn);
746 continue;
747 }
748
749 if ((h.edata->msn < 1) || (h.edata->msn > msn_end))
750 {
751 mutt_debug(LL_DEBUG1, "skipping hcache FETCH response for unknown message number %d\n",
752 h.edata->msn);
753 continue;
754 }
755
756 if (imap_msn_get(&mdata->msn, h.edata->msn - 1))
757 {
758 mutt_debug(LL_DEBUG2, "skipping hcache FETCH for duplicate message %d\n",
759 h.edata->msn);
760 continue;
761 }
762
763 struct Email *e = imap_hcache_get(mdata, h.edata->uid);
764 m->emails[idx] = e;
765 if (e)
766 {
767 imap_msn_set(&mdata->msn, h.edata->msn - 1, e);
768 mutt_hash_int_insert(mdata->uid_hash, h.edata->uid, e);
769
770 e->index = h.edata->uid;
771 /* messages which have not been expunged are ACTIVE (borrowed from mh
772 * folders) */
773 e->active = true;
774 e->changed = false;
775 if (eval_condstore)
776 {
777 h.edata->read = e->read;
778 h.edata->old = e->old;
779 h.edata->deleted = e->deleted;
780 h.edata->flagged = e->flagged;
781 h.edata->replied = e->replied;
782 }
783 else
784 {
785 e->read = h.edata->read;
786 e->old = h.edata->old;
787 e->deleted = h.edata->deleted;
788 e->flagged = h.edata->flagged;
789 e->replied = h.edata->replied;
790 }
791
792 /* mailbox->emails[msgno]->received is restored from hcache_fetch_email() */
793 e->edata = h.edata;
795
796 /* We take a copy of the tags so we can split the string */
797 char *tags_copy = mutt_str_dup(h.edata->flags_remote);
798 driver_tags_replace(&e->tags, tags_copy);
799 FREE(&tags_copy);
800
801 m->msg_count++;
802 mailbox_size_add(m, e);
803
804 /* If this is the first time we are fetching, we need to
805 * store the current state of flags back into the header cache */
806 if (!eval_condstore && store_flag_updates)
807 imap_hcache_put(mdata, e);
808
809 h.edata = NULL;
810 idx++;
811 }
812 } while (mfhrc == -1);
813
814 imap_edata_free((void **) &h.edata);
815
816 if ((mfhrc < -1) || ((rc != IMAP_RES_CONTINUE) && (rc != IMAP_RES_OK)))
817 goto fail;
818 }
819
820 rc = 0;
821fail:
822 progress_free(&progress);
823 return rc;
824}
825
838static int read_headers_qresync_eval_cache(struct ImapAccountData *adata, char *uid_seqset)
839{
840 int rc;
841 unsigned int uid = 0;
842
843 mutt_debug(LL_DEBUG2, "Reading uid seqset from header cache\n");
844 struct Mailbox *m = adata->mailbox;
845 unsigned int msn = 1;
846 struct ImapMboxData *mdata = adata->mailbox->mdata;
847 if (!mdata)
848 return -1;
849
850 if (m->verbose)
851 mutt_message(_("Evaluating cache..."));
852
853 struct SeqsetIterator *iter = mutt_seqset_iterator_new(uid_seqset);
854 if (!iter)
855 return -1;
856
857 while ((rc = mutt_seqset_iterator_next(iter, &uid)) == 0)
858 {
859 /* The seqset may contain more headers than the fetch request, so
860 * we need to watch and reallocate the context and msn_index */
861 imap_msn_reserve(&mdata->msn, msn);
862
863 struct Email *e = imap_hcache_get(mdata, uid);
864 if (e)
865 {
866 imap_msn_set(&mdata->msn, msn - 1, e);
867
869
871 e->edata = edata;
873
874 e->index = uid;
875 e->active = true;
876 e->changed = false;
877 edata->read = e->read;
878 edata->old = e->old;
879 edata->deleted = e->deleted;
880 edata->flagged = e->flagged;
881 edata->replied = e->replied;
882
883 edata->msn = msn;
884 edata->uid = uid;
886
887 mailbox_size_add(m, e);
888 m->emails[m->msg_count++] = e;
889
890 msn++;
891 }
892 else if (!uid)
893 {
894 /* A non-zero uid missing from the header cache is either the
895 * result of an expunged message (not recorded in the uid seqset)
896 * or a hole in the header cache.
897 *
898 * We have to assume it's an earlier expunge and compact the msn's
899 * in that case, because cmd_parse_vanished() won't find it in the
900 * uid_hash and decrement later msn's there.
901 *
902 * Thus we only increment the uid if the uid was 0: an actual
903 * stored "blank" in the uid seqset.
904 */
905 msn++;
906 }
907 }
908
910
911 return rc;
912}
913
927 unsigned int msn_end, unsigned int uid_next,
928 unsigned long long hc_modseq, bool eval_qresync)
929{
930 struct Progress *progress = NULL;
931 char buf[1024] = { 0 };
932 unsigned int header_msn = 0;
933
934 struct Mailbox *m = adata->mailbox;
935 struct ImapMboxData *mdata = imap_mdata_get(m);
936 if (!mdata)
937 return -1;
938
939 if (m->verbose)
940 {
941 /* L10N: Fetching IMAP flag changes, using the CONDSTORE extension */
942 progress = progress_new(MUTT_PROGRESS_READ, msn_end);
943 progress_set_message(progress, _("Fetching flag updates..."));
944 }
945
946 snprintf(buf, sizeof(buf), "UID FETCH 1:%u (FLAGS) (CHANGEDSINCE %llu%s)",
947 uid_next - 1, hc_modseq, eval_qresync ? " VANISHED" : "");
948
949 imap_cmd_start(adata, buf);
950
951 int rc = IMAP_RES_CONTINUE;
952 for (int msgno = 1; rc == IMAP_RES_CONTINUE; msgno++)
953 {
955 goto fail;
956
957 progress_update(progress, msgno, -1);
958
959 /* cmd_parse_fetch will update the flags */
960 rc = imap_cmd_step(adata);
961 if (rc != IMAP_RES_CONTINUE)
962 break;
963
964 /* so we just need to grab the header and persist it back into
965 * the header cache */
966 char *fetch_buf = adata->buf;
967 if (fetch_buf[0] != '*')
968 continue;
969
970 fetch_buf = imap_next_word(fetch_buf);
971 if (!mutt_isdigit(*fetch_buf) || !mutt_str_atoui(fetch_buf, &header_msn))
972 continue;
973
974 if ((header_msn < 1) || (header_msn > msn_end) ||
975 !imap_msn_get(&mdata->msn, header_msn - 1))
976 {
977 mutt_debug(LL_DEBUG1, "skipping CONDSTORE flag update for unknown message number %u\n",
978 header_msn);
979 continue;
980 }
981
982 imap_hcache_put(mdata, imap_msn_get(&mdata->msn, header_msn - 1));
983 }
984
985 if (rc != IMAP_RES_OK)
986 goto fail;
987
988 /* The IMAP flag setting as part of cmd_parse_fetch() ends up
989 * flipping these on. */
990 mdata->check_status &= ~IMAP_FLAGS_PENDING;
991 m->changed = false;
992
993 /* VANISHED handling: we need to empty out the messages */
994 if (mdata->reopen & IMAP_EXPUNGE_PENDING)
995 {
997 imap_expunge_mailbox(m, false);
998
999 imap_hcache_open(adata, mdata, false);
1000 mdata->reopen &= ~IMAP_EXPUNGE_PENDING;
1001 }
1002
1003 /* undo expunge count updates.
1004 * mview_update() will do this at the end of the header fetch. */
1005 m->vcount = 0;
1006 m->msg_tagged = 0;
1007 m->msg_deleted = 0;
1008 m->msg_new = 0;
1009 m->msg_unread = 0;
1010 m->msg_flagged = 0;
1011 m->changed = false;
1012
1013 rc = 0;
1014fail:
1015 progress_free(&progress);
1016 return rc;
1017}
1018
1027static int imap_verify_qresync(struct Mailbox *m)
1028{
1029 ASSERT(m);
1031 struct ImapMboxData *mdata = imap_mdata_get(m);
1032 if (!adata || (adata->mailbox != m) || !mdata)
1033 return -1;
1034
1035 const size_t max_msn = imap_msn_highest(&mdata->msn);
1036
1037 unsigned int msn;
1038 unsigned int uid;
1039 struct Email *e = NULL;
1040 struct Email *uidh = NULL;
1041
1042 for (int i = 0; i < m->msg_count; i++)
1043 {
1044 e = m->emails[i];
1045 const struct ImapEmailData *edata = imap_edata_get(e);
1046 if (!edata)
1047 goto fail;
1048
1049 msn = imap_edata_get(e)->msn;
1050 uid = imap_edata_get(e)->uid;
1051
1052 if ((msn < 1) || (msn > max_msn) || imap_msn_get(&mdata->msn, msn - 1) != e)
1053 goto fail;
1054
1055 uidh = (struct Email *) mutt_hash_int_find(mdata->uid_hash, uid);
1056 if (uidh != e)
1057 goto fail;
1058 }
1059
1060 return 0;
1061
1062fail:
1063 imap_msn_free(&mdata->msn);
1064 mutt_hash_free(&mdata->uid_hash);
1068
1069 for (int i = 0; i < m->msg_count; i++)
1070 {
1071 if (m->emails[i] && m->emails[i]->edata)
1072 imap_edata_free(&m->emails[i]->edata);
1073 email_free(&m->emails[i]);
1074 }
1075 m->msg_count = 0;
1076 m->size = 0;
1077 hcache_delete_raw(mdata->hcache, "MODSEQ", 6);
1079 imap_hcache_close(mdata);
1080
1081 if (m->verbose)
1082 {
1083 /* L10N: After opening an IMAP mailbox using QRESYNC, Mutt performs a quick
1084 sanity check. If that fails, Mutt reopens the mailbox using a normal
1085 download. */
1086 mutt_error(_("QRESYNC failed. Reopening mailbox."));
1087 }
1088 return -1;
1089}
1090
1091#endif /* USE_HCACHE */
1092
1104static int read_headers_fetch_new(struct Mailbox *m, unsigned int msn_begin,
1105 unsigned int msn_end, bool evalhc,
1106 unsigned int *maxuid, bool initial_download)
1107{
1108 int rc = -1;
1109 unsigned int fetch_msn_end = 0;
1110 struct Progress *progress = NULL;
1111 char *hdrreq = NULL;
1112 struct Buffer *tempfile = NULL;
1113 FILE *fp = NULL;
1114 struct ImapHeader h = { 0 };
1115 struct Buffer *buf = NULL;
1116 static const char *const want_headers = "DATE FROM SENDER SUBJECT TO CC MESSAGE-ID REFERENCES "
1117 "CONTENT-TYPE CONTENT-DESCRIPTION IN-REPLY-TO REPLY-TO "
1118 "LINES LIST-POST LIST-SUBSCRIBE LIST-UNSUBSCRIBE X-LABEL "
1119 "X-ORIGINAL-TO";
1120
1122 struct ImapMboxData *mdata = imap_mdata_get(m);
1123 struct ImapEmailData *edata = NULL;
1124
1125 if (!adata || (adata->mailbox != m) || !mdata)
1126 return -1;
1127
1128 struct Buffer *hdr_list = buf_pool_get();
1129 buf_strcpy(hdr_list, want_headers);
1130 const char *const c_imap_headers = cs_subset_string(NeoMutt->sub, "imap_headers");
1131 if (c_imap_headers)
1132 {
1133 buf_addch(hdr_list, ' ');
1134 buf_addstr(hdr_list, c_imap_headers);
1135 }
1136#ifdef USE_AUTOCRYPT
1137 const bool c_autocrypt = cs_subset_bool(NeoMutt->sub, "autocrypt");
1138 if (c_autocrypt)
1139 {
1140 buf_addch(hdr_list, ' ');
1141 buf_addstr(hdr_list, "AUTOCRYPT");
1142 }
1143#endif
1144
1145 if (adata->capabilities & IMAP_CAP_IMAP4REV1)
1146 {
1147 mutt_str_asprintf(&hdrreq, "BODY.PEEK[HEADER.FIELDS (%s)]", buf_string(hdr_list));
1148 }
1149 else if (adata->capabilities & IMAP_CAP_IMAP4)
1150 {
1151 mutt_str_asprintf(&hdrreq, "RFC822.HEADER.LINES (%s)", buf_string(hdr_list));
1152 }
1153 else
1154 { /* Unable to fetch headers for lower versions */
1155 mutt_error(_("Unable to fetch headers from this IMAP server version"));
1156 goto bail;
1157 }
1158
1159 buf_pool_release(&hdr_list);
1160
1161 /* instead of downloading all headers and then parsing them, we parse them
1162 * as they come in. */
1163 tempfile = buf_pool_get();
1164 buf_mktemp(tempfile);
1165 fp = mutt_file_fopen(buf_string(tempfile), "w+");
1166 if (!fp)
1167 {
1168 mutt_error(_("Could not create temporary file %s"), buf_string(tempfile));
1169 goto bail;
1170 }
1171 unlink(buf_string(tempfile));
1172 buf_pool_release(&tempfile);
1173
1174 if (m->verbose)
1175 {
1176 progress = progress_new(MUTT_PROGRESS_READ, msn_end);
1177 progress_set_message(progress, _("Fetching message headers..."));
1178 }
1179
1180 buf = buf_pool_get();
1181
1182 /* NOTE:
1183 * The (fetch_msn_end < msn_end) used to be important to prevent
1184 * an infinite loop, in the event the server did not return all
1185 * the headers (due to a pending expunge, for example).
1186 *
1187 * I believe the new chunking imap_fetch_msn_seqset()
1188 * implementation and "msn_begin = fetch_msn_end + 1" assignment
1189 * at the end of the loop makes the comparison unneeded, but to be
1190 * cautious I'm keeping it.
1191 */
1192 edata = imap_edata_new();
1193 while ((fetch_msn_end < msn_end) &&
1194 imap_fetch_msn_seqset(buf, adata, evalhc, msn_begin, msn_end, &fetch_msn_end))
1195 {
1196 char *cmd = NULL;
1197 mutt_str_asprintf(&cmd, "FETCH %s (UID FLAGS INTERNALDATE RFC822.SIZE %s)",
1198 buf_string(buf), hdrreq);
1199 imap_cmd_start(adata, cmd);
1200 FREE(&cmd);
1201
1202 int msgno = msn_begin;
1203
1204 while (true)
1205 {
1206 rewind(fp);
1207 memset(&h, 0, sizeof(h));
1208 h.edata = edata;
1209
1210 if (initial_download && SigInt && query_abort_header_download(adata))
1211 {
1212 goto bail;
1213 }
1214
1215 const int rc2 = imap_cmd_step(adata);
1216 if (rc2 != IMAP_RES_CONTINUE)
1217 {
1218 if (rc2 != IMAP_RES_OK)
1219 {
1220 goto bail;
1221 }
1222 break;
1223 }
1224
1225 switch (msg_fetch_header(m, &h, adata->buf, fp))
1226 {
1227 case 0:
1228 break;
1229 case -1:
1230 continue;
1231 case -2:
1232 goto bail;
1233 }
1234
1235 if (!ftello(fp))
1236 {
1237 mutt_debug(LL_DEBUG2, "ignoring fetch response with no body\n");
1238 continue;
1239 }
1240
1241 /* make sure we don't get remnants from older larger message headers */
1242 fputs("\n\n", fp);
1243
1244 if ((h.edata->msn < 1) || (h.edata->msn > fetch_msn_end))
1245 {
1246 mutt_debug(LL_DEBUG1, "skipping FETCH response for unknown message number %d\n",
1247 h.edata->msn);
1248 continue;
1249 }
1250
1251 /* May receive FLAGS updates in a separate untagged response */
1252 if (imap_msn_get(&mdata->msn, h.edata->msn - 1))
1253 {
1254 mutt_debug(LL_DEBUG2, "skipping FETCH response for duplicate message %d\n",
1255 h.edata->msn);
1256 continue;
1257 }
1258
1259 progress_update(progress, msgno++, -1);
1260
1261 struct Email *e = email_new();
1263
1264 m->emails[m->msg_count++] = e;
1265
1266 imap_msn_set(&mdata->msn, h.edata->msn - 1, e);
1267 mutt_hash_int_insert(mdata->uid_hash, h.edata->uid, e);
1268
1269 e->index = h.edata->uid;
1270 /* messages which have not been expunged are ACTIVE (borrowed from mh
1271 * folders) */
1272 e->active = true;
1273 e->changed = false;
1274 e->read = h.edata->read;
1275 e->old = h.edata->old;
1276 e->deleted = h.edata->deleted;
1277 e->flagged = h.edata->flagged;
1278 e->replied = h.edata->replied;
1279 e->received = h.received;
1280 e->edata = (void *) imap_edata_clone(h.edata);
1282 STAILQ_INIT(&e->tags);
1283
1284 /* We take a copy of the tags so we can split the string */
1285 char *tags_copy = mutt_str_dup(h.edata->flags_remote);
1286 driver_tags_replace(&e->tags, tags_copy);
1287 FREE(&tags_copy);
1288
1289 if (*maxuid < h.edata->uid)
1290 *maxuid = h.edata->uid;
1291
1292 rewind(fp);
1293 /* NOTE: if Date: header is missing, mutt_rfc822_read_header depends
1294 * on h.received being set */
1295 e->env = mutt_rfc822_read_header(fp, e, false, false);
1296 /* body built as a side-effect of mutt_rfc822_read_header */
1297 e->body->length = h.content_length;
1298 mailbox_size_add(m, e);
1299
1300#ifdef USE_HCACHE
1301 imap_hcache_put(mdata, e);
1302#endif /* USE_HCACHE */
1303 }
1304
1305 /* In case we get new mail while fetching the headers. */
1306 if (mdata->reopen & IMAP_NEWMAIL_PENDING)
1307 {
1308 msn_end = mdata->new_mail_count;
1309 mx_alloc_memory(m, msn_end);
1310 imap_msn_reserve(&mdata->msn, msn_end);
1311 mdata->reopen &= ~IMAP_NEWMAIL_PENDING;
1312 mdata->new_mail_count = 0;
1313 }
1314
1315 /* Note: RFC3501 section 7.4.1 and RFC7162 section 3.2.10.2 say we
1316 * must not get any EXPUNGE/VANISHED responses in the middle of a
1317 * FETCH, nor when no command is in progress (e.g. between the
1318 * chunked FETCH commands). We previously tried to be robust by
1319 * setting:
1320 * msn_begin = mdata->max_msn + 1;
1321 * but with chunking and header cache holes this
1322 * may not be correct. So here we must assume the msn values have
1323 * not been altered during or after the fetch. */
1324 msn_begin = fetch_msn_end + 1;
1325 }
1326
1327 rc = 0;
1328
1329bail:
1330 buf_pool_release(&hdr_list);
1331 buf_pool_release(&buf);
1332 buf_pool_release(&tempfile);
1333 mutt_file_fclose(&fp);
1334 FREE(&hdrreq);
1335 imap_edata_free((void **) &edata);
1336 progress_free(&progress);
1337
1338 return rc;
1339}
1340
1354int imap_read_headers(struct Mailbox *m, unsigned int msn_begin,
1355 unsigned int msn_end, bool initial_download)
1356{
1357 unsigned int maxuid = 0;
1358 int rc = -1;
1359 bool evalhc = false;
1360
1361#ifdef USE_HCACHE
1362 uint32_t uidvalidity = 0;
1363 unsigned int uid_next = 0;
1364 unsigned long long modseq = 0;
1365 bool has_condstore = false;
1366 bool has_qresync = false;
1367 bool eval_condstore = false;
1368 bool eval_qresync = false;
1369 char *uid_seqset = NULL;
1370 const unsigned int msn_begin_save = msn_begin;
1371#endif /* USE_HCACHE */
1372
1374 struct ImapMboxData *mdata = imap_mdata_get(m);
1375 if (!adata || (adata->mailbox != m) || !mdata)
1376 return -1;
1377
1378#ifdef USE_HCACHE
1379retry:
1380#endif /* USE_HCACHE */
1381
1382 /* make sure context has room to hold the mailbox */
1383 mx_alloc_memory(m, msn_end);
1384 imap_msn_reserve(&mdata->msn, msn_end);
1385 imap_alloc_uid_hash(adata, msn_end);
1386
1388 mdata->new_mail_count = 0;
1389
1390#ifdef USE_HCACHE
1391 imap_hcache_open(adata, mdata, true);
1392
1393 if (mdata->hcache && initial_download)
1394 {
1395 hcache_fetch_raw_obj(mdata->hcache, "UIDVALIDITY", 11, &uidvalidity);
1396 hcache_fetch_raw_obj(mdata->hcache, "UIDNEXT", 7, &uid_next);
1397 if (mdata->modseq)
1398 {
1399 const bool c_imap_condstore = cs_subset_bool(NeoMutt->sub, "imap_condstore");
1400 if ((adata->capabilities & IMAP_CAP_CONDSTORE) && c_imap_condstore)
1401 has_condstore = true;
1402
1403 /* If IMAP_CAP_QRESYNC and ImapQResync then NeoMutt sends ENABLE QRESYNC.
1404 * If we receive an ENABLED response back, then adata->qresync is set. */
1405 if (adata->qresync)
1406 has_qresync = true;
1407 }
1408
1409 if (uidvalidity && uid_next && (uidvalidity == mdata->uidvalidity))
1410 {
1411 evalhc = true;
1412 if (hcache_fetch_raw_obj(mdata->hcache, "MODSEQ", 6, &modseq))
1413 {
1414 if (has_qresync)
1415 {
1416 uid_seqset = imap_hcache_get_uid_seqset(mdata);
1417 if (uid_seqset)
1418 eval_qresync = true;
1419 }
1420
1421 if (!eval_qresync && has_condstore)
1422 eval_condstore = true;
1423 }
1424 }
1425 }
1426 if (evalhc)
1427 {
1428 if (eval_qresync)
1429 {
1430 if (read_headers_qresync_eval_cache(adata, uid_seqset) < 0)
1431 goto bail;
1432 }
1433 else
1434 {
1435 if (read_headers_normal_eval_cache(adata, msn_end, uid_next, has_condstore || has_qresync,
1436 eval_condstore) < 0)
1437 goto bail;
1438 }
1439
1440 if ((eval_condstore || eval_qresync) && (modseq != mdata->modseq))
1441 {
1443 modseq, eval_qresync) < 0)
1444 {
1445 goto bail;
1446 }
1447 }
1448
1449 /* Look for the first empty MSN and start there */
1450 while (msn_begin <= msn_end)
1451 {
1452 if (!imap_msn_get(&mdata->msn, msn_begin - 1))
1453 break;
1454 msn_begin++;
1455 }
1456 }
1457#endif /* USE_HCACHE */
1458
1459 if (read_headers_fetch_new(m, msn_begin, msn_end, evalhc, &maxuid, initial_download) < 0)
1460 goto bail;
1461
1462#ifdef USE_HCACHE
1463 if (eval_qresync && initial_download)
1464 {
1465 if (imap_verify_qresync(m) != 0)
1466 {
1467 eval_qresync = false;
1468 eval_condstore = false;
1469 evalhc = false;
1470 modseq = 0;
1471 maxuid = 0;
1472 FREE(&uid_seqset);
1473 uidvalidity = 0;
1474 uid_next = 0;
1475 msn_begin = msn_begin_save;
1476
1477 goto retry;
1478 }
1479 }
1480#endif /* USE_HCACHE */
1481
1482 if (maxuid && (mdata->uid_next < maxuid + 1))
1483 mdata->uid_next = maxuid + 1;
1484
1485#ifdef USE_HCACHE
1486 hcache_store_raw(mdata->hcache, "UIDVALIDITY", 11, &mdata->uidvalidity,
1487 sizeof(mdata->uidvalidity));
1488 if (maxuid && (mdata->uid_next < maxuid + 1))
1489 {
1490 mutt_debug(LL_DEBUG2, "Overriding UIDNEXT: %u -> %u\n", mdata->uid_next, maxuid + 1);
1491 mdata->uid_next = maxuid + 1;
1492 }
1493 if (mdata->uid_next > 1)
1494 {
1495 hcache_store_raw(mdata->hcache, "UIDNEXT", 7, &mdata->uid_next, sizeof(mdata->uid_next));
1496 }
1497
1498 /* We currently only sync CONDSTORE and QRESYNC on the initial download.
1499 * To do it more often, we'll need to deal with flag updates combined with
1500 * unsync'ed local flag changes. We'll also need to properly sync flags to
1501 * the header cache on close. I'm not sure it's worth the added complexity. */
1502 if (initial_download)
1503 {
1504 if (has_condstore || has_qresync)
1505 {
1506 hcache_store_raw(mdata->hcache, "MODSEQ", 6, &mdata->modseq, sizeof(mdata->modseq));
1507 }
1508 else
1509 {
1510 hcache_delete_raw(mdata->hcache, "MODSEQ", 6);
1511 }
1512
1513 if (has_qresync)
1515 else
1517 }
1518#endif /* USE_HCACHE */
1519
1520 /* TODO: it's not clear to me why we are calling mx_alloc_memory yet again. */
1522
1523 mdata->reopen |= IMAP_REOPEN_ALLOW;
1524
1525 rc = msn_end;
1526
1527bail:
1528#ifdef USE_HCACHE
1530 FREE(&uid_seqset);
1531#endif /* USE_HCACHE */
1532
1533 return rc;
1534}
1535
1543int imap_append_message(struct Mailbox *m, struct Message *msg)
1544{
1545 if (!m || !msg)
1546 return -1;
1547
1549 struct ImapMboxData *mdata = imap_mdata_get(m);
1550 if (!adata || !mdata)
1551 return -1;
1552
1553 FILE *fp = NULL;
1554 char buf[2048] = { 0 };
1555 struct Buffer *internaldate = NULL;
1556 struct Buffer *imap_flags = NULL;
1557 size_t len;
1558 struct Progress *progress = NULL;
1559 size_t sent;
1560 int c, last;
1561 int rc;
1562
1563 fp = mutt_file_fopen(msg->path, "r");
1564 if (!fp)
1565 {
1566 mutt_perror("%s", msg->path);
1567 goto fail;
1568 }
1569
1570 /* currently we set the \Seen flag on all messages, but probably we
1571 * should scan the message Status header for flag info. Since we're
1572 * already rereading the whole file for length it isn't any more
1573 * expensive (it'd be nice if we had the file size passed in already
1574 * by the code that writes the file, but that's a lot of changes.
1575 * Ideally we'd have an Email structure with flag info here... */
1576 for (last = EOF, len = 0; (c = fgetc(fp)) != EOF; last = c)
1577 {
1578 if ((c == '\n') && (last != '\r'))
1579 len++;
1580
1581 len++;
1582 }
1583 rewind(fp);
1584
1585 if (m->verbose)
1586 {
1587 progress = progress_new(MUTT_PROGRESS_NET, len);
1588 progress_set_message(progress, _("Uploading message..."));
1589 }
1590
1591 internaldate = buf_pool_get();
1592 mutt_date_make_imap(internaldate, msg->received);
1593
1594 imap_flags = buf_pool_get();
1595
1596 if (msg->flags.read)
1597 buf_addstr(imap_flags, " \\Seen");
1598 if (msg->flags.replied)
1599 buf_addstr(imap_flags, " \\Answered");
1600 if (msg->flags.flagged)
1601 buf_addstr(imap_flags, " \\Flagged");
1602 if (msg->flags.draft)
1603 buf_addstr(imap_flags, " \\Draft");
1604
1605 snprintf(buf, sizeof(buf), "APPEND %s (%s) \"%s\" {%lu}", mdata->munge_name,
1606 imap_flags->data + 1, buf_string(internaldate), (unsigned long) len);
1607 buf_pool_release(&internaldate);
1608
1609 imap_cmd_start(adata, buf);
1610
1611 do
1612 {
1613 rc = imap_cmd_step(adata);
1614 } while (rc == IMAP_RES_CONTINUE);
1615
1616 if (rc != IMAP_RES_RESPOND)
1617 goto cmd_step_fail;
1618
1619 for (last = EOF, sent = len = 0; (c = fgetc(fp)) != EOF; last = c)
1620 {
1621 if ((c == '\n') && (last != '\r'))
1622 buf[len++] = '\r';
1623
1624 buf[len++] = c;
1625
1626 if (len > sizeof(buf) - 3)
1627 {
1628 sent += len;
1629 if (flush_buffer(buf, &len, adata->conn) < 0)
1630 goto fail;
1631 progress_update(progress, sent, -1);
1632 }
1633 }
1634
1635 if (len > 0)
1636 if (flush_buffer(buf, &len, adata->conn) < 0)
1637 goto fail;
1638
1639 if (mutt_socket_send(adata->conn, "\r\n") < 0)
1640 goto fail;
1641 mutt_file_fclose(&fp);
1642
1643 do
1644 {
1645 rc = imap_cmd_step(adata);
1646 } while (rc == IMAP_RES_CONTINUE);
1647
1648 if (rc != IMAP_RES_OK)
1649 goto cmd_step_fail;
1650
1651 progress_free(&progress);
1652 buf_pool_release(&imap_flags);
1653 return 0;
1654
1655cmd_step_fail:
1656 mutt_debug(LL_DEBUG1, "command failed: %s\n", adata->buf);
1657 if (rc != IMAP_RES_BAD)
1658 {
1659 char *pc = imap_next_word(adata->buf); /* skip sequence number or token */
1660 pc = imap_next_word(pc); /* skip response code */
1661 if (*pc != '\0')
1662 mutt_error("%s", pc);
1663 }
1664
1665fail:
1666 mutt_file_fclose(&fp);
1667 progress_free(&progress);
1668 buf_pool_release(&imap_flags);
1669 return -1;
1670}
1671
1678static int emails_to_uid_array(struct EmailArray *ea, struct UidArray *uida)
1679{
1680 struct Email **ep = NULL;
1681 ARRAY_FOREACH(ep, ea)
1682 {
1683 struct Email *e = *ep;
1684 struct ImapEmailData *edata = imap_edata_get(e);
1685
1686 ARRAY_ADD(uida, edata->uid);
1687 }
1688 ARRAY_SORT(uida, imap_sort_uid, NULL);
1689
1690 return ARRAY_SIZE(uida);
1691}
1692
1703int imap_copy_messages(struct Mailbox *m, struct EmailArray *ea,
1704 const char *dest, enum MessageSaveOpt save_opt)
1705{
1706 if (!m || !ea || ARRAY_EMPTY(ea) || !dest)
1707 return -1;
1708
1710 if (!adata)
1711 return -1;
1712
1713 char buf[PATH_MAX] = { 0 };
1714 char mbox[PATH_MAX] = { 0 };
1715 char mmbox[PATH_MAX] = { 0 };
1716 char prompt[PATH_MAX + 64];
1717 int rc;
1718 struct ConnAccount cac = { { 0 } };
1719 enum QuadOption err_continue = MUTT_NO;
1720 int triedcreate = 0;
1721 struct Email *e_cur = *ARRAY_GET(ea, 0);
1722 bool single = (ARRAY_SIZE(ea) == 1);
1723
1724 if (single && e_cur->attach_del)
1725 {
1726 mutt_debug(LL_DEBUG3, "#1 Message contains attachments to be deleted\n");
1727 return 1;
1728 }
1729
1730 if (imap_parse_path(dest, &cac, buf, sizeof(buf)))
1731 {
1732 mutt_debug(LL_DEBUG1, "bad destination %s\n", dest);
1733 return -1;
1734 }
1735
1736 /* check that the save-to folder is in the same account */
1737 if (!imap_account_match(&adata->conn->account, &cac))
1738 {
1739 mutt_debug(LL_DEBUG3, "%s not same server as %s\n", dest, mailbox_path(m));
1740 return 1;
1741 }
1742
1743 imap_fix_path_with_delim(adata->delim, buf, mbox, sizeof(mbox));
1744 if (*mbox == '\0')
1745 mutt_str_copy(mbox, "INBOX", sizeof(mbox));
1746 imap_munge_mbox_name(adata->unicode, mmbox, sizeof(mmbox), mbox);
1747
1748 /* loop in case of TRYCREATE */
1749 struct Buffer *cmd = buf_pool_get();
1750 struct Buffer *sync_cmd = buf_pool_get();
1751 do
1752 {
1753 buf_reset(sync_cmd);
1754 buf_reset(cmd);
1755
1756 if (single)
1757 {
1758 mutt_message(_("Copying message %d to %s..."), e_cur->index + 1, mbox);
1759 buf_add_printf(cmd, "UID COPY %u %s", imap_edata_get(e_cur)->uid, mmbox);
1760
1761 if (e_cur->active && e_cur->changed)
1762 {
1763 rc = imap_sync_message_for_copy(m, e_cur, sync_cmd, &err_continue);
1764 if (rc < 0)
1765 {
1766 mutt_debug(LL_DEBUG1, "#2 could not sync\n");
1767 goto out;
1768 }
1769 }
1770 rc = imap_exec(adata, buf_string(cmd), IMAP_CMD_QUEUE);
1771 if (rc != IMAP_EXEC_SUCCESS)
1772 {
1773 mutt_debug(LL_DEBUG1, "#2 could not queue copy\n");
1774 goto out;
1775 }
1776 }
1777 else /* copy tagged messages */
1778 {
1779 /* if any messages have attachments to delete, fall through to FETCH
1780 * and APPEND. TODO: Copy what we can with COPY, fall through for the
1781 * remainder. */
1782 struct Email **ep = NULL;
1783 ARRAY_FOREACH(ep, ea)
1784 {
1785 struct Email *e = *ep;
1786 if (e->attach_del)
1787 {
1788 mutt_debug(LL_DEBUG3, "#2 Message contains attachments to be deleted\n");
1789 rc = 1;
1790 goto out;
1791 }
1792
1793 if (e->active && e->changed)
1794 {
1795 rc = imap_sync_message_for_copy(m, e, sync_cmd, &err_continue);
1796 if (rc < 0)
1797 {
1798 mutt_debug(LL_DEBUG1, "#1 could not sync\n");
1799 goto out;
1800 }
1801 }
1802 }
1803
1804 struct UidArray uida = ARRAY_HEAD_INITIALIZER;
1805 emails_to_uid_array(ea, &uida);
1806 rc = imap_exec_msg_set(adata, "UID COPY", mmbox, &uida);
1807 ARRAY_FREE(&uida);
1808
1809 if (rc == 0)
1810 {
1811 mutt_debug(LL_DEBUG1, "No messages tagged\n");
1812 rc = -1;
1813 goto out;
1814 }
1815 else if (rc < 0)
1816 {
1817 mutt_debug(LL_DEBUG1, "#1 could not queue copy\n");
1818 goto out;
1819 }
1820 else
1821 {
1822 mutt_message(ngettext("Copying %d message to %s...", "Copying %d messages to %s...", rc),
1823 rc, mbox);
1824 }
1825 }
1826
1827 /* let's get it on */
1828 rc = imap_exec(adata, NULL, IMAP_CMD_NO_FLAGS);
1829 if (rc == IMAP_EXEC_ERROR)
1830 {
1831 if (triedcreate)
1832 {
1833 mutt_debug(LL_DEBUG1, "Already tried to create mailbox %s\n", mbox);
1834 break;
1835 }
1836 /* bail out if command failed for reasons other than nonexistent target */
1837 if (!mutt_istr_startswith(imap_get_qualifier(adata->buf), "[TRYCREATE]"))
1838 break;
1839 mutt_debug(LL_DEBUG3, "server suggests TRYCREATE\n");
1840 snprintf(prompt, sizeof(prompt), _("Create %s?"), mbox);
1841 const bool c_confirm_create = cs_subset_bool(NeoMutt->sub, "confirm_create");
1842 if (c_confirm_create &&
1843 (query_yesorno_help(prompt, MUTT_YES, NeoMutt->sub, "confirm_create") != MUTT_YES))
1844 {
1846 goto out;
1847 }
1848 if (imap_create_mailbox(adata, mbox) < 0)
1849 break;
1850 triedcreate = 1;
1851 }
1852 } while (rc == IMAP_EXEC_ERROR);
1853
1854 if (rc != 0)
1855 {
1856 imap_error("imap_copy_messages", adata->buf);
1857 goto out;
1858 }
1859
1860 /* cleanup */
1861 if (save_opt == SAVE_MOVE)
1862 {
1863 struct Email **ep = NULL;
1864 ARRAY_FOREACH(ep, ea)
1865 {
1866 struct Email *e = *ep;
1867 mutt_set_flag(m, e, MUTT_DELETE, true, true);
1868 mutt_set_flag(m, e, MUTT_PURGE, true, true);
1869 }
1870 }
1871
1872 rc = 0;
1873
1874out:
1875 buf_pool_release(&cmd);
1876 buf_pool_release(&sync_cmd);
1877
1878 return (rc < 0) ? -1 : rc;
1879}
1880
1888int imap_cache_del(struct Mailbox *m, struct Email *e)
1889{
1891 struct ImapMboxData *mdata = imap_mdata_get(m);
1892
1893 if (!e || !adata || (adata->mailbox != m) || !mdata)
1894 return -1;
1895
1896 mdata->bcache = imap_bcache_open(m);
1897 char id[64] = { 0 };
1898 snprintf(id, sizeof(id), "%u-%u", mdata->uidvalidity, imap_edata_get(e)->uid);
1899 return mutt_bcache_del(mdata->bcache, id);
1900}
1901
1908{
1910 struct ImapMboxData *mdata = imap_mdata_get(m);
1911
1912 if (!adata || (adata->mailbox != m) || !mdata)
1913 return -1;
1914
1915 mdata->bcache = imap_bcache_open(m);
1917
1918 return 0;
1919}
1920
1939char *imap_set_flags(struct Mailbox *m, struct Email *e, char *s, bool *server_changes)
1940{
1942 if (!adata || (adata->mailbox != m))
1943 return NULL;
1944
1945 struct ImapHeader newh = { 0 };
1946 struct ImapEmailData old_edata = { 0 };
1947 int local_changes = e->changed;
1948
1949 struct ImapEmailData *edata = e->edata;
1950 newh.edata = edata;
1951
1952 mutt_debug(LL_DEBUG2, "parsing FLAGS\n");
1953 s = msg_parse_flags(&newh, s);
1954 if (!s)
1955 return NULL;
1956
1957 /* Update tags system */
1958 /* We take a copy of the tags so we can split the string */
1959 char *tags_copy = mutt_str_dup(edata->flags_remote);
1960 driver_tags_replace(&e->tags, tags_copy);
1961 FREE(&tags_copy);
1962
1963 /* YAUH (yet another ugly hack): temporarily set context to
1964 * read-write even if it's read-only, so *server* updates of
1965 * flags can be processed by mutt_set_flag. mailbox->changed must
1966 * be restored afterwards */
1967 bool readonly = m->readonly;
1968 m->readonly = false;
1969
1970 /* This is redundant with the following two checks. Removing:
1971 * mutt_set_flag (m, e, MUTT_NEW, !(edata->read || edata->old), true); */
1972 set_changed_flag(m, e, local_changes, server_changes, MUTT_OLD, old_edata.old,
1973 edata->old, e->old);
1974 set_changed_flag(m, e, local_changes, server_changes, MUTT_READ,
1975 old_edata.read, edata->read, e->read);
1976 set_changed_flag(m, e, local_changes, server_changes, MUTT_DELETE,
1977 old_edata.deleted, edata->deleted, e->deleted);
1978 set_changed_flag(m, e, local_changes, server_changes, MUTT_FLAG,
1979 old_edata.flagged, edata->flagged, e->flagged);
1980 set_changed_flag(m, e, local_changes, server_changes, MUTT_REPLIED,
1981 old_edata.replied, edata->replied, e->replied);
1982
1983 /* this message is now definitively *not* changed (mutt_set_flag
1984 * marks things changed as a side-effect) */
1985 if (local_changes == 0)
1986 e->changed = false;
1987 m->changed &= !readonly;
1988 m->readonly = readonly;
1989
1990 return s;
1991}
1992
1996bool imap_msg_open(struct Mailbox *m, struct Message *msg, struct Email *e)
1997{
1998 struct Envelope *newenv = NULL;
1999 char buf[1024] = { 0 };
2000 char *pc = NULL;
2001 unsigned int bytes;
2002 unsigned int uid;
2003 bool retried = false;
2004 bool read;
2005 int rc;
2006
2007 /* Sam's weird courier server returns an OK response even when FETCH
2008 * fails. Thanks Sam. */
2009 bool fetched = false;
2010
2012
2013 if (!adata || (adata->mailbox != m))
2014 return false;
2015
2016 msg->fp = msg_cache_get(m, e);
2017 if (msg->fp)
2018 {
2019 if (imap_edata_get(e)->parsed)
2020 return true;
2021 goto parsemsg;
2022 }
2023
2024 /* This function is called in a few places after endwin()
2025 * e.g. mutt_pipe_message(). */
2026 bool output_progress = !isendwin() && m->verbose;
2027 if (output_progress)
2028 mutt_message(_("Fetching message..."));
2029
2030 msg->fp = msg_cache_put(m, e);
2031 if (!msg->fp)
2032 {
2033 struct Buffer *tempfile = buf_pool_get();
2034 buf_mktemp(tempfile);
2035 msg->fp = mutt_file_fopen(buf_string(tempfile), "w+");
2036 unlink(buf_string(tempfile));
2037 buf_pool_release(&tempfile);
2038
2039 if (!msg->fp)
2040 return false;
2041 }
2042
2043 /* mark this header as currently inactive so the command handler won't
2044 * also try to update it. HACK until all this code can be moved into the
2045 * command handler */
2046 e->active = false;
2047
2048 const bool c_imap_peek = cs_subset_bool(NeoMutt->sub, "imap_peek");
2049 snprintf(buf, sizeof(buf), "UID FETCH %u %s", imap_edata_get(e)->uid,
2050 ((adata->capabilities & IMAP_CAP_IMAP4REV1) ?
2051 (c_imap_peek ? "BODY.PEEK[]" : "BODY[]") :
2052 "RFC822"));
2053
2054 imap_cmd_start(adata, buf);
2055 do
2056 {
2057 rc = imap_cmd_step(adata);
2058 if (rc != IMAP_RES_CONTINUE)
2059 break;
2060
2061 pc = adata->buf;
2062 pc = imap_next_word(pc);
2063 pc = imap_next_word(pc);
2064
2065 if (mutt_istr_startswith(pc, "FETCH"))
2066 {
2067 while (*pc)
2068 {
2069 pc = imap_next_word(pc);
2070 if (pc[0] == '(')
2071 pc++;
2072 if (mutt_istr_startswith(pc, "UID"))
2073 {
2074 pc = imap_next_word(pc);
2075 if (!mutt_str_atoui(pc, &uid))
2076 goto bail;
2077 if (uid != imap_edata_get(e)->uid)
2078 {
2079 mutt_error(_("The message index is incorrect. Try reopening the mailbox."));
2080 }
2081 }
2082 else if (mutt_istr_startswith(pc, "RFC822") || mutt_istr_startswith(pc, "BODY[]"))
2083 {
2084 pc = imap_next_word(pc);
2085 if (imap_get_literal_count(pc, &bytes) < 0)
2086 {
2087 imap_error("imap_msg_open()", buf);
2088 goto bail;
2089 }
2090
2091 const int res = imap_read_literal(msg->fp, adata, bytes, NULL);
2092 if (res < 0)
2093 {
2094 goto bail;
2095 }
2096 /* pick up trailing line */
2097 rc = imap_cmd_step(adata);
2098 if (rc != IMAP_RES_CONTINUE)
2099 goto bail;
2100 pc = adata->buf;
2101
2102 fetched = true;
2103 }
2104 else if (!e->changed && mutt_istr_startswith(pc, "FLAGS"))
2105 {
2106 /* UW-IMAP will provide a FLAGS update here if the FETCH causes a
2107 * change (eg from \Unseen to \Seen).
2108 * Uncommitted changes in neomutt take precedence. If we decide to
2109 * incrementally update flags later, this won't stop us syncing */
2110 pc = imap_set_flags(m, e, pc, NULL);
2111 if (!pc)
2112 goto bail;
2113 }
2114 }
2115 }
2116 } while (rc == IMAP_RES_CONTINUE);
2117
2118 /* see comment before command start. */
2119 e->active = true;
2120
2121 fflush(msg->fp);
2122 if (ferror(msg->fp))
2123 goto bail;
2124
2125 if (rc != IMAP_RES_OK)
2126 goto bail;
2127
2128 if (!fetched || !imap_code(adata->buf))
2129 goto bail;
2130
2131 if (msg_cache_commit(m, e) < 0)
2132 mutt_debug(LL_DEBUG1, "failed to add message to cache\n");
2133
2134parsemsg:
2135 /* Update the header information. Previously, we only downloaded a
2136 * portion of the headers, those required for the main display. */
2137 rewind(msg->fp);
2138 /* It may be that the Status header indicates a message is read, but the
2139 * IMAP server doesn't know the message has been \Seen. So we capture
2140 * the server's notion of 'read' and if it differs from the message info
2141 * picked up in mutt_rfc822_read_header, we mark the message (and context
2142 * changed). Another possibility: ignore Status on IMAP? */
2143 read = e->read;
2144 newenv = mutt_rfc822_read_header(msg->fp, e, false, false);
2145 mutt_env_merge(e->env, &newenv);
2146
2147 /* see above. We want the new status in e->read, so we unset it manually
2148 * and let mutt_set_flag set it correctly, updating context. */
2149 if (read != e->read)
2150 {
2151 e->read = read;
2152 mutt_set_flag(m, e, MUTT_NEW, read, true);
2153 }
2154
2155 e->lines = 0;
2156 while (fgets(buf, sizeof(buf), msg->fp) && !feof(msg->fp))
2157 {
2158 e->lines++;
2159 }
2160
2161 e->body->length = ftell(msg->fp) - e->body->offset;
2162
2164 rewind(msg->fp);
2165 imap_edata_get(e)->parsed = true;
2166
2167 /* retry message parse if cached message is empty */
2168 if (!retried && ((e->lines == 0) || (e->body->length == 0)))
2169 {
2170 imap_cache_del(m, e);
2171 retried = true;
2172 goto parsemsg;
2173 }
2174
2175 return true;
2176
2177bail:
2178 e->active = true;
2179 mutt_file_fclose(&msg->fp);
2180 imap_cache_del(m, e);
2181 return false;
2182}
2183
2189int imap_msg_commit(struct Mailbox *m, struct Message *msg)
2190{
2191 int rc = mutt_file_fclose(&msg->fp);
2192 if (rc != 0)
2193 return rc;
2194
2195 return imap_append_message(m, msg);
2196}
2197
2203int imap_msg_close(struct Mailbox *m, struct Message *msg)
2204{
2205 return mutt_file_fclose(&msg->fp);
2206}
2207
2211int imap_msg_save_hcache(struct Mailbox *m, struct Email *e)
2212{
2213 int rc = 0;
2214#ifdef USE_HCACHE
2215 bool close_hc = true;
2217 struct ImapMboxData *mdata = imap_mdata_get(m);
2218 if (!mdata || !adata)
2219 return -1;
2220 if (mdata->hcache)
2221 close_hc = false;
2222 else
2223 imap_hcache_open(adata, mdata, true);
2224 rc = imap_hcache_put(mdata, e);
2225 if (close_hc)
2227#endif
2228 return rc;
2229}
#define ARRAY_SORT(head, fn, sdata)
Sort an array.
Definition array.h:335
#define ARRAY_ADD(head, elem)
Add an element at the end of the array.
Definition array.h:156
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition array.h:214
#define ARRAY_EMPTY(head)
Check if an array is empty.
Definition array.h:74
#define ARRAY_SIZE(head)
The number of elements stored.
Definition array.h:87
#define ARRAY_FREE(head)
Release all memory.
Definition array.h:204
#define ARRAY_GET(head, idx)
Return the element at index.
Definition array.h:109
#define ARRAY_HEAD_INITIALIZER
Static initializer for arrays.
Definition array.h:58
const char * mutt_str_atol(const char *str, long *dst)
Convert ASCII string to a long.
Definition atoi.c:142
const char * mutt_str_atoui(const char *str, unsigned int *dst)
Convert ASCII string to an unsigned integer.
Definition atoi.c:217
Body Caching (local copies of email bodies)
int mutt_bcache_commit(struct BodyCache *bcache, const char *id)
Move a temporary file into the Body Cache.
Definition bcache.c:252
struct BodyCache * mutt_bcache_open(struct ConnAccount *account, const char *mailbox)
Open an Email-Body Cache.
Definition bcache.c:146
int mutt_bcache_list(struct BodyCache *bcache, bcache_list_t want_id, void *data)
Find matching entries in the Body Cache.
Definition bcache.c:334
FILE * mutt_bcache_get(struct BodyCache *bcache, const char *id)
Open a file in the Body Cache.
Definition bcache.c:185
int mutt_bcache_del(struct BodyCache *bcache, const char *id)
Delete a file from the Body Cache.
Definition bcache.c:269
FILE * mutt_bcache_put(struct BodyCache *bcache, const char *id)
Create a file in the Body Cache.
Definition bcache.c:212
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition buffer.c:161
int buf_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition buffer.c:204
size_t buf_len(const struct Buffer *buf)
Calculate the length of a Buffer.
Definition buffer.c:491
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition buffer.c:76
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition buffer.c:241
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition buffer.c:226
void buf_join_str(struct Buffer *buf, const char *str, char sep)
Join a buffer with a string separated by sep.
Definition buffer.c:748
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition buffer.c:395
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition buffer.c:571
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition helpers.c:291
long cs_subset_long(const struct ConfigSubset *sub, const char *name)
Get a long config item by name.
Definition helpers.c:95
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition helpers.c:47
Convenience wrapper for the config headers.
Connection Library.
Convenience wrapper for the core headers.
void mailbox_size_add(struct Mailbox *m, const struct Email *e)
Add an email's size to the total size of a Mailbox.
Definition mailbox.c:247
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox's path string.
Definition mailbox.h:214
bool mutt_isspace(int arg)
Wrapper for isspace(3)
Definition ctype.c:95
bool mutt_isdigit(int arg)
Wrapper for isdigit(3)
Definition ctype.c:65
struct Email * email_new(void)
Create a new Email.
Definition email.c:77
void email_free(struct Email **ptr)
Free an Email.
Definition email.c:46
Structs that make up an email.
struct Envelope * mutt_rfc822_read_header(FILE *fp, struct Email *e, bool user_hdrs, bool weed)
Parses an RFC822 header.
Definition parse.c:1204
void mutt_env_merge(struct Envelope *base, struct Envelope **extra)
Merge the headers of two Envelopes.
Definition envelope.c:192
Manage where the email is piped to external commands.
MessageSaveOpt
Message save option.
Definition external.h:52
@ SAVE_MOVE
Move message to another mailbox, removing the original.
Definition external.h:54
#define mutt_file_fclose(FP)
Definition file.h:139
#define mutt_file_fopen(PATH, MODE)
Definition file.h:138
void mutt_set_flag(struct Mailbox *m, struct Email *e, enum MessageType flag, bool bf, bool upd_mbox)
Set a flag on an email.
Definition flags.c:56
void mutt_flushinp(void)
Empty all the keyboard buffers.
Definition get.c:58
static int imap_bcache_delete(const char *id, struct BodyCache *bcache, void *data)
Delete an entry from the message cache - Implements bcache_list_t -.
Definition message.c:169
void imap_edata_free(void **ptr)
Free the private Email data - Implements Email::edata_free() -.
Definition edata.c:39
#define mutt_error(...)
Definition logging2.h:93
#define mutt_message(...)
Definition logging2.h:92
#define mutt_debug(LEVEL,...)
Definition logging2.h:90
#define mutt_perror(...)
Definition logging2.h:94
int imap_msg_close(struct Mailbox *m, struct Message *msg)
Close an email - Implements MxOps::msg_close() -.
Definition message.c:2203
int imap_msg_commit(struct Mailbox *m, struct Message *msg)
Save changes to an email - Implements MxOps::msg_commit() -.
Definition message.c:2189
bool imap_msg_open(struct Mailbox *m, struct Message *msg, struct Email *e)
Open an email message in a Mailbox - Implements MxOps::msg_open() -.
Definition message.c:1996
int imap_msg_save_hcache(struct Mailbox *m, struct Email *e)
Save message to the header cache - Implements MxOps::msg_save_hcache() -.
Definition message.c:2211
int imap_sort_uid(const void *a, const void *b, void *sdata)
Compare two UIDs - Implements sort_t -.
Definition msg_set.c:54
Convenience wrapper for the gui headers.
struct HashTable * mutt_hash_int_new(size_t num_elems, HashFlags flags)
Create a new Hash Table (with integer keys)
Definition hash.c:285
void * mutt_hash_int_find(const struct HashTable *table, unsigned int intkey)
Find the HashElem data in a Hash Table element using a key.
Definition hash.c:392
struct HashElem * mutt_hash_int_insert(struct HashTable *table, unsigned int intkey, void *data)
Add a new element to the Hash Table (with integer keys)
Definition hash.c:347
void mutt_hash_free(struct HashTable **ptr)
Free a hash table.
Definition hash.c:457
#define MUTT_HASH_NO_FLAGS
No flags are set.
Definition hash.h:111
int hcache_delete_raw(struct HeaderCache *hc, const char *key, size_t keylen)
Multiplexor for StoreOps::delete_record.
Definition hcache.c:754
int hcache_store_raw(struct HeaderCache *hc, const char *key, size_t keylen, void *data, size_t dlen)
Store a key / data pair.
Definition hcache.c:726
Header cache multiplexor.
#define hcache_fetch_raw_obj(hc, key, keylen, dst)
Definition lib.h:162
struct ImapAccountData * imap_adata_get(struct Mailbox *m)
Get the Account data for this mailbox.
Definition adata.c:123
Imap-specific Account data.
int imap_cmd_start(struct ImapAccountData *adata, const char *cmdstr)
Given an IMAP command, send it to the server.
Definition command.c:1133
int imap_cmd_step(struct ImapAccountData *adata)
Reads server responses from an IMAP command.
Definition command.c:1147
bool imap_code(const char *s)
Was the command successful.
Definition command.c:1274
int imap_exec(struct ImapAccountData *adata, const char *cmdstr, ImapCmdFlags flags)
Execute a command and wait for the response from the server.
Definition command.c:1322
struct ImapEmailData * imap_edata_new(void)
Create a new ImapEmailData.
Definition edata.c:56
struct ImapEmailData * imap_edata_clone(struct ImapEmailData *src)
Clone an ImapEmailData.
Definition edata.c:78
struct ImapEmailData * imap_edata_get(struct Email *e)
Get the private data for this Email.
Definition edata.c:66
Imap-specific Email data.
IMAP network mailbox.
int imap_parse_path(const char *path, struct ConnAccount *cac, char *mailbox, size_t mailboxlen)
Parse an IMAP mailbox name into ConnAccount, name.
Definition util.c:477
struct ImapMboxData * imap_mdata_get(struct Mailbox *m)
Get the Mailbox data for this mailbox.
Definition mdata.c:61
Imap-specific Mailbox data.
int imap_cache_clean(struct Mailbox *m)
Delete all the entries in the message cache.
Definition message.c:1907
static struct BodyCache * imap_bcache_open(struct Mailbox *m)
Open a message cache.
Definition message.c:81
static FILE * msg_cache_put(struct Mailbox *m, struct Email *e)
Put an email into the message cache.
Definition message.c:129
char * imap_set_flags(struct Mailbox *m, struct Email *e, char *s, bool *server_changes)
Fill the message header according to the server flags.
Definition message.c:1939
static unsigned int imap_fetch_msn_seqset(struct Buffer *buf, struct ImapAccountData *adata, bool evalhc, unsigned int msn_begin, unsigned int msn_end, unsigned int *fetch_msn_end)
Generate a sequence set.
Definition message.c:555
static int msg_parse_fetch(struct ImapHeader *h, char *s)
Handle headers returned from header fetch.
Definition message.c:311
static int imap_verify_qresync(struct Mailbox *m)
Check to see if QRESYNC got jumbled.
Definition message.c:1027
static int flush_buffer(char *buf, size_t *len, struct Connection *conn)
Write data to a connection.
Definition message.c:491
int imap_append_message(struct Mailbox *m, struct Message *msg)
Write an email back to the server.
Definition message.c:1543
static int emails_to_uid_array(struct EmailArray *ea, struct UidArray *uida)
Extract IMAP UIDs from Emails.
Definition message.c:1678
static int msg_fetch_header(struct Mailbox *m, struct ImapHeader *ih, char *buf, FILE *fp)
Import IMAP FETCH response into an ImapHeader.
Definition message.c:423
static int read_headers_condstore_qresync_updates(struct ImapAccountData *adata, unsigned int msn_end, unsigned int uid_next, unsigned long long hc_modseq, bool eval_qresync)
Retrieve updates from the server.
Definition message.c:926
static FILE * msg_cache_get(struct Mailbox *m, struct Email *e)
Get the message cache entry for an email.
Definition message.c:108
int imap_copy_messages(struct Mailbox *m, struct EmailArray *ea, const char *dest, enum MessageSaveOpt save_opt)
Server COPY messages to another folder.
Definition message.c:1703
static bool query_abort_header_download(struct ImapAccountData *adata)
Ask the user whether to abort the download.
Definition message.c:507
int imap_cache_del(struct Mailbox *m, struct Email *e)
Delete an email from the body cache.
Definition message.c:1888
static void set_changed_flag(struct Mailbox *m, struct Email *e, int local_changes, bool *server_changes, enum MessageType flag_name, bool old_hd_flag, bool new_hd_flag, bool h_flag)
Have the flags of an email changed.
Definition message.c:650
static int read_headers_fetch_new(struct Mailbox *m, unsigned int msn_begin, unsigned int msn_end, bool evalhc, unsigned int *maxuid, bool initial_download)
Retrieve new messages from the server.
Definition message.c:1104
int imap_read_headers(struct Mailbox *m, unsigned int msn_begin, unsigned int msn_end, bool initial_download)
Read headers from the server.
Definition message.c:1354
static int msg_cache_commit(struct Mailbox *m, struct Email *e)
Add to the message cache.
Definition message.c:150
static int read_headers_normal_eval_cache(struct ImapAccountData *adata, unsigned int msn_end, unsigned int uid_next, bool store_flag_updates, bool eval_condstore)
Retrieve data from the header cache.
Definition message.c:690
static char * msg_parse_flags(struct ImapHeader *h, char *s)
Read a FLAGS token into an ImapHeader.
Definition message.c:194
static int read_headers_qresync_eval_cache(struct ImapAccountData *adata, char *uid_seqset)
Retrieve data from the header cache.
Definition message.c:838
static void imap_alloc_uid_hash(struct ImapAccountData *adata, unsigned int msn_count)
Create a Hash Table for the UIDs.
Definition message.c:531
Manage IMAP messages.
Shared constants/structs that are private to IMAP.
#define IMAP_CMD_NO_FLAGS
No flags are set.
Definition private.h:71
#define IMAP_RES_RESPOND
+
Definition private.h:57
#define IMAP_EXPUNGE_PENDING
Messages on the server have been expunged.
Definition private.h:66
void imap_hcache_open(struct ImapAccountData *adata, struct ImapMboxData *mdata, bool create)
Open a header cache.
Definition util.c:302
int imap_get_literal_count(const char *buf, unsigned int *bytes)
Write number of bytes in an IMAP literal into bytes.
Definition util.c:780
#define IMAP_RES_OK
<tag> OK ...
Definition private.h:55
int imap_hcache_store_uid_seqset(struct ImapMboxData *mdata)
Store a UID Sequence Set in the header cache.
Definition util.c:418
int imap_hcache_put(struct ImapMboxData *mdata, struct Email *e)
Add an entry to the header cache.
Definition util.c:383
#define IMAP_REOPEN_ALLOW
Allow re-opening a folder upon expunge.
Definition private.h:64
#define IMAP_CAP_IMAP4
Server supports IMAP4.
Definition private.h:121
struct SeqsetIterator * mutt_seqset_iterator_new(const char *seqset)
Create a new Sequence Set Iterator.
Definition util.c:1127
#define IMAP_CAP_IMAP4REV1
Server supports IMAP4rev1.
Definition private.h:122
char * imap_hcache_get_uid_seqset(struct ImapMboxData *mdata)
Get a UID Sequence Set from the header cache.
Definition util.c:453
struct Email * imap_hcache_get(struct ImapMboxData *mdata, unsigned int uid)
Get a header cache entry by its UID.
Definition util.c:358
int mutt_seqset_iterator_next(struct SeqsetIterator *iter, unsigned int *next)
Get the next UID from a Sequence Set.
Definition util.c:1148
@ IMAP_EXEC_SUCCESS
Imap command executed or queued successfully.
Definition private.h:82
@ IMAP_EXEC_ERROR
Imap command failure.
Definition private.h:83
void mutt_seqset_iterator_free(struct SeqsetIterator **ptr)
Free a Sequence Set Iterator.
Definition util.c:1207
#define IMAP_NEWMAIL_PENDING
New mail is waiting on the server.
Definition private.h:67
void imap_cachepath(char delim, const char *mailbox, struct Buffer *dest)
Generate a cache path for a mailbox.
Definition util.c:749
void imap_error(const char *where, const char *msg)
Show an error and abort.
Definition util.c:660
#define IMAP_FLAGS_PENDING
Flags have changed on the server.
Definition private.h:68
void imap_hcache_close(struct ImapMboxData *mdata)
Close the header cache.
Definition util.c:343
char * imap_fix_path_with_delim(char delim, const char *mailbox, char *path, size_t plen)
Fix up the imap path.
Definition util.c:713
int imap_hcache_clear_uid_seqset(struct ImapMboxData *mdata)
Delete a UID Sequence Set from the header cache.
Definition util.c:439
bool imap_account_match(const struct ConnAccount *a1, const struct ConnAccount *a2)
Compare two Accounts.
Definition util.c:1095
void imap_munge_mbox_name(bool unicode, char *dest, size_t dlen, const char *src)
Quote awkward characters in a mailbox name.
Definition util.c:960
#define IMAP_RES_CONTINUE
* ...
Definition private.h:56
char * imap_next_word(char *s)
Find where the next IMAP word begins.
Definition util.c:824
#define IMAP_RES_BAD
<tag> BAD ...
Definition private.h:54
#define IMAP_CAP_CONDSTORE
RFC7162.
Definition private.h:136
#define IMAP_CMD_QUEUE
Queue a command, do not execute.
Definition private.h:73
char * imap_get_qualifier(char *buf)
Get the qualifier from a tagged response.
Definition util.c:807
void imap_close_connection(struct ImapAccountData *adata)
Close an IMAP connection.
Definition imap.c:869
void imap_expunge_mailbox(struct Mailbox *m, bool resort)
Purge messages from the server.
Definition imap.c:689
int imap_create_mailbox(struct ImapAccountData *adata, const char *mailbox)
Create a new mailbox.
Definition imap.c:448
int imap_sync_message_for_copy(struct Mailbox *m, struct Email *e, struct Buffer *cmd, enum QuadOption *err_continue)
Update server to reflect the flags of a single message.
Definition imap.c:946
int imap_read_literal(FILE *fp, struct ImapAccountData *adata, unsigned long bytes, struct Progress *progress)
Read bytes bytes from server into file.
Definition imap.c:610
Manage keymappings.
@ LL_DEBUG3
Log at debug level 3.
Definition logging2.h:46
@ LL_DEBUG2
Log at debug level 2.
Definition logging2.h:45
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:44
#define FREE(x)
Definition memory.h:62
#define MAX(a, b)
Definition memory.h:36
int imap_exec_msg_set(struct ImapAccountData *adata, const char *pre, const char *post, struct UidArray *uida)
Execute a command using a set of UIDs.
Definition msg_set.c:132
IMAP Message Sets.
void imap_msn_free(struct MSNArray *msn)
Free the cache.
Definition msn.c:62
struct Email * imap_msn_get(const struct MSNArray *msn, int idx)
Return the Email associated with an msn.
Definition msn.c:83
size_t imap_msn_highest(const struct MSNArray *msn)
Return the highest MSN in use.
Definition msn.c:72
void imap_msn_set(struct MSNArray *msn, size_t idx, struct Email *e)
Cache an Email into a given position.
Definition msn.c:95
void imap_msn_reserve(struct MSNArray *msn, size_t num)
Create / reallocate the cache.
Definition msn.c:44
IMAP MSN helper functions.
int mutt_date_make_imap(struct Buffer *buf, time_t timestamp)
Format date in IMAP style: DD-MMM-YYYY HH:MM:SS +ZZzz.
Definition date.c:810
time_t mutt_date_parse_imap(const char *s)
Parse date of the form: DD-MMM-YYYY HH:MM:SS +ZZzz.
Definition date.c:853
Convenience wrapper for the library headers.
#define FALLTHROUGH
Definition lib.h:113
#define _(a)
Definition message.h:28
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition string.c:255
int mutt_str_asprintf(char **strp, const char *fmt,...)
Definition string.c:803
size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition string.c:581
size_t mutt_istr_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix, ignoring case.
Definition string.c:244
Many unsorted constants and some structs.
MessageType
To set flags or match patterns.
Definition mutt.h:67
@ MUTT_READ
Messages that have been read.
Definition mutt.h:73
@ MUTT_OLD
Old messages.
Definition mutt.h:71
@ MUTT_PURGE
Messages to be purged (bypass trash)
Definition mutt.h:77
@ MUTT_FLAG
Flagged messages.
Definition mutt.h:79
@ MUTT_DELETE
Messages to be deleted.
Definition mutt.h:75
@ MUTT_NEW
New messages.
Definition mutt.h:70
@ MUTT_REPLIED
Messages that have been replied to.
Definition mutt.h:72
#define PATH_MAX
Definition mutt.h:42
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
NeoMutt Logging.
void mx_alloc_memory(struct Mailbox *m, int req_size)
Create storage for the emails.
Definition mx.c:1211
API for mailboxes.
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:82
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:96
Progress Bar.
@ MUTT_PROGRESS_NET
Progress tracks bytes, according to $net_inc
Definition lib.h:82
@ MUTT_PROGRESS_READ
Progress tracks elements, according to $read_inc
Definition lib.h:83
struct Progress * progress_new(enum ProgressType type, size_t size)
Create a new Progress Bar.
Definition progress.c:139
void progress_free(struct Progress **ptr)
Free a Progress Bar.
Definition progress.c:110
void progress_set_message(struct Progress *progress, const char *fmt,...) __attribute__((__format__(__printf__
bool progress_update(struct Progress *progress, size_t pos, int percent)
Update the state of the progress bar.
Definition progress.c:80
Prototypes for many functions.
QuadOption
Possible values for a quad-option.
Definition quad.h:36
@ MUTT_NO
User answered 'No', or assume 'No'.
Definition quad.h:38
@ MUTT_YES
User answered 'Yes', or assume 'Yes'.
Definition quad.h:39
Ask the user a question.
enum QuadOption query_yesorno_help(const char *prompt, enum QuadOption def, struct ConfigSubset *sub, const char *name)
Ask the user a Yes/No question offering help.
Definition question.c:353
enum QuadOption query_yesorno(const char *prompt, enum QuadOption def)
Ask the user a Yes/No question.
Definition question.c:325
#define STAILQ_INIT(head)
Definition queue.h:410
#define ASSERT(COND)
Definition signal2.h:60
volatile sig_atomic_t SigInt
true after SIGINT is received
Definition signal.c:68
#define mutt_socket_write_n(conn, buf, len)
Definition socket.h:59
#define mutt_socket_send(conn, buf)
Definition socket.h:57
#define SKIPWS(ch)
Definition string2.h:51
void * adata
Private data (for Mailbox backends)
Definition account.h:42
Local cache of email bodies.
Definition bcache.c:49
LOFF_T offset
offset where the actual data begins
Definition body.h:52
LOFF_T length
length (in bytes) of attachment
Definition body.h:53
String manipulation buffer.
Definition buffer.h:36
char * data
Pointer to data.
Definition buffer.h:37
Login details for a remote server.
Definition connaccount.h:53
struct ConnAccount account
Account details: username, password, etc.
Definition connection.h:49
The envelope/body of an email.
Definition email.h:39
bool read
Email is read.
Definition email.h:50
struct Envelope * env
Envelope information.
Definition email.h:68
void * edata
Driver-specific data.
Definition email.h:74
int lines
How many lines in the body of this message?
Definition email.h:62
struct Body * body
List of MIME parts.
Definition email.h:69
bool active
Message is not to be removed.
Definition email.h:76
bool old
Email is seen, but unread.
Definition email.h:49
void(* edata_free)(void **ptr)
Definition email.h:90
bool changed
Email has been edited.
Definition email.h:77
bool attach_del
Has an attachment marked for deletion.
Definition email.h:99
bool flagged
Marked important?
Definition email.h:47
bool replied
Email has been replied to.
Definition email.h:51
struct TagList tags
For drivers that support server tagging.
Definition email.h:72
bool deleted
Email is deleted.
Definition email.h:78
int index
The absolute (unsorted) message number.
Definition email.h:110
time_t received
Time when the message was placed in the mailbox.
Definition email.h:61
The header of an Email.
Definition envelope.h:57
IMAP-specific Account data -.
Definition adata.h:40
char delim
Path delimiter.
Definition adata.h:75
bool qresync
true, if QRESYNC is successfully ENABLE'd
Definition adata.h:63
bool unicode
If true, we can send UTF-8, and the server will use UTF8 rather than mUTF7.
Definition adata.h:62
ImapCapFlags capabilities
Capability flags.
Definition adata.h:55
struct Mailbox * mailbox
Current selected mailbox.
Definition adata.h:76
char * buf
Definition adata.h:59
struct Connection * conn
Connection to IMAP server.
Definition adata.h:41
IMAP-specific Email data -.
Definition edata.h:35
bool parsed
Definition edata.h:43
unsigned int uid
32-bit Message UID
Definition edata.h:45
unsigned int msn
Message Sequence Number.
Definition edata.h:46
char * flags_remote
Definition edata.h:49
bool deleted
Email has been deleted.
Definition edata.h:39
bool old
Email has been seen.
Definition edata.h:38
bool read
Email has been read.
Definition edata.h:37
bool flagged
Email has been flagged.
Definition edata.h:40
bool replied
Email has been replied to.
Definition edata.h:41
char * flags_system
Definition edata.h:48
IMAP-specific header.
Definition message.h:34
time_t received
Definition message.h:37
struct ImapEmailData * edata
Definition message.h:35
long content_length
Definition message.h:38
IMAP-specific Mailbox data -.
Definition mdata.h:40
ImapOpenFlags reopen
Flags, e.g. IMAP_REOPEN_ALLOW.
Definition mdata.h:45
unsigned int uid_next
Definition mdata.h:52
struct HeaderCache * hcache
Email header cache.
Definition mdata.h:63
struct BodyCache * bcache
Email body cache.
Definition mdata.h:61
unsigned int new_mail_count
Set when EXISTS notifies of new mail.
Definition mdata.h:47
struct HashTable * uid_hash
Hash Table: "uid" -> Email.
Definition mdata.h:59
unsigned long long modseq
Definition mdata.h:53
char * munge_name
Munged version of the mailbox name.
Definition mdata.h:42
uint32_t uidvalidity
Definition mdata.h:51
char * name
Mailbox name.
Definition mdata.h:41
A mailbox.
Definition mailbox.h:79
int vcount
The number of virtual messages.
Definition mailbox.h:99
bool changed
Mailbox has been modified.
Definition mailbox.h:110
int msg_new
Number of new messages.
Definition mailbox.h:92
int msg_count
Total number of messages.
Definition mailbox.h:88
void * mdata
Driver specific data.
Definition mailbox.h:132
struct HashTable * subj_hash
Hash Table: "subject" -> Email.
Definition mailbox.h:124
struct Email ** emails
Array of Emails.
Definition mailbox.h:96
struct HashTable * id_hash
Hash Table: "message-id" -> Email.
Definition mailbox.h:123
int msg_deleted
Number of deleted messages.
Definition mailbox.h:93
off_t size
Size of the Mailbox.
Definition mailbox.h:84
struct HashTable * label_hash
Hash Table: "x-labels" -> Email.
Definition mailbox.h:125
int msg_flagged
Number of flagged messages.
Definition mailbox.h:90
bool readonly
Don't allow changes to the mailbox.
Definition mailbox.h:116
int msg_tagged
How many messages are tagged?
Definition mailbox.h:94
bool verbose
Display status messages?
Definition mailbox.h:117
int msg_unread
Number of unread messages.
Definition mailbox.h:89
A local copy of an email.
Definition message.h:34
FILE * fp
pointer to the message data
Definition message.h:35
char * path
path to temp file
Definition message.h:36
struct Message::@264267271004327071125374067057142037276212342100 flags
Flags for the Message.
bool draft
Message has been read.
Definition message.h:44
bool replied
Message has been replied to.
Definition message.h:43
time_t received
Time at which this message was received.
Definition message.h:46
bool flagged
Message is flagged.
Definition message.h:42
bool read
Message has been read.
Definition message.h:41
Container for Accounts, Notifications.
Definition neomutt.h:43
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:47
UID Sequence Set Iterator.
Definition private.h:169
bool driver_tags_replace(struct TagList *tl, const char *tags)
Replace all tags.
Definition tags.c:201
#define buf_mktemp(buf)
Definition tmp.h:33