NeoMutt  2021-10-29-220-g2b1eec
Teaching an old dog new tricks
DOXYGEN
address.c
Go to the documentation of this file.
1 
36 #include "config.h"
37 #include <stddef.h>
38 #include <stdbool.h>
39 #include <stdint.h>
40 #include "mutt/lib.h"
41 #include "address/lib.h"
42 #include "address.h"
43 #include "set.h"
44 #include "types.h"
45 
49 static void address_destroy(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef)
50 {
51  struct Address **a = var;
52  if (!*a)
53  return;
54 
55  address_free(a);
56 }
57 
61 static int address_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef,
62  const char *value, struct Buffer *err)
63 {
64  struct Address *addr = NULL;
65 
66  /* An empty address "" will be stored as NULL */
67  if (var && value && (value[0] != '\0'))
68  {
69  // TODO - config can only store one
70  struct AddressList al = TAILQ_HEAD_INITIALIZER(al);
71  mutt_addrlist_parse(&al, value);
72  addr = mutt_addr_copy(TAILQ_FIRST(&al));
74  }
75 
76  int rc = CSR_SUCCESS;
77 
78  if (var)
79  {
80  if (cdef->validator)
81  {
82  rc = cdef->validator(cs, cdef, (intptr_t) addr, err);
83 
84  if (CSR_RESULT(rc) != CSR_SUCCESS)
85  {
86  address_destroy(cs, &addr, cdef);
87  return rc | CSR_INV_VALIDATOR;
88  }
89  }
90 
91  /* ordinary variable setting */
92  address_destroy(cs, var, cdef);
93 
94  *(struct Address **) var = addr;
95 
96  if (!addr)
97  rc |= CSR_SUC_EMPTY;
98  }
99  else
100  {
101  /* set the default/initial value */
102  if (cdef->type & DT_INITIAL_SET)
103  FREE(&cdef->initial);
104 
105  cdef->type |= DT_INITIAL_SET;
106  cdef->initial = (intptr_t) mutt_str_dup(value);
107  }
108 
109  return rc;
110 }
111 
115 static int address_string_get(const struct ConfigSet *cs, void *var,
116  const struct ConfigDef *cdef, struct Buffer *result)
117 {
118  char tmp[8192] = { 0 };
119  const char *str = NULL;
120 
121  if (var)
122  {
123  struct Address *a = *(struct Address **) var;
124  if (a)
125  {
126  mutt_addr_write(tmp, sizeof(tmp), a, false);
127  str = tmp;
128  }
129  }
130  else
131  {
132  str = (char *) cdef->initial;
133  }
134 
135  if (!str)
136  return CSR_SUCCESS | CSR_SUC_EMPTY; /* empty string */
137 
138  mutt_buffer_addstr(result, str);
139  return CSR_SUCCESS;
140 }
141 
147 static struct Address *address_dup(struct Address *addr)
148 {
149  if (!addr)
150  return NULL; /* LCOV_EXCL_LINE */
151 
152  struct Address *a = mutt_mem_calloc(1, sizeof(*a));
153  a->personal = mutt_str_dup(addr->personal);
154  a->mailbox = mutt_str_dup(addr->mailbox);
155  return a;
156 }
157 
161 static int address_native_set(const struct ConfigSet *cs, void *var,
162  const struct ConfigDef *cdef, intptr_t value,
163  struct Buffer *err)
164 {
165  int rc;
166 
167  if (cdef->validator)
168  {
169  rc = cdef->validator(cs, cdef, value, err);
170 
171  if (CSR_RESULT(rc) != CSR_SUCCESS)
172  return rc | CSR_INV_VALIDATOR;
173  }
174 
175  address_free(var);
176 
177  struct Address *addr = address_dup((struct Address *) value);
178 
179  rc = CSR_SUCCESS;
180  if (!addr)
181  rc |= CSR_SUC_EMPTY;
182 
183  *(struct Address **) var = addr;
184  return rc;
185 }
186 
190 static intptr_t address_native_get(const struct ConfigSet *cs, void *var,
191  const struct ConfigDef *cdef, struct Buffer *err)
192 {
193  struct Address *addr = *(struct Address **) var;
194 
195  return (intptr_t) addr;
196 }
197 
201 static int address_reset(const struct ConfigSet *cs, void *var,
202  const struct ConfigDef *cdef, struct Buffer *err)
203 {
204  struct Address *a = NULL;
205  const char *initial = (const char *) cdef->initial;
206 
207  if (initial)
208  a = address_new(initial);
209 
210  int rc = CSR_SUCCESS;
211 
212  if (cdef->validator)
213  {
214  rc = cdef->validator(cs, cdef, (intptr_t) a, err);
215 
216  if (CSR_RESULT(rc) != CSR_SUCCESS)
217  {
218  address_destroy(cs, &a, cdef);
219  return rc | CSR_INV_VALIDATOR;
220  }
221  }
222 
223  if (!a)
224  rc |= CSR_SUC_EMPTY;
225 
226  address_destroy(cs, var, cdef);
227 
228  *(struct Address **) var = a;
229  return rc;
230 }
231 
237 struct Address *address_new(const char *addr)
238 {
239  struct Address *a = mutt_mem_calloc(1, sizeof(*a));
240  // a->personal = mutt_str_dup(addr);
241  a->mailbox = mutt_str_dup(addr);
242  return a;
243 }
244 
249 void address_free(struct Address **addr)
250 {
251  if (!addr || !*addr)
252  return;
253 
254  FREE(&(*addr)->personal);
255  FREE(&(*addr)->mailbox);
256  FREE(addr);
257 }
258 
262 const struct ConfigSetType CstAddress = {
263  DT_ADDRESS,
264  "address",
269  NULL, // string_plus_equals
270  NULL, // string_minus_equals
273 };
struct Address * mutt_addr_copy(const struct Address *addr)
Copy the real address.
Definition: address.c:716
void mutt_addrlist_clear(struct AddressList *al)
Unlink and free all Address in an AddressList.
Definition: address.c:1470
int mutt_addrlist_parse(struct AddressList *al, const char *s)
Parse a list of email addresses.
Definition: address.c:458
size_t mutt_addr_write(char *buf, size_t buflen, struct Address *addr, bool display)
Write a single Address to a buffer.
Definition: address.c:1025
Email Address Handling.
size_t mutt_buffer_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:225
void address_free(struct Address **addr)
Free an Address object.
Definition: address.c:249
const struct ConfigSetType CstAddress
Config type representing an Email Address.
Definition: address.c:262
static struct Address * address_dup(struct Address *addr)
Create a copy of an Address object.
Definition: address.c:147
struct Address * address_new(const char *addr)
Create an Address from a string.
Definition: address.c:237
Type representing an email address.
static void address_destroy(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef)
Destroy an Address object - Implements ConfigSetType::destroy() -.
Definition: address.c:49
static intptr_t address_native_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Get an Address object from an Address config item - Implements ConfigSetType::native_get() -.
Definition: address.c:190
static int address_native_set(const struct ConfigSet *cs, 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: address.c:161
static int address_reset(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *err)
Reset an Address to its initial value - Implements ConfigSetType::reset() -.
Definition: address.c:201
static int address_string_get(const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef, struct Buffer *result)
Get an Address as a string - Implements ConfigSetType::string_get() -.
Definition: address.c:115
static int address_string_set(const struct ConfigSet *cs, void *var, struct ConfigDef *cdef, const char *value, struct Buffer *err)
Set an Address by string - Implements ConfigSetType::string_set() -.
Definition: address.c:61
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
#define FREE(x)
Definition: memory.h:40
Convenience wrapper for the library headers.
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:181
#define TAILQ_FIRST(head)
Definition: queue.h:723
#define TAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:637
A collection of config items.
#define CSR_INV_VALIDATOR
Value was rejected by the validator.
Definition: set.h:48
#define CSR_RESULT(x)
Definition: set.h:52
#define CSR_SUC_EMPTY
Value is empty/unset.
Definition: set.h:42
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
An email address.
Definition: address.h:36
char * mailbox
Mailbox and host address.
Definition: address.h:38
char * personal
Real name of address.
Definition: address.h:37
String manipulation buffer.
Definition: buffer.h:34
Definition: set.h:64
int(* validator)(const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Definition: set.h:82
intptr_t initial
Initial value.
Definition: set.h:67
uint32_t type
Variable type, e.g. DT_STRING.
Definition: set.h:66
Container for lots of config items.
Definition: set.h:260
Constants for all the config types.
#define DT_INITIAL_SET
Config item must have its initial value freed.
Definition: types.h:79
#define DT_ADDRESS
e-mail address
Definition: types.h:29