| 1 |
/* |
| 2 |
* Copyright (c) 2005 Atheme Development Group |
| 3 |
* Rights to this code are documented in doc/LICENSE. |
| 4 |
* |
| 5 |
* This file contains the main() routine. |
| 6 |
* |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "atheme.h" |
| 10 |
#include "chanserv.h" |
| 11 |
#include "template.h" |
| 12 |
#include <limits.h> |
| 13 |
|
| 14 |
DECLARE_MODULE_V1 |
| 15 |
( |
| 16 |
"chanserv/main", true, _modinit, _moddeinit, |
| 17 |
PACKAGE_STRING, |
| 18 |
VENDOR_STRING |
| 19 |
); |
| 20 |
|
| 21 |
static void cs_join(hook_channel_joinpart_t *hdata); |
| 22 |
static void cs_part(hook_channel_joinpart_t *hdata); |
| 23 |
static void cs_register(hook_channel_req_t *mc); |
| 24 |
static void cs_succession(hook_channel_succession_req_t *data); |
| 25 |
static void cs_newchan(channel_t *c); |
| 26 |
static void cs_keeptopic_topicset(channel_t *c); |
| 27 |
static void cs_topiccheck(hook_channel_topic_check_t *data); |
| 28 |
static void cs_tschange(channel_t *c); |
| 29 |
static void cs_leave_empty(void *unused); |
| 30 |
static void cs_bounce_mode_change(hook_channel_mode_change_t *data); |
| 31 |
static void on_shutdown(void *unused); |
| 32 |
|
| 33 |
static mowgli_eventloop_timer_t *cs_leave_empty_timer = NULL; |
| 34 |
|
| 35 |
static void join_registered(bool all) |
| 36 |
{ |
| 37 |
mychan_t *mc; |
| 38 |
mowgli_patricia_iteration_state_t state; |
| 39 |
|
| 40 |
MOWGLI_PATRICIA_FOREACH(mc, &state, mclist) |
| 41 |
{ |
| 42 |
if (!(mc->flags & MC_GUARD)) |
| 43 |
continue; |
| 44 |
if (metadata_find(mc, "private:botserv:bot-assigned") != NULL) |
| 45 |
continue; |
| 46 |
|
| 47 |
if (all) |
| 48 |
{ |
| 49 |
join(mc->name, chansvs.nick); |
| 50 |
continue; |
| 51 |
} |
| 52 |
else if (mc->chan != NULL && mc->chan->members.count != 0) |
| 53 |
{ |
| 54 |
join(mc->name, chansvs.nick); |
| 55 |
continue; |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
/* main services client routine */ |
| 61 |
static void chanserv(sourceinfo_t *si, int parc, char *parv[]) |
| 62 |
{ |
| 63 |
mychan_t *mc = NULL; |
| 64 |
char orig[BUFSIZE]; |
| 65 |
char newargs[BUFSIZE]; |
| 66 |
char *cmd; |
| 67 |
char *args; |
| 68 |
|
| 69 |
/* this should never happen */ |
| 70 |
if (parv[parc - 2][0] == '&') |
| 71 |
{ |
| 72 |
slog(LG_ERROR, "services(): got parv with local channel: %s", parv[0]); |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
/* is this a fantasy command? */ |
| 77 |
if (parv[parc - 2][0] == '#') |
| 78 |
{ |
| 79 |
metadata_t *md; |
| 80 |
|
| 81 |
if (chansvs.fantasy == false) |
| 82 |
{ |
| 83 |
/* *all* fantasy disabled */ |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
mc = mychan_find(parv[parc - 2]); |
| 88 |
if (!mc) |
| 89 |
{ |
| 90 |
/* unregistered, NFI how we got this message, but let's leave it alone! */ |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
md = metadata_find(mc, "disable_fantasy"); |
| 95 |
if (md) |
| 96 |
{ |
| 97 |
/* fantasy disabled on this channel. don't message them, just bail. */ |
| 98 |
return; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
/* make a copy of the original for debugging */ |
| 103 |
mowgli_strlcpy(orig, parv[parc - 1], BUFSIZE); |
| 104 |
|
| 105 |
/* lets go through this to get the command */ |
| 106 |
cmd = strtok(parv[parc - 1], " "); |
| 107 |
|
| 108 |
if (!cmd) |
| 109 |
return; |
| 110 |
if (*orig == '\001') |
| 111 |
{ |
| 112 |
handle_ctcp_common(si, cmd, strtok(NULL, "")); |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
/* take the command through the hash table */ |
| 117 |
if (mc == NULL) |
| 118 |
command_exec_split(si->service, si, cmd, strtok(NULL, ""), si->service->commands); |
| 119 |
else |
| 120 |
{ |
| 121 |
metadata_t *md = metadata_find(mc, "private:prefix"); |
| 122 |
const char *prefix = (md ? md->value : chansvs.trigger); |
| 123 |
|
| 124 |
if (strlen(cmd) >= 2 && strchr(prefix, cmd[0]) && isalpha((unsigned char)*++cmd)) |
| 125 |
{ |
| 126 |
const char *realcmd = service_resolve_alias(si->service, NULL, cmd); |
| 127 |
|
| 128 |
/* XXX not really nice to look up the command twice |
| 129 |
* -- jilles */ |
| 130 |
if (command_find(si->service->commands, realcmd) == NULL) |
| 131 |
return; |
| 132 |
if (floodcheck(si->su, si->service->me)) |
| 133 |
return; |
| 134 |
/* construct <channel> <args> */ |
| 135 |
mowgli_strlcpy(newargs, parv[parc - 2], sizeof newargs); |
| 136 |
args = strtok(NULL, ""); |
| 137 |
if (args != NULL) |
| 138 |
{ |
| 139 |
mowgli_strlcat(newargs, " ", sizeof newargs); |
| 140 |
mowgli_strlcat(newargs, args, sizeof newargs); |
| 141 |
} |
| 142 |
/* let the command know it's called as fantasy cmd */ |
| 143 |
si->c = mc->chan; |
| 144 |
/* fantasy commands are always verbose |
| 145 |
* (a little ugly but this way we can !set verbose) |
| 146 |
*/ |
| 147 |
mc->flags |= MC_FORCEVERBOSE; |
| 148 |
command_exec_split(si->service, si, realcmd, newargs, si->service->commands); |
| 149 |
mc->flags &= ~MC_FORCEVERBOSE; |
| 150 |
} |
| 151 |
else if (!ircncasecmp(cmd, chansvs.nick, strlen(chansvs.nick)) && !isalnum((unsigned char)cmd[strlen(chansvs.nick)]) && (cmd = strtok(NULL, "")) != NULL) |
| 152 |
{ |
| 153 |
const char *realcmd; |
| 154 |
char *pptr; |
| 155 |
|
| 156 |
mowgli_strlcpy(newargs, parv[parc - 2], sizeof newargs); |
| 157 |
while (*cmd == ' ') |
| 158 |
cmd++; |
| 159 |
if ((pptr = strchr(cmd, ' ')) != NULL) |
| 160 |
{ |
| 161 |
mowgli_strlcat(newargs, pptr, sizeof newargs); |
| 162 |
*pptr = '\0'; |
| 163 |
} |
| 164 |
|
| 165 |
realcmd = service_resolve_alias(si->service, NULL, cmd); |
| 166 |
|
| 167 |
if (command_find(si->service->commands, realcmd) == NULL) |
| 168 |
return; |
| 169 |
if (floodcheck(si->su, si->service->me)) |
| 170 |
return; |
| 171 |
|
| 172 |
si->c = mc->chan; |
| 173 |
|
| 174 |
/* fantasy commands are always verbose |
| 175 |
* (a little ugly but this way we can !set verbose) |
| 176 |
*/ |
| 177 |
mc->flags |= MC_FORCEVERBOSE; |
| 178 |
command_exec_split(si->service, si, realcmd, newargs, si->service->commands); |
| 179 |
mc->flags &= ~MC_FORCEVERBOSE; |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
static void chanserv_config_ready(void *unused) |
| 185 |
{ |
| 186 |
chansvs.nick = chansvs.me->nick; |
| 187 |
chansvs.user = chansvs.me->user; |
| 188 |
chansvs.host = chansvs.me->host; |
| 189 |
chansvs.real = chansvs.me->real; |
| 190 |
|
| 191 |
service_set_chanmsg(chansvs.me, true); |
| 192 |
|
| 193 |
if (me.connected) |
| 194 |
join_registered(false); /* !config_options.leave_chans */ |
| 195 |
} |
| 196 |
|
| 197 |
static int c_ci_vop(mowgli_config_file_entry_t *ce) |
| 198 |
{ |
| 199 |
if (ce->vardata == NULL) |
| 200 |
{ |
| 201 |
conf_report_warning(ce, "no parameter for configuration option"); |
| 202 |
return 0; |
| 203 |
} |
| 204 |
|
| 205 |
set_global_template_flags("VOP", flags_to_bitmask(ce->vardata, 0)); |
| 206 |
|
| 207 |
return 0; |
| 208 |
} |
| 209 |
|
| 210 |
static int c_ci_hop(mowgli_config_file_entry_t *ce) |
| 211 |
{ |
| 212 |
if (ce->vardata == NULL) |
| 213 |
{ |
| 214 |
conf_report_warning(ce, "no parameter for configuration option"); |
| 215 |
return 0; |
| 216 |
} |
| 217 |
|
| 218 |
set_global_template_flags("HOP", flags_to_bitmask(ce->vardata, 0)); |
| 219 |
|
| 220 |
return 0; |
| 221 |
} |
| 222 |
|
| 223 |
static int c_ci_aop(mowgli_config_file_entry_t *ce) |
| 224 |
{ |
| 225 |
if (ce->vardata == NULL) |
| 226 |
{ |
| 227 |
conf_report_warning(ce, "no parameter for configuration option"); |
| 228 |
return 0; |
| 229 |
} |
| 230 |
|
| 231 |
set_global_template_flags("AOP", flags_to_bitmask(ce->vardata, 0)); |
| 232 |
|
| 233 |
return 0; |
| 234 |
} |
| 235 |
|
| 236 |
static int c_ci_sop(mowgli_config_file_entry_t *ce) |
| 237 |
{ |
| 238 |
if (ce->vardata == NULL) |
| 239 |
{ |
| 240 |
conf_report_warning(ce, "no parameter for configuration option"); |
| 241 |
return 0; |
| 242 |
} |
| 243 |
|
| 244 |
set_global_template_flags("SOP", flags_to_bitmask(ce->vardata, 0)); |
| 245 |
|
| 246 |
return 0; |
| 247 |
} |
| 248 |
|
| 249 |
static int c_ci_templates(mowgli_config_file_entry_t *ce) |
| 250 |
{ |
| 251 |
mowgli_config_file_entry_t *flce; |
| 252 |
|
| 253 |
MOWGLI_ITER_FOREACH(flce, ce->entries) |
| 254 |
{ |
| 255 |
if (flce->vardata == NULL) |
| 256 |
{ |
| 257 |
conf_report_warning(ce, "no parameter for configuration option"); |
| 258 |
return 0; |
| 259 |
} |
| 260 |
|
| 261 |
set_global_template_flags(flce->varname, flags_to_bitmask(flce->vardata, 0)); |
| 262 |
} |
| 263 |
|
| 264 |
return 0; |
| 265 |
} |
| 266 |
|
| 267 |
void _modinit(module_t *m) |
| 268 |
{ |
| 269 |
hook_add_event("config_ready"); |
| 270 |
hook_add_config_ready(chanserv_config_ready); |
| 271 |
|
| 272 |
chansvs.me = service_add("chanserv", chanserv); |
| 273 |
|
| 274 |
hook_add_event("channel_join"); |
| 275 |
hook_add_event("channel_part"); |
| 276 |
hook_add_event("channel_register"); |
| 277 |
hook_add_event("channel_succession"); |
| 278 |
hook_add_event("channel_add"); |
| 279 |
hook_add_event("channel_topic"); |
| 280 |
hook_add_event("channel_can_change_topic"); |
| 281 |
hook_add_event("channel_tschange"); |
| 282 |
hook_add_event("channel_mode_change"); |
| 283 |
hook_add_event("user_identify"); |
| 284 |
hook_add_event("shutdown"); |
| 285 |
hook_add_channel_join(cs_join); |
| 286 |
hook_add_channel_part(cs_part); |
| 287 |
hook_add_channel_register(cs_register); |
| 288 |
hook_add_channel_succession(cs_succession); |
| 289 |
hook_add_channel_add(cs_newchan); |
| 290 |
hook_add_channel_topic(cs_keeptopic_topicset); |
| 291 |
hook_add_channel_can_change_topic(cs_topiccheck); |
| 292 |
hook_add_channel_tschange(cs_tschange); |
| 293 |
hook_add_channel_mode_change(cs_bounce_mode_change); |
| 294 |
hook_add_shutdown(on_shutdown); |
| 295 |
|
| 296 |
cs_leave_empty_timer = mowgli_timer_add(base_eventloop, "cs_leave_empty", cs_leave_empty, NULL, 300); |
| 297 |
|
| 298 |
/* chanserv{} block */ |
| 299 |
add_bool_conf_item("FANTASY", &chansvs.me->conf_table, 0, &chansvs.fantasy, false); |
| 300 |
add_conf_item("VOP", &chansvs.me->conf_table, c_ci_vop); |
| 301 |
add_conf_item("HOP", &chansvs.me->conf_table, c_ci_hop); |
| 302 |
add_conf_item("AOP", &chansvs.me->conf_table, c_ci_aop); |
| 303 |
add_conf_item("SOP", &chansvs.me->conf_table, c_ci_sop); |
| 304 |
add_conf_item("TEMPLATES", &chansvs.me->conf_table, c_ci_templates); |
| 305 |
add_bool_conf_item("CHANGETS", &chansvs.me->conf_table, 0, &chansvs.changets, false); |
| 306 |
add_bool_conf_item("HIDE_XOP", &chansvs.me->conf_table, 0, &chansvs.hide_xop, false); |
| 307 |
add_dupstr_conf_item("TRIGGER", &chansvs.me->conf_table, 0, &chansvs.trigger, "!"); |
| 308 |
add_duration_conf_item("EXPIRE", &chansvs.me->conf_table, 0, &chansvs.expiry, "d", 0); |
| 309 |
add_uint_conf_item("MAXCHANS", &chansvs.me->conf_table, 0, &chansvs.maxchans, 1, INT_MAX, 5); |
| 310 |
add_uint_conf_item("MAXCHANACS", &chansvs.me->conf_table, 0, &chansvs.maxchanacs, 0, INT_MAX, 0); |
| 311 |
add_uint_conf_item("MAXFOUNDERS", &chansvs.me->conf_table, 0, &chansvs.maxfounders, 1, (512 - 60) / (9 + 2), 4); /* fit on a line */ |
| 312 |
add_dupstr_conf_item("FOUNDER_FLAGS", &chansvs.me->conf_table, 0, &chansvs.founder_flags, NULL); |
| 313 |
add_dupstr_conf_item("DEFTEMPLATES", &chansvs.me->conf_table, 0, &chansvs.deftemplates, NULL); |
| 314 |
add_duration_conf_item("AKICK_TIME", &chansvs.me->conf_table, 0, &chansvs.akick_time, "m", 0); |
| 315 |
} |
| 316 |
|
| 317 |
void _moddeinit(module_unload_intent_t intent) |
| 318 |
{ |
| 319 |
if (chansvs.me) |
| 320 |
{ |
| 321 |
chansvs.nick = NULL; |
| 322 |
chansvs.user = NULL; |
| 323 |
chansvs.host = NULL; |
| 324 |
chansvs.real = NULL; |
| 325 |
service_delete(chansvs.me); |
| 326 |
chansvs.me = NULL; |
| 327 |
} |
| 328 |
|
| 329 |
hook_del_config_ready(chanserv_config_ready); |
| 330 |
hook_del_channel_join(cs_join); |
| 331 |
hook_del_channel_part(cs_part); |
| 332 |
hook_del_channel_register(cs_register); |
| 333 |
hook_del_channel_succession(cs_succession); |
| 334 |
hook_del_channel_add(cs_newchan); |
| 335 |
hook_del_channel_topic(cs_keeptopic_topicset); |
| 336 |
hook_del_channel_can_change_topic(cs_topiccheck); |
| 337 |
hook_del_channel_tschange(cs_tschange); |
| 338 |
hook_del_channel_mode_change(cs_bounce_mode_change); |
| 339 |
hook_del_shutdown(on_shutdown); |
| 340 |
|
| 341 |
mowgli_timer_destroy(base_eventloop, cs_leave_empty_timer); |
| 342 |
} |
| 343 |
|
| 344 |
static void cs_join(hook_channel_joinpart_t *hdata) |
| 345 |
{ |
| 346 |
chanuser_t *cu = hdata->cu; |
| 347 |
user_t *u; |
| 348 |
channel_t *chan; |
| 349 |
mychan_t *mc; |
| 350 |
unsigned int flags; |
| 351 |
bool noop; |
| 352 |
bool secure; |
| 353 |
metadata_t *md; |
| 354 |
chanacs_t *ca2; |
| 355 |
char akickreason[120] = "User is banned from this channel", *p; |
| 356 |
|
| 357 |
if (cu == NULL || is_internal_client(cu->user)) |
| 358 |
return; |
| 359 |
u = cu->user; |
| 360 |
chan = cu->chan; |
| 361 |
|
| 362 |
/* first check if this is a registered channel at all */ |
| 363 |
mc = mychan_find(chan->name); |
| 364 |
if (mc == NULL) |
| 365 |
return; |
| 366 |
|
| 367 |
flags = chanacs_user_flags(mc, u); |
| 368 |
noop = mc->flags & MC_NOOP || (u->myuser != NULL && |
| 369 |
u->myuser->flags & MU_NOOP); |
| 370 |
/* attempt to deop people recreating channels, if the more |
| 371 |
* sophisticated mechanism is disabled */ |
| 372 |
secure = mc->flags & MC_SECURE || (!chansvs.changets && |
| 373 |
chan->nummembers == 1 && chan->ts > CURRTIME - 300); |
| 374 |
|
| 375 |
if (chan->nummembers == 1 && mc->flags & MC_GUARD && |
| 376 |
metadata_find(mc, "private:botserv:bot-assigned") == NULL) |
| 377 |
join(chan->name, chansvs.nick); |
| 378 |
|
| 379 |
/* |
| 380 |
* CS SET RESTRICTED: if they don't have any access (excluding AKICK) |
| 381 |
* or special privs to join restricted chans, boot them. -- w00t |
| 382 |
*/ |
| 383 |
if ((mc->flags & MC_RESTRICTED) && !(flags & CA_ALLPRIVS) && !has_priv_user(u, PRIV_JOIN_STAFFONLY)) |
| 384 |
{ |
| 385 |
/* Stay on channel if this would empty it -- jilles */ |
| 386 |
if (chan->nummembers - chan->numsvcmembers == 1) |
| 387 |
{ |
| 388 |
mc->flags |= MC_INHABIT; |
| 389 |
if (chan->numsvcmembers == 0) |
| 390 |
join(chan->name, chansvs.nick); |
| 391 |
} |
| 392 |
if (mc->mlock_on & CMODE_INVITE || chan->modes & CMODE_INVITE) |
| 393 |
{ |
| 394 |
if (!(chan->modes & CMODE_INVITE)) |
| 395 |
check_modes(mc, true); |
| 396 |
remove_banlike(chansvs.me->me, chan, ircd->invex_mchar, u); |
| 397 |
modestack_flush_channel(chan); |
| 398 |
} |
| 399 |
else |
| 400 |
{ |
| 401 |
ban(chansvs.me->me, chan, u); |
| 402 |
remove_ban_exceptions(chansvs.me->me, chan, u); |
| 403 |
} |
| 404 |
try_kick(chansvs.me->me, chan, u, "You are not authorized to be on this channel"); |
| 405 |
hdata->cu = NULL; |
| 406 |
return; |
| 407 |
} |
| 408 |
|
| 409 |
if (flags & CA_AKICK && !(flags & CA_EXEMPT)) |
| 410 |
{ |
| 411 |
/* Stay on channel if this would empty it -- jilles */ |
| 412 |
if (chan->nummembers - chan->numsvcmembers == 1) |
| 413 |
{ |
| 414 |
mc->flags |= MC_INHABIT; |
| 415 |
if (chan->numsvcmembers == 0) |
| 416 |
join(chan->name, chansvs.nick); |
| 417 |
} |
| 418 |
/* use a user-given ban mask if possible -- jilles */ |
| 419 |
ca2 = chanacs_find_host_by_user(mc, u, CA_AKICK); |
| 420 |
if (ca2 != NULL) |
| 421 |
{ |
| 422 |
if (chanban_find(chan, ca2->host, 'b') == NULL) |
| 423 |
{ |
| 424 |
chanban_add(chan, ca2->host, 'b'); |
| 425 |
modestack_mode_param(chansvs.nick, chan, MTYPE_ADD, 'b', ca2->host); |
| 426 |
modestack_flush_channel(chan); |
| 427 |
} |
| 428 |
} |
| 429 |
else |
| 430 |
{ |
| 431 |
/* XXX this could be done more efficiently */ |
| 432 |
ca2 = chanacs_find(mc, entity(u->myuser), CA_AKICK); |
| 433 |
ban(chansvs.me->me, chan, u); |
| 434 |
} |
| 435 |
remove_ban_exceptions(chansvs.me->me, chan, u); |
| 436 |
if (ca2 != NULL) |
| 437 |
{ |
| 438 |
md = metadata_find(ca2, "reason"); |
| 439 |
if (md != NULL && *md->value != '|') |
| 440 |
{ |
| 441 |
snprintf(akickreason, sizeof akickreason, |
| 442 |
"Banned: %s", md->value); |
| 443 |
p = strchr(akickreason, '|'); |
| 444 |
if (p != NULL) |
| 445 |
*p = '\0'; |
| 446 |
else |
| 447 |
p = akickreason + strlen(akickreason); |
| 448 |
/* strip trailing spaces, so as not to |
| 449 |
* disclose the existence of an oper reason */ |
| 450 |
p--; |
| 451 |
while (p > akickreason && *p == ' ') |
| 452 |
p--; |
| 453 |
p[1] = '\0'; |
| 454 |
} |
| 455 |
} |
| 456 |
try_kick(chansvs.me->me, chan, u, akickreason); |
| 457 |
hdata->cu = NULL; |
| 458 |
return; |
| 459 |
} |
| 460 |
|
| 461 |
/* Kick out users who may be recreating channels mlocked +i. |
| 462 |
* Users with +i flag are allowed to join, as are users matching |
| 463 |
* an invite exception (the latter only works if the channel already |
| 464 |
* exists because members are sent before invite exceptions). |
| 465 |
* Because most networks do not use kick_on_split_riding or |
| 466 |
* no_create_on_split, do not trust users coming back from splits; |
| 467 |
* with the exception of users coming back after a services |
| 468 |
* restart if the channel TS did not change. |
| 469 |
* Unfortunately, this kicks users who have been invited by a channel |
| 470 |
* operator, after a split. |
| 471 |
*/ |
| 472 |
if (mc->mlock_on & CMODE_INVITE && !(flags & CA_INVITE) && |
| 473 |
(!me.bursting || mc->flags & MC_RECREATED) && |
| 474 |
(!(u->server->flags & SF_EOB) || (chan->nummembers - chan->numsvcmembers == 1)) && |
| 475 |
(!ircd->invex_mchar || !next_matching_ban(chan, u, ircd->invex_mchar, chan->bans.head))) |
| 476 |
{ |
| 477 |
if (chan->nummembers - chan->numsvcmembers == 1) |
| 478 |
{ |
| 479 |
mc->flags |= MC_INHABIT; |
| 480 |
if (chan->numsvcmembers == 0) |
| 481 |
join(chan->name, chansvs.nick); |
| 482 |
} |
| 483 |
if (!(chan->modes & CMODE_INVITE)) |
| 484 |
check_modes(mc, true); |
| 485 |
modestack_flush_channel(chan); |
| 486 |
try_kick(chansvs.me->me, chan, u, "Invite only channel"); |
| 487 |
hdata->cu = NULL; |
| 488 |
return; |
| 489 |
} |
| 490 |
|
| 491 |
/* A second user joined and was not kicked; we do not need |
| 492 |
* to stay on the channel artificially. |
| 493 |
* If there is only one user, stay in the channel to avoid |
| 494 |
* triggering autocycle-for-ops scripts and immediately |
| 495 |
* destroying channels with kick on split riding. |
| 496 |
*/ |
| 497 |
if (mc->flags & MC_INHABIT && chan->nummembers - chan->numsvcmembers >= 2) |
| 498 |
{ |
| 499 |
mc->flags &= ~MC_INHABIT; |
| 500 |
if (!(mc->flags & MC_GUARD) && !(chan->flags & CHAN_LOG) && chanuser_find(chan, chansvs.me->me)) |
| 501 |
part(chan->name, chansvs.nick); |
| 502 |
} |
| 503 |
|
| 504 |
if (ircd->uses_owner) |
| 505 |
{ |
| 506 |
if (flags & CA_USEOWNER) |
| 507 |
{ |
| 508 |
if (flags & CA_AUTOOP && !(noop || cu->modes & ircd->owner_mode)) |
| 509 |
{ |
| 510 |
modestack_mode_param(chansvs.nick, chan, MTYPE_ADD, ircd->owner_mchar[1], CLIENT_NAME(u)); |
| 511 |
cu->modes |= ircd->owner_mode; |
| 512 |
} |
| 513 |
} |
| 514 |
else if (secure && (cu->modes & ircd->owner_mode)) |
| 515 |
{ |
| 516 |
modestack_mode_param(chansvs.nick, chan, MTYPE_DEL, ircd->owner_mchar[1], CLIENT_NAME(u)); |
| 517 |
cu->modes &= ~ircd->owner_mode; |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
if (ircd->uses_protect) |
| 522 |
{ |
| 523 |
if (flags & CA_USEPROTECT) |
| 524 |
{ |
| 525 |
if (flags & CA_AUTOOP && !(noop || cu->modes & ircd->protect_mode || (ircd->uses_owner && cu->modes & ircd->owner_mode))) |
| 526 |
{ |
| 527 |
modestack_mode_param(chansvs.nick, chan, MTYPE_ADD, ircd->protect_mchar[1], CLIENT_NAME(u)); |
| 528 |
cu->modes |= ircd->protect_mode; |
| 529 |
} |
| 530 |
} |
| 531 |
else if (secure && (cu->modes & ircd->protect_mode)) |
| 532 |
{ |
| 533 |
modestack_mode_param(chansvs.nick, chan, MTYPE_DEL, ircd->protect_mchar[1], CLIENT_NAME(u)); |
| 534 |
cu->modes &= ~ircd->protect_mode; |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
if (flags & CA_AUTOOP) |
| 539 |
{ |
| 540 |
if (!(noop || cu->modes & CSTATUS_OP)) |
| 541 |
{ |
| 542 |
modestack_mode_param(chansvs.nick, chan, MTYPE_ADD, 'o', CLIENT_NAME(u)); |
| 543 |
cu->modes |= CSTATUS_OP; |
| 544 |
} |
| 545 |
} |
| 546 |
else if (secure && (cu->modes & CSTATUS_OP) && !(flags & CA_OP) && !is_service(cu->user)) |
| 547 |
{ |
| 548 |
modestack_mode_param(chansvs.nick, chan, MTYPE_DEL, 'o', CLIENT_NAME(u)); |
| 549 |
cu->modes &= ~CSTATUS_OP; |
| 550 |
} |
| 551 |
|
| 552 |
if (ircd->uses_halfops) |
| 553 |
{ |
| 554 |
if (flags & CA_AUTOHALFOP) |
| 555 |
{ |
| 556 |
if (!(noop || cu->modes & (CSTATUS_OP | ircd->halfops_mode))) |
| 557 |
{ |
| 558 |
modestack_mode_param(chansvs.nick, chan, MTYPE_ADD, 'h', CLIENT_NAME(u)); |
| 559 |
cu->modes |= ircd->halfops_mode; |
| 560 |
} |
| 561 |
} |
| 562 |
else if (secure && (cu->modes & ircd->halfops_mode) && !(flags & CA_HALFOP)) |
| 563 |
{ |
| 564 |
modestack_mode_param(chansvs.nick, chan, MTYPE_DEL, 'h', CLIENT_NAME(u)); |
| 565 |
cu->modes &= ~ircd->halfops_mode; |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
if (flags & CA_AUTOVOICE) |
| 570 |
{ |
| 571 |
if (!(noop || cu->modes & (CSTATUS_OP | ircd->halfops_mode | CSTATUS_VOICE))) |
| 572 |
{ |
| 573 |
modestack_mode_param(chansvs.nick, chan, MTYPE_ADD, 'v', CLIENT_NAME(u)); |
| 574 |
cu->modes |= CSTATUS_VOICE; |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
if (u->server->flags & SF_EOB && (md = metadata_find(mc, "private:entrymsg"))) |
| 579 |
{ |
| 580 |
if (metadata_find(mc, "private:botserv:bot-assigned") == NULL) |
| 581 |
{ |
| 582 |
if (!u->myuser || !(u->myuser->flags & MU_NOGREET)) |
| 583 |
notice(chansvs.nick, cu->user->nick, "[%s] %s", mc->name, md->value); |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
if (u->server->flags & SF_EOB && (md = metadata_find(mc, "url"))) |
| 588 |
numeric_sts(me.me, 328, cu->user, "%s :%s", mc->name, md->value); |
| 589 |
|
| 590 |
if (flags & CA_USEDUPDATE) |
| 591 |
mc->used = CURRTIME; |
| 592 |
} |
| 593 |
|
| 594 |
static void cs_part(hook_channel_joinpart_t *hdata) |
| 595 |
{ |
| 596 |
chanuser_t *cu; |
| 597 |
mychan_t *mc; |
| 598 |
|
| 599 |
cu = hdata->cu; |
| 600 |
if (cu == NULL) |
| 601 |
return; |
| 602 |
mc = mychan_find(cu->chan->name); |
| 603 |
if (mc == NULL) |
| 604 |
return; |
| 605 |
if (metadata_find(mc, "private:botserv:bot-assigned") != NULL) |
| 606 |
return; |
| 607 |
|
| 608 |
if (CURRTIME - mc->used >= 3600) |
| 609 |
if (chanacs_user_flags(mc, cu->user) & CA_USEDUPDATE) |
| 610 |
mc->used = CURRTIME; |
| 611 |
|
| 612 |
/* |
| 613 |
* When channel_part is fired, we haven't yet removed the |
| 614 |
* user from the room. So, the channel will have two members |
| 615 |
* if ChanServ is joining channels: the triggering user and |
| 616 |
* itself. |
| 617 |
* |
| 618 |
* This if block was utter nonsense. Refactor it into multiple |
| 619 |
* branches for better clarity and debugging ability. --nenolod |
| 620 |
*/ |
| 621 |
|
| 622 |
/* we're not parting if we've been told to never part. */ |
| 623 |
if (!config_options.leave_chans) |
| 624 |
return; |
| 625 |
|
| 626 |
/* we're not parting if the channel has more than one person on it */ |
| 627 |
if (cu->chan->nummembers - cu->chan->numsvcmembers > 1) |
| 628 |
return; |
| 629 |
|
| 630 |
/* internal clients parting a channel shouldn't cause chanserv to leave. */ |
| 631 |
if (is_internal_client(cu->user)) |
| 632 |
return; |
| 633 |
|
| 634 |
/* if we're enforcing an akick, we're MC_INHABIT. do not part. */ |
| 635 |
if (mc->flags & MC_INHABIT) |
| 636 |
{ |
| 637 |
slog(LG_DEBUG, "cs_part(): not leaving channel %s due to MC_INHABIT flag", mc->name); |
| 638 |
return; |
| 639 |
} |
| 640 |
|
| 641 |
/* seems we've met all conditions to be parted from a channel. */ |
| 642 |
part(cu->chan->name, chansvs.nick); |
| 643 |
} |
| 644 |
|
| 645 |
static user_t *get_changets_user(mychan_t *mc) |
| 646 |
{ |
| 647 |
metadata_t *md; |
| 648 |
|
| 649 |
return_val_if_fail(mc != NULL, chansvs.me->me); |
| 650 |
|
| 651 |
md = metadata_find(mc, "private:botserv:bot-assigned"); |
| 652 |
if (md != NULL) |
| 653 |
{ |
| 654 |
user_t *u = user_find(md->value); |
| 655 |
|
| 656 |
return_val_if_fail(is_internal_client(u), chansvs.me->me); |
| 657 |
|
| 658 |
return u; |
| 659 |
} |
| 660 |
|
| 661 |
return chansvs.me->me; |
| 662 |
} |
| 663 |
|
| 664 |
static void cs_register(hook_channel_req_t *hdata) |
| 665 |
{ |
| 666 |
mychan_t *mc; |
| 667 |
|
| 668 |
mc = hdata->mc; |
| 669 |
if (mc->chan) |
| 670 |
{ |
| 671 |
if (mc->flags & MC_GUARD) |
| 672 |
join(mc->name, chansvs.nick); |
| 673 |
if (metadata_find(mc, "private:botserv:bot-assigned") != NULL) |
| 674 |
return; |
| 675 |
|
| 676 |
mlock_sts(mc->chan); |
| 677 |
topiclock_sts(mc->chan); |
| 678 |
check_modes(mc, true); |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
static void cs_succession(hook_channel_succession_req_t *data) |
| 683 |
{ |
| 684 |
chanacs_change_simple(data->mc, entity(data->mu), NULL, custom_founder_check(), 0, NULL); |
| 685 |
} |
| 686 |
|
| 687 |
/* Called on every set of a topic, after updating our internal structures */ |
| 688 |
static void cs_keeptopic_topicset(channel_t *c) |
| 689 |
{ |
| 690 |
mychan_t *mc; |
| 691 |
metadata_t *md; |
| 692 |
|
| 693 |
mc = mychan_find(c->name); |
| 694 |
|
| 695 |
if (mc == NULL) |
| 696 |
return; |
| 697 |
|
| 698 |
md = metadata_find(mc, "private:topic:text"); |
| 699 |
if (md != NULL) |
| 700 |
{ |
| 701 |
if (c->topic != NULL && !strcmp(md->value, c->topic)) |
| 702 |
return; |
| 703 |
metadata_delete(mc, "private:topic:text"); |
| 704 |
} |
| 705 |
|
| 706 |
if (metadata_find(mc, "private:topic:setter")) |
| 707 |
metadata_delete(mc, "private:topic:setter"); |
| 708 |
|
| 709 |
if (metadata_find(mc, "private:topic:ts")) |
| 710 |
metadata_delete(mc, "private:topic:ts"); |
| 711 |
|
| 712 |
if (c->topic && c->topic_setter) |
| 713 |
{ |
| 714 |
slog(LG_DEBUG, "KeepTopic: topic set for %s by %s: %s", c->name, |
| 715 |
c->topic_setter, c->topic); |
| 716 |
metadata_add(mc, "private:topic:setter", |
| 717 |
c->topic_setter); |
| 718 |
metadata_add(mc, "private:topic:text", |
| 719 |
c->topic); |
| 720 |
metadata_add(mc, "private:topic:ts", |
| 721 |
number_to_string(c->topicts)); |
| 722 |
} |
| 723 |
else |
| 724 |
slog(LG_DEBUG, "KeepTopic: topic cleared for %s", c->name); |
| 725 |
} |
| 726 |
|
| 727 |
/* Called when a topic change is received from the network, before altering |
| 728 |
* our internal structures */ |
| 729 |
static void cs_topiccheck(hook_channel_topic_check_t *data) |
| 730 |
{ |
| 731 |
mychan_t *mc; |
| 732 |
unsigned int accessfl = 0; |
| 733 |
|
| 734 |
mc = mychan_find(data->c->name); |
| 735 |
if (mc == NULL) |
| 736 |
return; |
| 737 |
|
| 738 |
if ((mc->flags & (MC_KEEPTOPIC | MC_TOPICLOCK)) == (MC_KEEPTOPIC | MC_TOPICLOCK)) |
| 739 |
{ |
| 740 |
if (data->u == NULL || !((accessfl = chanacs_user_flags(mc, data->u)) & CA_TOPIC)) |
| 741 |
{ |
| 742 |
/* topic burst or unauthorized user, revert it */ |
| 743 |
data->approved = 1; |
| 744 |
slog(LG_DEBUG, "cs_topiccheck(): reverting topic change on channel %s by %s", |
| 745 |
data->c->name, |
| 746 |
data->u != NULL ? data->u->nick : "<server>"); |
| 747 |
|
| 748 |
if (data->u != NULL && !(mc->mlock_off & CMODE_TOPIC)) |
| 749 |
{ |
| 750 |
/* they do not have access to be opped either, |
| 751 |
* deop them and set +t */ |
| 752 |
/* note: channel_mode() takes nicks, not UIDs |
| 753 |
* when used with a non-NULL source */ |
| 754 |
if (ircd->uses_halfops && !(accessfl & (CA_OP | CA_AUTOOP | CA_HALFOP | CA_AUTOHALFOP))) |
| 755 |
channel_mode_va(chansvs.me->me, data->c, |
| 756 |
3, "+t-oh", data->u->nick, data->u->nick); |
| 757 |
else if (!ircd->uses_halfops && !(accessfl & (CA_OP | CA_AUTOOP))) |
| 758 |
channel_mode_va(chansvs.me->me, data->c, |
| 759 |
2, "+t-o", data->u->nick); |
| 760 |
} |
| 761 |
} |
| 762 |
} |
| 763 |
} |
| 764 |
|
| 765 |
/* Called on creation of a channel */ |
| 766 |
static void cs_newchan(channel_t *c) |
| 767 |
{ |
| 768 |
mychan_t *mc; |
| 769 |
chanuser_t *cu; |
| 770 |
metadata_t *md; |
| 771 |
char *setter; |
| 772 |
char *text; |
| 773 |
time_t channelts = 0; |
| 774 |
time_t topicts; |
| 775 |
char str[21]; |
| 776 |
|
| 777 |
if (!(mc = mychan_find(c->name))) |
| 778 |
return; |
| 779 |
|
| 780 |
/* set channel_t.mychan */ |
| 781 |
c->mychan = mc; |
| 782 |
|
| 783 |
/* schedule a mode lock check when we know the current modes |
| 784 |
* -- jilles */ |
| 785 |
mc->flags |= MC_MLOCK_CHECK; |
| 786 |
|
| 787 |
md = metadata_find(mc, "private:channelts"); |
| 788 |
if (md != NULL) |
| 789 |
channelts = atol(md->value); |
| 790 |
if (channelts == 0) |
| 791 |
channelts = mc->registered; |
| 792 |
|
| 793 |
if (c->ts > channelts && channelts > 0) |
| 794 |
mc->flags |= MC_RECREATED; |
| 795 |
else |
| 796 |
mc->flags &= ~MC_RECREATED; |
| 797 |
|
| 798 |
if (chansvs.changets && c->ts > channelts && channelts > 0) |
| 799 |
{ |
| 800 |
user_t *u; |
| 801 |
|
| 802 |
u = get_changets_user(mc); |
| 803 |
|
| 804 |
/* Stop the splitrider -- jilles */ |
| 805 |
c->ts = channelts; |
| 806 |
clear_simple_modes(c); |
| 807 |
c->modes |= CMODE_NOEXT | CMODE_TOPIC; |
| 808 |
check_modes(mc, false); |
| 809 |
/* No ops to clear */ |
| 810 |
chan_lowerts(c, u); |
| 811 |
cu = chanuser_add(c, CLIENT_NAME(u)); |
| 812 |
cu->modes |= CSTATUS_OP; |
| 813 |
/* make sure it parts again sometime (empty SJOIN etc) */ |
| 814 |
mc->flags |= MC_INHABIT; |
| 815 |
} |
| 816 |
else if (c->ts != channelts) |
| 817 |
{ |
| 818 |
snprintf(str, sizeof str, "%lu", (unsigned long)c->ts); |
| 819 |
metadata_add(mc, "private:channelts", str); |
| 820 |
} |
| 821 |
else if (!(MC_TOPICLOCK & mc->flags) && MOWGLI_LIST_LENGTH(&c->members) == 0) |
| 822 |
{ |
| 823 |
mlock_sts(c); |
| 824 |
/* Same channel, let's assume ircd has kept topic. |
| 825 |
* However, if topiclock is enabled, we must change it back |
| 826 |
* regardless. |
| 827 |
* Also, if there is someone in this channel already, it is |
| 828 |
* being created by a service and we must restore. |
| 829 |
* -- jilles */ |
| 830 |
return; |
| 831 |
} |
| 832 |
|
| 833 |
mlock_sts(c); |
| 834 |
topiclock_sts(c); |
| 835 |
|
| 836 |
if (!(MC_KEEPTOPIC & mc->flags)) |
| 837 |
return; |
| 838 |
|
| 839 |
md = metadata_find(mc, "private:topic:setter"); |
| 840 |
if (md == NULL) |
| 841 |
return; |
| 842 |
setter = md->value; |
| 843 |
|
| 844 |
md = metadata_find(mc, "private:topic:text"); |
| 845 |
if (md == NULL) |
| 846 |
return; |
| 847 |
text = md->value; |
| 848 |
|
| 849 |
md = metadata_find(mc, "private:topic:ts"); |
| 850 |
if (md == NULL) |
| 851 |
return; |
| 852 |
topicts = atol(md->value); |
| 853 |
|
| 854 |
handle_topic(c, setter, topicts, text); |
| 855 |
topic_sts(c, chansvs.me->me, setter, topicts, 0, text); |
| 856 |
} |
| 857 |
|
| 858 |
static void cs_tschange(channel_t *c) |
| 859 |
{ |
| 860 |
mychan_t *mc; |
| 861 |
char str[21]; |
| 862 |
|
| 863 |
if (!(mc = mychan_find(c->name))) |
| 864 |
return; |
| 865 |
|
| 866 |
/* store new TS */ |
| 867 |
snprintf(str, sizeof str, "%lu", (unsigned long)c->ts); |
| 868 |
metadata_add(mc, "private:channelts", str); |
| 869 |
|
| 870 |
/* schedule a mode lock check when we know the new modes |
| 871 |
* -- jilles */ |
| 872 |
mc->flags |= MC_MLOCK_CHECK; |
| 873 |
|
| 874 |
/* reset the mlock if needed */ |
| 875 |
mlock_sts(c); |
| 876 |
topiclock_sts(c); |
| 877 |
} |
| 878 |
|
| 879 |
static void on_shutdown(void *unused) |
| 880 |
{ |
| 881 |
if (chansvs.me != NULL && chansvs.me->me != NULL) |
| 882 |
quit_sts(chansvs.me->me, "shutting down"); |
| 883 |
} |
| 884 |
|
| 885 |
static void cs_leave_empty(void *unused) |
| 886 |
{ |
| 887 |
mychan_t *mc; |
| 888 |
mowgli_patricia_iteration_state_t state; |
| 889 |
|
| 890 |
(void)unused; |
| 891 |
MOWGLI_PATRICIA_FOREACH(mc, &state, mclist) |
| 892 |
{ |
| 893 |
if (!(mc->flags & MC_INHABIT)) |
| 894 |
continue; |
| 895 |
/* If there is only one user, stay indefinitely. */ |
| 896 |
if (mc->chan != NULL && mc->chan->nummembers - mc->chan->numsvcmembers == 1) |
| 897 |
continue; |
| 898 |
mc->flags &= ~MC_INHABIT; |
| 899 |
if (mc->chan != NULL && |
| 900 |
!(mc->chan->flags & CHAN_LOG) && |
| 901 |
(!(mc->flags & MC_GUARD) || |
| 902 |
(config_options.leave_chans && mc->chan->nummembers == mc->chan->numsvcmembers) || |
| 903 |
metadata_find(mc, "private:close:closer")) && |
| 904 |
chanuser_find(mc->chan, chansvs.me->me)) |
| 905 |
{ |
| 906 |
slog(LG_DEBUG, "cs_leave_empty(): leaving %s", mc->chan->name); |
| 907 |
part(mc->chan->name, chansvs.nick); |
| 908 |
} |
| 909 |
} |
| 910 |
} |
| 911 |
|
| 912 |
static void cs_bounce_mode_change(hook_channel_mode_change_t *data) |
| 913 |
{ |
| 914 |
mychan_t *mc; |
| 915 |
chanuser_t *cu; |
| 916 |
channel_t *chan; |
| 917 |
|
| 918 |
/* if we have SECURE mode enabled, then we want to bounce any changes */ |
| 919 |
cu = data->cu; |
| 920 |
chan = cu->chan; |
| 921 |
mc = chan->mychan; |
| 922 |
|
| 923 |
if (mc == NULL || (mc != NULL && !(mc->flags & MC_SECURE))) |
| 924 |
return; |
| 925 |
|
| 926 |
if (ircd->uses_owner && data->mchar == ircd->owner_mchar[1] && !(chanacs_user_flags(mc, cu->user) & (CA_USEOWNER))) |
| 927 |
{ |
| 928 |
modestack_mode_param(chansvs.nick, chan, MTYPE_DEL, ircd->owner_mchar[1], CLIENT_NAME(cu->user)); |
| 929 |
cu->modes &= ~data->mvalue; |
| 930 |
} |
| 931 |
else if (ircd->uses_protect && data->mchar == ircd->protect_mchar[1] && !(chanacs_user_flags(mc, cu->user) & (CA_USEPROTECT))) |
| 932 |
{ |
| 933 |
modestack_mode_param(chansvs.nick, chan, MTYPE_DEL, ircd->protect_mchar[1], CLIENT_NAME(cu->user)); |
| 934 |
cu->modes &= ~data->mvalue; |
| 935 |
} |
| 936 |
else if (data->mchar == 'o' && !(chanacs_user_flags(mc, cu->user) & (CA_OP | CA_AUTOOP)) && !is_service(cu->user)) |
| 937 |
{ |
| 938 |
modestack_mode_param(chansvs.nick, chan, MTYPE_DEL, 'o', CLIENT_NAME(cu->user)); |
| 939 |
cu->modes &= ~data->mvalue; |
| 940 |
} |
| 941 |
else if (ircd->uses_halfops && data->mchar == ircd->halfops_mchar[1] && !(chanacs_user_flags(mc, cu->user) & (CA_HALFOP | CA_AUTOHALFOP))) |
| 942 |
{ |
| 943 |
modestack_mode_param(chansvs.nick, chan, MTYPE_DEL, ircd->halfops_mchar[1], CLIENT_NAME(cu->user)); |
| 944 |
cu->modes &= ~data->mvalue; |
| 945 |
} |
| 946 |
} |
| 947 |
|
| 948 |
/* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs |
| 949 |
* vim:ts=8 |
| 950 |
* vim:sw=8 |
| 951 |
* vim:noexpandtab |
| 952 |
*/ |