]> jfr.im git - solanum.git/blob - src/capability.c
update NEWS
[solanum.git] / src / capability.c
1 /*
2 * Copyright (c) 2012 William Pitcock <nenolod@dereferenced.org>.
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice is present in all copies.
7 *
8 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
9 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
11 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
12 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
13 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
14 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
15 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
16 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
17 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
18 * POSSIBILITY OF SUCH DAMAGE.
19 */
20
21 #include "stdinc.h"
22 #include "capability.h"
23 #include "irc_dictionary.h"
24 #include "s_assert.h"
25
26 static rb_dlink_list capability_indexes = { NULL, NULL, 0 };
27
28 struct CapabilityIndex {
29 char *name;
30 struct Dictionary *cap_dict;
31 unsigned int highest_bit;
32 rb_dlink_node node;
33 };
34
35 #define CAP_ORPHANED 0x1
36 #define CAP_REQUIRED 0x2
37
38 struct CapabilityEntry {
39 char *cap;
40 unsigned int value;
41 unsigned int flags;
42 };
43
44 unsigned int
45 capability_get(struct CapabilityIndex *idx, const char *cap)
46 {
47 struct CapabilityEntry *entry;
48
49 s_assert(idx != NULL);
50 if (cap == NULL)
51 return 0;
52
53 entry = irc_dictionary_retrieve(idx->cap_dict, cap);
54 if (entry != NULL && !(entry->flags & CAP_ORPHANED))
55 return (1 << entry->value);
56
57 return 0;
58 }
59
60 unsigned int
61 capability_put(struct CapabilityIndex *idx, const char *cap)
62 {
63 struct CapabilityEntry *entry;
64
65 s_assert(idx != NULL);
66 if (!idx->highest_bit)
67 return 0xFFFFFFFF;
68
69 if ((entry = irc_dictionary_retrieve(idx->cap_dict, cap)) != NULL)
70 {
71 entry->flags &= ~CAP_ORPHANED;
72 return (1 << entry->value);
73 }
74
75 entry = rb_malloc(sizeof(struct CapabilityEntry));
76 entry->cap = rb_strdup(cap);
77 entry->flags = 0;
78 entry->value = idx->highest_bit;
79
80 irc_dictionary_add(idx->cap_dict, entry->cap, entry);
81
82 idx->highest_bit++;
83 if (idx->highest_bit % (sizeof(unsigned int) * 8) == 0)
84 idx->highest_bit = 0;
85
86 return (1 << entry->value);
87 }
88
89 unsigned int
90 capability_put_anonymous(struct CapabilityIndex *idx)
91 {
92 unsigned int value;
93
94 s_assert(idx != NULL);
95 if (!idx->highest_bit)
96 return 0xFFFFFFFF;
97 value = 1 << idx->highest_bit;
98 idx->highest_bit++;
99 if (idx->highest_bit % (sizeof(unsigned int) * 8) == 0)
100 idx->highest_bit = 0;
101 return value;
102 }
103
104 void
105 capability_orphan(struct CapabilityIndex *idx, const char *cap)
106 {
107 struct CapabilityEntry *entry;
108
109 s_assert(idx != NULL);
110
111 entry = irc_dictionary_retrieve(idx->cap_dict, cap);
112 if (entry != NULL)
113 {
114 entry->flags &= ~CAP_REQUIRED;
115 entry->flags |= CAP_ORPHANED;
116 }
117 }
118
119 void
120 capability_require(struct CapabilityIndex *idx, const char *cap)
121 {
122 struct CapabilityEntry *entry;
123
124 s_assert(idx != NULL);
125
126 entry = irc_dictionary_retrieve(idx->cap_dict, cap);
127 if (entry != NULL)
128 entry->flags |= CAP_REQUIRED;
129 }
130
131 static void
132 capability_destroy(struct DictionaryElement *delem, void *privdata)
133 {
134 s_assert(delem != NULL);
135
136 rb_free(delem->data);
137 }
138
139 struct CapabilityIndex *
140 capability_index_create(const char *name)
141 {
142 struct CapabilityIndex *idx;
143
144 idx = rb_malloc(sizeof(struct CapabilityIndex));
145 idx->name = rb_strdup(name);
146 idx->cap_dict = irc_dictionary_create(strcasecmp);
147 idx->highest_bit = 1;
148
149 rb_dlinkAdd(idx, &idx->node, &capability_indexes);
150
151 return idx;
152 }
153
154 void
155 capability_index_destroy(struct CapabilityIndex *idx)
156 {
157 s_assert(idx != NULL);
158
159 rb_dlinkDelete(&idx->node, &capability_indexes);
160
161 irc_dictionary_destroy(idx->cap_dict, capability_destroy, NULL);
162 rb_free(idx);
163 }
164
165 const char *
166 capability_index_list(struct CapabilityIndex *idx, unsigned int cap_mask)
167 {
168 struct DictionaryIter iter;
169 struct CapabilityEntry *entry;
170 static char buf[BUFSIZE];
171 char *t = buf;
172 int tl;
173
174 s_assert(idx != NULL);
175
176 *t = '\0';
177
178 DICTIONARY_FOREACH(entry, &iter, idx->cap_dict)
179 {
180 if ((1 << entry->value) & cap_mask)
181 {
182 tl = rb_sprintf(t, "%s ", entry->cap);
183 t += tl;
184 }
185 }
186
187 t--;
188 *t = '\0';
189
190 return buf;
191 }
192
193 unsigned int
194 capability_index_mask(struct CapabilityIndex *idx)
195 {
196 struct DictionaryIter iter;
197 struct CapabilityEntry *entry;
198 unsigned int mask = 0;
199
200 s_assert(idx != NULL);
201
202 DICTIONARY_FOREACH(entry, &iter, idx->cap_dict)
203 {
204 if (!(entry->flags & CAP_ORPHANED))
205 mask |= (1 << entry->value);
206 }
207
208 return mask;
209 }
210
211 unsigned int
212 capability_index_get_required(struct CapabilityIndex *idx)
213 {
214 struct DictionaryIter iter;
215 struct CapabilityEntry *entry;
216 unsigned int mask = 0;
217
218 s_assert(idx != NULL);
219
220 DICTIONARY_FOREACH(entry, &iter, idx->cap_dict)
221 {
222 if (!(entry->flags & CAP_ORPHANED) && (entry->flags & CAP_REQUIRED))
223 mask |= (1 << entry->value);
224 }
225
226 return mask;
227 }
228
229 void
230 capability_index_stats(void (*cb)(const char *line, void *privdata), void *privdata)
231 {
232 rb_dlink_node *node;
233 char buf[BUFSIZE];
234
235 RB_DLINK_FOREACH(node, capability_indexes.head)
236 {
237 struct CapabilityIndex *idx = node->data;
238 struct DictionaryIter iter;
239 struct CapabilityEntry *entry;
240
241 rb_snprintf(buf, sizeof buf, "'%s': allocated bits - %d", idx->name, (idx->highest_bit - 1));
242 cb(buf, privdata);
243
244 DICTIONARY_FOREACH(entry, &iter, idx->cap_dict)
245 {
246 rb_snprintf(buf, sizeof buf, "bit %d: '%s'", entry->value, entry->cap);
247 cb(buf, privdata);
248 }
249
250 rb_snprintf(buf, sizeof buf, "'%s': remaining bits - %u", idx->name,
251 (unsigned int)((sizeof(unsigned int) * 8) - (idx->highest_bit - 1)));
252 cb(buf, privdata);
253 }
254
255 rb_snprintf(buf, sizeof buf, "%ld capability indexes", rb_dlink_list_length(&capability_indexes));
256 cb(buf, privdata);
257 }