1 /**
2  * @file
3  * Imap-specific Mailbox data
4  *
5  * @authors
6  * Copyright (C) 2021 Richard Russon <rich@flatcap.org>
7  *
8  * @copyright
9  * This program is free software: you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License as published by the Free Software
11  * Foundation, either version 2 of the License, or (at your option) any later
12  * version.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License along with
20  * this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 /**
24  * @page imap_mdata Imap-specific Mailbox data
25  *
26  * Imap-specific Mailbox data
27  */
28 
29 #include "config.h"
30 #include <stddef.h>
31 #include "private.h"
32 #include "core/lib.h"
33 #include "mdata.h"
34 #include "hcache/lib.h"
35 #include "adata.h"
36 
37 /**
38  * imap_mdata_free - Free the private Mailbox data - Implements Mailbox::mdata_free()
39  */
imap_mdata_free(void ** ptr)40 void imap_mdata_free(void **ptr)
41 {
42   if (!ptr || !*ptr)
43     return;
44 
45   struct ImapMboxData *mdata = *ptr;
46 
47   imap_mdata_cache_reset(mdata);
48   mutt_list_free(&mdata->flags);
49   FREE(&mdata->name);
50   FREE(&mdata->real_name);
51   FREE(&mdata->munge_name);
52   FREE(ptr);
53 }
54 
55 /**
56  * imap_mdata_get - Get the Mailbox data for this mailbox
57  * @param m Mailbox
58  */
imap_mdata_get(struct Mailbox * m)59 struct ImapMboxData *imap_mdata_get(struct Mailbox *m)
60 {
61   if (!m || (m->type != MUTT_IMAP) || !m->mdata)
62     return NULL;
63   return m->mdata;
64 }
65 
66 /**
67  * imap_mdata_new - Allocate and initialise a new ImapMboxData structure
68  * @param adata Imap Account data
69  * @param name  Name for Mailbox
70  * @retval ptr New ImapMboxData
71  */
imap_mdata_new(struct ImapAccountData * adata,const char * name)72 struct ImapMboxData *imap_mdata_new(struct ImapAccountData *adata, const char *name)
73 {
74   char buf[1024];
75   struct ImapMboxData *mdata = mutt_mem_calloc(1, sizeof(struct ImapMboxData));
76 
77   mdata->real_name = mutt_str_dup(name);
78 
79   imap_fix_path(adata->delim, name, buf, sizeof(buf));
80   if (buf[0] == '\0')
81     mutt_str_copy(buf, "INBOX", sizeof(buf));
82   mdata->name = mutt_str_dup(buf);
83 
84   imap_munge_mbox_name(adata->unicode, buf, sizeof(buf), mdata->name);
85   mdata->munge_name = mutt_str_dup(buf);
86 
87   mdata->reopen &= IMAP_REOPEN_ALLOW;
88 
89   STAILQ_INIT(&mdata->flags);
90 
91 #ifdef USE_HCACHE
92   imap_hcache_open(adata, mdata);
93   if (mdata->hcache)
94   {
95     size_t dlen = 0;
96     void *uidvalidity = mutt_hcache_fetch_raw(mdata->hcache, "/UIDVALIDITY", 12, &dlen);
97     void *uidnext = mutt_hcache_fetch_raw(mdata->hcache, "/UIDNEXT", 8, &dlen);
98     unsigned long long *modseq =
99         mutt_hcache_fetch_raw(mdata->hcache, "/MODSEQ", 7, &dlen);
100     if (uidvalidity)
101     {
102       mdata->uidvalidity = *(uint32_t *) uidvalidity;
103       mdata->uid_next = uidnext ? *(unsigned int *) uidnext : 0;
104       mdata->modseq = modseq ? *modseq : 0;
105       mutt_debug(LL_DEBUG3, "hcache uidvalidity %u, uidnext %u, modseq %llu\n",
106                  mdata->uidvalidity, mdata->uid_next, mdata->modseq);
107     }
108     mutt_hcache_free_raw(mdata->hcache, &uidvalidity);
109     mutt_hcache_free_raw(mdata->hcache, &uidnext);
110     mutt_hcache_free_raw(mdata->hcache, (void **) &modseq);
111     imap_hcache_close(mdata);
112   }
113 #endif
114 
115   return mdata;
116 }
117