NeoMutt  2025-12-11-236-g400a5f
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
mailbox.c File Reference

Representation of a mailbox. More...

#include "config.h"
#include <sys/stat.h>
#include "config/lib.h"
#include "email/lib.h"
#include "mailbox.h"
#include "neomutt.h"
+ Include dependency graph for mailbox.c:

Go to the source code of this file.

Data Structures

struct  EmailGarbageCollector
 Email garbage collection. More...
 

Functions

int mailbox_gen (void)
 Get the next generation number.
 
struct Mailboxmailbox_new (void)
 Create a new Mailbox.
 
void mailbox_free (struct Mailbox **ptr)
 Free a Mailbox.
 
struct Mailboxmailbox_find (const char *path)
 Find the mailbox with a given path.
 
struct Mailboxmailbox_find_name (const char *name)
 Find the mailbox with a given name.
 
void mailbox_update (struct Mailbox *m)
 Get the mailbox's current size.
 
void mailbox_changed (struct Mailbox *m, enum NotifyMailbox action)
 Notify observers of a change to a Mailbox.
 
void mailbox_size_add (struct Mailbox *m, const struct Email *e)
 Add an email's size to the total size of a Mailbox.
 
void mailbox_size_sub (struct Mailbox *m, const struct Email *e)
 Subtract an email's size from the total size of a Mailbox.
 
bool mailbox_set_subset (struct Mailbox *m, struct ConfigSubset *sub)
 Set a Mailbox's Config Subset.
 
void mailbox_gc_add (struct Email *e)
 Add an Email to the garbage-collection set.
 
void mailbox_gc_run (void)
 Run the garbage-collection.
 
const char * mailbox_get_type_name (enum MailboxType type)
 Get the type of a Mailbox.
 

Variables

static const struct Mapping MailboxTypes []
 Lookups for Mailbox types.
 
static struct EmailGarbageCollector GC = { 0 }
 Set of Emails to be deleted.
 

Detailed Description

Representation of a mailbox.

Authors
  • Kevin J. McCarthy
  • Richard Russon
  • Pietro Cerutti
  • Alejandro Colomar

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file mailbox.c.

Function Documentation

◆ mailbox_gen()

int mailbox_gen ( void )

Get the next generation number.

Return values
numUnique number

Definition at line 59 of file mailbox.c.

60{
61 static int gen = 0;
62 return gen++;
63}
+ Here is the caller graph for this function:

◆ mailbox_new()

struct Mailbox * mailbox_new ( void )

Create a new Mailbox.

Return values
ptrNew Mailbox

Definition at line 69 of file mailbox.c.

70{
71 struct Mailbox *m = MUTT_MEM_CALLOC(1, 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, struct Email *);
78 m->v2r = MUTT_MEM_CALLOC(m->email_max, int);
79 m->gen = mailbox_gen();
80 m->notify_user = true;
81 m->poll_new_mail = true;
82
83 return m;
84}
struct Buffer * buf_init(struct Buffer *buf)
Initialise a new Buffer.
Definition buffer.c:61
int mailbox_gen(void)
Get the next generation number.
Definition mailbox.c:59
#define MUTT_MEM_CALLOC(n, type)
Definition memory.h:52
struct Notify * notify_new(void)
Create a new notifications handler.
Definition notify.c:62
The envelope/body of an email.
Definition email.h:39
A mailbox.
Definition mailbox.h:78
int * v2r
Mapping from virtual to real msgno.
Definition mailbox.h:97
int email_max
Size of emails array.
Definition mailbox.h:96
bool poll_new_mail
Check for new mail.
Definition mailbox.h:114
struct Email ** emails
Array of Emails.
Definition mailbox.h:95
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:79
int gen
Generation number, for sorting.
Definition mailbox.h:146
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mailbox_free()

void mailbox_free ( struct Mailbox ** ptr)

Free a Mailbox.

Parameters
[out]ptrMailbox to free

