NeoMutt  2020-08-07-1-gab41a1
Teaching an old dog new tricks
DOXYGEN
group.c File Reference

Handling for email address groups. More...

#include "config.h"
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include "group.h"
#include "address.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 *gl)
 Clear a GroupList. More...
 
static bool empty_group (struct Group *g)
 Is a Group empty? More...
 
void mutt_grouplist_add (struct GroupList *gl, struct Group *group)
 Add a Group to a GroupList. More...
 
void mutt_grouplist_destroy (struct GroupList *gl)
 Free a GroupList. More...
 
static void group_add_addrlist (struct Group *g, const struct AddressList *al)
 Add an Address List to 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 *gl, struct AddressList *al)
 Add Address list to a GroupList. More...
 
int mutt_grouplist_remove_addrlist (struct GroupList *gl, struct AddressList *al)
 Remove an AddressList from a GroupList. More...
 
int mutt_grouplist_add_regex (struct GroupList *gl, const char *s, int flags, struct Buffer *err)
 Add matching Addresses to a GroupList. More...
 
int mutt_grouplist_remove_regex (struct GroupList *gl, 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 HashTableGroups = NULL
 

Detailed Description

Handling for email address groups.

Authors
  • Thomas Roessler
  • Rocco Rutte
  • 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 group.c.

Function Documentation

◆ mutt_grouplist_init()

void mutt_grouplist_init ( void  )

Initialize the GroupList singleton.

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

Definition at line 45 of file group.c.

46 {
48 }
struct HashTable * mutt_hash_new(size_t num_elems, HashFlags flags)
Create a new Hash Table (with string keys)
Definition: hash.c:251
#define MUTT_HASH_NO_FLAGS
No flags are set.
Definition: hash.h:97
static struct HashTable * Groups
Definition: group.c:38
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_grouplist_free()

void mutt_grouplist_free ( void  )

Free GroupList singleton resource.

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

Definition at line 55 of file group.c.

56 {
58 }
void mutt_hash_free(struct HashTable **ptr)
Free a hash table.
Definition: hash.c:447
static struct HashTable * Groups
Definition: group.c:38
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_pattern_group()

struct Group* mutt_pattern_group ( const char *  pat)

Match a pattern to a Group.

Parameters
patPattern to match
Return values
ptrMatching Group, or new Group (if no match)

Definition at line 65 of file group.c.

66 {
67  if (!pat)
68  return NULL;
69 
70  struct Group *g = mutt_hash_find(Groups, pat);
71  if (!g)
72  {
73  mutt_debug(LL_DEBUG2, "Creating group %s\n", pat);
74  g = mutt_mem_calloc(1, sizeof(struct Group));
75  g->name = mutt_str_dup(pat);
76  STAILQ_INIT(&g->rs);
77  TAILQ_INIT(&g->al);
79  }
80 
81  return g;
82 }
void * mutt_hash_find(const struct HashTable *table, const char *strkey)
Find the HashElem data in a Hash Table element using a key.
Definition: hash.c:354
struct HashElem * mutt_hash_insert(struct HashTable *table, const char *strkey, void *data)
Add a new element to the Hash Table (with string keys)
Definition: hash.c:327
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:375
Log at debug level 2.
Definition: logging.h:41
#define STAILQ_INIT(head)
Definition: queue.h:369
char * name
Name of Group.
Definition: group.h:42
#define TAILQ_INIT(head)
Definition: queue.h:758
struct RegexList rs
Group Regex patterns.
Definition: group.h:41
struct AddressList al
List of Addresses.
Definition: group.h:40
A set of email addresses.
Definition: group.h:38
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
static struct HashTable * Groups
Definition: group.c:38
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ group_remove()

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;
95  FREE(&g->name);
96  FREE(&g);
97 }
void mutt_addrlist_clear(struct AddressList *al)
Unlink and free all Address in an AddressList.
Definition: address.c:1468
char * name
Name of Group.
Definition: group.h:42
struct RegexList rs
Group Regex patterns.
Definition: group.h:41
struct AddressList al
List of Addresses.
Definition: group.h:40
#define FREE(x)
Definition: memory.h:40
void mutt_hash_delete(struct HashTable *table, const char *strkey, const void *data)
Remove an element from a Hash Table.
Definition: hash.c:419
void mutt_regexlist_free(struct RegexList *rl)
Free a RegexList object.
Definition: regex.c:169
static struct HashTable * Groups
Definition: group.c:38
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_grouplist_clear()

void mutt_grouplist_clear ( struct GroupList *  gl)

Clear a GroupList.

Parameters
glGroupList to clear

