forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailbox.c
220 lines (189 loc) · 4.85 KB
/
mailbox.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
* @file
* Representation of a mailbox
*
* @authors
* Copyright (C) 1996-2000,2010,2013 Michael R. Elkins <[email protected]>
* Copyright (C) 2016-2017 Kevin J. McCarthy <[email protected]>
* Copyright (C) 2018-2019 Richard Russon <[email protected]>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* 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/>.
*/
/**
* @page core_mailbox Representation of a Mailbox
*
* Representation of a Mailbox
*/
#include "config.h"
#include <sys/stat.h>
#include "config/lib.h"
#include "email/lib.h"
#include "mailbox.h"
#include "neomutt.h"
/**
* mailbox_new - Create a new Mailbox
* @retval ptr New Mailbox
*/
struct Mailbox *mailbox_new(void)
{
static int gen = 0;
struct Mailbox *m = mutt_mem_calloc(1, sizeof(struct Mailbox));
mutt_buffer_init(&m->pathbuf);
m->notify = notify_new();
m->email_max = 25;
m->emails = mutt_mem_calloc(m->email_max, sizeof(struct Email *));
m->v2r = mutt_mem_calloc(m->email_max, sizeof(int));
m->gen = gen++;
return m;
}
/**
* mailbox_free - Free a Mailbox
* @param[out] ptr Mailbox to free
*/
void mailbox_free(struct Mailbox **ptr)
{
if (!ptr || !*ptr)
return;
struct Mailbox *m = *ptr;
mailbox_changed(m, NT_MAILBOX_CLOSED);
if (m->mdata && m->mdata_free)
m->mdata_free(&m->mdata);
for (size_t i = 0; i < m->email_max; i++)
email_free(&m->emails[i]);
mutt_buffer_dealloc(&m->pathbuf);
cs_subset_free(&m->sub);
FREE(&m->name);
FREE(&m->realpath);
FREE(&m->emails);
FREE(&m->v2r);
notify_free(&m->notify);
FREE(ptr);
}
/**
* mailbox_find - Find the mailbox with a given path
* @param path Path to match
* @retval ptr Matching Mailbox
*/
struct Mailbox *mailbox_find(const char *path)
{
if (!path)
return NULL;
struct stat sb;
struct stat tmp_sb;
if (stat(path, &sb) != 0)
return NULL;
struct MailboxList ml = STAILQ_HEAD_INITIALIZER(ml);
neomutt_mailboxlist_get_all(&ml, NeoMutt, MUTT_MAILBOX_ANY);
struct MailboxNode *np = NULL;
struct Mailbox *m = NULL;
STAILQ_FOREACH(np, &ml, entries)
{
if ((stat(mailbox_path(np->mailbox), &tmp_sb) == 0) &&
(sb.st_dev == tmp_sb.st_dev) && (sb.st_ino == tmp_sb.st_ino))
{
m = np->mailbox;
break;
}
}
neomutt_mailboxlist_clear(&ml);
return m;
}
/**
* mailbox_find_name - Find the mailbox with a given name
* @param name Name to match
* @retval ptr Matching Mailbox
* @retval NULL No matching mailbox found
*
* @note This searches across all Accounts
*/
struct Mailbox *mailbox_find_name(const char *name)
{
if (!name)
return NULL;
struct MailboxList ml = STAILQ_HEAD_INITIALIZER(ml);
neomutt_mailboxlist_get_all(&ml, NeoMutt, MUTT_MAILBOX_ANY);
struct MailboxNode *np = NULL;
struct Mailbox *m = NULL;
STAILQ_FOREACH(np, &ml, entries)
{
if (mutt_str_equal(np->mailbox->name, name))
{
m = np->mailbox;
break;
}
}
neomutt_mailboxlist_clear(&ml);
return m;
}
/**
* mailbox_update - Get the mailbox's current size
* @param m Mailbox to check
*
* @note Only applies to local Mailboxes
*/
void mailbox_update(struct Mailbox *m)
{
struct stat sb;
if (!m)
return;
if (stat(mailbox_path(m), &sb) == 0)
m->size = (off_t) sb.st_size;
else
m->size = 0;
}
/**
* mailbox_changed - Notify observers of a change to a Mailbox
* @param m Mailbox
* @param action Change to Mailbox
*/
void mailbox_changed(struct Mailbox *m, enum NotifyMailbox action)
{
if (!m)
return;
struct EventMailbox ev_m = { m };
notify_send(m->notify, NT_MAILBOX, action, &ev_m);
}
/**
* mailbox_size_add - Add an email's size to the total size of a Mailbox
* @param m Mailbox
* @param e Email
*/
void mailbox_size_add(struct Mailbox *m, const struct Email *e)
{
m->size += email_size(e);
}
/**
* mailbox_size_sub - Subtract an email's size from the total size of a Mailbox
* @param m Mailbox
* @param e Email
*/
void mailbox_size_sub(struct Mailbox *m, const struct Email *e)
{
m->size -= email_size(e);
}
/**
* mailbox_set_subset - Set a Mailbox's Config Subset
* @param m Mailbox
* @param sub Parent Config Subset
* @retval true Success
*/
bool mailbox_set_subset(struct Mailbox *m, struct ConfigSubset *sub)
{
if (!m || m->sub || !sub)
return false;
m->sub = cs_subset_new(m->name, sub, m->notify);
m->sub->scope = SET_SCOPE_MAILBOX;
return true;
}