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_orphan(struct CapabilityIndex
*idx
, const char *cap
)
89 struct CapabilityEntry
*entry
;
91 s_assert(idx
!= NULL
);
93 entry
= irc_dictionary_retrieve(idx
->cap_dict
, cap
);
96 entry
->flags
&= ~CAP_REQUIRED
;
97 entry
->flags
|= CAP_ORPHANED
;
102 capability_require(struct CapabilityIndex
*idx
, const char *cap
)
104 struct CapabilityEntry
*entry
;
106 s_assert(idx
!= NULL
);
108 entry
= irc_dictionary_retrieve(idx
->cap_dict
, cap
);
110 entry
->flags
|= CAP_REQUIRED
;
114 capability_destroy(struct DictionaryElement
*delem
, void *privdata
)
116 s_assert(delem
!= NULL
);
118 rb_free(delem
->data
);
121 struct CapabilityIndex
*
122 capability_index_create(const char *name
)
124 struct CapabilityIndex
*idx
;
126 idx
= rb_malloc(sizeof(struct CapabilityIndex
));
127 idx
->name
= rb_strdup(name
);
128 idx
->cap_dict
= irc_dictionary_create(strcasecmp
);
129 idx
->highest_bit
= 1;
131 rb_dlinkAdd(idx
, &idx
->node
, &capability_indexes
);
137 capability_index_destroy(struct CapabilityIndex
*idx
)
139 s_assert(idx
!= NULL
);
141 rb_dlinkDelete(&idx
->node
, &capability_indexes
);
143 irc_dictionary_destroy(idx
->cap_dict
, capability_destroy
, NULL
);
148 capability_index_list(struct CapabilityIndex
*idx
, unsigned int cap_mask
)
150 struct DictionaryIter iter
;
151 struct CapabilityEntry
*entry
;
152 static char buf
[BUFSIZE
];
156 s_assert(idx
!= NULL
);
160 DICTIONARY_FOREACH(entry
, &iter
, idx
->cap_dict
)
162 if (entry
->value
& cap_mask
)
164 tl
= rb_sprintf(t
, "%s ", entry
->cap
);
176 capability_index_mask(struct CapabilityIndex
*idx
)
178 struct DictionaryIter iter
;
179 struct CapabilityEntry
*entry
;
180 unsigned int mask
= 0;
182 s_assert(idx
!= NULL
);
184 DICTIONARY_FOREACH(entry
, &iter
, idx
->cap_dict
)
186 if (!(entry
->flags
& CAP_ORPHANED
))
187 mask
|= (1 << entry
->value
);
194 capability_index_get_required(struct CapabilityIndex
*idx
)
196 struct DictionaryIter iter
;
197 struct CapabilityEntry
*entry
;
198 unsigned int mask
= 0;
200 s_assert(idx
!= NULL
);
202 DICTIONARY_FOREACH(entry
, &iter
, idx
->cap_dict
)
204 if (!(entry
->flags
& CAP_ORPHANED
) && (entry
->flags
& CAP_REQUIRED
))
205 mask
|= (1 << entry
->value
);
212 capability_index_stats(void (*cb
)(const char *line
, void *privdata
), void *privdata
)
217 RB_DLINK_FOREACH(node
, capability_indexes
.head
)
219 struct CapabilityIndex
*idx
= node
->data
;
220 struct DictionaryIter iter
;
221 struct CapabilityEntry
*entry
;
223 rb_snprintf(buf
, sizeof buf
, "'%s': allocated bits - %d", idx
->name
, (idx
->highest_bit
- 1));
226 DICTIONARY_FOREACH(entry
, &iter
, idx
->cap_dict
)
228 rb_snprintf(buf
, sizeof buf
, "bit %d: '%s'", entry
->value
, entry
->cap
);
232 rb_snprintf(buf
, sizeof buf
, "'%s': remaining bits - %ld", idx
->name
,
233 (sizeof(unsigned int) * 8) - (idx
->highest_bit
- 1));
237 rb_snprintf(buf
, sizeof buf
, "%ld capability indexes", rb_dlink_list_length(&capability_indexes
));