NeoMutt  2025-12-11-694-ga89709
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
util.c
Go to the documentation of this file.
1
28
34
35#include "config.h"
36#include <arpa/inet.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 **ap = NULL;
84 {
85 struct Account *a = *ap;
86 if (a->type != MUTT_IMAP)
87 continue;
88
89 tmp_adata = a->adata;
90 if (!tmp_adata)
91 continue;
92 if (imap_account_match(&tmp_adata->conn->account, &cac))
93 {
94 if (mdata)
95 {
96 *mdata = imap_mdata_new(tmp_adata, tmp);
97 }
98 *adata = tmp_adata;
99 return 0;
100 }
101 }
102 mutt_debug(LL_DEBUG3, "no ImapAccountData found\n");
103 return -1;
104}
105
111{
112 mutt_hash_free(&mdata->uid_hash);
113 imap_msn_free(&mdata->msn);
114 mutt_bcache_close(&mdata->bcache);
115}
116
124void imap_get_parent(const char *mbox, char delim, char *buf, size_t buflen)
125{
126 /* Make a copy of the mailbox name, but only if the pointers are different */
127 if (mbox != buf)
128 mutt_str_copy(buf, mbox, buflen);
129
130 int n = mutt_str_len(buf);
131
132 /* Let's go backwards until the next delimiter
133 *
134 * If buf[n] is a '/', the first n-- will allow us
135 * to ignore it. If it isn't, then buf looks like
136 * "/aaaaa/bbbb". There is at least one "b", so we can't skip
137 * the "/" after the 'a's.
138 *
139 * If buf == '/', then n-- => n == 0, so the loop ends
140 * immediately */
141 for (n--; (n >= 0) && (buf[n] != delim); n--)
142 ; // do nothing
143
144 /* We stopped before the beginning. There is a trailing slash. */
145 if (n > 0)
146 {
147 /* Strip the trailing delimiter. */
148 buf[n] = '\0';
149 }
150 else
151 {
152 buf[0] = (n == 0) ? delim : '\0';
153 }
154}
155
165void imap_get_parent_path(const char *path, char *buf, size_t buflen)
166{
167 struct ImapAccountData *adata = NULL;
168 struct ImapMboxData *mdata = NULL;
169 char mbox[1024] = { 0 };
170
171 if (imap_adata_find(path, &adata, &mdata) < 0)
172 {
173 mutt_str_copy(buf, path, buflen);
174 return;
175 }
176
177 /* Gets the parent mbox in mbox */
178 imap_get_parent(mdata->name, adata->delim, mbox, sizeof(mbox));
179
180 /* Returns a fully qualified IMAP url */
181 imap_qualify_path(buf, buflen, &adata->conn->account, mbox);
182 imap_mdata_free((void *) &mdata);
183}
184
192void imap_clean_path(char *path, size_t plen)
193{
194 struct ImapAccountData *adata = NULL;
195 struct ImapMboxData *mdata = NULL;
196
197 if (imap_adata_find(path, &adata, &mdata) < 0)
198 return;
199
200 /* Returns a fully qualified IMAP url */
201 imap_qualify_path(path, plen, &adata->conn->account, mdata->name);
202 imap_mdata_free((void *) &mdata);
203}
204
208static const char *imap_get_field(enum ConnAccountField field, void *gf_data)
209{
210 switch (field)
211 {
212 case MUTT_CA_LOGIN:
213 return cs_subset_string(NeoMutt->sub, "imap_login");
214 case MUTT_CA_USER:
215 return cs_subset_string(NeoMutt->sub, "imap_user");
216 case MUTT_CA_PASS:
217 return cs_subset_string(NeoMutt->sub, "imap_pass");
219 return cs_subset_string(NeoMutt->sub, "imap_oauth_refresh_command");
220 case MUTT_CA_HOST:
221 default:
222 return NULL;
223 }
224}
225
226#ifdef USE_HCACHE
235static void imap_msn_index_to_uid_seqset(struct Buffer *buf, struct ImapMboxData *mdata)
236{
237 int first = 1, state = 0;
238 unsigned int cur_uid = 0, last_uid = 0;
239 unsigned int range_begin = 0, range_end = 0;
240 const size_t max_msn = imap_msn_highest(&mdata->msn);
241
242 for (unsigned int msn = 1; msn <= max_msn + 1; msn++)
243 {
244 bool match = false;
245 if (msn <= max_msn)
246 {
247 struct Email *e_cur = imap_msn_get(&mdata->msn, msn - 1);
248 cur_uid = e_cur ? imap_edata_get(e_cur)->uid : 0;
249 if (!state || (cur_uid && ((cur_uid - 1) == last_uid)))
250 match = true;
251 last_uid = cur_uid;
252 }
253
254 if (match)
255 {
256 switch (state)
257 {
258 case 1: /* single: convert to a range */
259 state = 2;
261
262 case 2: /* extend range ending */
263 range_end = cur_uid;
264 break;
265 default:
266 state = 1;
267 range_begin = cur_uid;
268 break;
269 }
270 }
271 else if (state)
272 {
273 if (first)
274 first = 0;
275 else
276 buf_addch(buf, ',');
277
278 if (state == 1)
279 buf_add_printf(buf, "%u", range_begin);
280 else if (state == 2)
281 buf_add_printf(buf, "%u:%u", range_begin, range_end);
282
283 state = 1;
284 range_begin = cur_uid;
285 }
286 }
287}
288
292static void imap_hcache_namer(const struct StoreOps *store_ops,
293 const char *path, struct Buffer *dest)
294{
295 buf_printf(dest, "%s.%s.hcache", path, store_ops->name);
296}
297
304void imap_hcache_open(struct ImapAccountData *adata, struct ImapMboxData *mdata, bool create)
305{
306 if (!adata || !mdata)
307 return;
308
309 if (mdata->hcache)
310 return;
311
312 struct HeaderCache *hc = NULL;
313 struct Buffer *mbox = buf_pool_get();
314 struct Buffer *cachepath = buf_pool_get();
315
316 imap_cachepath(adata->delim, mdata->name, mbox);
317
318 if (strstr(buf_string(mbox), "/../") || mutt_str_equal(buf_string(mbox), "..") ||
319 mutt_strn_equal(buf_string(mbox), "../", 3))
320 {
321 goto cleanup;
322 }
323 size_t len = buf_len(mbox);
324 if ((len > 3) && (mutt_str_equal(buf_string(mbox) + len - 3, "/..")))
325 goto cleanup;
326
327 struct Url url = { 0 };
328 account_to_url(&adata->conn->account, &url);
329 url.path = mbox->data;
330 url_tobuffer(&url, cachepath, U_PATH);
331
332 const char *const c_header_cache = cs_subset_path(NeoMutt->sub, "header_cache");
333 hc = hcache_open(c_header_cache, buf_string(cachepath), imap_hcache_namer, create);
334
335cleanup:
336 buf_pool_release(&mbox);
337 buf_pool_release(&cachepath);
338 mdata->hcache = hc;
339}
340
346{
347 if (!mdata->hcache)
348 return;
349
350 hcache_close(&mdata->hcache);
351}
352
360struct Email *imap_hcache_get(struct ImapMboxData *mdata, unsigned int uid)
361{
362 if (!mdata->hcache)
363 return NULL;
364
365 char key[16] = { 0 };
366
367 snprintf(key, sizeof(key), "%u", uid);
368 struct HCacheEntry hce = hcache_fetch_email(mdata->hcache, key, mutt_str_len(key),
369 mdata->uidvalidity);
370 if (!hce.email && hce.uidvalidity)
371 {
372 mutt_debug(LL_DEBUG3, "hcache uidvalidity mismatch: %u\n", hce.uidvalidity);
373 }
374
375 return hce.email;
376}
377
385int imap_hcache_put(struct ImapMboxData *mdata, struct Email *e)
386{
387 if (!mdata->hcache)
388 return -1;
389
390 char key[16] = { 0 };
391
392 snprintf(key, sizeof(key), "%u", imap_edata_get(e)->uid);
393 return hcache_store_email(mdata->hcache, key, mutt_str_len(key), e, mdata->uidvalidity);
394}
395
403int imap_hcache_del(struct ImapMboxData *mdata, unsigned int uid)
404{
405 if (!mdata->hcache)
406 return -1;
407
408 char key[16] = { 0 };
409
410 snprintf(key, sizeof(key), "%u", uid);
411 return hcache_delete_email(mdata->hcache, key, mutt_str_len(key));
412}
413
421{
422 if (!mdata->hcache)
423 return -1;
424
425 struct Buffer *buf = buf_pool_get();
426 buf_alloc(buf, 8192); // The seqset is likely large. Preallocate to reduce reallocs
428
429 int rc = hcache_store_raw(mdata->hcache, "UIDSEQSET", 9, buf->data, buf_len(buf) + 1);
430 mutt_debug(LL_DEBUG3, "Stored UIDSEQSET %s\n", buf_string(buf));
431 buf_pool_release(&buf);
432 return rc;
433}
434
442{
443 if (!mdata->hcache)
444 return -1;
445
446 return hcache_delete_raw(mdata->hcache, "UIDSEQSET", 9);
447}
448
456{
457 if (!mdata->hcache)
458 return NULL;
459
460 char *seqset = hcache_fetch_raw_str(mdata->hcache, "UIDSEQSET", 9);
461 mutt_debug(LL_DEBUG3, "Retrieved UIDSEQSET %s\n", NONULL(seqset));
462
463 return seqset;
464}
465#endif
466
479int imap_parse_path(const char *path, struct ConnAccount *cac, char *mailbox, size_t mailboxlen)
480{
481 static unsigned short ImapPort = 0;
482 static unsigned short ImapsPort = 0;
483
484 if (ImapPort == 0)
485 {
486 struct servent *service = getservbyname("imap", "tcp");
487 if (service)
488 ImapPort = ntohs(service->s_port);
489 else
490 ImapPort = IMAP_PORT;
491 mutt_debug(LL_DEBUG3, "Using default IMAP port %d\n", ImapPort);
492 }
493
494 if (ImapsPort == 0)
495 {
496 struct servent *service = getservbyname("imaps", "tcp");
497 if (service)
498 ImapsPort = ntohs(service->s_port);
499 else
500 ImapsPort = IMAP_SSL_PORT;
501 mutt_debug(LL_DEBUG3, "Using default IMAPS port %d\n", ImapsPort);
502 }
503
504 /* Defaults */
505 cac->port = ImapPort;
507 cac->service = "imap";
509
510 struct Url *url = url_parse(path);
511 if (!url)
512 return -1;
513
514 if ((url->scheme != U_IMAP) && (url->scheme != U_IMAPS))
515 {
516 url_free(&url);
517 return -1;
518 }
519
520 if ((account_from_url(cac, url) < 0) || (cac->host[0] == '\0'))
521 {
522 url_free(&url);
523 return -1;
524 }
525
526 if (url->scheme == U_IMAPS)
527 cac->flags |= MUTT_ACCT_SSL;
528
529 mutt_str_copy(mailbox, url->path, mailboxlen);
530
531 url_free(&url);
532
533 if ((cac->flags & MUTT_ACCT_SSL) && !(cac->flags & MUTT_ACCT_PORT))
534 cac->port = ImapsPort;
535
536 return 0;
537}
538
550int imap_mxcmp(const char *mx1, const char *mx2)
551{
552 char *b1 = NULL;
553 char *b2 = NULL;
554 int rc;
555
556 if (!mx1 || (*mx1 == '\0'))
557 mx1 = "INBOX";
558 if (!mx2 || (*mx2 == '\0'))
559 mx2 = "INBOX";
560 if (mutt_istr_equal(mx1, "INBOX") && mutt_istr_equal(mx2, "INBOX"))
561 {
562 return 0;
563 }
564
565 b1 = MUTT_MEM_MALLOC(strlen(mx1) + 1, char);
566 b2 = MUTT_MEM_MALLOC(strlen(mx2) + 1, char);
567
568 imap_fix_path(mx1, b1, strlen(mx1) + 1);
569 imap_fix_path(mx2, b2, strlen(mx2) + 1);
570
571 rc = mutt_str_cmp(b1, b2);
572 FREE(&b1);
573 FREE(&b2);
574
575 return rc;
576}
577
586void imap_pretty_mailbox(char *path, size_t pathlen, const char *folder)
587{
588 struct ConnAccount cac_target = { { 0 } };
589 struct ConnAccount cac_home = { { 0 } };
590 struct Url url = { 0 };
591 const char *delim = NULL;
592 int tlen;
593 int hlen = 0;
594 bool home_match = false;
595 char target_mailbox[1024] = { 0 };
596 char home_mailbox[1024] = { 0 };
597
598 if (imap_parse_path(path, &cac_target, target_mailbox, sizeof(target_mailbox)) < 0)
599 return;
600
601 if (imap_path_probe(folder, NULL) != MUTT_IMAP)
602 goto fallback;
603
604 if (imap_parse_path(folder, &cac_home, home_mailbox, sizeof(home_mailbox)) < 0)
605 goto fallback;
606
607 tlen = mutt_str_len(target_mailbox);
608 hlen = mutt_str_len(home_mailbox);
609
610 /* check whether we can do '+' substitution */
611 if (tlen && imap_account_match(&cac_home, &cac_target) &&
612 mutt_strn_equal(home_mailbox, target_mailbox, hlen))
613 {
614 const char *const c_imap_delim_chars = cs_subset_string(NeoMutt->sub, "imap_delim_chars");
615 if (hlen == 0)
616 {
617 home_match = true;
618 }
619 else if (c_imap_delim_chars)
620 {
621 for (delim = c_imap_delim_chars; *delim != '\0'; delim++)
622 if (target_mailbox[hlen] == *delim)
623 home_match = true;
624 }
625 }
626
627 /* do the '+' substitution */
628 if (home_match)
629 {
630 *path++ = '+';
631 /* copy remaining path, skipping delimiter */
632 if (hlen != 0)
633 hlen++;
634 memcpy(path, target_mailbox + hlen, tlen - hlen);
635 path[tlen - hlen] = '\0';
636 return;
637 }
638
639fallback:
640 account_to_url(&cac_target, &url);
641 url.path = target_mailbox;
642 url_tostring(&url, path, pathlen, U_NO_FLAGS);
643}
644
651enum QuadOption imap_continue(const char *msg, const char *resp)
652{
653 imap_error(msg, resp);
654 return query_yesorno(_("Continue?"), MUTT_NO);
655}
656
662void imap_error(const char *where, const char *msg)
663{
664 mutt_error("%s [%s]", where, msg);
665}
666
683char *imap_fix_path(const char *mailbox, char *path, size_t plen)
684{
685 const char *const c_imap_delim_chars = cs_subset_string(NeoMutt->sub, "imap_delim_chars");
686
687 char *out = path;
688 size_t space_left = plen - 1;
689
690 if (mailbox)
691 {
692 for (const char *c = mailbox; *c && space_left; c++, space_left--)
693 {
694 if (strchr(NONULL(c_imap_delim_chars), *c))
695 {
696 return imap_fix_path_with_delim(*c, mailbox, path, plen);
697 }
698 *out++ = *c;
699 }
700 }
701
702 *out = '\0';
703 return path;
704}
705
715char *imap_fix_path_with_delim(const char delim, const char *mailbox, char *path, size_t plen)
716{
717 char *out = path;
718 size_t space_left = plen - 1;
719
720 if (mailbox)
721 {
722 for (const char *c = mailbox; *c && space_left; c++, space_left--)
723 {
724 if (*c == delim || *c == '/')
725 {
726 while (*c && *(c + 1) == *c)
727 c++;
728 *out++ = delim;
729 }
730 else
731 {
732 *out++ = *c;
733 }
734 }
735 }
736
737 if (out != path && *(out - 1) == delim)
738 {
739 out--;
740 }
741 *out = '\0';
742 return path;
743}
744
751void imap_cachepath(char delim, const char *mailbox, struct Buffer *dest)
752{
753 const char *p = mailbox;
754 buf_reset(dest);
755 if (!p)
756 return;
757
758 while (*p)
759 {
760 if (p[0] == delim)
761 {
762 buf_addch(dest, '/');
763 /* simple way to avoid collisions with UIDs */
764 if ((p[1] >= '0') && (p[1] <= '9'))
765 buf_addch(dest, '_');
766 }
767 else
768 {
769 buf_addch(dest, *p);
770 }
771 p++;
772 }
773}
774
782int imap_get_literal_count(char *buf, unsigned int *bytes)
783{
784 char *pc = NULL;
785 char *pn = NULL;
786
787 if (!buf || !(pc = strchr(buf, '{')))
788 return -1;
789
790 pc++;
791 pn = pc;
792 while (mutt_isdigit(*pc))
793 pc++;
794 *pc = '\0';
795 if (!mutt_str_atoui(pn, bytes))
796 return -1;
797
798 return 0;
799}
800
809char *imap_get_qualifier(char *buf)
810{
811 char *s = buf;
812
813 /* skip tag */
814 s = imap_next_word(s);
815 /* skip OK/NO/BAD response */
816 s = imap_next_word(s);
817
818 return s;
819}
820
826char *imap_next_word(char *s)
827{
828 bool quoted = false;
829
830 while (*s)
831 {
832 if (*s == '\\')
833 {
834 s++;
835 if (*s)
836 s++;
837 continue;
838 }
839 if (*s == '\"')
840 quoted = !quoted;
841 if (!quoted && mutt_isspace(*s))
842 break;
843 s++;
844 }
845
846 SKIPWS(s);
847 return s;
848}
849
857void imap_qualify_path(char *buf, size_t buflen, struct ConnAccount *cac, char *path)
858{
859 struct Url url = { 0 };
860 account_to_url(cac, &url);
861 url.path = path;
862 url_tostring(&url, buf, buflen, U_NO_FLAGS);
863}
864
871void imap_buf_qualify_path(struct Buffer *buf, struct ConnAccount *cac, char *path)
872{
873 struct Url url = { 0 };
874 account_to_url(cac, &url);
875 url.path = path;
876 url_tobuffer(&url, buf, U_NO_FLAGS);
877}
878
888void imap_quote_string(char *dest, size_t dlen, const char *src, bool quote_backtick)
889{
890 const char *quote = "`\"\\";
891 if (!quote_backtick)
892 quote++;
893
894 char *pt = dest;
895 const char *s = src;
896
897 if (dlen < 3)
898 {
899 *dest = '\0';
900 return;
901 }
902
903 *pt++ = '"';
904 /* save room for quote-chars */
905 dlen -= 3;
906
907 for (; *s && dlen; s++)
908 {
909 if (strchr(quote, *s))
910 {
911 if (dlen < 2)
912 break;
913 dlen -= 2;
914 *pt++ = '\\';
915 *pt++ = *s;
916 }
917 else
918 {
919 *pt++ = *s;
920 dlen--;
921 }
922 }
923 *pt++ = '"';
924 *pt = '\0';
925}
926
932{
933 char *d = s;
934
935 if (*s == '\"')
936 s++;
937 else
938 return;
939
940 while (*s)
941 {
942 if (*s == '\"')
943 {
944 *d = '\0';
945 return;
946 }
947 if (*s == '\\')
948 {
949 s++;
950 }
951 if (*s)
952 {
953 *d = *s;
954 d++;
955 s++;
956 }
957 }
958 *d = '\0';
959}
960
968void imap_munge_mbox_name(bool unicode, char *dest, size_t dlen, const char *src)
969{
970 char *buf = mutt_str_dup(src);
971 imap_utf_encode(unicode, &buf);
972
973 imap_quote_string(dest, dlen, buf, false);
974
975 FREE(&buf);
976}
977
985void imap_unmunge_mbox_name(bool unicode, char *s)
986{
988
989 char *buf = mutt_str_dup(s);
990 if (buf)
991 {
992 imap_utf_decode(unicode, &buf);
993 strncpy(s, buf, strlen(s));
994 }
995
996 FREE(&buf);
997}
998
1003{
1004 time_t now = mutt_date_now();
1005 const short c_imap_keep_alive = cs_subset_number(NeoMutt->sub, "imap_keep_alive");
1006
1007 struct Account **ap = NULL;
1009 {
1010 struct Account *a = *ap;
1011 if (a->type != MUTT_IMAP)
1012 continue;
1013
1014 struct ImapAccountData *adata = a->adata;
1015 if (!adata || !adata->mailbox)
1016 continue;
1017
1018 if ((adata->state >= IMAP_AUTHENTICATED) && (now >= (adata->lastread + c_imap_keep_alive)))
1019 imap_check_mailbox(adata->mailbox, true);
1020 }
1021}
1022
1029{
1030 struct sigaction oldalrm = { 0 };
1031 struct sigaction act = { 0 };
1032 sigset_t oldmask = { 0 };
1033 int rc;
1034
1035 const bool c_imap_passive = cs_subset_bool(NeoMutt->sub, "imap_passive");
1036 cs_subset_str_native_set(NeoMutt->sub, "imap_passive", true, NULL);
1037 OptKeepQuiet = true;
1038
1039 sigprocmask(SIG_SETMASK, NULL, &oldmask);
1040
1041 sigemptyset(&act.sa_mask);
1042 act.sa_handler = mutt_sig_empty_handler;
1043#ifdef SA_INTERRUPT
1044 act.sa_flags = SA_INTERRUPT;
1045#else
1046 act.sa_flags = 0;
1047#endif
1048
1049 sigaction(SIGALRM, &act, &oldalrm);
1050
1051 const short c_imap_keep_alive = cs_subset_number(NeoMutt->sub, "imap_keep_alive");
1052 alarm(c_imap_keep_alive);
1053 while ((waitpid(pid, &rc, 0) < 0) && (errno == EINTR))
1054 {
1055 alarm(0); /* cancel a possibly pending alarm */
1057 alarm(c_imap_keep_alive);
1058 }
1059
1060 alarm(0); /* cancel a possibly pending alarm */
1061
1062 sigaction(SIGALRM, &oldalrm, NULL);
1063 sigprocmask(SIG_SETMASK, &oldmask, NULL);
1064
1065 OptKeepQuiet = false;
1066 cs_subset_str_native_set(NeoMutt->sub, "imap_passive", c_imap_passive, NULL);
1067
1068 return rc;
1069}
1070
1076{
1078 struct ImapMboxData *mdata = imap_mdata_get(m);
1079 if (!adata || !adata->mailbox || (adata->mailbox != m) || !mdata)
1080 return;
1081 mdata->reopen |= IMAP_REOPEN_ALLOW;
1082}
1083
1089{
1091 struct ImapMboxData *mdata = imap_mdata_get(m);
1092 if (!adata || !adata->mailbox || (adata->mailbox != m) || !mdata)
1093 return;
1094 mdata->reopen &= ~IMAP_REOPEN_ALLOW;
1095}
1096
1103bool imap_account_match(const struct ConnAccount *a1, const struct ConnAccount *a2)
1104{
1105 if (!a1 || !a2)
1106 return false;
1107 if (a1->type != a2->type)
1108 return false;
1109 if (!mutt_istr_equal(a1->host, a2->host))
1110 return false;
1111 if ((a1->port != 0) && (a2->port != 0) && (a1->port != a2->port))
1112 return false;
1113 if (a1->flags & a2->flags & MUTT_ACCT_USER)
1114 return mutt_str_equal(a1->user, a2->user);
1115
1116 const char *user = NONULL(NeoMutt->username);
1117
1118 const char *const c_imap_user = cs_subset_string(NeoMutt->sub, "imap_user");
1119 if ((a1->type == MUTT_ACCT_TYPE_IMAP) && c_imap_user)
1120 user = c_imap_user;
1121
1122 if (a1->flags & MUTT_ACCT_USER)
1123 return mutt_str_equal(a1->user, user);
1124 if (a2->flags & MUTT_ACCT_USER)
1125 return mutt_str_equal(a2->user, user);
1126
1127 return true;
1128}
1129
1135struct SeqsetIterator *mutt_seqset_iterator_new(const char *seqset)
1136{
1137 if (!seqset || (*seqset == '\0'))
1138 return NULL;
1139
1140 struct SeqsetIterator *iter = MUTT_MEM_CALLOC(1, struct SeqsetIterator);
1141 iter->full_seqset = mutt_str_dup(seqset);
1142 iter->eostr = strchr(iter->full_seqset, '\0');
1143 iter->substr_cur = iter->substr_end = iter->full_seqset;
1144
1145 return iter;
1146}
1147
1156int mutt_seqset_iterator_next(struct SeqsetIterator *iter, unsigned int *next)
1157{
1158 if (!iter || !next)
1159 return -1;
1160
1161 if (iter->in_range)
1162 {
1163 if ((iter->down && (iter->range_cur == (iter->range_end - 1))) ||
1164 (!iter->down && (iter->range_cur == (iter->range_end + 1))))
1165 {
1166 iter->in_range = 0;
1167 }
1168 }
1169
1170 if (!iter->in_range)
1171 {
1172 iter->substr_cur = iter->substr_end;
1173 if (iter->substr_cur == iter->eostr)
1174 return 1;
1175
1176 iter->substr_end = strchr(iter->substr_cur, ',');
1177 if (!iter->substr_end)
1178 iter->substr_end = iter->eostr;
1179 else
1180 *(iter->substr_end++) = '\0';
1181
1182 char *range_sep = strchr(iter->substr_cur, ':');
1183 if (range_sep)
1184 *range_sep++ = '\0';
1185
1186 if (!mutt_str_atoui_full(iter->substr_cur, &iter->range_cur))
1187 return -1;
1188 if (range_sep)
1189 {
1190 if (!mutt_str_atoui_full(range_sep, &iter->range_end))
1191 return -1;
1192 }
1193 else
1194 {
1195 iter->range_end = iter->range_cur;
1196 }
1197
1198 iter->down = (iter->range_end < iter->range_cur);
1199 iter->in_range = 1;
1200 }
1201
1202 *next = iter->range_cur;
1203 if (iter->down)
1204 iter->range_cur--;
1205 else
1206 iter->range_cur++;
1207
1208 return 0;
1209}
1210
1216{
1217 if (!ptr || !*ptr)
1218 return;
1219
1220 struct SeqsetIterator *iter = *ptr;
1221 FREE(&iter->full_seqset);
1222 FREE(ptr);
1223}
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition array.h:223
const char * mutt_str_atoui(const char *str, unsigned int *dst)
Convert ASCII string to an unsigned integer.
Definition atoi.c:217
Body Caching (local copies of email bodies)
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:49
bool mutt_isspace(int arg)
Wrapper for isspace(3)
Definition ctype.c:96
bool mutt_isdigit(int arg)
Wrapper for isdigit(3)
Definition ctype.c:66
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:49
Global variables.
static const char * imap_get_field(enum ConnAccountField field, void *gf_data)
Get connection login credentials - Implements ConnAccount::get_field() -.
Definition util.c:208
static void imap_hcache_namer(const struct StoreOps *store_ops, const char *path, struct Buffer *dest)
Generate a filename for the header cache - Implements hcache_namer_t -.
Definition util.c:292
#define mutt_error(...)
Definition logging2.h:94
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
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?
Definition imap.c:2546
void mutt_hash_free(struct HashTable **ptr)
Free a hash table.
Definition hash.c:459
struct HeaderCache * hcache_open(const char *path, const char *folder, hcache_namer_t namer, bool create)
Multiplexor for StoreOps::open.
Definition hcache.c:479
int hcache_delete_email(struct HeaderCache *hc, const char *key, size_t keylen)
Multiplexor for StoreOps::delete_record.
Definition hcache.c:753
void hcache_close(struct HeaderCache **ptr)
Multiplexor for StoreOps::close.
Definition hcache.c:550
struct HCacheEntry hcache_fetch_email(struct HeaderCache *hc, const char *key, size_t keylen, uint32_t uidvalidity)
Multiplexor for StoreOps::fetch.
Definition hcache.c:570
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:684
char * hcache_fetch_raw_str(struct HeaderCache *hc, const char *key, size_t keylen)
Fetch a string from the cache.
Definition hcache.c:663
int hcache_delete_raw(struct HeaderCache *hc, const char *key, size_t keylen)
Multiplexor for StoreOps::delete_record.
Definition hcache.c:766
int hcache_store_raw(struct HeaderCache *hc, const char *key, size_t keylen, void *data, size_t dlen)
Store a key / data pair.
Definition hcache.c:738
Header cache multiplexor.
struct ImapAccountData * imap_adata_get(struct Mailbox *m)
Get the Account data for this mailbox.
Definition adata.c:158
Imap-specific Account data.
struct ImapEmailData * imap_edata_get(struct Email *e)
Get the private data for this Email.
Definition edata.c:66
Imap-specific Email data.
IMAP network mailbox.
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
Imap-specific Mailbox data.
Shared constants/structs that are private to IMAP.
#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:1212
@ LL_DEBUG3
Log at debug level 3.
Definition logging2.h:47
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
#define MUTT_MEM_CALLOC(n, type)
Definition memory.h:52
#define MUTT_MEM_MALLOC(n, type)
Definition memory.h:53
void imap_msn_free(struct MSNArray *msn)
Free the cache.
Definition msn.c:62
struct Email * imap_msn_get(const struct MSNArray *msn, int idx)
Return the Email associated with an msn.
Definition msn.c:83
size_t imap_msn_highest(const struct MSNArray *msn)
Return the highest MSN in use.
Definition msn.c:72
IMAP MSN helper functions.
time_t mutt_date_now(void)
Return the number of seconds since the Unix epoch.
Definition date.c:457
Convenience wrapper for the library headers.
#define FALLTHROUGH
Definition lib.h:117
#define _(a)
Definition message.h:28
int mutt_str_cmp(const char *a, const char *b)
Compare two strings, safely.
Definition string.c:403
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition string.c:677
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition string.c:257
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition string.c:665
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:429
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition string.c:503
size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition string.c:586
void account_to_url(struct ConnAccount *cac, struct Url *url)
Fill URL with info from account.
int account_from_url(struct ConnAccount *cac, const struct Url *url)
Fill ConnAccount with information from url.
@ MUTT_ACCT_TYPE_IMAP
Imap Account.
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:91
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:111
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:329
void mutt_sig_empty_handler(int sig)
Dummy signal handler.
Definition signal.c:130
#define NONULL(x)
Definition string2.h:44
#define SKIPWS(ch)
Definition string2.h:52
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:100
uint32_t uidvalidity
IMAP-specific UIDVALIDITY.
Definition lib.h:101
struct Email * email
Retrieved email.
Definition lib.h:103
Header Cache.
Definition lib.h:87
IMAP-specific Account data -.
Definition adata.h:40
char delim
Path delimiter.
Definition adata.h:78
struct Mailbox * mailbox
Current selected mailbox.
Definition adata.h:79
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:64
struct BodyCache * bcache
Email body cache.
Definition mdata.h:62
struct HashTable * uid_hash
Hash Table: "uid" -> Email.
Definition mdata.h:60
uint32_t uidvalidity
UID validity.
Definition mdata.h:51
char * name
Mailbox name.
Definition mdata.h:41
A mailbox.
Definition mailbox.h:78
void * mdata
Driver specific data.
Definition mailbox.h:131
Container for Accounts, Notifications.
Definition neomutt.h:41
struct AccountArray accounts
All Accounts.
Definition neomutt.h:50
char * username
User's login name.
Definition neomutt.h:57
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:49
UID Sequence Set Iterator.
Definition private.h:169
char * eostr
End of string.
Definition private.h:171
char * substr_end
End of substring.
Definition private.h:177
unsigned int range_end
End of range.
Definition private.h:175
int down
Counting down.
Definition private.h:173
char * substr_cur
Current position in substring.
Definition private.h:176
int in_range
Currently in a range.
Definition private.h:172
char * full_seqset
Full sequence set string.
Definition private.h:170
unsigned int range_cur
Current range value.
Definition private.h:174
Definition lib.h:66
const char * name
Store name.
Definition lib.h:67
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:303
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
No flags are set for URL parsing.
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
Path is included in URL.
Definition url.h:50
void imap_unmunge_mbox_name(bool unicode, char *s)
Remove quoting from a mailbox name.
Definition util.c:985
int imap_parse_path(const char *path, struct ConnAccount *cac, char *mailbox, size_t mailboxlen)
Parse an IMAP mailbox name into ConnAccount, name.
Definition util.c:479
void imap_qualify_path(char *buf, size_t buflen, struct ConnAccount *cac, char *path)
Make an absolute IMAP folder target.
Definition util.c:857
void imap_allow_reopen(struct Mailbox *m)
Allow re-opening a folder upon expunge.
Definition util.c:1075
void imap_disallow_reopen(struct Mailbox *m)
Disallow re-opening a folder upon expunge.
Definition util.c:1088
void imap_hcache_open(struct ImapAccountData *adata, struct ImapMboxData *mdata, bool create)
Open a header cache.
Definition util.c:304
int imap_hcache_store_uid_seqset(struct ImapMboxData *mdata)
Store a UID Sequence Set in the header cache.
Definition util.c:420
int imap_hcache_put(struct ImapMboxData *mdata, struct Email *e)
Add an entry to the header cache.
Definition util.c:385
void imap_mdata_cache_reset(struct ImapMboxData *mdata)
Release and clear cache data of ImapMboxData structure.
Definition util.c:110
void imap_get_parent(const char *mbox, char delim, char *buf, size_t buflen)
Get an IMAP folder's parent.
Definition util.c:124
struct SeqsetIterator * mutt_seqset_iterator_new(const char *seqset)
Create a new Sequence Set Iterator.
Definition util.c:1135
void imap_quote_string(char *dest, size_t dlen, const char *src, bool quote_backtick)
Quote string according to IMAP rules.
Definition util.c:888
char * imap_hcache_get_uid_seqset(struct ImapMboxData *mdata)
Get a UID Sequence Set from the header cache.
Definition util.c:455
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:651
void imap_unquote_string(char *s)
Equally stupid unquoting routine.
Definition util.c:931
struct Email * imap_hcache_get(struct ImapMboxData *mdata, unsigned int uid)
Get a header cache entry by its UID.
Definition util.c:360
int imap_wait_keep_alive(pid_t pid)
Wait for a process to change state.
Definition util.c:1028
void imap_get_parent_path(const char *path, char *buf, size_t buflen)
Get the path of the parent folder.
Definition util.c:165
int mutt_seqset_iterator_next(struct SeqsetIterator *iter, unsigned int *next)
Get the next UID from a Sequence Set.
Definition util.c:1156
void imap_clean_path(char *path, size_t plen)
Cleans an IMAP path using imap_fix_path.
Definition util.c:192
void mutt_seqset_iterator_free(struct SeqsetIterator **ptr)
Free a Sequence Set Iterator.
Definition util.c:1215
int imap_get_literal_count(char *buf, unsigned int *bytes)
Write number of bytes in an IMAP literal into bytes.
Definition util.c:782
int imap_mxcmp(const char *mx1, const char *mx2)
Compare mailbox names, giving priority to INBOX.
Definition util.c:550
void imap_pretty_mailbox(char *path, size_t pathlen, const char *folder)
Prettify an IMAP mailbox name.
Definition util.c:586
void imap_cachepath(char delim, const char *mailbox, struct Buffer *dest)
Generate a cache path for a mailbox.
Definition util.c:751
char * imap_fix_path(const char *mailbox, char *path, size_t plen)
Fix up the imap path.
Definition util.c:683
void imap_error(const char *where, const char *msg)
Show an error and abort.
Definition util.c:662
void imap_hcache_close(struct ImapMboxData *mdata)
Close the header cache.
Definition util.c:345
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:715
int imap_hcache_del(struct ImapMboxData *mdata, unsigned int uid)
Delete an item from the header cache.
Definition util.c:403
int imap_hcache_clear_uid_seqset(struct ImapMboxData *mdata)
Delete a UID Sequence Set from the header cache.
Definition util.c:441
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:1103
void imap_munge_mbox_name(bool unicode, char *dest, size_t dlen, const char *src)
Quote awkward characters in a mailbox name.
Definition util.c:968
void imap_keep_alive(void)
Poll the current folder to keep the connection alive.
Definition util.c:1002
static void imap_msn_index_to_uid_seqset(struct Buffer *buf, struct ImapMboxData *mdata)
Convert MSN index of UIDs to Seqset.
Definition util.c:235
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:871
char * imap_next_word(char *s)
Find where the next IMAP word begins.
Definition util.c:826
char * imap_get_qualifier(char *buf)
Get the qualifier from a tagged response.
Definition util.c:809