2012-02-04 05:58:53 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012 William Pitcock <nenolod@dereferenced.org>.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice is present in all copies.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stdinc.h"
|
|
|
|
#include "capability.h"
|
|
|
|
#include "irc_dictionary.h"
|
|
|
|
|
|
|
|
struct CapabilityIndex {
|
|
|
|
struct Dictionary *cap_dict;
|
|
|
|
unsigned int highest_bit;
|
|
|
|
};
|
|
|
|
|
2012-02-04 07:55:11 +00:00
|
|
|
#define CAP_ORPHANED 0x1
|
2012-02-04 08:00:33 +00:00
|
|
|
#define CAP_REQUIRED 0x2
|
2012-02-04 07:55:11 +00:00
|
|
|
|
2012-02-04 05:58:53 +00:00
|
|
|
struct CapabilityEntry {
|
2012-02-04 10:47:37 +00:00
|
|
|
char *cap;
|
2012-02-04 05:58:53 +00:00
|
|
|
unsigned int value;
|
2012-02-04 07:55:11 +00:00
|
|
|
unsigned int flags;
|
2012-02-04 05:58:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
unsigned int
|
|
|
|
capability_get(struct CapabilityIndex *index, const char *cap)
|
|
|
|
{
|
|
|
|
struct CapabilityEntry *entry;
|
|
|
|
|
|
|
|
s_assert(index != NULL);
|
|
|
|
|
|
|
|
entry = irc_dictionary_retrieve(index->cap_dict, cap);
|
2012-02-04 07:55:11 +00:00
|
|
|
if (entry != NULL && !(entry->flags & CAP_ORPHANED))
|
2012-02-04 05:58:53 +00:00
|
|
|
return entry->value;
|
|
|
|
|
|
|
|
return 0xFFFFFFFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int
|
|
|
|
capability_put(struct CapabilityIndex *index, const char *cap)
|
|
|
|
{
|
|
|
|
struct CapabilityEntry *entry;
|
|
|
|
|
|
|
|
s_assert(index != NULL);
|
|
|
|
|
|
|
|
if ((entry = irc_dictionary_retrieve(index->cap_dict, cap)) != NULL)
|
|
|
|
{
|
2012-02-04 07:55:11 +00:00
|
|
|
entry->flags &= ~CAP_ORPHANED;
|
2012-02-04 05:58:53 +00:00
|
|
|
return entry->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
entry = rb_malloc(sizeof(struct CapabilityEntry));
|
2012-02-04 10:47:37 +00:00
|
|
|
entry->cap = rb_strdup(cap);
|
2012-02-04 07:55:11 +00:00
|
|
|
entry->flags = 0;
|
2012-02-04 05:58:53 +00:00
|
|
|
entry->value = index->highest_bit;
|
|
|
|
|
2012-02-04 10:47:37 +00:00
|
|
|
irc_dictionary_add(index->cap_dict, entry->cap, entry);
|
2012-02-04 05:58:53 +00:00
|
|
|
|
|
|
|
index->highest_bit <<= 1;
|
|
|
|
|
|
|
|
/* hmm... not sure what to do here, so i guess we will abort for now... --nenolod */
|
|
|
|
if (index->highest_bit == 0)
|
|
|
|
abort();
|
|
|
|
|
|
|
|
return entry->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
capability_orphan(struct CapabilityIndex *index, const char *cap)
|
|
|
|
{
|
|
|
|
struct CapabilityEntry *entry;
|
|
|
|
|
|
|
|
s_assert(index != NULL);
|
|
|
|
|
|
|
|
entry = irc_dictionary_retrieve(index->cap_dict, cap);
|
|
|
|
if (entry != NULL)
|
2012-02-04 08:00:33 +00:00
|
|
|
{
|
|
|
|
entry->flags &= ~CAP_REQUIRED;
|
2012-02-04 07:55:11 +00:00
|
|
|
entry->flags |= CAP_ORPHANED;
|
2012-02-04 08:00:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
capability_require(struct CapabilityIndex *index, const char *cap)
|
|
|
|
{
|
|
|
|
struct CapabilityEntry *entry;
|
|
|
|
|
|
|
|
s_assert(index != NULL);
|
|
|
|
|
|
|
|
entry = irc_dictionary_retrieve(index->cap_dict, cap);
|
|
|
|
if (entry != NULL)
|
|
|
|
entry->flags |= CAP_REQUIRED;
|
2012-02-04 05:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
capability_destroy(struct DictionaryElement *delem, void *privdata)
|
|
|
|
{
|
|
|
|
s_assert(delem != NULL);
|
|
|
|
|
|
|
|
rb_free(delem->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CapabilityIndex *
|
|
|
|
capability_index_create(void)
|
|
|
|
{
|
|
|
|
struct CapabilityIndex *index;
|
|
|
|
|
|
|
|
index = rb_malloc(sizeof(struct CapabilityIndex));
|
|
|
|
index->cap_dict = irc_dictionary_create(strcasecmp);
|
|
|
|
index->highest_bit = 1;
|
|
|
|
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
capability_index_destroy(struct CapabilityIndex *index)
|
|
|
|
{
|
|
|
|
s_assert(index != NULL);
|
|
|
|
|
|
|
|
irc_dictionary_destroy(index->cap_dict, capability_destroy, NULL);
|
|
|
|
rb_free(index);
|
|
|
|
}
|
2012-02-04 06:36:42 +00:00
|
|
|
|
|
|
|
const char *
|
|
|
|
capability_index_list(struct CapabilityIndex *index, unsigned int cap_mask)
|
|
|
|
{
|
|
|
|
struct DictionaryIter iter;
|
|
|
|
struct CapabilityEntry *entry;
|
|
|
|
static char buf[BUFSIZE];
|
|
|
|
char *t = buf;
|
|
|
|
int tl;
|
|
|
|
|
|
|
|
s_assert(index != NULL);
|
|
|
|
|
|
|
|
*t = '\0';
|
|
|
|
|
|
|
|
DICTIONARY_FOREACH(entry, &iter, index->cap_dict)
|
|
|
|
{
|
|
|
|
if (entry->value & cap_mask)
|
|
|
|
{
|
|
|
|
tl = rb_sprintf(t, "%s ", iter.cur->key);
|
|
|
|
t += tl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t--;
|
|
|
|
*t = '\0';
|
|
|
|
|
|
|
|
return buf;
|
|
|
|
}
|
2012-02-04 06:39:53 +00:00
|
|
|
|
|
|
|
unsigned int
|
|
|
|
capability_index_mask(struct CapabilityIndex *index)
|
|
|
|
{
|
|
|
|
struct DictionaryIter iter;
|
|
|
|
struct CapabilityEntry *entry;
|
|
|
|
unsigned int mask = 0;
|
|
|
|
|
|
|
|
s_assert(index != NULL);
|
|
|
|
|
|
|
|
DICTIONARY_FOREACH(entry, &iter, index->cap_dict)
|
|
|
|
{
|
2012-02-04 07:58:07 +00:00
|
|
|
if (!(entry->flags & CAP_ORPHANED))
|
2012-02-04 06:39:53 +00:00
|
|
|
mask |= entry->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mask;
|
|
|
|
}
|
2012-02-04 08:00:33 +00:00
|
|
|
|
|
|
|
unsigned int
|
|
|
|
capability_index_get_required(struct CapabilityIndex *index)
|
|
|
|
{
|
|
|
|
struct DictionaryIter iter;
|
|
|
|
struct CapabilityEntry *entry;
|
|
|
|
unsigned int mask = 0;
|
|
|
|
|
|
|
|
s_assert(index != NULL);
|
|
|
|
|
|
|
|
DICTIONARY_FOREACH(entry, &iter, index->cap_dict)
|
|
|
|
{
|
|
|
|
if (!(entry->flags & CAP_ORPHANED) && (entry->flags & CAP_REQUIRED))
|
|
|
|
mask |= entry->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mask;
|
|
|
|
}
|