]> jfr.im git - solanum.git/blame - ircd/hook.c
Avoid show_iline_prefix showing leftovers (#266)
[solanum.git] / ircd / hook.c
CommitLineData
212380e3
AC
1/*
2 * ircd-ratbox: an advanced Internet Relay Chat Daemon(ircd).
3 * hook.c - code for dealing with the hook system
4 *
5 * This code is basically a slow leaking array. Events are simply just a
6 * position in this array. When hooks are added, events will be created if
7 * they dont exist - this means modules with hooks can be loaded in any
8 * order, and events are preserved through module reloads.
9 *
10 * Copyright (C) 2004-2005 Lee Hardy <lee -at- leeh.co.uk>
11 * Copyright (C) 2004-2005 ircd-ratbox development team
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions are
15 * met:
16 *
17 * 1.Redistributions of source code must retain the above copyright notice,
18 * this list of conditions and the following disclaimer.
19 * 2.Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3.The name of the author may not be used to endorse or promote products
23 * derived from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
34 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
212380e3
AC
36 */
37#include "stdinc.h"
212380e3 38#include "hook.h"
4562c604 39#include "match.h"
212380e3
AC
40
41hook *hooks;
42
43#define HOOK_INCREMENT 1000
44
91b12782
EK
45struct hook_entry
46{
47 rb_dlink_node node;
48 hookfn fn;
49 enum hook_priority priority;
50};
51
212380e3
AC
52int num_hooks = 0;
53int last_hook = 0;
54int max_hooks = HOOK_INCREMENT;
55
212380e3
AC
56int h_burst_client;
57int h_burst_channel;
58int h_burst_finished;
59int h_server_introduced;
60int h_server_eob;
61int h_client_exit;
15b05f95 62int h_after_client_exit;
2e819b6b 63int h_umode_changed;
212380e3
AC
64int h_new_local_user;
65int h_new_remote_user;
66int h_introduce_client;
5f8d323c 67int h_can_kick;
ca4c2a86
AC
68int h_privmsg_user;
69int h_privmsg_channel;
7d603754
KB
70int h_conf_read_start;
71int h_conf_read_end;
4f8ababa 72int h_outbound_msgbuf;
2575a78b 73int h_rehash;
8aadf0ce 74int h_priv_change;
71f10f83 75int h_cap_change;
212380e3
AC
76
77void
78init_hook(void)
79{
eddc2ab6 80 hooks = rb_malloc(sizeof(hook) * HOOK_INCREMENT);
212380e3 81
212380e3
AC
82 h_burst_client = register_hook("burst_client");
83 h_burst_channel = register_hook("burst_channel");
84 h_burst_finished = register_hook("burst_finished");
85 h_server_introduced = register_hook("server_introduced");
86 h_server_eob = register_hook("server_eob");
87 h_client_exit = register_hook("client_exit");
15b05f95 88 h_after_client_exit = register_hook("after_client_exit");
212380e3
AC
89 h_umode_changed = register_hook("umode_changed");
90 h_new_local_user = register_hook("new_local_user");
91 h_new_remote_user = register_hook("new_remote_user");
92 h_introduce_client = register_hook("introduce_client");
5f8d323c 93 h_can_kick = register_hook("can_kick");
ca4c2a86
AC
94 h_privmsg_user = register_hook("privmsg_user");
95 h_privmsg_channel = register_hook("privmsg_channel");
7d603754
KB
96 h_conf_read_start = register_hook("conf_read_start");
97 h_conf_read_end = register_hook("conf_read_end");
4f8ababa 98 h_outbound_msgbuf = register_hook("outbound_msgbuf");
2575a78b 99 h_rehash = register_hook("rehash");
8aadf0ce 100 h_priv_change = register_hook("priv_change");
71f10f83 101 h_cap_change = register_hook("cap_change");
212380e3
AC
102}
103
104/* grow_hooktable()
105 * Enlarges the hook table by HOOK_INCREMENT
106 */
107static void
108grow_hooktable(void)
109{
110 hook *newhooks;
111
eddc2ab6 112 newhooks = rb_malloc(sizeof(hook) * (max_hooks + HOOK_INCREMENT));
212380e3
AC
113 memcpy(newhooks, hooks, sizeof(hook) * num_hooks);
114
637c4932 115 rb_free(hooks);
212380e3
AC
116 hooks = newhooks;
117 max_hooks += HOOK_INCREMENT;
118}
119
120/* find_freehookslot()
121 * Finds the next free slot in the hook table, given by an entry with
122 * h->name being NULL.
123 */
124static int
125find_freehookslot(void)
126{
127 int i;
128
129 if((num_hooks + 1) > max_hooks)
130 grow_hooktable();
131
132 for(i = 0; i < max_hooks; i++)
133 {
134 if(!hooks[i].name)
135 return i;
136 }
137
138 /* shouldnt ever get here */
139 return(max_hooks - 1);
140}
141
142/* find_hook()
143 * Finds an event in the hook table.
144 */
145static int
146find_hook(const char *name)
147{
148 int i;
149
150 for(i = 0; i < max_hooks; i++)
151 {
152 if(!hooks[i].name)
153 continue;
154
155 if(!irccmp(hooks[i].name, name))
156 return i;
157 }
158
159 return -1;
160}
161
162/* register_hook()
163 * Finds an events position in the hook table, creating it if it doesnt
164 * exist.
165 */
166int
167register_hook(const char *name)
168{
169 int i;
170
171 if((i = find_hook(name)) < 0)
172 {
173 i = find_freehookslot();
47a03750 174 hooks[i].name = rb_strdup(name);
212380e3
AC
175 num_hooks++;
176 }
177
178 return i;
179}
180
181/* add_hook()
182 * Adds a hook to an event in the hook table, creating event first if
183 * needed.
184 */
185void
186add_hook(const char *name, hookfn fn)
187{
91b12782
EK
188 add_hook_prio(name, fn, HOOK_NORMAL);
189}
190
191/* add_hook_prio()
192 * Adds a hook with the specified priority
193 */
194void
195add_hook_prio(const char *name, hookfn fn, enum hook_priority priority)
196{
197 rb_dlink_node *ptr;
198 struct hook_entry *entry = rb_malloc(sizeof *entry);
212380e3
AC
199 int i;
200
201 i = register_hook(name);
91b12782
EK
202 entry->fn = fn;
203 entry->priority = priority;
204
c500b0bd 205 RB_DLINK_FOREACH(ptr, hooks[i].hooks.head)
91b12782
EK
206 {
207 struct hook_entry *o = ptr->data;
c6b2daca 208 if (entry->priority <= o->priority)
91b12782
EK
209 {
210 rb_dlinkAddBefore(ptr, entry, &entry->node, &hooks[i].hooks);
211 return;
212 }
213 }
212380e3 214
91b12782 215 rb_dlinkAddTail(entry, &entry->node, &hooks[i].hooks);
212380e3
AC
216}
217
218/* remove_hook()
219 * Removes a hook from an event in the hook table.
220 */
221void
222remove_hook(const char *name, hookfn fn)
223{
91b12782 224 rb_dlink_node *ptr, *scratch;
212380e3
AC
225 int i;
226
227 if((i = find_hook(name)) < 0)
228 return;
229
c500b0bd 230 RB_DLINK_FOREACH_SAFE(ptr, scratch, hooks[i].hooks.head)
91b12782
EK
231 {
232 struct hook_entry *entry = ptr->data;
233 if (entry->fn == fn)
234 {
235 rb_dlinkDelete(ptr, &hooks[i].hooks);
236 return;
237 }
238 }
212380e3
AC
239}
240
241/* call_hook()
242 * Calls functions from a given event in the hook table.
243 */
244void
245call_hook(int id, void *arg)
246{
330fc5c1 247 rb_dlink_node *ptr;
212380e3
AC
248
249 /* The ID we were passed is the position in the hook table of this
250 * hook
251 */
5cefa1d6 252 RB_DLINK_FOREACH(ptr, hooks[id].hooks.head)
212380e3 253 {
91b12782
EK
254 struct hook_entry *entry = ptr->data;
255 entry->fn(arg);
212380e3
AC
256 }
257}
258