NeoMutt  2025-09-05-70-gcfdde0
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
neomutt.c
Go to the documentation of this file.
1
23
29
30#include "config.h"
31#include <errno.h>
32#include <locale.h>
33#include <stdio.h>
34#include <string.h>
35#include <sys/stat.h>
36#include "mutt/lib.h"
37#include "config/lib.h"
38#include "neomutt.h"
39#include "account.h"
40#include "mailbox.h"
41
42struct NeoMutt *NeoMutt = NULL;
43
49struct NeoMutt *neomutt_new(struct ConfigSet *cs)
50{
51 if (!cs)
52 return NULL;
53
54 struct NeoMutt *n = MUTT_MEM_CALLOC(1, struct NeoMutt);
55
57 n->notify = notify_new();
58 n->sub = cs_subset_new(NULL, NULL, n->notify);
59 n->sub->cs = cs;
61
62 n->time_c_locale = duplocale(LC_GLOBAL_LOCALE);
63 if (n->time_c_locale)
64 n->time_c_locale = newlocale(LC_TIME_MASK, "C", n->time_c_locale);
65
66 if (!n->time_c_locale)
67 {
68 mutt_error("%s", strerror(errno)); // LCOV_EXCL_LINE
69 mutt_exit(1); // LCOV_EXCL_LINE
70 }
71
74
77
78 return n;
79}
80
85void neomutt_free(struct NeoMutt **ptr)
86{
87 if (!ptr || !*ptr)
88 return;
89
90 struct NeoMutt *n = *ptr;
91
97 if (n->time_c_locale)
98 freelocale(n->time_c_locale);
99
100 FREE(&n->home_dir);
101 FREE(&n->username);
102
103 envlist_free(&n->env);
104
105 FREE(ptr);
106}
107
114bool neomutt_account_add(struct NeoMutt *n, struct Account *a)
115{
116 if (!n || !a)
117 return false;
118
119 ARRAY_ADD(&n->accounts, a);
121
122 mutt_debug(LL_NOTIFY, "NT_ACCOUNT_ADD: %s %p\n",
123 mailbox_get_type_name(a->type), (void *) a);
124 struct EventAccount ev_a = { a };
126 return true;
127}
128
134void neomutt_account_remove(struct NeoMutt *n, const struct Account *a)
135{
136 if (!n || !a || ARRAY_EMPTY(&n->accounts))
137 return;
138
139 struct Account **ap = NULL;
140 ARRAY_FOREACH(ap, &n->accounts)
141 {
142 if ((*ap) != a)
143 continue;
144
145 ARRAY_REMOVE(&n->accounts, ap);
146 account_free(ap);
147 break;
148 }
149}
150
156{
157 if (!n)
158 return;
159
160 if (!ARRAY_EMPTY(&n->accounts))
161 {
162 mutt_debug(LL_NOTIFY, "NT_ACCOUNT_DELETE_ALL\n");
163 struct EventAccount ev_a = { NULL };
165
166 struct Account **ap = NULL;
167 ARRAY_FOREACH(ap, &n->accounts)
168 {
169 account_free(ap);
170 }
171 }
172
173 ARRAY_FREE(&n->accounts);
174}
175
184struct MailboxArray neomutt_mailboxes_get(struct NeoMutt *n, enum MailboxType type)
185{
186 struct MailboxArray ma = ARRAY_HEAD_INITIALIZER;
187
188 if (!n)
189 return ma;
190
191 struct Account **ap = NULL;
192 struct Mailbox **mp = NULL;
193
194 ARRAY_FOREACH(ap, &n->accounts)
195 {
196 struct Account *a = *ap;
197 if ((type > MUTT_UNKNOWN) && (a->type != type))
198 continue;
199
200 ARRAY_FOREACH(mp, &a->mailboxes)
201 {
202 ARRAY_ADD(&ma, *mp);
203 }
204 }
205
206 return ma;
207}
208
221FILE *mutt_file_fopen_masked_full(const char *path, const char *mode,
222 const char *file, int line, const char *func)
223{
224 // Set the user's umask (saved on startup)
225 mode_t old_umask = umask(NeoMutt->user_default_umask);
226 mutt_debug(LL_DEBUG3, "umask set to %03o\n", NeoMutt->user_default_umask);
227
228 // The permissions will be limited by the umask
229 FILE *fp = mutt_file_fopen_full(path, mode, 0666, file, line, func);
230
231 umask(old_umask); // Immediately restore the umask
232 mutt_debug(LL_DEBUG3, "umask set to %03o\n", old_umask);
233
234 return fp;
235}
#define ARRAY_ADD(head, elem)
Add an element at the end of the array.
Definition array.h:156
#define ARRAY_REMOVE(head, elem)
Remove an entry from the array, shifting down the subsequent entries.
Definition array.h:323
#define ARRAY_FOREACH(elem, head)
Iterate over all elements of the array.
Definition array.h:214
#define ARRAY_EMPTY(head)
Check if an array is empty.
Definition array.h:74
#define ARRAY_FREE(head)
Release all memory.
Definition array.h:204
#define ARRAY_INIT(head)
Initialize an array.
Definition array.h:65
#define ARRAY_HEAD_INITIALIZER
Static initializer for arrays.
Definition array.h:58
Convenience wrapper for the config headers.
void account_free(struct Account **ptr)
Free an Account.
Definition account.c:148
A group of associated Mailboxes.
@ NT_ACCOUNT_ADD
Account has been added.
Definition account.h:67
@ NT_ACCOUNT_DELETE_ALL
All Accounts are about to be deleted.
Definition account.h:69
const char * mailbox_get_type_name(enum MailboxType type)
Get the type of a Mailbox.
Definition mailbox.c:324
Representation of a mailbox.
MailboxType
Supported mailbox formats.
Definition mailbox.h:41
@ MUTT_UNKNOWN
Mailbox wasn't recognised.
Definition mailbox.h:44
void envlist_free(char ***envp)
Free the private copy of the environment.
Definition envlist.c:42
void mutt_exit(int code)
Leave NeoMutt NOW.
Definition exit.c:41
FILE * mutt_file_fopen_full(const char *path, const char *mode, const mode_t perms, const char *file, int line, const char *func)
Call fopen() safely.
Definition file.c:563
#define mutt_error(...)
Definition logging2.h:93
#define mutt_debug(LEVEL,...)
Definition logging2.h:90
@ LL_DEBUG3
Log at debug level 3.
Definition logging2.h:46
@ LL_NOTIFY
Log of notifications.
Definition logging2.h:49
#define FREE(x)
Definition memory.h:62
#define MUTT_MEM_CALLOC(n, type)
Definition memory.h:47
Convenience wrapper for the library headers.
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_set_parent(struct Notify *notify, struct Notify *parent)
Set the parent notification handler.
Definition notify.c:95
void notify_free(struct Notify **ptr)
Free a notification handler.
Definition notify.c:75
struct MailboxArray neomutt_mailboxes_get(struct NeoMutt *n, enum MailboxType type)
Get an Array of matching Mailboxes.
Definition neomutt.c:184
void neomutt_account_remove(struct NeoMutt *n, const struct Account *a)
Remove an Account from the global list.
Definition neomutt.c:134
bool neomutt_account_add(struct NeoMutt *n, struct Account *a)
Add an Account to the global list.
Definition neomutt.c:114
struct NeoMutt * neomutt_new(struct ConfigSet *cs)
Create the main NeoMutt object.
Definition neomutt.c:49
void neomutt_accounts_free(struct NeoMutt *n)
Definition neomutt.c:155
FILE * mutt_file_fopen_masked_full(const char *path, const char *mode, const char *file, int line, const char *func)
Wrapper around mutt_file_fopen_full()
Definition neomutt.c:221
void neomutt_free(struct NeoMutt **ptr)
Free a NeoMutt.
Definition neomutt.c:85
Container for Accounts, Notifications.
@ NT_ACCOUNT
Account has changed, NotifyAccount, EventAccount.
Definition notify_type.h:36
A group of associated Mailboxes.
Definition account.h:36
enum MailboxType type
Type of Mailboxes this Account contains.
Definition account.h:37
struct Notify * notify
Notifications: NotifyAccount, EventAccount.
Definition account.h:41
struct MailboxArray mailboxes
All Mailboxes.
Definition account.h:40
Container for lots of config items.
Definition set.h:248
struct ConfigSet * cs
Parent ConfigSet.
Definition subset.h:50
enum ConfigScope scope
Scope of Subset, e.g. SET_SCOPE_ACCOUNT.
Definition subset.h:48
An Event that happened to an Account.
Definition account.h:77
A mailbox.
Definition mailbox.h:79
Container for Accounts, Notifications.
Definition neomutt.h:42
struct Notify * notify_timeout
Timeout notifications handler.
Definition neomutt.h:45
struct AccountArray accounts
All Accounts.
Definition neomutt.h:47
struct Notify * notify_resize
Window resize notifications handler.
Definition neomutt.h:44
char ** env
Private copy of the environment variables.
Definition neomutt.h:54
char * username
User's login name.
Definition neomutt.h:53
mode_t user_default_umask
User's default file writing permissions (inferred from umask)
Definition neomutt.h:49
char * home_dir
User's home directory.
Definition neomutt.h:52
struct Notify * notify
Notifications handler.
Definition neomutt.h:43
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:46
locale_t time_c_locale
Current locale but LC_TIME=C.
Definition neomutt.h:48
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
void cs_subset_free(struct ConfigSubset **ptr)
Free a Config Subset.
Definition subset.c:112
@ SET_SCOPE_NEOMUTT
This Config is NeoMutt-specific (global)
Definition subset.h:37