]> jfr.im git - solanum.git/blame - ircd/operhash.c
support RSFNC indicating type of FNC (e.g. FORCE vs REGAIN) (#406)
[solanum.git] / ircd / operhash.c
CommitLineData
a6f63a82 1/* solanum
27f616dd
JT
2 * operhash.c - Hashes nick!user@host{oper}
3 *
4 * Copyright (C) 2005 Lee Hardy <lee -at- leeh.co.uk>
b02a913b 5 * Copyright (C) 2005-2016 ircd-ratbox development team
3fc0499e 6 * Copyright (C) 2016 Ariadne Conill <ariadne@dereferenced.org>
27f616dd
JT
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * 1.Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
14 * 2.Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3.The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
27f616dd 31 */
b02a913b 32
fe037171 33#include <rb_lib.h>
27f616dd
JT
34#include "stdinc.h"
35#include "match.h"
36#include "hash.h"
37#include "operhash.h"
a4bf26dd 38#include "rb_radixtree.h"
27f616dd 39
2fc6772e 40static rb_radixtree *operhash_tree = NULL;
27f616dd 41
b02a913b
AC
42struct operhash_entry
43{
44 unsigned int refcount;
45 char name[];
46};
27f616dd 47
b02a913b
AC
48void
49init_operhash(void)
50{
a4bf26dd 51 operhash_tree = rb_radixtree_create("operhash", NULL);
b02a913b 52}
27f616dd
JT
53
54const char *
55operhash_add(const char *name)
56{
b02a913b
AC
57 struct operhash_entry *ohash;
58 size_t len;
27f616dd
JT
59
60 if(EmptyString(name))
61 return NULL;
62
a4bf26dd 63 if((ohash = (struct operhash_entry *) rb_radixtree_retrieve(operhash_tree, name)) != NULL)
27f616dd 64 {
b02a913b
AC
65 ohash->refcount++;
66 return ohash->name;
27f616dd
JT
67 }
68
b02a913b
AC
69 len = strlen(name) + 1;
70 ohash = rb_malloc(sizeof(struct operhash_entry) + len);
71 ohash->refcount = 1;
72 memcpy(ohash->name, name, len);
a4bf26dd 73 rb_radixtree_add(operhash_tree, ohash->name, ohash);
b02a913b 74 return ohash->name;
27f616dd
JT
75}
76
77const char *
78operhash_find(const char *name)
79{
a4bf26dd 80 return rb_radixtree_retrieve(operhash_tree, name);
27f616dd
JT
81}
82
83void
84operhash_delete(const char *name)
85{
a4bf26dd 86 struct operhash_entry *ohash = rb_radixtree_retrieve(operhash_tree, name);
27f616dd 87
4cc889ae
JV
88 if(ohash == NULL)
89 return;
90
91 ohash->refcount--;
92 if(ohash->refcount == 0)
93 {
94 rb_radixtree_delete(operhash_tree, ohash->name);
b02a913b 95 rb_free(ohash);
4cc889ae 96 }
27f616dd 97}