ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/branches/8.1.x/src/hook.c
Revision: 1867
Committed: Thu Apr 25 18:01:53 2013 UTC (10 years, 11 months ago) by michael
Content type: text/x-csrc
File size: 5831 byte(s)
Log Message:
- Create 8.1.x branch

File Contents

# Content
1 /*
2 * ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3 *
4 * Copyright (C) 2003 Piotr Nizynski, Advanced IRC Services Project Team
5 * Copyright (C) 2005 Hybrid Development Team
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 * USA
21 *
22 * $Id$
23 */
24
25 /*! \file hook.c
26 * \brief Provides a generic event hooking interface.
27 * \version $Id$
28 */
29
30 #include "stdinc.h"
31 #include "list.h"
32 #include "hook.h"
33 #include "ircd.h"
34 #include "memory.h"
35 #include "numeric.h"
36 #include "irc_string.h"
37 #include "send.h"
38 #include "client.h"
39
40 static dlink_list callback_list = { NULL, NULL, 0} ;
41
42 /*! \brief Creates a new callback.
43 * \param name name used to identify the callback
44 * (can be NULL for anonymous callbacks)
45 * \param func initial function attached to the chain
46 * (can be NULL to create an empty chain)
47 * \return pointer to Callback structure or NULL if already exists
48 * \note Once registered, a callback should never be freed!
49 * That's because there may be modules which depend on it
50 * (even if no functions are attached). That's also why
51 * we dynamically allocate the struct here -- we don't want
52 * to allow anyone to place it in module data, which can be
53 * unloaded at any time.
54 */
55 struct Callback *
56 register_callback(const char *name, CBFUNC *func)
57 {
58 struct Callback *cb = NULL;
59
60 if (name != NULL)
61 {
62 if ((cb = find_callback(name)) != NULL)
63 {
64 if (func != NULL)
65 dlinkAddTail(func, MyMalloc(sizeof(dlink_node)), &cb->chain);
66
67 return cb;
68 }
69 }
70
71 cb = MyMalloc(sizeof(struct Callback));
72 if (func != NULL)
73 dlinkAdd(func, MyMalloc(sizeof(dlink_node)), &cb->chain);
74
75 if (name != NULL)
76 {
77 cb->name = xstrdup(name);
78 dlinkAdd(cb, &cb->node, &callback_list);
79 }
80
81 return cb;
82 }
83
84 /*! \brief Passes control down the callback hook chain.
85 * \param cb pointer to Callback structure
86 * \param ... argument to pass
87 * \return function return value
88 */
89 void *
90 execute_callback(struct Callback *cb, ...)
91 {
92 void *res;
93 va_list args;
94
95 cb->called++;
96 cb->last = CurrentTime;
97
98 if (!is_callback_present(cb))
99 return NULL;
100
101 va_start(args, cb);
102 res = ((CBFUNC *) cb->chain.head->data)(args);
103 va_end(args);
104
105 return res;
106 }
107
108 /*! \brief Called by a hook function to pass code flow further
109 * in the hook chain.
110 * \param this_hook pointer to dlink_node of the current hook function
111 * \param ... (original or modified) arguments to be passed
112 * \return callback return value
113 */
114 void *
115 pass_callback(dlink_node *this_hook, ...)
116 {
117 void *res;
118 va_list args;
119
120 if (this_hook->next == NULL)
121 return NULL; /* reached the last one */
122
123 va_start(args, this_hook);
124 res = ((CBFUNC *) this_hook->next->data)(args);
125 va_end(args);
126
127 return res;
128 }
129
130 /*! \brief Finds a named callback.
131 * \param name name of the callback
132 * \return pointer to Callback structure or NULL if not found
133 */
134 struct Callback *
135 find_callback(const char *name)
136 {
137 dlink_node *ptr;
138
139 DLINK_FOREACH(ptr, callback_list.head)
140 {
141 struct Callback *cb = ptr->data;
142
143 if (!irccmp(cb->name, name))
144 return cb;
145 }
146
147 return NULL;
148 }
149
150 /*! \brief Installs a hook for the given callback.
151 *
152 * The new hook is installed at the beginning of the chain,
153 * so it has full control over functions installed earlier.
154 *
155 * \param cb pointer to Callback structure
156 * \param hook address of hook function
157 * \return pointer to dlink_node of the hook (used when passing
158 * control to the next hook in the chain);
159 * valid till uninstall_hook() is called
160 */
161 dlink_node *
162 install_hook(struct Callback *cb, CBFUNC *hook)
163 {
164 dlink_node *node = MyMalloc(sizeof(dlink_node));
165
166 dlinkAdd(hook, node, &cb->chain);
167 return node;
168 }
169
170 /*! \brief Removes a specific hook for the given callback.
171 * \param cb pointer to Callback structure
172 * \param hook address of hook function
173 */
174 void
175 uninstall_hook(struct Callback *cb, CBFUNC *hook)
176 {
177 /* let it core if not found */
178 dlink_node *ptr = dlinkFind(&cb->chain, hook);
179
180 dlinkDelete(ptr, &cb->chain);
181 MyFree(ptr);
182 }
183
184 /*! \brief Displays registered callbacks and lengths of their hook chains.
185 * (This is the handler of /stats h)
186 * \param source_p pointer to struct Client
187 */
188 void
189 stats_hooks(struct Client *source_p)
190 {
191 dlink_node *ptr;
192 char lastused[32];
193
194 sendto_one(source_p, ":%s %d %s : %-20s %-20s Used Hooks", me.name,
195 RPL_STATSDEBUG, source_p->name, "Callback", "Last Execution");
196 sendto_one(source_p, ":%s %d %s : ------------------------------------"
197 "--------------------", me.name, RPL_STATSDEBUG, source_p->name);
198
199 DLINK_FOREACH(ptr, callback_list.head)
200 {
201 struct Callback *cb = ptr->data;
202
203 if (cb->last != 0)
204 snprintf(lastused, sizeof(lastused), "%d seconds ago",
205 (int) (CurrentTime - cb->last));
206 else
207 strcpy(lastused, "NEVER");
208
209 sendto_one(source_p, ":%s %d %s : %-20s %-20s %-8u %d", me.name,
210 RPL_STATSDEBUG, source_p->name, cb->name, lastused, cb->called,
211 dlink_list_length(&cb->chain));
212 }
213
214 sendto_one(source_p, ":%s %d %s : ", me.name, RPL_STATSDEBUG,
215 source_p->name);
216 }

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision