| 1 |
/* |
| 2 |
* Copyright (c) 2005 William Pitcock, et al. |
| 3 |
* Rights to this code are as documented in doc/LICENSE. |
| 4 |
* |
| 5 |
* This file contains code for the CService QUIET/UNQUIET function. |
| 6 |
* |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "atheme.h" |
| 10 |
#include "chanserv.h" |
| 11 |
|
| 12 |
DECLARE_MODULE_V1 |
| 13 |
( |
| 14 |
"chanserv/quiet", false, _modinit, _moddeinit, |
| 15 |
PACKAGE_STRING, |
| 16 |
VENDOR_STRING |
| 17 |
); |
| 18 |
|
| 19 |
static void cs_cmd_quiet(sourceinfo_t *si, int parc, char *parv[]); |
| 20 |
static void cs_cmd_unquiet(sourceinfo_t *si, int parc, char *parv[]); |
| 21 |
|
| 22 |
command_t cs_quiet = { "QUIET", N_("Sets a quiet on a channel."), |
| 23 |
AC_AUTHENTICATED, 2, cs_cmd_quiet, { .path = "cservice/quiet" } }; |
| 24 |
command_t cs_unquiet = { "UNQUIET", N_("Removes a quiet on a channel."), |
| 25 |
AC_AUTHENTICATED, 2, cs_cmd_unquiet, { .path = "cservice/unquiet" } }; |
| 26 |
|
| 27 |
void _modinit(module_t *m) |
| 28 |
{ |
| 29 |
service_named_bind_command("chanserv", &cs_quiet); |
| 30 |
service_named_bind_command("chanserv", &cs_unquiet); |
| 31 |
} |
| 32 |
|
| 33 |
void _moddeinit(module_unload_intent_t intent) |
| 34 |
{ |
| 35 |
service_named_unbind_command("chanserv", &cs_quiet); |
| 36 |
service_named_unbind_command("chanserv", &cs_unquiet); |
| 37 |
} |
| 38 |
|
| 39 |
static void make_extbanmask(char *buf, size_t buflen, const char *mask) |
| 40 |
{ |
| 41 |
return_if_fail(buf != NULL); |
| 42 |
return_if_fail(mask != NULL); |
| 43 |
|
| 44 |
switch (ircd->type) |
| 45 |
{ |
| 46 |
case PROTOCOL_INSPIRCD: |
| 47 |
mowgli_strlcpy(buf, "m:", buflen); |
| 48 |
break; |
| 49 |
case PROTOCOL_UNREAL: |
| 50 |
mowgli_strlcpy(buf, "~q:", buflen); |
| 51 |
break; |
| 52 |
default: |
| 53 |
*buf = '\0'; |
| 54 |
break; |
| 55 |
} |
| 56 |
|
| 57 |
mowgli_strlcat(buf, mask, buflen); |
| 58 |
} |
| 59 |
|
| 60 |
static char get_quiet_ban_char(void) |
| 61 |
{ |
| 62 |
return (ircd->type == PROTOCOL_UNREAL || |
| 63 |
ircd->type == PROTOCOL_INSPIRCD) ? 'b' : 'q'; |
| 64 |
} |
| 65 |
|
| 66 |
static char *strip_extban(char *mask) |
| 67 |
{ |
| 68 |
if (ircd->type == PROTOCOL_INSPIRCD) |
| 69 |
return sstrdup(&mask[2]); |
| 70 |
if (ircd->type == PROTOCOL_UNREAL) |
| 71 |
return sstrdup(&mask[3]); |
| 72 |
return sstrdup(mask); |
| 73 |
} |
| 74 |
|
| 75 |
chanban_t *place_quietmask(channel_t *c, int dir, const char *hostbuf) |
| 76 |
{ |
| 77 |
char rhostbuf[BUFSIZE]; |
| 78 |
chanban_t *cb = NULL; |
| 79 |
char banlike_char = get_quiet_ban_char(); |
| 80 |
|
| 81 |
make_extbanmask(rhostbuf, sizeof rhostbuf, hostbuf); |
| 82 |
modestack_mode_param(chansvs.nick, c, MTYPE_ADD, banlike_char, |
| 83 |
rhostbuf); |
| 84 |
cb = chanban_add(c, rhostbuf, banlike_char); |
| 85 |
|
| 86 |
return cb; |
| 87 |
} |
| 88 |
|
| 89 |
static void make_extban(char *buf, size_t size, user_t *tu) |
| 90 |
{ |
| 91 |
return_if_fail(buf != NULL); |
| 92 |
return_if_fail(tu != NULL); |
| 93 |
|
| 94 |
switch (ircd->type) |
| 95 |
{ |
| 96 |
case PROTOCOL_INSPIRCD: |
| 97 |
mowgli_strlcpy(buf, "m:", size); |
| 98 |
break; |
| 99 |
case PROTOCOL_UNREAL: |
| 100 |
mowgli_strlcpy(buf, "~q:", size); |
| 101 |
break; |
| 102 |
default: |
| 103 |
*buf = '\0'; |
| 104 |
break; |
| 105 |
} |
| 106 |
|
| 107 |
mowgli_strlcat(buf, tu->nick, size); |
| 108 |
mowgli_strlcat(buf, "!", size); |
| 109 |
mowgli_strlcat(buf, tu->user, size); |
| 110 |
mowgli_strlcat(buf, "@", size); |
| 111 |
mowgli_strlcat(buf, tu->vhost, size); |
| 112 |
} |
| 113 |
|
| 114 |
enum devoice_result { DEVOICE_FAILED, DEVOICE_NO_ACTION, DEVOICE_DONE }; |
| 115 |
|
| 116 |
static enum devoice_result |
| 117 |
devoice_user(sourceinfo_t *si, mychan_t *mc, channel_t *c, user_t *tu) |
| 118 |
{ |
| 119 |
chanuser_t *cu; |
| 120 |
unsigned int flag; |
| 121 |
char buf[3]; |
| 122 |
enum devoice_result result = DEVOICE_NO_ACTION; |
| 123 |
|
| 124 |
cu = chanuser_find(c, tu); |
| 125 |
if (cu == NULL) |
| 126 |
return DEVOICE_NO_ACTION; |
| 127 |
if (cu->modes & CSTATUS_OP) |
| 128 |
flag = CA_OP; |
| 129 |
else if (cu->modes & CSTATUS_VOICE) |
| 130 |
flag = CA_VOICE; |
| 131 |
else |
| 132 |
flag = 0; |
| 133 |
if (flag != 0 && !chanacs_source_has_flag(mc, si, flag)) |
| 134 |
{ |
| 135 |
command_fail(si, fault_noprivs, _("You are not authorized to perform this operation.")); |
| 136 |
return DEVOICE_FAILED; |
| 137 |
} |
| 138 |
|
| 139 |
if (cu->modes & CSTATUS_OWNER || cu->modes & CSTATUS_PROTECT) |
| 140 |
{ |
| 141 |
command_fail(si, fault_noprivs, _("\2%s\2 is protected from quiets; you cannot quiet them."), tu->nick); |
| 142 |
return DEVOICE_FAILED; |
| 143 |
} |
| 144 |
|
| 145 |
buf[0] = '-'; |
| 146 |
buf[2] = '\0'; |
| 147 |
if (cu->modes & CSTATUS_OP) |
| 148 |
{ |
| 149 |
channel_mode_va(chansvs.me->me, c, 2, "-o", tu->nick); |
| 150 |
result = DEVOICE_DONE; |
| 151 |
} |
| 152 |
if (cu->modes & CSTATUS_VOICE) |
| 153 |
{ |
| 154 |
channel_mode_va(chansvs.me->me, c, 2, "-v", tu->nick); |
| 155 |
result = DEVOICE_DONE; |
| 156 |
} |
| 157 |
if (ircd->uses_owner && (cu->modes & ircd->owner_mode)) |
| 158 |
{ |
| 159 |
buf[1] = ircd->owner_mchar[1]; |
| 160 |
channel_mode_va(chansvs.me->me, c, 2, buf, tu->nick); |
| 161 |
result = DEVOICE_DONE; |
| 162 |
} |
| 163 |
if (ircd->uses_protect && (cu->modes & ircd->protect_mode)) |
| 164 |
{ |
| 165 |
buf[1] = ircd->protect_mchar[1]; |
| 166 |
channel_mode_va(chansvs.me->me, c, 2, buf, tu->nick); |
| 167 |
result = DEVOICE_DONE; |
| 168 |
} |
| 169 |
if (ircd->uses_halfops && (cu->modes & ircd->halfops_mode)) |
| 170 |
{ |
| 171 |
buf[1] = ircd->halfops_mchar[1]; |
| 172 |
channel_mode_va(chansvs.me->me, c, 2, buf, tu->nick); |
| 173 |
result = DEVOICE_DONE; |
| 174 |
} |
| 175 |
|
| 176 |
return result; |
| 177 |
} |
| 178 |
|
| 179 |
/* Notify at most this many users in private notices, otherwise channel */ |
| 180 |
#define MAX_SINGLE_NOTIFY 3 |
| 181 |
|
| 182 |
static void notify_one_victim(sourceinfo_t *si, channel_t *c, user_t *u, int dir) |
| 183 |
{ |
| 184 |
return_if_fail(dir == MTYPE_ADD || dir == MTYPE_DEL); |
| 185 |
|
| 186 |
/* fantasy command, they can see it */ |
| 187 |
if (si->c != NULL) |
| 188 |
return; |
| 189 |
/* self */ |
| 190 |
if (si->su == u) |
| 191 |
return; |
| 192 |
|
| 193 |
if (dir == MTYPE_ADD) |
| 194 |
change_notify(chansvs.nick, u, |
| 195 |
"You have been quieted on %s by %s", |
| 196 |
c->name, get_source_name(si)); |
| 197 |
else if (dir == MTYPE_DEL) |
| 198 |
change_notify(chansvs.nick, u, |
| 199 |
"You have been unquieted on %s by %s", |
| 200 |
c->name, get_source_name(si)); |
| 201 |
} |
| 202 |
|
| 203 |
static void notify_victims(sourceinfo_t *si, channel_t *c, chanban_t *cb, int dir) |
| 204 |
{ |
| 205 |
mowgli_node_t *n; |
| 206 |
chanuser_t *cu; |
| 207 |
chanban_t tmpban; |
| 208 |
mowgli_list_t ban_l = { NULL, NULL, 0 }; |
| 209 |
mowgli_node_t ban_n; |
| 210 |
user_t *to_notify[MAX_SINGLE_NOTIFY]; |
| 211 |
unsigned int to_notify_count = 0, i; |
| 212 |
char banlike_char = get_quiet_ban_char(); |
| 213 |
|
| 214 |
return_if_fail(dir == MTYPE_ADD || dir == MTYPE_DEL); |
| 215 |
|
| 216 |
if (cb == NULL) |
| 217 |
return; |
| 218 |
|
| 219 |
/* fantasy command, they can see it */ |
| 220 |
if (si->c != NULL) |
| 221 |
return; |
| 222 |
|
| 223 |
/* some ircds use an action extban for mute |
| 224 |
* strip it from those who do so we can reliably match users */ |
| 225 |
memcpy(&tmpban, cb, sizeof(chanban_t)); |
| 226 |
tmpban.mask = strip_extban(cb->mask); |
| 227 |
|
| 228 |
/* only check the newly added/removed quiet */ |
| 229 |
mowgli_node_add(&tmpban, &ban_n, &ban_l); |
| 230 |
|
| 231 |
MOWGLI_ITER_FOREACH(n, c->members.head) |
| 232 |
{ |
| 233 |
cu = n->data; |
| 234 |
if (cu->modes & (CSTATUS_OP | CSTATUS_VOICE)) |
| 235 |
continue; |
| 236 |
if (is_internal_client(cu->user)) |
| 237 |
continue; |
| 238 |
if (cu->user == si->su) |
| 239 |
continue; |
| 240 |
if (next_matching_ban(c, cu->user, banlike_char, &ban_n)) |
| 241 |
{ |
| 242 |
to_notify[to_notify_count++] = cu->user; |
| 243 |
if (to_notify_count >= MAX_SINGLE_NOTIFY) |
| 244 |
break; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
if (to_notify_count >= MAX_SINGLE_NOTIFY) |
| 249 |
{ |
| 250 |
if (dir == MTYPE_ADD) |
| 251 |
notice(chansvs.nick, c->name, |
| 252 |
"\2%s\2 quieted \2%s\2", |
| 253 |
get_source_name(si), tmpban.mask); |
| 254 |
else if (dir == MTYPE_DEL) |
| 255 |
notice(chansvs.nick, c->name, |
| 256 |
"\2%s\2 unquieted \2%s\2", |
| 257 |
get_source_name(si), tmpban.mask); |
| 258 |
} |
| 259 |
else |
| 260 |
for (i = 0; i < to_notify_count; i++) |
| 261 |
notify_one_victim(si, c, to_notify[i], dir); |
| 262 |
|
| 263 |
free(tmpban.mask); |
| 264 |
} |
| 265 |
|
| 266 |
static void cs_cmd_quiet(sourceinfo_t *si, int parc, char *parv[]) |
| 267 |
{ |
| 268 |
char *channel = parv[0]; |
| 269 |
char *target = parv[1]; |
| 270 |
char *newtarget; |
| 271 |
channel_t *c = channel_find(channel); |
| 272 |
mychan_t *mc = mychan_find(channel); |
| 273 |
user_t *tu; |
| 274 |
chanban_t *cb; |
| 275 |
int n; |
| 276 |
char *targetlist; |
| 277 |
char *strtokctx = NULL; |
| 278 |
enum devoice_result devoice_result; |
| 279 |
|
| 280 |
if (!channel || !target) |
| 281 |
{ |
| 282 |
command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "QUIET"); |
| 283 |
command_fail(si, fault_needmoreparams, _("Syntax: QUIET <#channel> <nickname|hostmask> [...]")); |
| 284 |
return; |
| 285 |
} |
| 286 |
|
| 287 |
if (!mc) |
| 288 |
{ |
| 289 |
command_fail(si, fault_nosuch_target, _("Channel \2%s\2 is not registered."), channel); |
| 290 |
return; |
| 291 |
} |
| 292 |
|
| 293 |
if (!c) |
| 294 |
{ |
| 295 |
command_fail(si, fault_nosuch_target, _("\2%s\2 is currently empty."), channel); |
| 296 |
return; |
| 297 |
} |
| 298 |
|
| 299 |
if (!chanacs_source_has_flag(mc, si, CA_REMOVE)) |
| 300 |
{ |
| 301 |
command_fail(si, fault_noprivs, _("You are not authorized to perform this operation.")); |
| 302 |
return; |
| 303 |
} |
| 304 |
|
| 305 |
if (metadata_find(mc, "private:close:closer")) |
| 306 |
{ |
| 307 |
command_fail(si, fault_noprivs, _("\2%s\2 is closed."), channel); |
| 308 |
return; |
| 309 |
} |
| 310 |
|
| 311 |
targetlist = strdup(target); |
| 312 |
target = strtok_r(targetlist, " ", &strtokctx); |
| 313 |
do |
| 314 |
{ |
| 315 |
if ((tu = user_find_named(target))) |
| 316 |
{ |
| 317 |
char hostbuf[BUFSIZE]; |
| 318 |
|
| 319 |
devoice_result = devoice_user(si, mc, c, tu); |
| 320 |
if (devoice_result == DEVOICE_FAILED) |
| 321 |
continue; |
| 322 |
|
| 323 |
hostbuf[0] = '\0'; |
| 324 |
|
| 325 |
mowgli_strlcat(hostbuf, "*!*@", BUFSIZE); |
| 326 |
mowgli_strlcat(hostbuf, tu->vhost, BUFSIZE); |
| 327 |
|
| 328 |
cb = place_quietmask(c, MTYPE_ADD, hostbuf); |
| 329 |
n = remove_ban_exceptions(si->service->me, c, tu); |
| 330 |
if (n > 0) |
| 331 |
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); |
| 332 |
/* Notify if we did anything. */ |
| 333 |
if (cb != NULL) |
| 334 |
notify_victims(si, c, cb, MTYPE_ADD); |
| 335 |
else if (devoice_result == DEVOICE_DONE || n > 0) |
| 336 |
notify_one_victim(si, c, tu, MTYPE_ADD); |
| 337 |
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); |
| 338 |
if (si->su == NULL || !chanuser_find(mc->chan, si->su)) |
| 339 |
command_success_nodata(si, _("Quieted \2%s\2 on \2%s\2."), target, channel); |
| 340 |
continue; |
| 341 |
} |
| 342 |
else if ((is_extban(target) && (newtarget = target)) || ((newtarget = pretty_mask(target)) && validhostmask(newtarget))) |
| 343 |
{ |
| 344 |
cb = place_quietmask(c, MTYPE_ADD, newtarget); |
| 345 |
notify_victims(si, c, cb, MTYPE_ADD); |
| 346 |
logcommand(si, CMDLOG_DO, "QUIET: \2%s\2 on \2%s\2", newtarget, mc->name); |
| 347 |
if (si->su == NULL || !chanuser_find(mc->chan, si->su)) |
| 348 |
command_success_nodata(si, _("Quieted \2%s\2 on \2%s\2."), newtarget, channel); |
| 349 |
continue; |
| 350 |
} |
| 351 |
else |
| 352 |
{ |
| 353 |
command_fail(si, fault_badparams, _("Invalid nickname/hostmask provided: \2%s\2"), target); |
| 354 |
command_fail(si, fault_badparams, _("Syntax: QUIET <#channel> <nickname|hostmask> [,,,]")); |
| 355 |
continue; |
| 356 |
} |
| 357 |
} while ((target = strtok_r(NULL, " ", &strtokctx)) != NULL); |
| 358 |
free(targetlist); |
| 359 |
} |
| 360 |
|
| 361 |
static void cs_cmd_unquiet(sourceinfo_t *si, int parc, char *parv[]) |
| 362 |
{ |
| 363 |
const char *channel = parv[0]; |
| 364 |
const char *target = parv[1]; |
| 365 |
char banlike_char = get_quiet_ban_char(); |
| 366 |
channel_t *c = channel_find(channel); |
| 367 |
mychan_t *mc = mychan_find(channel); |
| 368 |
user_t *tu; |
| 369 |
chanban_t *cb; |
| 370 |
char *targetlist; |
| 371 |
char *strtokctx; |
| 372 |
char target_extban[BUFSIZE]; |
| 373 |
|
| 374 |
if (!channel) |
| 375 |
{ |
| 376 |
command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "UNQUIET"); |
| 377 |
command_fail(si, fault_needmoreparams, _("Syntax: UNQUIET <#channel> <nickname|hostmask> [...]")); |
| 378 |
return; |
| 379 |
} |
| 380 |
|
| 381 |
if (!target) |
| 382 |
{ |
| 383 |
if (si->su == NULL) |
| 384 |
{ |
| 385 |
command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "UNQUIET"); |
| 386 |
command_fail(si, fault_needmoreparams, _("Syntax: UNQUIET <#channel> <nickname|hostmask> [...]")); |
| 387 |
return; |
| 388 |
} |
| 389 |
target = si->su->nick; |
| 390 |
} |
| 391 |
|
| 392 |
if (!mc) |
| 393 |
{ |
| 394 |
command_fail(si, fault_nosuch_target, _("Channel \2%s\2 is not registered."), channel); |
| 395 |
return; |
| 396 |
} |
| 397 |
|
| 398 |
if (!c) |
| 399 |
{ |
| 400 |
command_fail(si, fault_nosuch_target, _("\2%s\2 is currently empty."), channel); |
| 401 |
return; |
| 402 |
} |
| 403 |
|
| 404 |
if (!chanacs_source_has_flag(mc, si, CA_REMOVE) && |
| 405 |
(si->su == NULL || |
| 406 |
!chanacs_source_has_flag(mc, si, CA_EXEMPT) || |
| 407 |
irccasecmp(target, si->su->nick))) |
| 408 |
{ |
| 409 |
command_fail(si, fault_noprivs, _("You are not authorized to perform this operation.")); |
| 410 |
return; |
| 411 |
} |
| 412 |
|
| 413 |
targetlist = strdup(target); |
| 414 |
target = strtok_r(targetlist, " ", &strtokctx); |
| 415 |
do |
| 416 |
{ |
| 417 |
if ((tu = user_find_named(target))) |
| 418 |
{ |
| 419 |
mowgli_node_t *n, *tn; |
| 420 |
char hostbuf2[BUFSIZE]; |
| 421 |
int count = 0; |
| 422 |
|
| 423 |
make_extban(hostbuf2, sizeof hostbuf2, tu); |
| 424 |
for (n = next_matching_ban(c, tu, banlike_char, c->bans.head); n != NULL; n = next_matching_ban(c, tu, banlike_char, tn)) |
| 425 |
{ |
| 426 |
tn = n->next; |
| 427 |
cb = n->data; |
| 428 |
|
| 429 |
logcommand(si, CMDLOG_DO, "UNQUIET: \2%s\2 on \2%s\2 (for user \2%s\2)", cb->mask, mc->name, hostbuf2); |
| 430 |
modestack_mode_param(chansvs.nick, c, MTYPE_DEL, cb->type, cb->mask); |
| 431 |
chanban_delete(cb); |
| 432 |
count++; |
| 433 |
} |
| 434 |
if (count > 0) |
| 435 |
{ |
| 436 |
/* one notification only */ |
| 437 |
if (chanuser_find(c, tu)) |
| 438 |
notify_one_victim(si, c, tu, MTYPE_DEL); |
| 439 |
command_success_nodata(si, _("Unquieted \2%s\2 on \2%s\2 (%d quiet%s removed)."), |
| 440 |
target, channel, count, (count != 1 ? "s" : "")); |
| 441 |
} |
| 442 |
else |
| 443 |
command_success_nodata(si, _("No quiets found matching \2%s\2 on \2%s\2."), target, channel); |
| 444 |
continue; |
| 445 |
} |
| 446 |
else if (make_extbanmask(target_extban, sizeof target_extban, target), |
| 447 |
(cb = chanban_find(c, target_extban, banlike_char)) != NULL || |
| 448 |
validhostmask(target)) |
| 449 |
{ |
| 450 |
if (cb != NULL) |
| 451 |
{ |
| 452 |
modestack_mode_param(chansvs.nick, c, MTYPE_DEL, banlike_char, cb->mask); |
| 453 |
notify_victims(si, c, cb, MTYPE_DEL); |
| 454 |
chanban_delete(cb); |
| 455 |
logcommand(si, CMDLOG_DO, "UNQUIET: \2%s\2 on \2%s\2", target_extban, mc->name); |
| 456 |
if (si->su == NULL || !chanuser_find(mc->chan, si->su)) |
| 457 |
command_success_nodata(si, _("Unquieted \2%s\2 on \2%s\2."), target_extban, channel); |
| 458 |
} |
| 459 |
else |
| 460 |
command_fail(si, fault_nosuch_key, _("No such quiet \2%s\2 on \2%s\2."), target_extban, channel); |
| 461 |
|
| 462 |
continue; |
| 463 |
} |
| 464 |
else |
| 465 |
{ |
| 466 |
command_fail(si, fault_badparams, _("Invalid nickname/hostmask provided: \2%s\2"), target); |
| 467 |
command_fail(si, fault_badparams, _("Syntax: UNQUIET <#channel> [nickname|hostmask] [...]")); |
| 468 |
continue; |
| 469 |
} |
| 470 |
} while ((target = strtok_r(NULL, " ", &strtokctx)) != NULL); |
| 471 |
free(targetlist); |
| 472 |
} |
| 473 |
|
| 474 |
/* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs |
| 475 |
* vim:ts=8 |
| 476 |
* vim:sw=8 |
| 477 |
* vim:noexpandtab |
| 478 |
*/ |