NeoMutt  2024-12-12-14-g7b49f7
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
group.c
Go to the documentation of this file.
1
32#include "config.h"
33#include <stdbool.h>
34#include <stdlib.h>
35#include "group.h"
36#include "address.h"
37
43static struct HashTable *Groups = NULL;
44
49static void group_free(struct Group **ptr)
50{
51 if (!ptr || !*ptr)
52 return;
53
54 struct Group *g = *ptr;
55
58 FREE(&g->name);
59
60 FREE(ptr);
61}
62
70static struct Group *group_new(const char *pat)
71{
72 struct Group *g = MUTT_MEM_CALLOC(1, struct Group);
73
74 g->name = mutt_str_dup(pat);
75 STAILQ_INIT(&g->rs);
76 TAILQ_INIT(&g->al);
77
78 return g;
79}
80
84static void group_hash_free(int type, void *obj, intptr_t data)
85{
86 struct Group *g = obj;
87 group_free(&g);
88}
89
96{
98
100}
101
108{
110}
111
117struct Group *mutt_pattern_group(const char *pat)
118{
119 if (!pat)
120 return NULL;
121
122 struct Group *g = mutt_hash_find(Groups, pat);
123 if (!g)
124 {
125 mutt_debug(LL_DEBUG2, "Creating group %s\n", pat);
126 g = group_new(pat);
128 }
129
130 return g;
131}
132
137static void group_remove(struct Group *g)
138{
139 if (!g)
140 return;
142}
143
148void mutt_grouplist_clear(struct GroupList *gl)
149{
150 if (!gl)
151 return;
152
153 struct GroupNode *np = STAILQ_FIRST(gl);
154 struct GroupNode *next = NULL;
155 while (np)
156 {
157 group_remove(np->group);
158 next = STAILQ_NEXT(np, entries);
159 FREE(&np);
160 np = next;
161 }
162 STAILQ_INIT(gl);
163}
164
170static bool empty_group(struct Group *g)
171{
172 if (!g)
173 return true;
174 return TAILQ_EMPTY(&g->al) && STAILQ_EMPTY(&g->rs);
175}
176
182void mutt_grouplist_add(struct GroupList *gl, struct Group *group)
183{
184 if (!gl || !group)
185 return;
186
187 struct GroupNode *np = NULL;
188 STAILQ_FOREACH(np, gl, entries)
189 {
190 if (np->group == group)
191 return;
192 }
193 np = MUTT_MEM_CALLOC(1, struct GroupNode);
194 np->group = group;
195 STAILQ_INSERT_TAIL(gl, np, entries);
196}
197
202void mutt_grouplist_destroy(struct GroupList *gl)
203{
204 if (!gl)
205 return;
206
207 struct GroupNode *np = STAILQ_FIRST(gl);
208 struct GroupNode *next = NULL;
209 while (np)
210 {
211 next = STAILQ_NEXT(np, entries);
212 FREE(&np);
213 np = next;
214 }
215 STAILQ_INIT(gl);
216}
217
223static void group_add_addrlist(struct Group *g, const struct AddressList *al)
224{
225 if (!g || !al)
226 return;
227
228 struct AddressList al_new = TAILQ_HEAD_INITIALIZER(al_new);
229 mutt_addrlist_copy(&al_new, al, false);
230 mutt_addrlist_remove_xrefs(&g->al, &al_new);
231 struct Address *a = NULL, *tmp = NULL;
232 TAILQ_FOREACH_SAFE(a, &al_new, entries, tmp)
233 {
234 TAILQ_REMOVE(&al_new, a, entries);
235 mutt_addrlist_append(&g->al, a);
236 }
237 ASSERT(TAILQ_EMPTY(&al_new));
238}
239
249static int group_add_regex(struct Group *g, const char *s, uint16_t flags, struct Buffer *err)
250{
251 return mutt_regexlist_add(&g->rs, s, flags, err);
252}
253
261static int group_remove_regex(struct Group *g, const char *s)
262{
263 return mutt_regexlist_remove(&g->rs, s);
264}
265
271void mutt_grouplist_add_addrlist(struct GroupList *gl, struct AddressList *al)
272{
273 if (!gl || !al)
274 return;
275
276 struct GroupNode *np = NULL;
277 STAILQ_FOREACH(np, gl, entries)
278 {
279 group_add_addrlist(np->group, al);
280 }
281}
282
290int mutt_grouplist_remove_addrlist(struct GroupList *gl, struct AddressList *al)
291{
292 if (!gl || !al)
293 return -1;
294
295 struct GroupNode *gnp = NULL;
296 STAILQ_FOREACH(gnp, gl, entries)
297 {
298 struct Address *a = NULL;
299 TAILQ_FOREACH(a, al, entries)
300 {
302 }
303 if (empty_group(gnp->group))
304 {
305 group_remove(gnp->group);
306 }
307 }
308
309 return 0;
310}
311
321int mutt_grouplist_add_regex(struct GroupList *gl, const char *s,
322 uint16_t flags, struct Buffer *err)
323{
324 if (!gl || !s)
325 return -1;
326
327 int rc = 0;
328
329 struct GroupNode *np = NULL;
330 STAILQ_FOREACH(np, gl, entries)
331 {
332 rc = group_add_regex(np->group, s, flags, err);
333 if (rc)
334 return rc;
335 }
336 return rc;
337}
338
346int mutt_grouplist_remove_regex(struct GroupList *gl, const char *s)
347{
348 if (!gl || !s)
349 return -1;
350
351 int rc = 0;
352 struct GroupNode *np = NULL;
353 STAILQ_FOREACH(np, gl, entries)
354 {
355 rc = group_remove_regex(np->group, s);
356 if (empty_group(np->group))
357 group_remove(np->group);
358 if (rc)
359 return rc;
360 }
361 return rc;
362}
363
370bool mutt_group_match(struct Group *g, const char *s)
371{
372 if (!g || !s)
373 return false;
374
375 if (mutt_regexlist_match(&g->rs, s))
376 return true;
377 struct Address *a = NULL;
378 TAILQ_FOREACH(a, &g->al, entries)
379 {
380 if (a->mailbox && mutt_istr_equal(s, buf_string(a->mailbox)))
381 return true;
382 }
383
384 return false;
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: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
static bool empty_group(struct Group *g)
Is a Group empty?
Definition: group.c:170
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:249
int mutt_grouplist_remove_addrlist(struct GroupList *gl, struct AddressList *al)
Remove an AddressList from a GroupList.
Definition: group.c:290
void mutt_grouplist_add(struct GroupList *gl, struct Group *group)
Add a Group to a GroupList.
Definition: group.c:182
static void group_remove(struct Group *g)
Remove a Group from the Hash Table.
Definition: group.c:137
static void group_add_addrlist(struct Group *g, const struct AddressList *al)
Add an Address List to a Group.
Definition: group.c:223
static struct HashTable * Groups
Hash Table: "group-name" -> Group.
Definition: group.c:43
static void group_free(struct Group **ptr)
Free an Address Group.
Definition: group.c:49
bool mutt_group_match(struct Group *g, const char *s)
Does a string match an entry in a Group?
Definition: group.c:370
int mutt_grouplist_add_regex(struct GroupList *gl, const char *s, uint16_t flags, struct Buffer *err)
Add matching Addresses to a GroupList.
Definition: group.c:321
static struct Group * group_new(const char *pat)
Create a new Address Group.
Definition: group.c:70
static int group_remove_regex(struct Group *g, const char *s)
Remove a Regex from a Group.
Definition: group.c:261
struct Group * mutt_pattern_group(const char *pat)
Match a pattern to a Group.
Definition: group.c:117
int mutt_grouplist_remove_regex(struct GroupList *gl, const char *s)
Remove matching addresses from a GroupList.
Definition: group.c:346
void mutt_grouplist_init(void)
Initialize the GroupList singleton.
Definition: group.c:95
void mutt_grouplist_destroy(struct GroupList *gl)
Free a GroupList.
Definition: group.c:202
void mutt_grouplist_clear(struct GroupList *gl)
Clear a GroupList.
Definition: group.c:148
void mutt_grouplist_add_addrlist(struct GroupList *gl, struct AddressList *al)
Add Address list to a GroupList.
Definition: group.c:271
void mutt_grouplist_cleanup(void)
Free GroupList singleton resource.
Definition: group.c:107
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:84
#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_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:109
@ LL_DEBUG2
Log at debug level 2.
Definition: logging2.h:44
#define FREE(x)
Definition: memory.h:55
#define MUTT_MEM_CALLOC(n, type)
Definition: memory.h:40
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:235
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:672
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:253
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:743
#define TAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition: queue.h:753
#define STAILQ_INIT(head)
Definition: queue.h:372
#define STAILQ_FIRST(head)
Definition: queue.h:350
#define TAILQ_INIT(head)
Definition: queue.h:783
#define STAILQ_FOREACH(var, head, field)
Definition: queue.h:352
#define STAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:389
#define STAILQ_EMPTY(head)
Definition: queue.h:348
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:862
#define TAILQ_HEAD_INITIALIZER(head)
Definition: queue.h:655
#define TAILQ_EMPTY(head)
Definition: queue.h:739
#define STAILQ_NEXT(elm, field)
Definition: queue.h:400
#define ASSERT(COND)
Definition: signal2.h:58
An email address.
Definition: address.h:36
struct Buffer * mailbox
Mailbox and host address.
Definition: address.h:38
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:97