Definition at line 103 of file group.c.

104 {
105  if (!gl)
106  return;
107 
108  struct GroupNode *np = STAILQ_FIRST(gl);
109  struct GroupNode *next = NULL;
110  while (np)
111  {
112  group_remove(np->group);
113  next = STAILQ_NEXT(np, entries);
114  FREE(&np);
115  np = next;
116  }
117  STAILQ_INIT(gl);
118 }
struct Group * group
Address 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:369
An element in a GroupList.
Definition: group.h:48
#define STAILQ_NEXT(elm, field)
Definition: queue.h:397
#define FREE(x)
Definition: memory.h:40
#define STAILQ_FIRST(head)
Definition: queue.h:347
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ empty_group()

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

126 {
127  if (!g)
128  return true;
129  return TAILQ_EMPTY(&g->al) && STAILQ_EMPTY(&g->rs);
130 }
struct RegexList rs
Group Regex patterns.
Definition: group.h:41
struct AddressList al
List of Addresses.
Definition: group.h:40
#define STAILQ_EMPTY(head)
Definition: queue.h:345
#define TAILQ_EMPTY(head)
Definition: queue.h:714
+ Here is the caller graph for this function:

◆ mutt_grouplist_add()

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

Add a Group to a GroupList.

Parameters
glGroupList to add to
groupGroup to add

Definition at line 137 of file group.c.

138 {
139  if (!gl || !group)
140  return;
141 
142  struct GroupNode *np = NULL;
143  STAILQ_FOREACH(np, gl, entries)
144  {
145  if (np->group == group)
146  return;
147  }
148  np = mutt_mem_calloc(1, sizeof(struct GroupNode));
149  np->group = group;
150  STAILQ_INSERT_TAIL(gl, np, entries);
151 }
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
struct Group * group
Address Group.
Definition: group.h:50
#define STAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:386
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:349
An element in a GroupList.
Definition: group.h:48
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_grouplist_destroy()

void mutt_grouplist_destroy ( struct GroupList *  gl)

Free a GroupList.

Parameters
glGroupList to free

Definition at line 157 of file group.c.

158 {
159  if (!gl)
160  return;
161 
162  struct GroupNode *np = STAILQ_FIRST(gl);
163  struct GroupNode *next = NULL;
164  while (np)
165  {
166  next = STAILQ_NEXT(np, entries);
167  FREE(&np);
168  np = next;
169  }
170  STAILQ_INIT(gl);
171 }
#define STAILQ_INIT(head)
Definition: queue.h:369
An element in a GroupList.
Definition: group.h:48
#define STAILQ_NEXT(elm, field)
Definition: queue.h:397
#define FREE(x)
Definition: memory.h:40
#define STAILQ_FIRST(head)
Definition: queue.h:347
+ Here is the caller graph for this function:

◆ group_add_addrlist()

static void group_add_addrlist ( struct Group g,
const struct AddressList *  al 
)
static

Add an Address List to a Group.

Parameters
gGroup to add to
alAddress List

Definition at line 178 of file group.c.

179 {
180  if (!g || !al)
181  return;
182 
183  struct AddressList al_new = TAILQ_HEAD_INITIALIZER(al_new);
184  mutt_addrlist_copy(&al_new, al, false);
185  mutt_addrlist_remove_xrefs(&g->al, &al_new);
186  struct Address *a = NULL, *tmp = NULL;
187  TAILQ_FOREACH_SAFE(a, &al_new, entries, tmp)
188  {
189  TAILQ_REMOVE(&al_new, a, entries);
190  mutt_addrlist_append(&g->al, a);
191  }
192  assert(TAILQ_EMPTY(&al_new));
193 }
void mutt_addrlist_copy(struct AddressList *dst, const struct AddressList *src, bool prune)
Copy a list of addresses into another list.
Definition: address.c:737
#define TAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition: queue.h:728
An email address.
Definition: address.h:34
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:834
struct AddressList al
List of Addresses.
Definition: group.h:40
void mutt_addrlist_remove_xrefs(const struct AddressList *a, struct AddressList *b)
Remove cross-references.
Definition: address.c:1441
#define TAILQ_EMPTY(head)
Definition: queue.h:714
#define TAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:630
void mutt_addrlist_append(struct AddressList *al, struct Address *a)
Append an Address to an AddressList.
Definition: address.c:1488
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ group_add_regex()

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

205 {
206  return mutt_regexlist_add(&g->rs, s, flags, err);
207 }
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
Group Regex patterns.
Definition: group.h:41
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ group_remove_regex()

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

