NeoMutt  2025-12-11-435-g4ac674
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
mailboxes.c File Reference

Parse Mailboxes Commands. More...

#include "config.h"
#include <stdbool.h>
#include <stdio.h>
#include "mutt/lib.h"
#include "config/lib.h"
#include "core/lib.h"
#include "mailboxes.h"
#include "parse/lib.h"
#include "muttlib.h"
#include "mx.h"
#include "monitor.h"
+ Include dependency graph for mailboxes.c:

Go to the source code of this file.

Functions

static enum CommandResult mailbox_add (const char *folder, const char *mailbox, const char *label, enum TriBool poll, enum TriBool notify, struct Buffer *err)
 Add a new Mailbox.
 
bool mailbox_add_simple (const char *mailbox, struct Buffer *err)
 Add a new Mailbox.
 
void parse_mailbox_free (struct ParseMailbox *pm)
 Free a ParseMailbox structure.
 
void parse_mailbox_array_free (struct ParseMailboxArray *pma)
 Free a ParseMailboxArray.
 
bool parse_mailboxes_args (const struct Command *cmd, struct Buffer *line, struct Buffer *err, struct ParseMailboxArray *args)
 Parse the 'mailboxes' and 'named-mailboxes' commands.
 
enum CommandResult parse_mailboxes_exec (const struct Command *cmd, struct ParseMailboxArray *args, struct Buffer *err)
 Execute the 'mailboxes' or 'named-mailboxes' command.
 
