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