NeoMutt  2025-12-11-58-g09398d
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
group.c File Reference

Handling for email address groups. More...

#include "config.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

static void group_free (struct Group **ptr)
 Free an Address Group.
 
static struct Groupgroup_new (const char *name)
 Create a new Address Group.
 
static void group_hash_free (int type, void *obj, intptr_t data)
 Free our hash table data - Implements hash_hdata_free_t -.
 
static void group_remove (struct HashTable *groups, struct Group *g)
 Remove a Group from the Hash Table.
 
static bool group_is_empty (struct Group *g)
 Is a Group empty?
 
static void group_add_addrlist (struct Group *g, const struct AddressList *al)
 Add an Address List to a Group.
 
static int group_add_regex (struct Group *g, const char *str, uint16_t flags, struct Buffer *err)
 Add a Regex to a Group.
 
static int group_remove_regex (struct Group *g, const char *str)
 Remove a Regex from a Group.
 
bool group_match (struct Group *g, const char *str)
 Does a string match an entry in a Group?
 
void grouplist_add_group (struct GroupList *gl, struct Group *g)
 Add a Group to a GroupList.
 
void grouplist_destroy (struct GroupList *gl)
 Free a GroupList.
 
void grouplist_add_addrlist (struct GroupList *gl, struct AddressList *al)
 Add Address list to a GroupList.
 
int grouplist_add_regex (struct GroupList *gl, const char *str, uint16_t flags, struct Buffer *err)
 Add matching Addresses to a GroupList.
 
struct HashTablegroups_new (void)
 Create a HashTable for the Address Groups.
 
void groups_free (struct HashTable **pptr)
 Free Address Groups HashTable.
 
struct Groupgroups_get_group (struct HashTable *groups, const char *name)
 Get a Group by its name.
 
void groups_remove_grouplist (struct HashTable *groups, struct GroupList *gl)
 Clear a GroupList.
 
int groups_remove_addrlist (struct HashTable *groups, struct GroupList *gl, struct AddressList *al)
 Remove an AddressList from a GroupList.
 
int groups_remove_regex (struct HashTable *groups, struct GroupList *gl, const char *str)
 Remove matching addresses from a GroupList.
 

Detailed Description

Handling for email address groups.

Authors
  • Richard Russon
  • Bo Yu
  • Pietro Cerutti
  • Federico Kircheis

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()

static void group_free ( struct Group ** ptr)
static

Free an Address Group.

Parameters
ptrGroup to free

Definition at line 42 of file group.c.

43{
44 if (!ptr || !*ptr)
45 return;
46
47 struct Group *g = *ptr;
48
51 FREE(&g->name);
52
53 FREE(ptr);
54}
void mutt_addrlist_clear(struct AddressList *al)
Unlink and free all Address in an AddressList.
Definition address.c:1460
#define FREE(x)
Definition memory.h:62
void mutt_regexlist_free(struct RegexList *rl)
Free a RegexList object.
Definition regex.c:178
A set of email addresses.
Definition group.h:39
char * name
Name of Group.
Definition group.h:42
struct AddressList al
List of Addresses.
Definition group.h:40
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_new()

static struct Group * group_new ( const char * name)
static

Create a new Address Group.

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

Definition at line 63 of file group.c.

64{
65 struct Group *g = MUTT_MEM_CALLOC(1, struct Group);
66
68 STAILQ_INIT(&g->rs);
69 TAILQ_INIT(&g->al);
70
71 return g;
72}
#define MUTT_MEM_CALLOC(n, type)
Definition memory.h:47
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition string.c:255
#define STAILQ_INIT(head)
Definition queue.h:410
#define TAILQ_INIT(head)
Definition queue.h:822
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ group_remove()

static void group_remove ( struct HashTable * groups,
struct Group * g )
static

Remove a Group from the Hash Table.

Parameters
groupsGroups HashTable
gGroup to remove

Definition at line 88 of file group.c.

89{
90 if (!groups || !g)
91 return;
92 mutt_hash_delete(groups, g->name, g);
93}
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:

◆ group_is_empty()

static bool group_is_empty ( struct Group * g)
static

Is a Group empty?

Parameters
gGroup to test
Return values
trueThe Group is empty

Definition at line 100 of file group.c.

101{
102 if (!g)
103 return true;
104 return TAILQ_EMPTY(&g->al) && STAILQ_EMPTY(&g->rs);
105}
#define STAILQ_EMPTY(head)
Definition queue.h:382
#define TAILQ_EMPTY(head)
Definition queue.h:778
+ 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 112 of file group.c.

