2 * Copyright (c) 2012 William Pitcock <nenolod@dereferenced.org>.
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.
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.
22 #include "capability.h"
23 #include "irc_dictionary.h"
25 static rb_dlink_list capability_indexes
= { NULL
, NULL
, 0 };
27 struct CapabilityIndex
{
29 struct Dictionary
*cap_dict
;
30 unsigned int highest_bit
;
34 #define CAP_ORPHANED 0x1
35 #define CAP_REQUIRED 0x2
37 struct CapabilityEntry
{
44 capability_get(struct CapabilityIndex
*idx
, const char *cap
)
46 struct CapabilityEntry
*entry
;
48 s_assert(idx
!= NULL
);
50 entry
= irc_dictionary_retrieve(idx
->cap_dict
, cap
);
51 if (entry
!= NULL
&& !(entry
->flags
& CAP_ORPHANED
))
52 return (1 << entry
->value
);
58 capability_put(struct CapabilityIndex
*idx
, const char *cap
)
60 struct CapabilityEntry
*entry
;
62 s_assert(idx
!= NULL
);
63 if (!idx
->highest_bit
)
66 if ((entry
= irc_dictionary_retrieve(idx
->cap_dict
, cap
)) != NULL
)
68 entry
->flags
&= ~CAP_ORPHANED
;
69 return (1 << entry
->value
);
72 entry
= rb_malloc(sizeof(struct CapabilityEntry
));
73 entry
->cap
= rb_strdup(cap
);
75 entry
->value
= idx
->highest_bit
;
77 irc_dictionary_add(idx
->cap_dict
, entry
->cap
, entry
);
80 if (idx
->highest_bit
% (sizeof(unsigned int) * 8) == 0)
83 return (1 << entry
->value
);
87 capability_put_anonymous(struct CapabilityIndex
*idx
)
91 s_assert(idx
!= NULL
);
92 if (!idx
->highest_bit
)
94 value
= 1 << idx
->highest_bit
;
96 if (idx
->highest_bit
% (sizeof(unsigned int) * 8) == 0)
102 capability_orphan(struct CapabilityIndex
*idx
, const char *cap
)
104 struct CapabilityEntry
*entry
;
106 s_assert(idx
!= NULL
);
108 entry
= irc_dictionary_retrieve(idx
->cap_dict
, cap
);
111 entry
->flags
&= ~CAP_REQUIRED
;
112 entry
->flags
|= CAP_ORPHANED
;
117 capability_require(struct CapabilityIndex
*idx
, const char *cap
)
119 struct CapabilityEntry
*entry
;
121 s_assert(idx
!= NULL
);
123 entry
= irc_dictionary_retrieve(idx
->cap_dict
, cap
);
125 entry
->flags
|= CAP_REQUIRED
;
129 capability_destroy(struct DictionaryElement
*delem
, void *privdata
)
131 s_assert(delem
!= NULL
);
133 rb_free(delem
->data
);
136 struct CapabilityIndex
*
137 capability_index_create(const char *name
)
139 struct CapabilityIndex
*idx
;
141 idx
= rb_malloc(sizeof(struct CapabilityIndex
));
142 idx
->name
= rb_strdup(name
);
143 idx
->cap_dict
= irc_dictionary_create(strcasecmp
);
144 idx
->highest_bit
= 1;
146 rb_dlinkAdd(idx
, &idx
->node
, &capability_indexes
);
152 capability_index_destroy(struct CapabilityIndex
*idx
)
154 s_assert(idx
!= NULL
);
156 rb_dlinkDelete(&idx
->node
, &capability_indexes
);
158 irc_dictionary_destroy(idx
->cap_dict
, capability_destroy
, NULL
);
163 capability_index_list(struct CapabilityIndex
*idx
, unsigned int cap_mask
)
165 struct DictionaryIter iter
;
166 struct CapabilityEntry
*entry
;
167 static char buf
[BUFSIZE
];
171 s_assert(idx
!= NULL
);
175 DICTIONARY_FOREACH(entry
, &iter
, idx
->cap_dict
)
177 if ((1 << entry
->value
) & cap_mask
)
179 tl
= rb_sprintf(t
, "%s ", entry
->cap
);
191 capability_index_mask(struct CapabilityIndex
*idx
)
193 struct DictionaryIter iter
;
194 struct CapabilityEntry
*entry
;
195 unsigned int mask
= 0;
197 s_assert(idx
!= NULL
);
199 DICTIONARY_FOREACH(entry
, &iter
, idx
->cap_dict
)
201 if (!(entry
->flags
& CAP_ORPHANED
))
202 mask
|= (1 << entry
->value
);
209 capability_index_get_required(struct CapabilityIndex
*idx
)
211 struct DictionaryIter iter
;
212 struct CapabilityEntry
*entry
;
213 unsigned int mask
= 0;
215 s_assert(idx
!= NULL
);
217 DICTIONARY_FOREACH(entry
, &iter
, idx
->cap_dict
)
219 if (!(entry
->flags
& CAP_ORPHANED
) && (entry
->flags
& CAP_REQUIRED
))
220 mask
|= (1 << entry
->value
);
227 capability_index_stats(void (*cb
)(const char *line
, void *privdata
), void *privdata
)
232 RB_DLINK_FOREACH(node
, capability_indexes
.head
)
234 struct CapabilityIndex
*idx
= node
->data
;
235 struct DictionaryIter iter
;
236 struct CapabilityEntry
*entry
;
238 rb_snprintf(buf
, sizeof buf
, "'%s': allocated bits - %d", idx
->name
, (idx
->highest_bit
- 1));
241 DICTIONARY_FOREACH(entry
, &iter
, idx
->cap_dict
)
243 rb_snprintf(buf
, sizeof buf
, "bit %d: '%s'", entry
->value
, entry
->cap
);
247 rb_snprintf(buf
, sizeof buf
, "'%s': remaining bits - %ld", idx
->name
,
248 (sizeof(unsigned int) * 8) - (idx
->highest_bit
- 1));
252 rb_snprintf(buf
, sizeof buf
, "%ld capability indexes", rb_dlink_list_length(&capability_indexes
));