Definition at line 90 of file mailbox.c.

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 };
111 notify_send(m->notify, NT_MAILBOX, NT_MAILBOX_DELETE, &ev_m);
112
113 mutt_debug(LL_NOTIFY, "NT_EMAIL_DELETE_ALL\n");
114 struct EventEmail ev_e = { 0, NULL };
115 notify_send(m->notify, NT_EMAIL, NT_EMAIL_DELETE_ALL, &ev_e);
116
117 for (size_t i = 0; i < m->email_max; i++)
118 email_free(&m->emails[i]);
119
120 m->email_max = 0;
121 m->msg_count = 0;
122 m->msg_deleted = 0;
123 m->msg_flagged = 0;
124 m->msg_new = 0;
125 m->msg_tagged = 0;
126 m->msg_unread = 0;
127
128 if (m->mdata_free && m->mdata)
129 m->mdata_free(&m->mdata);
130
131 buf_dealloc(&m->pathbuf);
132 cs_subset_free(&m->sub);
133 FREE(&m->name);
134 FREE(&m->realpath);
135 FREE(&m->emails);
136 FREE(&m->v2r);
137 notify_free(&m->notify);
139
140 /* The NT_MAILBOX_DELETE notification might already have caused *ptr to be NULL,
141 * so call free() on the m pointer */
142 *ptr = NULL;
143 FREE(&m);
144}
void buf_dealloc(struct Buffer *buf)
Release the memory allocated by a buffer.
Definition buffer.c:377
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
void mailbox_gc_run(void)
Run the garbage-collection.
Definition mailbox.c:311
const char * mailbox_get_type_name(enum MailboxType type)
Get the type of a Mailbox.
Definition mailbox.c:325
@ NT_MAILBOX_DELETE
Mailbox is about to be deleted.
Definition mailbox.h:173
void email_free(struct Email **ptr)
Free an Email.
Definition email.c:46
@ NT_EMAIL_DELETE_ALL
All the Emails are about to be deleted.
Definition email.h:185
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
@ LL_DEBUG3
Log at debug level 3.
Definition logging2.h:47
@ LL_NOTIFY
Log of notifications.
Definition logging2.h:50
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
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
@ NT_EMAIL
Email has changed, NotifyEmail, EventEmail.
Definition notify_type.h:44
@ NT_MAILBOX
Mailbox has changed, NotifyMailbox, EventMailbox.
Definition notify_type.h:49
An Event that happened to an Email.
Definition email.h:196
An Event that happened to a Mailbox.
Definition mailbox.h:189
bool visible
True if a result of "mailboxes".
Definition mailbox.h:129
int opened
Number of times mailbox is opened.
Definition mailbox.h:127
void cs_subset_free(struct ConfigSubset **ptr)
Free a Config Subset.
Definition subset.c:112
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mailbox_find()

struct Mailbox * mailbox_find ( const char * path)

Find the mailbox with a given path.

Parameters
pathPath to match
Return values
ptrMatching Mailbox

Definition at line 151 of file mailbox.c.

152{
153 if (!path)
154 return NULL;
155
156 struct stat st = { 0 };
157 struct stat st_tmp = { 0 };
158
159 if (stat(path, &st) != 0)
160 return NULL;
161
162 struct MailboxArray ma = neomutt_mailboxes_get(NeoMutt, MUTT_MAILBOX_ANY);
163 struct Mailbox **mp = NULL;
164 struct Mailbox *m = NULL;
165 ARRAY_FOREACH(mp, &ma)
166 {
167 if ((stat(mailbox_path(*mp), &st_tmp) == 0) &&
168 (st.st_dev == st_tmp.st_dev) && (st.st_ino == st_tmp.st_ino))
169 {
170 m = *mp;
171 break;
172 }
173 }
174
175 ARRAY_FREE(&ma); // Clean up the ARRAY, but not the Mailboxes
176 return m;
177}
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition array.h:223
#define ARRAY_FREE(head)
Release all memory.
Definition array.h:209
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox's path string.
Definition mailbox.h:213
@ MUTT_MAILBOX_ANY
Match any Mailbox type.
Definition mailbox.h:41
struct MailboxArray neomutt_mailboxes_get(struct NeoMutt *n, enum MailboxType type)
Get an Array of matching Mailboxes.
Definition neomutt.c:488
Container for Accounts, Notifications.
Definition neomutt.h:128
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mailbox_find_name()

struct Mailbox * mailbox_find_name ( const char * name)

Find the mailbox with a given name.

Parameters
nameName to match
Return values
ptrMatching Mailbox
NULLNo matching mailbox found
Note
This searches across all Accounts

Definition at line 187 of file mailbox.c.

188{
189 if (!name)
190 return NULL;
191
192 struct MailboxArray ma = neomutt_mailboxes_get(NeoMutt, MUTT_MAILBOX_ANY);
193 struct Mailbox **mp = NULL;
194 struct Mailbox *m = NULL;
195 ARRAY_FOREACH(mp, &ma)
196 {
197 if (mutt_str_equal((*mp)->name, name))
198 {
199 m = *mp;
200 break;
201 }
202 }
203
204 ARRAY_FREE(&ma); // Clean up the ARRAY, but not the Mailboxes
205 return m;
206}
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition string.c:662
char * name
A short name for the Mailbox.
Definition mailbox.h:81
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mailbox_update()

void mailbox_update ( struct Mailbox * m)

Get the mailbox's current size.

Parameters
mMailbox to check
Note
Only applies to local Mailboxes

Definition at line 214 of file mailbox.c.

215{
216 struct stat st = { 0 };
217
218 if (!m)
219 return;
220
221 if (stat(mailbox_path(m), &st) == 0)
222 m->size = (off_t) st.st_size;
223 else
224 m->size = 0;
225}
off_t size
Size of the Mailbox.
Definition mailbox.h:83
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mailbox_changed()

void mailbox_changed ( struct Mailbox * m,
enum NotifyMailbox action )

Notify observers of a change to a Mailbox.

Parameters
mMailbox
actionChange to Mailbox

Definition at line 232 of file mailbox.c.

233{
234 if (!m)
235 return;
236
237 mutt_debug(LL_NOTIFY, "NT_MAILBOX_CHANGE: %s %p\n",
238 mailbox_get_type_name(m->type), (void *) m);
239 struct EventMailbox ev_m = { m };
240 notify_send(m->notify, NT_MAILBOX, action, &ev_m);
241}
enum MailboxType type
Mailbox type.
Definition mailbox.h:101
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mailbox_size_add()

void mailbox_size_add ( struct Mailbox * m,
const struct Email * e )

Add an email's size to the total size of a Mailbox.

Parameters
mMailbox
eEmail

Definition at line 248 of file mailbox.c.

249{
250 m->size += email_get_size(e);
251}
size_t email_get_size(const struct Email *e)
Compute the size of an email.
Definition email.c:121
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mailbox_size_sub()

void mailbox_size_sub ( struct Mailbox * m,
const struct Email * e )

Subtract an email's size from the total size of a Mailbox.

Parameters
mMailbox
eEmail

Definition at line 258 of file mailbox.c.

259{
260 m->size -= email_get_size(e);
261}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mailbox_set_subset()

bool mailbox_set_subset ( struct Mailbox * m,
struct ConfigSubset * sub )

Set a Mailbox's Config Subset.

Parameters
mMailbox
subParent Config Subset
Return values
trueSuccess

Definition at line 269 of file mailbox.c.

270{
271 if (!m || m->sub || !sub)
272 return false;
273
274 m->sub = cs_subset_new(m->name, sub, m->notify);
276 return true;
277}
enum ConfigScope scope
Scope of Subset, e.g. SET_SCOPE_ACCOUNT.
Definition subset.h:48
struct ConfigSubset * sub
Inherited config items.
Definition mailbox.h:82
struct ConfigSubset * cs_subset_new(const char *name, struct ConfigSubset *sub_parent, struct Notify *not_parent)
Create a new Config Subset.
Definition subset.c:158
@ SET_SCOPE_MAILBOX
This Config is Mailbox-specific.
Definition subset.h:39
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mailbox_gc_add()

void mailbox_gc_add ( struct Email * e)

Add an Email to the garbage-collection set.

Parameters
eEmail
Precondition
e is not NULL

Definition at line 297 of file mailbox.c.

298{
299 ASSERT(e);
300 if (GC.idx == countof(GC.arr))
301 {
303 }
304 GC.arr[GC.idx] = e;
305 GC.idx++;
306}
static struct EmailGarbageCollector GC
Set of Emails to be deleted.
Definition mailbox.c:289
#define countof(x)
Definition memory.h:49
#define ASSERT(COND)
Definition signal2.h:59
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mailbox_gc_run()

void mailbox_gc_run ( void )

Run the garbage-collection.

Definition at line 311 of file mailbox.c.

312{
313 for (size_t i = 0; i < GC.idx; i++)
314 {
315 email_free(&GC.arr[i]);
316 }
317 GC.idx = 0;
318}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mailbox_get_type_name()

const char * mailbox_get_type_name ( enum MailboxType type)

Get the type of a Mailbox.

Parameters
typeMailbox type, e.g. MUTT_IMAP
Return values
ptrString describing Mailbox type

Definition at line 325 of file mailbox.c.

326{
327 const char *name = mutt_map_get_name(type, MailboxTypes);
328 if (name)
329 return name;
330 return "UNKNOWN";
331}
static const struct Mapping MailboxTypes[]
Lookups for Mailbox types.
Definition mailbox.c:40
const char * mutt_map_get_name(int val, const struct Mapping *map)
Lookup a string for a constant.
Definition mapping.c:42
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ MailboxTypes

const struct Mapping MailboxTypes[]
static
Initial value:
= {
{ "compressed", MUTT_COMPRESSED },
{ "imap", MUTT_IMAP },
{ "maildir", MUTT_MAILDIR },
{ "mbox", MUTT_MBOX },
{ "mh", MUTT_MH },
{ "mmdf", MUTT_MMDF },
{ "nntp", MUTT_NNTP },
{ "notmuch", MUTT_NOTMUCH },
{ "pop", MUTT_POP },
{ NULL, 0 },
}
@ MUTT_NOTMUCH
'Notmuch' (virtual) Mailbox type
Definition mailbox.h:50
@ MUTT_MMDF
'mmdf' Mailbox type
Definition mailbox.h:45
@ MUTT_POP
'POP3' Mailbox type
Definition mailbox.h:51
@ MUTT_MH
'MH' Mailbox type
Definition mailbox.h:46
@ MUTT_NNTP
'NNTP' (Usenet) Mailbox type
Definition mailbox.h:48
@ MUTT_IMAP
'IMAP' Mailbox type
Definition mailbox.h:49
@ MUTT_MBOX
'mbox' Mailbox type
Definition mailbox.h:44
@ MUTT_COMPRESSED
Compressed file Mailbox type.
Definition mailbox.h:52
@ MUTT_MAILDIR
'Maildir' Mailbox type
Definition mailbox.h:47

Lookups for Mailbox types.

Definition at line 40 of file mailbox.c.

40 {
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};

◆ GC

struct EmailGarbageCollector GC = { 0 }
static

Set of Emails to be deleted.

Definition at line 289 of file mailbox.c.

289{ 0 };