113{
114 if (!g || !al)
115 return;
116
117 struct AddressList al_new = TAILQ_HEAD_INITIALIZER(al_new);
118 mutt_addrlist_copy(&al_new, al, false);
119 mutt_addrlist_remove_xrefs(&g->al, &al_new);
120 struct Address *a = NULL, *tmp = NULL;
121 TAILQ_FOREACH_SAFE(a, &al_new, entries, tmp)
122 {
123 TAILQ_REMOVE(&al_new, a, entries);
124 mutt_addrlist_append(&g->al, a);
125 }
126 ASSERT(TAILQ_EMPTY(&al_new));
127}
void mutt_addrlist_copy(struct AddressList *dst, const struct AddressList *src, bool prune)
Copy a list of addresses into another list.
Definition address.c:765
void mutt_addrlist_append(struct AddressList *al, struct Address *a)
Append an Address to an AddressList.
Definition address.c:1480
void mutt_addrlist_remove_xrefs(const struct AddressList *a, struct AddressList *b)
Remove cross-references.
Definition address.c:1433
#define TAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition queue.h:792
#define TAILQ_REMOVE(head, elm, field)
Definition queue.h:901
#define TAILQ_HEAD_INITIALIZER(head)
Definition queue.h:694
#define ASSERT(COND)
Definition signal2.h:60
An email address.
Definition address.h:35
+ 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 * str,
uint16_t flags,
struct Buffer * err )
static

Add a Regex to a Group.

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

Definition at line 138 of file group.c.

139{
140 return mutt_regexlist_add(&g->rs, str, flags, err);
141}
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:139
+ 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 * str )
static

Remove a Regex from a Group.

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

Definition at line 150 of file group.c.

151{
152 return mutt_regexlist_remove(&g->rs, str);
153}
int mutt_regexlist_remove(struct RegexList *rl, const char *str)
Remove a Regex from a list.
Definition regex.c:234
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ group_match()

bool group_match ( struct Group * g,
const char * str )

Does a string match an entry in a Group?

Parameters
gGroup to match against
strString to match
Return values
trueThere's a match

Definition at line 161 of file group.c.

162{
163 if (!g || !str)
164 return false;
165
166 if (mutt_regexlist_match(&g->rs, str))
167 return true;
168 struct Address *a = NULL;
169 TAILQ_FOREACH(a, &g->al, entries)
170 {
171 if (a->mailbox && mutt_istr_equal(str, buf_string(a->mailbox)))
172 return true;
173 }
174
175 return false;
176}
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
bool mutt_regexlist_match(struct RegexList *rl, const char *str)
Does a string match any Regex in the list?
Definition regex.c:199
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition string.c:672
#define TAILQ_FOREACH(var, head, field)
Definition queue.h:782
struct Buffer * 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:

◆ grouplist_add_group()

void grouplist_add_group ( struct GroupList * gl,
struct Group * g )

Add a Group to a GroupList.

Parameters
glGroupList to add to
gGroup to add

Definition at line 183 of file group.c.

184{
185 if (!gl || !g)
186 return;
187
188 struct GroupNode *np = NULL;
189 STAILQ_FOREACH(np, gl, entries)
190 {
191 if (np->group == g)
192 return;
193 }
194 np = MUTT_MEM_CALLOC(1, struct GroupNode);
195 np->group = g;
196 STAILQ_INSERT_TAIL(gl, np, entries);
197}
#define STAILQ_FOREACH(var, head, field)
Definition queue.h:390
#define STAILQ_INSERT_TAIL(head, elm, field)
Definition queue.h:427
An element in a GroupList.
Definition group.h:49
struct Group * group
Address Group.
Definition group.h:50
+ Here is the caller graph for this function:

◆ grouplist_destroy()

void grouplist_destroy ( struct GroupList * gl)

Free a GroupList.

Parameters
glGroupList to free

Definition at line 203 of file group.c.

204{
205 if (!gl)
206 return;
207
208 struct GroupNode *np = STAILQ_FIRST(gl);
209 struct GroupNode *next = NULL;
210 while (np)
211 {
212 next = STAILQ_NEXT(np, entries);
213 FREE(&np);
214 np = next;
215 }
216 STAILQ_INIT(gl);
217}
#define STAILQ_FIRST(head)
Definition queue.h:388
#define STAILQ_NEXT(elm, field)
Definition queue.h:439
+ Here is the caller graph for this function:

◆ grouplist_add_addrlist()

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

225{
226 if (!gl || !al)
227 return;
228
229 struct GroupNode *np = NULL;
230 STAILQ_FOREACH(np, gl, entries)
231 {
232 group_add_addrlist(np->group, al);
233 }
234}
static void group_add_addrlist(struct Group *g, const struct AddressList *al)
Add an Address List to a Group.
Definition group.c:112
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ grouplist_add_regex()

int grouplist_add_regex ( struct GroupList * gl,
const char * str,
uint16_t flags,
struct Buffer * err )

