NeoMutt  2018-07-16 +1783-b00bd9
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 "address/lib.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 *pat)
 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 {
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:276
#define MUTT_HASH_NO_FLAGS
No flags are set.
Definition: hash.h:74

+ 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:472

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

struct Group* mutt_pattern_group ( const char *  pat)

Match a pattern to a Group.

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

Definition at line 64 of file group.c.

65 {
66  if (!pat)
67  return 0;
68 
69  struct Group *g = mutt_hash_find(Groups, pat);
70  if (!g)
71  {
72  mutt_debug(LL_DEBUG2, "Creating group %s\n", pat);
73  g = mutt_mem_calloc(1, sizeof(struct Group));
74  g->name = mutt_str_strdup(pat);
75  STAILQ_INIT(&g->rs);
77  }
78 
79  return g;
80 }
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:379
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:380
A set of email addresses.
Definition: group.h:38
#define mutt_debug(LEVEL,...)
Definition: logging.h:80
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:352

+ 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 86 of file group.c.

87 {
88  if (!g)
89  return;
91  mutt_addr_free(&g->as);
93  FREE(&g->name);
94  FREE(&g);
95 }
void mutt_hash_delete(struct Hash *table, const char *strkey, const void *data)
Remove an element from a Hash table.
Definition: hash.c:444
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:451
void mutt_regexlist_free(struct RegexList *rl)
Free a RegexList object.
Definition: regex.c:169

+ 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 101 of file group.c.

102 {
103  if (!head)
104  return;
105 
106  struct GroupNode *np = STAILQ_FIRST(head), *next = NULL;
107  while (np)
108  {
109  group_remove(np->group);
110  next = STAILQ_NEXT(np, entries);
111  FREE(&np);
112  np = next;
113  }
114  STAILQ_INIT(head);
115 }
struct Group * group
Definition: group.h:50
static void group_remove(struct Group *g)
Remove a Group from the Hash Table.
Definition: group.c:86
#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 122 of file group.c.

123 {
124  if (!g)
125  return true;
126  return !g->as && STAILQ_EMPTY(&g->rs);
127 }
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 134 of file group.c.

135 {
136  if (!head || !group)
137  return;
138 
139  struct GroupNode *np = NULL;
140  STAILQ_FOREACH(np, head, entries)
141  {
142  if (np->group == group)
143  return;
144  }
145  np = mutt_mem_calloc(1, sizeof(struct GroupNode));
146  np->group = group;
147  STAILQ_INSERT_TAIL(head, np, entries);
148 }
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 154 of file group.c.

155 {
156  if (!head)
157  return;
158 
159  struct GroupNode *np = STAILQ_FIRST(head), *next = NULL;
160  while (np)
161  {
162  next = STAILQ_NEXT(np, entries);
163  FREE(&np);
164  np = next;
165  }
166  STAILQ_INIT(head);
167 }
#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 174 of file group.c.

175 {
176  if (!g || !a)
177  return;
178 
179  struct Address **p = NULL, *q = NULL;
180 
181  for (p = &g->as; *p; p = &((*p)->next))
182  ;
183 
184  q = mutt_addr_copy_list(a, false);
185  q = mutt_addr_remove_xrefs(g->as, q);
186  *p = q;
187 }
struct Address * mutt_addr_copy_list(struct Address *addr, bool prune)
Copy a list of addresses.
Definition: address.c:750
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:1369
struct Address * as
Definition: group.h:40

+ 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 196 of file group.c.

197 {
198  if (!g || !a)
199  return -1;
200 
201  for (struct Address *p = a; p; p = p->next)
203 
204  return 0;
205 }
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 216 of file group.c.

217 {
218  return mutt_regexlist_add(&g->rs, s, flags, err);
219 }
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:131
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 228 of file group.c.

229 {
230  return mutt_regexlist_remove(&g->rs, s);
231 }
int mutt_regexlist_remove(struct RegexList *rl, const char *str)
Remove a Regex from a list.
Definition: regex.c:227
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 238 of file group.c.

239 {
240  if (!head || !a)
241  return;
242 
243  struct GroupNode *np = NULL;
244  STAILQ_FOREACH(np, head, entries)
245  {
246  group_add_addrlist(np->group, a);
247  }
248 }
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:174
#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 257 of file group.c.

258 {
259  if (!head || !a)
260  return -1;
261 
262  int rc = 0;
263  struct GroupNode *np = NULL;
264 
265  STAILQ_FOREACH(np, head, entries)
266  {
267  rc = group_remove_addrlist(np->group, a);
268  if (empty_group(np->group))
269  group_remove(np->group);
270  if (rc)
271  return rc;
272  }
273  return rc;
274 }
static bool empty_group(struct Group *g)
Is a Group empty?
Definition: group.c:122
struct Group * group
Definition: group.h:50
static void group_remove(struct Group *g)
Remove a Group from the Hash Table.
Definition: group.c:86
#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:196

+ 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 285 of file group.c.

287 {
288  if (!head || !s)
289  return -1;
290 
291  int rc = 0;
292 
293  struct GroupNode *np = NULL;
294  STAILQ_FOREACH(np, head, entries)
295  {
296  rc = group_add_regex(np->group, s, flags, err);
297  if (rc)
298  return rc;
299  }
300  return rc;
301 }
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:216
#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 310 of file group.c.

311 {
312  if (!head || !s)
313  return -1;
314 
315  int rc = 0;
316  struct GroupNode *np = NULL;
317  STAILQ_FOREACH(np, head, entries)
318  {
319  rc = group_remove_regex(np->group, s);
320  if (empty_group(np->group))
321  group_remove(np->group);
322  if (rc)
323  return rc;
324  }
325  return rc;
326 }
static int group_remove_regex(struct Group *g, const char *s)
Remove a Regex from a Group.
Definition: group.c:228
static bool empty_group(struct Group *g)
Is a Group empty?
Definition: group.c:122
struct Group * group
Definition: group.h:50
static void group_remove(struct Group *g)
Remove a Group from the Hash Table.
Definition: group.c:86
#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 334 of file group.c.

335 {
336  if (!g || !s)
337  return false;
338 
339  if (mutt_regexlist_match(&g->rs, s))
340  return true;
341  for (struct Address *ap = g->as; ap; ap = ap->next)
342  if (ap->mailbox && (mutt_str_strcasecmp(s, ap->mailbox) == 0))
343  return true;
344 
345  return false;
346 }
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:190
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:631
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.