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