Add matching Addresses to a GroupList.

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

Definition at line 245 of file group.c.

247{
248 if (!gl || !str)
249 return -1;
250
251 int rc = 0;
252
253 struct GroupNode *np = NULL;
254 STAILQ_FOREACH(np, gl, entries)
255 {
256 rc = group_add_regex(np->group, str, flags, err);
257 if (rc)
258 return rc;
259 }
260 return rc;
261}
static int group_add_regex(struct Group *g, const char *str, uint16_t flags, struct Buffer *err)
Add a Regex to a Group.
Definition group.c:138
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ groups_new()

struct HashTable * groups_new ( void )

Create a HashTable for the Address Groups.

Return values
ptrNew Groups HashTable

Definition at line 267 of file group.c.

268{
269 struct HashTable *groups = mutt_hash_new(1031, MUTT_HASH_NO_FLAGS);
270
272
273 return groups;
274}
static void group_hash_free(int type, void *obj, intptr_t data)
Free our hash table data - Implements hash_hdata_free_t -.
Definition group.c:77
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:111
A Hash Table.
Definition hash.h:99
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ groups_free()

void groups_free ( struct HashTable ** pptr)

Free Address Groups HashTable.

Parameters
pptrGroups HashTable to free

Definition at line 280 of file group.c.

281{
282 mutt_hash_free(pptr);
283}
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:

◆ groups_get_group()

struct Group * groups_get_group ( struct HashTable * groups,
const char * name )

Get a Group by its name.

Parameters
groupsGroups HashTable
nameName to find
Return values
ptrMatching Group, or new Group (if no match)

Definition at line 291 of file group.c.

292{
293 if (!groups || !name)
294 return NULL;
295
296 struct Group *g = mutt_hash_find(groups, name);
297 if (!g)
298 {
299 mutt_debug(LL_DEBUG2, "Creating group %s\n", name);
300 g = group_new(name);
301 mutt_hash_insert(groups, g->name, g);
302 }
303
304 return g;
305}
static struct Group * group_new(const char *name)
Create a new Address Group.
Definition group.c:63
#define mutt_debug(LEVEL,...)
Definition logging2.h:90
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 logging2.h:45
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ groups_remove_grouplist()

void groups_remove_grouplist ( struct HashTable * groups,
struct GroupList * gl )

Clear a GroupList.

Parameters
groupsGroups HashTable
glGroupList to clear

Definition at line 312 of file group.c.

313{
314 if (!groups || !gl)
315 return;
316
317 struct GroupNode *np = STAILQ_FIRST(gl);
318 struct GroupNode *next = NULL;
319 while (np)
320 {
321 group_remove(groups, np->group);
322 next = STAILQ_NEXT(np, entries);
323 FREE(&np);
324 np = next;
325 }
326 STAILQ_INIT(gl);
327}
static void group_remove(struct HashTable *groups, struct Group *g)
Remove a Group from the Hash Table.
Definition group.c:88
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ groups_remove_addrlist()

int groups_remove_addrlist ( struct HashTable * groups,
struct GroupList * gl,
struct AddressList * al )

Remove an AddressList from a GroupList.

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

Definition at line 337 of file group.c.

339{
340 if (!groups || !gl || !al)
341 return -1;
342
343 struct GroupNode *gnp = NULL;
344 STAILQ_FOREACH(gnp, gl, entries)
345 {
346 struct Address *a = NULL;
347 TAILQ_FOREACH(a, al, entries)
348 {
350 }
351 if (group_is_empty(gnp->group))
352 {
353 group_remove(groups, gnp->group);
354 }
355 }
356
357 return 0;
358}
int mutt_addrlist_remove(struct AddressList *al, const char *mailbox)
Remove an Address from a list.
Definition address.c:435
static bool group_is_empty(struct Group *g)
Is a Group empty?
Definition group.c:100
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ groups_remove_regex()

int groups_remove_regex ( struct HashTable * groups,
struct GroupList * gl,
const char * str )

Remove matching addresses from a GroupList.

Parameters
groupsGroups HashTable
glGroupList to remove from
strRegex string to match
Return values
0Success
-1Error

Definition at line 368 of file group.c.

369{
370 if (!groups || !gl || !str)
371 return -1;
372
373 int rc = 0;
374 struct GroupNode *np = NULL;
375 STAILQ_FOREACH(np, gl, entries)
376 {
377 rc = group_remove_regex(np->group, str);
378 if (group_is_empty(np->group))
379 group_remove(groups, np->group);
380 if (rc != 0)
381 return rc;
382 }
383 return rc;
384}
static int group_remove_regex(struct Group *g, const char *str)
Remove a Regex from a Group.
Definition group.c:150
+ Here is the call graph for this function:
+ Here is the caller graph for this function: