NeoMutt  2025-12-11-980-ge38c27
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:1475
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
void mutt_regexlist_free(struct RegexList *rl)
Free a RegexList object.
Definition regex.c:179
A set of email addresses.
Definition group.h:36
char * name
Name of Group.
Definition group.h:39
struct AddressList al
List of Addresses.
Definition group.h:37
struct RegexList rs
Group Regex patterns.
Definition group.h:38
+ 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:52
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition string.c:257
#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:430
+ 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;
121 struct Address *tmp = NULL;
122 TAILQ_FOREACH_SAFE(a, &al_new, entries, tmp)
123 {
124 TAILQ_REMOVE(&al_new, a, entries);
125 mutt_addrlist_append(&g->al, a);
126 }
127 ASSERT(TAILQ_EMPTY(&al_new));
128}
void mutt_addrlist_copy(struct AddressList *dst, const struct AddressList *src, bool prune)
Copy a list of addresses into another list.
Definition address.c:776
void mutt_addrlist_append(struct AddressList *al, struct Address *a)
Append an Address to an AddressList.
Definition address.c:1496
void mutt_addrlist_remove_xrefs(const struct AddressList *a, struct AddressList *b)
Remove cross-references.
Definition address.c:1446
#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:59
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 139 of file group.c.

140{
141 return mutt_regexlist_add(&g->rs, str, flags, err);
142}
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:140
+ 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 151 of file group.c.

152{
153 return mutt_regexlist_remove(&g->rs, str);
154}
int mutt_regexlist_remove(struct RegexList *rl, const char *str)
Remove a Regex from a list.
Definition regex.c:236
+ 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 162 of file group.c.

163{
164 if (!g || !str)
165 return false;
166
167 if (mutt_regexlist_match(&g->rs, str))
168 return true;
169 struct Address *a = NULL;
170 TAILQ_FOREACH(a, &g->al, entries)
171 {
172 if (a->mailbox && mutt_istr_equal(str, buf_string(a->mailbox)))
173 return true;
174 }
175
176 return false;
177}
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:201
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition string.c:678
#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 184 of file group.c.

185{
186 if (!gl || !g)
187 return;
188
189 struct GroupNode *np = NULL;
190 STAILQ_FOREACH(np, gl, entries)
191 {
192 if (np->group == g)
193 return;
194 }
195 np = MUTT_MEM_CALLOC(1, struct GroupNode);
196 np->group = g;
197 STAILQ_INSERT_TAIL(gl, np, entries);
198}
#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:46
struct Group * group
Address Group.
Definition group.h:47
+ 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 204 of file group.c.

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

226{
227 if (!gl || !al)
228 return;
229
230 struct GroupNode *np = NULL;
231 STAILQ_FOREACH(np, gl, entries)
232 {
233 group_add_addrlist(np->group, al);
234 }
235}
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 246 of file group.c.

248{
249 if (!gl || !str)
250 return -1;
251
252 int rc = 0;
253
254 struct GroupNode *np = NULL;
255 STAILQ_FOREACH(np, gl, entries)
256 {
257 rc = group_add_regex(np->group, str, flags, err);
258 if (rc)
259 return rc;
260 }
261 return rc;
262}
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:139
+ 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 268 of file group.c.

269{
270 struct HashTable *groups = mutt_hash_new(1031, MUTT_HASH_NONE);
271
273
274 return groups;
275}
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:262
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:304
@ MUTT_HASH_NONE
No flags are set.
Definition hash.h:115
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 281 of file group.c.

282{
283 mutt_hash_free(pptr);
284}
void mutt_hash_free(struct HashTable **ptr)
Free a hash table.
Definition hash.c:460
+ 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 292 of file group.c.

293{
294 if (!groups || !name)
295 return NULL;
296
297 struct Group *g = mutt_hash_find(groups, name);
298 if (!g)
299 {
300 mutt_debug(LL_DEBUG2, "Creating group %s\n", name);
301 g = group_new(name);
302 mutt_hash_insert(groups, g->name, g);
303 }
304
305 return g;
306}
static struct Group * group_new(const char *name)
Create a new Address Group.
Definition group.c:63
#define mutt_debug(LEVEL,...)
Definition logging2.h:91
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:338
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:365
@ LL_DEBUG2
Log at debug level 2.
Definition logging2.h:46
+ 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 313 of file group.c.

314{
315 if (!groups || !gl)
316 return;
317
318 struct GroupNode *np = STAILQ_FIRST(gl);
319 struct GroupNode *next = NULL;
320 while (np)
321 {
322 group_remove(groups, np->group);
323 next = STAILQ_NEXT(np, entries);
324 FREE(&np);
325 np = next;
326 }
327 STAILQ_INIT(gl);
328}
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 338 of file group.c.

340{
341 if (!groups || !gl || !al)
342 return -1;
343
344 struct GroupNode *gnp = NULL;
345 STAILQ_FOREACH(gnp, gl, entries)
346 {
347 struct Address *a = NULL;
348 TAILQ_FOREACH(a, al, entries)
349 {
351 }
352 if (group_is_empty(gnp->group))
353 {
354 group_remove(groups, gnp->group);
355 }
356 }
357
358 return 0;
359}
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 369 of file group.c.

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