This repository has been archived by the owner on Jun 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
joinrate.c
304 lines (245 loc) · 8.34 KB
/
joinrate.c
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
/*
* syn: a utility bot to manage IRC network access
* Copyright (C) 2009-2016 Stephen Bennett
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "atheme.h"
#include "syn.h"
static void syn_ratecheck(hook_channel_joinpart_t *data);
static void syn_cmd_setrate(sourceinfo_t *si, int parc, char **parv);
static void syn_cmd_showrate(sourceinfo_t *si, int parc, char **parv);
command_t syn_setrate = { "SETRATE", N_("Sets join-rate monitoring thresholds"), "syn:general", 3, syn_cmd_setrate };
command_t syn_showrate = { "SHOWRATE", N_("Displays join-rate monitoring thresholds"), "syn:general", 1, syn_cmd_showrate };
int default_rate;
int default_burst;
int warn_time;
typedef struct
{
char chname[CHANNELLEN];
bool use_custom;
int rate[2];
time_t curr_rate_time;
time_t last_warn_time;
} channelentry;
mowgli_patricia_t *channellist;
mowgli_heap_t *channelheap;
static void free_channelentry(const char *, void *data, void *);
void load_rate_settings()
{
FILE *f = fopen(DATADIR "/rates.db", "r");
if (!f)
{
slog(LG_DEBUG, "Couldn't open rates.db for reading: %s", strerror(errno));
return;
}
char line[BUFSIZE];
while (fgets(line, BUFSIZE, f))
{
char *chname = strtok(line, " ");
if (0 == strcmp(chname, "default"))
{
char *srate = strtok(NULL, " ");
char *sburst = strtok(NULL, " ");
if (!srate || !sburst)
continue;
default_rate = atoi(srate);
default_burst = atoi(sburst);
continue;
}
char *srate = strtok(NULL, " ");
char *sburst = strtok(NULL, " ");
if (!srate || !sburst)
continue;
int rate = atoi(srate), burst = atoi(sburst);
channelentry *ce = mowgli_patricia_retrieve(channellist, chname);
if (!ce)
{
ce = mowgli_heap_alloc(channelheap);
strncpy(ce->chname, chname, CHANNELLEN);
ce->use_custom = true;
mowgli_patricia_add(channellist, chname, ce);
}
ce->rate[0] = rate;
ce->rate[1] = burst;
}
fclose(f);
}
void save_rate_settings()
{
FILE *f = fopen(DATADIR "/rates.db", "w");
if (!f)
{
slog(LG_ERROR, "Couldn't open rates.db for writing: %s", strerror(errno));
return;
}
fprintf(f, "default %d %d\n", default_rate, default_burst);
channelentry *ce;
mowgli_patricia_iteration_state_t state;
MOWGLI_PATRICIA_FOREACH(ce, &state, channellist)
{
fprintf(f, "%s %d %d\n", ce->chname, ce->rate[0], ce->rate[1]);
}
fclose(f);
}
static void mod_init(module_t *m)
{
use_syn_main_symbols(m);
service_named_bind_command("syn", &syn_setrate);
service_named_bind_command("syn", &syn_showrate);
hook_add_event("channel_join");
hook_add_channel_join(syn_ratecheck);
channellist = mowgli_patricia_create(noopcanon);
channelheap = mowgli_heap_create(sizeof(channelentry), 512, BH_NOW);
default_rate = 5;
default_burst = 5;
warn_time = 30;
load_rate_settings();
}
static void mod_deinit(module_unload_intent_t intent)
{
save_rate_settings();
mowgli_patricia_destroy(channellist, free_channelentry, NULL);
service_named_unbind_command("syn", &syn_setrate);
service_named_unbind_command("syn", &syn_showrate);
hook_del_channel_join(syn_ratecheck);
}
static void free_channelentry(const char *key, void *data, void *privdata)
{
mowgli_heap_free(channelheap, data);
}
static void syn_ratecheck(hook_channel_joinpart_t *data)
{
chanuser_t *cu = data->cu;
// Don't warn about burst joins.
if (me.bursting)
return;
channelentry *ce = mowgli_patricia_retrieve(channellist, cu->chan->name);
if (!ce)
{
ce = mowgli_heap_alloc(channelheap);
strncpy(ce->chname, cu->chan->name, CHANNELLEN);
mowgli_patricia_add(channellist, cu->chan->name, ce);
}
int rate, burst;
if (ce->use_custom)
{
rate = ce->rate[0];
burst = ce->rate[1];
}
else
{
rate = default_rate;
burst = default_burst;
}
if (ce->curr_rate_time < CURRTIME)
ce->curr_rate_time = CURRTIME;
ce->curr_rate_time += rate;
if (ce->curr_rate_time > (rate * burst) + CURRTIME &&
ce->last_warn_time + warn_time < CURRTIME)
{
ce->last_warn_time = CURRTIME;
syn_report("Join rate in %s exceeds warning threshold(%d/%d)", ce->chname, rate, burst);
}
}
static void syn_cmd_showrate(sourceinfo_t *si, int parc, char **parv)
{
if (parc == 0)
{
command_success_nodata(si, "Global warning threshold is %d seconds, %d burst", default_rate, default_burst);
return;
}
channelentry *ce = mowgli_patricia_retrieve(channellist, parv[0]);
if (!ce || !ce->use_custom)
{
command_success_nodata(si, "No custom warning threshold is set for %s", parv[0]);
return;
}
command_success_nodata(si, "Warning threshold for %s is %d seconds, %d burst", ce->chname, ce->rate[0], ce->rate[1]);
}
static void syn_cmd_setrate(sourceinfo_t *si, int parc, char **parv)
{
if (parc < 2)
{
command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "SETRATE");
command_fail(si, fault_needmoreparams, _("Syntax: SETRATE [#channel] default|(<rate> <burst>)"));
return;
}
if (parv[0][0] != '#')
{
int r, b;
r = atoi(parv[0]);
b = atoi(parv[1]);
if (r * b == 0)
{
command_fail(si, fault_badparams, STR_INVALID_PARAMS, "SETRATE");
command_fail(si, fault_needmoreparams, _("Syntax: SETRATE [#channel] default|(<rate> <burst>)"));
return;
}
default_rate = r;
default_burst = b;
syn_report("\002SETRATE\002 default->%d/%d by %s", default_rate, default_burst, get_oper_name(si));
command_success_nodata(si, "Warning threshold set to %d seconds, with a burst of %d", default_rate, default_burst);
save_rate_settings();
return;
}
channelentry *ce = mowgli_patricia_retrieve(channellist, parv[0]);
if (0 == strcasecmp(parv[1], "default"))
{
if (!ce || !ce->use_custom)
{
command_fail(si, fault_nochange, "No custom rate settings were defined for %s", parv[0]);
return;
}
ce->use_custom = false;
syn_report("\002SETRATE\002 %s->default by %s", parv[0], get_oper_name(si));
command_success_nodata(si, "Custom rate settings have been disabled for %s", parv[0]);
save_rate_settings();
return;
}
if (parc < 3)
{
command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "SETRATE");
command_fail(si, fault_needmoreparams, _("Syntax: SETRATE [#channel] default|(<rate> <burst>)"));
return;
}
if (!ce)
{
ce = mowgli_heap_alloc(channelheap);
strncpy(ce->chname, parv[0], CHANNELLEN);
mowgli_patricia_add(channellist, parv[0], ce);
}
int r, b;
r = atoi(parv[1]);
b = atoi(parv[2]);
if (r * b == 0)
{
command_fail(si, fault_badparams, STR_INVALID_PARAMS, "SETRATE");
command_fail(si, fault_needmoreparams, _("Syntax: SETRATE [#channel] default|(<rate> <burst>)"));
return;
}
ce->use_custom = true;
ce->rate[0] = r;
ce->rate[1] = b;
ce->curr_rate_time = 0;
syn_report("\002SETRATE\002 %s->%d/%d by %s", parv[0], r, b, get_oper_name(si));
command_success_nodata(si, "Warning threshold for %s set to %d seconds, with a burst of %d", parv[0], r, b);
save_rate_settings();
}
DECLARE_MODULE_V1
(
"syn/joinrate", false, mod_init, mod_deinit,
"$Revision$",
"Stephen Bennett <stephen -at- freenode.net>"
);