File: flood.c

package info (click to toggle)
teknap 1.4-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 5,428 kB
  • ctags: 6,297
  • sloc: ansic: 55,591; makefile: 545; sh: 112
file content (215 lines) | stat: -rw-r--r-- 4,436 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
/*
 * 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. 
 * $Id: flood.c,v 1.1.1.1 2000/06/23 05:55:51 edwards Exp $
 */

#include "teknap.h"
#include "struct.h"
#include "flood.h"
#include "hook.h"
#include "ircaux.h"
#include "output.h"
#include "server.h"
#include "vars.h"

static	char	*ignore_types[NUMBER_OF_FLOODS] =
{
	"JOINS",
	"MSGS",
	"PUBLIC",
};

typedef struct flood_stru
{
	char		nick    [NICKNAME_LEN + 1];
	char		channel [NICKNAME_LEN + 1];
	int		server;

	FloodType	type;
	long		cnt;
	time_t		start;
	int		floods;
}	Flooding;

static	Flooding *flood = NULL;


/*
 * 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 check_flooding (char *nick, char *chan, char *line, FloodType type)
{
static	int	 users = 0,
		 pos = 0;
	int	 i, 
		 numusers,
		 server,
		 retval = 1;
	time_t	 right_now,
		 diff;
	Flooding *tmp;

	if (from_server == -1)
		return 0;
	/*
	 * Figure out how many people we want to track
	 */
	numusers = get_int_var(FLOOD_USERS_VAR);

	/*
	 * Following 0 people turns off flood checking entirely.
	 */
	if (numusers == 0)
	{
		if (flood)
			new_free((char **)&flood);
		return 1;
	}

	/*
	 * If the number of users has changed, then resize the info array
	 */
	if (users != numusers)
	{
		users = numusers;
		RESIZE(flood, Flooding, users);
		for (i = 0; i < users; i++)
		{
			flood[i].nick[0] = 0;
			flood[i].channel[0] = 0;
			flood[i].server = -1;
			flood[i].type = -1;
			flood[i].cnt = 0;
			flood[i].start = 0;
			flood[i].floods = 0;
		}
	}

	/*
	 * What server are we using?
	 */
	server = from_server;

	/*
	 * Look in the flooding array.
	 * Find an entry that matches us:
	 *	It must be the same flooding type and server.
	 *	It must be for the same nickname
	 *	If we're for a channel, it must also be for a channel
	 *		and it must be for our channel
	 *	else if we're not for a channel, it must also not be for
	 *		a channel.
	 */
	for (i = 0; i < users; i++)
	{
		/*
		 * Do some inexpensive tests first
		 */
		if (type != flood[i].type)
			continue;
		if (server != flood[i].server)
			continue;

		/*
		 * Must be for the person we're looking for
		 */
		if (my_stricmp(nick, flood[i].nick))
			continue;

		/*
		 * Must be for a channel if we're for a channel
		 */
		if (!!flood[i].channel[0] != !!chan)
			continue;

		/*
		 * Must be for the channel we're looking for.
		 */
		if (chan && my_stricmp(chan, flood[i].channel))
			continue;

		/*
		 * We have a winner!
		 */
		break;
	}

	time(&right_now);

	/*
	 * We didnt find anybody.
	 */
	if (i == users)
	{
		/*
		 * pos points at the next insertion point in the array.
		 */
		int old_pos = pos;
		do
			pos = (pos + 1) % users;
		while (flood[pos].floods && pos != old_pos);

		tmp = flood + pos;
		strmcpy(tmp->nick, nick, NICKNAME_LEN);
		if (chan)
			strmcpy(tmp->channel, chan, NICKNAME_LEN);
		else
			tmp->channel[0] = 0;

		tmp->server = server;
		tmp->type = type;
		tmp->cnt = 1;
		tmp->start = right_now;

		return 1;
	}
	else
		tmp = flood + i;

	/*
	 * Has the person flooded too much?
	 */
	if (++tmp->cnt >= get_int_var(FLOOD_AFTER_VAR))
	{
		diff = right_now - tmp->start;

		if (diff == 0 || tmp->cnt / diff >= get_int_var(FLOOD_RATE_VAR))
		{
			if (get_int_var(FLOOD_WARNING_VAR))
				say("%s flooding detected from %s on %s", 
						ignore_types[type], nick, 
						tmp->channel[0] ? chan : "*");
			if (tmp->channel[0])
				retval = do_hook(FLOOD_LIST, "%s %s %s %s",
					nick, ignore_types[type], chan, line);
			else
				retval = do_hook(FLOOD_LIST, "%s %s * %s",
					nick, ignore_types[type], line);

			tmp->floods++;
		}
		else
		{
			/*
			 * Not really flooding -- reset back to normal.
			 */
			tmp->floods = 0;
			tmp->cnt = 1;
			tmp->start = right_now;
		}
	}
	return retval;
}