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