NeoMutt  2022-04-29-145-g9b6a0e
Teaching an old dog new tricks
DOXYGEN
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 
40 static 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 
58 int mailbox_gen(void)
59 {
60  static int gen = 0;
61  return gen++;
62 }
63 
68 struct Mailbox *mailbox_new(void)
69 {
70  struct Mailbox *m = mutt_mem_calloc(1, sizeof(struct Mailbox));
71 
73  m->notify = notify_new();
74 
75  m->email_max = 25;
76  m->emails = mutt_mem_calloc(m->email_max, sizeof(struct Email *));
77  m->v2r = mutt_mem_calloc(m->email_max, sizeof(int));
78  m->gen = mailbox_gen();
79 
80  return m;
81 }
82 
87 void mailbox_free(struct Mailbox **ptr)
88 {
89  if (!ptr || !*ptr)
90  return;
91 
92  struct Mailbox *m = *ptr;
93 
94  const bool do_free = (m->opened == 0) && !m->visible;
95 
96  mutt_debug(LL_DEBUG3, "%sfreeing %s mailbox %s with refcount %d\n",
97  do_free ? "" : "not ", m->visible ? "visible" : "invisible",
99 
100  if (!do_free)
101  {
102  return;
103  }
104 
105  mutt_debug(LL_NOTIFY, "NT_MAILBOX_DELETE: %s %p\n", mailbox_get_type_name(m->type), m);
106  struct EventMailbox ev_m = { m };
108 
109  mutt_debug(LL_NOTIFY, "NT_EMAIL_DELETE_ALL\n");
110  struct EventEmail ev_e = { 0, NULL };
112 
113  for (size_t i = 0; i < m->email_max; i++)
114  email_free(&m->emails[i]);
115 
116  if (m->mdata_free && m->mdata)
117  m->mdata_free(&m->mdata);
118 
120  cs_subset_free(&m->sub);
121  FREE(&m->name);
122  FREE(&m->realpath);
123  FREE(&m->emails);
124  FREE(&m->v2r);
125  notify_free(&m->notify);
126  mailbox_gc_run();
127 
128  /* The NT_MAILBOX_DELETE notification might already have caused *ptr to be NULL,
129  * so call free() on the m pointer */
130  *ptr = NULL;
131  FREE(&m);
132 }
133 
139 struct Mailbox *mailbox_find(const char *path)
140 {
141  if (!path)
142  return NULL;
143 
144  struct stat st = { 0 };
145  struct stat st_tmp = { 0 };
146 
147  if (stat(path, &st) != 0)
148  return NULL;
149 
150  struct MailboxList ml = STAILQ_HEAD_INITIALIZER(ml);
152  struct MailboxNode *np = NULL;
153  struct Mailbox *m = NULL;
154  STAILQ_FOREACH(np, &ml, entries)
155  {
156  if ((stat(mailbox_path(np->mailbox), &st_tmp) == 0) &&
157  (st.st_dev == st_tmp.st_dev) && (st.st_ino == st_tmp.st_ino))
158  {
159  m = np->mailbox;
160  break;
161  }
162  }
164 
165  return m;
166 }
167 
176 struct Mailbox *mailbox_find_name(const char *name)
177 {
178  if (!name)
179  return NULL;
180 
181  struct MailboxList ml = STAILQ_HEAD_INITIALIZER(ml);
183  struct MailboxNode *np = NULL;
184  struct Mailbox *m = NULL;
185  STAILQ_FOREACH(np, &ml, entries)
186  {
187  if (mutt_str_equal(np->mailbox->name, name))
188  {
189  m = np->mailbox;
190  break;
191  }
192  }
194 
195  return m;
196 }
197 
204 void mailbox_update(struct Mailbox *m)
205 {
206  struct stat st = { 0 };
207 
208  if (!m)
209  return;
210 
211  if (stat(mailbox_path(m), &st) == 0)
212  m->size = (off_t) st.st_size;
213  else
214  m->size = 0;
215 }
216 
222 void mailbox_changed(struct Mailbox *m, enum NotifyMailbox action)
223 {
224  if (!m)
225  return;
226 
227  mutt_debug(LL_NOTIFY, "NT_MAILBOX_CHANGE: %s %p\n", mailbox_get_type_name(m->type), m);
228  struct EventMailbox ev_m = { m };
229  notify_send(m->notify, NT_MAILBOX, action, &ev_m);
230 }
231 
237 void mailbox_size_add(struct Mailbox *m, const struct Email *e)
238 {
239  m->size += email_size(e);
240 }
241 
247 void mailbox_size_sub(struct Mailbox *m, const struct Email *e)
248 {
249  m->size -= email_size(e);
250 }
251 
258 bool mailbox_set_subset(struct Mailbox *m, struct ConfigSubset *sub)
259 {
260  if (!m || m->sub || !sub)
261  return false;
262 
263  m->sub = cs_subset_new(m->name, sub, m->notify);
265  return true;
266 }
267 
272 {
273  struct Email *arr[10];
274  size_t idx;
275 } gc = { 0 };
276 
282 void mailbox_gc_add(struct Email *e)
283 {
284  assert(e);
285  if (gc.idx == mutt_array_size(gc.arr))
286  {
287  mailbox_gc_run();
288  }
289  gc.arr[gc.idx] = e;
290  gc.idx++;
291 }
292 
296 void mailbox_gc_run(void)
297 {
298  for (size_t i = 0; i < gc.idx; i++)
299  {
300  email_free(&gc.arr[i]);
301  }
302  gc.idx = 0;
303 }
304 
310 const char *mailbox_get_type_name(enum MailboxType type)
311 {
312  const char *name = mutt_map_get_name(type, MailboxTypes);
313  if (name)
314  return name;
315  return "UNKNOWN";
316 }
void mutt_buffer_dealloc(struct Buffer *buf)
Release the memory allocated by a buffer.
Definition: buffer.c:292
struct Buffer * mutt_buffer_init(struct Buffer *buf)
Initialise a new Buffer.
Definition: buffer.c:48
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
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: logging.h:84
@ LL_DEBUG3
Log at debug level 3.
Definition: logging.h:42
@ LL_NOTIFY
Log of notifications.
Definition: logging.h:45
struct Mailbox * mailbox_new(void)
Create a new Mailbox.
Definition: mailbox.c:68
bool mailbox_set_subset(struct Mailbox *m, struct ConfigSubset *sub)
Set a Mailbox's Config Subset.
Definition: mailbox.c:258
void mailbox_gc_run(void)
Run the garbage-collection.
Definition: mailbox.c:296
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:237
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:247
static struct EmailGarbageCollector gc
int mailbox_gen(void)
Get the next generation number.
Definition: mailbox.c:58
void mailbox_gc_add(struct Email *e)
Add an Email to the garbage-collection set.
Definition: mailbox.c:282
void mailbox_update(struct Mailbox *m)
Get the mailbox's current size.
Definition: mailbox.c:204
void mailbox_free(struct Mailbox **ptr)
Free a Mailbox.
Definition: mailbox.c:87
static const struct Mapping MailboxTypes[]
Lookups for Mailbox types.
Definition: mailbox.c:40
const char * mailbox_get_type_name(enum MailboxType type)
Get the type of a Mailbox.
Definition: mailbox.c:310
struct Mailbox * mailbox_find_name(const char *name)
Find the mailbox with a given name.
Definition: mailbox.c:176
void mailbox_changed(struct Mailbox *m, enum NotifyMailbox action)
Notify observers of a change to a Mailbox.
Definition: mailbox.c:222
struct Mailbox * mailbox_find(const char *path)
Find the mailbox with a given path.
Definition: mailbox.c:139
Representation of a mailbox.
NotifyMailbox
Types of Mailbox Event.
Definition: mailbox.h:169
@ NT_MAILBOX_DELETE
Mailbox is about to be deleted.
Definition: mailbox.h:171
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox's path string.
Definition: mailbox.h:211
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:43
#define mutt_array_size(x)
Definition: memory.h:36
struct Notify * notify_new(void)
Create a new notifications handler.
Definition: notify.c:60
bool notify_send(struct Notify *notify, enum NotifyType event_type, int event_subtype, void *event_data)
Send out a notification message.
Definition: notify.c:171
void notify_free(struct Notify **ptr)
Free a notification handler.
Definition: notify.c:73
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:784
void neomutt_mailboxlist_clear(struct MailboxList *ml)
Free a Mailbox List.
Definition: neomutt.c:141
size_t neomutt_mailboxlist_get_all(struct MailboxList *head, struct NeoMutt *n, enum MailboxType type)
Get a List of all Mailboxes.
Definition: neomutt.c:164
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:272
size_t idx
Current position.
Definition: mailbox.c:274
struct Email * arr[10]
Array of Emails to be deleted.
Definition: mailbox.c:273
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:187
List of Mailboxes.
Definition: mailbox.h:154
struct Mailbox * mailbox
Mailbox in the list.
Definition: mailbox.h:155
A mailbox.
Definition: mailbox.h:79
void(* mdata_free)(void **ptr)
Free the private data attached to the Mailbox.
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 email_max
Number of pointers in emails.
Definition: mailbox.h:97
enum MailboxType type
Mailbox type.
Definition: mailbox.h:102
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:145
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:147
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:37
struct ConfigSubset * cs_subset_new(const char *name, struct ConfigSubset *sub_parent, struct Notify *not_parent)
Create a new Config Subset.
Definition: subset.c:144
void cs_subset_free(struct ConfigSubset **ptr)
Free a Config Subset.
Definition: subset.c:104
@ SET_SCOPE_MAILBOX
This Config is Mailbox-specific.
Definition: subset.h:40