NeoMutt  2018-07-16 +1360-3df4a2
Teaching an old dog new tricks
DOXYGEN
group.c File Reference

Handling for email address groups. More...

#include "config.h"
#include <stdbool.h>
#include <stdlib.h>
#include "email/address.h"
#include "group.h"
+ Include dependency graph for group.c:

Go to the source code of this file.

Functions

void mutt_grouplist_init (void)
 Initialize the GroupList singleton. More...
 
void mutt_grouplist_free (void)
 Free GroupList singleton resource. More...
 
struct Groupmutt_pattern_group (const char *k)
 Match a pattern to a Group. More...
 
static void group_remove (struct Group *g)
 Remove a Group from the Hash Table. More...
 
void mutt_grouplist_clear (struct GroupList *head)
 Clear a GroupList. More...
 
static bool empty_group (struct Group *g)
 Is a Group empty? More...
 
void mutt_grouplist_add (struct GroupList *head, struct Group *group)
 Add a Group to a GroupList. More...
 
void mutt_grouplist_destroy (struct GroupList *head)
 Free a GroupList. More...
 
static void group_add_addrlist (struct Group *g, struct Address *a)
 Add an Address List to a Group. More...
 
static int group_remove_addrlist (struct Group *g, struct Address *a)
 Remove an Address List from a Group. More...
 
static int group_add_regex (struct Group *g, const char *s, int flags, struct Buffer *err)
 Add a Regex to a Group. More...
 
static int group_remove_regex (struct Group *g, const char *s)
 Remove a Regex from a Group. More...
 
void mutt_grouplist_add_addrlist (struct GroupList *head, struct Address *a)
 Add Address list to a GroupList. More...
 
int mutt_grouplist_remove_addrlist (struct GroupList *head, struct Address *a)
 Remove Address from a GroupList. More...
 
int mutt_grouplist_add_regex (struct GroupList *head, const char *s, int flags, struct Buffer *err)
 Add matching Addresses to a GroupList. More...
 
int mutt_grouplist_remove_regex (struct GroupList *head, const char *s)
 Remove matching addresses from a GroupList. More...
 
bool mutt_group_match (struct Group *g, const char *s)
 Does a string match an entry in a Group? More...
 

Variables

static struct HashGroups = NULL
 

Detailed Description

Handling for email address groups.

Authors
  • Thomas Roessler
  • Rocco Rutte

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 group.c.

Function Documentation

void mutt_grouplist_init ( void  )

Initialize the GroupList singleton.

This is called once from init.c when initializing the global structures.

Definition at line 43 of file group.c.

44 {
45  Groups = mutt_hash_new(1031, 0);
46 }
static struct Hash * Groups
Definition: group.c:36
struct Hash * mutt_hash_new(size_t nelem, HashFlags flags)
Create a new Hash table (with string keys)
Definition: hash.c:277

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void mutt_grouplist_free ( void  )

Free GroupList singleton resource.

This is called once from init.c when deinitializing the global resources.

Definition at line 53 of file group.c.

54 {
56 }
static struct Hash * Groups
Definition: group.c:36
void mutt_hash_free(struct Hash **ptr)
elem_free a hash table
Definition: hash.c:457

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

struct Group* mutt_pattern_group ( const char *  k)

Match a pattern to a Group.

Parameters
kPattern to match
Return values
ptrMatching Group
ptrNewly created Group (if no match)

Definition at line 64 of file group.c.

