This repository was archived by the owner on Jun 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcs_quiet.c
485 lines (425 loc) · 12.9 KB
/
cs_quiet.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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
* Copyright (c) 2005 William Pitcock, et al.
* Rights to this code are as documented in doc/LICENSE.
*
* This file contains code for the CService QUIET/UNQUIET function.
*
*/
#include "atheme.h"
DECLARE_MODULE_V1
(
"freenode/cs_quiet", false, _modinit, _moddeinit,
PACKAGE_STRING,
"freenode <https://freenode.net>"
);
static void cs_cmd_quiet(sourceinfo_t *si, int parc, char *parv[]);
static void cs_cmd_unquiet(sourceinfo_t *si, int parc, char *parv[]);
static command_t cs_quiet = { "QUIET", N_("Sets a quiet on a channel."),
AC_AUTHENTICATED, 2, cs_cmd_quiet, { .path = "cservice/quiet" } };
static command_t cs_unquiet = { "UNQUIET", N_("Removes a quiet on a channel."),
AC_AUTHENTICATED, 2, cs_cmd_unquiet, { .path = "cservice/unquiet" } };
void _modinit(module_t *m)
{
MODULE_CONFLICT(m, "chanserv/quiet")
service_named_bind_command("chanserv", &cs_quiet);
service_named_bind_command("chanserv", &cs_unquiet);
}
void _moddeinit(module_unload_intent_t intent)
{
service_named_unbind_command("chanserv", &cs_quiet);
service_named_unbind_command("chanserv", &cs_unquiet);
}
static void make_extbanmask(char *buf, size_t buflen, const char *mask)
{
return_if_fail(buf != NULL);
return_if_fail(mask != NULL);
switch (ircd->type)
{
case PROTOCOL_INSPIRCD:
mowgli_strlcpy(buf, "m:", buflen);
break;
case PROTOCOL_UNREAL:
mowgli_strlcpy(buf, "~q:", buflen);
break;
default:
*buf = '\0';
break;
}
mowgli_strlcat(buf, mask, buflen);
}
static char get_quiet_ban_char(void)
{
return (ircd->type == PROTOCOL_UNREAL ||
ircd->type == PROTOCOL_INSPIRCD) ? 'b' : 'q';
}
static char *strip_extban(char *mask)
{
if (ircd->type == PROTOCOL_INSPIRCD)
return sstrdup(&mask[2]);
if (ircd->type == PROTOCOL_UNREAL)
return sstrdup(&mask[3]);
return sstrdup(mask);
}
chanban_t *place_quietmask(channel_t *c, int dir, const char *hostbuf)
{
char rhostbuf[BUFSIZE];
chanban_t *cb = NULL;
char banlike_char = get_quiet_ban_char();
make_extbanmask(rhostbuf, sizeof rhostbuf, hostbuf);
modestack_mode_param(chansvs.nick, c, MTYPE_ADD, banlike_char,
rhostbuf);
cb = chanban_add(c, rhostbuf, banlike_char);
return cb;
}
static void make_extban(char *buf, size_t size, user_t *tu)
{
return_if_fail(buf != NULL);
return_if_fail(tu != NULL);
switch (ircd->type)
{
case PROTOCOL_INSPIRCD:
mowgli_strlcpy(buf, "m:", size);
break;
case PROTOCOL_UNREAL:
mowgli_strlcpy(buf, "~q:", size);
break;
default:
*buf = '\0';
break;
}
mowgli_strlcat(buf, tu->nick, size);
mowgli_strlcat(buf, "!", size);
mowgli_strlcat(buf, tu->user, size);
mowgli_strlcat(buf, "@", size);
mowgli_strlcat(buf, tu->vhost, size);
}
enum devoice_result { DEVOICE_FAILED, DEVOICE_NO_ACTION, DEVOICE_DONE };
static enum devoice_result
devoice_user(sourceinfo_t *si, mychan_t *mc, channel_t *c, user_t *tu)
{
chanuser_t *cu;
unsigned int flag;
char buf[3];
enum devoice_result result = DEVOICE_NO_ACTION;
cu = chanuser_find(c, tu);
if (cu == NULL)
return DEVOICE_NO_ACTION;
if (cu->modes & CSTATUS_OP)
flag = CA_OP;
else if (cu->modes & CSTATUS_VOICE)
flag = CA_VOICE;
else
flag = 0;
if (flag != 0 && !chanacs_source_has_flag(mc, si, flag))
{
command_fail(si, fault_noprivs, _("You are not authorized to perform this operation."));
return DEVOICE_FAILED;
}
if (cu->modes & CSTATUS_OWNER || cu->modes & CSTATUS_PROTECT)
{
command_fail(si, fault_noprivs, _("\2%s\2 is protected from quiets; you cannot quiet them."), tu->nick);
return DEVOICE_FAILED;
}
buf[0] = '-';
buf[2] = '\0';
if (cu->modes & CSTATUS_OP)
{
channel_mode_va(chansvs.me->me, c, 2, "-o", tu->nick);
result = DEVOICE_DONE;
}
if (cu->modes & CSTATUS_VOICE)
{
channel_mode_va(chansvs.me->me, c, 2, "-v", tu->nick);
result = DEVOICE_DONE;
}
if (ircd->uses_owner && (cu->modes & ircd->owner_mode))
{
buf[1] = ircd->owner_mchar[1];
channel_mode_va(chansvs.me->me, c, 2, buf, tu->nick);
result = DEVOICE_DONE;
}
if (ircd->uses_protect && (cu->modes & ircd->protect_mode))
{
buf[1] = ircd->protect_mchar[1];
channel_mode_va(chansvs.me->me, c, 2, buf, tu->nick);
result = DEVOICE_DONE;
}
if (ircd->uses_halfops && (cu->modes & ircd->halfops_mode))
{
buf[1] = ircd->halfops_mchar[1];
channel_mode_va(chansvs.me->me, c, 2, buf, tu->nick);
result = DEVOICE_DONE;
}
return result;
}
/* Notify at most this many users in private notices, otherwise channel */
#define MAX_SINGLE_NOTIFY 3
static void notify_one_victim(sourceinfo_t *si, channel_t *c, user_t *u, int dir)
{
return_if_fail(dir == MTYPE_ADD || dir == MTYPE_DEL);
/* fantasy command, they can see it */
if (si->c != NULL)
return;
/* self */
if (si->su == u)
return;
if (dir == MTYPE_ADD)
change_notify(chansvs.nick, u,
"You have been quieted on %s by %s",
c->name, get_source_name(si));
else if (dir == MTYPE_DEL)
change_notify(chansvs.nick, u,
"You have been unquieted on %s by %s",
c->name, get_source_name(si));
}
static void notify_victims(sourceinfo_t *si, channel_t *c, chanban_t *cb, int dir)
{
mowgli_node_t *n;
chanuser_t *cu;
chanban_t tmpban;
mowgli_list_t ban_l = { NULL, NULL, 0 };
mowgli_node_t ban_n;
user_t *to_notify[MAX_SINGLE_NOTIFY];
unsigned int to_notify_count = 0, i;
char banlike_char = get_quiet_ban_char();
return_if_fail(dir == MTYPE_ADD || dir == MTYPE_DEL);
if (cb == NULL)
return;
/* fantasy command, they can see it */
if (si->c != NULL)
return;
/* some ircds use an action extban for mute
* strip it from those who do so we can reliably match users */
memcpy(&tmpban, cb, sizeof(chanban_t));
tmpban.mask = strip_extban(cb->mask);
/* only check the newly added/removed quiet */
mowgli_node_add(&tmpban, &ban_n, &ban_l);
MOWGLI_ITER_FOREACH(n, c->members.head)
{
cu = n->data;
if (cu->modes & (CSTATUS_OP | CSTATUS_VOICE))
continue;
if (is_internal_client(cu->user))
continue;
if (cu->user == si->su)
continue;
if (next_matching_ban(c, cu->user, banlike_char, &ban_n))
{
to_notify[to_notify_count++] = cu->user;
if (to_notify_count >= MAX_SINGLE_NOTIFY)
break;
}
}
if (to_notify_count >= MAX_SINGLE_NOTIFY)
{
if (dir == MTYPE_ADD)
notice(chansvs.nick, c->name,
"\2%s\2 quieted \2%s\2",
get_source_name(si), tmpban.mask);
else if (dir == MTYPE_DEL)
notice(chansvs.nick, c->name,
"\2%s\2 unquieted \2%s\2",
get_source_name(si), tmpban.mask);
}
else
for (i = 0; i < to_notify_count; i++)
notify_one_victim(si, c, to_notify[i], dir);
free(tmpban.mask);
}
static void cs_cmd_quiet(sourceinfo_t *si, int parc, char *parv[])
{
char *channel = parv[0];
char *target = parv[1];
char *newtarget;
channel_t *c = channel_find(channel);
mychan_t *mc = mychan_find(channel);
user_t *tu;
chanban_t *cb;
int n;
char *targetlist;
char *strtokctx = NULL;
enum devoice_result devoice_result;
if (!channel || !target)
{
command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "QUIET");
command_fail(si, fault_needmoreparams, _("Syntax: QUIET <#channel> <nickname|hostmask> [...]"));
return;
}
if (!mc)
{
command_fail(si, fault_nosuch_target, _("Channel \2%s\2 is not registered."), channel);
return;
}
if (!c)
{
command_fail(si, fault_nosuch_target, _("\2%s\2 is currently empty."), channel);
return;
}
if (!chanacs_source_has_flag(mc, si, CA_REMOVE))
{
command_fail(si, fault_noprivs, _("You are not authorized to perform this operation."));
return;
}
if (metadata_find(mc, "private:close:closer"))
{
command_fail(si, fault_noprivs, _("\2%s\2 is closed."), channel);
return;
}
targetlist = strdup(target);
target = strtok_r(targetlist, " ", &strtokctx);
do
{
if ((tu = user_find_named(target)))
{
char hostbuf[BUFSIZE];
devoice_result = devoice_user(si, mc, c, tu);
if (devoice_result == DEVOICE_FAILED)
continue;
hostbuf[0] = '\0';
mowgli_strlcat(hostbuf, "*!*@", BUFSIZE);
mowgli_strlcat(hostbuf, tu->vhost, BUFSIZE);
cb = place_quietmask(c, MTYPE_ADD, hostbuf);
n = remove_ban_exceptions(si->service->me, c, tu);
if (n > 0)
command_success_nodata(si, _("To ensure the quiet takes effect, %d ban exception(s) matching \2%s\2 have been removed from \2%s\2."), n, tu->nick, c->name);
/* Notify if we did anything. */
if (cb != NULL)
notify_victims(si, c, cb, MTYPE_ADD);
else if (devoice_result == DEVOICE_DONE || n > 0)
notify_one_victim(si, c, tu, MTYPE_ADD);
logcommand(si, CMDLOG_DO, "QUIET: \2%s\2 on \2%s\2 (for user \2%s!%s@%s\2)", hostbuf, mc->name, tu->nick, tu->user, tu->vhost);
if (si->su == NULL || !chanuser_find(mc->chan, si->su))
command_success_nodata(si, _("Quieted \2%s\2 on \2%s\2."), target, channel);
continue;
}
else if ((is_extban(target) && (newtarget = target)) || ((newtarget = pretty_mask(target)) && validhostmask(newtarget)))
{
cb = place_quietmask(c, MTYPE_ADD, newtarget);
notify_victims(si, c, cb, MTYPE_ADD);
logcommand(si, CMDLOG_DO, "QUIET: \2%s\2 on \2%s\2", newtarget, mc->name);
if (si->su == NULL || !chanuser_find(mc->chan, si->su))
command_success_nodata(si, _("Quieted \2%s\2 on \2%s\2."), newtarget, channel);
continue;
}
else
{
command_fail(si, fault_badparams, _("Invalid nickname/hostmask provided: \2%s\2"), target);
command_fail(si, fault_badparams, _("Syntax: QUIET <#channel> <nickname|hostmask> [,,,]"));
continue;
}
} while ((target = strtok_r(NULL, " ", &strtokctx)) != NULL);
free(targetlist);
}
static void cs_cmd_unquiet(sourceinfo_t *si, int parc, char *parv[])
{
const char *channel = parv[0];
const char *target = parv[1];
char banlike_char = get_quiet_ban_char();
channel_t *c = channel_find(channel);
mychan_t *mc = mychan_find(channel);
user_t *tu;
chanban_t *cb;
char *targetlist;
char *strtokctx;
char target_extban[BUFSIZE];
if (!channel)
{
command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "UNQUIET");
command_fail(si, fault_needmoreparams, _("Syntax: UNQUIET <#channel> <nickname|hostmask> [...]"));
return;
}
if (!target)
{
if (si->su == NULL)
{
command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "UNQUIET");
command_fail(si, fault_needmoreparams, _("Syntax: UNQUIET <#channel> <nickname|hostmask> [...]"));
return;
}
target = si->su->nick;
}
if (!mc)
{
command_fail(si, fault_nosuch_target, _("Channel \2%s\2 is not registered."), channel);
return;
}
if (!c)
{
command_fail(si, fault_nosuch_target, _("\2%s\2 is currently empty."), channel);
return;
}
if (!chanacs_source_has_flag(mc, si, CA_REMOVE) &&
(si->su == NULL ||
!chanacs_source_has_flag(mc, si, CA_EXEMPT) ||
irccasecmp(target, si->su->nick)))
{
command_fail(si, fault_noprivs, _("You are not authorized to perform this operation."));
return;
}
targetlist = strdup(target);
target = strtok_r(targetlist, " ", &strtokctx);
do
{
if ((tu = user_find_named(target)))
{
if (tu != si->su && !chanuser_find(c, tu))
{
command_fail(si, fault_nosuch_key, _("\2%s\2 is not on \2%s\2."), target, channel);
continue;
}
mowgli_node_t *n, *tn;
char hostbuf2[BUFSIZE];
int count = 0;
make_extban(hostbuf2, sizeof hostbuf2, tu);
for (n = next_matching_ban(c, tu, banlike_char, c->bans.head); n != NULL; n = next_matching_ban(c, tu, banlike_char, tn))
{
tn = n->next;
cb = n->data;
logcommand(si, CMDLOG_DO, "UNQUIET: \2%s\2 on \2%s\2 (for user \2%s\2)", cb->mask, mc->name, hostbuf2);
modestack_mode_param(chansvs.nick, c, MTYPE_DEL, cb->type, cb->mask);
chanban_delete(cb);
count++;
}
if (count > 0)
{
/* one notification only */
if (chanuser_find(c, tu))
notify_one_victim(si, c, tu, MTYPE_DEL);
command_success_nodata(si, _("Unquieted \2%s\2 on \2%s\2 (%d quiet%s removed)."),
target, channel, count, (count != 1 ? "s" : ""));
}
else
command_success_nodata(si, _("No quiets found matching \2%s\2 on \2%s\2."), target, channel);
continue;
}
else if (make_extbanmask(target_extban, sizeof target_extban, target),
(cb = chanban_find(c, target_extban, banlike_char)) != NULL ||
validhostmask(target))
{
if (cb != NULL)
{
modestack_mode_param(chansvs.nick, c, MTYPE_DEL, banlike_char, cb->mask);
notify_victims(si, c, cb, MTYPE_DEL);
chanban_delete(cb);
logcommand(si, CMDLOG_DO, "UNQUIET: \2%s\2 on \2%s\2", target_extban, mc->name);
if (si->su == NULL || !chanuser_find(mc->chan, si->su))
command_success_nodata(si, _("Unquieted \2%s\2 on \2%s\2."), target_extban, channel);
}
else
command_fail(si, fault_nosuch_key, _("No such quiet \2%s\2 on \2%s\2."), target_extban, channel);
continue;
}
else
{
command_fail(si, fault_badparams, _("Invalid nickname/hostmask provided: \2%s\2"), target);
command_fail(si, fault_badparams, _("Syntax: UNQUIET <#channel> [nickname|hostmask] [...]"));
continue;
}
} while ((target = strtok_r(NULL, " ", &strtokctx)) != NULL);
free(targetlist);
}
/* 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
*/