Restore stats_spy hook that was removed in commit 401f2454671ca233e35b0e6e4f3fa4c43cd...
[seven-1.x.git] / src / hook.c
blob8da4973635caded2e8009168a75e36744c33986b
1 /*
2 * ircd-ratbox: an advanced Internet Relay Chat Daemon(ircd).
3 * hook.c - code for dealing with the hook system
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.
10 * Copyright (C) 2004-2005 Lee Hardy <lee -at- leeh.co.uk>
11 * Copyright (C) 2004-2005 ircd-ratbox development team
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions are
15 * met:
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.
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.
37 #include "stdinc.h"
38 #include "memory.h"
39 #include "tools.h"
40 #include "hook.h"
41 #include "irc_string.h"
43 hook *hooks;
45 #define HOOK_INCREMENT 1000
47 int num_hooks = 0;
48 int last_hook = 0;
49 int max_hooks = HOOK_INCREMENT;
51 #ifdef USE_IODEBUG_HOOKS
52 int h_iosend_id;
53 int h_iorecv_id;
54 int h_iorecvctrl_id;
55 #endif
56 int h_burst_client;
57 int h_burst_channel;
58 int h_burst_finished;
59 int h_server_introduced;
60 int h_server_eob;
61 int h_client_exit;
62 int h_new_local_user;
63 int h_new_remote_user;
64 int h_introduce_client;
66 void
67 init_hook(void)
69 hooks = MyMalloc(sizeof(hook) * HOOK_INCREMENT);
71 #ifdef USE_IODEBUG_HOOKS
72 h_iosend_id = register_hook("iosend");
73 h_iorecv_id = register_hook("iorecv");
74 h_iorecvctrl_id = register_hook("iorecvctrl");
75 #endif
77 h_burst_client = register_hook("burst_client");
78 h_burst_channel = register_hook("burst_channel");
79 h_burst_finished = register_hook("burst_finished");
80 h_server_introduced = register_hook("server_introduced");
81 h_server_eob = register_hook("server_eob");
82 h_client_exit = register_hook("client_exit");
83 h_umode_changed = register_hook("umode_changed");
84 h_new_local_user = register_hook("new_local_user");
85 h_new_remote_user = register_hook("new_remote_user");
86 h_introduce_client = register_hook("introduce_client");
89 /* grow_hooktable()
90 * Enlarges the hook table by HOOK_INCREMENT
92 static void
93 grow_hooktable(void)
95 hook *newhooks;
97 newhooks = MyMalloc(sizeof(hook) * (max_hooks + HOOK_INCREMENT));
98 memcpy(newhooks, hooks, sizeof(hook) * num_hooks);
100 MyFree(hooks);
101 hooks = newhooks;
102 max_hooks += HOOK_INCREMENT;
105 /* find_freehookslot()
106 * Finds the next free slot in the hook table, given by an entry with
107 * h->name being NULL.
109 static int
110 find_freehookslot(void)
112 int i;
114 if((num_hooks + 1) > max_hooks)
115 grow_hooktable();
117 for(i = 0; i < max_hooks; i++)
119 if(!hooks[i].name)
120 return i;
123 /* shouldnt ever get here */
124 return(max_hooks - 1);
127 /* find_hook()
128 * Finds an event in the hook table.
130 static int
131 find_hook(const char *name)
133 int i;
135 for(i = 0; i < max_hooks; i++)
137 if(!hooks[i].name)
138 continue;
140 if(!irccmp(hooks[i].name, name))
141 return i;
144 return -1;
147 /* register_hook()
148 * Finds an events position in the hook table, creating it if it doesnt
149 * exist.
152 register_hook(const char *name)
154 int i;
156 if((i = find_hook(name)) < 0)
158 i = find_freehookslot();
159 DupString(hooks[i].name, name);
160 num_hooks++;
163 return i;
166 /* add_hook()
167 * Adds a hook to an event in the hook table, creating event first if
168 * needed.
170 void
171 add_hook(const char *name, hookfn fn)
173 int i;
175 i = register_hook(name);
177 dlinkAddAlloc(fn, &hooks[i].hooks);
180 /* remove_hook()
181 * Removes a hook from an event in the hook table.
183 void
184 remove_hook(const char *name, hookfn fn)
186 int i;
188 if((i = find_hook(name)) < 0)
189 return;
191 dlinkFindDestroy(fn, &hooks[i].hooks);
194 /* call_hook()
195 * Calls functions from a given event in the hook table.
197 void
198 call_hook(int id, void *arg)
200 hookfn fn;
201 dlink_node *ptr;
203 /* The ID we were passed is the position in the hook table of this
204 * hook
206 DLINK_FOREACH(ptr, hooks[id].hooks.head)
208 fn = ptr->data;
209 fn(arg);