File: flood.c

package info (click to toggle)
ircii-pana 1%3A1.1-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 14,048 kB
  • ctags: 14,567
  • sloc: ansic: 130,654; sql: 6,041; makefile: 4,313; cpp: 1,270; tcl: 1,230; sh: 638; java: 151
file content (610 lines) | stat: -rw-r--r-- 15,199 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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/*
 * flood.c: handle channel flooding. 
 *
 * This attempts to give you some protection from flooding.  Basically, it keeps
 * track of how far apart (timewise) messages come in from different people.
 * If a single nickname sends more than 3 messages in a row in under a
 * second, this is considered flooding.  It then activates the ON FLOOD with
 * the nickname and type (appropriate for use with IGNORE). 
 *
 * Thanks to Tomi Ollila <f36664r@puukko.hut.fi> for this one. 
 */


#include "irc.h"
static char cvsrevision[] = "$Id: flood.c,v 1.1.1.1 2003/04/11 01:09:07 dan Exp $";
CVS_REVISION(flood_c)
#include "struct.h"

#include "alias.h"
#include "hook.h"
#include "ircaux.h"
#include "ignore.h"
#include "flood.h"
#include "vars.h"
#include "output.h"
#include "list.h"
#include "misc.h"
#include "server.h"
#include "userlist.h"
#include "timer.h"
#include "ignore.h"
#include "status.h"
#include "hash2.h"
#include "cset.h"
#define MAIN_SOURCE
#include "modval.h"

static	char	*ignore_types[] =
{
	"",
	"MSG",
	"PUBLIC",
	"NOTICE",
	"WALL",
	"WALLOP",
	"CTCP",
	"INVITE",
	"CDCC",
	"ACTION",
	"NICK",
	"DEOP",
	"KICK",
	"JOIN"
};

#define FLOOD_HASHSIZE 31
HashEntry no_flood_list[FLOOD_HASHSIZE];
HashEntry flood_list[FLOOD_HASHSIZE];

static int remove_oldest_flood_hashlist(HashEntry *, time_t, int);




extern	char	*FromUserHost;
extern	unsigned int window_display;
extern	int	from_server;

static double allow_flood = 0.0;
static double this_flood = 0.0;

#define NO_RESET 0
#define RESET 1

char *get_flood_types(unsigned int type)
{
int x = 0;
	while (type)
	{
		type = type >> 1;
		x++;
	}
	return ignore_types[x];
}

#if 0
int get_flood_rate(int type, ChannelList * channel)
{
	int flood_rate = get_int_var(FLOOD_RATE_VAR);
	if (channel)
	{
		switch(type)
		{
			case JOIN_FLOOD:
				flood_rate = get_cset_int_var(channel->csets, JOINFLOOD_TIME_CSET);
				break;
			case PUBLIC_FLOOD:
				flood_rate = get_cset_int_var(channel->csets, PUBFLOOD_TIME_CSET);
				break;
			case NICK_FLOOD:
				flood_rate = get_cset_int_var(channel->csets, NICKFLOOD_TIME_CSET);
				break;
			case KICK_FLOOD:
				flood_rate = get_cset_int_var(channel->csets, KICKFLOOD_TIME_CSET);
				break;
			case DEOP_FLOOD:
				flood_rate = get_cset_int_var(channel->csets, DEOPFLOOD_TIME_CSET);
				break;
			default:
				break;
		}
	}
	else
	{
		switch(type)
		{
			case CDCC_FLOOD:
				flood_rate = get_int_var(CDCC_FLOOD_RATE_VAR);
				break;
			case CTCP_FLOOD:
				flood_rate = get_int_var(CTCP_FLOOD_RATE_VAR);
			case CTCP_ACTION_FLOOD:
			default:
				break;
		}
	}
	return flood_rate;
}

