File: flood.c

package info (click to toggle)
epic4 1%3A2.10.1-1
  • links: PTS
  • area: main
  • in suites: squeeze, wheezy
  • size: 3,620 kB
  • ctags: 5,415
  • sloc: ansic: 56,168; makefile: 686; sh: 160; perl: 30
file content (332 lines) | stat: -rw-r--r-- 8,256 bytes parent folder | download | duplicates (10)
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
/* $EPIC: flood.c,v 1.15 2003/09/23 21:49:47 jnelson Exp $ */
/*
 * flood.c: handle channel flooding.
 *
 * Copyright (c) 1990-1992 Tomi Ollila
 * Copyright  1997, 2003 EPIC Software Labs
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notices, the above paragraph (the one permitting redistribution),
 *    this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The names of the author(s) may not be used to endorse or promote
 *    products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``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 AUTHORS 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.
 */
/*
 * 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).
 */

#include "irc.h"
#include "flood.h"
#include "hook.h"
#include "ignore.h"
#include "ircaux.h"
#include "output.h"
#include "server.h"
#include "vars.h"
#include "functions.h"
#include "lastlog.h"
#include "window.h"

static	const char *ignore_types[NUMBER_OF_FLOODS] =
{
	"CRAP",
	"CTCPS",
	"INVITES",
	"JOINS",
	"MSGS",
	"NICKS",
	"NOTES",
	"NOTICES",
	"PUBLIC",
	"TOPICS",
	"WALLOPS",
	"WALLS"
};

typedef struct flood_stru
{
	char		*nuh;
	char		*channel;
	int		server;

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

static	Flooding *flood = (Flooding *) 0;
int	users = 0;


/*
 * If flood_maskuser is 0, proceed normally.  If 2, keep
 * track of the @host only.  If 1, keep track of the U@H
 * only if it begins with an alphanum, and use the @host
 * otherwise.
 */
static const char *	normalize_nuh (const char *nuh)
{
	int maskuser = get_int_var(FLOOD_MASKUSER_VAR);

	if (maskuser == 0) {
	} else if (!nuh) {
	} else if (maskuser == 1 && isalnum(*nuh)) {
	} else {
		char *nnuh = strrchr(nuh, '@');
		nuh = nnuh ? nnuh : nuh;
	}

	return nuh;
}

/*
 * 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	new_check_flooding (const char *nick, const char *nuh, const char *chan, const char *line, FloodType type)
{
static	int	 pos = 0;
	int	 i,
		 numusers,
		 server,
		 retval = 0;
	Timeval	 right_now;
	double	 diff;
	Flooding *tmp;


	/*
	 * Figure out how many people we want to track
	 */
	numusers = get_int_var(FLOOD_USERS_VAR);

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

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

	if (nuh && *nuh)
		nuh = normalize_nuh(nuh);
	else
		return 0;

	/*
	 * What server are we using?
	 */
	server = (from_server == NOSERV) ? primary_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;
		if (!flood[i].nuh)
			continue;

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

		/*
		 * Must be for a channel if we're for a channel
		 */
		if (!!flood[i].channel != !!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;
	}

	get_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 = (0 < pos ? pos : users) - 1;
		} while (0 < --flood[pos].floods && pos != old_pos);

		tmp = flood + pos;
		malloc_strcpy(&tmp->nuh, nuh);
		if (chan)
			malloc_strcpy(&tmp->channel, chan);
		else
			new_free(&tmp->channel);

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

		pos = (0 < old_pos ? old_pos : users) - 1;
		return 0;
	}
	else
		tmp = flood + i;

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

		if ((diff == 0.0 || tmp->cnt / diff >= rate) &&
				(retval = do_hook(FLOOD_LIST, "%s %s %s %s",
				nick, ignore_types[type],
				chan ? chan : "*", line)))
		{
			tmp->floods++;
			message_from(chan, LOG_CRAP);
			if (get_int_var(FLOOD_WARNING_VAR))
				say("FLOOD: %ld %s detected from %s in %f seconds",
					tmp->cnt+1, ignore_types[type], nick, diff);
			message_from(NULL, LOG_CRAP);
		}
		else
		{
			/*
			 * Not really flooding -- reset back to normal.
			 */
			tmp->cnt = 0;
			tmp->start = right_now;
		}
	}

	if (get_int_var(FLOOD_IGNORE_VAR))
		return retval;
	else
		return 0;
}

int	check_flooding (const char *nick, const char *nuh, FloodType type, const char *line)
{
	return new_check_flooding(nick, nuh, NULL, line, type);
}

/*
 * Note:  This will break whatever uses it when any of the arguments
 *        contain a double quote.
 */
char *	function_floodinfo (char *args)
{
	const char	*arg;
	char *ret = NULL;
	size_t	clue = 0;
	Timeval right_now;
	int	i;

	get_time(&right_now);

	while ((arg = new_next_arg(args, &args))) {
		if (!strspn(arg, "*?"))
			arg = normalize_nuh(arg);
		for (i = 0; i < users; i++) {
			if (flood[i].nuh && wild_match(arg, flood[i].nuh)) {
				malloc_strcat_wordlist_c(&ret, space, "\"", &clue);
				malloc_strcat_wordlist_c(&ret, empty_string, flood[i].nuh, &clue);
				malloc_strcat_wordlist_c(&ret, space, flood[i].channel ? flood[i].channel : star, &clue);
				malloc_strcat_wordlist_c(&ret, space, ignore_types[flood[i].type], &clue);
				malloc_strcat_wordlist_c(&ret, space, ltoa(flood[i].server), &clue);
				malloc_strcat_wordlist_c(&ret, space, ltoa(flood[i].cnt), &clue);
				malloc_strcat_wordlist_c(&ret, space, ftoa(time_diff(flood[i].start, right_now)), &clue);
				malloc_strcat_wordlist_c(&ret, empty_string, "\"", &clue);
			}
		}
	}

	RETURN_MSTR(ret);
}