NeoMutt  2022-04-29-249-gaae397
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 group_free (struct Group **ptr)
 Free an Address Group. More...
 
struct Groupgroup_new (const char *pat)
 Create a new Address Group. More...
 
void group_hash_free (int type, void *obj, intptr_t data)
 Free our hash table data - Implements hash_hdata_free_t -. More...
 
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, uint16_t 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, uint16_t 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

◆ group_free()

void group_free ( struct Group **  ptr)

Free an Address Group.

Parameters
ptrGroup to free

Definition at line 44 of file group.c.

45{
46 if (!ptr || !*ptr)
47 return;
48
49 struct Group *g = *ptr;
50
53 FREE(&g->name);
54
55 FREE(ptr);
56}
void mutt_addrlist_clear(struct AddressList *al)
Unlink and free all Address in an AddressList.
Definition: address.c:1470
#define FREE(x)
Definition: memory.h:43
void mutt_regexlist_free(struct RegexList *rl)
Free a RegexList object.
Definition: regex.c:174
A set of email addresses.
Definition: group.h:40
char * name
Name of Group.
Definition: group.h:43
struct AddressList al
List of Addresses.
Definition: group.h:41
struct RegexList rs
Group Regex patterns.
Definition: group.h:42
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ group_new()

struct Group * group_new ( const char *  pat)

Create a new Address Group.

Parameters
patPattern
Return values
ptrNew Address Group
Note
The pattern will be copied

Definition at line 65 of file group.c.

66{
67 struct Group *g = mutt_mem_calloc(1, sizeof(struct Group));
68
69 g->name = mutt_str_dup(pat);
70 STAILQ_INIT(&g->rs);
71 TAILQ_INIT(&g->al);
72
73 return g;
74}
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:250
#define STAILQ_INIT(head)
Definition: queue.h:372
#define TAILQ_INIT(head)
Definition: queue.h:765
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

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

91{
93
95}
static struct HashTable * Groups
Definition: group.c:38
void group_hash_free(int type, void *obj, intptr_t data)
Free our hash table data - Implements hash_hdata_free_t -.
Definition: group.c:79
struct HashTable * mutt_hash_new(size_t num_elems, HashFlags flags)
Create a new Hash Table (with string keys)
Definition: hash.c:259
void mutt_hash_set_destructor(struct HashTable *table, hash_hdata_free_t fn, intptr_t fn_data)
Set the destructor for a Hash Table.
Definition: hash.c:301
#define MUTT_HASH_NO_FLAGS
No flags are set.
Definition: hash.h:109
+ 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 102 of file group.c.

103{
105}
void mutt_hash_free(struct HashTable **ptr)
Free a hash table.
Definition: hash.c:457
+ 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 112 of file group.c.

113{
114 if (!pat)
115 return NULL;
116
117 struct Group *g = mutt_hash_find(Groups, pat);
118 if (!g)
119 {
120 mutt_debug(LL_DEBUG2, "Creating group %s\n", pat);
121 g = group_new(pat);
123 }
124
125 return g;
126}
struct Group * group_new(const char *pat)
Create a new Address Group.
Definition: group.c:65
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
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:335
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:362
@ LL_DEBUG2
Log at debug level 2.
Definition: logging.h:41
+ 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 132 of file group.c.

133{
134 if (!g)
135 return;
139 FREE(&g->name);
140 FREE(&g);
141}
void mutt_hash_delete(struct HashTable *table, const char *strkey, const void *data)
Remove an element from a Hash Table.
Definition: hash.c:427
+ 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 147 of file group.c.

148{
149 if (!gl)
150 return;
151
152 struct GroupNode *np = STAILQ_FIRST(gl);
153 struct GroupNode *next = NULL;
154 while (np)
155 {
156 group_remove(np->group);
157 next = STAILQ_NEXT(np, entries);
158 FREE(&np);
159 np = next;
160 }
161 STAILQ_INIT(gl);
162}
static void group_remove(struct Group *g)
Remove a Group from the Hash Table.
Definition: group.c:132
#define STAILQ_FIRST(head)
Definition: queue.h:350
#define STAILQ_NEXT(elm, field)
Definition: queue.h:400
An element in a GroupList.
Definition: group.h:50
struct Group * group
Address Group.
Definition: group.h:51
+ 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
trueThe Group is empty

Definition at line 169 of file group.c.

170{
171 if (!g)
172 return true;
173 return TAILQ_EMPTY(&g->al) && STAILQ_EMPTY(&g->rs);
174}
#define STAILQ_EMPTY(head)
Definition: queue.h:348
#define TAILQ_EMPTY(head)
Definition: queue.h:721
+ 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 181 of file group.c.