int get_flood_count(int type, ChannelList * channel)
{
	int flood_count = get_int_var(FLOOD_AFTER_VAR);
	if (channel) {
		switch(type)
		{
			case JOIN_FLOOD:
				flood_count = get_cset_int_var(channel->csets, KICK_ON_JOINFLOOD_CSET);
				break;
			case PUBLIC_FLOOD:
				flood_count = get_cset_int_var(channel->csets, KICK_ON_PUBFLOOD_CSET);
				break;
			case NICK_FLOOD:
				flood_count = get_cset_int_var(channel->csets, KICK_ON_NICKFLOOD_CSET);
				break;
			case KICK_FLOOD:
				flood_count = get_cset_int_var(channel->csets, KICK_ON_KICKFLOOD_CSET);
				break;
			case DEOP_FLOOD:
				flood_count = get_cset_int_var(channel->csets, KICK_ON_DEOPFLOOD_CSET);
				break;
			default:
			break;
		}
	} 
	else
	{
		switch(type)
		{
			case CDCC_FLOOD:
				flood_count = get_int_var(CDCC_FLOOD_AFTER_VAR);
				break;
			case CTCP_FLOOD:
				flood_count = get_int_var(CTCP_FLOOD_AFTER_VAR);
			case CTCP_ACTION_FLOOD:
			default:
				break;
		}
	}
	return flood_count;
}
#endif

void get_flood_val(ChannelList *chan, int type, int *flood_count, int *flood_rate)
{
	*flood_count = get_int_var(FLOOD_AFTER_VAR);
	*flood_rate = get_int_var(FLOOD_RATE_VAR);
	if (chan)
	{
		switch(type)
		{
			case JOIN_FLOOD:
				*flood_count = get_cset_int_var(chan->csets, KICK_ON_JOINFLOOD_CSET);
				*flood_rate = get_cset_int_var(chan->csets, JOINFLOOD_TIME_CSET);
				break;
			case PUBLIC_FLOOD:
				*flood_count = get_cset_int_var(chan->csets, KICK_ON_PUBFLOOD_CSET);
				*flood_rate = get_cset_int_var(chan->csets, PUBFLOOD_TIME_CSET);
				break;
			case NICK_FLOOD:
				*flood_count = get_cset_int_var(chan->csets, KICK_ON_NICKFLOOD_CSET);
				*flood_rate = get_cset_int_var(chan->csets, NICKFLOOD_TIME_CSET);
				break;
			case KICK_FLOOD:
				*flood_count = get_cset_int_var(chan->csets, KICK_ON_KICKFLOOD_CSET);
				*flood_rate = get_cset_int_var(chan->csets, KICKFLOOD_TIME_CSET);
				break;
			case DEOP_FLOOD:
				*flood_count = get_cset_int_var(chan->csets, KICK_ON_DEOPFLOOD_CSET);
				*flood_rate = get_cset_int_var(chan->csets, DEOPFLOOD_TIME_CSET);
				break;
			default:
			break;
		}
	}
	else
	{
		switch(type)
		{
			case CDCC_FLOOD:
				*flood_count = get_int_var(CDCC_FLOOD_AFTER_VAR);
				*flood_rate = get_int_var(CDCC_FLOOD_RATE_VAR);
				break;
			case CTCP_FLOOD:
				*flood_count = get_int_var(CTCP_FLOOD_AFTER_VAR);
				*flood_rate = get_int_var(CTCP_FLOOD_RATE_VAR);
			case CTCP_ACTION_FLOOD:
			default:
				break;
		}
	}
}

int set_flood(int type, time_t flood_time, int reset, NickList *tmpnick)
{
	if (!tmpnick)
		return 0;
	switch(type)
	{
		case JOIN_FLOOD:
			if (reset == RESET)
			{
				tmpnick->joincount = 1; 
				tmpnick->jointime = flood_time;
			} else tmpnick->joincount++;
			break;
		case PUBLIC_FLOOD:
			if (reset == RESET)
			{
				tmpnick->floodcount = 1;
				tmpnick->floodtime = tmpnick->idle_time = flood_time;
			} else tmpnick->floodcount++;
			break;
		case NICK_FLOOD:
			if (reset == RESET)
			{
				tmpnick->nickcount = 1;
				tmpnick->nicktime = flood_time;
			} else tmpnick->nickcount++;
			break;
		case KICK_FLOOD:
			if (reset == RESET)
			{
				tmpnick->kickcount = 1;
				tmpnick->kicktime = flood_time;
			} else tmpnick->kickcount++;
			break;
		case DEOP_FLOOD:
			if (reset == RESET)
			{
				tmpnick->dopcount = 1;
				tmpnick->doptime = flood_time;
			} else tmpnick->dopcount++;
			break;
		default:
		break;
	}
	return 1;
}