65 {
66  struct Group *p = NULL;
67 
68  if (!k)
69  return 0;
70 
71  p = mutt_hash_find(Groups, k);
72  if (!p)
73  {
74  mutt_debug(LL_DEBUG2, "Creating group %s.\n", k);
75  p = mutt_mem_calloc(1, sizeof(struct Group));
76  p->name = mutt_str_strdup(k);
77  STAILQ_INIT(&p->rs);
79  }
80 
81  return p;
82 }
static struct Hash * Groups
Definition: group.c:36
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:51
void * mutt_hash_find(const struct Hash *table, const char *strkey)
Find the HashElem data in a Hash table element using a key.
Definition: hash.c:373
Log at debug level 2.
Definition: logging.h:57
#define STAILQ_INIT(head)
Definition: queue.h:370
char * name
Definition: group.h:42
struct RegexList rs
Definition: group.h:41
char * mutt_str_strdup(const char *str)
Copy a string, safely.
Definition: string.c:383
A set of email addresses.
Definition: group.h:38
#define mutt_debug(LEVEL,...)
Definition: logging.h:85
struct HashElem * mutt_hash_insert(struct Hash *table, const char *strkey, void *data)
Add a new element to the Hash table (with string keys)
Definition: hash.c:348

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void group_remove ( struct Group g)
static

Remove a Group from the Hash Table.

Parameters
gGroup to remove

Definition at line 88 of file group.c.

89 {
90  if (!g)
91  return;
93  mutt_addr_free(&g->as);
95  FREE(&g->name);
96  FREE(&g);
97 }
void mutt_hash_delete(struct Hash *table, const char *strkey, const void *data)
Remove an element from a Hash table.
Definition: hash.c:433
static struct Hash * Groups
Definition: group.c:36
char * name
Definition: group.h:42
struct Address * as
Definition: group.h:40
struct RegexList rs
Definition: group.h:41
#define FREE(x)
Definition: memory.h:40
void mutt_addr_free(struct Address **p)
Free a list of Addresses.
Definition: address.c:446
void mutt_regexlist_free(struct RegexList *rl)
Free a RegexList object.
Definition: regex.c:167

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void mutt_grouplist_clear ( struct GroupList *  head)

Clear a GroupList.

Parameters
headGroupList to clear

Definition at line 103 of file group.c.

104 {
105  struct GroupNode *np = STAILQ_FIRST(head), *next = NULL;
106  while (np)
107  {
108  group_remove(np->group);
109  next = STAILQ_NEXT(np, entries);
110  FREE(&np);
111  np = next;
112  }
113  STAILQ_INIT(head);
114 }
struct Group * group
Definition: group.h:50
static void group_remove(struct Group *g)
Remove a Group from the Hash Table.
Definition: group.c:88
#define STAILQ_INIT(head)
Definition: queue.h:370
A node in a GroupNode.
Definition: group.h:48
#define STAILQ_NEXT(elm, field)
Definition: queue.h:398
#define FREE(x)
Definition: memory.h:40
#define STAILQ_FIRST(head)
Definition: queue.h:348

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static bool empty_group ( struct Group g)
static

Is a Group empty?

Parameters
gGroup to test
Return values
trueIf the Group is empty

Definition at line 121 of file group.c.

122 {
123  if (!g)
124  return true;
125  return !g->as && STAILQ_EMPTY(&g->rs);
126 }
struct Address * as
Definition: group.h:40
struct RegexList rs
Definition: group.h:41
#define STAILQ_EMPTY(head)
Definition: queue.h:346

+ Here is the caller graph for this function:

void mutt_grouplist_add ( struct GroupList *  head,
struct Group group 
)

Add a Group to a GroupList.

Parameters
headGroupList to add to
groupGroup to add

Definition at line 133 of file group.c.

134 {
135  struct GroupNode *np = NULL;
136  STAILQ_FOREACH(np, head, entries)
137  {
138  if (np->group == group)
139  return;
140  }
141  np = mutt_mem_calloc(1, sizeof(struct GroupNode));
142  np->group = group;
143  STAILQ_INSERT_TAIL(head, np, entries);
144 }
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:51
struct Group * group
Definition: group.h:50
#define STAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:387
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:350
A node in a GroupNode.
Definition: group.h:48

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void mutt_grouplist_destroy ( struct GroupList *  head)

Free a GroupList.

Parameters
headGroupList to free

Definition at line 150 of file group.c.