182{
183 if (!gl || !group)
184 return;
185
186 struct GroupNode *np = NULL;
187 STAILQ_FOREACH(np, gl, entries)
188 {
189 if (np->group == group)
190 return;
191 }
192 np = mutt_mem_calloc(1, sizeof(struct GroupNode));
193 np->group = group;
194 STAILQ_INSERT_TAIL(gl, np, entries);
195}
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:352
#define STAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:389
+ 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 201 of file group.c.

202{
203 if (!gl)
204 return;
205
206 struct GroupNode *np = STAILQ_FIRST(gl);
207 struct GroupNode *next = NULL;
208 while (np)
209 {
210 next = STAILQ_NEXT(np, entries);
211 FREE(&np);
212 np = next;
213 }
214 STAILQ_INIT(gl);
215}
+ 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 222 of file group.c.

223{
224 if (!g || !al)
225 return;
226
227 struct AddressList al_new = TAILQ_HEAD_INITIALIZER(al_new);
228 mutt_addrlist_copy(&al_new, al, false);
229 mutt_addrlist_remove_xrefs(&g->al, &al_new);
230 struct Address *a = NULL, *tmp = NULL;
231 TAILQ_FOREACH_SAFE(a, &al_new, entries, tmp)
232 {
233 TAILQ_REMOVE(&al_new, a, entries);
234 mutt_addrlist_append(&g->al, a);
235 }
236 assert(TAILQ_EMPTY(&al_new));
237}
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
void mutt_addrlist_append(struct AddressList *al, struct Address *a)
Append an Address to an AddressList.
Definition: address.c:1490
void mutt_addrlist_remove_xrefs(const struct AddressList *a, struct AddressList *b)
Remove cross-references.
Definition: address.c:1443
#define TAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition: queue.h:735
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:841
#define TAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:637
An email address.
Definition: address.h:36
+ 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,
uint16_t  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 248 of file group.c.

249{
250 return mutt_regexlist_add(&g->rs, s, flags, err);
251}
int mutt_regexlist_add(struct RegexList *rl, const char *str, uint16_t flags, struct Buffer *err)
Compile a regex string and add it to a list.
Definition: regex.c:135
+ 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 260 of file group.c.

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

271{
272 if (!gl || !al)
273 return;
274
275 struct GroupNode *np = NULL;
276 STAILQ_FOREACH(np, gl, entries)
277 {
278 group_add_addrlist(np->group, al);
279 }
280}
static void group_add_addrlist(struct Group *g, const struct AddressList *al)
Add an Address List to a Group.
Definition: group.c:222
+ 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 289 of file group.c.

290{
291 if (!gl || !al)
292 return -1;
293
294 struct GroupNode *gnp = NULL;
295 STAILQ_FOREACH(gnp, gl, entries)
296 {
297 struct Address *a = NULL;
298 TAILQ_FOREACH(a, al, entries)
299 {
301 }
302 if (empty_group(gnp->group))
303 {
304 group_remove(gnp->group);
305 }
306 }
307
308 return 0;
309}
int mutt_addrlist_remove(struct AddressList *al, const char *mailbox)
Remove an Address from a list.
Definition: address.c:413
static bool empty_group(struct Group *g)
Is a Group empty?
Definition: group.c:169
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:725
char * mailbox
Mailbox and host address.
Definition: address.h:38
+ 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,
uint16_t  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 320 of file group.c.

322{
323 if (!gl || !s)
324 return -1;
325
326 int rc = 0;
327
328 struct GroupNode *np = NULL;
329 STAILQ_FOREACH(np, gl, entries)
330 {
331 rc = group_add_regex(np->group, s, flags, err);
332 if (rc)
333 return rc;
334 }
335 return rc;
336}
static int group_add_regex(struct Group *g, const char *s, uint16_t flags, struct Buffer *err)
Add a Regex to a Group.
Definition: group.c:248
+ 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 345 of file group.c.

346{
347 if (!gl || !s)
348 return -1;
349
350 int rc = 0;
351 struct GroupNode *np = NULL;
352 STAILQ_FOREACH(np, gl, entries)
353 {
354 rc = group_remove_regex(np->group, s);
355 if (empty_group(np->group))
356 group_remove(np->group);
357 if (rc)
358 return rc;
359 }
360 return rc;
361}
static int group_remove_regex(struct Group *g, const char *s)
Remove a Regex from a Group.
Definition: group.c:260
+ 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
trueThere's a match

Definition at line 369 of file group.c.

370{
371 if (!g || !s)
372 return false;
373
374 if (mutt_regexlist_match(&g->rs, s))
375 return true;
376 struct Address *a = NULL;
377 TAILQ_FOREACH(a, &g->al, entries)
378 {
379 if (a->mailbox && mutt_istr_equal(s, a->mailbox))
380 return true;
381 }
382
383 return false;
384}
bool mutt_regexlist_match(struct RegexList *rl, const char *str)
Does a string match any Regex in the list?
Definition: regex.c:195
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition: string.c:819
+ 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.