int BX_is_other_flood(ChannelList *channel, NickList *tmpnick, int type, int *t_flood)
{
time_t diff = 0, flood_time = 0;
int doit = 0;
int count = 0;
int flood_rate = 0, flood_count = 0;

	flood_time = now;
	
	
	if (!channel || !tmpnick)
		return 0;
	if (isme(tmpnick->nick))
		return 0;
	if (find_name_in_genericlist(tmpnick->nick, no_flood_list, FLOOD_HASHSIZE, 0))
		return 0;
	set_flood(type, flood_time, NO_RESET, tmpnick);
	switch(type)
	{
		case JOIN_FLOOD:
			if (!get_cset_int_var(channel->csets, JOINFLOOD_CSET))
				break;
			diff = flood_time - tmpnick->jointime;
			count = tmpnick->joincount;
			doit = 1;
			break;
		case PUBLIC_FLOOD:
			if (!get_cset_int_var(channel->csets, PUBFLOOD_CSET))
				break;
			diff = flood_time - tmpnick->floodtime;
			count = tmpnick->floodcount;
			doit = 1;
			break;
		case NICK_FLOOD:
			if (!get_cset_int_var(channel->csets, NICKFLOOD_CSET))
				break;
			diff = flood_time - tmpnick->nicktime;
			count = tmpnick->nickcount;
			doit = 1;
			break;
		case DEOP_FLOOD:
			if (!get_cset_int_var(channel->csets, DEOPFLOOD_CSET))
				break;
			diff = flood_time - tmpnick->doptime;
			count = tmpnick->dopcount;
			doit = 1;
			break;
		case KICK_FLOOD:
			if (!get_cset_int_var(channel->csets, KICKFLOOD_CSET))
				break;
			diff = flood_time - tmpnick->kicktime;
			count = tmpnick->kickcount;
			doit = 1;
			break;
		default:
			return 0;
			break;
	}
	if (doit)
	{
		int is_user = 0;
		if (!get_int_var(FLOOD_PROTECTION_VAR))
			return 0;
		get_flood_val(channel, type, &flood_count, &flood_rate);
		if ((tmpnick->userlist && (tmpnick->userlist->flags & ADD_FLOOD)))
			is_user = 1;
		if (!is_user && (count >= flood_count))
		{
			int flooded = 0;
			if (count >= flood_count)
			{
				if (!diff || (flood_rate && (diff < flood_rate)))
				{
					*t_flood = diff;
					flooded = 1;
					do_hook(FLOOD_LIST, "%s %s %s %s", tmpnick->nick, get_flood_types(type),channel?channel->channel:zero, tmpnick->host);
				}
				set_flood(type, flood_time, RESET, tmpnick);
				return flooded;
			}
			else if (diff > flood_rate)
				set_flood(type, flood_time, RESET, tmpnick);
		}
	} 
	return 0;
} 

/*
 * check_flooding: This checks for message flooding of the type specified for
 * the given nickname.  This is described above.  This will return 0 if no
 * flooding took place, or flooding is not being monitored from a certain
 * person.  It will return 1 if flooding is being check for someone and an ON
 * FLOOD is activated. 
 */

