]> jfr.im git - solanum.git/blame - src/capability.c
m_server: make sure required_caps is non-zero.
[solanum.git] / src / capability.c
CommitLineData
64b56afd
AC
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
25struct CapabilityIndex {
26 struct Dictionary *cap_dict;
27 unsigned int highest_bit;
28};
29
5058c8eb 30#define CAP_ORPHANED 0x1
f01f67f0 31#define CAP_REQUIRED 0x2
5058c8eb 32
64b56afd 33struct CapabilityEntry {
64b56afd 34 unsigned int value;
5058c8eb 35 unsigned int flags;
64b56afd
AC
36};
37
38unsigned int
39capability_get(struct CapabilityIndex *index, const char *cap)
40{
41 struct CapabilityEntry *entry;
42
43 s_assert(index != NULL);
44
45 entry = irc_dictionary_retrieve(index->cap_dict, cap);
5058c8eb 46 if (entry != NULL && !(entry->flags & CAP_ORPHANED))
64b56afd
AC
47 return entry->value;
48
49 return 0xFFFFFFFF;
50}
51
52unsigned int
53capability_put(struct CapabilityIndex *index, const char *cap)
54{
55 struct CapabilityEntry *entry;
56
57 s_assert(index != NULL);
58
59 if ((entry = irc_dictionary_retrieve(index->cap_dict, cap)) != NULL)
60 {
5058c8eb 61 entry->flags &= ~CAP_ORPHANED;
64b56afd
AC
62 return entry->value;
63 }
64
65 entry = rb_malloc(sizeof(struct CapabilityEntry));
5058c8eb 66 entry->flags = 0;
64b56afd
AC
67 entry->value = index->highest_bit;
68
69 irc_dictionary_add(index->cap_dict, cap, entry);
70
71 index->highest_bit <<= 1;
72
73 /* hmm... not sure what to do here, so i guess we will abort for now... --nenolod */
74 if (index->highest_bit == 0)
75 abort();
76
77 return entry->value;
78}
79
80void
81capability_orphan(struct CapabilityIndex *index, const char *cap)
82{
83 struct CapabilityEntry *entry;
84
85 s_assert(index != NULL);
86
87 entry = irc_dictionary_retrieve(index->cap_dict, cap);
88 if (entry != NULL)
f01f67f0
AC
89 {
90 entry->flags &= ~CAP_REQUIRED;
5058c8eb 91 entry->flags |= CAP_ORPHANED;
f01f67f0
AC
92 }
93}
94
95void
96capability_require(struct CapabilityIndex *index, const char *cap)
97{
98 struct CapabilityEntry *entry;
99
100 s_assert(index != NULL);
101
102 entry = irc_dictionary_retrieve(index->cap_dict, cap);
103 if (entry != NULL)
104 entry->flags |= CAP_REQUIRED;
64b56afd
AC
105}
106
107static void
108capability_destroy(struct DictionaryElement *delem, void *privdata)
109{
110 s_assert(delem != NULL);
111
112 rb_free(delem->data);
113}
114
115struct CapabilityIndex *
116capability_index_create(void)
117{
118 struct CapabilityIndex *index;
119
120 index = rb_malloc(sizeof(struct CapabilityIndex));
121 index->cap_dict = irc_dictionary_create(strcasecmp);
122 index->highest_bit = 1;
123
124 return index;
125}
126
127void
128capability_index_destroy(struct CapabilityIndex *index)
129{
130 s_assert(index != NULL);
131
132 irc_dictionary_destroy(index->cap_dict, capability_destroy, NULL);
133 rb_free(index);
134}
5e773521
AC
135
136const char *
137capability_index_list(struct CapabilityIndex *index, unsigned int cap_mask)
138{
139 struct DictionaryIter iter;
140 struct CapabilityEntry *entry;
141 static char buf[BUFSIZE];
142 char *t = buf;
143 int tl;
144
145 s_assert(index != NULL);
146
147 *t = '\0';
148
149 DICTIONARY_FOREACH(entry, &iter, index->cap_dict)
150 {
151 if (entry->value & cap_mask)
152 {
153 tl = rb_sprintf(t, "%s ", iter.cur->key);
154 t += tl;
155 }
156 }
157
158 t--;
159 *t = '\0';
160
161 return buf;
162}
ec3a9055
AC
163
164unsigned int
165capability_index_mask(struct CapabilityIndex *index)
166{
167 struct DictionaryIter iter;
168 struct CapabilityEntry *entry;
169 unsigned int mask = 0;
170
171 s_assert(index != NULL);
172
173 DICTIONARY_FOREACH(entry, &iter, index->cap_dict)
174 {
0582290f 175 if (!(entry->flags & CAP_ORPHANED))
ec3a9055
AC
176 mask |= entry->value;
177 }
178
179 return mask;
180}
f01f67f0
AC
181
182unsigned int
183capability_index_get_required(struct CapabilityIndex *index)
184{
185 struct DictionaryIter iter;
186 struct CapabilityEntry *entry;
187 unsigned int mask = 0;
188
189 s_assert(index != NULL);
190
191 DICTIONARY_FOREACH(entry, &iter, index->cap_dict)
192 {
193 if (!(entry->flags & CAP_ORPHANED) && (entry->flags & CAP_REQUIRED))
194 mask |= entry->value;
195 }
196
197 return mask;
198}