NeoMutt  2024-12-12-14-g7b49f7
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
util.c
Go to the documentation of this file.
1
34#include "config.h"
35#include <arpa/inet.h>
36#include <ctype.h>
37#include <errno.h>
38#include <netdb.h>
39#include <signal.h>
40#include <stdbool.h>
41#include <stdio.h>
42#include <string.h>
43#include <sys/types.h>
44#include <sys/wait.h>
45#include <unistd.h>
46#include "private.h"
47#include "mutt/lib.h"
48#include "config/lib.h"
49#include "email/lib.h"
50#include "core/lib.h"
51#include "conn/lib.h"
52#include "lib.h"
53#include "bcache/lib.h"
54#include "question/lib.h"
55#include "adata.h"
56#include "edata.h"
57#include "globals.h"
58#include "mdata.h"
59#include "msn.h"
60#ifdef USE_HCACHE
61#include "hcache/lib.h"
62#endif
63
72int imap_adata_find(const char *path, struct ImapAccountData **adata,
73 struct ImapMboxData **mdata)
74{
75 struct ConnAccount cac = { { 0 } };
76 struct ImapAccountData *tmp_adata = NULL;
77 char tmp[1024] = { 0 };
78
79 if (imap_parse_path(path, &cac, tmp, sizeof(tmp)) < 0)
80 return -1;
81
82 struct Account *np = NULL;
83 TAILQ_FOREACH(np, &NeoMutt->accounts, entries)
84 {
85 if (np->type != MUTT_IMAP)
86 continue;
87
88 tmp_adata = np->adata;
89 if (!tmp_adata)
90 continue;
91 if (imap_account_match(&tmp_adata->conn->account, &cac))
92 {
93 if (mdata)
94 {
95 *mdata = imap_mdata_new(tmp_adata, tmp);
96 }
97 *adata = tmp_adata;
98 return 0;
99 }
100 }
101 mutt_debug(LL_DEBUG3, "no ImapAccountData found\n");
102 return -1;
103}
104
110{
111 mutt_hash_free(&mdata->uid_hash);
112 imap_msn_free(&mdata->msn);
113 mutt_bcache_close(&mdata->bcache);
114}
115
123void imap_get_parent(const char *mbox, char delim, char *buf, size_t buflen)
124{
125 /* Make a copy of the mailbox name, but only if the pointers are different */
126 if (mbox != buf)
127 mutt_str_copy(buf, mbox, buflen);
128
129 int n = mutt_str_len(buf);
130
131 /* Let's go backwards until the next delimiter
132 *
133 * If buf[n] is a '/', the first n-- will allow us
134 * to ignore it. If it isn't, then buf looks like
135 * "/aaaaa/bbbb". There is at least one "b", so we can't skip
136 * the "/" after the 'a's.
137 *
138 * If buf == '/', then n-- => n == 0, so the loop ends
139 * immediately */
140 for (n--; (n >= 0) && (buf[n] != delim); n--)
141 ; // do nothing
142
143 /* We stopped before the beginning. There is a trailing slash. */
144 if (n > 0)
145 {
146 /* Strip the trailing delimiter. */
147 buf[n] = '\0';
148 }
149 else
150 {
151 buf[0] = (n == 0) ? delim : '\0';
152 }
153}
154
164void imap_get_parent_path(const char *path, char *buf, size_t buflen)
165{
166 struct ImapAccountData *adata = NULL;
167 struct ImapMboxData *mdata = NULL;
168 char mbox[1024] = { 0 };
169
170 if (imap_adata_find(path, &adata, &mdata) < 0)
171 {
172 mutt_str_copy(buf, path, buflen);
173 return;
174 }
175
176 /* Gets the parent mbox in mbox */
177 imap_get_parent(mdata->name, adata->delim, mbox, sizeof(mbox));
178
179 /* Returns a fully qualified IMAP url */
180 imap_qualify_path(buf, buflen, &adata->conn->account, mbox);
181 imap_mdata_free((void *) &mdata);
182}
183
191void imap_clean_path(char *path, size_t plen)
192{
193 struct ImapAccountData *adata = NULL;
194 struct ImapMboxData *mdata = NULL;
195
196 if (imap_adata_find(path, &adata, &mdata) < 0)
197 return;
198
199 /* Returns a fully qualified IMAP url */
200 imap_qualify_path(path, plen, &adata->conn->account, mdata->name);
201 imap_mdata_free((void *) &mdata);
202}
203
207static const char *imap_get_field(enum ConnAccountField field, void *gf_data)
208{
209 switch (field)
210 {
211 case MUTT_CA_LOGIN:
212 return cs_subset_string(NeoMutt->sub, "imap_login");
213 case MUTT_CA_USER:
214 return cs_subset_string(NeoMutt->sub, "imap_user");
215 case MUTT_CA_PASS:
216 return cs_subset_string(NeoMutt->sub, "imap_pass");
218 return cs_subset_string(NeoMutt->sub, "imap_oauth_refresh_command");
219 case MUTT_CA_HOST:
220 default:
221 return NULL;
222 }
223}
224
225#ifdef USE_HCACHE
234static void imap_msn_index_to_uid_seqset(struct Buffer *buf, struct ImapMboxData *mdata)
235{
236 int first = 1, state = 0;
237 unsigned int cur_uid = 0, last_uid = 0;
238 unsigned int range_begin = 0, range_end = 0;
239 const size_t max_msn = imap_msn_highest(&mdata->msn);
240
241 for (unsigned int msn = 1; msn <= max_msn + 1; msn++)
242 {
243 bool match = false;
244 if (msn <= max_msn)
245 {
246 struct Email *e_cur = imap_msn_get(&mdata->msn, msn - 1);
247 cur_uid = e_cur ? imap_edata_get(e_cur)->uid : 0;
248 if (!state || (cur_uid && ((cur_uid - 1) == last_uid)))
249 match = true;
250 last_uid = cur_uid;
251 }
252
253 if (match)
254 {
255 switch (state)
256 {
257 case 1: /* single: convert to a range */
258 state = 2;
260
261 case 2: /* extend range ending */
262 range_end = cur_uid;
263 break;
264 default:
265 state = 1;
266 range_begin = cur_uid;
267 break;
268 }
269 }
270 else if (state)
271 {
272 if (first)
273 first = 0;
274 else
275 buf_addch(buf, ',');
276
277 if (state == 1)
278 buf_add_printf(buf, "%u", range_begin);
279 else if (state == 2)
280 buf_add_printf(buf, "%u:%u", range_begin, range_end);
281
282 state = 1;
283 range_begin = cur_uid;
284 }
285 }
286}
287
291static void imap_hcache_namer(const char *path, struct Buffer *dest)
292{
293 buf_printf(dest, "%s.hcache", path);
294}
295
302void imap_hcache_open(struct ImapAccountData *adata, struct ImapMboxData *mdata, bool create)
303{
304 if (!adata || !mdata)
305 return;
306
307 if (mdata->hcache)
308 return;
309
310 struct HeaderCache *hc = NULL;
311 struct Buffer *mbox = buf_pool_get();
312 struct Buffer *cachepath = buf_pool_get();
313
314 imap_cachepath(adata->delim, mdata->name, mbox);
315
316 if (strstr(buf_string(mbox), "/../") || mutt_str_equal(buf_string(mbox), "..") ||
317 mutt_strn_equal(buf_string(mbox), "../", 3))
318 {
319 goto cleanup;
320 }
321 size_t len = buf_len(mbox);
322 if ((len > 3) && (mutt_str_equal(buf_string(mbox) + len - 3, "/..")))
323 goto cleanup;
324
325 struct Url url = { 0 };
326 account_to_url(&adata->conn->account, &url);
327 url.path = mbox->data;
328 url_tobuffer(&url, cachepath, U_PATH);
329
330 const char *const c_header_cache = cs_subset_path(NeoMutt->sub, "header_cache");
331 hc = hcache_open(c_header_cache, buf_string(cachepath), imap_hcache_namer, create);
332
333cleanup:
334 buf_pool_release(&mbox);
335 buf_pool_release(&cachepath);
336 mdata->hcache = hc;
337}
338
344{
345 if (!mdata->hcache)
346 return;
347
348 hcache_close(&mdata->hcache);
349}
350
358struct Email *imap_hcache_get(struct ImapMboxData *mdata, unsigned int uid)
359{
360 if (!mdata->hcache)
361 return NULL;
362
363 char key[16] = { 0 };
364
365 snprintf(key, sizeof(key), "%u", uid);
366 struct HCacheEntry hce = hcache_fetch_email(mdata->hcache, key, mutt_str_len(key),
367 mdata->uidvalidity);
368 if (!hce.email && hce.uidvalidity)
369 {
370 mutt_debug(LL_DEBUG3, "hcache uidvalidity mismatch: %u\n", hce.uidvalidity);
371 }
372
373 return hce.email;
374}
375
383int imap_hcache_put(struct ImapMboxData *mdata, struct Email *e)
384{
385 if (!mdata->hcache)
386 return -1;
387
388 char key[16] = { 0 };
389
390 snprintf(key, sizeof(key), "%u", imap_edata_get(e)->uid);
391 return hcache_store_email(mdata->hcache, key, mutt_str_len(key), e, mdata->uidvalidity);
392}
393
401int imap_hcache_del(struct ImapMboxData *mdata, unsigned int uid)
402{
403 if (!mdata->hcache)
404 return -1;
405
406 char key[16] = { 0 };
407
408 snprintf(key, sizeof(key), "%u", uid);
409 return hcache_delete_email(mdata->hcache, key, mutt_str_len(key));
410}
411
419{
420 if (!mdata->hcache)
421 return -1;
422
423 struct Buffer *buf = buf_pool_get();
424 buf_alloc(buf, 8192); // The seqset is likely large. Preallocate to reduce reallocs
426
427 int rc = hcache_store_raw(mdata->hcache, "UIDSEQSET", 9, buf->data, buf_len(buf) + 1);
428 mutt_debug(LL_DEBUG3, "Stored UIDSEQSET %s\n", buf_string(buf));
429 buf_pool_release(&buf);
430 return rc;
431}
432
440{
441 if (!mdata->hcache)
442 return -1;
443
444 return hcache_delete_raw(mdata->hcache, "UIDSEQSET", 9);
445}
446
454{
455 if (!mdata->hcache)
456 return NULL;
457
458 char *seqset = hcache_fetch_raw_str(mdata->hcache, "UIDSEQSET", 9);
459 mutt_debug(LL_DEBUG3, "Retrieved UIDSEQSET %s\n", NONULL(seqset));
460
461 return seqset;
462}
463#endif
464
477int imap_parse_path(const char *path, struct ConnAccount *cac, char *mailbox, size_t mailboxlen)
478{
479 static unsigned short ImapPort = 0;
480 static unsigned short ImapsPort = 0;
481
482 if (ImapPort == 0)
483 {
484 struct servent *service = getservbyname("imap", "tcp");
485 if (service)
486 ImapPort = ntohs(service->s_port);
487 else
488 ImapPort = IMAP_PORT;
489 mutt_debug(LL_DEBUG3, "Using default IMAP port %d\n", ImapPort);
490 }
491
492 if (ImapsPort == 0)
493 {
494 struct servent *service = getservbyname("imaps", "tcp");
495 if (service)
496 ImapsPort = ntohs(service->s_port);
497 else
498 ImapsPort = IMAP_SSL_PORT;
499 mutt_debug(LL_DEBUG3, "Using default IMAPS port %d\n", ImapsPort);
500 }
501
502 /* Defaults */
503 cac->port = ImapPort;
505 cac->service = "imap";
507
508 struct Url *url = url_parse(path);
509 if (!url)
510 return -1;
511
512 if ((url->scheme != U_IMAP) && (url->scheme != U_IMAPS))
513 {
514 url_free(&url);
515 return -1;
516 }
517
518 if ((account_from_url(cac, url) < 0) || (cac->host[0] == '\0'))
519 {
520 url_free(&url);
521 return -1;
522 }
523
524 if (url->scheme == U_IMAPS)
525 cac->flags |= MUTT_ACCT_SSL;
526
527 mutt_str_copy(mailbox, url->path, mailboxlen);
528
529 url_free(&url);
530
531 if ((cac->flags & MUTT_ACCT_SSL) && !(cac->flags & MUTT_ACCT_PORT))
532 cac->port = ImapsPort;
533
534 return 0;
535}
536
548int imap_mxcmp(const char *mx1, const char *mx2)
549{
550 char *b1 = NULL;
551 char *b2 = NULL;
552 int rc;
553
554 if (!mx1 || (*mx1 == '\0'))
555 mx1 = "INBOX";
556 if (!mx2 || (*mx2 == '\0'))
557 mx2 = "INBOX";
558 if (mutt_istr_equal(mx1, "INBOX") && mutt_istr_equal(mx2, "INBOX"))
559 {
560 return 0;
561 }
562
563 b1 = MUTT_MEM_MALLOC(strlen(mx1) + 1, char);
564 b2 = MUTT_MEM_MALLOC(strlen(mx2) + 1, char);
565
566 imap_fix_path(mx1, b1, strlen(mx1) + 1);
567 imap_fix_path(mx2, b2, strlen(mx2) + 1);
568
569 rc = mutt_str_cmp(b1, b2);
570 FREE(&b1);
571 FREE(&b2);
572
573 return rc;
574}
575
584void imap_pretty_mailbox(char *path, size_t pathlen, const char *folder)
585{
586 struct ConnAccount cac_target = { { 0 } };
587 struct ConnAccount cac_home = { { 0 } };
588 struct Url url = { 0 };
589 const char *delim = NULL;
590 int tlen;
591 int hlen = 0;
592 bool home_match = false;
593 char target_mailbox[1024] = { 0 };
594 char home_mailbox[1024] = { 0 };
595
596 if (imap_parse_path(path, &cac_target, target_mailbox, sizeof(target_mailbox)) < 0)
597 return;
598
599 if (imap_path_probe(folder, NULL) != MUTT_IMAP)
600 goto fallback;
601
602 if (imap_parse_path(folder, &cac_home, home_mailbox, sizeof(home_mailbox)) < 0)
603 goto fallback;
604
605 tlen = mutt_str_len(target_mailbox);
606 hlen = mutt_str_len(home_mailbox);
607
608 /* check whether we can do '+' substitution */
609 if (tlen && imap_account_match(&cac_home, &cac_target) &&
610 mutt_strn_equal(home_mailbox, target_mailbox, hlen))
611 {
612 const char *const c_imap_delim_chars = cs_subset_string(NeoMutt->sub, "imap_delim_chars");
613 if (hlen == 0)
614 {
615 home_match = true;
616 }
617 else if (c_imap_delim_chars)
618 {
619 for (delim = c_imap_delim_chars; *delim != '\0'; delim++)
620 if (target_mailbox[hlen] == *delim)
621 home_match = true;
622 }
623 }
624
625 /* do the '+' substitution */
626 if (home_match)
627 {
628 *path++ = '+';
629 /* copy remaining path, skipping delimiter */
630 if (hlen != 0)
631 hlen++;
632 memcpy(path, target_mailbox + hlen, tlen - hlen);
633 path[tlen - hlen] = '\0';
634 return;
635 }
636
637fallback:
638 account_to_url(&cac_target, &url);
639 url.path = target_mailbox;
640 url_tostring(&url, path, pathlen, U_NO_FLAGS);
641}
642
649enum QuadOption imap_continue(const char *msg, const char *resp)
650{
651 imap_error(msg, resp);
652 return query_yesorno(_("Continue?"), MUTT_NO);
653}
654
660void imap_error(const char *where, const char *msg)
661{
662 mutt_error("%s [%s]", where, msg);
663}
664
681char *imap_fix_path(const char *mailbox, char *path, size_t plen)
682{
683 const char *const c_imap_delim_chars = cs_subset_string(NeoMutt->sub, "imap_delim_chars");
684
685 char *out = path;
686 size_t space_left = plen - 1;
687
688 if (mailbox)
689 {
690 for (const char *c = mailbox; *c && space_left; ++c, --space_left)
691 {
692 if (strchr(NONULL(c_imap_delim_chars), *c))
693 {
694 return imap_fix_path_with_delim(*c, mailbox, path, plen);
695 }
696 *out++ = *c;
697 }
698 }
699
700 *out = '\0';
701 return path;
702}
703
713char *imap_fix_path_with_delim(const char delim, const char *mailbox, char *path, size_t plen)
714{
715 char *out = path;
716 size_t space_left = plen - 1;
717
718 if (mailbox)
719 {
720 for (const char *c = mailbox; *c && space_left; ++c, --space_left)
721 {
722 if (*c == delim || *c == '/')
723 {
724 while (*c && *(c + 1) == *c)
725 c++;
726 *out++ = delim;
727 }
728 else
729 {
730 *out++ = *c;
731 }
732 }
733 }
734
735 if (out != path && *(out - 1) == delim)
736 {
737 --out;
738 }
739 *out = '\0';
740 return path;
741}
742
749void imap_cachepath(char delim, const char *mailbox, struct Buffer *dest)
750{
751 const char *p = mailbox;
752 buf_reset(dest);
753 if (!p)
754 return;
755
756 while (*p)
757 {
758 if (p[0] == delim)
759 {
760 buf_addch(dest, '/');
761 /* simple way to avoid collisions with UIDs */
762 if ((p[1] >= '0') && (p[1] <= '9'))
763 buf_addch(dest, '_');
764 }
765 else
766 {
767 buf_addch(dest, *p);
768 }
769 p++;
770 }
771}
772
780int imap_get_literal_count(const char *buf, unsigned int *bytes)
781{
782 char *pc = NULL;
783 char *pn = NULL;
784
785 if (!buf || !(pc = strchr(buf, '{')))
786 return -1;
787
788 pc++;
789 pn = pc;
790 while (isdigit((unsigned char) *pc))
791 pc++;
792 *pc = '\0';
793 if (!mutt_str_atoui(pn, bytes))
794 return -1;
795
796 return 0;
797}
798
807char *imap_get_qualifier(char *buf)
808{
809 char *s = buf;
810
811 /* skip tag */
812 s = imap_next_word(s);
813 /* skip OK/NO/BAD response */
814 s = imap_next_word(s);
815
816 return s;
817}
818
824char *imap_next_word(char *s)
825{
826 bool quoted = false;
827
828 while (*s)
829 {
830 if (*s == '\\')
831 {
832 s++;
833 if (*s)
834 s++;
835 continue;
836 }
837 if (*s == '\"')
838 quoted = !quoted;
839 if (!quoted && isspace(*s))
840 break;
841 s++;
842 }
843
844 SKIPWS(s);
845 return s;
846}
847
855void imap_qualify_path(char *buf, size_t buflen, struct ConnAccount *cac, char *path)
856{
857 struct Url url = { 0 };
858 account_to_url(cac, &url);
859 url.path = path;
860 url_tostring(&url, buf, buflen, U_NO_FLAGS);
861}
862
869void imap_buf_qualify_path(struct Buffer *buf, struct ConnAccount *cac, char *path)
870{
871 struct Url url = { 0 };
872 account_to_url(cac, &url);
873 url.path = path;
874 url_tobuffer(&url, buf, U_NO_FLAGS);
875}
876
886void imap_quote_string(char *dest, size_t dlen, const char *src, bool quote_backtick)
887{
888 const char *quote = "`\"\\";
889 if (!quote_backtick)
890 quote++;
891
892 char *pt = dest;
893 const char *s = src;
894
895 *pt++ = '"';
896 /* save room for quote-chars */
897 dlen -= 3;
898
899 for (; *s && dlen; s++)
900 {
901 if (strchr(quote, *s))
902 {
903 if (dlen < 2)
904 break;
905 dlen -= 2;
906 *pt++ = '\\';
907 *pt++ = *s;
908 }
909 else
910 {
911 *pt++ = *s;
912 dlen--;
913 }
914 }
915 *pt++ = '"';
916 *pt = '\0';
917}
918
924{
925 char *d = s;
926
927 if (*s == '\"')
928 s++;
929 else
930 return;
931
932 while (*s)
933 {
934 if (*s == '\"')
935 {
936 *d = '\0';
937 return;
938 }
939 if (*s == '\\')
940 {
941 s++;
942 }
943 if (*s)
944 {
945 *d = *s;
946 d++;
947 s++;
948 }
949 }
950 *d = '\0';
951}
952
960void imap_munge_mbox_name(bool unicode, char *dest, size_t dlen, const char *src)
961{
962 char *buf = mutt_str_dup(src);
963 imap_utf_encode(unicode, &buf);
964
965 imap_quote_string(dest, dlen, buf, false);
966
967 FREE(&buf);
968}
969
977void imap_unmunge_mbox_name(bool unicode, char *s)
978{
980
981 char *buf = mutt_str_dup(s);
982 if (buf)
983 {
984 imap_utf_decode(unicode, &buf);
985 strncpy(s, buf, strlen(s));
986 }
987
988 FREE(&buf);
989}
990
995{
996 time_t now = mutt_date_now();
997 struct Account *np = NULL;
998 const short c_imap_keep_alive = cs_subset_number(NeoMutt->sub, "imap_keep_alive");
999 TAILQ_FOREACH(np, &NeoMutt->accounts, entries)
1000 {
1001 if (np->type != MUTT_IMAP)
1002 continue;
1003
1004 struct ImapAccountData *adata = np->adata;
1005 if (!adata || !adata->mailbox)
1006 continue;
1007
1008 if ((adata->state >= IMAP_AUTHENTICATED) && (now >= (adata->lastread + c_imap_keep_alive)))
1009 imap_check_mailbox(adata->mailbox, true);
1010 }
1011}
1012
1019{
1020 struct sigaction oldalrm = { 0 };
1021 struct sigaction act = { 0 };
1022 sigset_t oldmask = { 0 };
1023 int rc;
1024
1025 const bool c_imap_passive = cs_subset_bool(NeoMutt->sub, "imap_passive");
1026 cs_subset_str_native_set(NeoMutt->sub, "imap_passive", true, NULL);
1027 OptKeepQuiet = true;
1028
1029 sigprocmask(SIG_SETMASK, NULL, &oldmask);
1030
1031 sigemptyset(&act.sa_mask);
1032 act.sa_handler = mutt_sig_empty_handler;
1033#ifdef SA_INTERRUPT
1034 act.sa_flags = SA_INTERRUPT;
1035#else
1036 act.sa_flags = 0;
1037#endif
1038
1039 sigaction(SIGALRM, &act, &oldalrm);
1040
1041 const short c_imap_keep_alive = cs_subset_number(NeoMutt->sub, "imap_keep_alive");
1042 alarm(c_imap_keep_alive);
1043 while ((waitpid(pid, &rc, 0) < 0) && (errno == EINTR))
1044 {
1045 alarm(0); /* cancel a possibly pending alarm */
1047 alarm(c_imap_keep_alive);
1048 }
1049
1050 alarm(0); /* cancel a possibly pending alarm */
1051
1052 sigaction(SIGALRM, &oldalrm, NULL);
1053 sigprocmask(SIG_SETMASK, &oldmask, NULL);
1054
1055 OptKeepQuiet = false;
1056 cs_subset_str_native_set(NeoMutt->sub, "imap_passive", c_imap_passive, NULL);
1057
1058 return rc;
1059}
1060
1066{
1068 struct ImapMboxData *mdata = imap_mdata_get(m);
1069 if (!adata || !adata->mailbox || (adata->mailbox != m) || !mdata)
1070 return;
1071 mdata->reopen |= IMAP_REOPEN_ALLOW;
1072}
1073
1079{
1081 struct ImapMboxData *mdata = imap_mdata_get(m);
1082 if (!adata || !adata->mailbox || (adata->mailbox != m) || !mdata)
1083 return;
1084 mdata->reopen &= ~IMAP_REOPEN_ALLOW;
1085}
1086
1093bool imap_account_match(const struct ConnAccount *a1, const struct ConnAccount *a2)
1094{
1095 if (!a1 || !a2)
1096 return false;
1097 if (a1->type != a2->type)
1098 return false;
1099 if (!mutt_istr_equal(a1->host, a2->host))
1100 return false;
1101 if ((a1->port != 0) && (a2->port != 0) && (a1->port != a2->port))
1102 return false;
1103 if (a1->flags & a2->flags & MUTT_ACCT_USER)
1104 return mutt_str_equal(a1->user, a2->user);
1105
1106 const char *user = NONULL(Username);
1107
1108 const char *const c_imap_user = cs_subset_string(NeoMutt->sub, "imap_user");
1109 if ((a1->type == MUTT_ACCT_TYPE_IMAP) && c_imap_user)
1110 user = c_imap_user;
1111
1112 if (a1->flags & MUTT_ACCT_USER)
1113 return mutt_str_equal(a1->user, user);
1114 if (a2->flags & MUTT_ACCT_USER)
1115 return mutt_str_equal(a2->user, user);
1116
1117 return true;
1118}
1119
1125struct SeqsetIterator *mutt_seqset_iterator_new(const char *seqset)
1126{
1127 if (!seqset || (*seqset == '\0'))
1128 return NULL;
1129
1130 struct SeqsetIterator *iter = MUTT_MEM_CALLOC(1, struct SeqsetIterator);
1131 iter->full_seqset = mutt_str_dup(seqset);
1132 iter->eostr = strchr(iter->full_seqset, '\0');
1133 iter->substr_cur = iter->substr_end = iter->full_seqset;
1134
1135 return iter;
1136}
1137
1146int mutt_seqset_iterator_next(struct SeqsetIterator *iter, unsigned int *next)
1147{
1148 if (!iter || !next)
1149 return -1;
1150
1151 if (iter->in_range)
1152 {
1153 if ((iter->down && (iter->range_cur == (iter->range_end - 1))) ||
1154 (!iter->down && (iter->range_cur == (iter->range_end + 1))))
1155 {
1156 iter->in_range = 0;
1157 }
1158 }
1159
1160 if (!iter->in_range)
1161 {
1162 iter->substr_cur = iter->substr_end;
1163 if (iter->substr_cur == iter->eostr)
1164 return 1;
1165
1166 iter->substr_end = strchr(iter->substr_cur, ',');
1167 if (!iter->substr_end)
1168 iter->substr_end = iter->eostr;
1169 else
1170 *(iter->substr_end++) = '\0';
1171
1172 char *range_sep = strchr(iter->substr_cur, ':');
1173 if (range_sep)
1174 *range_sep++ = '\0';
1175
1176 if (!mutt_str_atoui_full(iter->substr_cur, &iter->range_cur))
1177 return -1;
1178 if (range_sep)
1179 {
1180 if (!mutt_str_atoui_full(range_sep, &iter->range_end))
1181 return -1;
1182 }
1183 else
1184 {
1185 iter->range_end = iter->range_cur;
1186 }
1187
1188 iter->down = (iter->range_end < iter->range_cur);
1189 iter->in_range = 1;
1190 }
1191
1192 *next = iter->range_cur;
1193 if (iter->down)
1194 iter->range_cur--;
1195 else
1196 iter->range_cur++;
1197
1198 return 0;
1199}
1200
1206{
1207 if (!ptr || !*ptr)
1208 return;
1209
1210 struct SeqsetIterator *iter = *ptr;
1211 FREE(&iter->full_seqset);
1212 FREE(ptr);
1213}
const char * mutt_str_atoui(const char *str, unsigned int *dst)
Convert ASCII string to an unsigned integer.
Definition: atoi.c:218
Body Caching (local copies of email bodies)
void mutt_bcache_close(struct BodyCache **ptr)
Close an Email-Body Cache.
Definition: bcache.c:167
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
void buf_alloc(struct Buffer *buf, size_t new_size)
Make sure a buffer can store at least new_size bytes.
Definition: buffer.c:337
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
short cs_subset_number(const struct ConfigSubset *sub, const char *name)
Get a number config item by name.
Definition: helpers.c:143
const char * cs_subset_path(const struct ConfigSubset *sub, const char *name)
Get a path config item by name.
Definition: helpers.c:168
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.
ConnAccountField
Login credentials.
Definition: connaccount.h:33
@ MUTT_CA_OAUTH_CMD
OAuth refresh command.
Definition: connaccount.h:38
@ MUTT_CA_USER
User name.
Definition: connaccount.h:36
@ MUTT_CA_LOGIN
Login name.
Definition: connaccount.h:35
@ MUTT_CA_HOST
Server name.
Definition: connaccount.h:34
@ MUTT_CA_PASS
Password.
Definition: connaccount.h:37
#define MUTT_ACCT_SSL
Account uses SSL/TLS.
Definition: connaccount.h:47
#define MUTT_ACCT_USER
User field has been set.
Definition: connaccount.h:44
#define MUTT_ACCT_PORT
Port field has been set.
Definition: connaccount.h:43
Convenience wrapper for the core headers.
@ MUTT_IMAP
'IMAP' Mailbox type
Definition: mailbox.h:50
Structs that make up an email.
bool OptKeepQuiet
(pseudo) shut up the message and refresh functions while we are executing an external program
Definition: globals.c:63
char * Username
User's login name.
Definition: globals.c:40
static const char * imap_get_field(enum ConnAccountField field, void *gf_data)
Get connection login credentials - Implements ConnAccount::get_field() -.
Definition: util.c:207
static void imap_hcache_namer(const char *path, struct Buffer *dest)
Generate a filename for the header cache - Implements hcache_namer_t -.
Definition: util.c:291
#define mutt_error(...)
Definition: logging2.h:92
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
void imap_mdata_free(void **ptr)
Free the private Mailbox data - Implements Mailbox::mdata_free() -.
Definition: mdata.c:40
enum MailboxType imap_path_probe(const char *path, const struct stat *st)
Is this an IMAP Mailbox? - Implements MxOps::path_probe() -.
Definition: imap.c:2344
void mutt_hash_free(struct HashTable **ptr)
Free a hash table.
Definition: hash.c:457
struct HeaderCache * hcache_open(const char *path, const char *folder, hcache_namer_t namer, bool create)
Multiplexor for StoreOps::open.
Definition: hcache.c:471
int hcache_delete_email(struct HeaderCache *hc, const char *key, size_t keylen)
Multiplexor for StoreOps::delete_record.
Definition: hcache.c:739
void hcache_close(struct HeaderCache **ptr)
Multiplexor for StoreOps::close.
Definition: hcache.c:542
struct HCacheEntry hcache_fetch_email(struct HeaderCache *hc, const char *key, size_t keylen, uint32_t uidvalidity)
Multiplexor for StoreOps::fetch.
Definition: hcache.c:562
int hcache_store_email(struct HeaderCache *hc, const char *key, size_t keylen, struct Email *e, uint32_t uidvalidity)
Multiplexor for StoreOps::store.
Definition: hcache.c:670
char * hcache_fetch_raw_str(struct HeaderCache *hc, const char *key, size_t keylen)
Fetch a string from the cache.
Definition: hcache.c:652
int hcache_delete_raw(struct HeaderCache *hc, const char *key, size_t keylen)
Multiplexor for StoreOps::delete_record.
Definition: hcache.c:752
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:724
Header cache multiplexor.
struct ImapAccountData * imap_adata_get(struct Mailbox *m)
Get the Account data for this mailbox.
Definition: adata.c:123
struct ImapEmailData * imap_edata_get(struct Email *e)
Get the private data for this Email.
Definition: edata.c:67
struct ImapMboxData * imap_mdata_new(struct ImapAccountData *adata, const char *name)
Allocate and initialise a new ImapMboxData structure.
Definition: mdata.c:74
struct ImapMboxData * imap_mdata_get(struct Mailbox *m)
Get the Mailbox data for this mailbox.
Definition: mdata.c:61
#define IMAP_PORT
Default port for IMAP.
Definition: private.h:44
@ IMAP_AUTHENTICATED
Connection is authenticated.
Definition: private.h:107
#define IMAP_REOPEN_ALLOW
Allow re-opening a folder upon expunge.
Definition: private.h:64
void imap_utf_encode(bool unicode, char **s)
Encode email from local charset to UTF-8.
Definition: utf7.c:397
#define IMAP_SSL_PORT
Port for IMAP over SSL/TLS.
Definition: private.h:45
void imap_utf_decode(bool unicode, char **s)
Decode email from UTF-8 to local charset.
Definition: utf7.c:430
enum MxStatus imap_check_mailbox(struct Mailbox *m, bool force)
Use the NOOP or IDLE command to poll for new mail.
Definition: imap.c:1031
@ LL_DEBUG3
Log at debug level 3.
Definition: logging2.h:45
#define FREE(x)
Definition: memory.h:55
#define MUTT_MEM_CALLOC(n, type)
Definition: memory.h:40
#define MUTT_MEM_MALLOC(n, type)
Definition: memory.h:41
void imap_msn_free(struct MSNArray *msn)
Free the cache.
Definition: msn.c:62
size_t imap_msn_highest(const struct MSNArray *msn)
Return the highest MSN in use.
Definition: msn.c:72
struct Email * imap_msn_get(const struct MSNArray *msn, size_t idx)
Return the Email associated with an msn.
Definition: msn.c:83
IMAP MSN helper functions.
time_t mutt_date_now(void)
Return the number of seconds since the Unix epoch.
Definition: date.c:456
Convenience wrapper for the library headers.
#define FALLTHROUGH
Definition: lib.h:111
#define _(a)
Definition: message.h:28
int mutt_str_cmp(const char *a, const char *b)
Compare two strings, safely.
Definition: string.c:399
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition: string.c:672
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:660
bool mutt_strn_equal(const char *a, const char *b, size_t num)
Check for equality of two strings (to a maximum), safely.
Definition: string.c:425
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:496
size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition: string.c:581
void account_to_url(struct ConnAccount *cac, struct Url *url)
Fill URL with info from account.
Definition: mutt_account.c:80
int account_from_url(struct ConnAccount *cac, const struct Url *url)
Fill ConnAccount with information from url.
Definition: mutt_account.c:44
@ MUTT_ACCT_TYPE_IMAP
Imap Account.
Definition: mutt_account.h:36
Notmuch-specific Mailbox data.
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:82
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:96
Pop-specific Account data.
Pop-specific Email data.
QuadOption
Possible values for a quad-option.
Definition: quad.h:36
@ MUTT_NO
User answered 'No', or assume 'No'.
Definition: quad.h:38
Ask the user a question.
enum QuadOption query_yesorno(const char *prompt, enum QuadOption def)
Ask the user a Yes/No question.
Definition: question.c:327
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:743
GUI display the mailboxes in a side panel.
void mutt_sig_empty_handler(int sig)
Dummy signal handler.
Definition: signal.c:117
Key value store.
#define NONULL(x)
Definition: string2.h:37
#define SKIPWS(ch)
Definition: string2.h:45
A group of associated Mailboxes.
Definition: account.h:36
enum MailboxType type
Type of Mailboxes this Account contains.
Definition: account.h:37
void * adata
Private data (for Mailbox backends)
Definition: account.h:42
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
char user[128]
Username.
Definition: connaccount.h:56
const char * service
Name of the service, e.g. "imap".
Definition: connaccount.h:61
char host[128]
Server to login to.
Definition: connaccount.h:54
const char *(* get_field)(enum ConnAccountField field, void *gf_data)
Definition: connaccount.h:70
unsigned char type
Connection type, e.g. MUTT_ACCT_TYPE_IMAP.
Definition: connaccount.h:59
MuttAccountFlags flags
Which fields are initialised, e.g. MUTT_ACCT_USER.
Definition: connaccount.h:60
unsigned short port
Port to connect to.
Definition: connaccount.h:58
struct ConnAccount account
Account details: username, password, etc.
Definition: connection.h:49
The envelope/body of an email.
Definition: email.h:39
char * path
Path of Email (for local Mailboxes)
Definition: email.h:70
Wrapper for Email retrieved from the header cache.
Definition: lib.h:99
uint32_t uidvalidity
IMAP-specific UIDVALIDITY.
Definition: lib.h:100
struct Email * email
Retrieved email.
Definition: lib.h:102
Header Cache.
Definition: lib.h:86
IMAP-specific Account data -.
Definition: adata.h:40
char delim
Path delimiter.
Definition: adata.h:75
struct Mailbox * mailbox
Current selected mailbox.
Definition: adata.h:76
struct Connection * conn
Connection to IMAP server.
Definition: adata.h:41
unsigned int uid
32-bit Message UID
Definition: edata.h:45
IMAP-specific Mailbox data -.
Definition: mdata.h:40
struct HeaderCache * hcache
Email header cache.
Definition: mdata.h:63
struct BodyCache * bcache
Email body cache.
Definition: mdata.h:61
struct HashTable * uid_hash
Hash Table: "uid" -> Email.
Definition: mdata.h:59
uint32_t uidvalidity
Definition: mdata.h:51
char * name
Mailbox name.
Definition: mdata.h:41
A mailbox.
Definition: mailbox.h:79
void * mdata
Driver specific data.
Definition: mailbox.h:132
Container for Accounts, Notifications.
Definition: neomutt.h:42
struct AccountList accounts
List of all Accounts.
Definition: neomutt.h:47
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:46
UID Sequence Set Iterator.
Definition: private.h:169
char * eostr
Definition: private.h:171
char * substr_end
Definition: private.h:177
unsigned int range_end
Definition: private.h:175
char * substr_cur
Definition: private.h:176
char * full_seqset
Definition: private.h:170
unsigned int range_cur
Definition: private.h:174
A parsed URL proto://user:password@host:port/path?a=1&b=2
Definition: url.h:69
char * src
Raw URL string.
Definition: url.h:77
char * path
Path.
Definition: url.h:75
enum UrlScheme scheme
Scheme, e.g. U_SMTPS.
Definition: url.h:70
int cs_subset_str_native_set(const struct ConfigSubset *sub, const char *name, intptr_t value, struct Buffer *err)
Natively set the value of a string config item.
Definition: subset.c:297
struct Url * url_parse(const char *src)
Fill in Url.
Definition: url.c:239
void url_free(struct Url **ptr)
Free the contents of a URL.
Definition: url.c:124
int url_tostring(const struct Url *url, char *dest, size_t len, uint8_t flags)
Output the URL string for a given Url object.
Definition: url.c:423
int url_tobuffer(const struct Url *url, struct Buffer *buf, uint8_t flags)
Output the URL string for a given Url object.
Definition: url.c:358
#define U_NO_FLAGS
Definition: url.h:49
@ U_IMAP
Url is imap://.
Definition: url.h:39
@ U_IMAPS
Url is imaps://.
Definition: url.h:40
#define U_PATH
Definition: url.h:50
void imap_unmunge_mbox_name(bool unicode, char *s)
Remove quoting from a mailbox name.
Definition: util.c:977
int imap_parse_path(const char *path, struct ConnAccount *cac, char *mailbox, size_t mailboxlen)
Parse an IMAP mailbox name into ConnAccount, name.
Definition: util.c:477
void imap_qualify_path(char *buf, size_t buflen, struct ConnAccount *cac, char *path)
Make an absolute IMAP folder target.
Definition: util.c:855
void imap_allow_reopen(struct Mailbox *m)
Allow re-opening a folder upon expunge.
Definition: util.c:1065
void imap_disallow_reopen(struct Mailbox *m)
Disallow re-opening a folder upon expunge.
Definition: util.c:1078
void imap_hcache_open(struct ImapAccountData *adata, struct ImapMboxData *mdata, bool create)
Open a header cache.
Definition: util.c:302
int imap_get_literal_count(const char *buf, unsigned int *bytes)
Write number of bytes in an IMAP literal into bytes.
Definition: util.c:780
int imap_hcache_store_uid_seqset(struct ImapMboxData *mdata)
Store a UID Sequence Set in the header cache.
Definition: util.c:418
int imap_hcache_put(struct ImapMboxData *mdata, struct Email *e)
Add an entry to the header cache.
Definition: util.c:383
void imap_mdata_cache_reset(struct ImapMboxData *mdata)
Release and clear cache data of ImapMboxData structure.
Definition: util.c:109
void imap_get_parent(const char *mbox, char delim, char *buf, size_t buflen)
Get an IMAP folder's parent.
Definition: util.c:123
struct SeqsetIterator * mutt_seqset_iterator_new(const char *seqset)
Create a new Sequence Set Iterator.
Definition: util.c:1125
void imap_quote_string(char *dest, size_t dlen, const char *src, bool quote_backtick)
Quote string according to IMAP rules.
Definition: util.c:886
char * imap_hcache_get_uid_seqset(struct ImapMboxData *mdata)
Get a UID Sequence Set from the header cache.
Definition: util.c:453
enum QuadOption imap_continue(const char *msg, const char *resp)
Display a message and ask the user if they want to go on.
Definition: util.c:649
void imap_unquote_string(char *s)
Equally stupid unquoting routine.
Definition: util.c:923
struct Email * imap_hcache_get(struct ImapMboxData *mdata, unsigned int uid)
Get a header cache entry by its UID.
Definition: util.c:358
int imap_wait_keep_alive(pid_t pid)
Wait for a process to change state.
Definition: util.c:1018
void imap_get_parent_path(const char *path, char *buf, size_t buflen)
Get the path of the parent folder.
Definition: util.c:164
int mutt_seqset_iterator_next(struct SeqsetIterator *iter, unsigned int *next)
Get the next UID from a Sequence Set.
Definition: util.c:1146
void imap_clean_path(char *path, size_t plen)
Cleans an IMAP path using imap_fix_path.
Definition: util.c:191
void mutt_seqset_iterator_free(struct SeqsetIterator **ptr)
Free a Sequence Set Iterator.
Definition: util.c:1205
int imap_mxcmp(const char *mx1, const char *mx2)
Compare mailbox names, giving priority to INBOX.
Definition: util.c:548
void imap_pretty_mailbox(char *path, size_t pathlen, const char *folder)
Prettify an IMAP mailbox name.
Definition: util.c:584
void imap_cachepath(char delim, const char *mailbox, struct Buffer *dest)
Generate a cache path for a mailbox.
Definition: util.c:749
char * imap_fix_path(const char *mailbox, char *path, size_t plen)
Fix up the imap path.
Definition: util.c:681
void imap_error(const char *where, const char *msg)
Show an error and abort.
Definition: util.c:660
void imap_hcache_close(struct ImapMboxData *mdata)
Close the header cache.
Definition: util.c:343
char * imap_fix_path_with_delim(const char delim, const char *mailbox, char *path, size_t plen)
Fix up the imap path.
Definition: util.c:713
int imap_hcache_del(struct ImapMboxData *mdata, unsigned int uid)
Delete an item from the header cache.
Definition: util.c:401
int imap_hcache_clear_uid_seqset(struct ImapMboxData *mdata)
Delete a UID Sequence Set from the header cache.
Definition: util.c:439
int imap_adata_find(const char *path, struct ImapAccountData **adata, struct ImapMboxData **mdata)
Find the Account data for this path.
Definition: util.c:72
bool imap_account_match(const struct ConnAccount *a1, const struct ConnAccount *a2)
Compare two Accounts.
Definition: util.c:1093
void imap_munge_mbox_name(bool unicode, char *dest, size_t dlen, const char *src)
Quote awkward characters in a mailbox name.
Definition: util.c:960
void imap_keep_alive(void)
Poll the current folder to keep the connection alive.
Definition: util.c:994
static void imap_msn_index_to_uid_seqset(struct Buffer *buf, struct ImapMboxData *mdata)
Convert MSN index of UIDs to Seqset.
Definition: util.c:234
void imap_buf_qualify_path(struct Buffer *buf, struct ConnAccount *cac, char *path)
Make an absolute IMAP folder target to a buffer.
Definition: util.c:869
char * imap_next_word(char *s)
Find where the next IMAP word begins.
Definition: util.c:824
char * imap_get_qualifier(char *buf)
Get the qualifier from a tagged response.
Definition: util.c:807