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.
37 * $Id: hook.c 26 2006-09-20 18:02:06Z spb $
43 #include "irc_string.h"
47 #define HOOK_INCREMENT 1000
51 int max_hooks
= HOOK_INCREMENT
;
53 #ifdef USE_IODEBUG_HOOKS
61 int h_server_introduced
;
65 int h_new_remote_user
;
66 int h_introduce_client
;
71 hooks
= MyMalloc(sizeof(hook
) * HOOK_INCREMENT
);
73 #ifdef USE_IODEBUG_HOOKS
74 h_iosend_id
= register_hook("iosend");
75 h_iorecv_id
= register_hook("iorecv");
76 h_iorecvctrl_id
= register_hook("iorecvctrl");
79 h_burst_client
= register_hook("burst_client");
80 h_burst_channel
= register_hook("burst_channel");
81 h_burst_finished
= register_hook("burst_finished");
82 h_server_introduced
= register_hook("server_introduced");
83 h_server_eob
= register_hook("server_eob");
84 h_client_exit
= register_hook("client_exit");
85 h_umode_changed
= register_hook("umode_changed");
86 h_new_local_user
= register_hook("new_local_user");
87 h_new_remote_user
= register_hook("new_remote_user");
88 h_introduce_client
= register_hook("introduce_client");
92 * Enlarges the hook table by HOOK_INCREMENT
99 newhooks
= MyMalloc(sizeof(hook
) * (max_hooks
+ HOOK_INCREMENT
));
100 memcpy(newhooks
, hooks
, sizeof(hook
) * num_hooks
);
104 max_hooks
+= HOOK_INCREMENT
;
107 /* find_freehookslot()
108 * Finds the next free slot in the hook table, given by an entry with
109 * h->name being NULL.
112 find_freehookslot(void)
116 if((num_hooks
+ 1) > max_hooks
)
119 for(i
= 0; i
< max_hooks
; i
++)
125 /* shouldnt ever get here */
126 return(max_hooks
- 1);
130 * Finds an event in the hook table.
133 find_hook(const char *name
)
137 for(i
= 0; i
< max_hooks
; i
++)
142 if(!irccmp(hooks
[i
].name
, name
))
150 * Finds an events position in the hook table, creating it if it doesnt
154 register_hook(const char *name
)
158 if((i
= find_hook(name
)) < 0)
160 i
= find_freehookslot();
161 DupString(hooks
[i
].name
, name
);
169 * Adds a hook to an event in the hook table, creating event first if
173 add_hook(const char *name
, hookfn fn
)
177 i
= register_hook(name
);
179 dlinkAddAlloc(fn
, &hooks
[i
].hooks
);
183 * Removes a hook from an event in the hook table.
186 remove_hook(const char *name
, hookfn fn
)
190 if((i
= find_hook(name
)) < 0)
193 dlinkFindDestroy(fn
, &hooks
[i
].hooks
);
197 * Calls functions from a given event in the hook table.
200 call_hook(int id
, void *arg
)
205 /* The ID we were passed is the position in the hook table of this
208 DLINK_FOREACH(ptr
, hooks
[id
].hooks
.head
)