forked from atheme/atheme-contrib-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
os_defcon.c
247 lines (206 loc) · 6.42 KB
/
os_defcon.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
/*
* Copyright (c) 2014-2016 Xtheme Development Group
* Copyright (c) 2010 JD Horelick <[email protected]>
* Rights to this code are as defined in doc/LICENSE.
*
* DEFCON implementation.
*
* By default, any setting except 5 will expire after 15 minutes,
* to change this, add a defcon_timeout = X; option to the operserv{}
* block in your atheme.con. X = amount of time in minutes for a defcon
* setting to time out/expire.
*
*/
#include "atheme-compat.h"
#define DEFCON_CMODE "R"
DECLARE_MODULE_V1
(
"contrib/os_defcon", false, _modinit, _moddeinit,
PACKAGE_STRING,
"Xtheme Development Group <http://www.Xtheme.org>"
);
static void os_cmd_defcon(sourceinfo_t *si, int parc, char *parv[]);
static void defcon_nouserreg(hook_user_register_check_t *hdata);
static void defcon_nochanreg(hook_channel_register_check_t *hdatac);
static void defcon_useradd(hook_user_nick_t *data);
static void defcon_timeoutfunc(void *dummy);
static int level = 5;
static unsigned int defcon_timeout = 900;
static mowgli_eventloop_timer_t *defcon_timer = NULL;
command_t os_defcon = { "DEFCON", N_("Implements Defense Condition lockdowns."), PRIV_ADMIN, 1, os_cmd_defcon, { .path = "contrib/defcon" } };
void _modinit(module_t *m)
{
service_named_bind_command("operserv", &os_defcon);
TAINT_ON("Using os_defcon", "Use of os_defcon is unsupported and not recommend. Use only at your own risk.");
/* Hooks for all the stuff defcon disables */
hook_add_event("user_can_register");
hook_add_user_can_register(defcon_nouserreg);
hook_add_event("channel_can_register");
hook_add_channel_can_register(defcon_nochanreg);
hook_add_event("user_add");
hook_add_user_add(defcon_useradd);
service_t *svs;
svs = service_find("operserv");
add_duration_conf_item("DEFCON_TIMEOUT", &svs->conf_table, 0, &defcon_timeout, "m", 900);
}
void _moddeinit(module_unload_intent_t intent)
{
service_named_unbind_command("operserv", &os_defcon);
hook_del_user_can_register(defcon_nouserreg);
hook_del_channel_can_register(defcon_nochanreg);
hook_del_user_add(defcon_useradd);
service_t *svs;
svs = service_find("operserv");
del_conf_item("DEFCON_TIMEOUT", &svs->conf_table);
if (defcon_timer != NULL)
mowgli_timer_destroy(base_eventloop, defcon_timer);
}
static void defcon_nouserreg(hook_user_register_check_t *hdata)
{
return_if_fail(hdata != NULL);
return_if_fail(hdata->si != NULL);
if (level < 5)
{
command_fail(hdata->si, fault_badparams, _("Registrations are currently disabled on this network, please try again later."));
hdata->approved++;
}
}
static void defcon_nochanreg(hook_channel_register_check_t *hdatac)
{
return_if_fail(hdatac != NULL);
return_if_fail(hdatac->si != NULL);
if (level < 5)
{
command_fail(hdatac->si, fault_badparams, _("Registrations are currently disabled on this network, please try again later."));
hdatac->approved++;
}
}
static void defcon_useradd(hook_user_nick_t *data)
{
user_t *u = data->u;
kline_t *k;
service_t *svs;
svs = service_find("operserv");
if (!u)
return;
if (is_internal_client(u))
return;
if (level == 1)
{
slog(LG_INFO, "DEFCON:AKILL: %s!%s@%s", u->nick, u->user, u->host);
if (! (u->flags & UF_KLINESENT)) {
k = kline_add(u->user, u->host, "This network is currently not accepting connections, please try again later.", 900, svs->me->nick);
u->flags |= UF_KLINESENT;
}
}
}
static void defcon_svsignore(void)
{
svsignore_t *svsignore;
mowgli_node_t *n, *tn;
if (level <= 2)
{
MOWGLI_ITER_FOREACH(n, svs_ignore_list.head)
{
svsignore = (svsignore_t *)n->data;
if (!strcasecmp(svsignore->mask, "*@*"))
return;
}
slog(LG_INFO, "DEFCON:IGNORE:ADD");
svsignore = svsignore_add("*@*", "DEFCON Level 1 or 2 activated");
svsignore->setby = "DEFCON";
svsignore->settime = CURRTIME;
}
else if (level >= 3)
{
MOWGLI_ITER_FOREACH_SAFE(n, tn, svs_ignore_list.head)
{
svsignore = (svsignore_t *)n->data;
if (!strcasecmp(svsignore->mask,"*@*"))
{
slog(LG_INFO, "DEFCON:IGNORE:REMOVE");
svsignore_delete(svsignore);
}
}
}
}
static void defcon_forcechanmodes(void)
{
channel_t *chptr;
mowgli_patricia_iteration_state_t state;
service_t *svs;
char modesetbuf[256];
char modeunsetbuf[256];
svs = service_find("operserv");
if (level <= 3)
{
snprintf(modesetbuf, sizeof modesetbuf, "+%s", DEFCON_CMODE);
slog(LG_INFO, "DEFCON:MODE: %s", modesetbuf);
MOWGLI_PATRICIA_FOREACH(chptr, &state, chanlist)
{
channel_mode_va(svs->me, chptr, 1, modesetbuf);
}
}
else if (level >= 4)
{
snprintf(modeunsetbuf, sizeof modeunsetbuf, "-%s", DEFCON_CMODE);
slog(LG_INFO, "DEFCON:MODE: %s", modeunsetbuf);
MOWGLI_PATRICIA_FOREACH(chptr, &state, chanlist)
{
channel_mode_va(svs->me, chptr, 1, modeunsetbuf);
}
}
}
static void defcon_timeoutfunc(void *dummy)
{
service_t *svs;
char buf[BUFSIZE];
svs = service_find("operserv");
level = 5;
defcon_svsignore();
defcon_forcechanmodes();
slog(LG_INFO, "DEFCON:TIMEOUT");
snprintf(buf, sizeof buf, "The DEFCON level is now back to normal (\2%d\2). Sorry for any inconvenience this caused.", level);
notice_global_sts(svs->me, "*", buf);
}
static void os_cmd_defcon(sourceinfo_t *si, int parc, char *parv[])
{
char *defcon = parv[0];
char buf[BUFSIZE];
if (!defcon)
{
command_success_nodata(si, _("Defense condition is currently level \2%d\2."), level);
return;
}
level = atoi(defcon);
if ((level <= 0) || (level > 5))
{
command_fail(si, fault_badparams, _("Defcon level must be between 1 and 5"));
level = 5;
return;
}
/* Call the 2 functions that don't use hooks */
defcon_svsignore();
defcon_forcechanmodes();
if (level < 5)
{
snprintf(buf, sizeof buf, "The DEFCON level has been changed to \2%d\2. We apologize for any inconvenience.", level);
if (defcon_timer == NULL)
defcon_timer = mowgli_timer_add_once(base_eventloop, "defcon_timeout", defcon_timeoutfunc, NULL, defcon_timeout);
}
else
{
snprintf(buf, sizeof buf, "The DEFCON level is now back to normal (\2%d\2). Sorry for any inconvenience this caused.", level);
mowgli_timer_destroy(base_eventloop, defcon_timer);
defcon_timer = NULL;
}
notice_global_sts(si->service->me, "*", buf);
command_success_nodata(si, _("Defense condition set to level \2%d\2."), level);
wallops(_("\2%s\2 set Defense condition to level \2%d\2."), get_oper_name(si), level);
logcommand(si, CMDLOG_ADMIN, "DEFCON: \2%d\2", level);
}
/* 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
*/