]>
jfr.im git - solanum.git/blob - ircd/hook.c
2 * ircd-ratbox: an advanced Internet Relay Chat Daemon(ircd).
3 * hook.c - code for dealing with the hook system
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.
10 * Copyright (C) 2004-2005 Lee Hardy <lee -at- leeh.co.uk>
11 * Copyright (C) 2004-2005 ircd-ratbox development team
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions are
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.
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.
43 #define HOOK_INCREMENT 1000
49 enum hook_priority priority
;
54 int max_hooks
= HOOK_INCREMENT
;
59 int h_server_introduced
;
62 int h_after_client_exit
;
65 int h_new_remote_user
;
66 int h_introduce_client
;
69 int h_privmsg_channel
;
70 int h_conf_read_start
;
72 int h_outbound_msgbuf
;
79 hooks
= rb_malloc(sizeof(hook
) * HOOK_INCREMENT
);
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_after_client_exit
= register_hook("after_client_exit");
88 h_umode_changed
= register_hook("umode_changed");
89 h_new_local_user
= register_hook("new_local_user");
90 h_new_remote_user
= register_hook("new_remote_user");
91 h_introduce_client
= register_hook("introduce_client");
92 h_can_kick
= register_hook("can_kick");
93 h_privmsg_user
= register_hook("privmsg_user");
94 h_privmsg_channel
= register_hook("privmsg_channel");
95 h_conf_read_start
= register_hook("conf_read_start");
96 h_conf_read_end
= register_hook("conf_read_end");
97 h_outbound_msgbuf
= register_hook("outbound_msgbuf");
98 h_rehash
= register_hook("rehash");
99 h_cap_change
= register_hook("cap_change");
103 * Enlarges the hook table by HOOK_INCREMENT
110 newhooks
= rb_malloc(sizeof(hook
) * (max_hooks
+ HOOK_INCREMENT
));
111 memcpy(newhooks
, hooks
, sizeof(hook
) * num_hooks
);
115 max_hooks
+= HOOK_INCREMENT
;
118 /* find_freehookslot()
119 * Finds the next free slot in the hook table, given by an entry with
120 * h->name being NULL.
123 find_freehookslot(void)
127 if((num_hooks
+ 1) > max_hooks
)
130 for(i
= 0; i
< max_hooks
; i
++)
136 /* shouldnt ever get here */
137 return(max_hooks
- 1);
141 * Finds an event in the hook table.
144 find_hook(const char *name
)
148 for(i
= 0; i
< max_hooks
; i
++)
153 if(!irccmp(hooks
[i
].name
, name
))
161 * Finds an events position in the hook table, creating it if it doesnt
165 register_hook(const char *name
)
169 if((i
= find_hook(name
)) < 0)
171 i
= find_freehookslot();
172 hooks
[i
].name
= rb_strdup(name
);
180 * Adds a hook to an event in the hook table, creating event first if
184 add_hook(const char *name
, hookfn fn
)
186 add_hook_prio(name
, fn
, HOOK_NORMAL
);
190 * Adds a hook with the specified priority
193 add_hook_prio(const char *name
, hookfn fn
, enum hook_priority priority
)
196 struct hook_entry
*entry
= rb_malloc(sizeof *entry
);
199 i
= register_hook(name
);
201 entry
->priority
= priority
;
203 RB_DLINK_FOREACH(ptr
, hooks
[i
].hooks
.head
)
205 struct hook_entry
*o
= ptr
->data
;
206 if (entry
->priority
<= o
->priority
)
208 rb_dlinkAddBefore(ptr
, entry
, &entry
->node
, &hooks
[i
].hooks
);
213 rb_dlinkAddTail(entry
, &entry
->node
, &hooks
[i
].hooks
);
217 * Removes a hook from an event in the hook table.
220 remove_hook(const char *name
, hookfn fn
)
222 rb_dlink_node
*ptr
, *scratch
;
225 if((i
= find_hook(name
)) < 0)
228 RB_DLINK_FOREACH_SAFE(ptr
, scratch
, hooks
[i
].hooks
.head
)
230 struct hook_entry
*entry
= ptr
->data
;
233 rb_dlinkDelete(ptr
, &hooks
[i
].hooks
);
240 * Calls functions from a given event in the hook table.
243 call_hook(int id
, void *arg
)
247 /* The ID we were passed is the position in the hook table of this
250 RB_DLINK_FOREACH(ptr
, hooks
[id
].hooks
.head
)
252 struct hook_entry
*entry
= ptr
->data
;