NeoMutt  2024-03-23-142-g2b2e76
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 <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

static void group_free (struct Group **ptr)
 Free an Address Group.
 
static struct Groupgroup_new (const char *pat)
 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 -.
 
void mutt_grouplist_init (void)
 Initialize the GroupList singleton.
 
void mutt_grouplist_cleanup (void)
 Free GroupList singleton resource.
 
struct Groupmutt_pattern_group (const char *pat)
 Match a pattern to a Group.
 
static void group_remove (struct Group *g)
 Remove a Group from the Hash Table.
 
void mutt_grouplist_clear (struct GroupList *gl)
 Clear a GroupList.
 
static bool empty_group (struct Group *g)
 Is a Group empty?
 
void mutt_grouplist_add (struct GroupList *gl, struct Group *group)
 Add a Group to a GroupList.
 
void mutt_grouplist_destroy (struct GroupList *gl)
 Free a GroupList.
 
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 *s, uint16_t flags, struct Buffer *err)
 Add a Regex to a Group.
 
static int group_remove_regex (struct Group *g, const char *s)
 Remove a Regex from a Group.
 
void mutt_grouplist_add_addrlist (struct GroupList *gl, struct AddressList *al)
 Add Address list to a GroupList.
 
int mutt_grouplist_remove_addrlist (struct GroupList *gl, struct AddressList *al)
 Remove an AddressList from a GroupList.
 
int mutt_grouplist_add_regex (struct GroupList *gl, const char *s, uint16_t flags, struct Buffer *err)
 Add matching Addresses to a GroupList.
 
int mutt_grouplist_remove_regex (struct GroupList *gl, const char *s)
 Remove matching addresses from a GroupList.
 
bool mutt_group_match (struct Group *g, const char *s)
 Does a string match an entry in a Group?
 

Variables

static struct HashTableGroups = NULL
 Hash Table: "group-name" -> Group.
 

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

51{
52 if (!ptr || !*ptr)
53 return;
54
55 struct Group *g = *ptr;
56
59 FREE(&g->name);
60
61 FREE(ptr);
62}
void mutt_addrlist_clear(struct AddressList *al)
Unlink and free all Address in an AddressList.
Definition: address.c:1464
#define FREE(x)
Definition: memory.h:45
void mutt_regexlist_free(struct RegexList *rl)
Free a RegexList object.
Definition: regex.c:179
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 *  pat)
static

Create a new Address Group.

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

Definition at line 71 of file group.c.

72{
73 struct Group *g = mutt_mem_calloc(1, sizeof(struct Group));
74
75 g->name = mutt_str_dup(pat);
76 STAILQ_INIT(&g->rs);
77 TAILQ_INIT(&g->al);
78
79 return g;
80}
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:253
#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 96 of file group.c.

97{
99
101}
static struct HashTable * Groups
Hash Table: "group-name" -> Group.
Definition: group.c:44
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:85
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_cleanup()

void mutt_grouplist_cleanup ( void  )

Free GroupList singleton resource.

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

Definition at line 108 of file group.c.

109{
111}
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 118 of file group.c.

119{
120 if (!pat)
121 return NULL;
122
123 struct Group *g = mutt_hash_find(Groups, pat);
124 if (!g)
125 {
126 mutt_debug(LL_DEBUG2, "Creating group %s\n", pat);
127 g = group_new(pat);
129 }
130
131 return g;
132}
static struct Group * group_new(const char *pat)
Create a new Address Group.
Definition: group.c:71
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
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:44
+ 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 138 of file group.c.

139{
140 if (!g)
141 return;
143}
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 149 of file group.c.

150{
151 if (!gl)
152 return;
153
154 struct GroupNode *np = STAILQ_FIRST(gl);
155 struct GroupNode *next = NULL;
156 while (np)
157 {
158 group_remove(np->group);
159 next = STAILQ_NEXT(np, entries);
160 FREE(&np);
161 np = next;
162 }
163 STAILQ_INIT(gl);
164}
static void group_remove(struct Group *g)
Remove a Group from the Hash Table.
Definition: group.c:138
#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:49
struct Group * group
Address Group.
Definition: group.h:50
+ 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 171 of file group.c.

172{
173 if (!g)
174 return true;
175 return TAILQ_EMPTY(&g->al) && STAILQ_EMPTY(&g->rs);
176}
#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 183 of file group.c.

184{
185 if (!gl || !group)
186 return;
187
188 struct GroupNode *np = NULL;
189 STAILQ_FOREACH(np, gl, entries)
190 {
191 if (np->group == group)
192 return;
193 }
194 np = mutt_mem_calloc(1, sizeof(struct GroupNode));
195 np->group = group;
196 STAILQ_INSERT_TAIL(gl, np, entries);
197}
#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 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}
+ 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 224 of file group.c.

225{
226 if (!g || !al)
227 return;
228
229 struct AddressList al_new = TAILQ_HEAD_INITIALIZER(al_new);
230 mutt_addrlist_copy(&al_new, al, false);
231 mutt_addrlist_remove_xrefs(&g->al, &al_new);
232 struct Address *a = NULL, *tmp = NULL;
233 TAILQ_FOREACH_SAFE(a, &al_new, entries, tmp)
234 {
235 TAILQ_REMOVE(&al_new, a, entries);
236 mutt_addrlist_append(&g->al, a);
237 }
238 assert(TAILQ_EMPTY(&al_new));
239}
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:1484
void mutt_addrlist_remove_xrefs(const struct AddressList *a, struct AddressList *b)
Remove cross-references.
Definition: address.c:1437
#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 250 of file group.c.

251{
252 return mutt_regexlist_add(&g->rs, s, flags, err);
253}
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 *  s 
)
static

Remove a Regex from a Group.

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

Definition at line 262 of file group.c.

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

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

292{
293 if (!gl || !al)
294 return -1;
295
296 struct GroupNode *gnp = NULL;
297 STAILQ_FOREACH(gnp, gl, entries)
298 {
299 struct Address *a = NULL;
300 TAILQ_FOREACH(a, al, entries)
301 {
303 }
304 if (empty_group(gnp->group))
305 {
306 group_remove(gnp->group);
307 }
308 }
309
310 return 0;
311}
int mutt_addrlist_remove(struct AddressList *al, const char *mailbox)
Remove an Address from a list.
Definition: address.c:435
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
static bool empty_group(struct Group *g)
Is a Group empty?
Definition: group.c:171
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:725
struct Buffer * 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 322 of file group.c.

324{
325 if (!gl || !s)
326 return -1;
327
328 int rc = 0;
329
330 struct GroupNode *np = NULL;
331 STAILQ_FOREACH(np, gl, entries)
332 {
333 rc = group_add_regex(np->group, s, flags, err);
334 if (rc)
335 return rc;
336 }
337 return rc;
338}
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:250
+ 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 347 of file group.c.

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

372{
373 if (!g || !s)
374 return false;
375
376 if (mutt_regexlist_match(&g->rs, s))
377 return true;
378 struct Address *a = NULL;
379 TAILQ_FOREACH(a, &g->al, entries)
380 {
381 if (a->mailbox && mutt_istr_equal(s, buf_string(a->mailbox)))
382 return true;
383 }
384
385 return false;
386}
bool mutt_regexlist_match(struct RegexList *rl, const char *str)
Does a string match any Regex in the list?
Definition: regex.c:200
bool mutt_istr_equal(const char *a, const char *b)
Compare two strings, ignoring case.
Definition: string.c:721
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ Groups

struct HashTable* Groups = NULL
static

Hash Table: "group-name" -> Group.

A set of all the Address Groups.

Definition at line 44 of file group.c.