int BX_check_flooding(char *nick, int type, char *line, char *channel)
{
static	int	users = 0,
		pos = 0;
time_t flood_time = now,
		diff = 0;

Flooding 	*tmp;
int		flood_rate, 
		flood_count;


	if (!(users = get_int_var(FLOOD_USERS_VAR)) || !*FromUserHost)
		return 1;
	if (find_name_in_genericlist(nick, no_flood_list, FLOOD_HASHSIZE, 0))
		return 1;
	if (!(tmp = find_name_in_floodlist(nick, FromUserHost, flood_list, FLOOD_HASHSIZE, 0)))
	{
		if (pos >= users)
		{
			pos -= remove_oldest_flood_hashlist(&flood_list[0], 0, (users + 1 - pos));
		}
		tmp = add_name_to_floodlist(nick, FromUserHost, channel, flood_list, FLOOD_HASHSIZE);
		tmp->type = type;
		tmp->cnt = 1;
		tmp->start = flood_time;
		tmp->flood = 0;
		pos++;
		return 1;
	} 
	if (!(tmp->type & type))
	{
		tmp->type |= type; 
		return 1;
	}

#if 0
	flood_count = get_flood_count(type, NULL); /* FLOOD_AFTER_VAR */
	flood_rate = get_flood_rate(type, NULL); /* FLOOD_RATE_VAR */
#endif
	get_flood_val(NULL, type, &flood_count, &flood_rate);
	if (!flood_count || !flood_rate)
		return 1;
	tmp->cnt++;
	if (tmp->cnt > flood_count)
	{
		int ret;
		diff = flood_time - tmp->start;
		if (diff != 0)
			this_flood = (double)tmp->cnt / (double)diff;
		else
			this_flood = 0;
		allow_flood = (double)flood_count / (double)flood_rate;
		if (!diff || !this_flood || (this_flood > allow_flood))
		{
			if (tmp->flood == 0)
			{
				tmp->flood = 1;
				if ((ret = do_hook(FLOOD_LIST, "%s %s %s %s", nick, get_flood_types(type),channel?channel:zero, line)) != 1)
					return ret;
				switch(type)
				{
					case WALL_FLOOD:
					case MSG_FLOOD:
					case NOTICE_FLOOD:
					case CDCC_FLOOD:
					case CTCP_FLOOD:
						if (flood_prot(nick, FromUserHost, get_flood_types(type), type, get_int_var(IGNORE_TIME_VAR), channel))
							return 0;
						break;
					case CTCP_ACTION_FLOOD:
						if (flood_prot(nick, FromUserHost, get_flood_types(CTCP_FLOOD), type, get_int_var(IGNORE_TIME_VAR), channel))
							return 0;
					default:
						break;
				}
				if (get_int_var(FLOOD_WARNING_VAR))
					put_it("%s", convert_output_format(fget_string_var(FORMAT_FLOOD_FSET), "%s %s %s %s %s", update_clock(GET_TIME), get_flood_types(type), nick, FromUserHost, channel?channel:"unknown"));
			}
			return 1;
		}
		else
		{
			tmp->flood = 0;
			tmp->cnt = 1;
			tmp->start = flood_time;
		}
	}
	return 1;
}

void check_ctcp_ban_flood(char *channel, char *nick)
{
NickList *Nick = NULL;
ChannelList *chan = NULL;
	for (chan = get_server_channels(from_server); chan; chan = chan->next)
		if ((Nick = find_nicklist_in_channellist(nick, chan, 0)))
			break;
	if (chan && chan->chop && get_cset_int_var(chan->csets, CTCP_FLOOD_BAN_CSET) && Nick)
	{
		if (!Nick->userlist || (Nick->userlist && !(Nick->userlist->flags & ADD_FLOOD)))
		{
			if (!nick_isop(Nick) || get_cset_int_var(chan->csets, KICK_OPS_CSET))
			{
				char *ban, *u, *h;
				u = alloca(strlen(Nick->host)+1);
				strcpy(u, Nick->host);
				h = strchr(u, '@');
				*h++ = 0;
				ban = ban_it(Nick->nick, u, h, Nick->ip);
				if (!ban_is_on_channel(ban, chan) && !eban_is_on_channel(ban, chan))
					send_to_server("MODE %s +b %s", chan->channel, ban);
			}
		}
	}
}

