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