NeoMutt  2025-01-09-117-gace867
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
neomutt.c
Go to the documentation of this file.
1
30#include "config.h"
31#include <errno.h>
32#include <locale.h>
33#include <stddef.h>
34#include <stdio.h>
35#include <string.h>
36#include <sys/stat.h>
37#include "mutt/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 return n;
80}
81
86void neomutt_free(struct NeoMutt **ptr)
87{
88 if (!ptr || !*ptr)
89 return;
90
91 struct NeoMutt *n = *ptr;
92
98 if (n->time_c_locale)
99 freelocale(n->time_c_locale);
100
101 FREE(&n->home_dir);
102 FREE(&n->username);
103
104 envlist_free(&n->env);
105
106 FREE(ptr);
107}
108
115bool neomutt_account_add(struct NeoMutt *n, struct Account *a)
116{
117 if (!n || !a)
118 return false;
119
120 TAILQ_INSERT_TAIL(&n->accounts, a, entries);
122
123 mutt_debug(LL_NOTIFY, "NT_ACCOUNT_ADD: %s %p\n",
124 mailbox_get_type_name(a->type), (void *) a);
125 struct EventAccount ev_a = { a };
127 return true;
128}
129
138bool neomutt_account_remove(struct NeoMutt *n, const struct Account *a)
139{
140 if (!n || TAILQ_EMPTY(&n->accounts))
141 return false;
142
143 if (!a)
144 {
145 mutt_debug(LL_NOTIFY, "NT_ACCOUNT_DELETE_ALL\n");
146 struct EventAccount ev_a = { NULL };
148 }
149
150 bool result = false;
151 struct Account *np = NULL;
152 struct Account *tmp = NULL;
153 TAILQ_FOREACH_SAFE(np, &n->accounts, entries, tmp)
154 {
155 if (a && (np != a))
156 continue;
157
158 TAILQ_REMOVE(&n->accounts, np, entries);
159 account_free(&np);
160 result = true;
161 if (a)
162 break;
163 }
164 return result;
165}
166
173void neomutt_mailboxlist_clear(struct MailboxList *ml)
174{
175 if (!ml)
176 return;
177
178 struct MailboxNode *mn = NULL;
179 struct MailboxNode *tmp = NULL;
180 STAILQ_FOREACH_SAFE(mn, ml, entries, tmp)
181 {
182 STAILQ_REMOVE(ml, mn, MailboxNode, entries);
183 FREE(&mn);
184 }
185}
186
196size_t neomutt_mailboxlist_get_all(struct MailboxList *head, struct NeoMutt *n,
197 enum MailboxType type)
198{
199 if (!n)
200 return 0;
201
202 size_t count = 0;
203 struct Account *a = NULL;
204 struct MailboxNode *mn = NULL;
205
206 TAILQ_FOREACH(a, &n->accounts, entries)
207 {
208 if ((type > MUTT_UNKNOWN) && (a->type != type))
209 continue;
210
211 STAILQ_FOREACH(mn, &a->mailboxes, entries)
212 {
213 struct MailboxNode *mn2 = MUTT_MEM_CALLOC(1, struct MailboxNode);
214 mn2->mailbox = mn->mailbox;
215 STAILQ_INSERT_TAIL(head, mn2, entries);
216 count++;
217 }
218 }
219
220 return count;
221}
222
235FILE *mutt_file_fopen_masked_full(const char *path, const char *mode,
236 const char *file, int line, const char *func)
237{
238 // Set the user's umask (saved on startup)
239 mode_t old_umask = umask(NeoMutt->user_default_umask);
240 mutt_debug(LL_DEBUG3, "umask set to %03o\n", NeoMutt->user_default_umask);
241
242 // The permissions will be limited by the umask
243 FILE *fp = mutt_file_fopen_full(path, mode, 0666, file, line, func);
244
245 umask(old_umask); // Immediately restore the umask
246 mutt_debug(LL_DEBUG3, "umask set to %03o\n", old_umask);
247
248 return fp;
249}
Convenience wrapper for the config headers.
void account_free(struct Account **ptr)
Free an Account.
Definition: account.c:143
@ NT_ACCOUNT_ADD
Account has been added.
Definition: account.h:69
@ NT_ACCOUNT_DELETE_ALL
All Accounts are about to be deleted.
Definition: account.h:71
const char * mailbox_get_type_name(enum MailboxType type)
Get the type of a Mailbox.
Definition: mailbox.c:326
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
Maildir Account.
Maildir Mailbox.
#define FREE(x)
Definition: memory.h:55
#define MUTT_MEM_CALLOC(n, type)
Definition: memory.h:40
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
void neomutt_mailboxlist_clear(struct MailboxList *ml)
Free a Mailbox List.
Definition: neomutt.c:173
bool neomutt_account_add(struct NeoMutt *n, struct Account *a)
Add an Account to the global list.
Definition: neomutt.c:115
size_t neomutt_mailboxlist_get_all(struct MailboxList *head, struct NeoMutt *n, enum MailboxType type)
Get a List of all Mailboxes.
Definition: neomutt.c:196
bool neomutt_account_remove(struct NeoMutt *n, const struct Account *a)
Remove an Account from the global list.
Definition: neomutt.c:138
struct NeoMutt * neomutt_new(struct ConfigSet *cs)
Create the main NeoMutt object.
Definition: neomutt.c:50
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:235
void neomutt_free(struct NeoMutt **ptr)
Free a NeoMutt.
Definition: neomutt.c:86
Container for Accounts, Notifications.
@ NT_ACCOUNT
Account has changed, NotifyAccount, EventAccount.
Definition: notify_type.h:36
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:782
#define STAILQ_REMOVE(head, elm, type, field)
Definition: queue.h:441
#define TAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition: queue.h:792
#define TAILQ_INIT(head)
Definition: queue.h:822
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:866
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:390
#define STAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:427
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:901
#define STAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition: queue.h:400
#define TAILQ_EMPTY(head)
Definition: queue.h:778
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 MailboxList mailboxes
List of 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:79
List of Mailboxes.
Definition: mailbox.h:166
struct Mailbox * mailbox
Mailbox in the list.
Definition: mailbox.h:167
Container for Accounts, Notifications.
Definition: neomutt.h:43
struct Notify * notify_timeout
Timeout notifications handler.
Definition: neomutt.h:46
struct Notify * notify_resize
Window resize notifications handler.
Definition: neomutt.h:45
char ** env
Private copy of the environment variables.
Definition: neomutt.h:55
char * username
User's login name.
Definition: neomutt.h:54
struct AccountList accounts
List of all Accounts.
Definition: neomutt.h:48
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:53
struct Notify * notify
Notifications handler.
Definition: neomutt.h:44
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:154
void cs_subset_free(struct ConfigSubset **ptr)
Free a Config Subset.
Definition: subset.c:108
@ SET_SCOPE_NEOMUTT
This Config is NeoMutt-specific (global)
Definition: subset.h:37