]>
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 | #include "stdinc.h" | |
38 | #include "hook.h" | |
39 | #include "match.h" | |
40 | ||
41 | hook *hooks; | |
42 | ||
43 | #define HOOK_INCREMENT 1000 | |
44 | ||
45 | struct hook_entry | |
46 | { | |
47 | rb_dlink_node node; | |
48 | hookfn fn; | |
49 | enum hook_priority priority; | |
50 | }; | |
51 | ||
52 | int num_hooks = 0; | |
53 | int last_hook = 0; | |
54 | int max_hooks = HOOK_INCREMENT; | |
55 | ||
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_after_client_exit; | |
63 | int h_umode_changed; | |
64 | int h_new_local_user; | |
65 | int h_new_remote_user; | |
66 | int h_introduce_client; | |
67 | int h_can_kick; | |
68 | int h_privmsg_user; | |
69 | int h_privmsg_channel; | |
70 | int h_conf_read_start; | |
71 | int h_conf_read_end; | |
72 | int h_outbound_msgbuf; | |
73 | int h_rehash; | |
74 | ||
75 | void | |
76 | init_hook(void) | |
77 | { | |
78 | hooks = rb_malloc(sizeof(hook) * HOOK_INCREMENT); | |
79 | ||
80 | h_burst_client = register_hook("burst_client"); | |
81 | h_burst_channel = register_hook("burst_channel"); | |
82 | h_burst_finished = register_hook("burst_finished"); | |
83 | h_server_introduced = register_hook("server_introduced"); | |
84 | h_server_eob = register_hook("server_eob"); | |
85 | h_client_exit = register_hook("client_exit"); | |
86 | h_after_client_exit = register_hook("after_client_exit"); | |
87 | h_umode_changed = register_hook("umode_changed"); | |
88 | h_new_local_user = register_hook("new_local_user"); | |
89 | h_new_remote_user = register_hook("new_remote_user"); | |
90 | h_introduce_client = register_hook("introduce_client"); | |
91 | h_can_kick = register_hook("can_kick"); | |
92 | h_privmsg_user = register_hook("privmsg_user"); | |
93 | h_privmsg_channel = register_hook("privmsg_channel"); | |
94 | h_conf_read_start = register_hook("conf_read_start"); | |
95 | h_conf_read_end = register_hook("conf_read_end"); | |
96 | h_outbound_msgbuf = register_hook("outbound_msgbuf"); | |
97 | h_rehash = register_hook("rehash"); | |
98 | } | |
99 | ||
100 | /* grow_hooktable() | |
101 | * Enlarges the hook table by HOOK_INCREMENT | |
102 | */ | |
103 | static void | |
104 | grow_hooktable(void) | |
105 | { | |
106 | hook *newhooks; | |
107 | ||
108 | newhooks = rb_malloc(sizeof(hook) * (max_hooks + HOOK_INCREMENT)); | |
109 | memcpy(newhooks, hooks, sizeof(hook) * num_hooks); | |
110 | ||
111 | rb_free(hooks); | |
112 | hooks = newhooks; | |
113 | max_hooks += HOOK_INCREMENT; | |
114 | } | |
115 | ||
116 | /* find_freehookslot() | |
117 | * Finds the next free slot in the hook table, given by an entry with | |
118 | * h->name being NULL. | |
119 | */ | |
120 | static int | |
121 | find_freehookslot(void) | |
122 | { | |
123 | int i; | |
124 | ||
125 | if((num_hooks + 1) > max_hooks) | |
126 | grow_hooktable(); | |
127 | ||
128 | for(i = 0; i < max_hooks; i++) | |
129 | { | |
130 | if(!hooks[i].name) | |
131 | return i; | |
132 | } | |
133 | ||
134 | /* shouldnt ever get here */ | |
135 | return(max_hooks - 1); | |
136 | } | |
137 | ||
138 | /* find_hook() | |
139 | * Finds an event in the hook table. | |
140 | */ | |
141 | static int | |
142 | find_hook(const char *name) | |
143 | { | |
144 | int i; | |
145 | ||
146 | for(i = 0; i < max_hooks; i++) | |
147 | { | |
148 | if(!hooks[i].name) | |
149 | continue; | |
150 | ||
151 | if(!irccmp(hooks[i].name, name)) | |
152 | return i; | |
153 | } | |
154 | ||
155 | return -1; | |
156 | } | |
157 | ||
158 | /* register_hook() | |
159 | * Finds an events position in the hook table, creating it if it doesnt | |
160 | * exist. | |
161 | */ | |
162 | int | |
163 | register_hook(const char *name) | |
164 | { | |
165 | int i; | |
166 | ||
167 | if((i = find_hook(name)) < 0) | |
168 | { | |
169 | i = find_freehookslot(); | |
170 | hooks[i].name = rb_strdup(name); | |
171 | num_hooks++; | |
172 | } | |
173 | ||
174 | return i; | |
175 | } | |
176 | ||
177 | /* add_hook() | |
178 | * Adds a hook to an event in the hook table, creating event first if | |
179 | * needed. | |
180 | */ | |
181 | void | |
182 | add_hook(const char *name, hookfn fn) | |
183 | { | |
184 | add_hook_prio(name, fn, HOOK_NORMAL); | |
185 | } | |
186 | ||
187 | /* add_hook_prio() | |
188 | * Adds a hook with the specified priority | |
189 | */ | |
190 | void | |
191 | add_hook_prio(const char *name, hookfn fn, enum hook_priority priority) | |
192 | { | |
193 | rb_dlink_node *ptr; | |
194 | struct hook_entry *entry = rb_malloc(sizeof *entry); | |
195 | int i; | |
196 | ||
197 | i = register_hook(name); | |
198 | entry->fn = fn; | |
199 | entry->priority = priority; | |
200 | ||
201 | RB_DLINK_FOREACH(ptr, hooks[i].hooks.head) | |
202 | { | |
203 | struct hook_entry *o = ptr->data; | |
204 | if (entry->priority <= o->priority) | |
205 | { | |
206 | rb_dlinkAddBefore(ptr, entry, &entry->node, &hooks[i].hooks); | |
207 | return; | |
208 | } | |
209 | } | |
210 | ||
211 | rb_dlinkAddTail(entry, &entry->node, &hooks[i].hooks); | |
212 | } | |
213 | ||
214 | /* remove_hook() | |
215 | * Removes a hook from an event in the hook table. | |
216 | */ | |
217 | void | |
218 | remove_hook(const char *name, hookfn fn) | |
219 | { | |
220 | rb_dlink_node *ptr, *scratch; | |
221 | int i; | |
222 | ||
223 | if((i = find_hook(name)) < 0) | |
224 | return; | |
225 | ||
226 | RB_DLINK_FOREACH_SAFE(ptr, scratch, hooks[i].hooks.head) | |
227 | { | |
228 | struct hook_entry *entry = ptr->data; | |
229 | if (entry->fn == fn) | |
230 | { | |
231 | rb_dlinkDelete(ptr, &hooks[i].hooks); | |
232 | return; | |
233 | } | |
234 | } | |
235 | } | |
236 | ||
237 | /* call_hook() | |
238 | * Calls functions from a given event in the hook table. | |
239 | */ | |
240 | void | |
241 | call_hook(int id, void *arg) | |
242 | { | |
243 | rb_dlink_node *ptr; | |
244 | ||
245 | /* The ID we were passed is the position in the hook table of this | |
246 | * hook | |
247 | */ | |
248 | RB_DLINK_FOREACH(ptr, hooks[id].hooks.head) | |
249 | { | |
250 | struct hook_entry *entry = ptr->data; | |
251 | entry->fn(arg); | |
252 | } | |
253 | } | |
254 |