NeoMutt  2025-12-11-58-g09398d
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
group.c
Go to the documentation of this file.
1
25
31
32#include "config.h"
33#include <stdbool.h>
34#include <stdlib.h>
35#include "group.h"
36#include "address.h"
37
42static void group_free(struct Group **ptr)
43{
44 if (!ptr || !*ptr)
45 return;
46
47 struct Group *g = *ptr;
48
51 FREE(&g->name);
52
53 FREE(ptr);
54}
55
63static struct Group *group_new(const char *name)
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}
73
77static void group_hash_free(int type, void *obj, intptr_t data)
78{
79 struct Group *g = obj;
80 group_free(&g);
81}
82
88static void group_remove(struct HashTable *groups, struct Group *g)
89{
90 if (!groups || !g)
91 return;
92 mutt_hash_delete(groups, g->name, g);
93}
94
100static bool group_is_empty(struct Group *g)
101{
102 if (!g)
103 return true;
104 return TAILQ_EMPTY(&g->al) && STAILQ_EMPTY(&g->rs);
105}
106
112static void group_add_addrlist(struct Group *g, const struct AddressList *al)
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}
128
138static int group_add_regex(struct Group *g, const char *str, uint16_t flags, struct Buffer *err)
139{
140 return mutt_regexlist_add(&g->rs, str, flags, err);
141}
142
150static int group_remove_regex(struct Group *g, const char *str)
151{
152 return mutt_regexlist_remove(&g->rs, str);
153}
154
161bool group_match(struct Group *g, const char *str)
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}
177
183void grouplist_add_group(struct GroupList *gl, struct Group *g)
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}
198
203void grouplist_destroy(struct GroupList *gl)
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}
218
224void grouplist_add_addrlist(struct GroupList *gl, struct AddressList *al)
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}
235
245int grouplist_add_regex(struct GroupList *gl, const char *str, uint16_t flags,
246 struct Buffer *err)
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}
262
268{
269 struct HashTable *groups = mutt_hash_new(1031, MUTT_HASH_NO_FLAGS);
270
272
273 return groups;
274}
275
280void groups_free(struct HashTable **pptr)
281{
282 mutt_hash_free(pptr);
283}
284
291struct Group *groups_get_group(struct HashTable *groups, const char *name)
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}
306
312void groups_remove_grouplist(struct HashTable *groups, struct GroupList *gl)
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}
328
337int groups_remove_addrlist(struct HashTable *groups, struct GroupList *gl,
338 struct AddressList *al)
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}
359
368int groups_remove_regex(struct HashTable *groups, struct GroupList *gl, const char *str)
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}
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_clear(struct AddressList *al)
Unlink and free all Address in an AddressList.
Definition address.c:1460
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
int mutt_addrlist_remove(struct AddressList *al, const char *mailbox)
Remove an Address from a list.
Definition address.c:435
Representation of an email address.
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition buffer.h:96
int grouplist_add_regex(struct GroupList *gl, const char *str, uint16_t flags, struct Buffer *err)
Add matching Addresses to a GroupList.
Definition group.c:245
int groups_remove_addrlist(struct HashTable *groups, struct GroupList *gl, struct AddressList *al)
Remove an AddressList from a GroupList.
Definition group.c:337
bool group_match(struct Group *g, const char *str)
Does a string match an entry in a Group?
Definition group.c:161
void groups_remove_grouplist(struct HashTable *groups, struct GroupList *gl)
Clear a GroupList.
Definition group.c:312
int groups_remove_regex(struct HashTable *groups, struct GroupList *gl, const char *str)
Remove matching addresses from a GroupList.
Definition group.c:368
struct Group * groups_get_group(struct HashTable *groups, const char *name)
Get a Group by its name.
Definition group.c:291
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
static void group_add_addrlist(struct Group *g, const struct AddressList *al)
Add an Address List to a Group.
Definition group.c:112
void grouplist_add_addrlist(struct GroupList *gl, struct AddressList *al)
Add Address list to a GroupList.
Definition group.c:224
static void group_free(struct Group **ptr)
Free an Address Group.
Definition group.c:42
static int group_remove_regex(struct Group *g, const char *str)
Remove a Regex from a Group.
Definition group.c:150
static bool group_is_empty(struct Group *g)
Is a Group empty?
Definition group.c:100
static struct Group * group_new(const char *name)
Create a new Address Group.
Definition group.c:63
static void group_remove(struct HashTable *groups, struct Group *g)
Remove a Group from the Hash Table.
Definition group.c:88
void grouplist_add_group(struct GroupList *gl, struct Group *g)
Add a Group to a GroupList.
Definition group.c:183
struct HashTable * groups_new(void)
Create a HashTable for the Address Groups.
Definition group.c:267
void groups_free(struct HashTable **pptr)
Free Address Groups HashTable.
Definition group.c:280
void grouplist_destroy(struct GroupList *gl)
Free a GroupList.
Definition group.c:203
Handling for email address groups.
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
#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_delete(struct HashTable *table, const char *strkey, const void *data)
Remove an element from a Hash Table.
Definition hash.c:427
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
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
void mutt_hash_free(struct HashTable **ptr)
Free a hash table.
Definition hash.c:457
#define MUTT_HASH_NO_FLAGS
No flags are set.
Definition hash.h:111
@ LL_DEBUG2
Log at debug level 2.
Definition logging2.h:45
#define FREE(x)
Definition memory.h:62
#define MUTT_MEM_CALLOC(n, type)
Definition memory.h:47
void mutt_regexlist_free(struct RegexList *rl)
Free a RegexList object.
Definition regex.c:178
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
int mutt_regexlist_remove(struct RegexList *rl, const char *str)
Remove a Regex from a list.
Definition regex.c:234
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
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition string.c:255
#define TAILQ_FOREACH(var, head, field)
Definition queue.h:782
#define TAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition queue.h:792
#define STAILQ_INIT(head)
Definition queue.h:410
#define STAILQ_FIRST(head)
Definition queue.h:388
#define TAILQ_INIT(head)
Definition queue.h:822
#define STAILQ_FOREACH(var, head, field)
Definition queue.h:390
#define STAILQ_INSERT_TAIL(head, elm, field)
Definition queue.h:427
#define STAILQ_EMPTY(head)
Definition queue.h:382
#define TAILQ_REMOVE(head, elm, field)
Definition queue.h:901
#define TAILQ_HEAD_INITIALIZER(head)
Definition queue.h:694
#define TAILQ_EMPTY(head)
Definition queue.h:778
#define STAILQ_NEXT(elm, field)
Definition queue.h:439
#define ASSERT(COND)
Definition signal2.h:60
An email address.
Definition address.h:35
struct Buffer * mailbox
Mailbox and host address.
Definition address.h:37
String manipulation buffer.
Definition buffer.h:36
An element in a GroupList.
Definition group.h:49
struct Group * group
Address Group.
Definition group.h:50
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
A Hash Table.
Definition hash.h:99