NeoMutt  2023-11-03-85-g512e01
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
util.c
Go to the documentation of this file.
1
32#include "config.h"
33#include <arpa/inet.h>
34#include <ctype.h>
35#include <errno.h>
36#include <netdb.h>
37#include <signal.h>
38#include <stdbool.h>
39#include <stdio.h>
40#include <string.h>
41#include <sys/wait.h>
42#include <time.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 "lib.h"
51#include "bcache/lib.h"
52#include "question/lib.h"
53#include "adata.h"
54#include "edata.h"
55#include "globals.h"
56#include "mdata.h"
57#include "msn.h"
58#include "mutt_account.h"
59#ifdef USE_HCACHE
60#include "hcache/lib.h"
61#endif
62
71int imap_adata_find(const char *path, struct ImapAccountData **adata,
72 struct ImapMboxData **mdata)
73{
74 struct ConnAccount cac = { { 0 } };
75 struct ImapAccountData *tmp_adata = NULL;
76 char tmp[1024] = { 0 };
77
78 if (imap_parse_path(path, &cac, tmp, sizeof(tmp)) < 0)
79 return -1;
80
81 struct Account *np = NULL;
82 TAILQ_FOREACH(np, &NeoMutt->accounts, entries)
83 {
84 if (np->type != MUTT_IMAP)
85 continue;
86
87 tmp_adata = np->adata;
88 if (!tmp_adata)
89 continue;
90 if (imap_account_match(&tmp_adata->conn->account, &cac))
91 {
92 *mdata = imap_mdata_new(tmp_adata, tmp);
93 *adata = tmp_adata;
94 return 0;
95 }
96 }
97 mutt_debug(LL_DEBUG3, "no ImapAccountData found\n");
98 return -1;
99}
100
106{
107 mutt_hash_free(&mdata->uid_hash);
108 imap_msn_free(&mdata->msn);
109 mutt_bcache_close(&mdata->bcache);
110}
111
119void imap_get_parent(const char *mbox, char delim, char *buf, size_t buflen)
120{
121 /* Make a copy of the mailbox name, but only if the pointers are different */
122 if (mbox != buf)
123 mutt_str_copy(buf, mbox, buflen);
124
125 int n = mutt_str_len(buf);
126
127 /* Let's go backwards until the next delimiter
128 *
129 * If buf[n] is a '/', the first n-- will allow us
130 * to ignore it. If it isn't, then buf looks like
131 * "/aaaaa/bbbb". There is at least one "b", so we can't skip
132 * the "/" after the 'a's.
133 *
134 * If buf == '/', then n-- => n == 0, so the loop ends
135 * immediately */
136 for (n--; (n >= 0) && (buf[n] != delim); n--)
137 ; // do nothing
138
139 /* We stopped before the beginning. There is a trailing slash. */
140 if (n > 0)
141 {
142 /* Strip the trailing delimiter. */
143 buf[n] = '\0';
144 }
145 else
146 {
147 buf[0] = (n == 0) ? delim : '\0';
148 }
149}
150
160void imap_get_parent_path(const char *path, char *buf, size_t buflen)
161{
162 struct ImapAccountData *adata = NULL;
163 struct ImapMboxData *mdata = NULL;
164 char mbox[1024] = { 0 };
165
166 if (imap_adata_find(path, &adata, &mdata) < 0)
167 {
168 mutt_str_copy(buf, path, buflen);
169 return;
170 }
171
172 /* Gets the parent mbox in mbox */
173 imap_get_parent(mdata->name, adata->delim, mbox, sizeof(mbox));
174
175 /* Returns a fully qualified IMAP url */
176 imap_qualify_path(buf, buflen, &adata->conn->account, mbox);
177 imap_mdata_free((void *) &mdata);
178}
179
187void imap_clean_path(char *path, size_t plen)
188{
189 struct ImapAccountData *adata = NULL;
190 struct ImapMboxData *mdata = NULL;
191
192 if (imap_adata_find(path, &adata, &mdata) < 0)
193 return;
194
195 /* Returns a fully qualified IMAP url */
196 imap_qualify_path(path, plen, &adata->conn->account, mdata->name);
197 imap_mdata_free((void *) &mdata);
198}
199
203static const char *imap_get_field(enum ConnAccountField field, void *gf_data)
204{
205 switch (field)
206 {
207 case MUTT_CA_LOGIN:
208 return cs_subset_string(NeoMutt->sub, "imap_login");
209 case MUTT_CA_USER:
210 return cs_subset_string(NeoMutt->sub, "imap_user");
211 case MUTT_CA_PASS:
212 return cs_subset_string(NeoMutt->sub, "imap_pass");
214 return cs_subset_string(NeoMutt->sub, "imap_oauth_refresh_command");
215 case MUTT_CA_HOST:
216 default:
217 return NULL;
218 }
219}
220
221#ifdef USE_HCACHE
230static void imap_msn_index_to_uid_seqset(struct Buffer *buf, struct ImapMboxData *mdata)
231{
232 int first = 1, state = 0;
233 unsigned int cur_uid = 0, last_uid = 0;
234 unsigned int range_begin = 0, range_end = 0;
235 const size_t max_msn = imap_msn_highest(&mdata->msn);
236
237 for (unsigned int msn = 1; msn <= max_msn + 1; msn++)
238 {
239 bool match = false;
240 if (msn <= max_msn)
241 {
242 struct Email *e_cur = imap_msn_get(&mdata->msn, msn - 1);
243 cur_uid = e_cur ? imap_edata_get(e_cur)->uid : 0;
244 if (!state || (cur_uid && ((cur_uid - 1) == last_uid)))
245 match = true;
246 last_uid = cur_uid;
247 }
248
249 if (match)
250 {
251 switch (state)
252 {
253 case 1: /* single: convert to a range */
254 state = 2;
256
257 case 2: /* extend range ending */
258 range_end = cur_uid;
259 break;
260 default:
261 state = 1;
262 range_begin = cur_uid;
263 break;
264 }
265 }
266 else if (state)
267 {
268 if (first)
269 first = 0;
270 else
271 buf_addch(buf, ',');
272
273 if (state == 1)
274 buf_add_printf(buf, "%u", range_begin);
275 else if (state == 2)
276 buf_add_printf(buf, "%u:%u", range_begin, range_end);
277
278 state = 1;
279 range_begin = cur_uid;
280 }
281 }
282}
283
287static void imap_hcache_namer(const char *path, struct Buffer *dest)
288{
289 buf_printf(dest, "%s.hcache", path);
290}
291
297void imap_hcache_open(struct ImapAccountData *adata, struct ImapMboxData *mdata)
298{
299 if (!adata || !mdata)
300 return;
301
302 if (mdata->hcache)
303 return;
304
305 struct HeaderCache *hc = NULL;
306 struct Buffer *mbox = buf_pool_get();
307 struct Buffer *cachepath = buf_pool_get();
308
309 imap_cachepath(adata->delim, mdata->name, mbox);
310
311 if (strstr(buf_string(mbox), "/../") || mutt_str_equal(buf_string(mbox), "..") ||
312 mutt_strn_equal(buf_string(mbox), "../", 3))
313 {
314 goto cleanup;
315 }
316 size_t len = buf_len(mbox);
317 if ((len > 3) && (mutt_str_equal(buf_string(mbox) + len - 3, "/..")))
318 goto cleanup;
319
320 struct Url url = { 0 };
321 mutt_account_tourl(&adata->conn->account, &url);
322 url.path = mbox->data;
323 url_tobuffer(&url, cachepath, U_PATH);
324
325 const char *const c_header_cache = cs_subset_path(NeoMutt->sub, "header_cache");
326 hc = hcache_open(c_header_cache, buf_string(cachepath), imap_hcache_namer);
327
328cleanup:
329 buf_pool_release(&mbox);
330 buf_pool_release(&cachepath);
331 mdata->hcache = hc;
332}
333
339{
340 if (!mdata->hcache)
341 return;
342
343 hcache_close(&mdata->hcache);
344}
345
353struct Email *imap_hcache_get(struct ImapMboxData *mdata, unsigned int uid)
354{
355 if (!mdata->hcache)
356 return NULL;
357
358 char key[16] = { 0 };
359
360 snprintf(key, sizeof(key), "/%u", uid);
361 struct HCacheEntry hce = hcache_fetch(mdata->hcache, key, mutt_str_len(key),
362 mdata->uidvalidity);
363 if (!hce.email && hce.uidvalidity)
364 {
365 mutt_debug(LL_DEBUG3, "hcache uidvalidity mismatch: %u\n", hce.uidvalidity);
366 }
367
368 return hce.email;
369}
370
378int imap_hcache_put(struct ImapMboxData *mdata, struct Email *e)
379{
380 if (!mdata->hcache)
381 return -1;
382
383 char key[16] = { 0 };
384
385 snprintf(key, sizeof(key), "/%u", imap_edata_get(e)->uid);
386 return hcache_store(mdata->hcache, key, mutt_str_len(key), e, mdata->uidvalidity);
387}
388
396int imap_hcache_del(struct ImapMboxData *mdata, unsigned int uid)
397{
398 if (!mdata->hcache)
399 return -1;
400
401 char key[16] = { 0 };
402
403 snprintf(key, sizeof(key), "/%u", uid);
404 return hcache_delete_record(mdata->hcache, key, mutt_str_len(key));
405}
406
414{
415 if (!mdata->hcache)
416 return -1;
417
418 /* The seqset is likely large. Preallocate to reduce reallocs */
419 struct Buffer buf = buf_make(8192);
420 imap_msn_index_to_uid_seqset(&buf, mdata);
421
422 int rc = hcache_store_raw(mdata->hcache, "/UIDSEQSET", 10, buf.data, buf_len(&buf) + 1);
423 mutt_debug(LL_DEBUG3, "Stored /UIDSEQSET %s\n", buf.data);
424 buf_dealloc(&buf);
425 return rc;
426}
427
435{
436 if (!mdata->hcache)
437 return -1;
438
439 return hcache_delete_record(mdata->hcache, "/UIDSEQSET", 10);
440}
441
449{
450 if (!mdata->hcache)
451 return NULL;
452
453 char *seqset = hcache_fetch_str(mdata->hcache, "/UIDSEQSET", 10);
454 mutt_debug(LL_DEBUG3, "Retrieved /UIDSEQSET %s\n", NONULL(seqset));
455
456 return seqset;
457}
458#endif
459
472int imap_parse_path(const char *path, struct ConnAccount *cac, char *mailbox, size_t mailboxlen)
473{
474 static unsigned short ImapPort = 0;
475 static unsigned short ImapsPort = 0;
476
477 if (ImapPort == 0)
478 {
479 struct servent *service = getservbyname("imap", "tcp");
480 if (service)
481 ImapPort = ntohs(service->s_port);
482 else
483 ImapPort = IMAP_PORT;
484 mutt_debug(LL_DEBUG3, "Using default IMAP port %d\n", ImapPort);
485 }
486
487 if (ImapsPort == 0)
488 {
489 struct servent *service = getservbyname("imaps", "tcp");
490 if (service)
491 ImapsPort = ntohs(service->s_port);
492 else
493 ImapsPort = IMAP_SSL_PORT;
494 mutt_debug(LL_DEBUG3, "Using default IMAPS port %d\n", ImapsPort);
495 }
496
497 /* Defaults */
498 cac->port = ImapPort;
500 cac->service = "imap";
502
503 struct Url *url = url_parse(path);
504 if (!url)
505 return -1;
506
507 if ((url->scheme != U_IMAP) && (url->scheme != U_IMAPS))
508 {
509 url_free(&url);
510 return -1;
511 }
512
513 if ((mutt_account_fromurl(cac, url) < 0) || (cac->host[0] == '\0'))
514 {
515 url_free(&url);
516 return -1;
517 }
518
519 if (url->scheme == U_IMAPS)
520 cac->flags |= MUTT_ACCT_SSL;
521
522 mutt_str_copy(mailbox, url->path, mailboxlen);
523
524 url_free(&url);
525
526 if ((cac->flags & MUTT_ACCT_SSL) && !(cac->flags & MUTT_ACCT_PORT))
527 cac->port = ImapsPort;
528
529 return 0;
530}
531
543int imap_mxcmp(const char *mx1, const char *mx2)
544{
545 char *b1 = NULL;
546 char *b2 = NULL;
547 int rc;
548
549 if (!mx1 || (*mx1 == '\0'))
550 mx1 = "INBOX";
551 if (!mx2 || (*mx2 == '\0'))
552 mx2 = "INBOX";
553 if (mutt_istr_equal(mx1, "INBOX") && mutt_istr_equal(mx2, "INBOX"))
554 {
555 return 0;
556 }
557
558 b1 = mutt_mem_malloc(strlen(mx1) + 1);
559 b2 = mutt_mem_malloc(strlen(mx2) + 1);
560
561 imap_fix_path('\0', mx1, b1, strlen(mx1) + 1);
562 imap_fix_path('\0', mx2, b2, strlen(mx2) + 1);
563
564 rc = mutt_str_cmp(b1, b2);
565 FREE(&b1);
566 FREE(&b2);
567
568 return rc;
569}
570
579void imap_pretty_mailbox(char *path, size_t pathlen, const char *folder)
580{
581 struct ConnAccount cac_target = { { 0 } };
582 struct ConnAccount cac_home = { { 0 } };
583 struct Url url = { 0 };
584 const char *delim = NULL;
585 int tlen;
586 int hlen = 0;
587 bool home_match = false;
588 char target_mailbox[1024] = { 0 };
589 char home_mailbox[1024] = { 0 };
590
591 if (imap_parse_path(path, &cac_target, target_mailbox, sizeof(target_mailbox)) < 0)
592 return;
593
594 if (imap_path_probe(folder, NULL) != MUTT_IMAP)
595 goto fallback;
596
597 if (imap_parse_path(folder, &cac_home, home_mailbox, sizeof(home_mailbox)) < 0)
598 goto fallback;
599
600 tlen = mutt_str_len(target_mailbox);
601 hlen = mutt_str_len(home_mailbox);
602
603 /* check whether we can do '+' substitution */
604 if (tlen && imap_account_match(&cac_home, &cac_target) &&
605 mutt_strn_equal(home_mailbox, target_mailbox, hlen))
606 {
607 const char *const c_imap_delim_chars = cs_subset_string(NeoMutt->sub, "imap_delim_chars");
608 if (hlen == 0)
609 {
610 home_match = true;
611 }
612 else if (c_imap_delim_chars)
613 {
614 for (delim = c_imap_delim_chars; *delim != '\0'; delim++)
615 if (target_mailbox[hlen] == *delim)
616 home_match = true;
617 }
618 }
619
620 /* do the '+' substitution */
621 if (home_match)
622 {
623 *path++ = '+';
624 /* copy remaining path, skipping delimiter */
625 if (hlen != 0)
626 ++hlen;
627 memcpy(path, target_mailbox + hlen, tlen - hlen);
628 path[tlen - hlen] = '\0';
629 return;
630 }
631
632fallback:
633 mutt_account_tourl(&cac_target, &url);
634 url.path = target_mailbox;
635 url_tostring(&url, path, pathlen, U_NO_FLAGS);
636}
637
644enum QuadOption imap_continue(const char *msg, const char *resp)
645{
646 imap_error(msg, resp);
647 return query_yesorno(_("Continue?"), MUTT_NO);
648}
649
655void imap_error(const char *where, const char *msg)
656{
657 mutt_error("%s [%s]", where, msg);
658}
659
677char *imap_fix_path(char delim, const char *mailbox, char *path, size_t plen)
678{
679 int i = 0;
680 const char *const c_imap_delim_chars = cs_subset_string(NeoMutt->sub, "imap_delim_chars");
681 for (; mailbox && *mailbox && (i < (plen - 1)); i++)
682 {
683 if ((*mailbox == delim) || (!delim && strchr(NONULL(c_imap_delim_chars), *mailbox)))
684 {
685 delim = *mailbox;
686 /* Skip multiple occurrences of delim */
687 while (*mailbox && *(mailbox + 1) == delim)
688 mailbox++;
689 }
690 path[i] = *mailbox++;
691 }
692
693 /* Do not terminate with a delimiter */
694 if ((i != 0) && (path[i - 1] == delim))
695 i--;
696
697 /* Ensure null termination */
698 path[i] = '\0';
699 return path;
700}
701
708void imap_cachepath(char delim, const char *mailbox, struct Buffer *dest)
709{
710 const char *p = mailbox;
711 buf_reset(dest);
712 if (!p)
713 return;
714
715 while (*p)
716 {
717 if (p[0] == delim)
718 {
719 buf_addch(dest, '/');
720 /* simple way to avoid collisions with UIDs */
721 if ((p[1] >= '0') && (p[1] <= '9'))
722 buf_addch(dest, '_');
723 }
724 else
725 {
726 buf_addch(dest, *p);
727 }
728 p++;
729 }
730}
731
739int imap_get_literal_count(const char *buf, unsigned int *bytes)
740{
741 char *pc = NULL;
742 char *pn = NULL;
743
744 if (!buf || !(pc = strchr(buf, '{')))
745 return -1;
746
747 pc++;
748 pn = pc;
749 while (isdigit((unsigned char) *pc))
750 pc++;
751 *pc = '\0';
752 if (!mutt_str_atoui(pn, bytes))
753 return -1;
754
755 return 0;
756}
757
766char *imap_get_qualifier(char *buf)
767{
768 char *s = buf;
769
770 /* skip tag */
771 s = imap_next_word(s);
772 /* skip OK/NO/BAD response */
773 s = imap_next_word(s);
774
775 return s;
776}
777
783char *imap_next_word(char *s)
784{
785 bool quoted = false;
786
787 while (*s)
788 {
789 if (*s == '\\')
790 {
791 s++;
792 if (*s)
793 s++;
794 continue;
795 }
796 if (*s == '\"')
797 quoted = !quoted;
798 if (!quoted && isspace(*s))
799 break;
800 s++;
801 }
802
803 SKIPWS(s);
804 return s;
805}
806
814void imap_qualify_path(char *buf, size_t buflen, struct ConnAccount *cac, char *path)
815{
816 struct Url url = { 0 };
817 mutt_account_tourl(cac, &url);
818 url.path = path;
819 url_tostring(&url, buf, buflen, U_NO_FLAGS);
820}
821
828void imap_buf_qualify_path(struct Buffer *buf, struct ConnAccount *cac, char *path)
829{
830 struct Url url = { 0 };
831 mutt_account_tourl(cac, &url);
832 url.path = path;
833 url_tobuffer(&url, buf, U_NO_FLAGS);
834}
835
845void imap_quote_string(char *dest, size_t dlen, const char *src, bool quote_backtick)
846{
847 const char *quote = "`\"\\";
848 if (!quote_backtick)
849 quote++;
850
851 char *pt = dest;
852 const char *s = src;
853
854 *pt++ = '"';
855 /* save room for quote-chars */
856 dlen -= 3;
857
858 for (; *s && dlen; s++)
859 {
860 if (strchr(quote, *s))
861 {
862 if (dlen < 2)
863 break;
864 dlen -= 2;
865 *pt++ = '\\';
866 *pt++ = *s;
867 }
868 else
869 {
870 *pt++ = *s;
871 dlen--;
872 }
873 }
874 *pt++ = '"';
875 *pt = '\0';
876}
877
883{
884 char *d = s;
885
886 if (*s == '\"')
887 s++;
888 else
889 return;
890
891 while (*s)
892 {
893 if (*s == '\"')
894 {
895 *d = '\0';
896 return;
897 }
898 if (*s == '\\')
899 {
900 s++;
901 }
902 if (*s)
903 {
904 *d = *s;
905 d++;
906 s++;
907 }
908 }
909 *d = '\0';
910}
911
919void imap_munge_mbox_name(bool unicode, char *dest, size_t dlen, const char *src)
920{
921 char *buf = mutt_str_dup(src);
922 imap_utf_encode(unicode, &buf);
923
924 imap_quote_string(dest, dlen, buf, false);
925
926 FREE(&buf);
927}
928
936void imap_unmunge_mbox_name(bool unicode, char *s)
937{
939
940 char *buf = mutt_str_dup(s);
941 if (buf)
942 {
943 imap_utf_decode(unicode, &buf);
944 strncpy(s, buf, strlen(s));
945 }
946
947 FREE(&buf);
948}
949
954{
955 time_t now = mutt_date_now();
956 struct Account *np = NULL;
957 const short c_imap_keep_alive = cs_subset_number(NeoMutt->sub, "imap_keep_alive");
958 TAILQ_FOREACH(np, &NeoMutt->accounts, entries)
959 {
960 if (np->type != MUTT_IMAP)
961 continue;
962
963 struct ImapAccountData *adata = np->adata;
964 if (!adata || !adata->mailbox)
965 continue;
966
967 if ((adata->state >= IMAP_AUTHENTICATED) && (now >= (adata->lastread + c_imap_keep_alive)))
968 imap_check_mailbox(adata->mailbox, true);
969 }
970}
971
978{
979 struct sigaction oldalrm = { 0 };
980 struct sigaction act = { 0 };
981 sigset_t oldmask = { 0 };
982 int rc;
983
984 const bool c_imap_passive = cs_subset_bool(NeoMutt->sub, "imap_passive");
985 cs_subset_str_native_set(NeoMutt->sub, "imap_passive", true, NULL);
986 OptKeepQuiet = true;
987
988 sigprocmask(SIG_SETMASK, NULL, &oldmask);
989
990 sigemptyset(&act.sa_mask);
991 act.sa_handler = mutt_sig_empty_handler;
992#ifdef SA_INTERRUPT
993 act.sa_flags = SA_INTERRUPT;
994#else
995 act.sa_flags = 0;
996#endif
997
998 sigaction(SIGALRM, &act, &oldalrm);
999
1000 const short c_imap_keep_alive = cs_subset_number(NeoMutt->sub, "imap_keep_alive");
1001 alarm(c_imap_keep_alive);
1002 while ((waitpid(pid, &rc, 0) < 0) && (errno == EINTR))
1003 {
1004 alarm(0); /* cancel a possibly pending alarm */
1006 alarm(c_imap_keep_alive);
1007 }
1008
1009 alarm(0); /* cancel a possibly pending alarm */
1010
1011 sigaction(SIGALRM, &oldalrm, NULL);
1012 sigprocmask(SIG_SETMASK, &oldmask, NULL);
1013
1014 OptKeepQuiet = false;
1015 cs_subset_str_native_set(NeoMutt->sub, "imap_passive", c_imap_passive, NULL);
1016
1017 return rc;
1018}
1019
1025{
1027 struct ImapMboxData *mdata = imap_mdata_get(m);
1028 if (!adata || !adata->mailbox || (adata->mailbox != m) || !mdata)
1029 return;
1030 mdata->reopen |= IMAP_REOPEN_ALLOW;
1031}
1032
1038{
1040 struct ImapMboxData *mdata = imap_mdata_get(m);
1041 if (!adata || !adata->mailbox || (adata->mailbox != m) || !mdata)
1042 return;
1043 mdata->reopen &= ~IMAP_REOPEN_ALLOW;
1044}
1045
1052bool imap_account_match(const struct ConnAccount *a1, const struct ConnAccount *a2)
1053{
1054 if (!a1 || !a2)
1055 return false;
1056 if (a1->type != a2->type)
1057 return false;
1058 if (!mutt_istr_equal(a1->host, a2->host))
1059 return false;
1060 if ((a1->port != 0) && (a2->port != 0) && (a1->port != a2->port))
1061 return false;
1062 if (a1->flags & a2->flags & MUTT_ACCT_USER)
1063 return mutt_str_equal(a1->user, a2->user);
1064
1065 const char *user = NONULL(Username);
1066
1067 const char *const c_imap_user = cs_subset_string(NeoMutt->sub, "imap_user");
1068 if ((a1->type == MUTT_ACCT_TYPE_IMAP) && c_imap_user)
1069 user = c_imap_user;
1070
1071 if (a1->flags & MUTT_ACCT_USER)
1072 return mutt_str_equal(a1->user, user);
1073 if (a2->flags & MUTT_ACCT_USER)
1074 return mutt_str_equal(a2->user, user);
1075
1076 return true;
1077}
1078
1084struct SeqsetIterator *mutt_seqset_iterator_new(const char *seqset)
1085{
1086 if (!seqset || (*seqset == '\0'))
1087 return NULL;
1088
1089 struct SeqsetIterator *iter = mutt_mem_calloc(1, sizeof(struct SeqsetIterator));
1090 iter->full_seqset = mutt_str_dup(seqset);
1091 iter->eostr = strchr(iter->full_seqset, '\0');
1092 iter->substr_cur = iter->substr_end = iter->full_seqset;
1093
1094 return iter;
1095}
1096
1105int mutt_seqset_iterator_next(struct SeqsetIterator *iter, unsigned int *next)
1106{
1107 if (!iter || !next)
1108 return -1;
1109
1110 if (iter->in_range)
1111 {
1112 if ((iter->down && (iter->range_cur == (iter->range_end - 1))) ||
1113 (!iter->down && (iter->range_cur == (iter->range_end + 1))))
1114 {
1115 iter->in_range = 0;
1116 }
1117 }
1118
1119 if (!iter->in_range)
1120 {
1121 iter->substr_cur = iter->substr_end;
1122 if (iter->substr_cur == iter->eostr)
1123 return 1;
1124
1125 iter->substr_end = strchr(iter->substr_cur, ',');
1126 if (!iter->substr_end)
1127 iter->substr_end = iter->eostr;
1128 else
1129 *(iter->substr_end++) = '\0';
1130
1131 char *range_sep = strchr(iter->substr_cur, ':');
1132 if (range_sep)
1133 *range_sep++ = '\0';
1134
1135 if (!mutt_str_atoui_full(iter->substr_cur, &iter->range_cur))
1136 return -1;
1137 if (range_sep)
1138 {
1139 if (!mutt_str_atoui_full(range_sep, &iter->range_end))
1140 return -1;
1141 }
1142 else
1143 {
1144 iter->range_end = iter->range_cur;
1145 }
1146
1147 iter->down = (iter->range_end < iter->range_cur);
1148 iter->in_range = 1;
1149 }
1150
1151 *next = iter->range_cur;
1152 if (iter->down)
1153 iter->range_cur--;
1154 else
1155 iter->range_cur++;
1156
1157 return 0;
1158}
1159
1165{
1166 if (!ptr || !*ptr)
1167 return;
1168
1169 struct SeqsetIterator *iter = *ptr;
1170 FREE(&iter->full_seqset);
1171 FREE(ptr);
1172}
const char * mutt_str_atoui(const char *str, unsigned int *dst)
Convert ASCII string to an unsigned integer.
Definition: atoi.c:213
Body Caching (local copies of email bodies)
void mutt_bcache_close(struct BodyCache **ptr)
Close an Email-Body Cache.
Definition: bcache.c:164
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:173
int buf_add_printf(struct Buffer *buf, const char *fmt,...)
Format a string appending a Buffer.
Definition: buffer.c:216
size_t buf_len(const struct Buffer *buf)
Calculate the length of a Buffer.
Definition: buffer.c:466
void buf_dealloc(struct Buffer *buf)
Release the memory allocated by a buffer.
Definition: buffer.c:389
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition: buffer.c:88
struct Buffer buf_make(size_t size)
Make a new buffer on the stack.
Definition: buffer.c:70
size_t buf_addch(struct Buffer *buf, char c)
Add a single character to a Buffer.
Definition: buffer.c:253
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:93
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:292
short cs_subset_number(const struct ConfigSubset *sub, const char *name)
Get a number config item by name.
Definition: helpers.c:144
const char * cs_subset_path(const struct ConfigSubset *sub, const char *name)
Get a path config item by name.
Definition: helpers.c:169
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:48
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.
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:71
char * Username
User's login name.
Definition: globals.c:42
static const char * imap_get_field(enum ConnAccountField field, void *gf_data)
Get connection login credentials - Implements ConnAccount::get_field() -.
Definition: util.c:203
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:287
#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:39
enum MailboxType imap_path_probe(const char *path, const struct stat *st)
Is this an IMAP Mailbox? - Implements MxOps::path_probe() -.
Definition: imap.c:2327
void mutt_hash_free(struct HashTable **ptr)
Free a hash table.
Definition: hash.c:457
Header cache multiplexor.
int hcache_store(struct HeaderCache *hc, const char *key, size_t keylen, struct Email *e, uint32_t uidvalidity)
Multiplexor for StoreOps::store.
Definition: hcache.c:687
struct HCacheEntry hcache_fetch(struct HeaderCache *hc, const char *key, size_t keylen, uint32_t uidvalidity)
Multiplexor for StoreOps::fetch.
Definition: hcache.c:584
char * hcache_fetch_str(struct HeaderCache *hc, const char *key, size_t keylen)
Fetch a string from the cache.
Definition: hcache.c:671
struct HeaderCache * hcache_open(const char *path, const char *folder, hcache_namer_t namer)
Multiplexor for StoreOps::open.
Definition: hcache.c:495
void hcache_close(struct HeaderCache **ptr)
Multiplexor for StoreOps::close.
Definition: hcache.c:564
int hcache_delete_record(struct HeaderCache *hc, const char *key, size_t keylen)
Multiplexor for StoreOps::delete_record.
Definition: hcache.c:760
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:742
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:66
struct ImapMboxData * imap_mdata_new(struct ImapAccountData *adata, const char *name)
Allocate and initialise a new ImapMboxData structure.
Definition: mdata.c:73
struct ImapMboxData * imap_mdata_get(struct Mailbox *m)
Get the Mailbox data for this mailbox.
Definition: mdata.c:60
#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:395
#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:428
enum MxStatus imap_check_mailbox(struct Mailbox *m, bool force)
Use the NOOP or IDLE command to poll for new mail.
Definition: imap.c:1019
@ LL_DEBUG3
Log at debug level 3.
Definition: logging2.h:45
@ MUTT_IMAP
'IMAP' Mailbox type
Definition: mailbox.h:50
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:90
#define FREE(x)
Definition: memory.h:45
void imap_msn_free(struct MSNArray *msn)
Free the cache.
Definition: msn.c:59
size_t imap_msn_highest(const struct MSNArray *msn)
Return the highest MSN in use.
Definition: msn.c:69
struct Email * imap_msn_get(const struct MSNArray *msn, size_t idx)
Return the Email associated with an msn.
Definition: msn.c:80
IMAP MSN helper functions.
time_t mutt_date_now(void)
Return the number of seconds since the Unix epoch.
Definition: date.c:446
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:471
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition: string.c:810
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:251
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:798
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:497
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition: string.c:568
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:653
int mutt_account_fromurl(struct ConnAccount *cac, const struct Url *url)
Fill ConnAccount with information from url.
Definition: mutt_account.c:43
void mutt_account_tourl(struct ConnAccount *cac, struct Url *url)
Fill URL with info from account.
Definition: mutt_account.c:79
ConnAccount object used by POP and IMAP.
@ MUTT_ACCT_TYPE_IMAP
Imap Account.
Definition: mutt_account.h:37
Notmuch-specific Mailbox data.
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
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:330
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:725
GUI display the mailboxes in a side panel.
void mutt_sig_empty_handler(int sig)
Dummy signal handler.
Definition: signal.c:70
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:37
enum MailboxType type
Type of Mailboxes this Account contains.
Definition: account.h:38
void * adata
Private data (for Mailbox backends)
Definition: account.h:43
String manipulation buffer.
Definition: buffer.h:34
char * data
Pointer to data.
Definition: buffer.h:35
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:50
The envelope/body of an email.
Definition: email.h:37
char * path
Path of Email (for local Mailboxes)
Definition: email.h:68
Wrapper for Email retrieved from the header cache.
Definition: lib.h:101
uint32_t uidvalidity
IMAP-specific UIDVALIDITY.
Definition: lib.h:102
struct Email * email
Retrieved email.
Definition: lib.h:104
Header Cache.
Definition: lib.h:88
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:44
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:131
Container for Accounts, Notifications.
Definition: neomutt.h:41
struct AccountList accounts
List of all Accounts.
Definition: neomutt.h:46
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:45
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:304
struct Url * url_parse(const char *src)
Fill in Url.
Definition: url.c:238
int url_tostring(struct Url *url, char *dest, size_t len, uint8_t flags)
Output the URL string for a given Url object.
Definition: url.c:422
int url_tobuffer(struct Url *url, struct Buffer *buf, uint8_t flags)
Output the URL string for a given Url object.
Definition: url.c:357
void url_free(struct Url **ptr)
Free the contents of a URL.
Definition: url.c:123
#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:936
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:472
void imap_qualify_path(char *buf, size_t buflen, struct ConnAccount *cac, char *path)
Make an absolute IMAP folder target.
Definition: util.c:814
void imap_allow_reopen(struct Mailbox *m)
Allow re-opening a folder upon expunge.
Definition: util.c:1024
void imap_disallow_reopen(struct Mailbox *m)
Disallow re-opening a folder upon expunge.
Definition: util.c:1037
int imap_get_literal_count(const char *buf, unsigned int *bytes)
Write number of bytes in an IMAP literal into bytes.
Definition: util.c:739
int imap_hcache_store_uid_seqset(struct ImapMboxData *mdata)
Store a UID Sequence Set in the header cache.
Definition: util.c:413
int imap_hcache_put(struct ImapMboxData *mdata, struct Email *e)
Add an entry to the header cache.
Definition: util.c:378
void imap_mdata_cache_reset(struct ImapMboxData *mdata)
Release and clear cache data of ImapMboxData structure.
Definition: util.c:105
void imap_get_parent(const char *mbox, char delim, char *buf, size_t buflen)
Get an IMAP folder's parent.
Definition: util.c:119
struct SeqsetIterator * mutt_seqset_iterator_new(const char *seqset)
Create a new Sequence Set Iterator.
Definition: util.c:1084
void imap_quote_string(char *dest, size_t dlen, const char *src, bool quote_backtick)
Quote string according to IMAP rules.
Definition: util.c:845
char * imap_hcache_get_uid_seqset(struct ImapMboxData *mdata)
Get a UID Sequence Set from the header cache.
Definition: util.c:448
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:644
void imap_unquote_string(char *s)
Equally stupid unquoting routine.
Definition: util.c:882
struct Email * imap_hcache_get(struct ImapMboxData *mdata, unsigned int uid)
Get a header cache entry by its UID.
Definition: util.c:353
int imap_wait_keep_alive(pid_t pid)
Wait for a process to change state.
Definition: util.c:977
void imap_get_parent_path(const char *path, char *buf, size_t buflen)
Get the path of the parent folder.
Definition: util.c:160
int mutt_seqset_iterator_next(struct SeqsetIterator *iter, unsigned int *next)
Get the next UID from a Sequence Set.
Definition: util.c:1105
void imap_clean_path(char *path, size_t plen)
Cleans an IMAP path using imap_fix_path.
Definition: util.c:187
void mutt_seqset_iterator_free(struct SeqsetIterator **ptr)
Free a Sequence Set Iterator.
Definition: util.c:1164
char * imap_fix_path(char delim, const char *mailbox, char *path, size_t plen)
Fix up the imap path.
Definition: util.c:677
int imap_mxcmp(const char *mx1, const char *mx2)
Compare mailbox names, giving priority to INBOX.
Definition: util.c:543
void imap_pretty_mailbox(char *path, size_t pathlen, const char *folder)
Prettify an IMAP mailbox name.
Definition: util.c:579
void imap_cachepath(char delim, const char *mailbox, struct Buffer *dest)
Generate a cache path for a mailbox.
Definition: util.c:708
void imap_error(const char *where, const char *msg)
Show an error and abort.
Definition: util.c:655
void imap_hcache_close(struct ImapMboxData *mdata)
Close the header cache.
Definition: util.c:338
int imap_hcache_del(struct ImapMboxData *mdata, unsigned int uid)
Delete an item from the header cache.
Definition: util.c:396
int imap_hcache_clear_uid_seqset(struct ImapMboxData *mdata)
Delete a UID Sequence Set from the header cache.
Definition: util.c:434
int imap_adata_find(const char *path, struct ImapAccountData **adata, struct ImapMboxData **mdata)
Find the Account data for this path.
Definition: util.c:71
bool imap_account_match(const struct ConnAccount *a1, const struct ConnAccount *a2)
Compare two Accounts.
Definition: util.c:1052
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:919
void imap_keep_alive(void)
Poll the current folder to keep the connection alive.
Definition: util.c:953
void imap_hcache_open(struct ImapAccountData *adata, struct ImapMboxData *mdata)
Open a header cache.
Definition: util.c:297
static void imap_msn_index_to_uid_seqset(struct Buffer *buf, struct ImapMboxData *mdata)
Convert MSN index of UIDs to Seqset.
Definition: util.c:230
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:828
char * imap_next_word(char *s)
Find where the next IMAP word begins.
Definition: util.c:783
char * imap_get_qualifier(char *buf)
Get the qualifier from a tagged response.
Definition: util.c:766