217 {
218  return mutt_regexlist_remove(&g->rs, s);
219 }
int mutt_regexlist_remove(struct RegexList *rl, const char *str)
Remove a Regex from a list.
Definition: regex.c:225
struct RegexList rs
Group Regex patterns.
Definition: group.h:41
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_grouplist_add_addrlist()

void mutt_grouplist_add_addrlist ( struct GroupList *  gl,
struct AddressList *  al 
)

Add Address list to a GroupList.

Parameters
glGroupList to add to
alAddress list to add

Definition at line 226 of file group.c.

227 {
228  if (!gl || !al)
229  return;
230 
231  struct GroupNode *np = NULL;
232  STAILQ_FOREACH(np, gl, entries)
233  {
234  group_add_addrlist(np->group, al);
235  }
236 }
static void group_add_addrlist(struct Group *g, const struct AddressList *al)
Add an Address List to a Group.
Definition: group.c:178
struct Group * group
Address Group.
Definition: group.h:50
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:349
An element in a GroupList.
Definition: group.h:48
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_grouplist_remove_addrlist()

int mutt_grouplist_remove_addrlist ( struct GroupList *  gl,
struct AddressList *  al 
)

Remove an AddressList from a GroupList.

Parameters
glGroupList to remove from
alAddressList to remove
Return values
0Success
-1Error

Definition at line 245 of file group.c.

246 {
247  if (!gl || !al)
248  return -1;
249 
250  struct GroupNode *gnp = NULL;
251  STAILQ_FOREACH(gnp, gl, entries)
252  {
253  struct Address *a = NULL;
254  TAILQ_FOREACH(a, al, entries)
255  {
256  mutt_addrlist_remove(&gnp->group->al, a->mailbox);
257  }
258  if (empty_group(gnp->group))
259  {
260  group_remove(gnp->group);
261  }
262  }
263 
264  return 0;
265 }
int mutt_addrlist_remove(struct AddressList *al, const char *mailbox)
Remove an Address from a list.
Definition: address.c:413
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:718
static bool empty_group(struct Group *g)
Is a Group empty?
Definition: group.c:125
struct Group * group
Address Group.
Definition: group.h:50
static void group_remove(struct Group *g)
Remove a Group from the Hash Table.
Definition: group.c:88
An email address.
Definition: address.h:34
char * mailbox
Mailbox and host address.
Definition: address.h:37
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:349
An element in a GroupList.
Definition: group.h:48
struct AddressList al
List of Addresses.
Definition: group.h:40
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_grouplist_add_regex()

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

Add matching Addresses to a GroupList.

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

Definition at line 276 of file group.c.

277 {
278  if (!gl || !s)
279  return -1;
280 
281  int rc = 0;
282 
283  struct GroupNode *np = NULL;
284  STAILQ_FOREACH(np, gl, 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
Address 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:204
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:349
An element in a GroupList.
Definition: group.h:48
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_grouplist_remove_regex()

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

Remove matching addresses from a GroupList.

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

Definition at line 300 of file group.c.

301 {
302  if (!gl || !s)
303  return -1;
304 
305  int rc = 0;
306  struct GroupNode *np = NULL;
307  STAILQ_FOREACH(np, gl, entries)
308  {
309  rc = group_remove_regex(np->group, s);
310  if (empty_group(np->group))
311  group_remove(np->group);
312  if (rc)
313  return rc;
314  }
315  return rc;
316 }
static int group_remove_regex(struct Group *g, const char *s)
Remove a Regex from a Group.
Definition: group.c:216
static bool empty_group(struct Group *g)
Is a Group empty?
Definition: group.c:125
struct Group * group
Address 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:349
An element in a GroupList.
Definition: group.h:48
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_group_match()

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

325 {
326  if (!g || !s)
327  return false;
328 
329  if (mutt_regexlist_match(&g->rs, s))
330  return true;
331  struct Address *a = NULL;
332  TAILQ_FOREACH(a, &g->al, entries)
333  {
334  if (a->mailbox && mutt_istr_equal(s, a->mailbox))
335  return true;
336  }
337 
338  return false;
339 }
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:718
An email address.
Definition: address.h:34
char * mailbox
Mailbox and host address.
Definition: address.h:37
bool mutt_regexlist_match(struct RegexList *rl, const char *str)
Does a string match any Regex in the list?
Definition: regex.c:190
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition: string.c:888
struct RegexList rs
Group Regex patterns.
Definition: group.h:41
struct AddressList al
List of Addresses.
Definition: group.h:40
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ Groups

struct HashTable* Groups = NULL
static

Definition at line 38 of file group.c.