mutt stable branch with some hacks
1/*
2 * Copyright (C) 2006 Thomas Roessler <roessler@does-not-exist.org>
3 * Copyright (C) 2009 Rocco Rutte <pdmef@gmx.net>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20#if HAVE_CONFIG_H
21# include "config.h"
22#endif
23
24#include "mutt.h"
25#include "mutt_curses.h"
26#include "mutt_regex.h"
27#include "mbyte.h"
28#include "charset.h"
29
30#include <ctype.h>
31#include <stdlib.h>
32#include <unistd.h>
33#include <string.h>
34#include <sys/utsname.h>
35#include <errno.h>
36#include <sys/wait.h>
37
38group_t *mutt_pattern_group (const char *k)
39{
40 group_t *p;
41
42 if (!k)
43 return 0;
44
45 if (!(p = hash_find (Groups, k)))
46 {
47 dprint (2, (debugfile, "mutt_pattern_group: Creating group %s.\n", k));
48 p = safe_calloc (1, sizeof (group_t));
49 p->name = safe_strdup (k);
50 hash_insert (Groups, p->name, p);
51 }
52
53 return p;
54}
55
56static void mutt_group_remove (group_t *g)
57{
58 if (!g)
59 return;
60 hash_delete (Groups, g->name, g, NULL);
61 rfc822_free_address (&g->as);
62 mutt_free_rx_list (&g->rs);
63 FREE(&g->name);
64 FREE(&g);
65}
66
67int mutt_group_context_clear (group_context_t **ctx)
68{
69 group_context_t *t;
70 for ( ; ctx && *ctx; (*ctx) = t)
71 {
72 mutt_group_remove ((*ctx)->g);
73 t = (*ctx)->next;
74 FREE(ctx); /* __FREE_CHECKED__ */
75 }
76 return 0;
77}
78
79static int empty_group (group_t *g)
80{
81 if (!g)
82 return -1;
83 return !g->as && !g->rs;
84}
85
86void mutt_group_context_add (group_context_t **ctx, group_t *group)
87{
88 for (; *ctx; ctx = &((*ctx)->next))
89 {
90 if ((*ctx)->g == group)
91 return;
92 }
93
94 *ctx = safe_calloc (1, sizeof (group_context_t));
95 (*ctx)->g = group;
96}
97
98void mutt_group_context_destroy (group_context_t **ctx)
99{
100 group_context_t *p;
101 for (; *ctx; *ctx = p)
102 {
103 p = (*ctx)->next;
104 FREE (ctx); /* __FREE_CHECKED__ */
105 }
106}
107
108void mutt_group_add_adrlist (group_t *g, ADDRESS *a)
109{
110 ADDRESS **p, *q;
111
112 if (!g)
113 return;
114 if (!a)
115 return;
116
117 for (p = &g->as; *p; p = &((*p)->next))
118 ;
119
120 q = rfc822_cpy_adr (a, 0);
121 q = mutt_remove_xrefs (g->as, q);
122 *p = q;
123}
124
125static int mutt_group_remove_adrlist (group_t *g, ADDRESS *a)
126{
127 ADDRESS *p;
128
129 if (!g)
130 return -1;
131 if (!a)
132 return -1;
133
134 for (p = a; p; p = p->next)
135 rfc822_remove_from_adrlist (&g->as, p->mailbox);
136
137 return 0;
138}
139
140static int mutt_group_add_rx (group_t *g, const char *s, int flags, BUFFER *err)
141{
142 return mutt_add_to_rx_list (&g->rs, s, flags, err);
143}
144
145static int mutt_group_remove_rx (group_t *g, const char *s)
146{
147 return mutt_remove_from_rx_list (&g->rs, s);
148}
149
150void mutt_group_context_add_adrlist (group_context_t *ctx, ADDRESS *a)
151{
152 for (; ctx; ctx = ctx->next)
153 mutt_group_add_adrlist (ctx->g, a);
154}
155
156int mutt_group_context_remove_adrlist (group_context_t *ctx, ADDRESS * a)
157{
158 int rv = 0;
159
160 for (; (!rv) && ctx; ctx = ctx->next)
161 {
162 rv = mutt_group_remove_adrlist (ctx->g, a);
163 if (empty_group (ctx->g))
164 mutt_group_remove (ctx->g);
165 }
166
167 return rv;
168}
169
170int mutt_group_context_add_rx (group_context_t *ctx, const char *s, int flags, BUFFER *err)
171{
172 int rv = 0;
173
174 for (; (!rv) && ctx; ctx = ctx->next)
175 rv = mutt_group_add_rx (ctx->g, s, flags, err);
176
177 return rv;
178}
179
180int mutt_group_context_remove_rx (group_context_t *ctx, const char *s)
181{
182 int rv = 0;
183
184 for (; (!rv) && ctx; ctx = ctx->next)
185 {
186 rv = mutt_group_remove_rx (ctx->g, s);
187 if (empty_group (ctx->g))
188 mutt_group_remove (ctx->g);
189 }
190
191 return rv;
192}
193
194int mutt_group_match (group_t *g, const char *s)
195{
196 ADDRESS *ap;
197
198 if (s && g)
199 {
200 if (mutt_match_rx_list (s, g->rs))
201 return 1;
202 for (ap = g->as; ap; ap = ap->next)
203 if (ap->mailbox && !mutt_strcasecmp (s, ap->mailbox))
204 return 1;
205 }
206 return 0;
207}