151 {
152  struct GroupNode *np = STAILQ_FIRST(head), *next = NULL;
153  while (np)
154  {
155  next = STAILQ_NEXT(np, entries);
156  FREE(&np);
157  np = next;
158  }
159  STAILQ_INIT(head);
160 }
#define STAILQ_INIT(head)
Definition: queue.h:370
A node in a GroupNode.
Definition: group.h:48
#define STAILQ_NEXT(elm, field)
Definition: queue.h:398
#define FREE(x)
Definition: memory.h:40
#define STAILQ_FIRST(head)
Definition: queue.h:348

+ Here is the caller graph for this function:

static void group_add_addrlist ( struct Group g,
struct Address a 
)
static

Add an Address List to a Group.

Parameters
gGroup to add to
aAddress List

Definition at line 167 of file group.c.

168 {
169  struct Address **p = NULL, *q = NULL;
170 
171  if (!g)
172  return;
173  if (!a)
174  return;
175 
176  for (p = &g->as; *p; p = &((*p)->next))
177  ;
178 
179  q = mutt_addr_copy_list(a, false);
180  q = mutt_addr_remove_xrefs(g->as, q);
181  *p = q;
182 }
An email address.
Definition: address.h:32
struct Address * mutt_addr_remove_xrefs(struct Address *a, struct Address *b)
Remove cross-references.
Definition: address.c:1328
struct Address * as
Definition: group.h:40
struct Address * mutt_addr_copy_list(struct Address *addr, bool prune)
Copy a list of addresses.
Definition: address.c:730

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static int group_remove_addrlist ( struct Group g,
struct Address a 
)
static

Remove an Address List from a Group.

Parameters
gGroup to modify
aAddress List to remove
Return values
0Success
-1Error

Definition at line 191 of file group.c.

192 {
193  struct Address *p = NULL;
194 
195  if (!g)
196  return -1;
197  if (!a)
198  return -1;
199 
200  for (p = a; p; p = p->next)
202 
203  return 0;
204 }
int mutt_addr_remove_from_list(struct Address **a, const char *mailbox)
Remove an Address from a list.
Definition: address.c:413
An email address.
Definition: address.h:32
char * mailbox
mailbox and host address
Definition: address.h:35
struct Address * as
Definition: group.h:40
struct Address * next
Definition: address.h:39

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static int group_add_regex ( struct Group g,
const char *  s,
int  flags,
struct Buffer err 
)
static

Add a Regex to a Group.

Parameters
gGroup to add to
sRegex string to add
flagsFlags, e.g. REG_ICASE
errBuffer for error message
Return values
0Success
-1Error

Definition at line 215 of file group.c.

216 {
217  return mutt_regexlist_add(&g->rs, s, flags, err);
218 }
int mutt_regexlist_add(struct RegexList *rl, const char *str, int flags, struct Buffer *err)
Compile a regex string and add it to a list.
Definition: regex.c:129
struct RegexList rs
Definition: group.h:41

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static int group_remove_regex ( struct Group g,
const char *  s 
)
static

Remove a Regex from a Group.

Parameters
gGroup to modify
sRegex string to match
Return values
0Success
-1Error

Definition at line 227 of file group.c.

228 {
229  return mutt_regexlist_remove(&g->rs, s);
230 }
int mutt_regexlist_remove(struct RegexList *rl, const char *str)
Remove a Regex from a list.
Definition: regex.c:225
struct RegexList rs
Definition: group.h:41

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void mutt_grouplist_add_addrlist ( struct GroupList *  head,
struct Address a 
)

Add Address list to a GroupList.

Parameters
headGroupList to add to
aAddress to add

Definition at line 237 of file group.c.

238 {
239  struct GroupNode *np = NULL;
240  STAILQ_FOREACH(np, head, entries)
241  {
242  group_add_addrlist(np->group, a);
243  }
244 }
struct Group * group
Definition: group.h:50
static void group_add_addrlist(struct Group *g, struct Address *a)
Add an Address List to a Group.
Definition: group.c:167
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:350
A node in a GroupNode.
Definition: group.h:48

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int mutt_grouplist_remove_addrlist ( struct GroupList *  head,
struct Address a 
)

