forked from neomutt/neomutt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
group.c
295 lines (261 loc) · 6.56 KB
/
group.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/**
* @file
* Handling for email address groups
*
* @authors
* Copyright (C) 2006 Thomas Roessler <[email protected]>
* Copyright (C) 2009 Rocco Rutte <[email protected]>
*
* @copyright
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 2 of the License, or (at your option) any later
* version.
*
* 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/>.
*/
#include "config.h"
#include <stdbool.h>
#include <stdlib.h>
#include "mutt/mutt.h"
#include "email/lib.h"
#include "group.h"
#include "globals.h"
#include "send.h"
/**
* mutt_pattern_group - Match a pattern to a Group
* @param k Pattern to match
* @retval ptr Matching Group
* @retval ptr Newly created Group (if no match)
*/
struct Group *mutt_pattern_group(const char *k)
{
struct Group *p = NULL;
if (!k)
return 0;
p = mutt_hash_find(Groups, k);
if (!p)
{
mutt_debug(2, "Creating group %s.\n", k);
p = mutt_mem_calloc(1, sizeof(struct Group));
p->name = mutt_str_strdup(k);
STAILQ_INIT(&p->rs);
mutt_hash_insert(Groups, p->name, p);
}
return p;
}
/**
* group_remove - Remove a Group from the Hash Table
* @param g Group to remove
*/
static void group_remove(struct Group *g)
{
if (!g)
return;
mutt_hash_delete(Groups, g->name, g);
mutt_addr_free(&g->as);
mutt_regexlist_free(&g->rs);
FREE(&g->name);
FREE(&g);
}
/**
* mutt_group_context_clear - Empty a Group List
* @param ctx Group List to modify
* @retval 0 Always
*/
int mutt_group_context_clear(struct GroupContext **ctx)
{
struct GroupContext *t = NULL;
for (; ctx && *ctx; (*ctx) = t)
{
group_remove((*ctx)->g);
t = (*ctx)->next;
FREE(ctx);
}
return 0;
}
/**
* empty_group - Is a Group empty?
* @param g Group to test
* @retval true If the Group is empty
*/
static bool empty_group(struct Group *g)
{
if (!g)
return true;
return !g->as && STAILQ_EMPTY(&g->rs);
}
/**
* mutt_group_context_add - Add a Group to a List
* @param ctx Group List
* @param group Group to add
*/
void mutt_group_context_add(struct GroupContext **ctx, struct Group *group)
{
for (; *ctx; ctx = &((*ctx)->next))
{
if ((*ctx)->g == group)
return;
}
*ctx = mutt_mem_calloc(1, sizeof(struct GroupContext));
(*ctx)->g = group;
}
/**
* mutt_group_context_destroy - Destroy a Group List
* @param ctx Group List to destroy
*/
void mutt_group_context_destroy(struct GroupContext **ctx)
{
struct GroupContext *p = NULL;
for (; *ctx; *ctx = p)
{
p = (*ctx)->next;
FREE(ctx);
}
}
/**
* group_add_addrlist - Add an Address List to a Group
* @param g Group to add to
* @param a Address List
*/
static void group_add_addrlist(struct Group *g, struct Address *a)
{
struct Address **p = NULL, *q = NULL;
if (!g)
return;
if (!a)
return;
for (p = &g->as; *p; p = &((*p)->next))
;
q = mutt_addr_copy_list(a, false);
q = mutt_remove_xrefs(g->as, q);
*p = q;
}
/**
* group_remove_addrlist - Remove an Address List from a Group
* @param g Group to modify
* @param a Address List to remove
* @retval 0 Success
* @retval -1 Error
*/
static int group_remove_addrlist(struct Group *g, struct Address *a)
{
struct Address *p = NULL;
if (!g)
return -1;
if (!a)
return -1;
for (p = a; p; p = p->next)
mutt_addr_remove_from_list(&g->as, p->mailbox);
return 0;
}
/**
* group_add_regex - Add a Regex to a Group
* @param g Group to add to
* @param s Regex string to add
* @param flags Flags, e.g. REG_ICASE
* @param err Buffer for error message
* @retval 0 Success
* @retval -1 Error
*/
static int group_add_regex(struct Group *g, const char *s, int flags, struct Buffer *err)
{
return mutt_regexlist_add(&g->rs, s, flags, err);
}
/**
* group_remove_regex - Remove a Regex from a Group
* @param g Group to modify
* @param s Regex string to match
* @retval 0 Success
* @retval -1 Error
*/
static int group_remove_regex(struct Group *g, const char *s)
{
return mutt_regexlist_remove(&g->rs, s);
}
/**
* mutt_group_context_add_addrlist - Add an Address List to a Group List
* @param ctx Group List to add to
* @param a Address List to add
*/
void mutt_group_context_add_addrlist(struct GroupContext *ctx, struct Address *a)
{
for (; ctx; ctx = ctx->next)
group_add_addrlist(ctx->g, a);
}
/**
* mutt_group_context_remove_addrlist - Remove an Address List from a Group List
* @param ctx Group List to modify
* @param a Address List to remove
* @retval 0 Success
* @retval -1 Error
*/
int mutt_group_context_remove_addrlist(struct GroupContext *ctx, struct Address *a)
{
int rc = 0;
for (; (!rc) && ctx; ctx = ctx->next)
{
rc = group_remove_addrlist(ctx->g, a);
if (empty_group(ctx->g))
group_remove(ctx->g);
}
return rc;
}
/**
* mutt_group_context_add_regex - Add a Regex to a Group List
* @param ctx Group List to add to
* @param s Regex string to add
* @param flags Flags, e.g. REG_ICASE
* @param err Buffer for error message
* @retval 0 Success
* @retval -1 Error
*/
int mutt_group_context_add_regex(struct GroupContext *ctx, const char *s,
int flags, struct Buffer *err)
{
int rc = 0;
for (; (!rc) && ctx; ctx = ctx->next)
rc = group_add_regex(ctx->g, s, flags, err);
return rc;
}
/**
* mutt_group_context_remove_regex - Remove a Regex from a Group List
* @param ctx Group List to modify
* @param s Regex string to remove
* @retval 0 Success
* @retval -1 Error
*/
int mutt_group_context_remove_regex(struct GroupContext *ctx, const char *s)
{
int rc = 0;
for (; (!rc) && ctx; ctx = ctx->next)
{
rc = group_remove_regex(ctx->g, s);
if (empty_group(ctx->g))
group_remove(ctx->g);
}
return rc;
}
/**
* mutt_group_match - Does a string match an entry in a Group?
* @param g Group to match against
* @param s String to match
* @retval true If there's a match
*/
bool mutt_group_match(struct Group *g, const char *s)
{
if (!g || !s)
return false;
if (mutt_regexlist_match(&g->rs, s))
return true;
for (struct Address *ap = g->as; ap; ap = ap->next)
if (ap->mailbox && (mutt_str_strcasecmp(s, ap->mailbox) == 0))
return true;
return false;
}