File: linestack.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 (396 lines) | stat: -rw-r--r-- 9,851 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/* 
	ctrlproxy: A modular IRC proxy
	(c) 2002-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"

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

#include "irc.h"

static GSList *linestack_backends = NULL;

void register_linestack(const struct linestack_ops *b)
{
	linestack_backends = g_slist_append(linestack_backends, g_memdup(b, sizeof(*b)));
}

struct linestack_context *create_linestack(const struct linestack_ops *ops, 
										   const char *name, 
										   struct ctrlproxy_config *cfg,
										   const struct irc_network_state *state)
{
	struct linestack_context *ctx;

	g_assert(name);
	g_assert(state);
	g_assert(cfg);

	ctx = g_new0(struct linestack_context, 1);
	ctx->ops = ops;
	ops->init(ctx, name, cfg, state);

	return ctx;
}

void free_linestack_context(struct linestack_context *ctx)
{
	if (ctx)
		ctx->ops->fini(ctx);

	g_free(ctx);
}

static struct linestack_marker *wrap_linestack_marker(struct linestack_context *ctx, void *data)
{
	struct linestack_marker *mrk;
	if (data == NULL)
		return NULL;

	mrk = g_new0(struct linestack_marker, 1);
	mrk->free_fn = ctx->ops->free_marker;
	mrk->data = data;
	return mrk;
}

struct linestack_marker *linestack_get_marker_numlines (struct linestack_context *ctx, int lines)
{
	g_assert(ctx != NULL);
	if (ctx->ops == NULL) 
		return NULL;

	if (ctx->ops->get_marker_numlines == NULL) 
		return NULL;

	return wrap_linestack_marker(ctx, ctx->ops->get_marker_numlines(ctx, lines));
}

struct irc_network_state *linestack_get_state(
		struct linestack_context *ctx, struct linestack_marker *lm)
{
	struct irc_network_state *st;
	g_assert(ctx != NULL);

	if (!ctx->ops) return NULL;
	if (!ctx->ops->get_state) return NULL;

	st = ctx->ops->get_state(ctx, lm?lm->data:NULL);
	if (st == NULL)
		return NULL;

	g_assert(st->me.nick);
	g_assert(st->me.query);
	return st;
}

gboolean linestack_traverse(struct linestack_context *ctx,					
		struct linestack_marker *lm_from, struct linestack_marker *lm_to,
		linestack_traverse_fn handler, void *userdata)
{
	g_assert(ctx != NULL);
	g_assert(ctx->ops != NULL);
	g_assert(ctx->ops->traverse != NULL);

	return ctx->ops->traverse(ctx, lm_from?lm_from->data:NULL, lm_to?lm_to->data:NULL, handler, userdata);
}

struct traverse_object_data {
	linestack_traverse_fn handler;
	const char *object;
	void *userdata;
};

static gboolean traverse_object_handler(struct irc_line *l, time_t t, void *state)
{
	struct traverse_object_data *d = state;
	gboolean ret = TRUE;

	if (l->argc < 2) 
		return TRUE;

	if (strchr(l->args[1], ',') == NULL) {
		if (!strcmp(l->args[1], d->object))
			ret &= d->handler(l, t, d->userdata);
	} else {
		int i;
		char **channels = g_strsplit(l->args[1], ",", 0);
		for (i = 0; channels[i]; i++) {
			if (!strcmp(channels[i], d->object)) {
				gboolean ret = d->handler(l, t, d->userdata);
				g_strfreev(channels);
				return ret;
			}
		}
		g_strfreev(channels);
	}

	return ret;
}

gboolean linestack_traverse_object(
			struct linestack_context *ctx,
			const char *obj, 
			struct linestack_marker *lm_from, 
			struct linestack_marker *lm_to, linestack_traverse_fn hl,
			void *userdata)
{
	struct traverse_object_data d;
	g_assert(ctx != NULL);
	g_assert(ctx->ops != NULL);

	d.object = obj;
	d.userdata = userdata;
	d.handler = hl;
	
	return linestack_traverse(ctx, lm_from, lm_to, traverse_object_handler, &d);
}

void linestack_free_marker(struct linestack_marker *lm)
{
	if (lm == NULL)
		return;

	if (lm->free_fn != NULL) 
		lm->free_fn(lm->data);
	g_free(lm);
}

struct linestack_marker *linestack_get_marker(struct linestack_context *ctx)
{
	if (ctx == NULL)
		return NULL;

	g_assert (ctx->ops != NULL);
	g_assert (ctx->ops->get_marker != NULL);

	return wrap_linestack_marker(ctx, ctx->ops->get_marker(ctx));
}

static const char *linestack_messages[] = { 
	"NICK", "JOIN", "QUIT", "PART", "PRIVMSG", "NOTICE", "KICK", 
	"MODE", "TOPIC", 
	"353", /* RPL_NAMREPLY */
	"366", /* RPL_ENDOFNAMES */
	"331", /* RPL_NOTOPIC */
	"333", /* RPL_TOPICWHOTIME */
	"332", /* RPL_TOPIC */
	"324", /* RPL_CHANNELMODEIS */
	"329", /* RPL_CREATIONTIME */
	NULL };

