File: hooks.c

package info (click to toggle)
ctrlproxy 3.0.7-1
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 1,668 kB
  • ctags: 2,045
  • sloc: ansic: 15,358; sh: 2,805; xml: 1,078; python: 346; makefile: 302; perl: 18
file content (327 lines) | stat: -rw-r--r-- 7,798 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* 
	ctrlproxy: A modular IRC proxy
	(c) 2003 Jelmer Vernooij <jelmer@nl.linux.org>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "internals.h"

struct filter_data {
	char *name;
	int priority;
	server_filter_function function;
	void *userdata;
};

struct client_filter_data {
	char *name;
	int priority;
	client_filter_function function;
	void *userdata;
};

static gint filter_cmp(gconstpointer _a, gconstpointer _b)
{
	struct filter_data *a = (struct filter_data *)_a;
	struct filter_data *b = (struct filter_data *)_b;

	return a->priority - b->priority;
}

static GList *add_filter_ex(GList *class, const char *name, server_filter_function f, void *userdata, int prio)
{
	struct filter_data *d = (struct filter_data *)g_malloc(sizeof(struct filter_data));

	d->name = g_strdup(name);
	d->function = f;
	d->priority = prio;
	d->userdata = userdata;

	return g_list_insert_sorted(class, d, filter_cmp);
}

static GList *del_filter_ex(GList *list, const char *name)
{
	GList *gl;
	
	for (gl = list; gl; gl = gl->next)
	{
		struct filter_data *d = (struct filter_data *)gl->data;

		if (!g_strcasecmp(d->name, name)) 
		{
			g_free(d->name);
			g_free(d);
			return g_list_remove(list, d);
		}
	}

	return list;
}


static gboolean filter_class_execute(GList *gl, struct irc_network *s, enum data_direction dir, const struct irc_line *l) 
{
	while(gl) {
		struct filter_data *d = (struct filter_data *)gl->data;
		
		if (!d->function(s, l, dir, d->userdata)) {
			return FALSE;
		}

		gl = gl->next;
	}
	
	return TRUE;
}

static GList *log_filters = NULL, 
			 *replication_filters = NULL, 
			 *client_filters = NULL, 
			 *server_filters = NULL;

#define FILTER_FUNCTIONS(n,list) \
void add_##n##_filter(const char *name, server_filter_function f, void *userdata, int priority)\
{\
	list = add_filter_ex(list, name, f, userdata, priority);\
}\
\
void del_##n##_filter(const char *name)\
{\
	list = del_filter_ex(list, name); \
}\
gboolean run_##n##_filter(struct irc_network *s, const struct irc_line *l, enum data_direction dir)\
{\
	return filter_class_execute(list, s, dir, l);\
}

FILTER_FUNCTIONS(log,log_filters)
FILTER_FUNCTIONS(replication,replication_filters)
FILTER_FUNCTIONS(server,server_filters)

void add_client_filter(const char *name, client_filter_function f, void *userdata, int priority)
{
	client_filters = add_filter_ex(client_filters, name, (server_filter_function)f, userdata, priority);
}

void del_client_filter(const char *name)
{
	client_filters = del_filter_ex(client_filters, name); 
}

gboolean run_client_filter(struct irc_client *c, const struct irc_line *l, enum data_direction dir)
{
	GList *gl;
	
	for (gl = client_filters; gl; gl = gl->next)
	{
		struct client_filter_data *d = (struct client_filter_data *)gl->data;
		
		if (!d->function(c, l, dir, d->userdata)) {
			return FALSE;
		}
	}
	
	return TRUE;
}

/* Hooks that are called when a client is added or removed. */

struct new_client_hook_data {
	char *name;
	new_client_hook hook;
	void *userdata;
};

GList *new_client_hooks = NULL;

struct lose_client_hook_data {
	char *name;
	lose_client_hook hook;
	void *userdata;
};

GList *lose_client_hooks = NULL;

void add_new_client_hook(const char *name, new_client_hook h, void *userdata)
{
	struct new_client_hook_data *d;
	
	d = g_malloc(sizeof(struct new_client_hook_data));
	d->name = g_strdup(name);
	d->userdata = userdata;
	d->hook = h;
	new_client_hooks = g_list_append(new_client_hooks, d);
}

