NeoMutt  2025-01-09-117-gace867
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
config_type.c
Go to the documentation of this file.
1
37#include "config.h"
38#include <limits.h>
39#include <stdbool.h>
40#include <stddef.h>
41#include <stdint.h>
42#include "mutt/lib.h"
43#include "config/lib.h"
44#include "config_type.h"
45#include "address.h"
46
52struct Address *address_new(const char *addr)
53{
54 struct Address *a = MUTT_MEM_CALLOC(1, struct Address);
55 a->mailbox = buf_new(addr);
56 return a;
57}
58
62static void address_destroy(void *var, const struct ConfigDef *cdef)
63{
64 struct Address **a = var;
65 if (!*a)
66 return;
67
69}
70
74static int address_string_set(void *var, struct ConfigDef *cdef,
75 const char *value, struct Buffer *err)
76{
77 /* Store empty address as NULL */
78 if (value && (value[0] == '\0'))
79 value = NULL;
80
81 struct Address *addr = NULL;
82
83 int rc = CSR_SUCCESS;
84
85 if (!value && (cdef->type & D_NOT_EMPTY))
86 {
87 buf_printf(err, _("Option %s may not be empty"), cdef->name);
89 }
90
91 if (var && value)
92 {
93 // TODO - config can only store one
94 struct AddressList al = TAILQ_HEAD_INITIALIZER(al);
95 mutt_addrlist_parse(&al, value);
96 addr = mutt_addr_copy(TAILQ_FIRST(&al));
98 }
99
100 if (var)
101 {
102 if (cdef->validator)
103 {
104 rc = cdef->validator(cdef, (intptr_t) addr, err);
105
106 if (CSR_RESULT(rc) != CSR_SUCCESS)
107 {
108 address_destroy(&addr, cdef);
109 return rc | CSR_INV_VALIDATOR;
110 }
111 }
112
113 /* ordinary variable setting */
114 address_destroy(var, cdef);
115
116 *(struct Address **) var = addr;
117
118 if (!addr)
119 rc |= CSR_SUC_EMPTY;
120 }
121 else
122 {
123 /* set the default/initial value */
124 if (cdef->type & D_INTERNAL_INITIAL_SET)
125 FREE(&cdef->initial);
126
128 cdef->initial = (intptr_t) mutt_str_dup(value);
129 }
130
131 return rc;
132}
133
137static int address_string_get(void *var, const struct ConfigDef *cdef, struct Buffer *result)
138{
139 if (var)
140 {
141 struct Address *a = *(struct Address **) var;
142 if (a)
143 {
144 mutt_addr_write(result, a, false);
145 }
146 }
147 else
148 {
149 buf_addstr(result, (char *) cdef->initial);
150 }
151
152 if (buf_is_empty(result))
153 return CSR_SUCCESS | CSR_SUC_EMPTY; /* empty string */
154
155 return CSR_SUCCESS;
156}
157
163static struct Address *address_dup(struct Address *addr)
164{
165 if (!addr)
166 return NULL; /* LCOV_EXCL_LINE */
167
168 struct Address *a = MUTT_MEM_CALLOC(1, struct Address);
169 a->personal = buf_dup(addr->personal);
170 a->mailbox = buf_dup(addr->mailbox);
171 return a;
172}
173
177static int address_native_set(void *var, const struct ConfigDef *cdef,
178 intptr_t value, struct Buffer *err)
179{
180 int rc;
181
182 if (cdef->validator)
183 {
184 rc = cdef->validator(cdef, value, err);
185
186 if (CSR_RESULT(rc) != CSR_SUCCESS)
187 return rc | CSR_INV_VALIDATOR;
188 }
189
190 mutt_addr_free(var);
191
192 struct Address *addr = address_dup((struct Address *) value);
193
194 rc = CSR_SUCCESS;
195 if (!addr)
196 rc |= CSR_SUC_EMPTY;
197
198 *(struct Address **) var = addr;
199 return rc;
200}
201
205static intptr_t address_native_get(void *var, const struct ConfigDef *cdef, struct Buffer *err)
206{
207 struct Address *addr = *(struct Address **) var;
208
209 return (intptr_t) addr;
210}
211
215static bool address_has_been_set(void *var, const struct ConfigDef *cdef)
216{
217 struct Buffer *value = buf_pool_get();
218 struct Address *a = *(struct Address **) var;
219 if (a)
220 mutt_addr_write(value, a, false);
221
222 const char *initial = (const char *) cdef->initial;
223
224 bool rc = !mutt_str_equal(initial, buf_string(value));
225 buf_pool_release(&value);
226 return rc;
227}
228
232static int address_reset(void *var, const struct ConfigDef *cdef, struct Buffer *err)
233{
234 struct Address *a = NULL;
235 const char *initial = (const char *) cdef->initial;
236
237 if (initial)
238 a = address_new(initial);
239
240 int rc = CSR_SUCCESS;
241
242 if (cdef->validator)
243 {
244 rc = cdef->validator(cdef, (intptr_t) a, err);
245
246 if (CSR_RESULT(rc) != CSR_SUCCESS)
247 {
248 address_destroy(&a, cdef);
249 return rc | CSR_INV_VALIDATOR;
250 }
251 }
252
253 if (!a)
254 rc |= CSR_SUC_EMPTY;
255
256 address_destroy(var, cdef);
257
258 *(struct Address **) var = a;
259 return rc;
260}
261
265const struct ConfigSetType CstAddress = {
267 "address",
272 NULL, // string_plus_equals
273 NULL, // string_minus_equals
277};
278
286const struct Address *cs_subset_address(const struct ConfigSubset *sub, const char *name)
287{
288 ASSERT(sub && name);
289
290 struct HashElem *he = cs_subset_create_inheritance(sub, name);
291 ASSERT(he);
292
293#ifndef NDEBUG
294 struct HashElem *he_base = cs_get_base(he);
295 ASSERT(CONFIG_TYPE(he_base->type) == DT_ADDRESS);
296#endif
297
298 intptr_t value = cs_subset_he_native_get(sub, he, NULL);
299 ASSERT(value != INT_MIN);
300
301 return (const struct Address *) value;
302}
void mutt_addrlist_clear(struct AddressList *al)
Unlink and free all Address in an AddressList.
Definition: address.c:1460
void mutt_addr_free(struct Address **ptr)
Free a single Address.
Definition: address.c:462
size_t mutt_addr_write(struct Buffer *buf, struct Address *addr, bool display)
Write a single Address to a buffer.
Definition: address.c:1050
struct Address * mutt_addr_copy(const struct Address *addr)
Copy the real address.
Definition: address.c:745
int mutt_addrlist_parse(struct AddressList *al, const char *s)
Parse a list of email addresses.
Definition: address.c:480
struct Address * address_new(const char *addr)
Create an Address from a string.
Definition: config_type.c:52
const struct ConfigSetType CstAddress
Config type representing an Email Address.
Definition: config_type.c:265
static struct Address * address_dup(struct Address *addr)
Create a copy of an Address object.
Definition: config_type.c:163
const struct Address * cs_subset_address(const struct ConfigSubset *sub, const char *name)
Get an Address config item by name.
Definition: config_type.c:286
Representation of an email address.
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:161
bool buf_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition: buffer.c:291
struct Buffer * buf_new(const char *str)
Allocate a new Buffer.
Definition: buffer.c:304
size_t buf_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:226
struct Buffer * buf_dup(const struct Buffer *buf)
Copy a Buffer into a new allocated buffer.
Definition: buffer.c:586
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
Convenience wrapper for the config headers.
struct HashElem * cs_get_base(struct HashElem *he)
Find the root Config Item.
Definition: set.c:160
#define CSR_ERR_INVALID
Value hasn't been set.
Definition: set.h:36
#define CSR_INV_VALIDATOR
Value was rejected by the validator.
Definition: set.h:46
#define CSR_RESULT(x)
Definition: set.h:50
#define CSR_SUC_EMPTY
Value is empty/unset.
Definition: set.h:40
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:33
Config type representing an email address.
static void address_destroy(void *var, const struct ConfigDef *cdef)
Destroy an Address object - Implements ConfigSetType::destroy() -.
Definition: config_type.c:62
static bool address_has_been_set(void *var, const struct ConfigDef *cdef)
Is the config value different to its initial value? - Implements ConfigSetType::has_been_set() -.
Definition: config_type.c:215
static intptr_t address_native_get(void *var, const struct ConfigDef *cdef, struct Buffer *err)
Get an Address object from an Address config item - Implements ConfigSetType::native_get() -.
Definition: config_type.c:205
static int address_native_set(void *var, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Set an Address config item by Address object - Implements ConfigSetType::native_set() -.
Definition: config_type.c:177
static int address_reset(void *var, const struct ConfigDef *cdef, struct Buffer *err)
Reset an Address to its initial value - Implements ConfigSetType::reset() -.
Definition: config_type.c:232
static int address_string_get(void *var, const struct ConfigDef *cdef, struct Buffer *result)
Get an Address as a string - Implements ConfigSetType::string_get() -.
Definition: config_type.c:137
static int address_string_set(void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
Set an Address by string - Implements ConfigSetType::string_set() -.
Definition: config_type.c:74
#define FREE(x)
Definition: memory.h:55
#define MUTT_MEM_CALLOC(n, type)
Definition: memory.h:40
Convenience wrapper for the library headers.
#define _(a)
Definition: message.h:28
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:254
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:661
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:82
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:96
#define TAILQ_FIRST(head)
Definition: queue.h:780
#define TAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:694
#define ASSERT(COND)
Definition: signal2.h:60
An email address.
Definition: address.h:36
struct Buffer * personal
Real name of address.
Definition: address.h:37
struct Buffer * mailbox
Mailbox and host address.
Definition: address.h:38
String manipulation buffer.
Definition: buffer.h:36
Definition: set.h:62
const char * name
User-visible name.
Definition: set.h:63
int(* validator)(const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Definition: set.h:79
intptr_t initial
Initial value.
Definition: set.h:65
uint32_t type
Variable type, e.g. DT_STRING.
Definition: set.h:64
A set of inherited config items.
Definition: subset.h:46
The item stored in a Hash Table.
Definition: hash.h:44
int type
Type of data stored in Hash Table, e.g. DT_STRING.
Definition: hash.h:45
intptr_t cs_subset_he_native_get(const struct ConfigSubset *sub, struct HashElem *he, struct Buffer *err)
Natively get the value of a HashElem config item.
Definition: subset.c:260
struct HashElem * cs_subset_create_inheritance(const struct ConfigSubset *sub, const char *name)
Create a Subset config item (inherited)
Definition: subset.c:210
#define CONFIG_TYPE(t)
Definition: types.h:49
#define D_INTERNAL_INITIAL_SET
Config item must have its initial value freed.
Definition: types.h:89
@ DT_ADDRESS
e-mail address
Definition: types.h:31
#define D_NOT_EMPTY
Empty strings are not allowed.
Definition: types.h:79