int BX_flood_prot (char *nick, char *userhost, char *type, int ctcp_type, int ignoretime, char *channel)
{
ChannelList *chan;
NickList *Nick;
char tmp[BIG_BUFFER_SIZE+1];
char *uh;
int	old_window_display;
int	kick_on_flood = 1;

	if ((ctcp_type == CDCC_FLOOD || ctcp_type == CTCP_FLOOD || ctcp_type == CTCP_ACTION_FLOOD) && !get_int_var(CTCP_FLOOD_PROTECTION_VAR))
		return 0;
	else if (!get_int_var(FLOOD_PROTECTION_VAR))
		return 0;
	else if (!my_stricmp(nick, get_server_nickname(from_server)))
		return 0;
	switch (ctcp_type)
	{
		case WALL_FLOOD:
		case MSG_FLOOD:
			break;
		case NOTICE_FLOOD:
			break;
		case PUBLIC_FLOOD:
			if (channel)
			{
				if ((chan = lookup_channel(channel, from_server, 0)))
				{
					kick_on_flood = get_cset_int_var(chan->csets, PUBFLOOD_CSET);
					if (kick_on_flood && (Nick = find_nicklist_in_channellist(nick, chan, 0)))
					{
						if (chan->chop && (!Nick->userlist || (Nick->userlist && !(Nick->userlist->flags & ADD_FLOOD))))
							if (!nick_isop(Nick) || get_cset_int_var(chan->csets, KICK_OPS_CSET))
								send_to_server("KICK %s %s :\002%s\002 flooder", chan->channel, nick, type);
					} 
				}
			}
			break;
		case CTCP_FLOOD:
		case CTCP_ACTION_FLOOD:
			check_ctcp_ban_flood(channel, nick);
		default:
			if (get_int_var(FLOOD_KICK_VAR) && kick_on_flood && channel)
			{
				for (chan = get_server_channels(from_server); chan; chan = chan->next)
				{
					if (chan->chop && (Nick = find_nicklist_in_channellist(nick, chan, 0)))
					{
						if ((!Nick->userlist || (Nick->userlist && !(Nick->userlist->flags & ADD_FLOOD))))
							if (!nick_isop(Nick) || get_cset_int_var(chan->csets, KICK_OPS_CSET))
								send_to_server("KICK %s %s :\002%s\002 flooder", chan->channel, nick, type);
					}
				}
			}
	}
	if (!ignoretime)
		return 0;
	uh = clear_server_flags(userhost);
	sprintf(tmp, "*!*%s", uh);
	old_window_display = window_display;
	window_display = 0;
	ignore_nickname(tmp, ignore_type(type, strlen(type)), 0);
	window_display = old_window_display;
	sprintf(tmp, "%d ^IGNORE *!*%s NONE", ignoretime, uh);
	timercmd("TIMER", tmp, NULL, NULL);
	bitchsay("Auto-ignoring %s for %d minutes [\002%s\002 flood]", nick, ignoretime/60, type);
	return 1;
}

static int remove_oldest_flood_hashlist(HashEntry *list, time_t timet, int count)
{
Flooding *ptr;
register time_t t;
int total = 0;
register unsigned long x;
	t = now;
	if (!count)
	{
		for (x = 0; x < FLOOD_HASHSIZE; x++)
		{
			ptr = (Flooding *) (list + x)->list;
			if (!ptr || !*ptr->name)
				continue;
			while (ptr)
			{
				if ((ptr->start + timet) <= t)
				{
					if (!(ptr = find_name_in_floodlist(ptr->name, ptr->host, flood_list, FLOOD_HASHSIZE, 1)))
						continue;
					new_free(&(ptr->channel));
					new_free(&(ptr->name));
					new_free(&ptr->host);
					new_free((char **)&ptr);
					total++;
					ptr = (Flooding *) (list + x)->list;
				} else ptr = ptr->next;
			}
		}
	}
	else
	{
		for (x = 0; x < FLOOD_HASHSIZE; x++)
		{
			Flooding *next = NULL;
			ptr = (Flooding *) (list + x)->list;
			if (!ptr || !*ptr->name)
				continue;
			while(ptr && count)
			{
				if ((ptr = find_name_in_floodlist(ptr->name, ptr->host, flood_list, FLOOD_HASHSIZE, 1)))
				{
					next = ptr->next;
					new_free(&(ptr->channel));
					new_free(&(ptr->name));
					new_free(&ptr->host);
					new_free((char **)&ptr);
					total++; count--;			
					ptr = (Flooding *) (list + x)->list;
					ptr = next;
				}
			}
		}
	}
	return total;
}

void clean_flood_list()
{
	remove_oldest_flood_hashlist(&flood_list[0], get_int_var(FLOOD_RATE_VAR)+1, 0);
}