void del_new_client_hook(const char *name)
{
	GList *l;
	
	for (l = new_client_hooks; l; l = l->next)
	{
		struct new_client_hook_data *d = (struct new_client_hook_data *)l->data;
		if (!strcmp(d->name, name)) {
			g_free(d->name);
			g_free(d);
			new_client_hooks = g_list_remove(new_client_hooks, d);
			return;
		}
	}
}

gboolean new_client_hook_execute(struct irc_client *c)
{
	GList *l;
	
	for (l = new_client_hooks; l; l = l->next)
	{
		struct new_client_hook_data *d = (struct new_client_hook_data *)l->data;
	
		if (!d->hook(c, d->userdata)) {
			g_debug(("New client hook '%s' refused new client"), d->name);
			return FALSE;
		}
	}

	return TRUE;
}

void add_lose_client_hook(const char *name, lose_client_hook h, void *userdata)
{
	struct lose_client_hook_data *d;
	
	d = g_malloc(sizeof(struct lose_client_hook_data));
	d->name = g_strdup(name);
	d->hook = h;
	d->userdata = userdata;
	lose_client_hooks = g_list_append(lose_client_hooks, d);
}

void del_lose_client_hook(const char *name)
{
	GList *l;
	for (l = lose_client_hooks; l; l = l->next)
	{
		struct lose_client_hook_data *d = (struct lose_client_hook_data *)l->data;
		if (!strcmp(d->name, name)) {
			g_free(d->name);
			lose_client_hooks = g_list_remove(lose_client_hooks, d);
			g_free(d);
			return;
		}
	}
}

void lose_client_hook_execute(struct irc_client *c)
{
	GList *l;
	
	for (l = lose_client_hooks; l; l = l->next)
	{
		struct lose_client_hook_data *d = (struct lose_client_hook_data *)l->data;
		d->hook(c, d->userdata);
	}
}

struct server_connected_hook_data {
	char *name;
	server_connected_hook hook;
	void *userdata;
};

GList *server_connected_hooks = NULL;

void add_server_connected_hook(const char *name, server_connected_hook h, void *userdata)
{
	struct server_connected_hook_data *d;
	
	d = g_malloc(sizeof(struct server_connected_hook_data));
	d->name = g_strdup(name);
	d->hook = h;
	d->userdata = userdata;
	server_connected_hooks = g_list_append(server_connected_hooks, d);
}

void del_server_connected_hook(const char *name)
{
	GList *l;
	for (l = server_connected_hooks; l; l = l->next) 
	{
		struct server_connected_hook_data *d = (struct server_connected_hook_data *)l->data;
		if (!strcmp(d->name, name)) {
			g_free(d->name);
			g_free(d);
			server_connected_hooks = g_list_remove(server_connected_hooks, d);
			return;
		}
	}
}

void server_connected_hook_execute(struct irc_network *c)
{
	GList *l;
	
	for (l = server_connected_hooks; l; l = l->next)
	{
		struct server_connected_hook_data *d = (struct server_connected_hook_data *)l->data;
		d->hook(c, d->userdata);
	}
}

struct server_disconnected_hook_data {
	char *name;
	server_disconnected_hook hook;
	void *userdata;
};

GList *server_disconnected_hooks = NULL;

void add_server_disconnected_hook(const char *name, server_disconnected_hook h, void *userdata)
{
	struct server_disconnected_hook_data *d;
	
	d = g_malloc(sizeof(struct server_disconnected_hook_data));
	d->name = g_strdup(name);
	d->hook = h;
	d->userdata = userdata;
	server_disconnected_hooks = g_list_append(server_disconnected_hooks, d);
}

void del_server_disconnected_hook(const char *name)
{
	GList *l;
	for (l = server_disconnected_hooks; l; l = l->next) 
	{
		struct server_disconnected_hook_data *d = (struct server_disconnected_hook_data *)l->data;
		if (!strcmp(d->name, name)) {
			g_free(d->name);
			g_free(d);
			server_disconnected_hooks = g_list_remove(server_disconnected_hooks, d);
			return;
		}
	}
}

void server_disconnected_hook_execute(struct irc_network *c)
{
	GList *l;
	for (l = server_disconnected_hooks; l; l = l->next) 
	{
		struct server_disconnected_hook_data *d = (struct server_disconnected_hook_data *)l->data;
		d->hook(c, d->userdata);
	}
}