NeoMutt  2025-12-11-980-ge38c27
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;
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}
129
139static int group_add_regex(struct Group *g, const char *str, uint16_t flags, struct Buffer *err)
140{
141 return mutt_regexlist_add(&g->rs, str, flags, err);
142}
143
151static int group_remove_regex(struct Group *g, const char *str)
152{
153 return mutt_regexlist_remove(&g->rs, str);
154}
155
162bool group_match(struct Group *g, const char *str)
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}
178
184void grouplist_add_group(struct GroupList *gl, struct Group *g)
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}
199
204void grouplist_destroy(struct GroupList *gl)
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}
219
225void grouplist_add_addrlist(struct GroupList *gl, struct AddressList *al)
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}
236
246int grouplist_add_regex(struct GroupList *gl, const char *str, uint16_t flags,
247 struct Buffer *err)
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}
263
269{
270 struct HashTable *groups = mutt_hash_new(1031, MUTT_HASH_NONE);
271
273
274 return groups;
275}
276
281void groups_free(struct HashTable **pptr)
282{
283 mutt_hash_free(pptr);
284}
285
292struct Group *groups_get_group(struct HashTable *groups, const char *name)
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}
307
313void groups_remove_grouplist(struct HashTable *groups, struct GroupList *gl)
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}
329
338int groups_remove_addrlist(struct HashTable *groups, struct GroupList *gl,
339 struct AddressList *al)
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}
360
369int groups_remove_regex(struct HashTable *groups, struct GroupList *gl, const char *str)
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}
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_clear(struct AddressList *al)
Unlink and free all Address in an AddressList.
Definition address.c:1475
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
int mutt_addrlist_remove(struct AddressList *al, const char *mailbox)
Remove an Address from a list.
Definition address.c:435
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:246
int groups_remove_addrlist(struct HashTable *groups, struct GroupList *gl, struct AddressList *al)
Remove an AddressList from a GroupList.
Definition group.c:338
bool group_match(struct Group *g, const char *str)
Does a string match an entry in a Group?
Definition group.c:162
void groups_remove_grouplist(struct HashTable *groups, struct GroupList *gl)
Clear a GroupList.
Definition group.c:313
int groups_remove_regex(struct HashTable *groups, struct GroupList *gl, const char *str)
Remove matching addresses from a GroupList.
Definition group.c:369
struct Group * groups_get_group(struct HashTable *groups, const char *name)
Get a Group by its name.
Definition group.c:292
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
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:225
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:151
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:184
struct HashTable * groups_new(void)
Create a HashTable for the Address Groups.
Definition group.c:268
void groups_free(struct HashTable **pptr)
Free Address Groups HashTable.
Definition group.c:281
void grouplist_destroy(struct GroupList *gl)
Free a GroupList.
Definition group.c:204
Handling for email address groups.
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
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: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_delete(struct HashTable *table, const char *strkey, const void *data)
Remove an element from a Hash Table.
Definition hash.c:430
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
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
void mutt_hash_free(struct HashTable **ptr)
Free a hash table.
Definition hash.c:460
@ MUTT_HASH_NONE
No flags are set.
Definition hash.h:115
@ LL_DEBUG2
Log at debug level 2.
Definition logging2.h:46
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
#define MUTT_MEM_CALLOC(n, type)
Definition memory.h:52
void mutt_regexlist_free(struct RegexList *rl)
Free a RegexList object.
Definition regex.c:179
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
int mutt_regexlist_remove(struct RegexList *rl, const char *str)
Remove a Regex from a list.
Definition regex.c:236
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
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition string.c:257
#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:59
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:46
struct Group * group
Address Group.
Definition group.h:47
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
A Hash Table.
Definition hash.h:99