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