]> jfr.im git - irc/rqf/shadowircd.git/blame - src/hook.c
Fix an off by one error with zipstats processing
[irc/rqf/shadowircd.git] / src / hook.c
CommitLineData
212380e3 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.
36 *
37 * $Id: hook.c 712 2006-02-06 04:42:14Z gxti $
38 */
39#include "stdinc.h"
212380e3 40#include "hook.h"
13ae2f4b 41#include "match.h"
212380e3 42
43hook *hooks;
44
45#define HOOK_INCREMENT 1000
46
47int num_hooks = 0;
48int last_hook = 0;
49int max_hooks = HOOK_INCREMENT;
50
51#ifdef USE_IODEBUG_HOOKS
52int h_iosend_id;
53int h_iorecv_id;
54int h_iorecvctrl_id;
55#endif
56int h_burst_client;
57int h_burst_channel;
58int h_burst_finished;
59int h_server_introduced;
60int h_server_eob;
61int h_client_exit;
aa65834c 62int h_umode_changed;
212380e3 63int h_new_local_user;
64int h_new_remote_user;
65int h_introduce_client;
66
67void
68init_hook(void)
69{
8e43b0b4 70 hooks = rb_malloc(sizeof(hook) * HOOK_INCREMENT);
212380e3 71
72#ifdef USE_IODEBUG_HOOKS
73 h_iosend_id = register_hook("iosend");
74 h_iorecv_id = register_hook("iorecv");
75 h_iorecvctrl_id = register_hook("iorecvctrl");
76#endif
77
78 h_burst_client = register_hook("burst_client");
79 h_burst_channel = register_hook("burst_channel");
80 h_burst_finished = register_hook("burst_finished");
81 h_server_introduced = register_hook("server_introduced");
82 h_server_eob = register_hook("server_eob");
83 h_client_exit = register_hook("client_exit");
84 h_umode_changed = register_hook("umode_changed");
85 h_new_local_user = register_hook("new_local_user");
86 h_new_remote_user = register_hook("new_remote_user");
87 h_introduce_client = register_hook("introduce_client");
88}
89
90/* grow_hooktable()
91 * Enlarges the hook table by HOOK_INCREMENT
92 */
93static void
94grow_hooktable(void)
95{
96 hook *newhooks;
97
8e43b0b4 98 newhooks = rb_malloc(sizeof(hook) * (max_hooks + HOOK_INCREMENT));
212380e3 99 memcpy(newhooks, hooks, sizeof(hook) * num_hooks);
100
90a3c35b 101 rb_free(hooks);
212380e3 102 hooks = newhooks;
103 max_hooks += HOOK_INCREMENT;
104}
105
106/* find_freehookslot()
107 * Finds the next free slot in the hook table, given by an entry with
108 * h->name being NULL.
109 */
110static int
111find_freehookslot(void)
112{
113 int i;
114
115 if((num_hooks + 1) > max_hooks)
116 grow_hooktable();
117
118 for(i = 0; i < max_hooks; i++)
119 {
120 if(!hooks[i].name)
121 return i;
122 }
123
124 /* shouldnt ever get here */
125 return(max_hooks - 1);
126}
127
128/* find_hook()
129 * Finds an event in the hook table.
130 */
131static int
132find_hook(const char *name)
133{
134 int i;
135
136 for(i = 0; i < max_hooks; i++)
137 {
138 if(!hooks[i].name)
139 continue;
140
141 if(!irccmp(hooks[i].name, name))
142 return i;
143 }
144
145 return -1;
146}
147
148/* register_hook()
149 * Finds an events position in the hook table, creating it if it doesnt
150 * exist.
151 */
152int
153register_hook(const char *name)
154{
155 int i;
156
157 if((i = find_hook(name)) < 0)
158 {
159 i = find_freehookslot();
62d28946 160 hooks[i].name = rb_strdup(name);
212380e3 161 num_hooks++;
162 }
163
164 return i;
165}
166
167/* add_hook()
168 * Adds a hook to an event in the hook table, creating event first if
169 * needed.
170 */
171void
172add_hook(const char *name, hookfn fn)
173{
174 int i;
175
176 i = register_hook(name);
177
af81d5a0 178 rb_dlinkAddAlloc(fn, &hooks[i].hooks);
212380e3 179}
180
181/* remove_hook()
182 * Removes a hook from an event in the hook table.
183 */
184void
185remove_hook(const char *name, hookfn fn)
186{
187 int i;
188
189 if((i = find_hook(name)) < 0)
190 return;
191
af81d5a0 192 rb_dlinkFindDestroy(fn, &hooks[i].hooks);
212380e3 193}
194
195/* call_hook()
196 * Calls functions from a given event in the hook table.
197 */
198void
199call_hook(int id, void *arg)
200{
201 hookfn fn;
af81d5a0 202 rb_dlink_node *ptr;
212380e3 203
204 /* The ID we were passed is the position in the hook table of this
205 * hook
206 */
8e69bb4e 207 RB_DLINK_FOREACH(ptr, hooks[id].hooks.head)
212380e3 208 {
209 fn = ptr->data;
210 fn(arg);
211 }
212}
213