NeoMutt
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
mailbox.c
Go to the documentation of this file.
1
31#include "config.h"
32#include <assert.h>
33#include <sys/stat.h>
34#include "config/lib.h"
35#include "email/lib.h"
36#include "mailbox.h"
37#include "neomutt.h"
38
40static const struct Mapping MailboxTypes[] = {
41 // clang-format off
42 { "compressed", MUTT_COMPRESSED },
43 { "imap", MUTT_IMAP },
44 { "maildir", MUTT_MAILDIR },
45 { "mbox", MUTT_MBOX },
46 { "mh", MUTT_MH },
47 { "mmdf", MUTT_MMDF },
48 { "nntp", MUTT_NNTP },
49 { "notmuch", MUTT_NOTMUCH },
50 { "pop", MUTT_POP },
51 { NULL, 0 },
52 // clang-format on
53};
54
59int mailbox_gen(void)
60{
61 static int gen = 0;
62 return gen++;
63}
64
69struct Mailbox *mailbox_new(void)
70{
71 struct Mailbox *m = mutt_mem_calloc(1, sizeof(struct Mailbox));
72
73 buf_init(&m->pathbuf);
74 m->notify = notify_new();
75
76 m->email_max = 25;
77 m->emails = mutt_mem_calloc(m->email_max, sizeof(struct Email *));
78 m->v2r = mutt_mem_calloc(m->email_max, sizeof(int));
79 m->gen = mailbox_gen();
80 m->notify_user = true;
81 m->poll_new_mail = true;
82
83 return m;
84}
85
90void mailbox_free(struct Mailbox **ptr)
91{
92 if (!ptr || !*ptr)
93 return;
94
95 struct Mailbox *m = *ptr;
96
97 const bool do_free = (m->opened == 0) && !m->visible;
98
99 mutt_debug(LL_DEBUG3, "%sfreeing %s mailbox %s with refcount %d\n",
100 do_free ? "" : "not ", m->visible ? "visible" : "invisible",
101 buf_string(&m->pathbuf), m->opened);
102
103 if (!do_free)
104 {
105 return;
106 }
107
108 mutt_debug(LL_NOTIFY, "NT_MAILBOX_DELETE: %s %p\n",
109 mailbox_get_type_name(m->type), (void *) m);
110 struct EventMailbox ev_m = { m };
112
113 mutt_debug(LL_NOTIFY, "NT_EMAIL_DELETE_ALL\n");
114 struct EventEmail ev_e = { 0, NULL };
116
117 for (size_t i = 0; i < m->email_max; i++)
118 email_free(&m->emails[i]);
119
120 if (m->mdata_free && m->mdata)
121 m->mdata_free(&m->mdata);
122
123 buf_dealloc(&m->pathbuf);
124 cs_subset_free(&m->sub);
125 FREE(&m->name);
126 FREE(&m->realpath);
127 FREE(&m->emails);
128 FREE(&m->v2r);
129 notify_free(&m->notify);
131
132 /* The NT_MAILBOX_DELETE notification might already have caused *ptr to be NULL,
133 * so call free() on the m pointer */
134 *ptr = NULL;
135 FREE(&m);
136}
137
143struct Mailbox *mailbox_find(const char *path)
144{
145 if (!path)
146 return NULL;
147
148 struct stat st = { 0 };
149 struct stat st_tmp = { 0 };
150
151 if (stat(path, &st) != 0)
152 return NULL;
153
154 struct MailboxList ml = STAILQ_HEAD_INITIALIZER(ml);
156 struct MailboxNode *np = NULL;
157 struct Mailbox *m = NULL;
158 STAILQ_FOREACH(np, &ml, entries)
159 {
160 if ((stat(mailbox_path(np->mailbox), &st_tmp) == 0) &&
161 (st.st_dev == st_tmp.st_dev) && (st.st_ino == st_tmp.st_ino))
162 {
163 m = np->mailbox;
164 break;
165 }
166 }
168
169 return m;
170}
171
180struct Mailbox *mailbox_find_name(const char *name)
181{
182 if (!name)
183 return NULL;
184
185 struct MailboxList ml = STAILQ_HEAD_INITIALIZER(ml);
187 struct MailboxNode *np = NULL;
188 struct Mailbox *m = NULL;
189 STAILQ_FOREACH(np, &ml, entries)
190 {
191 if (mutt_str_equal(np->mailbox->name, name))
192 {
193 m = np->mailbox;
194 break;
195 }
196 }
198
199 return m;
200}
201
208void mailbox_update(struct Mailbox *m)
209{
210 struct stat st = { 0 };
211
212 if (!m)
213 return;
214
215 if (stat(mailbox_path(m), &st) == 0)
216 m->size = (off_t) st.st_size;
217 else
218 m->size = 0;
219}
220
226void mailbox_changed(struct Mailbox *m, enum NotifyMailbox action)
227{
228 if (!m)
229 return;
230
231 mutt_debug(LL_NOTIFY, "NT_MAILBOX_CHANGE: %s %p\n",
232 mailbox_get_type_name(m->type), (void *) m);
233 struct EventMailbox ev_m = { m };
234 notify_send(m->notify, NT_MAILBOX, action, &ev_m);
235}
236
242void mailbox_size_add(struct Mailbox *m, const struct Email *e)
243{
244 m->size += email_size(e);
245}
246
252void mailbox_size_sub(struct Mailbox *m, const struct Email *e)
253{
254 m->size -= email_size(e);
255}
256
263bool mailbox_set_subset(struct Mailbox *m, struct ConfigSubset *sub)
264{
265 if (!m || m->sub || !sub)
266 return false;
267
268 m->sub = cs_subset_new(m->name, sub, m->notify);
270 return true;
271}
272
277{
278 struct Email *arr[10];
279 size_t idx;
280};
281
283static struct EmailGarbageCollector GC = { 0 };
284
291void mailbox_gc_add(struct Email *e)
292{
293 assert(e);
294 if (GC.idx == mutt_array_size(GC.arr))
295 {
297 }
298 GC.arr[GC.idx] = e;
299 GC.idx++;
300}
301
306{
307 for (size_t i = 0; i < GC.idx; i++)
308 {
309 email_free(&GC.arr[i]);
310 }
311 GC.idx = 0;
312}
313
319const char *mailbox_get_type_name(enum MailboxType type)
320{
321 const char *name = mutt_map_get_name(type, MailboxTypes);
322 if (name)
323 return name;
324 return "UNKNOWN";
325}
void buf_dealloc(struct Buffer *buf)
Release the memory allocated by a buffer.
Definition: buffer.c:389
struct Buffer * buf_init(struct Buffer *buf)
Initialise a new Buffer.
Definition: buffer.c:55
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:93
Convenience wrapper for the config headers.
size_t email_size(const struct Email *e)
Compute the size of an email.
Definition: email.c:125
void email_free(struct Email **ptr)
Free an Email.
Definition: email.c:44
Structs that make up an email.
@ NT_EMAIL_DELETE_ALL
All the Emails are about to be deleted.
Definition: email.h:149
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
@ LL_DEBUG3
Log at debug level 3.
Definition: logging2.h:45
@ LL_NOTIFY
Log of notifications.
Definition: logging2.h:48
bool mailbox_set_subset(struct Mailbox *m, struct ConfigSubset *sub)
Set a Mailbox's Config Subset.
Definition: mailbox.c:263
void mailbox_gc_run(void)
Run the garbage-collection.
Definition: mailbox.c:305
void mailbox_size_add(struct Mailbox *m, const struct Email *e)
Add an email's size to the total size of a Mailbox.
Definition: mailbox.c:242
void mailbox_size_sub(struct Mailbox *m, const struct Email *e)
Subtract an email's size from the total size of a Mailbox.
Definition: mailbox.c:252
int mailbox_gen(void)
Get the next generation number.
Definition: mailbox.c:59
struct Mailbox * mailbox_new(void)
Create a new Mailbox.
Definition: mailbox.c:69
void mailbox_gc_add(struct Email *e)
Add an Email to the garbage-collection set.
Definition: mailbox.c:291
void mailbox_update(struct Mailbox *m)
Get the mailbox's current size.
Definition: mailbox.c:208
void mailbox_free(struct Mailbox **ptr)
Free a Mailbox.
Definition: mailbox.c:90
static const struct Mapping MailboxTypes[]
Lookups for Mailbox types.
Definition: mailbox.c:40
struct Mailbox * mailbox_find_name(const char *name)
Find the mailbox with a given name.
Definition: mailbox.c:180
static struct EmailGarbageCollector GC
Set of Emails to be deleted.
Definition: mailbox.c:283
void mailbox_changed(struct Mailbox *m, enum NotifyMailbox action)
Notify observers of a change to a Mailbox.
Definition: mailbox.c:226
const char * mailbox_get_type_name(enum MailboxType type)
Get the type of a Mailbox.
Definition: mailbox.c:319
struct Mailbox * mailbox_find(const char *path)
Find the mailbox with a given path.
Definition: mailbox.c:143
Representation of a mailbox.
NotifyMailbox
Types of Mailbox Event.
Definition: mailbox.h:168
@ NT_MAILBOX_DELETE
Mailbox is about to be deleted.
Definition: mailbox.h:170
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox's path string.
Definition: mailbox.h:210
MailboxType
Supported mailbox formats.
Definition: mailbox.h:41
@ MUTT_NOTMUCH
'Notmuch' (virtual) Mailbox type
Definition: mailbox.h:51
@ MUTT_MMDF
'mmdf' Mailbox type
Definition: mailbox.h:46
@ MUTT_POP
'POP3' Mailbox type
Definition: mailbox.h:52
@ MUTT_MH
'MH' Mailbox type
Definition: mailbox.h:47
@ MUTT_NNTP
'NNTP' (Usenet) Mailbox type
Definition: mailbox.h:49
@ MUTT_IMAP
'IMAP' Mailbox type
Definition: mailbox.h:50
@ MUTT_MBOX
'mbox' Mailbox type
Definition: mailbox.h:45
@ MUTT_MAILBOX_ANY
Match any Mailbox type.
Definition: mailbox.h:42
@ MUTT_COMPRESSED
Compressed file Mailbox type.
Definition: mailbox.h:53
@ MUTT_MAILDIR
'Maildir' Mailbox type
Definition: mailbox.h:48
const char * mutt_map_get_name(int val, const struct Mapping *map)
Lookup a string for a constant.
Definition: mapping.c:42
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
#define FREE(x)
Definition: memory.h:45
#define mutt_array_size(x)
Definition: memory.h:38
struct Notify * notify_new(void)
Create a new notifications handler.
Definition: notify.c:62
bool notify_send(struct Notify *notify, enum NotifyType event_type, int event_subtype, void *event_data)
Send out a notification message.
Definition: notify.c:173
void notify_free(struct Notify **ptr)
Free a notification handler.
Definition: notify.c:75
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:798
void neomutt_mailboxlist_clear(struct MailboxList *ml)
Free a Mailbox List.
Definition: neomutt.c:162
size_t neomutt_mailboxlist_get_all(struct MailboxList *head, struct NeoMutt *n, enum MailboxType type)
Get a List of all Mailboxes.
Definition: neomutt.c:185
Container for Accounts, Notifications.
@ NT_EMAIL
Email has changed, NotifyEmail, EventEmail.
Definition: notify_type.h:44
@ NT_MAILBOX
Mailbox has changed, NotifyMailbox, EventMailbox.
Definition: notify_type.h:49
#define STAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:324
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:352
A set of inherited config items.
Definition: subset.h:47
enum ConfigScope scope
Scope of Subset, e.g. SET_SCOPE_ACCOUNT.
Definition: subset.h:49
Email garbage collection.
Definition: mailbox.c:277
size_t idx
Current position.
Definition: mailbox.c:279
struct Email * arr[10]
Array of Emails to be deleted.
Definition: mailbox.c:278
The envelope/body of an email.
Definition: email.h:37
An Event that happened to an Email.
Definition: email.h:160
An Event that happened to a Mailbox.
Definition: mailbox.h:186
List of Mailboxes.
Definition: mailbox.h:153
struct Mailbox * mailbox
Mailbox in the list.
Definition: mailbox.h:154
A mailbox.
Definition: mailbox.h:79
void(* mdata_free)(void **ptr)
Free the private data attached to the Mailbox.
Definition: mailbox.h:142
char * realpath
Used for duplicate detection, context comparison, and the sidebar.
Definition: mailbox.h:81
int * v2r
Mapping from virtual to real msgno.
Definition: mailbox.h:98
int email_max
Size of emails array.
Definition: mailbox.h:97
enum MailboxType type
Mailbox type.
Definition: mailbox.h:102
bool poll_new_mail
Check for new mail.
Definition: mailbox.h:114
void * mdata
Driver specific data.
Definition: mailbox.h:133
struct Email ** emails
Array of Emails.
Definition: mailbox.h:96
char * name
A short name for the Mailbox.
Definition: mailbox.h:82
struct Notify * notify
Notifications: NotifyMailbox, EventMailbox.
Definition: mailbox.h:144
bool notify_user
Notify the user of new mail.
Definition: mailbox.h:112
struct Buffer pathbuf
Path of the Mailbox.
Definition: mailbox.h:80
off_t size
Size of the Mailbox.
Definition: mailbox.h:84
bool visible
True if a result of "mailboxes".
Definition: mailbox.h:131
int opened
Number of times mailbox is opened.
Definition: mailbox.h:129
int gen
Generation number, for sorting.
Definition: mailbox.h:146
struct ConfigSubset * sub
Inherited config items.
Definition: mailbox.h:83
Mapping between user-readable string and a constant.
Definition: mapping.h:32
Container for Accounts, Notifications.
Definition: neomutt.h:41
struct ConfigSubset * cs_subset_new(const char *name, struct ConfigSubset *sub_parent, struct Notify *not_parent)
Create a new Config Subset.
Definition: subset.c:143
void cs_subset_free(struct ConfigSubset **ptr)
Free a Config Subset.
Definition: subset.c:99
@ SET_SCOPE_MAILBOX
This Config is Mailbox-specific.
Definition: subset.h:40