NeoMutt  2025-12-11-58-g09398d
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 "address/lib.h"
38#include "config/lib.h"
39#include "neomutt.h"
40#include "account.h"
41#include "mailbox.h"
42
43struct NeoMutt *NeoMutt = NULL;
44
50struct NeoMutt *neomutt_new(struct ConfigSet *cs)
51{
52 if (!cs)
53 return NULL;
54
55 struct NeoMutt *n = MUTT_MEM_CALLOC(1, struct NeoMutt);
56
58 n->notify = notify_new();
59 n->sub = cs_subset_new(NULL, NULL, n->notify);
60 n->sub->cs = cs;
62
63 n->time_c_locale = duplocale(LC_GLOBAL_LOCALE);
64 if (n->time_c_locale)
65 n->time_c_locale = newlocale(LC_TIME_MASK, "C", n->time_c_locale);
66
67 if (!n->time_c_locale)
68 {
69 mutt_error("%s", strerror(errno)); // LCOV_EXCL_LINE
70 mutt_exit(1); // LCOV_EXCL_LINE
71 }
72
75
78
79 n->groups = groups_new();
80
81 return n;
82}
83
88void neomutt_free(struct NeoMutt **ptr)
89{
90 if (!ptr || !*ptr)
91 return;
92
93 struct NeoMutt *n = *ptr;
94
100 if (n->time_c_locale)
101 freelocale(n->time_c_locale);
102
103 groups_free(&n->groups);
104
105 FREE(&n->home_dir);
106 FREE(&n->username);
107
108 envlist_free(&n->env);
109
110 FREE(ptr);
111}
112
119bool neomutt_account_add(struct NeoMutt *n, struct Account *a)
120{
121 if (!n || !a)
122 return false;
123
124 ARRAY_ADD(&n->accounts, a);
126
127 mutt_debug(LL_NOTIFY, "NT_ACCOUNT_ADD: %s %p\n",
128 mailbox_get_type_name(a->type), (void *) a);
129 struct EventAccount ev_a = { a };
131 return true;
132}
133
139void neomutt_account_remove(struct NeoMutt *n, struct Account *a)
140{
141 if (!n || !a || ARRAY_EMPTY(&n->accounts))
142 return;
143
144 struct Account **ap = NULL;
145 ARRAY_FOREACH(ap, &n->accounts)
146 {
147 if ((*ap) != a)
148 continue;
149
150 ARRAY_REMOVE(&n->accounts, ap);
151 account_free(&a);
152 break;
153 }
154}
155
161{
162 if (!n)
163 return;
164
165 if (!ARRAY_EMPTY(&n->accounts))
166 {
167 mutt_debug(LL_NOTIFY, "NT_ACCOUNT_DELETE_ALL\n");
168 struct EventAccount ev_a = { NULL };
170
171 struct Account **ap = NULL;
172 ARRAY_FOREACH(ap, &n->accounts)
173 {
174 account_free(ap);
175 }
176 }
177
178 ARRAY_FREE(&n->accounts);
179}
180
189struct MailboxArray neomutt_mailboxes_get(struct NeoMutt *n, enum MailboxType type)
190{
191 struct MailboxArray ma = ARRAY_HEAD_INITIALIZER;
192
193 if (!n)
194 return ma;
195
196 struct Account **ap = NULL;
197 struct Mailbox **mp = NULL;
198
199 ARRAY_FOREACH(ap, &n->accounts)
200 {
201 struct Account *a = *ap;
202 if ((type > MUTT_UNKNOWN) && (a->type != type))
203 continue;
204
205 ARRAY_FOREACH(mp, &a->mailboxes)
206 {
207 ARRAY_ADD(&ma, *mp);
208 }
209 }
210
211 return ma;
212}
213
226FILE *mutt_file_fopen_masked_full(const char *path, const char *mode,
227 const char *file, int line, const char *func)
228{
229 // Set the user's umask (saved on startup)
230 mode_t old_umask = umask(NeoMutt->user_default_umask);
231 mutt_debug(LL_DEBUG3, "umask set to %03o\n", NeoMutt->user_default_umask);
232
233 // The permissions will be limited by the umask
234 FILE *fp = mutt_file_fopen_full(path, mode, 0666, file, line, func);
235
236 umask(old_umask); // Immediately restore the umask
237 mutt_debug(LL_DEBUG3, "umask set to %03o\n", old_umask);
238
239 return fp;
240}
Email Address Handling.
#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
struct HashTable * groups_new(void)
Create a HashTable for the Address Groups.
Definition group.c:267
void groups_free(struct HashTable **pptr)
Free Address Groups HashTable.
Definition group.c:280
#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:189
void neomutt_account_remove(struct NeoMutt *n, struct Account *a)
Remove an Account from the global list.
Definition neomutt.c:139
bool neomutt_account_add(struct NeoMutt *n, struct Account *a)
Add an Account to the global list.
Definition neomutt.c:119
struct NeoMutt * neomutt_new(struct ConfigSet *cs)
Create the main NeoMutt object.
Definition neomutt.c:50
void neomutt_accounts_free(struct NeoMutt *n)
Definition neomutt.c:160
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:226
void neomutt_free(struct NeoMutt **ptr)
Free a NeoMutt.
Definition neomutt.c:88
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:43
struct Notify * notify_timeout
Timeout notifications handler.
Definition neomutt.h:46
struct AccountArray accounts
All Accounts.
Definition neomutt.h:48
struct Notify * notify_resize
Window resize notifications handler.
Definition neomutt.h:45
char ** env
Private copy of the environment variables.
Definition neomutt.h:56
char * username
User's login name.
Definition neomutt.h:55
mode_t user_default_umask
User's default file writing permissions (inferred from umask)
Definition neomutt.h:50
char * home_dir
User's home directory.
Definition neomutt.h:54
struct Notify * notify
Notifications handler.
Definition neomutt.h:44
struct HashTable * groups
Hash Table: "group-name" -> Group.
Definition neomutt.h:52
struct ConfigSubset * sub
Inherited config items.
Definition neomutt.h:47
locale_t time_c_locale
Current locale but LC_TIME=C.
Definition neomutt.h:49
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