NeoMutt  2025-12-11-911-gd8d604
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_NONE);
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 */
716 progress = progress_new(MUTT_PROGRESS_READ, msn_end);
717 progress_set_message(progress, _("Evaluating cache..."));
718 }
719
720 /* If we are using CONDSTORE's "FETCH CHANGEDSINCE", then we keep
721 * the flags in the header cache, and update them further below.
722 * Otherwise, we fetch the current state of the flags here. */
723 snprintf(buf, sizeof(buf), "UID FETCH 1:%u (UID%s)", uid_next - 1,
724 eval_condstore ? "" : " FLAGS");
725
726 imap_cmd_start(adata, buf);
727
729 int mfhrc = 0;
730 struct ImapHeader h = { 0 };
731 for (int msgno = 1; rc == IMAP_RES_CONTINUE; msgno++)
732 {
734 goto fail;
735
736 progress_update(progress, msgno, -1);
737
738 memset(&h, 0, sizeof(h));
739 h.edata = imap_edata_new();
740 do
741 {
742 rc = imap_cmd_step(adata);
743 if (rc != IMAP_RES_CONTINUE)
744 break;
745
746 mfhrc = msg_fetch_header(m, &h, adata->buf, NULL);
747 if (mfhrc < 0)
748 continue;
749
750 if (!h.edata->uid)
751 {
752 mutt_debug(LL_DEBUG2, "skipping hcache FETCH response for message number %d missing a UID\n",
753 h.edata->msn);
754 continue;
755 }
756
757 if ((h.edata->msn < 1) || (h.edata->msn > msn_end))
758 {
759 mutt_debug(LL_DEBUG1, "skipping hcache FETCH response for unknown message number %d\n",
760 h.edata->msn);
761 continue;
762 }
763
764 if (imap_msn_get(&mdata->msn, h.edata->msn - 1))
765 {
766 mutt_debug(LL_DEBUG2, "skipping hcache FETCH for duplicate message %d\n",
767 h.edata->msn);
768 continue;
769 }
770
771 struct Email *e = imap_hcache_get(mdata, h.edata->uid);
772 m->emails[idx] = e;
773 if (e)
774 {
775 imap_msn_set(&mdata->msn, h.edata->msn - 1, e);
776 mutt_hash_int_insert(mdata->uid_hash, h.edata->uid, e);
777
778 e->index = h.edata->uid;
779 /* messages which have not been expunged are ACTIVE (borrowed from mh
780 * folders) */
781 e->active = true;
782 e->changed = false;
783 if (eval_condstore)
784 {
785 h.edata->read = e->read;
786 h.edata->old = e->old;
787 h.edata->deleted = e->deleted;
788 h.edata->flagged = e->flagged;
789 h.edata->replied = e->replied;
790 }
791 else
792 {
793 e->read = h.edata->read;
794 e->old = h.edata->old;
795 e->deleted = h.edata->deleted;
796 e->flagged = h.edata->flagged;
797 e->replied = h.edata->replied;
798 }
799
800 /* mailbox->emails[msgno]->received is restored from hcache_fetch_email() */
801 e->edata = h.edata;
803
804 /* We take a copy of the tags so we can split the string */
805 char *tags_copy = mutt_str_dup(h.edata->flags_remote);
806 driver_tags_replace(&e->tags, tags_copy);
807 FREE(&tags_copy);
808
809 m->msg_count++;
810 mailbox_size_add(m, e);
811
812 /* If this is the first time we are fetching, we need to
813 * store the current state of flags back into the header cache */
814 if (!eval_condstore && store_flag_updates)
815 imap_hcache_put(mdata, e);
816
817 h.edata = NULL;
818 idx++;
819 }
820 } while (mfhrc == -1);
821
822 imap_edata_free((void **) &h.edata);
823
824 if ((mfhrc < -1) || ((rc != IMAP_RES_CONTINUE) && (rc != IMAP_RES_OK)))
825 goto fail;
826 }
827
828 rc = 0;
829fail:
830 progress_free(&progress);
831 return rc;
832}
833
848 struct Mailbox *m, char *uid_seqset)
849{
850 int rc;
851 unsigned int uid = 0;
852
853 mutt_debug(LL_DEBUG2, "Reading uid seqset from header cache\n");
854 unsigned int msn = 1;
855 struct ImapMboxData *mdata = imap_mdata_get(m);
856 if (!mdata)
857 return -1;
858
859 if (m->verbose)
860 mutt_message(_("Evaluating cache..."));
861
862 struct SeqsetIterator *iter = mutt_seqset_iterator_new(uid_seqset);
863 if (!iter)
864 return -1;
865
866 while ((rc = mutt_seqset_iterator_next(iter, &uid)) == 0)
867 {
868 /* The seqset may contain more headers than the fetch request, so
869 * we need to watch and reallocate the context and msn_index */
870 imap_msn_reserve(&mdata->msn, msn);
871
872 struct Email *e = imap_hcache_get(mdata, uid);
873 if (e)
874 {
875 imap_msn_set(&mdata->msn, msn - 1, e);
876
878
880 e->edata = edata;
882
883 e->index = uid;
884 e->active = true;
885 e->changed = false;
886 edata->read = e->read;
887 edata->old = e->old;
888 edata->deleted = e->deleted;
889 edata->flagged = e->flagged;
890 edata->replied = e->replied;
891
892 edata->msn = msn;
893 edata->uid = uid;
895
896 mailbox_size_add(m, e);
897 m->emails[m->msg_count++] = e;
898
899 msn++;
900 }
901 else if (!uid)
902 {
903 /* A non-zero uid missing from the header cache is either the
904 * result of an expunged message (not recorded in the uid seqset)
905 * or a hole in the header cache.
906 *
907 * We have to assume it's an earlier expunge and compact the msn's
908 * in that case, because cmd_parse_vanished() won't find it in the
909 * uid_hash and decrement later msn's there.
910 *
911 * Thus we only increment the uid if the uid was 0: an actual
912 * stored "blank" in the uid seqset.
913 */
914 msn++;
915 }
916 }
917
919
920 return rc;
921}
922
937 struct Mailbox *m, unsigned int msn_end,
938 unsigned int uid_next,
939 unsigned long long hc_modseq, bool eval_qresync)
940{
941 struct Progress *progress = NULL;
942 char buf[1024] = { 0 };
943 unsigned int header_msn = 0;
944
945 struct ImapMboxData *mdata = imap_mdata_get(m);
946 if (!mdata)
947 return -1;
948
949 if (m->verbose)
950 {
951 /* L10N: Fetching IMAP flag changes, using the CONDSTORE extension */
953 progress = progress_new(MUTT_PROGRESS_READ, msn_end);
954 progress_set_message(progress, _("Fetching flag updates..."));
955 }
956
957 snprintf(buf, sizeof(buf), "UID FETCH 1:%u (FLAGS) (CHANGEDSINCE %llu%s)",
958 uid_next - 1, hc_modseq, eval_qresync ? " VANISHED" : "");
959
960 imap_cmd_start(adata, buf);
961
962 int rc = IMAP_RES_CONTINUE;
963 for (int msgno = 1; rc == IMAP_RES_CONTINUE; msgno++)
964 {
966 goto fail;
967
968 progress_update(progress, msgno, -1);
969
970 /* cmd_parse_fetch will update the flags */
971 rc = imap_cmd_step(adata);
972 if (rc != IMAP_RES_CONTINUE)
973 break;
974
975 /* so we just need to grab the header and persist it back into
976 * the header cache */
977 char *fetch_buf = adata->buf;
978 if (fetch_buf[0] != '*')
979 continue;
980
981 fetch_buf = imap_next_word(fetch_buf);
982 if (!mutt_isdigit(*fetch_buf) || !mutt_str_atoui(fetch_buf, &header_msn))
983 continue;
984
985 if ((header_msn < 1) || (header_msn > msn_end) ||
986 !imap_msn_get(&mdata->msn, header_msn - 1))
987 {
988 mutt_debug(LL_DEBUG1, "skipping CONDSTORE flag update for unknown message number %u\n",
989 header_msn);
990 continue;
991 }
992
993 imap_hcache_put(mdata, imap_msn_get(&mdata->msn, header_msn - 1));
994 }
995
996 if (rc != IMAP_RES_OK)
997 goto fail;
998
999 /* The IMAP flag setting as part of cmd_parse_fetch() ends up
1000 * flipping these on. */
1001 mdata->check_status &= ~IMAP_FLAGS_PENDING;
1002 m->changed = false;
1003
1004 /* VANISHED handling: we need to empty out the messages */
1005 if (mdata->reopen & IMAP_EXPUNGE_PENDING)
1006 {
1008 imap_expunge_mailbox(m, false);
1009
1010 imap_hcache_open(adata, mdata, false);
1011 mdata->reopen &= ~IMAP_EXPUNGE_PENDING;
1012 }
1013
1014 /* undo expunge count updates.
1015 * mview_update() will do this at the end of the header fetch. */
1016 m->vcount = 0;
1017 m->msg_tagged = 0;
1018 m->msg_deleted = 0;
1019 m->msg_new = 0;
1020 m->msg_unread = 0;
1021 m->msg_flagged = 0;
1022 m->changed = false;
1023
1024 rc = 0;
1025fail:
1026 progress_free(&progress);
1027 return rc;
1028}
1029
1038static int imap_verify_qresync(struct Mailbox *m)
1039{
1040 ASSERT(m);
1042 struct ImapMboxData *mdata = imap_mdata_get(m);
1043 if (!adata || (adata->mailbox != m) || !mdata)
1044 return -1;
1045
1046 const size_t max_msn = imap_msn_highest(&mdata->msn);
1047
1048 unsigned int msn;
1049 unsigned int uid;
1050 struct Email *e = NULL;
1051 struct Email *uidh = NULL;
1052
1053 for (int i = 0; i < m->msg_count; i++)
1054 {
1055 e = m->emails[i];
1056 const struct ImapEmailData *edata = imap_edata_get(e);
1057 if (!edata)
1058 goto fail;
1059
1060 msn = imap_edata_get(e)->msn;
1061 uid = imap_edata_get(e)->uid;
1062
1063 if ((msn < 1) || (msn > max_msn) || imap_msn_get(&mdata->msn, msn - 1) != e)
1064 goto fail;
1065
1066 uidh = (struct Email *) mutt_hash_int_find(mdata->uid_hash, uid);
1067 if (uidh != e)
1068 goto fail;
1069 }
1070
1071 return 0;
1072
1073fail:
1074 imap_msn_free(&mdata->msn);
1075 mutt_hash_free(&mdata->uid_hash);
1079
1080 for (int i = 0; i < m->msg_count; i++)
1081 {
1082 if (m->emails[i] && m->emails[i]->edata)
1083 imap_edata_free(&m->emails[i]->edata);
1084 email_free(&m->emails[i]);
1085 }
1086 m->msg_count = 0;
1087 m->size = 0;
1088 hcache_delete_raw(mdata->hcache, "MODSEQ", 6);
1090 imap_hcache_close(mdata);
1091
1092 if (m->verbose)
1093 {
1094 /* L10N: After opening an IMAP mailbox using QRESYNC, Mutt performs a quick
1095 sanity check. If that fails, Mutt reopens the mailbox using a normal
1096 download. */
1097 mutt_error(_("QRESYNC failed. Reopening mailbox."));
1098 }
1099 return -1;
1100}
1101
1102#endif /* USE_HCACHE */
1103
1115static int read_headers_fetch_new(struct Mailbox *m, unsigned int msn_begin,
1116 unsigned int msn_end, bool evalhc,
1117 unsigned int *maxuid, bool initial_download)
1118{
1119 int rc = -1;
1120 unsigned int fetch_msn_end = 0;
1121 struct Progress *progress = NULL;
1122 char *hdrreq = NULL;
1123 struct Buffer *tempfile = NULL;
1124 FILE *fp = NULL;
1125 struct ImapHeader h = { 0 };
1126 struct Buffer *buf = NULL;
1127 static const char *const want_headers = "DATE FROM SENDER SUBJECT TO CC MESSAGE-ID REFERENCES "
1128 "CONTENT-TYPE CONTENT-DESCRIPTION IN-REPLY-TO REPLY-TO "
1129 "LINES LIST-POST LIST-SUBSCRIBE LIST-UNSUBSCRIBE X-LABEL "
1130 "X-ORIGINAL-TO";
1131
1133 struct ImapMboxData *mdata = imap_mdata_get(m);
1134 struct ImapEmailData *edata = NULL;
1135
1136 if (!adata || (adata->mailbox != m) || !mdata)
1137 return -1;
1138
1139 struct Buffer *hdr_list = buf_pool_get();
1140 buf_strcpy(hdr_list, want_headers);
1141 const char *const c_imap_headers = cs_subset_string(NeoMutt->sub, "imap_headers");
1142 if (c_imap_headers)
1143 {
1144 buf_addch(hdr_list, ' ');
1145 buf_addstr(hdr_list, c_imap_headers);
1146 }
1147#ifdef USE_AUTOCRYPT
1148 const bool c_autocrypt = cs_subset_bool(NeoMutt->sub, "autocrypt");
1149 if (c_autocrypt)
1150 {
1151 buf_addch(hdr_list, ' ');
1152 buf_addstr(hdr_list, "AUTOCRYPT");
1153 }
1154#endif
1155
1156 if (adata->capabilities & IMAP_CAP_IMAP4REV1)
1157 {
1158 mutt_str_asprintf(&hdrreq, "BODY.PEEK[HEADER.FIELDS (%s)]", buf_string(hdr_list));
1159 }
1160 else if (adata->capabilities & IMAP_CAP_IMAP4)
1161 {
1162 mutt_str_asprintf(&hdrreq, "RFC822.HEADER.LINES (%s)", buf_string(hdr_list));
1163 }
1164 else
1165 { /* Unable to fetch headers for lower versions */
1166 mutt_error(_("Unable to fetch headers from this IMAP server version"));
1167 goto bail;
1168 }
1169
1170 buf_pool_release(&hdr_list);
1171
1172 /* instead of downloading all headers and then parsing them, we parse them
1173 * as they come in. */
1174 tempfile = buf_pool_get();
1175 buf_mktemp(tempfile);
1176 fp = mutt_file_fopen(buf_string(tempfile), "w+");
1177 if (!fp)
1178 {
1179 mutt_error(_("Could not create temporary file %s"), buf_string(tempfile));
1180 goto bail;
1181 }
1182 unlink(buf_string(tempfile));
1183 buf_pool_release(&tempfile);
1184
1185 if (m->verbose)
1186 {
1188 progress = progress_new(MUTT_PROGRESS_READ, msn_end);
1189 progress_set_message(progress, _("Fetching message headers..."));
1190 }
1191
1192 buf = buf_pool_get();
1193
1194 /* NOTE:
1195 * The (fetch_msn_end < msn_end) used to be important to prevent
1196 * an infinite loop, in the event the server did not return all
1197 * the headers (due to a pending expunge, for example).
1198 *
1199 * I believe the new chunking imap_fetch_msn_seqset()
1200 * implementation and "msn_begin = fetch_msn_end + 1" assignment
1201 * at the end of the loop makes the comparison unneeded, but to be
1202 * cautious I'm keeping it.
1203 */
1204 edata = imap_edata_new();
1205 while ((fetch_msn_end < msn_end) &&
1206 imap_fetch_msn_seqset(buf, adata, m, evalhc, msn_begin, msn_end, &fetch_msn_end))
1207 {
1208 char *cmd = NULL;
1209 mutt_str_asprintf(&cmd, "FETCH %s (UID FLAGS INTERNALDATE RFC822.SIZE %s)",
1210 buf_string(buf), hdrreq);
1211 imap_cmd_start(adata, cmd);
1212 FREE(&cmd);
1213
1214 int msgno = msn_begin;
1215
1216 while (true)
1217 {
1218 rewind(fp);
1219 memset(&h, 0, sizeof(h));
1220 h.edata = edata;
1221
1222 if (initial_download && SigInt && query_abort_header_download(adata))
1223 {
1224 goto bail;
1225 }
1226
1227 const int rc2 = imap_cmd_step(adata);
1228 if (rc2 != IMAP_RES_CONTINUE)
1229 {
1230 if (rc2 != IMAP_RES_OK)
1231 {
1232 goto bail;
1233 }
1234 break;
1235 }
1236
1237 switch (msg_fetch_header(m, &h, adata->buf, fp))
1238 {
1239 case 0:
1240 break;
1241 case -1:
1242 continue;
1243 case -2:
1244 goto bail;
1245 }
1246
1247 if (!ftello(fp))
1248 {
1249 mutt_debug(LL_DEBUG2, "ignoring fetch response with no body\n");
1250 continue;
1251 }
1252
1253 /* make sure we don't get remnants from older larger message headers */
1254 fputs("\n\n", fp);
1255
1256 if ((h.edata->msn < 1) || (h.edata->msn > fetch_msn_end))
1257 {
1258 mutt_debug(LL_DEBUG1, "skipping FETCH response for unknown message number %d\n",
1259 h.edata->msn);
1260 continue;
1261 }
1262
1263 /* May receive FLAGS updates in a separate untagged response */
1264 if (imap_msn_get(&mdata->msn, h.edata->msn - 1))
1265 {
1266 mutt_debug(LL_DEBUG2, "skipping FETCH response for duplicate message %d\n",
1267 h.edata->msn);
1268 continue;
1269 }
1270
1271 progress_update(progress, msgno++, -1);
1272
1273 struct Email *e = email_new();
1275
1276 m->emails[m->msg_count++] = e;
1277
1278 imap_msn_set(&mdata->msn, h.edata->msn - 1, e);
1279 mutt_hash_int_insert(mdata->uid_hash, h.edata->uid, e);
1280
1281 e->index = h.edata->uid;
1282 /* messages which have not been expunged are ACTIVE (borrowed from mh
1283 * folders) */
1284 e->active = true;
1285 e->changed = false;
1286 e->read = h.edata->read;
1287 e->old = h.edata->old;
1288 e->deleted = h.edata->deleted;
1289 e->flagged = h.edata->flagged;
1290 e->replied = h.edata->replied;
1291 e->received = h.received;
1292 e->edata = (void *) imap_edata_clone(h.edata);
1294 STAILQ_INIT(&e->tags);
1295
1296 /* We take a copy of the tags so we can split the string */
1297 char *tags_copy = mutt_str_dup(h.edata->flags_remote);
1298 driver_tags_replace(&e->tags, tags_copy);
1299 FREE(&tags_copy);
1300
1301 if (*maxuid < h.edata->uid)
1302 *maxuid = h.edata->uid;
1303
1304 rewind(fp);
1305 /* NOTE: if Date: header is missing, mutt_rfc822_read_header depends
1306 * on h.received being set */
1307 e->env = mutt_rfc822_read_header(fp, e, false, false);
1308 /* body built as a side-effect of mutt_rfc822_read_header */
1309 e->body->length = h.content_length;
1310 mailbox_size_add(m, e);
1311
1312#ifdef USE_HCACHE
1313 imap_hcache_put(mdata, e);
1314#endif /* USE_HCACHE */
1315 }
1316
1317 /* In case we get new mail while fetching the headers. */
1318 if (mdata->reopen & IMAP_NEWMAIL_PENDING)
1319 {
1320 msn_end = mdata->new_mail_count;
1321 mx_alloc_memory(m, msn_end);
1322 imap_msn_reserve(&mdata->msn, msn_end);
1323 mdata->reopen &= ~IMAP_NEWMAIL_PENDING;
1324 mdata->new_mail_count = 0;
1325 }
1326
1327 /* Note: RFC3501 section 7.4.1 and RFC7162 section 3.2.10.2 say we
1328 * must not get any EXPUNGE/VANISHED responses in the middle of a
1329 * FETCH, nor when no command is in progress (e.g. between the
1330 * chunked FETCH commands). We previously tried to be robust by
1331 * setting:
1332 * msn_begin = mdata->max_msn + 1;
1333 * but with chunking and header cache holes this
1334 * may not be correct. So here we must assume the msn values have
1335 * not been altered during or after the fetch. */
1336 msn_begin = fetch_msn_end + 1;
1337 }
1338
1339 rc = 0;
1340
1341bail:
1342 buf_pool_release(&hdr_list);
1343 buf_pool_release(&buf);
1344 buf_pool_release(&tempfile);
1345 mutt_file_fclose(&fp);
1346 FREE(&hdrreq);
1347 imap_edata_free((void **) &edata);
1348 progress_free(&progress);
1349
1350 return rc;
1351}
1352
1366int imap_read_headers(struct Mailbox *m, unsigned int msn_begin,
1367 unsigned int msn_end, bool initial_download)
1368{
1369 unsigned int maxuid = 0;
1370 int rc = -1;
1371 bool evalhc = false;
1372
1373#ifdef USE_HCACHE
1374 uint32_t uidvalidity = 0;
1375 unsigned int uid_next = 0;
1376 unsigned long long modseq = 0;
1377 bool has_condstore = false;
1378 bool has_qresync = false;
1379 bool eval_condstore = false;
1380 bool eval_qresync = false;
1381 char *uid_seqset = NULL;
1382 const unsigned int msn_begin_save = msn_begin;
1383#endif /* USE_HCACHE */
1384
1386 struct ImapMboxData *mdata = imap_mdata_get(m);
1387 if (!adata || (adata->mailbox != m) || !mdata)
1388 return -1;
1389
1390#ifdef USE_HCACHE
1391retry:
1392#endif /* USE_HCACHE */
1393
1394 /* make sure context has room to hold the mailbox */
1395 mx_alloc_memory(m, msn_end);
1396 imap_msn_reserve(&mdata->msn, msn_end);
1397 imap_alloc_uid_hash(adata, m, msn_end);
1398
1400 mdata->new_mail_count = 0;
1401
1402#ifdef USE_HCACHE
1403 imap_hcache_open(adata, mdata, true);
1404
1405 if (mdata->hcache && initial_download)
1406 {
1407 hcache_fetch_raw_obj(mdata->hcache, "UIDVALIDITY", 11, &uidvalidity);
1408 hcache_fetch_raw_obj(mdata->hcache, "UIDNEXT", 7, &uid_next);
1409 if (mdata->modseq)
1410 {
1411 const bool c_imap_condstore = cs_subset_bool(NeoMutt->sub, "imap_condstore");
1412 if ((adata->capabilities & IMAP_CAP_CONDSTORE) && c_imap_condstore)
1413 has_condstore = true;
1414
1415 /* If IMAP_CAP_QRESYNC and ImapQResync then NeoMutt sends ENABLE QRESYNC.
1416 * If we receive an ENABLED response back, then adata->qresync is set. */
1417 if (adata->qresync)
1418 has_qresync = true;
1419 }
1420
1421 if (uidvalidity && uid_next && (uidvalidity == mdata->uidvalidity))
1422 {
1423 evalhc = true;
1424 if (hcache_fetch_raw_obj(mdata->hcache, "MODSEQ", 6, &modseq))
1425 {
1426 if (has_qresync)
1427 {
1428 uid_seqset = imap_hcache_get_uid_seqset(mdata);
1429 if (uid_seqset)
1430 eval_qresync = true;
1431 }
1432
1433 if (!eval_qresync && has_condstore)
1434 eval_condstore = true;
1435 }
1436 }
1437 }
1438 if (evalhc)
1439 {
1440 if (eval_qresync)
1441 {
1442 if (read_headers_qresync_eval_cache(adata, m, uid_seqset) < 0)
1443 goto bail;
1444 }
1445 else
1446 {
1447 if (read_headers_normal_eval_cache(adata, m, msn_end, uid_next,
1448 has_condstore || has_qresync, eval_condstore) < 0)
1449 goto bail;
1450 }
1451
1452 if ((eval_condstore || eval_qresync) && (modseq != mdata->modseq))
1453 {
1454 if (read_headers_condstore_qresync_updates(adata, m, msn_end, uid_next,
1455 modseq, eval_qresync) < 0)
1456 {
1457 goto bail;
1458 }
1459 }
1460
1461 /* Look for the first empty MSN and start there */
1462 while (msn_begin <= msn_end)
1463 {
1464 if (!imap_msn_get(&mdata->msn, msn_begin - 1))
1465 break;
1466 msn_begin++;
1467 }
1468 }
1469#endif /* USE_HCACHE */
1470
1471 if (read_headers_fetch_new(m, msn_begin, msn_end, evalhc, &maxuid, initial_download) < 0)
1472 goto bail;
1473
1474#ifdef USE_HCACHE
1475 if (eval_qresync && initial_download)
1476 {
1477 if (imap_verify_qresync(m) != 0)
1478 {
1479 eval_qresync = false;
1480 eval_condstore = false;
1481 evalhc = false;
1482 modseq = 0;
1483 maxuid = 0;
1484 FREE(&uid_seqset);
1485 uidvalidity = 0;
1486 uid_next = 0;
1487 msn_begin = msn_begin_save;
1488
1489 goto retry;
1490 }
1491 }
1492#endif /* USE_HCACHE */
1493
1494 if (maxuid && (mdata->uid_next < maxuid + 1))
1495 mdata->uid_next = maxuid + 1;
1496
1497#ifdef USE_HCACHE
1498 hcache_store_raw(mdata->hcache, "UIDVALIDITY", 11, &mdata->uidvalidity,
1499 sizeof(mdata->uidvalidity));
1500 if (maxuid && (mdata->uid_next < maxuid + 1))
1501 {
1502 mutt_debug(LL_DEBUG2, "Overriding UIDNEXT: %u -> %u\n", mdata->uid_next, maxuid + 1);
1503 mdata->uid_next = maxuid + 1;
1504 }
1505 if (mdata->uid_next > 1)
1506 {
1507 hcache_store_raw(mdata->hcache, "UIDNEXT", 7, &mdata->uid_next, sizeof(mdata->uid_next));
1508 }
1509
1510 /* We currently only sync CONDSTORE and QRESYNC on the initial download.
1511 * To do it more often, we'll need to deal with flag updates combined with
1512 * unsync'ed local flag changes. We'll also need to properly sync flags to
1513 * the header cache on close. I'm not sure it's worth the added complexity. */
1514 if (initial_download)
1515 {
1516 if (has_condstore || has_qresync)
1517 {
1518 hcache_store_raw(mdata->hcache, "MODSEQ", 6, &mdata->modseq, sizeof(mdata->modseq));
1519 }
1520 else
1521 {
1522 hcache_delete_raw(mdata->hcache, "MODSEQ", 6);
1523 }
1524
1525 if (has_qresync)
1527 else
1529 }
1530#endif /* USE_HCACHE */
1531
1532 /* TODO: it's not clear to me why we are calling mx_alloc_memory yet again. */
1534
1535 mdata->reopen |= IMAP_REOPEN_ALLOW;
1536
1537 rc = msn_end;
1538
1539bail:
1540#ifdef USE_HCACHE
1542 FREE(&uid_seqset);
1543#endif /* USE_HCACHE */
1544
1545 return rc;
1546}
1547
1555int imap_append_message(struct Mailbox *m, struct Message *msg)
1556{
1557 if (!m || !msg)
1558 return -1;
1559
1561 struct ImapMboxData *mdata = imap_mdata_get(m);
1562 if (!adata || !mdata)
1563 return -1;
1564
1565 FILE *fp = NULL;
1566 char buf[2048] = { 0 };
1567 struct Buffer *internaldate = NULL;
1568 struct Buffer *imap_flags = NULL;
1569 size_t len;
1570 struct Progress *progress = NULL;
1571 size_t sent;
1572 int c, last;
1573 int rc;
1574
1575 fp = mutt_file_fopen(msg->path, "r");
1576 if (!fp)
1577 {
1578 mutt_perror("%s", msg->path);
1579 goto fail;
1580 }
1581
1582 /* currently we set the \Seen flag on all messages, but probably we
1583 * should scan the message Status header for flag info. Since we're
1584 * already rereading the whole file for length it isn't any more
1585 * expensive (it'd be nice if we had the file size passed in already
1586 * by the code that writes the file, but that's a lot of changes.
1587 * Ideally we'd have an Email structure with flag info here... */
1588 for (last = EOF, len = 0; (c = fgetc(fp)) != EOF; last = c)
1589 {
1590 if ((c == '\n') && (last != '\r'))
1591 len++;
1592
1593 len++;
1594 }
1595 rewind(fp);
1596
1597 if (m->verbose)
1598 {
1600 progress = progress_new(MUTT_PROGRESS_NET, len);
1601 progress_set_message(progress, _("Uploading message..."));
1602 }
1603
1604 internaldate = buf_pool_get();
1605 mutt_date_make_imap(internaldate, msg->received);
1606
1607 imap_flags = buf_pool_get();
1608
1609 if (msg->flags.read)
1610 buf_addstr(imap_flags, " \\Seen");
1611 if (msg->flags.replied)
1612 buf_addstr(imap_flags, " \\Answered");
1613 if (msg->flags.flagged)
1614 buf_addstr(imap_flags, " \\Flagged");
1615 if (msg->flags.draft)
1616 buf_addstr(imap_flags, " \\Draft");
1617
1618 snprintf(buf, sizeof(buf), "APPEND %s (%s) \"%s\" {%lu}", mdata->munge_name,
1619 imap_flags->data + 1, buf_string(internaldate), (unsigned long) len);
1620 buf_pool_release(&internaldate);
1621
1622 imap_cmd_start(adata, buf);
1623
1624 do
1625 {
1626 rc = imap_cmd_step(adata);
1627 } while (rc == IMAP_RES_CONTINUE);
1628
1629 if (rc != IMAP_RES_RESPOND)
1630 goto cmd_step_fail;
1631
1632 for (last = EOF, sent = len = 0; (c = fgetc(fp)) != EOF; last = c)
1633 {
1634 if ((c == '\n') && (last != '\r'))
1635 buf[len++] = '\r';
1636
1637 buf[len++] = c;
1638
1639 if (len > sizeof(buf) - 3)
1640 {
1641 sent += len;
1642 if (flush_buffer(buf, &len, adata->conn) < 0)
1643 goto fail;
1644 progress_update(progress, sent, -1);
1645 }
1646 }
1647
1648 if (len > 0)
1649 if (flush_buffer(buf, &len, adata->conn) < 0)
1650 goto fail;
1651
1652 if (mutt_socket_send(adata->conn, "\r\n") < 0)
1653 goto fail;
1654 mutt_file_fclose(&fp);
1655
1656 do
1657 {
1658 rc = imap_cmd_step(adata);
1659 } while (rc == IMAP_RES_CONTINUE);
1660
1661 if (rc != IMAP_RES_OK)
1662 goto cmd_step_fail;
1663
1664 progress_free(&progress);
1665 buf_pool_release(&imap_flags);
1666 return 0;
1667
1668cmd_step_fail:
1669 mutt_debug(LL_DEBUG1, "command failed: %s\n", adata->buf);
1670 if (rc != IMAP_RES_BAD)
1671 {
1672 char *pc = imap_next_word(adata->buf); /* skip sequence number or token */
1673 pc = imap_next_word(pc); /* skip response code */
1674 if (*pc != '\0')
1675 mutt_error("%s", pc);
1676 }
1677
1678fail:
1679 mutt_file_fclose(&fp);
1680 progress_free(&progress);
1681 buf_pool_release(&imap_flags);
1682 return -1;
1683}
1684
1691static int emails_to_uid_array(struct EmailArray *ea, struct UidArray *uida)
1692{
1693 struct Email **ep = NULL;
1694 ARRAY_FOREACH(ep, ea)
1695 {
1696 struct Email *e = *ep;
1697 struct ImapEmailData *edata = imap_edata_get(e);
1698
1699 ARRAY_ADD(uida, edata->uid);
1700 }
1701 ARRAY_SORT(uida, imap_sort_uid, NULL);
1702
1703 return ARRAY_SIZE(uida);
1704}
1705
1716int imap_copy_messages(struct Mailbox *m, struct EmailArray *ea,
1717 const char *dest, enum MessageSaveOpt save_opt)
1718{
1719 if (!m || !ea || ARRAY_EMPTY(ea) || !dest)
1720 return -1;
1721
1723 if (!adata)
1724 return -1;
1725
1726 char buf[PATH_MAX] = { 0 };
1727 char mbox[PATH_MAX] = { 0 };
1728 char mmbox[PATH_MAX] = { 0 };
1729 char prompt[PATH_MAX + 64];
1730 int rc;
1731 struct ConnAccount cac = { { 0 } };
1732 enum QuadOption err_continue = MUTT_NO;
1733 int triedcreate = 0;
1734 struct Email *e_cur = *ARRAY_GET(ea, 0);
1735 bool single = (ARRAY_SIZE(ea) == 1);
1736
1737 if (single && e_cur->attach_del)
1738 {
1739 mutt_debug(LL_DEBUG3, "#1 Message contains attachments to be deleted\n");
1740 return 1;
1741 }
1742
1743 if (imap_parse_path(dest, &cac, buf, sizeof(buf)) != 0)
1744 {
1745 mutt_debug(LL_DEBUG1, "bad destination %s\n", dest);
1746 return -1;
1747 }
1748
1749 /* check that the save-to folder is in the same account */
1750 if (!imap_account_match(&adata->conn->account, &cac))
1751 {
1752 mutt_debug(LL_DEBUG3, "%s not same server as %s\n", dest, mailbox_path(m));
1753 return 1;
1754 }
1755
1756 imap_fix_path_with_delim(adata->delim, buf, mbox, sizeof(mbox));
1757 if (*mbox == '\0')
1758 mutt_str_copy(mbox, "INBOX", sizeof(mbox));
1759 imap_munge_mbox_name(adata->unicode, mmbox, sizeof(mmbox), mbox);
1760
1761 /* loop in case of TRYCREATE */
1762 struct Buffer *cmd = buf_pool_get();
1763 struct Buffer *sync_cmd = buf_pool_get();
1764 do
1765 {
1766 buf_reset(sync_cmd);
1767 buf_reset(cmd);
1768
1769 if (single)
1770 {
1771 mutt_message(_("Copying message %d to %s..."), e_cur->index + 1, mbox);
1772 buf_add_printf(cmd, "UID COPY %u %s", imap_edata_get(e_cur)->uid, mmbox);
1773
1774 if (e_cur->active && e_cur->changed)
1775 {
1776 rc = imap_sync_message_for_copy(m, e_cur, sync_cmd, &err_continue);
1777 if (rc < 0)
1778 {
1779 mutt_debug(LL_DEBUG1, "#2 could not sync\n");
1780 goto out;
1781 }
1782 }
1783 rc = imap_exec(adata, buf_string(cmd), IMAP_CMD_QUEUE);
1784 if (rc != IMAP_EXEC_SUCCESS)
1785 {
1786 mutt_debug(LL_DEBUG1, "#2 could not queue copy\n");
1787 goto out;
1788 }
1789 }
1790 else /* copy tagged messages */
1791 {
1792 /* if any messages have attachments to delete, fall through to FETCH
1793 * and APPEND. TODO: Copy what we can with COPY, fall through for the
1794 * remainder. */
1795 struct Email **ep = NULL;
1796 ARRAY_FOREACH(ep, ea)
1797 {
1798 struct Email *e = *ep;
1799 if (e->attach_del)
1800 {
1801 mutt_debug(LL_DEBUG3, "#2 Message contains attachments to be deleted\n");
1802 rc = 1;
1803 goto out;
1804 }
1805
1806 if (e->active && e->changed)
1807 {
1808 rc = imap_sync_message_for_copy(m, e, sync_cmd, &err_continue);
1809 if (rc < 0)
1810 {
1811 mutt_debug(LL_DEBUG1, "#1 could not sync\n");
1812 goto out;
1813 }
1814 }
1815 }
1816
1817 struct UidArray uida = ARRAY_HEAD_INITIALIZER;
1818 emails_to_uid_array(ea, &uida);
1819 rc = imap_exec_msg_set(adata, "UID COPY", mmbox, &uida);
1820 ARRAY_FREE(&uida);
1821
1822 if (rc == 0)
1823 {
1824 mutt_debug(LL_DEBUG1, "No messages tagged\n");
1825 rc = -1;
1826 goto out;
1827 }
1828 else if (rc < 0)
1829 {
1830 mutt_debug(LL_DEBUG1, "#1 could not queue copy\n");
1831 goto out;
1832 }
1833 else
1834 {
1835 mutt_message(ngettext("Copying %d message to %s...", "Copying %d messages to %s...", rc),
1836 rc, mbox);
1837 }
1838 }
1839
1840 /* let's get it on */
1841 rc = imap_exec(adata, NULL, IMAP_CMD_NONE);
1842 if (rc == IMAP_EXEC_ERROR)
1843 {
1844 if (triedcreate)
1845 {
1846 mutt_debug(LL_DEBUG1, "Already tried to create mailbox %s\n", mbox);
1847 break;
1848 }
1849 /* bail out if command failed for reasons other than nonexistent target */
1850 if (!mutt_istr_startswith(imap_get_qualifier(adata->buf), "[TRYCREATE]"))
1851 break;
1852 mutt_debug(LL_DEBUG3, "server suggests TRYCREATE\n");
1853 snprintf(prompt, sizeof(prompt), _("Create %s?"), mbox);
1854 const bool c_confirm_create = cs_subset_bool(NeoMutt->sub, "confirm_create");
1855 if (c_confirm_create &&
1856 (query_yesorno_help(prompt, MUTT_YES, NeoMutt->sub, "confirm_create") != MUTT_YES))
1857 {
1859 goto out;
1860 }
1861 if (imap_create_mailbox(adata, mbox) < 0)
1862 break;
1863 triedcreate = 1;
1864 }
1865 } while (rc == IMAP_EXEC_ERROR);
1866
1867 if (rc != 0)
1868 {
1869 imap_error("imap_copy_messages", adata->buf);
1870 goto out;
1871 }
1872
1873 /* cleanup */
1874 if (save_opt == SAVE_MOVE)
1875 {
1876 struct Email **ep = NULL;
1877 ARRAY_FOREACH(ep, ea)
1878 {
1879 struct Email *e = *ep;
1880 mutt_set_flag(m, e, MUTT_DELETE, true, true);
1881 mutt_set_flag(m, e, MUTT_PURGE, true, true);
1882 }
1883 }
1884
1885 rc = 0;
1886
1887out:
1888 buf_pool_release(&cmd);
1889 buf_pool_release(&sync_cmd);
1890
1891 return (rc < 0) ? -1 : rc;
1892}
1893
1901int imap_cache_del(struct Mailbox *m, struct Email *e)
1902{
1904 struct ImapMboxData *mdata = imap_mdata_get(m);
1905
1906 if (!e || !adata || (adata->mailbox != m) || !mdata)
1907 return -1;
1908
1909 mdata->bcache = imap_bcache_open(m);
1910 char id[64] = { 0 };
1911 snprintf(id, sizeof(id), "%u-%u", mdata->uidvalidity, imap_edata_get(e)->uid);
1912 return mutt_bcache_del(mdata->bcache, id);
1913}
1914
1921{
1923 struct ImapMboxData *mdata = imap_mdata_get(m);
1924
1925 if (!adata || (adata->mailbox != m) || !mdata)
1926 return -1;
1927
1928 mdata->bcache = imap_bcache_open(m);
1930
1931 return 0;
1932}
1933
1952char *imap_set_flags(struct Mailbox *m, struct Email *e, char *s, bool *server_changes)
1953{
1955 if (!adata || (adata->mailbox != m))
1956 return NULL;
1957
1958 struct ImapHeader newh = { 0 };
1959 struct ImapEmailData old_edata = { 0 };
1960 int local_changes = e->changed;
1961
1962 struct ImapEmailData *edata = e->edata;
1963 newh.edata = edata;
1964
1965 mutt_debug(LL_DEBUG2, "parsing FLAGS\n");
1966 s = msg_parse_flags(&newh, s);
1967 if (!s)
1968 return NULL;
1969
1970 /* Update tags system */
1971 /* We take a copy of the tags so we can split the string */
1972 char *tags_copy = mutt_str_dup(edata->flags_remote);
1973 driver_tags_replace(&e->tags, tags_copy);
1974 FREE(&tags_copy);
1975
1976 /* YAUH (yet another ugly hack): temporarily set context to
1977 * read-write even if it's read-only, so *server* updates of
1978 * flags can be processed by mutt_set_flag. mailbox->changed must
1979 * be restored afterwards */
1980 bool readonly = m->readonly;
1981 m->readonly = false;
1982
1983 /* This is redundant with the following two checks. Removing:
1984 * mutt_set_flag (m, e, MUTT_NEW, !(edata->read || edata->old), true); */
1985 set_changed_flag(m, e, local_changes, server_changes, MUTT_OLD, old_edata.old,
1986 edata->old, e->old);
1987 set_changed_flag(m, e, local_changes, server_changes, MUTT_READ,
1988 old_edata.read, edata->read, e->read);
1989 set_changed_flag(m, e, local_changes, server_changes, MUTT_DELETE,
1990 old_edata.deleted, edata->deleted, e->deleted);
1991 set_changed_flag(m, e, local_changes, server_changes, MUTT_FLAG,
1992 old_edata.flagged, edata->flagged, e->flagged);
1993 set_changed_flag(m, e, local_changes, server_changes, MUTT_REPLIED,
1994 old_edata.replied, edata->replied, e->replied);
1995
1996 /* this message is now definitively *not* changed (mutt_set_flag
1997 * marks things changed as a side-effect) */
1998 if (local_changes == 0)
1999 e->changed = false;
2000 m->changed &= !readonly;
2001 m->readonly = readonly;
2002
2003 return s;
2004}
2005
2009bool imap_msg_open(struct Mailbox *m, struct Message *msg, struct Email *e)
2010{
2011 struct Envelope *newenv = NULL;
2012 char buf[1024] = { 0 };
2013 char *pc = NULL;
2014 unsigned int bytes;
2015 unsigned int uid;
2016 bool retried = false;
2017 bool read;
2018 int rc;
2019
2020 /* Sam's weird courier server returns an OK response even when FETCH
2021 * fails. Thanks Sam. */
2022 bool fetched = false;
2023
2025
2026 if (!adata || (adata->mailbox != m))
2027 return false;
2028
2029 msg->fp = msg_cache_get(m, e);
2030 if (msg->fp)
2031 {
2032 if (imap_edata_get(e)->parsed)
2033 return true;
2034 goto parsemsg;
2035 }
2036
2037 /* This function is called in a few places after endwin()
2038 * e.g. mutt_pipe_message(). */
2039 bool output_progress = !isendwin() && m->verbose;
2040 if (output_progress)
2041 mutt_message(_("Fetching message..."));
2042
2043 msg->fp = msg_cache_put(m, e);
2044 if (!msg->fp)
2045 {
2046 struct Buffer *tempfile = buf_pool_get();
2047 buf_mktemp(tempfile);
2048 msg->fp = mutt_file_fopen(buf_string(tempfile), "w+");
2049 unlink(buf_string(tempfile));
2050 buf_pool_release(&tempfile);
2051
2052 if (!msg->fp)
2053 return false;
2054 }
2055
2056 /* mark this header as currently inactive so the command handler won't
2057 * also try to update it. HACK until all this code can be moved into the
2058 * command handler */
2059 e->active = false;
2060
2061 const bool c_imap_peek = cs_subset_bool(NeoMutt->sub, "imap_peek");
2062 snprintf(buf, sizeof(buf), "UID FETCH %u %s", imap_edata_get(e)->uid,
2063 ((adata->capabilities & IMAP_CAP_IMAP4REV1) ?
2064 (c_imap_peek ? "BODY.PEEK[]" : "BODY[]") :
2065 "RFC822"));
2066
2067 imap_cmd_start(adata, buf);
2068 do
2069 {
2070 rc = imap_cmd_step(adata);
2071 if (rc != IMAP_RES_CONTINUE)
2072 break;
2073
2074 pc = adata->buf;
2075 pc = imap_next_word(pc);
2076 pc = imap_next_word(pc);
2077
2078 if (mutt_istr_startswith(pc, "FETCH"))
2079 {
2080 while (*pc)
2081 {
2082 pc = imap_next_word(pc);
2083 if (pc[0] == '(')
2084 pc++;
2085 if (mutt_istr_startswith(pc, "UID"))
2086 {
2087 pc = imap_next_word(pc);
2088 if (!mutt_str_atoui(pc, &uid))
2089 goto bail;
2090 if (uid != imap_edata_get(e)->uid)
2091 {
2092 mutt_error(_("The message index is incorrect. Try reopening the mailbox."));
2093 }
2094 }
2095 else if (mutt_istr_startswith(pc, "RFC822") || mutt_istr_startswith(pc, "BODY[]"))
2096 {
2097 pc = imap_next_word(pc);
2098 if (imap_get_literal_count(pc, &bytes) < 0)
2099 {
2100 imap_error("imap_msg_open()", buf);
2101 goto bail;
2102 }
2103
2104 const int res = imap_read_literal(msg->fp, adata, bytes, NULL);
2105 if (res < 0)
2106 {
2107 goto bail;
2108 }
2109 /* pick up trailing line */
2110 rc = imap_cmd_step(adata);
2111 if (rc != IMAP_RES_CONTINUE)
2112 goto bail;
2113 pc = adata->buf;
2114
2115 fetched = true;
2116 }
2117 else if (!e->changed && mutt_istr_startswith(pc, "FLAGS"))
2118 {
2119 /* UW-IMAP will provide a FLAGS update here if the FETCH causes a
2120 * change (eg from \Unseen to \Seen).
2121 * Uncommitted changes in neomutt take precedence. If we decide to
2122 * incrementally update flags later, this won't stop us syncing */
2123 pc = imap_set_flags(m, e, pc, NULL);
2124 if (!pc)
2125 goto bail;
2126 }
2127 }
2128 }
2129 } while (rc == IMAP_RES_CONTINUE);
2130
2131 /* see comment before command start. */
2132 e->active = true;
2133
2134 fflush(msg->fp);
2135 if (ferror(msg->fp))
2136 goto bail;
2137
2138 if (rc != IMAP_RES_OK)
2139 goto bail;
2140
2141 if (!fetched || !imap_code(adata->buf))
2142 goto bail;
2143
2144 if (msg_cache_commit(m, e) < 0)
2145 mutt_debug(LL_DEBUG1, "failed to add message to cache\n");
2146
2147parsemsg:
2148 /* Update the header information. Previously, we only downloaded a
2149 * portion of the headers, those required for the main display. */
2150 rewind(msg->fp);
2151 /* It may be that the Status header indicates a message is read, but the
2152 * IMAP server doesn't know the message has been \Seen. So we capture
2153 * the server's notion of 'read' and if it differs from the message info
2154 * picked up in mutt_rfc822_read_header, we mark the message (and context
2155 * changed). Another possibility: ignore Status on IMAP? */
2156 read = e->read;
2157 newenv = mutt_rfc822_read_header(msg->fp, e, false, false);
2158 mutt_env_merge(e->env, &newenv);
2159
2160 /* see above. We want the new status in e->read, so we unset it manually
2161 * and let mutt_set_flag set it correctly, updating context. */
2162 if (read != e->read)
2163 {
2164 e->read = read;
2165 mutt_set_flag(m, e, MUTT_NEW, read, true);
2166 }
2167
2168 e->lines = 0;
2169 while (fgets(buf, sizeof(buf), msg->fp) && !feof(msg->fp))
2170 {
2171 e->lines++;
2172 }
2173
2174 e->body->length = ftell(msg->fp) - e->body->offset;
2175
2177 rewind(msg->fp);
2178 imap_edata_get(e)->parsed = true;
2179
2180 /* retry message parse if cached message is empty */
2181 if (!retried && ((e->lines == 0) || (e->body->length == 0)))
2182 {
2183 imap_cache_del(m, e);
2184 retried = true;
2185 goto parsemsg;
2186 }
2187
2188 return true;
2189
2190bail:
2191 e->active = true;
2192 mutt_file_fclose(&msg->fp);
2193 imap_cache_del(m, e);
2194 return false;
2195}
2196
2202int imap_msg_commit(struct Mailbox *m, struct Message *msg)
2203{
2204 int rc = mutt_file_fclose(&msg->fp);
2205 if (rc != 0)
2206 return rc;
2207
2208 return imap_append_message(m, msg);
2209}
2210
2216int imap_msg_close(struct Mailbox *m, struct Message *msg)
2217{
2218 return mutt_file_fclose(&msg->fp);
2219}
2220
2224int imap_msg_save_hcache(struct Mailbox *m, struct Email *e)
2225{
2226 int rc = 0;
2227#ifdef USE_HCACHE
2228 bool close_hc = true;
2230 struct ImapMboxData *mdata = imap_mdata_get(m);
2231 if (!mdata || !adata)
2232 return -1;
2233 if (mdata->hcache)
2234 close_hc = false;
2235 else
2236 imap_hcache_open(adata, mdata, true);
2237 rc = imap_hcache_put(mdata, e);
2238 if (close_hc)
2240#endif
2241 return rc;
2242}
#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:216
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:1261
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:144
#define mutt_file_fopen(PATH, MODE)
Definition file.h:143
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)
MacroEvents moved to KeyModuleData UngetKeyEvents moved to KeyModuleData.
Definition get.c:81
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:2216
int imap_msg_commit(struct Mailbox *m, struct Message *msg)
Save changes to an email - Implements MxOps::msg_commit() -.
Definition message.c:2202
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:2009
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:2224
int imap_sort_uid(const void *a, const void *b, void *sdata)
Compare two UIDs - Implements sort_t -.
Definition msg_set.c:48
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
@ MUTT_HASH_NONE
No flags are set.
Definition hash.h:115
int hcache_delete_raw(struct HeaderCache *hc, const char *key, size_t keylen)
Multiplexor for StoreOps::delete_record.
Definition hcache.c:765
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:737
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:162
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:1216
int imap_cmd_step(struct ImapAccountData *adata)
Reads server responses from an IMAP command.
Definition command.c:1230
bool imap_code(const char *s)
Was the command successful.
Definition command.c:1372
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:1420
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:482
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:1920
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:1952
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:1038
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:1555
static int emails_to_uid_array(struct EmailArray *ea, struct UidArray *uida)
Extract IMAP UIDs from Emails.
Definition message.c:1691
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:936
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:847
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:1716
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:1901
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:1115
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:1366
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_RES_RESPOND
+
Definition private.h:56
void imap_hcache_open(struct ImapAccountData *adata, struct ImapMboxData *mdata, bool create)
Open a header cache.
Definition util.c:307
#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:423
int imap_hcache_put(struct ImapMboxData *mdata, struct Email *e)
Add an entry to the header cache.
Definition util.c:388
struct SeqsetIterator * mutt_seqset_iterator_new(const char *seqset)
Create a new Sequence Set Iterator.
Definition util.c:1138
char * imap_hcache_get_uid_seqset(struct ImapMboxData *mdata)
Get a UID Sequence Set from the header cache.
Definition util.c:458
struct Email * imap_hcache_get(struct ImapMboxData *mdata, unsigned int uid)
Get a header cache entry by its UID.
Definition util.c:363
int mutt_seqset_iterator_next(struct SeqsetIterator *iter, unsigned int *next)
Get the next UID from a Sequence Set.
Definition util.c:1159
@ IMAP_EXEC_SUCCESS
Imap command executed or queued successfully.
Definition private.h:95
@ IMAP_EXEC_ERROR
Imap command failure.
Definition private.h:96
void mutt_seqset_iterator_free(struct SeqsetIterator **ptr)
Free a Sequence Set Iterator.
Definition util.c:1218
int imap_get_literal_count(char *buf, unsigned int *bytes)
Write number of bytes in an IMAP literal into bytes.
Definition util.c:785
void imap_cachepath(char delim, const char *mailbox, struct Buffer *dest)
Generate a cache path for a mailbox.
Definition util.c:754
void imap_error(const char *where, const char *msg)
Show an error and abort.
Definition util.c:665
void imap_hcache_close(struct ImapMboxData *mdata)
Close the header cache.
Definition util.c:348
char * imap_fix_path_with_delim(char delim, const char *mailbox, char *path, size_t plen)
Fix up the imap path.
Definition util.c:718
@ IMAP_EXPUNGE_PENDING
Messages on the server have been expunged.
Definition private.h:70
@ IMAP_NEWMAIL_PENDING
New mail is waiting on the server.
Definition private.h:71
@ IMAP_FLAGS_PENDING
Flags have changed on the server.
Definition private.h:72
@ IMAP_REOPEN_ALLOW
Allow re-opening a folder upon expunge.
Definition private.h:68
int imap_hcache_clear_uid_seqset(struct ImapMboxData *mdata)
Delete a UID Sequence Set from the header cache.
Definition util.c:444
bool imap_account_match(const struct ConnAccount *a1, const struct ConnAccount *a2)
Compare two Accounts.
Definition util.c:1106
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:971
@ IMAP_CAP_CONDSTORE
RFC7162.
Definition private.h:150
@ IMAP_CAP_IMAP4REV1
Server supports IMAP4rev1.
Definition private.h:136
@ IMAP_CAP_IMAP4
Server supports IMAP4.
Definition private.h:135
#define IMAP_RES_CONTINUE
* ...
Definition private.h:55
char * imap_next_word(char *s)
Find where the next IMAP word begins.
Definition util.c:829
#define IMAP_RES_BAD
<tag> BAD ...
Definition private.h:53
@ IMAP_CMD_NONE
No flags are set.
Definition private.h:82
@ IMAP_CMD_QUEUE
Queue a command, do not execute.
Definition private.h:84
char * imap_get_qualifier(char *buf)
Get the qualifier from a tagged response.
Definition util.c:812
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:1114
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:127
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:59
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:79
bool qresync
true, if QRESYNC is successfully ENABLE'd
Definition adata.h:65
bool unicode
If true, we can send UTF-8, and the server will use UTF8 rather than mUTF7.
Definition adata.h:64
ImapCapFlags capabilities
Capability flags.
Definition adata.h:56
struct Mailbox * mailbox
Current selected mailbox.
Definition adata.h:80
char * buf
Command buffer.
Definition adata.h:61
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:81
int vcount
The number of virtual messages.
Definition mailbox.h:101
bool changed
Mailbox has been modified.
Definition mailbox.h:112
int msg_new
Number of new messages.
Definition mailbox.h:94
int msg_count
Total number of messages.
Definition mailbox.h:90
void * mdata
Driver specific data.
Definition mailbox.h:134
struct HashTable * subj_hash
Hash Table: "Subject" -> Email.
Definition mailbox.h:126
struct Email ** emails
Array of Emails.
Definition mailbox.h:98
struct HashTable * id_hash
Hash Table: "Message-ID" -> Email.
Definition mailbox.h:125
int msg_deleted
Number of deleted messages.
Definition mailbox.h:95
off_t size
Size of the Mailbox.
Definition mailbox.h:86
struct HashTable * label_hash
Hash Table: "X-Label" -> Email.
Definition mailbox.h:127
int msg_flagged
Number of flagged messages.
Definition mailbox.h:92
bool readonly
Don't allow changes to the mailbox.
Definition mailbox.h:118
int msg_tagged
How many messages are tagged?
Definition mailbox.h:96
bool verbose
Display status messages?
Definition mailbox.h:119
int msg_unread
Number of unread messages.
Definition mailbox.h:91
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:185
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