Remove Address from a GroupList.

Parameters
headGroupList to remove from
aAddress to remove
Return values
0Success
-1Error

Definition at line 253 of file group.c.

254 {
255  int rc = 0;
256  struct GroupNode *np = NULL;
257 
258  STAILQ_FOREACH(np, head, entries)
259  {
260  rc = group_remove_addrlist(np->group, a);
261  if (empty_group(np->group))
262  group_remove(np->group);
263  if (rc)
264  return rc;
265  }
266  return rc;
267 }
static bool empty_group(struct Group *g)
Is a Group empty?
Definition: group.c:121
struct Group * group
Definition: group.h:50
static void group_remove(struct Group *g)
Remove a Group from the Hash Table.
Definition: group.c:88
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:350
A node in a GroupNode.
Definition: group.h:48
static int group_remove_addrlist(struct Group *g, struct Address *a)
Remove an Address List from a Group.
Definition: group.c:191

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int mutt_grouplist_add_regex ( struct GroupList *  head,
const char *  s,
int  flags,
struct Buffer err 
)

Add matching Addresses to a GroupList.

Parameters
headGroupList to add to
sAddress to match
flagsFlags, e.g. REG_ICASE
errBuffer for error message
Return values
0Success
-1Error

Definition at line 278 of file group.c.

280 {
281  int rc = 0;
282 
283  struct GroupNode *np = NULL;
284  STAILQ_FOREACH(np, head, entries)
285  {
286  rc = group_add_regex(np->group, s, flags, err);
287  if (rc)
288  return rc;
289  }
290  return rc;
291 }
struct Group * group
Definition: group.h:50
static int group_add_regex(struct Group *g, const char *s, int flags, struct Buffer *err)
Add a Regex to a Group.
Definition: group.c:215
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:350
A node in a GroupNode.
Definition: group.h:48

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int mutt_grouplist_remove_regex ( struct GroupList *  head,
const char *  s 
)

Remove matching addresses from a GroupList.

Parameters
headGroupList to remove from
sAddress to match
Return values
0Success
-1Error

Definition at line 300 of file group.c.

301 {
302  int rc = 0;
303  struct GroupNode *np = NULL;
304  STAILQ_FOREACH(np, head, entries)
305  {
306  rc = group_remove_regex(np->group, s);
307  if (empty_group(np->group))
308  group_remove(np->group);
309  if (rc)
310  return rc;
311  }
312  return rc;
313 }
static int group_remove_regex(struct Group *g, const char *s)
Remove a Regex from a Group.
Definition: group.c:227
static bool empty_group(struct Group *g)
Is a Group empty?
Definition: group.c:121
struct Group * group
Definition: group.h:50
static void group_remove(struct Group *g)
Remove a Group from the Hash Table.
Definition: group.c:88
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:350
A node in a GroupNode.
Definition: group.h:48

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool mutt_group_match ( struct Group g,
const char *  s 
)

Does a string match an entry in a Group?

Parameters
gGroup to match against
sString to match
Return values
trueIf there's a match

Definition at line 321 of file group.c.

322 {
323  if (!g || !s)
324  return false;
325 
326  if (mutt_regexlist_match(&g->rs, s))
327  return true;
328  for (struct Address *ap = g->as; ap; ap = ap->next)
329  if (ap->mailbox && (mutt_str_strcasecmp(s, ap->mailbox) == 0))
330  return true;
331 
332  return false;
333 }
An email address.
Definition: address.h:32
bool mutt_regexlist_match(struct RegexList *rl, const char *str)
Does a string match any Regex in the list?
Definition: regex.c:188
struct Address * as
Definition: group.h:40
struct RegexList rs
Definition: group.h:41
int mutt_str_strcasecmp(const char *a, const char *b)
Compare two strings ignoring case, safely.
Definition: string.c:624
struct Address * next
Definition: address.h:39

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Variable Documentation

struct Hash* Groups = NULL
static

Definition at line 36 of file group.c.