enum CommandResult parse_mailboxes (const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
 Parse the 'mailboxes' command - Implements Command::parse() -.
 
static void do_unmailboxes (struct Mailbox *m)
 Remove a Mailbox from the Sidebar/notifications.
 
bool mailbox_remove_simple (const char *mailbox)
 Remove a Mailbox.
 
static void do_unmailboxes_star (void)
 Remove all Mailboxes from the Sidebar/notifications.
 
enum CommandResult parse_unmailboxes (const struct Command *cmd, struct Buffer *line, const struct ParseContext *pc, struct ParseError *pe)
 Parse the 'unmailboxes' command - Implements Command::parse() -.
 

Detailed Description

Parse Mailboxes Commands.

Authors
  • Richard Russon

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 mailboxes.c.

Function Documentation

◆ mailbox_add()

static enum CommandResult mailbox_add ( const char * folder,
const char * mailbox,
const char * label,
enum TriBool poll,
enum TriBool notify,
struct Buffer * err )
static

Add a new Mailbox.

Parameters
folderPath to use for '+' abbreviations
mailboxMailbox to add
labelDescriptive label
pollEnable mailbox polling?
notifyEnable mailbox notification?
errBuffer for error messages
Return values
CommandResultResult e.g. MUTT_CMD_SUCCESS

Definition at line 53 of file mailboxes.c.

56{
57 mutt_debug(LL_DEBUG1, "Adding mailbox: '%s' label '%s', poll %s, notify %s\n",
58 mailbox, label ? label : "[NONE]",
59 (poll == TB_UNSET) ? "[UNSPECIFIED]" :
60 (poll == TB_TRUE) ? "true" :
61 "false",
62 (notify == TB_UNSET) ? "[UNSPECIFIED]" :
63 (notify == TB_TRUE) ? "true" :
64 "false");
65 struct Mailbox *m = mailbox_new();
66
67 buf_strcpy(&m->pathbuf, mailbox);
68 /* int rc = */ mx_path_canon2(m, folder);
69
70 if (m->type <= MUTT_UNKNOWN)
71 {
72 buf_printf(err, "Unknown Mailbox: %s", m->realpath);
73 mailbox_free(&m);
74 return MUTT_CMD_ERROR;
75 }
76
77 bool new_account = false;
78 struct Account *a = mx_ac_find(m);
79 if (!a)
80 {
81 a = account_new(NULL, NeoMutt->sub);
82 a->type = m->type;
83 new_account = true;
84 }
85
86 if (!new_account)
87 {
88 struct Mailbox *m_old = mx_mbox_find(a, m->realpath);
89 if (m_old)
90 {
91 if (!m_old->visible)
92 {
93 m_old->visible = true;
94 m_old->gen = mailbox_gen();
95 }
96
97 if (label)
98 mutt_str_replace(&m_old->name, label);
99
100 if (notify != TB_UNSET)
101 m_old->notify_user = notify;
102
103 if (poll != TB_UNSET)
104 m_old->poll_new_mail = poll;
105
106 struct EventMailbox ev_m = { m_old };
108
109 mailbox_free(&m);
110 return MUTT_CMD_SUCCESS;
111 }
112 }
113
114 if (label)
115 m->name = mutt_str_dup(label);
116
117 if (notify != TB_UNSET)
118 m->notify_user = notify;
119
120 if (poll != TB_UNSET)
121 m->poll_new_mail = poll;
122
123 if (!mx_ac_add(a, m))
124 {
125 mailbox_free(&m);
126 if (new_account)
127 {
128 cs_subset_free(&a->sub);
129 FREE(&a->name);
130 notify_free(&a->notify);
131 FREE(&a);
132 }
133 return MUTT_CMD_SUCCESS;
134 }
135
136 if (new_account)
137 {
139 }
140
141 // this is finally a visible mailbox in the sidebar and mailboxes list
142 m->visible = true;
143
144#ifdef USE_INOTIFY
146#endif
147
148 return MUTT_CMD_SUCCESS;
149}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition buffer.c:161
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition buffer.c:395
@ MUTT_CMD_SUCCESS
Success: Command worked.
Definition command.h:40
@ MUTT_CMD_ERROR
Error: Can't help the user.
Definition command.h:38
struct Account * account_new(const char *name, struct ConfigSubset *sub)
Create a new Account.
Definition account.c:44
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_free(struct Mailbox **ptr)
Free a Mailbox.
Definition mailbox.c:90
@ NT_MAILBOX_CHANGE
Mailbox has been changed.
Definition mailbox.h:175
@ MUTT_UNKNOWN
Mailbox wasn't recognised.
Definition mailbox.h:43
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
@ LL_DEBUG1
Log at debug level 1.
Definition logging2.h:45
@ TB_TRUE
Value is true.
Definition mailboxes.h:40
@ TB_UNSET
Value hasn't been set.
Definition mailboxes.h:38
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
int mutt_monitor_add(struct Mailbox *m)
Add a watch for a mailbox.
Definition monitor.c:484
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
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition string.c:257
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition string.c:284
struct Mailbox * mx_mbox_find(struct Account *a, const char *path)
Find a Mailbox on an Account.
Definition mx.c:1546
bool mx_ac_add(struct Account *a, struct Mailbox *m)
Add a Mailbox to an Account - Wrapper for MxOps::ac_add()
Definition mx.c:1737
struct Account * mx_ac_find(struct Mailbox *m)
Find the Account owning a Mailbox.
Definition mx.c:1521
int mx_path_canon2(struct Mailbox *m, const char *folder)
Canonicalise the path to realpath.
Definition mx.c:1473
bool neomutt_account_add(struct NeoMutt *n, struct Account *a)
Add an Account to the global list.
Definition neomutt.c:456
@ NT_MAILBOX
Mailbox has changed, NotifyMailbox, EventMailbox.
Definition notify_type.h:49
A group of associated Mailboxes.
Definition account.h:36
enum MailboxType type
Type of Mailboxes this Account contains.
Definition account.h:37
char * name
Name of Account.
Definition account.h:38
struct Notify * notify
Notifications: NotifyAccount, EventAccount.
Definition account.h:41
struct ConfigSubset * sub
Inherited config items.
Definition account.h:39
An Event that happened to a Mailbox.
Definition mailbox.h:189
A mailbox.
Definition mailbox.h:78
char * realpath
Used for duplicate detection, context comparison, and the sidebar.
Definition mailbox.h:80
enum MailboxType type
Mailbox type.
Definition mailbox.h:101
bool poll_new_mail
Check for new mail.
Definition mailbox.h:114
char * name
A short name for the Mailbox.
Definition mailbox.h:81
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
bool visible
True if a result of "mailboxes".
Definition mailbox.h:129
int gen
Generation number, for sorting.
Definition mailbox.h:146
Container for Accounts, Notifications.
Definition neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:49
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_add_simple()

bool mailbox_add_simple ( const char * mailbox,
struct Buffer * err )

Add a new Mailbox.

Parameters
mailboxMailbox to add
errBuffer for error messages
Return values
trueSuccess

Definition at line 157 of file mailboxes.c.

158{
159 enum CommandResult rc = mailbox_add("", mailbox, NULL, TB_UNSET, TB_UNSET, err);
160
161 return (rc == MUTT_CMD_SUCCESS);
162}
CommandResult
Error codes for command_t parse functions.
Definition command.h:37
static enum CommandResult mailbox_add(const char *folder, const char *mailbox, const char *label, enum TriBool poll, enum TriBool notify, struct Buffer *err)
Add a new Mailbox.
Definition mailboxes.c:53
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parse_mailbox_free()

void parse_mailbox_free ( struct ParseMailbox * pm)

Free a ParseMailbox structure.

Parameters
pmParseMailbox to free

Definition at line 168 of file mailboxes.c.

169{
170 if (!pm)
171 return;
172
173 FREE(&pm->path);
174 FREE(&pm->label);
175}
char * label
Descriptive label (strdup'd, may be NULL)
Definition mailboxes.h:49
char * path
Mailbox path.
Definition mailboxes.h:48
+ Here is the caller graph for this function:

◆ parse_mailbox_array_free()

void parse_mailbox_array_free ( struct ParseMailboxArray * pma)

Free a ParseMailboxArray.

Parameters
pmaParseMailboxArray to free

Definition at line 181 of file mailboxes.c.

182{
183 if (!pma)
184 return;
185
186 struct ParseMailbox *pm = NULL;
187 ARRAY_FOREACH(pm, pma)
188 {
190 }
191 ARRAY_FREE(pma);
192}
#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
void parse_mailbox_free(struct ParseMailbox *pm)
Free a ParseMailbox structure.
Definition mailboxes.c:168
Parsed data for a single mailbox.
Definition mailboxes.h:47
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parse_mailboxes_args()

bool parse_mailboxes_args ( const struct Command * cmd,
struct Buffer * line,
struct Buffer * err,
struct ParseMailboxArray * args )

Parse the 'mailboxes' and 'named-mailboxes' commands.

Parameters
[in]cmdCommand being parsed
[in]lineText to parse
[out]errBuffer for error messages
[out]argsParsed args
Return values
trueSuccess

Parse:

  • mailboxes [[ -label <label> ] | -nolabel ] [ -notify | -nonotify ] [ -poll | -nopoll ] <mailbox> [ ... ]
  • named-mailboxes [ -notify | -nonotify ] [ -poll | -nopoll ] <mailbox> [ ... ]

Definition at line 206 of file mailboxes.c.

208{
209 if (!cmd || !line || !err || !args)
210 return false;
211
212 if (!MoreArgs(line))
213 {
214 buf_printf(err, _("%s: too few arguments"), cmd->name);
215 return false;
216 }
217
218 // Parsed (label, mailbox, flags) tuples will be appended to the @args array
219 struct Buffer *label = buf_pool_get();
220 struct Buffer *mailbox = buf_pool_get();
221 struct Buffer *token = buf_pool_get();
222 bool rc = false;
223
224 while (MoreArgs(line))
225 {
226 bool label_set = false;
227 enum TriBool notify = TB_UNSET;
228 enum TriBool poll = TB_UNSET;
229
230 do
231 {
232 // Start by handling the options
234
235 if (mutt_str_equal(buf_string(token), "-label"))
236 {
237 if (!MoreArgs(line))
238 {
239 buf_printf(err, _("%s: too few arguments"), "mailboxes -label");
240 goto done;
241 }
242
244 label_set = true;
245 }
246 else if (mutt_str_equal(buf_string(token), "-nolabel"))
247 {
248 buf_reset(label);
249 label_set = true;
250 }
251 else if (mutt_str_equal(buf_string(token), "-notify"))
252 {
253 notify = TB_TRUE;
254 }
255 else if (mutt_str_equal(buf_string(token), "-nonotify"))
256 {
257 notify = TB_FALSE;
258 }
259 else if (mutt_str_equal(buf_string(token), "-poll"))
260 {
261 poll = TB_TRUE;
262 }
263 else if (mutt_str_equal(buf_string(token), "-nopoll"))
264 {
265 poll = TB_FALSE;
266 }
267 else if ((cmd->id == CMD_NAMED_MAILBOXES) && !label_set)
268 {
269 if (!MoreArgs(line))
270 {
271 buf_printf(err, _("%s: too few arguments"), cmd->name);
272 goto done;
273 }
274
275 buf_copy(label, token);
276 label_set = true;
277 }
278 else
279 {
280 buf_copy(mailbox, token);
281 break;
282 }
283 } while (MoreArgs(line));
284
285 if (buf_is_empty(mailbox))
286 {
287 buf_printf(err, _("%s: too few arguments"), cmd->name);
288 goto done;
289 }
290
291 struct ParseMailbox pm = {
292 .path = buf_strdup(mailbox),
293 .label = label_set ? buf_strdup(label) : NULL,
294 .poll = poll,
295 .notify = notify,
296 };
297 ARRAY_ADD(args, pm);
298
300 buf_reset(mailbox);
301 }
302
303 rc = true;
304
305done:
307 buf_pool_release(&mailbox);
308 buf_pool_release(&token);
309 return rc;
310}
#define ARRAY_ADD(head, elem)
Add an element at the end of the array.
Definition array.h:157
void buf_reset(struct Buffer *buf)
Reset an existing Buffer.
Definition buffer.c:76
bool buf_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition buffer.c:291
size_t buf_copy(struct Buffer *dst, const struct Buffer *src)
Copy a Buffer's contents to another Buffer.
Definition buffer.c:601
char * buf_strdup(const struct Buffer *buf)
Copy a Buffer's string.
Definition buffer.c:571
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
@ CMD_NAMED_MAILBOXES
:named-mailboxes
Definition command.h:98
int parse_extract_token(struct Buffer *dest, struct Buffer *line, TokenFlags flags)
Extract one token from a string.
Definition extract.c:49
#define MoreArgs(buf)
Definition extract.h:31
#define TOKEN_NO_FLAGS
No flags are set.
Definition extract.h:45
TriBool
Tri-state boolean.
Definition mailboxes.h:37
@ TB_FALSE
Value is false.
Definition mailboxes.h:39
#define _(a)
Definition message.h:28
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition string.c:662
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition pool.c:91
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition pool.c:111
String manipulation buffer.
Definition buffer.h:36
const char * name
Name of the Command.
Definition command.h:159
enum CommandId id
ID of the Command.
Definition command.h:160
enum TriBool notify
Enable mailbox notification?
Definition mailboxes.h:51
enum TriBool poll
Enable mailbox polling?
Definition mailboxes.h:50
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parse_mailboxes_exec()

enum CommandResult parse_mailboxes_exec ( const struct Command * cmd,
struct ParseMailboxArray * args,
struct Buffer * err )

Execute the 'mailboxes' or 'named-mailboxes' command.

Parameters
[in]cmdCommand being executed
[in]argsParsed arguments
[out]errBuffer for error messages
Return values
CommandResultResult e.g. MUTT_CMD_SUCCESS

Definition at line 319 of file mailboxes.c.

321{
322 if (!cmd || !args || !err)
323 return MUTT_CMD_ERROR;
324
325 const char *const c_folder = cs_subset_string(NeoMutt->sub, "folder");
327
328 struct ParseMailbox *pm = NULL;
329 ARRAY_FOREACH(pm, args)
330 {
331 rc = mailbox_add(c_folder, pm->path, pm->label, pm->poll, pm->notify, err);
332 if (rc != MUTT_CMD_SUCCESS)
333 return rc;
334 }
335
336 return rc;
337}
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition helpers.c:291
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ do_unmailboxes()

static void do_unmailboxes ( struct Mailbox * m)
static

Remove a Mailbox from the Sidebar/notifications.

Parameters
mMailbox to unmailboxes

Definition at line 368 of file mailboxes.c.

369{
370#ifdef USE_INOTIFY
371 if (m->poll_new_mail)
373#endif
374 m->visible = false;
375 m->gen = -1;
376 if (m->opened)
377 {
378 struct EventMailbox ev_m = { NULL };
379 mutt_debug(LL_NOTIFY, "NT_MAILBOX_CHANGE: NULL\n");
381 }
382 else
383 {
385 mailbox_free(&m);
386 }
387}
void account_mailbox_remove(struct Account *a, struct Mailbox *m)
Remove a Mailbox from an Account.
Definition account.c:94
@ LL_NOTIFY
Log of notifications.
Definition logging2.h:50
int mutt_monitor_remove(struct Mailbox *m)
Remove a watch for a mailbox.
Definition monitor.c:528
struct Account * account
Account that owns this Mailbox.
Definition mailbox.h:126
int opened
Number of times mailbox is opened.
Definition mailbox.h:127
struct Notify * notify
Notifications handler.
Definition neomutt.h:45
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mailbox_remove_simple()

bool mailbox_remove_simple ( const char * mailbox)

Remove a Mailbox.

Parameters
mailboxPath of Mailbox to remove
Return values
trueSuccess

Definition at line 394 of file mailboxes.c.

395{
396 struct Buffer *buf = buf_pool_get();
397 buf_strcpy(buf, mailbox);
398 expand_path(buf, false);
399
400 struct Account **ap = NULL;
402 {
403 struct Mailbox *m = mx_mbox_find(*ap, buf_string(buf));
404 if (m)
405 {
407 buf_pool_release(&buf);
408 return true;
409 }
410 }
411
412 buf_pool_release(&buf);
413 return false;
414}
static void do_unmailboxes(struct Mailbox *m)
Remove a Mailbox from the Sidebar/notifications.
Definition mailboxes.c:368
void expand_path(struct Buffer *buf, bool regex)
Create the canonical path.
Definition muttlib.c:121
struct AccountArray accounts
All Accounts.
Definition neomutt.h:50
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ do_unmailboxes_star()

static void do_unmailboxes_star ( void )
static

Remove all Mailboxes from the Sidebar/notifications.

Definition at line 419 of file mailboxes.c.

420{
421 struct MailboxArray ma = neomutt_mailboxes_get(NeoMutt, MUTT_MAILBOX_ANY);
422
423 struct Mailbox **mp = NULL;
424 ARRAY_FOREACH(mp, &ma)
425 {
426 do_unmailboxes(*mp);
427 }
428 ARRAY_FREE(&ma); // Clean up the ARRAY, but not the Mailboxes
429}
@ 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:526
+ Here is the call graph for this function:
+ Here is the caller graph for this function: