NeoMutt  2020-11-20
Teaching an old dog new tricks
DOXYGEN
address.c File Reference

Type representing an email address. More...

#include "config.h"
#include <stddef.h>
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include "mutt/lib.h"
#include "address/lib.h"
#include "address.h"
#include "set.h"
#include "types.h"
+ Include dependency graph for address.c:

Go to the source code of this file.

Functions

static void address_destroy (const struct ConfigSet *cs, void *var, const struct ConfigDef *cdef)
 Destroy an Address object - Implements ConfigSetType::destroy() More...
 
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() More...
 
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() More...
 
static struct Addressaddress_dup (struct Address *addr)
 Create a copy of an Address object. More...
 
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() More...
 
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() More...
 
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() More...
 
struct Addressaddress_new (const char *addr)
 Create an Address from a string. More...
 
void address_free (struct Address **addr)
 Free an Address object. More...
 

Variables

const struct ConfigSetType cst_address
 Config type representing an Email Addresss. More...
 

Detailed Description

Type representing an email address.

Authors
  • Richard Russon
  • Pietro Cerutti

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file address.c.

Function Documentation

◆ address_destroy()

static void address_destroy ( const struct ConfigSet cs,
void *  var,
const struct ConfigDef cdef 
)
static

Destroy an Address object - Implements ConfigSetType::destroy()

Definition at line 49 of file address.c.

50 {
51  struct Address **a = var;
52  if (!*a)
53  return;
54 
55  address_free(a);
56 }
An email address.
Definition: address.h:34
void address_free(struct Address **addr)
Free an Address object.
Definition: address.c:249
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ address_string_set()

static int address_string_set ( const struct ConfigSet cs,
void *  var,
struct ConfigDef cdef,
const char *  value,
struct Buffer err 
)
static

Set an Address by string - Implements ConfigSetType::string_set()

Definition at line 61 of file address.c.

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 = IP mutt_str_dup(value);
107  }
108 
109  return rc;
110 }
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
#define IP
Definition: set.h:54
#define TAILQ_FIRST(head)
Definition: queue.h:716
int mutt_addrlist_parse(struct AddressList *al, const char *s)
Parse a list of email addresses.
Definition: address.c:458
void mutt_addrlist_clear(struct AddressList *al)
Unlink and free all Address in an AddressList.
Definition: address.c:1468
intptr_t initial
Initial value.
Definition: set.h:66
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:370
#define DT_INITIAL_SET
Config item must have its initial value freed.
Definition: types.h:81
An email address.
Definition: address.h:34
struct Address * mutt_addr_copy(const struct Address *addr)
Copy the real address.
Definition: address.c:716
#define CSR_RESULT(x)
Definition: set.h:52
#define CSR_SUC_EMPTY
Value is empty/unset.
Definition: set.h:42
int(* validator)(const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Validate a config variable.
Definition: set.h:78
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
unsigned int type
Variable type, e.g. DT_STRING.
Definition: set.h:64
#define FREE(x)
Definition: memory.h:40
#define TAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:630
#define CSR_INV_VALIDATOR
Value was rejected by the validator.
Definition: set.h:48
+ Here is the call graph for this function:

◆ address_string_get()

static int address_string_get ( const struct ConfigSet cs,
void *  var,
const struct ConfigDef cdef,
struct Buffer result 
)
static

Get an Address as a string - Implements ConfigSetType::string_get()

Definition at line 115 of file address.c.

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 }
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
intptr_t initial
Initial value.
Definition: set.h:66
An email address.
Definition: address.h:34
#define CSR_SUC_EMPTY
Value is empty/unset.
Definition: set.h:42
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
size_t mutt_buffer_addstr(struct Buffer *buf, const char *s)
Add a string to a Buffer.
Definition: buffer.c:225
+ Here is the call graph for this function:

◆ address_dup()

static struct Address* address_dup ( struct Address addr)
static

Create a copy of an Address object.

Parameters
addrAddress to duplicate
Return values
ptrNew Address object

Definition at line 147 of file address.c.

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 }
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:370
An email address.
Definition: address.h:34
char * mailbox
Mailbox and host address.
Definition: address.h:37
char * personal
Real name of address.
Definition: address.h:36
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ address_native_set()

static int address_native_set ( const struct ConfigSet cs,
void *  var,
const struct ConfigDef cdef,
intptr_t  value,
struct Buffer err 
)
static

Set an Address config item by Address object - Implements ConfigSetType::native_set()

Definition at line 161 of file address.c.

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 }
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
An email address.
Definition: address.h:34
#define CSR_RESULT(x)
Definition: set.h:52
#define CSR_SUC_EMPTY
Value is empty/unset.
Definition: set.h:42
static struct Address * address_dup(struct Address *addr)
Create a copy of an Address object.
Definition: address.c:147
int(* validator)(const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Validate a config variable.
Definition: set.h:78
void address_free(struct Address **addr)
Free an Address object.
Definition: address.c:249
#define CSR_INV_VALIDATOR
Value was rejected by the validator.
Definition: set.h:48
+ Here is the call graph for this function:

◆ address_native_get()

static intptr_t address_native_get ( const struct ConfigSet cs,
void *  var,
const struct ConfigDef cdef,
struct Buffer err 
)
static

Get an Address object from an Address config item - Implements ConfigSetType::native_get()

Definition at line 190 of file address.c.

192 {
193  struct Address *addr = *(struct Address **) var;
194 
195  return (intptr_t) addr;
196 }
An email address.
Definition: address.h:34

◆ address_reset()

static int address_reset ( const struct ConfigSet cs,
void *  var,
const struct ConfigDef cdef,
struct Buffer err 
)
static

Reset an Address to its initial value - Implements ConfigSetType::reset()

Definition at line 201 of file address.c.

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 }
#define CSR_SUCCESS
Action completed successfully.
Definition: set.h:35
intptr_t initial
Initial value.
Definition: set.h:66
An email address.
Definition: address.h:34
struct Address * address_new(const char *addr)
Create an Address from a string.
Definition: address.c:237
#define CSR_RESULT(x)
Definition: set.h:52
#define CSR_SUC_EMPTY
Value is empty/unset.
Definition: set.h:42
int(* validator)(const struct ConfigSet *cs, const struct ConfigDef *cdef, intptr_t value, struct Buffer *err)
Validate a config variable.
Definition: set.h:78
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
#define CSR_INV_VALIDATOR
Value was rejected by the validator.
Definition: set.h:48
+ Here is the call graph for this function:

◆ address_new()

struct Address* address_new ( const char *  addr)

Create an Address from a string.

Parameters
addrEmail address to parse
Return values
ptrNew Address object

Definition at line 237 of file address.c.

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 }
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:370
An email address.
Definition: address.h:34
char * mailbox
Mailbox and host address.
Definition: address.h:37
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ address_free()

void address_free ( struct Address **  addr)

Free an Address object.

Parameters
[out]addrAddress to free

Definition at line 249 of file address.c.

250 {
251  if (!addr || !*addr)
252  return;
253 
254  FREE(&(*addr)->personal);
255  FREE(&(*addr)->mailbox);
256  FREE(addr);
257 }
#define FREE(x)
Definition: memory.h:40
+ Here is the caller graph for this function:

Variable Documentation

◆ cst_address

const struct ConfigSetType cst_address
Initial value:
= {
"address",
NULL,
NULL,
}
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_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
#define DT_ADDRESS
e-mail address
Definition: types.h:29
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 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_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

Config type representing an Email Addresss.

Definition at line 262 of file address.c.