Initial import
[ratbox-ambernet.git] / src / hook.c
blobf8463f008c3a122a3381ada28184830ec321ba73
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 * $Id: hook.c 18369 2005-01-31 00:16:07Z leeh $
39 #include "stdinc.h"
40 #include "memory.h"
41 #include "tools.h"
42 #include "hook.h"
43 #include "irc_string.h"
45 hook *hooks;
47 #define HOOK_INCREMENT 10
49 int num_hooks = 0;
50 int last_hook = 0;
51 int max_hooks = HOOK_INCREMENT;
53 #ifdef USE_IODEBUG_HOOKS
54 int h_iosend_id;
55 int h_iorecv_id;
56 int h_iorecvctrl_id;
57 #endif
58 int h_burst_client;
59 int h_burst_channel;
60 int h_burst_finished;
61 int h_server_introduced;
63 void
64 init_hook(void)
66 hooks = MyMalloc(sizeof(hook) * HOOK_INCREMENT);
68 #ifdef USE_IODEBUG_HOOKS
69 h_iosend_id = register_hook("iosend");
70 h_iorecv_id = register_hook("iorecv");
71 h_iorecvctrl_id = register_hook("iorecvctrl");
72 #endif
74 h_burst_client = register_hook("burst_client");
75 h_burst_channel = register_hook("burst_channel");
76 h_burst_finished = register_hook("burst_finished");
77 h_server_introduced = register_hook("server_introduced");
80 /* grow_hooktable()
81 * Enlarges the hook table by HOOK_INCREMENT
83 static void
84 grow_hooktable(void)
86 hook *newhooks;
88 newhooks = MyMalloc(sizeof(hook) * (max_hooks + HOOK_INCREMENT));
89 memcpy(newhooks, hooks, sizeof(hook) * num_hooks);
91 MyFree(hooks);
92 hooks = newhooks;
93 max_hooks += HOOK_INCREMENT;
96 /* find_freehookslot()
97 * Finds the next free slot in the hook table, given by an entry with
98 * h->name being NULL.
100 static int
101 find_freehookslot(void)
103 int i;
105 if((num_hooks + 1) > max_hooks)
106 grow_hooktable();
108 for(i = 0; i < max_hooks; i++)
110 if(!hooks[i].name)
111 return i;
114 /* shouldnt ever get here */
115 return(max_hooks - 1);
118 /* find_hook()
119 * Finds an event in the hook table.
121 static int
122 find_hook(const char *name)
124 int i;
126 for(i = 0; i < max_hooks; i++)
128 if(!hooks[i].name)
129 continue;
131 if(!irccmp(hooks[i].name, name))
132 return i;
135 return -1;
138 /* register_hook()
139 * Finds an events position in the hook table, creating it if it doesnt
140 * exist.
143 register_hook(const char *name)
145 int i;
147 if((i = find_hook(name)) < 0)
149 i = find_freehookslot();
150 DupString(hooks[i].name, name);
151 num_hooks++;
154 return i;
157 /* add_hook()
158 * Adds a hook to an event in the hook table, creating event first if
159 * needed.
161 void
162 add_hook(const char *name, hookfn fn)
164 int i;
166 i = register_hook(name);
168 dlinkAddAlloc(fn, &hooks[i].hooks);
171 /* remove_hook()
172 * Removes a hook from an event in the hook table.
174 void
175 remove_hook(const char *name, hookfn fn)
177 int i;
179 if((i = find_hook(name)) < 0)
180 return;
182 dlinkFindDestroy(fn, &hooks[i].hooks);
185 /* call_hook()
186 * Calls functions from a given event in the hook table.
188 void
189 call_hook(int id, void *arg)
191 hookfn fn;
192 dlink_node *ptr;
194 /* The ID we were passed is the position in the hook table of this
195 * hook
197 DLINK_FOREACH(ptr, hooks[id].hooks.head)
199 fn = ptr->data;
200 fn(arg);