gboolean linestack_insert_line(struct linestack_context *ctx, 
							   const struct irc_line *l, enum data_direction dir, 
							   const struct irc_network_state *state)
{
	int i;
	gboolean needed = FALSE;

	if (ctx == NULL) return TRUE;

	if (l->argc == 0) return TRUE;

	if (!ctx->ops) return TRUE;
	g_assert(ctx->ops->insert_line);

	/* Only need PRIVMSG and NOTICE messages we send ourselves */
	if (dir == TO_SERVER && 
		g_strcasecmp(l->args[0], "PRIVMSG") && 
		g_strcasecmp(l->args[0], "NOTICE")) return TRUE;

	/* No CTCP, please */
	if ((!g_strcasecmp(l->args[0], "PRIVMSG") ||
		!g_strcasecmp(l->args[0], "NOTICE")) && 
		l->argc > 2 && l->args[2][0] == '\001' && 
		g_strncasecmp(l->args[2], "\001ACTION", 7) != 0)
		return TRUE;

	for (i = 0; linestack_messages[i]; i++) 
		if (!g_strcasecmp(linestack_messages[i], l->args[0]))
			needed = TRUE;

	if (!needed) return TRUE;

	for (i = 0; i < l->argc; i++) {
		g_assert(strchr(l->args[i], '\n') == NULL);
		g_assert(strchr(l->args[i], '\r') == NULL);
	}

	return ctx->ops->insert_line(ctx, l, state);
}

struct send_line_privdata {
	int time_offset;
	struct irc_client *client;
};

static gboolean send_line(struct irc_line *l, time_t t, void *_privdata)
{
	struct send_line_privdata *privdata = _privdata;
	return client_send_line(privdata->client, l);
}

static gboolean send_line_timed(struct irc_line *l, time_t t, void *_privdata)
{
	struct send_line_privdata *privdata = _privdata;

	if ((!g_strcasecmp(l->args[0], "PRIVMSG") ||
		!g_strcasecmp(l->args[0], "NOTICE")) &&
		l->argc > 2) {
		struct irc_line *nl = line_prefix_time(l, t+privdata->time_offset);
		gboolean ret;
		ret = client_send_line(privdata->client, nl);
		free_line(nl);
		return ret;
	} else {
		return client_send_line(privdata->client, l);
	}
}

static gboolean send_line_timed_dataonly(struct irc_line *l, time_t t, void *_privdata)
{
	struct send_line_privdata *privdata = _privdata;
	gboolean ret;
	struct irc_line *nl;

	if (g_strcasecmp(l->args[0], "PRIVMSG") != 0 && 
		g_strcasecmp(l->args[0], "NOTICE") != 0)
		return TRUE;

	if (l->argc <= 2)
		return TRUE;

	nl = line_prefix_time(l, t+privdata->time_offset);
	ret = client_send_line(privdata->client, nl);
	free_line(nl);
	return ret;
}

static gboolean send_line_dataonly(struct irc_line *l, time_t t, void *_privdata)
{
	struct send_line_privdata *privdata = _privdata;

	if (g_strcasecmp(l->args[0], "PRIVMSG") != 0 && 
		g_strcasecmp(l->args[0], "NOTICE") != 0)
		return TRUE;

	if (l->argc <= 2)
		return TRUE;

	return client_send_line(privdata->client, l);
}

gboolean linestack_send(struct linestack_context *ctx, struct linestack_marker *mf, struct linestack_marker *mt, struct irc_client *c, gboolean dataonly, gboolean timed, int time_offset)
{
	struct send_line_privdata privdata;
	linestack_traverse_fn trav_fn;

	privdata.client = c;
	privdata.time_offset = time_offset;

	if (dataonly) {
		if (timed) 
			trav_fn = send_line_timed_dataonly;
		else 
			trav_fn = send_line_dataonly;
	} else {
		if (timed)
			trav_fn = send_line_timed;
		else
			trav_fn = send_line;
	}

	return linestack_traverse(ctx, mf, mt, trav_fn, &privdata);
}

gboolean linestack_send_object(struct linestack_context *ctx, const char *obj, struct linestack_marker *mf, struct linestack_marker *mt, struct irc_client *c, gboolean dataonly, gboolean timed, int time_offset)
{
	struct send_line_privdata privdata;
	linestack_traverse_fn trav_fn;

	privdata.client = c;
	privdata.time_offset = time_offset;

	if (dataonly) {
		if (timed) 
			trav_fn = send_line_timed_dataonly;
		else 
			trav_fn = send_line_dataonly;
	} else {
		if (timed)
			trav_fn = send_line_timed;
		else
			trav_fn = send_line;
	}

	return linestack_traverse_object(ctx, obj, mf, mt, trav_fn, &privdata);
}

static gboolean replay_line(struct irc_line *l, time_t t, void *state)
{
	struct irc_network_state *st = state;
	state_handle_data(st, l);
	return TRUE;
}

gboolean linestack_replay(struct linestack_context *ctx, 
						  struct linestack_marker *mf, 
						  struct linestack_marker *mt, 
						  struct irc_network_state *st)
{
	return linestack_traverse(ctx, mf, mt, replay_line, st);
}

struct linestack_ops *linestack_find_ops(const char *name)
{
	GSList *gl;
	for (gl = linestack_backends; gl ; gl = gl->next) {
		struct linestack_ops *ops = gl->data;
		if (!strcmp(ops->name, name))
			return ops;
	}

	return NULL;
}

struct linestack_context *new_linestack(struct irc_network *n)
{
	const struct linestack_ops *current_backend = NULL;
	struct ctrlproxy_config *cfg = NULL;
	
	if (n->global != NULL)
		cfg = n->global->config;

	register_linestack(&linestack_file);

	if (cfg && cfg->linestack_backend) {
		current_backend = linestack_find_ops(cfg->linestack_backend);

		if (!current_backend) 
			log_global(LOG_WARNING, "Unable to find linestack backend %s: falling back to default", cfg->linestack_backend);
	}

	if (!current_backend) {
		current_backend = &linestack_file;
	}

	return create_linestack(current_backend, n->info->name, cfg, n->state);
}