NeoMutt  2024-11-14-138-ge5ca67
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 <sys/stat.h>
33#include "config/lib.h"
34#include "email/lib.h"
35#include "mailbox.h"
36#include "neomutt.h"
37
39static const struct Mapping MailboxTypes[] = {
40 // clang-format off
41 { "compressed", MUTT_COMPRESSED },
42 { "imap", MUTT_IMAP },
43 { "maildir", MUTT_MAILDIR },
44 { "mbox", MUTT_MBOX },
45 { "mh", MUTT_MH },
46 { "mmdf", MUTT_MMDF },
47 { "nntp", MUTT_NNTP },
48 { "notmuch", MUTT_NOTMUCH },
49 { "pop", MUTT_POP },
50 { NULL, 0 },
51 // clang-format on
52};
53
58int mailbox_gen(void)
59{
60 static int gen = 0;
61 return gen++;
62}
63
68struct Mailbox *mailbox_new(void)
69{
70 struct Mailbox *m = MUTT_MEM_CALLOC(1, struct Mailbox);
71
72 buf_init(&m->pathbuf);
73 m->notify = notify_new();
74
75 m->email_max = 25;
76 m->emails = MUTT_MEM_CALLOC(m->email_max, struct Email *);
77 m->v2r = MUTT_MEM_CALLOC(m->email_max, int);
78 m->gen = mailbox_gen();
79 m->notify_user = true;
80 m->poll_new_mail = true;
81
82 return m;
83}
84
89void mailbox_free(struct Mailbox **ptr)
90{
91 if (!ptr || !*ptr)
92 return;
93
94 struct Mailbox *m = *ptr;
95
96 const bool do_free = (m->opened == 0) && !m->visible;
97
98 mutt_debug(LL_DEBUG3, "%sfreeing %s mailbox %s with refcount %d\n",
99 do_free ? "" : "not ", m->visible ? "visible" : "invisible",
100 buf_string(&m->pathbuf), m->opened);
101
102 if (!do_free)
103 {
104 return;
105 }
106
107 mutt_debug(LL_NOTIFY, "NT_MAILBOX_DELETE: %s %p\n",
108 mailbox_get_type_name(m->type), (void *) m);
109 struct EventMailbox ev_m = { m };
111
112 mutt_debug(LL_NOTIFY, "NT_EMAIL_DELETE_ALL\n");
113 struct EventEmail ev_e = { 0, NULL };
115
116 for (size_t i = 0; i < m->email_max; i++)
117 email_free(&m->emails[i]);
118
119 m->email_max = 0;
120 m->msg_count = 0;
121 m->msg_deleted = 0;
122 m->msg_flagged = 0;
123 m->msg_new = 0;
124 m->msg_tagged = 0;
125 m->msg_unread = 0;
126
127 if (m->mdata_free && m->mdata)
128 m->mdata_free(&m->mdata);
129
130 buf_dealloc(&m->pathbuf);
131 cs_subset_free(&m->sub);
132 FREE(&m->name);
133 FREE(&m->realpath);
134 FREE(&m->emails);
135 FREE(&m->v2r);
136 notify_free(&m->notify);
138
139 /* The NT_MAILBOX_DELETE notification might already have caused *ptr to be NULL,
140 * so call free() on the m pointer */
141 *ptr = NULL;
142 FREE(&m);
143}
144
150struct Mailbox *mailbox_find(const char *path)
151{
152 if (!path)
153 return NULL;
154
155 struct stat st = { 0 };
156 struct stat st_tmp = { 0 };
157
158 if (stat(path, &st) != 0)
159 return NULL;
160
161 struct MailboxList ml = STAILQ_HEAD_INITIALIZER(ml);
163 struct MailboxNode *np = NULL;
164 struct Mailbox *m = NULL;
165 STAILQ_FOREACH(np, &ml, entries)
166 {
167 if ((stat(mailbox_path(np->mailbox), &st_tmp) == 0) &&
168 (st.st_dev == st_tmp.st_dev) && (st.st_ino == st_tmp.st_ino))
169 {
170 m = np->mailbox;
171 break;
172 }
173 }
175
176 return m;
177}
178
187struct Mailbox *mailbox_find_name(const char *name)
188{
189 if (!name)
190 return NULL;
191
192 struct MailboxList ml = STAILQ_HEAD_INITIALIZER(ml);
194 struct MailboxNode *np = NULL;
195 struct Mailbox *m = NULL;
196 STAILQ_FOREACH(np, &ml, entries)
197 {
198 if (mutt_str_equal(np->mailbox->name, name))
199 {
200 m = np->mailbox;
201 break;
202 }
203 }
205
206 return m;
207}
208
215void mailbox_update(struct Mailbox *m)
216{
217 struct stat st = { 0 };
218
219 if (!m)
220 return;
221
222 if (stat(mailbox_path(m), &st) == 0)
223 m->size = (off_t) st.st_size;
224 else
225 m->size = 0;
226}
227
233void mailbox_changed(struct Mailbox *m, enum NotifyMailbox action)
234{
235 if (!m)
236 return;
237
238 mutt_debug(LL_NOTIFY, "NT_MAILBOX_CHANGE: %s %p\n",
239 mailbox_get_type_name(m->type), (void *) m);
240 struct EventMailbox ev_m = { m };
241 notify_send(m->notify, NT_MAILBOX, action, &ev_m);
242}
243
249void mailbox_size_add(struct Mailbox *m, const struct Email *e)
250{
251 m->size += email_get_size(e);
252}
253
259void mailbox_size_sub(struct Mailbox *m, const struct Email *e)
260{
261 m->size -= email_get_size(e);
262}
263
270bool mailbox_set_subset(struct Mailbox *m, struct ConfigSubset *sub)
271{
272 if (!m || m->sub || !sub)
273 return false;
274
275 m->sub = cs_subset_new(m->name, sub, m->notify);
277 return true;
278}
279
284{
285 struct Email *arr[10];
286 size_t idx;
287};
288
290static struct EmailGarbageCollector GC = { 0 };
291
298void mailbox_gc_add(struct Email *e)
299{
300 ASSERT(e);
301 if (GC.idx == mutt_array_size(GC.arr))
302 {
304 }
305 GC.arr[GC.idx] = e;
306 GC.idx++;
307}
308
313{
314 for (size_t i = 0; i < GC.idx; i++)
315 {
316 email_free(&GC.arr[i]);
317 }
318 GC.idx = 0;
319}
320
326const char *mailbox_get_type_name(enum MailboxType type)
327{
328 const char *name = mutt_map_get_name(type, MailboxTypes);
329 if (name)
330 return name;
331 return "UNKNOWN";
332}
void buf_dealloc(struct Buffer *buf)
Release the memory allocated by a buffer.
Definition: buffer.c:377
struct Buffer * buf_init(struct Buffer *buf)
Initialise a new Buffer.
Definition: buffer.c:61
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
Convenience wrapper for the config headers.
bool mailbox_set_subset(struct Mailbox *m, struct ConfigSubset *sub)
Set a Mailbox's Config Subset.
Definition: mailbox.c:270
void mailbox_gc_run(void)
Run the garbage-collection.
Definition: mailbox.c:312
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:249
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:259
int mailbox_gen(void)
Get the next generation number.
Definition: mailbox.c:58
struct Mailbox * mailbox_new(void)
Create a new Mailbox.
Definition: mailbox.c:68
void mailbox_gc_add(struct Email *e)
Add an Email to the garbage-collection set.
Definition: mailbox.c:298
void mailbox_update(struct Mailbox *m)
Get the mailbox's current size.
Definition: mailbox.c:215
void mailbox_free(struct Mailbox **ptr)
Free a Mailbox.
Definition: mailbox.c:89
static const struct Mapping MailboxTypes[]
Lookups for Mailbox types.
Definition: mailbox.c:39
struct Mailbox * mailbox_find_name(const char *name)
Find the mailbox with a given name.
Definition: mailbox.c:187
static struct EmailGarbageCollector GC
Set of Emails to be deleted.
Definition: mailbox.c:290
void mailbox_changed(struct Mailbox *m, enum NotifyMailbox action)
Notify observers of a change to a Mailbox.
Definition: mailbox.c:233
const char * mailbox_get_type_name(enum MailboxType type)
Get the type of a Mailbox.
Definition: mailbox.c:326
struct Mailbox * mailbox_find(const char *path)
Find the mailbox with a given path.
Definition: mailbox.c:150
NotifyMailbox
Types of Mailbox Event.
Definition: mailbox.h:181
@ NT_MAILBOX_DELETE
Mailbox is about to be deleted.
Definition: mailbox.h:183
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox's path string.
Definition: mailbox.h:223
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
size_t email_get_size(const struct Email *e)
Compute the size of an email.
Definition: email.c:121
void email_free(struct Email **ptr)
Free an Email.
Definition: email.c:46
Structs that make up an email.
@ NT_EMAIL_DELETE_ALL
All the Emails are about to be deleted.
Definition: email.h:185
#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
Maildir Mailbox.
const char * mutt_map_get_name(int val, const struct Mapping *map)
Lookup a string for a constant.
Definition: mapping.c:42
#define FREE(x)
Definition: memory.h:55
#define MUTT_MEM_CALLOC(n, type)
Definition: memory.h:40
#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:660
void neomutt_mailboxlist_clear(struct MailboxList *ml)
Free a Mailbox List.
Definition: neomutt.c:168
size_t neomutt_mailboxlist_get_all(struct MailboxList *head, struct NeoMutt *n, enum MailboxType type)
Get a List of all Mailboxes.
Definition: neomutt.c:191
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
#define ASSERT(COND)
Definition: signal2.h:58
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:284
size_t idx
Current position.
Definition: mailbox.c:286
struct Email * arr[10]
Array of Emails to be deleted.
Definition: mailbox.c:285
The envelope/body of an email.
Definition: email.h:39
An Event that happened to an Email.
Definition: email.h:196
An Event that happened to a Mailbox.
Definition: mailbox.h:199
List of Mailboxes.
Definition: mailbox.h:166
struct Mailbox * mailbox
Mailbox in the list.
Definition: mailbox.h:167
A mailbox.
Definition: mailbox.h:79
void(* mdata_free)(void **ptr)
Definition: mailbox.h:143
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 msg_new
Number of new messages.
Definition: mailbox.h:92
int msg_count
Total number of messages.
Definition: mailbox.h:88
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:115
void * mdata
Driver specific data.
Definition: mailbox.h:132
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:145
bool notify_user
Notify the user of new mail.
Definition: mailbox.h:113
struct Buffer pathbuf
Path of the Mailbox.
Definition: mailbox.h:80
int msg_deleted
Number of deleted messages.
Definition: mailbox.h:93
off_t size
Size of the Mailbox.
Definition: mailbox.h:84
bool visible
True if a result of "mailboxes".
Definition: mailbox.h:130
int msg_flagged
Number of flagged messages.
Definition: mailbox.h:90
int opened
Number of times mailbox is opened.
Definition: mailbox.h:128
int msg_tagged
How many messages are tagged?
Definition: mailbox.h:94
int msg_unread
Number of unread messages.
Definition: mailbox.h:89
int gen
Generation number, for sorting.
Definition: mailbox.h:147
struct ConfigSubset * sub
Inherited config items.
Definition: mailbox.h:83
Mapping between user-readable string and a constant.
Definition: mapping.h:33
Container for Accounts, Notifications.
Definition: neomutt.h:42
struct ConfigSubset * cs_subset_new(const char *name, struct ConfigSubset *sub_parent, struct Notify *not_parent)
Create a new Config Subset.
Definition: subset.c:152
void cs_subset_free(struct ConfigSubset **ptr)
Free a Config Subset.
Definition: subset.c:108
@ SET_SCOPE_MAILBOX
This Config is Mailbox-specific.
Definition: subset.h:40