]>
jfr.im git - solanum.git/blob - src/capability.c
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 struct CapabilityIndex
{
26 struct Dictionary
*cap_dict
;
27 unsigned int highest_bit
;
30 struct CapabilityEntry
{
31 unsigned int orphaned
;
36 capability_get(struct CapabilityIndex
*index
, const char *cap
)
38 struct CapabilityEntry
*entry
;
40 s_assert(index
!= NULL
);
42 entry
= irc_dictionary_retrieve(index
->cap_dict
, cap
);
43 if (entry
!= NULL
&& !entry
->orphaned
)
50 capability_put(struct CapabilityIndex
*index
, const char *cap
)
52 struct CapabilityEntry
*entry
;
54 s_assert(index
!= NULL
);
56 if ((entry
= irc_dictionary_retrieve(index
->cap_dict
, cap
)) != NULL
)
62 entry
= rb_malloc(sizeof(struct CapabilityEntry
));
64 entry
->value
= index
->highest_bit
;
66 irc_dictionary_add(index
->cap_dict
, cap
, entry
);
68 index
->highest_bit
<<= 1;
70 /* hmm... not sure what to do here, so i guess we will abort for now... --nenolod */
71 if (index
->highest_bit
== 0)
78 capability_orphan(struct CapabilityIndex
*index
, const char *cap
)
80 struct CapabilityEntry
*entry
;
82 s_assert(index
!= NULL
);
84 entry
= irc_dictionary_retrieve(index
->cap_dict
, cap
);
90 capability_destroy(struct DictionaryElement
*delem
, void *privdata
)
92 s_assert(delem
!= NULL
);
97 struct CapabilityIndex
*
98 capability_index_create(void)
100 struct CapabilityIndex
*index
;
102 index
= rb_malloc(sizeof(struct CapabilityIndex
));
103 index
->cap_dict
= irc_dictionary_create(strcasecmp
);
104 index
->highest_bit
= 1;
110 capability_index_destroy(struct CapabilityIndex
*index
)
112 s_assert(index
!= NULL
);
114 irc_dictionary_destroy(index
->cap_dict
, capability_destroy
, NULL
);
119 capability_index_list(struct CapabilityIndex
*index
, unsigned int cap_mask
)
121 struct DictionaryIter iter
;
122 struct CapabilityEntry
*entry
;
123 static char buf
[BUFSIZE
];
127 s_assert(index
!= NULL
);
131 DICTIONARY_FOREACH(entry
, &iter
, index
->cap_dict
)
133 if (entry
->value
& cap_mask
)
135 tl
= rb_sprintf(t
, "%s ", iter
.cur
->key
);
147 capability_index_mask(struct CapabilityIndex
*index
)
149 struct DictionaryIter iter
;
150 struct CapabilityEntry
*entry
;
151 unsigned int mask
= 0;
153 s_assert(index
!= NULL
);
155 DICTIONARY_FOREACH(entry
, &iter
, index
->cap_dict
)
157 if (!entry
->orphaned
)
158 mask
|= entry
->value
;