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