File: channels.c

package info (click to toggle)
atheme-services 7.2.9-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 13,992 kB
  • sloc: ansic: 93,230; sh: 7,440; php: 5,032; perl: 3,327; makefile: 1,261; sed: 16; ruby: 15; python: 3
file content (530 lines) | stat: -rw-r--r-- 12,299 bytes parent folder | download | duplicates (2)
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*
 * atheme-services: A collection of minimalist IRC services
 * channels.c: Channel event and state tracking
 *
 * Copyright (c) 2005-2007 Atheme Project (http://www.atheme.org)
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "atheme.h"

mowgli_patricia_t *chanlist;

mowgli_heap_t *chan_heap;
mowgli_heap_t *chanuser_heap;
mowgli_heap_t *chanban_heap;

/*
 * init_channels()
 *
 * Initializes the channel-related heaps and DTree structures.
 *
 * Inputs:
 *     - nothing
 *
 * Outputs:
 *     - nothing
 *
 * Side Effects:
 *     - if the heaps or DTrees fail to initialize, the program will abort.
 */
void init_channels(void)
{
	chan_heap = sharedheap_get(sizeof(channel_t));
	chanuser_heap = sharedheap_get(sizeof(chanuser_t));
	chanban_heap = sharedheap_get(sizeof(chanban_t));

	if (chan_heap == NULL || chanuser_heap == NULL || chanban_heap == NULL)
	{
		slog(LG_INFO, "init_channels(): block allocator failure.");
		exit(EXIT_FAILURE);
	}

	chanlist = mowgli_patricia_create(irccasecanon);
}

/*
 * channel_add(const char *name, time_t ts, server_t *creator)
 *
 * Channel object factory.
 *
 * Inputs:
 *     - channel name
 *     - timestamp of channel creation
 *     - server that is creating the channel
 *
 * Outputs:
 *     - on success, a channel object
 *     - on failure, NULL
 *
 * Side Effects:
 *     - the channel is automatically inserted into the channel DTree
 *     - if the creator is not me.me:
 *       - channel_add hook is called
 *       - all services are joined if this is the snoop channel
 *     - if the creator is me.me these actions must be performed by the
 *       caller (i.e. join()) after joining the service
 */
channel_t *channel_add(const char *name, time_t ts, server_t *creator)
{
	channel_t *c;
	mychan_t *mc;

	if (!VALID_GLOBAL_CHANNEL_PFX(name))
	{
		slog(LG_DEBUG, "channel_add(): got channel with invalid global prefix: %s", name);
		return NULL;
	}

	c = channel_find(name);

	if (c)
	{
		slog(LG_DEBUG, "channel_add(): channel already exists: %s", name);
		return c;
	}

	slog(LG_DEBUG, "channel_add(): %s by %s", name, creator->name);

	c = mowgli_heap_alloc(chan_heap);

	c->name = sstrdup(name);
	c->ts = ts;

	c->topic = NULL;
	c->topic_setter = NULL;

	if (ignore_mode_list_size != 0)
		c->extmodes = scalloc(sizeof(char *), ignore_mode_list_size);

	c->bans.head = NULL;
	c->bans.tail = NULL;
	c->bans.count = 0;

	if ((mc = mychan_find(c->name)))
		mc->chan = c;

	mowgli_patricia_add(chanlist, c->name, c);

	cnt.chan++;

	if (creator != me.me)
	{
		hook_call_channel_add(c);

	}

	return c;
}

/*
 * channel_delete(channel_t *c)
 *
 * Destroys a channel object and its children member objects.
 *
 * Inputs:
 *     - channel object to destroy
 *
 * Outputs:
 *     - nothing
 *
 * Side Effects:
 *     - channel_delete hook is called
 *     - a channel and all attached structures are destroyed
 *     - no protocol messages are sent for any remaining members
 */
void channel_delete(channel_t *c)
{
	mychan_t *mc;
	mowgli_node_t *n, *tn;
	chanuser_t *cu;

	return_if_fail(c != NULL);

	slog(LG_DEBUG, "channel_delete(): %s", c->name);

	modestack_finalize_channel(c);

	/* If this is called from uplink_close(), there may still be services
	 * in the channel. Remove them. Calling chanuser_delete() could lead
	 * to a recursive call, so don't do that.
	 * -- jilles */
	MOWGLI_ITER_FOREACH_SAFE(n, tn, c->members.head)
	{
		cu = n->data;
		soft_assert(is_internal_client(cu->user) && !me.connected);
		mowgli_node_delete(&cu->cnode, &c->members);
		mowgli_node_delete(&cu->unode, &cu->user->channels);
		mowgli_heap_free(chanuser_heap, cu);
		cnt.chanuser--;
	}
	c->nummembers = 0;
	c->numsvcmembers = 0;

	hook_call_channel_delete(c);

	mowgli_patricia_delete(chanlist, c->name);

	if ((mc = mychan_find(c->name)))
		mc->chan = NULL;

	clear_simple_modes(c);
	chanban_clear(c);

	if (c->extmodes != NULL)
		free(c->extmodes);

	free(c->name);
	if (c->topic != NULL)
		free(c->topic);
	if (c->topic_setter != NULL)
		free(c->topic_setter);

	mowgli_heap_free(chan_heap, c);

	cnt.chan--;
}

/*
 * chanban_add(channel_t *chan, const char *mask, int type)
 *
 * Channel ban factory.
 *
 * Inputs:
 *     - channel that the ban belongs to
 *     - banmask
 *     - type of ban, e.g. 'b' or 'e'
 *
 * Outputs:
 *     - on success, a new channel ban object
 *     - on failure, NULL
 *
 * Side Effects:
 *     - the created channel ban object is added to the channel automatically.
 */
chanban_t *chanban_add(channel_t *chan, const char *mask, int type)
{
	chanban_t *c;

	return_val_if_fail(chan != NULL, NULL);
	return_val_if_fail(mask != NULL, NULL);

	/* this would break protocol and/or cause crashes */
	if (*mask == '\0' || *mask == ':' || strchr(mask, ' '))
	{
		slog(LG_ERROR, "chanban_add(): trying to add invalid +%c %s to channel %s", type, mask, chan->name);
		return NULL;
	}

	c = chanban_find(chan, mask, type);

	if (c)
	{
		slog(LG_DEBUG, "chanban_add(): channel ban %s:%s already exists", chan->name, c->mask);
		return NULL;
	}

	slog(LG_DEBUG, "chanban_add(): %s +%c %s", chan->name, type, mask);

	c = mowgli_heap_alloc(chanban_heap);

	c->chan = chan;
	c->mask = sstrdup(mask);
	c->type = type;

	mowgli_node_add(c, &c->node, &chan->bans);

	return c;
}

/*
 * chanban_delete(chanban_t *c)
 *
 * Destroys a channel ban.
 *
 * Inputs:
 *     - channel ban object to destroy
 *
 * Outputs:
 *     - nothing
 *
 * Side Effects:
 *     - the channel ban is automatically removed from the channel that owned it
 */
void chanban_delete(chanban_t * c)
{
	return_if_fail(c != NULL);

	mowgli_node_delete(&c->node, &c->chan->bans);

	free(c->mask);
	mowgli_heap_free(chanban_heap, c);
}

/*
 * chanban_find(channel_t *chan, const char *mask, int type)
 *
 * Looks up a channel ban.
 *
 * Inputs:
 *     - channel that the ban is supposedly on
 *     - mask that is being looked for
 *     - type of ban that is being looked for
 *
 * Outputs:
 *     - on success, returns the channel ban object requested
 *     - on failure, returns NULL
 *
 * Side Effects:
 *     - none
 */
chanban_t *chanban_find(channel_t *chan, const char *mask, int type)
{
	chanban_t *c;
	mowgli_node_t *n;

	return_val_if_fail(chan != NULL, NULL);
	return_val_if_fail(mask != NULL, NULL);

	MOWGLI_ITER_FOREACH(n, chan->bans.head)
	{
		c = n->data;

		if (c->type == type && !irccasecmp(c->mask, mask))
			return c;
	}

	return NULL;
}

/*
 * chanuser_add(channel_t *chan, const char *nick)
 *
 * Channel user factory.
 *
 * Inputs:
 *     - channel that the user should belong to
 *     - nick/UID with any appropriate prefixes (e.g. ~, &, @, %, +)
 *       (if the user has a UID it must be used)
 *
 * Outputs:
 *     - on success, a new channel user object
 *     - on failure (NULL channel, or user kicked by channel_join hook), NULL
 *
 * Side Effects:
 *     - the channel user object is automatically associated to its parents
 *     - channel_join hook is called
 */

/*
 * Rewritten 06/23/05 by nenolod:
 *
 * Iterate through the list of prefix characters we know about.
 * Continue to do so until all prefixes are covered. Then add the
 * nick to the channel, with the privs they have acquired thus far.
 *
 * Once, and only once we have done that do we start in on checking
 * privileges. Otherwise we have a very inefficient way of doing
 * things. It worked fine for shrike, but the old code was restricted
 * to handling only @, @+ and + as prefixes.
 */
chanuser_t *chanuser_add(channel_t *chan, const char *nick)
{
	user_t *u;
	chanuser_t *cu, *tcu;
	unsigned int flags = 0;
	int i = 0;
	hook_channel_joinpart_t hdata;

	return_val_if_fail(chan != NULL, NULL);
	return_val_if_fail(chan->name != NULL, NULL);
	return_val_if_fail(nick != NULL, NULL);

	if (!VALID_GLOBAL_CHANNEL_PFX(chan->name))
	{
		slog(LG_DEBUG, "chanuser_add(): got an invalid global channel prefix: %s", chan->name);
		return NULL;
	}

	while (*nick != '\0')
	{
		for (i = 0; prefix_mode_list[i].mode; i++)
			if (*nick == prefix_mode_list[i].mode)
			{
				flags |= prefix_mode_list[i].value;
				break;
			}
		if (!prefix_mode_list[i].mode)
			break;
		nick++;
	}

	u = user_find(nick);
	if (u == NULL)
	{
		slog(LG_DEBUG, "chanuser_add(): nonexist user: %s", nick);
		return NULL;
	}

	tcu = chanuser_find(chan, u);
	if (tcu != NULL)
	{
		slog(LG_DEBUG, "chanuser_add(): user is already present: %s -> %s", chan->name, u->nick);

		/* could be an OPME or other desyncher... */
		tcu->modes |= flags;

		return tcu;
	}

	slog(LG_DEBUG, "chanuser_add(): %s -> %s", chan->name, u->nick);

	cu = mowgli_heap_alloc(chanuser_heap);

	cu->chan = chan;
	cu->user = u;
	cu->modes = flags;

	chan->nummembers++;
	if (is_internal_client(u))
		chan->numsvcmembers++;

	mowgli_node_add(cu, &cu->cnode, &chan->members);
	mowgli_node_add(cu, &cu->unode, &u->channels);

	cnt.chanuser++;

	hdata.cu = cu;
	hook_call_channel_join(&hdata);

	/* Return NULL if a hook function kicked the user out */
	return hdata.cu;
}

/*
 * chanuser_delete(channel_t *chan, user_t *user)
 *
 * Destroys a channel user object.
 *
 * Inputs:
 *     - channel the user is on
 *     - the user itself
 *
 * Outputs:
 *     - nothing
 *
 * Side Effects:
 *     if the user is on the channel:
 *     - a channel user object is removed from the
 *       channel's userlist and the user's channellist.
 *     - channel_part hook is called
 *     - if this empties the channel and the channel is not set permanent
 *       (ircd->perm_mode), channel_delete() is called (q.v.)
 */
void chanuser_delete(channel_t *chan, user_t *user)
{
	chanuser_t *cu;
	hook_channel_joinpart_t hdata;

	return_if_fail(chan != NULL);
	return_if_fail(user != NULL);

	cu = chanuser_find(chan, user);
	if (cu == NULL)
		return;

	/* this is called BEFORE we remove the user */
	hdata.cu = cu;
	hook_call_channel_part(&hdata);

	slog(LG_DEBUG, "chanuser_delete(): %s -> %s (%d)", cu->chan->name, cu->user->nick, cu->chan->nummembers - 1);

	mowgli_node_delete(&cu->cnode, &chan->members);
	mowgli_node_delete(&cu->unode, &user->channels);

	mowgli_heap_free(chanuser_heap, cu);

	chan->nummembers--;
	cnt.chanuser--;

	if (is_internal_client(user))
		chan->numsvcmembers--;

	if (chan->nummembers == 0 && !(chan->modes & ircd->perm_mode))
	{
		/* empty channels die */
		slog(LG_DEBUG, "chanuser_delete(): `%s' is empty, removing", chan->name);

		channel_delete(chan);
	}
}

/*
 * chanuser_find(channel_t *chan, user_t *user)
 *
 * Looks up a channel user object.
 *
 * Inputs:
 *     - channel object that the user is on
 *     - target user object
 *
 * Outputs:
 *     - on success, a channel user object
 *     - on failure, NULL
 *
 * Side Effects:
 *     - none
 */
chanuser_t *chanuser_find(channel_t *chan, user_t *user)
{
	mowgli_node_t *n;
	chanuser_t *cu;

	return_val_if_fail(chan != NULL, NULL);
	return_val_if_fail(user != NULL, NULL);

	/* choose shortest list to search -- jilles */
	if (MOWGLI_LIST_LENGTH(&user->channels) < MOWGLI_LIST_LENGTH(&chan->members))
	{
		MOWGLI_ITER_FOREACH(n, user->channels.head)
		{
			cu = (chanuser_t *)n->data;

			if (cu->chan == chan)
				return cu;
		}
	}
	else
	{
		MOWGLI_ITER_FOREACH(n, chan->members.head)
		{
			cu = (chanuser_t *)n->data;

			if (cu->user == user)
				return cu;
		}
	}

	return NULL;
}

/* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
 * vim:ts=8
 * vim:sw=8
 * vim:noexpandtab
 */