-
Notifications
You must be signed in to change notification settings - Fork 6
/
os_klinechan.c
234 lines (200 loc) · 6.65 KB
/
os_klinechan.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
/*
* Copyright (c) 2005-2016 Atheme Development Group
* Rights to this code are as documented in doc/LICENSE.
*
* Autokline channels.
*
* Default AKILL Time is based on the value of SET KLINETIME.
*/
#include "atheme.h"
static void
klinechan_check_join(struct hook_channel_joinpart *hdata)
{
struct mychan *mc;
struct chanuser *cu = hdata->cu;
struct service *svs;
const char *khost;
struct kline *k;
svs = service_find("operserv");
if (svs == NULL)
return;
if (cu == NULL || is_internal_client(cu->user))
return;
if (!(mc = mychan_from(cu->chan)))
return;
/* If they've already been sent a kline, do nothing */
if (cu->user->flags & UF_KLINESENT)
return;
if (metadata_find(mc, "private:klinechan:closer"))
{
khost = cu->user->ip ? cu->user->ip : cu->user->host;
if (has_priv_user(cu->user, PRIV_JOIN_STAFFONLY))
notice(svs->me->nick, cu->user->nick,
"Warning: %s klines normal users",
cu->chan->name);
else if (is_autokline_exempt(cu->user))
{
char buf[BUFSIZE];
snprintf(buf, sizeof(buf), "Not klining *@%s due to klinechan %s (user %s!%s@%s is exempt)",
khost, cu->chan->name,
cu->user->nick, cu->user->user, cu->user->host);
wallops_sts(buf);
}
else
{
struct metadata* md = metadata_find(mc, "private:klinechan:reason");
const char *reason = md != NULL ? md->value : "unknown";
slog(LG_INFO, "klinechan_check_join(): klining \2*@%s\2 (user \2%s!%s@%s\2 joined \2%s\2)",
khost, cu->user->nick,
cu->user->user, cu->user->host,
cu->chan->name);
k = kline_add("*", khost, reason, config_options.kline_time, "*");
cu->user->flags |= UF_KLINESENT;
}
}
}
static void
klinechan_show_info(struct hook_channel_req *hdata)
{
struct metadata *md;
const char *setter, *reason;
time_t ts;
struct tm tm;
char strfbuf[BUFSIZE];
if (!has_priv(hdata->si, PRIV_CHAN_AUSPEX))
return;
md = metadata_find(hdata->mc, "private:klinechan:closer");
if (md == NULL)
return;
setter = md->value;
md = metadata_find(hdata->mc, "private:klinechan:reason");
reason = md != NULL ? md->value : "unknown";
md = metadata_find(hdata->mc, "private:klinechan:timestamp");
ts = md != NULL ? atoi(md->value) : 0;
tm = *localtime(&ts);
strftime(strfbuf, sizeof strfbuf, TIME_FORMAT, &tm);
command_success_nodata(hdata->si, "%s had \2automatic klines\2 enabled on it by %s on %s (%s)", hdata->mc->name, setter, strfbuf, reason);
}
static void
os_cmd_klinechan(struct sourceinfo *si, int parc, char *parv[])
{
char *target = parv[0];
char *action = parv[1];
char *reason = parv[2];
struct mychan *mc;
if (!target || !action)
{
command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "KLINECHAN");
command_fail(si, fault_needmoreparams, "Usage: KLINECHAN <#channel> <ON|OFF> [reason]");
return;
}
if (!(mc = mychan_find(target)))
{
command_fail(si, fault_nosuch_target, STR_IS_NOT_REGISTERED, target);
return;
}
if (!strcasecmp(action, "ON"))
{
if (!reason)
{
command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "KLINECHAN");
command_fail(si, fault_needmoreparams, "Usage: KLINECHAN <#channel> ON <reason>");
return;
}
if (mc->flags & CHAN_LOG)
{
command_fail(si, fault_noprivs, "\2%s\2 cannot be closed.", target);
return;
}
if (metadata_find(mc, "private:klinechan:closer"))
{
command_fail(si, fault_nochange, "\2%s\2 is already on autokline.", target);
return;
}
metadata_add(mc, "private:klinechan:closer", si->su->nick);
metadata_add(mc, "private:klinechan:reason", reason);
metadata_add(mc, "private:klinechan:timestamp", number_to_string(CURRTIME));
wallops("%s enabled automatic klines on the channel \2%s\2 (%s).", get_oper_name(si), target, reason);
logcommand(si, CMDLOG_ADMIN, "KLINECHAN:ON: \2%s\2 (reason: \2%s\2)", target, reason);
command_success_nodata(si, "Klining all users joining \2%s\2.", target);
}
else if (!strcasecmp(action, "OFF"))
{
if (!metadata_find(mc, "private:klinechan:closer"))
{
command_fail(si, fault_nochange, "\2%s\2 is not closed.", target);
return;
}
metadata_delete(mc, "private:klinechan:closer");
metadata_delete(mc, "private:klinechan:reason");
metadata_delete(mc, "private:klinechan:timestamp");
wallops("%s disabled automatic klines on the channel \2%s\2.", get_oper_name(si), target);
logcommand(si, CMDLOG_ADMIN, "KLINECHAN:OFF: \2%s\2", target);
command_success_nodata(si, "No longer klining users joining \2%s\2.", target);
}
else
{
command_fail(si, fault_badparams, STR_INVALID_PARAMS, "KLINECHAN");
command_fail(si, fault_badparams, "Usage: KLINECHAN <#channel> <ON|OFF> [reason]");
}
}
static void
os_cmd_listklinechans(struct sourceinfo *si, int parc, char *parv[])
{
const char *pattern;
mowgli_patricia_iteration_state_t state;
struct mychan *mc;
struct metadata *md;
unsigned int matches = 0;
pattern = parc >= 1 ? parv[0] : "*";
MOWGLI_PATRICIA_FOREACH(mc, &state, mclist)
{
md = metadata_find(mc, "private:klinechan:closer");
if (md == NULL)
continue;
if (!match(pattern, mc->name))
{
command_success_nodata(si, "- %-30s", mc->name);
matches++;
}
}
logcommand(si, CMDLOG_ADMIN, "LISTKLINECHANS: \2%s\2 (\2%u\2 matches)", pattern, matches);
if (matches == 0)
command_success_nodata(si, _("No K:line channels matched pattern \2%s\2"), pattern);
else
command_success_nodata(si, ngettext(N_("\2%u\2 match for pattern \2%s\2"),
N_("\2%u\2 matches for pattern \2%s\2"), matches), matches, pattern);
}
static struct command os_klinechan = {
.name = "KLINECHAN",
.desc = N_("Klines all users joining a channel for the duration set by SET KLINETIME."),
.access = PRIV_MASS_AKILL,
.maxparc = 3,
.cmd = &os_cmd_klinechan,
.help = { .path = "contrib/klinechan" },
};
static struct command os_listklinechans = {
.name = "LISTKLINECHAN",
.desc = N_("Lists active K:line channels."),
.access = PRIV_MASS_AKILL,
.maxparc = 1,
.cmd = &os_cmd_listklinechans,
.help = { .path = "contrib/listklinechans" },
};
static void
mod_init(struct module *const restrict m)
{
service_named_bind_command("operserv", &os_klinechan);
service_named_bind_command("operserv", &os_listklinechans);
hook_add_first_channel_join(klinechan_check_join);
hook_add_channel_info(klinechan_show_info);
}
static void
mod_deinit(const enum module_unload_intent intent)
{
service_named_unbind_command("operserv", &os_klinechan);
service_named_unbind_command("operserv", &os_listklinechans);
hook_del_channel_join(klinechan_check_join);
hook_del_channel_info(klinechan_show_info);
}
SIMPLE_DECLARE_MODULE_V1("contrib/os_klinechan", MODULE_UNLOAD_CAPABILITY_OK)