1 |
pippijn |
1.1 |
/* |
2 |
|
|
* hook.C: Hook system. |
3 |
pippijn |
1.2 |
* Rights to this code are documented in doc/pod/license.pod. |
4 |
pippijn |
1.1 |
* |
5 |
|
|
* Copyright © 2005-2007 Atheme Project (http://www.atheme.org) |
6 |
|
|
*/ |
7 |
|
|
|
8 |
pippijn |
1.3 |
static char const rcsid[] = "$Id$"; |
9 |
pippijn |
1.1 |
|
10 |
|
|
#include "atheme.h" |
11 |
|
|
#include "internal.h" |
12 |
|
|
|
13 |
|
|
list_t hooks; |
14 |
|
|
static BlockHeap *hook_heap; |
15 |
|
|
static hook_t *find_hook (const char *name); |
16 |
|
|
|
17 |
|
|
void |
18 |
|
|
hooks_init () |
19 |
|
|
{ |
20 |
|
|
hook_heap = BlockHeapCreate (sizeof (hook_t), 1024); |
21 |
|
|
|
22 |
|
|
if (!hook_heap) |
23 |
|
|
{ |
24 |
|
|
slog (LG_INFO, "hooks_init(): block allocator failed."); |
25 |
|
|
exit (EXIT_SUCCESS); |
26 |
|
|
} |
27 |
|
|
} |
28 |
|
|
|
29 |
|
|
static hook_t * |
30 |
|
|
new_hook (const char *name) |
31 |
|
|
{ |
32 |
|
|
hook_t *h; |
33 |
|
|
|
34 |
|
|
h = static_cast<hook_t *> (BlockHeapAlloc (hook_heap)); |
35 |
|
|
h->name = sstrdup (name); |
36 |
|
|
|
37 |
|
|
return h; |
38 |
|
|
} |
39 |
|
|
|
40 |
|
|
void |
41 |
|
|
hook_add_event (const char *name) |
42 |
|
|
{ |
43 |
|
|
hook_t *nh; |
44 |
|
|
node_t *n; |
45 |
|
|
|
46 |
|
|
if (find_hook (name)) |
47 |
|
|
return; |
48 |
|
|
|
49 |
|
|
nh = new_hook (name); |
50 |
|
|
n = node_create (); |
51 |
|
|
|
52 |
|
|
node_add (nh, n, &hooks); |
53 |
|
|
} |
54 |
|
|
|
55 |
|
|
void |
56 |
|
|
hook_del_event (const char *name) |
57 |
|
|
{ |
58 |
|
|
node_t *n; |
59 |
|
|
hook_t *h; |
60 |
|
|
|
61 |
|
|
LIST_FOREACH (n, hooks.head) |
62 |
|
|
{ |
63 |
|
|
h = static_cast<hook_t *> (n->data); |
64 |
|
|
|
65 |
|
|
if (!strcmp (h->name, name)) |
66 |
|
|
{ |
67 |
|
|
node_del (n, &hooks); |
68 |
|
|
BlockHeapFree (hook_heap, h); |
69 |
|
|
return; |
70 |
|
|
} |
71 |
|
|
} |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
static hook_t * |
75 |
|
|
find_hook (const char *name) |
76 |
|
|
{ |
77 |
|
|
node_t *n; |
78 |
|
|
hook_t *h; |
79 |
|
|
|
80 |
|
|
LIST_FOREACH (n, hooks.head) |
81 |
|
|
{ |
82 |
|
|
h = static_cast<hook_t *> (n->data); |
83 |
|
|
|
84 |
|
|
if (!strcmp (h->name, name)) |
85 |
|
|
return h; |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
return NULL; |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
void |
92 |
|
|
hook_del_hook (const char *event, void (*handler) (void *data)) |
93 |
|
|
{ |
94 |
|
|
node_t *n, *n2; |
95 |
|
|
hook_t *h; |
96 |
|
|
|
97 |
|
|
if (!(h = find_hook (event))) |
98 |
|
|
return; |
99 |
|
|
|
100 |
|
|
LIST_FOREACH_SAFE (n, n2, h->hooks.head) |
101 |
|
|
{ |
102 |
|
|
if (handler == (void (*)(void *)) n->data) |
103 |
|
|
{ |
104 |
|
|
node_del (n, &h->hooks); |
105 |
|
|
node_free (n); |
106 |
|
|
} |
107 |
|
|
} |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
void |
111 |
|
|
hook_add_hook (const char *event, void (*handler) (void *data)) |
112 |
|
|
{ |
113 |
|
|
hook_t *h; |
114 |
|
|
node_t *n; |
115 |
|
|
|
116 |
|
|
if (!(h = find_hook (event))) |
117 |
|
|
return; |
118 |
|
|
|
119 |
|
|
n = node_create (); |
120 |
|
|
|
121 |
|
|
node_add ((void *) handler, n, &h->hooks); |
122 |
|
|
} |
123 |
|
|
|
124 |
|
|
void |
125 |
|
|
hook_add_hook_first (const char *event, void (*handler) (void *data)) |
126 |
|
|
{ |
127 |
|
|
hook_t *h; |
128 |
|
|
node_t *n; |
129 |
|
|
|
130 |
|
|
if (!(h = find_hook (event))) |
131 |
|
|
return; |
132 |
|
|
|
133 |
|
|
n = node_create (); |
134 |
|
|
|
135 |
|
|
node_add_head ((void *) handler, n, &h->hooks); |
136 |
|
|
} |
137 |
|
|
|
138 |
|
|
void |
139 |
|
|
hook_call_event (const char *event, void *dptr) |
140 |
|
|
{ |
141 |
|
|
hook_t *h; |
142 |
|
|
node_t *n, *tn; |
143 |
|
|
void (*func) (void *data); |
144 |
|
|
|
145 |
|
|
if (!(h = find_hook (event))) |
146 |
|
|
return; |
147 |
|
|
|
148 |
|
|
LIST_FOREACH_SAFE (n, tn, h->hooks.head) |
149 |
|
|
{ |
150 |
|
|
func = (void (*)(void *)) n->data; |
151 |
|
|
func (dptr); |
152 |
|
|
} |
153 |
|
|
} |