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