| 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 "botserv.h" |
| 11 |
|
| 12 |
DECLARE_MODULE_V1 |
| 13 |
( |
| 14 |
"botserv/main", true, _modinit, _moddeinit, |
| 15 |
PACKAGE_STRING, |
| 16 |
"Rizon Development Group <http://www.atheme.org>" |
| 17 |
); |
| 18 |
|
| 19 |
static void bs_join(hook_channel_joinpart_t *hdata); |
| 20 |
static void bs_part(hook_channel_joinpart_t *hdata); |
| 21 |
|
| 22 |
static void bs_cmd_bot(sourceinfo_t *si, int parc, char *parv[]); |
| 23 |
static void bs_cmd_add(sourceinfo_t *si, int parc, char *parv[]); |
| 24 |
static void bs_cmd_change(sourceinfo_t *si, int parc, char *parv[]); |
| 25 |
static void bs_cmd_delete(sourceinfo_t *si, int parc, char *parv[]); |
| 26 |
static void bs_cmd_assign(sourceinfo_t *si, int parc, char *parv[]); |
| 27 |
static void bs_cmd_unassign(sourceinfo_t *si, int parc, char *parv[]); |
| 28 |
static void bs_cmd_botlist(sourceinfo_t *si, int parc, char *parv[]); |
| 29 |
static void on_shutdown(void *unused); |
| 30 |
static void osinfo_hook(sourceinfo_t *si); |
| 31 |
|
| 32 |
static void botserv_save_database(database_handle_t *db); |
| 33 |
static void db_h_bot(database_handle_t *db, const char *type); |
| 34 |
static void db_h_bot_count(database_handle_t *db, const char *type); |
| 35 |
|
| 36 |
/* visible for other modules; use the typedef to enforce type checking */ |
| 37 |
fn_botserv_bot_find botserv_bot_find; |
| 38 |
|
| 39 |
service_t *botsvs; |
| 40 |
|
| 41 |
unsigned int min_users = 0; |
| 42 |
|
| 43 |
E mowgli_list_t mychan; |
| 44 |
|
| 45 |
mowgli_list_t bs_bots; |
| 46 |
|
| 47 |
command_t bs_bot = { "BOT", "Maintains network bot list.", PRIV_USER_ADMIN, 6, bs_cmd_bot, { .path = "botserv/bot" } }; |
| 48 |
command_t bs_assign = { "ASSIGN", "Assigns a bot to a channel.", AC_NONE, 2, bs_cmd_assign, { .path = "botserv/assign" } }; |
| 49 |
command_t bs_unassign = { "UNASSIGN", "Unassigns a bot from a channel.", AC_NONE, 1, bs_cmd_unassign, { .path = "botserv/unassign" } }; |
| 50 |
command_t bs_botlist = { "BOTLIST", "Lists available bots.", AC_NONE, 0, bs_cmd_botlist, { .path = "botserv/botlist" } }; |
| 51 |
|
| 52 |
/* ******************************************************************** */ |
| 53 |
|
| 54 |
static botserv_bot_t *bs_mychan_find_bot(mychan_t *mc) |
| 55 |
{ |
| 56 |
metadata_t *md; |
| 57 |
botserv_bot_t *bot; |
| 58 |
|
| 59 |
md = metadata_find(mc, "private:botserv:bot-assigned"); |
| 60 |
bot = md != NULL ? botserv_bot_find(md->value) : NULL; |
| 61 |
if (bot != NULL && !user_find_named(bot->nick)) |
| 62 |
bot = NULL; |
| 63 |
if (bot == NULL && md != NULL) |
| 64 |
{ |
| 65 |
slog(LG_INFO, "bs_mychan_find_bot(): unassigning invalid bot %s from %s", |
| 66 |
md->value, mc->name); |
| 67 |
|
| 68 |
metadata_delete(mc, "private:botserv:bot-assigned"); |
| 69 |
metadata_delete(mc, "private:botserv:bot-handle-fantasy"); |
| 70 |
} |
| 71 |
return bot; |
| 72 |
} |
| 73 |
|
| 74 |
/* ******************************************************************** */ |
| 75 |
|
| 76 |
static void (*msg_real)(const char *from, const char *target, const char *fmt, ...); |
| 77 |
|
| 78 |
static void |
| 79 |
bs_msg(const char *from, const char *target, const char *fmt, ...) |
| 80 |
{ |
| 81 |
va_list ap; |
| 82 |
char buf[BUFSIZE]; |
| 83 |
const char *real_source = from; |
| 84 |
|
| 85 |
va_start(ap, fmt); |
| 86 |
if (vsnprintf(buf, sizeof buf, fmt, ap) < 0) |
| 87 |
{ |
| 88 |
va_end(ap); |
| 89 |
return; |
| 90 |
} |
| 91 |
va_end(ap); |
| 92 |
|
| 93 |
if (*target == '#' && !strcmp(from, chansvs.nick)) |
| 94 |
{ |
| 95 |
mychan_t *mc; |
| 96 |
botserv_bot_t *bot = NULL; |
| 97 |
|
| 98 |
mc = mychan_find(target); |
| 99 |
if (mc != NULL) |
| 100 |
{ |
| 101 |
bot = bs_mychan_find_bot(mc); |
| 102 |
|
| 103 |
real_source = bot != NULL ? bot->nick : from; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
msg_real(real_source, target, "%s", buf); |
| 108 |
} |
| 109 |
|
| 110 |
static void (*notice_real)(const char *from, const char *target, const char *fmt, ...); |
| 111 |
|
| 112 |
static void |
| 113 |
bs_notice(const char *from, const char *target, const char *fmt, ...) |
| 114 |
{ |
| 115 |
va_list ap; |
| 116 |
char buf[BUFSIZE]; |
| 117 |
const char *real_source = from; |
| 118 |
|
| 119 |
va_start(ap, fmt); |
| 120 |
if (vsnprintf(buf, sizeof buf, fmt, ap) < 0) |
| 121 |
{ |
| 122 |
va_end(ap); |
| 123 |
return; |
| 124 |
} |
| 125 |
va_end(ap); |
| 126 |
|
| 127 |
if (*target == '#' && !strcmp(from, chansvs.nick)) |
| 128 |
{ |
| 129 |
mychan_t *mc; |
| 130 |
botserv_bot_t *bot = NULL; |
| 131 |
|
| 132 |
mc = mychan_find(target); |
| 133 |
if (mc != NULL) |
| 134 |
{ |
| 135 |
bot = bs_mychan_find_bot(mc); |
| 136 |
|
| 137 |
real_source = bot != NULL ? bot->nick : from; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
notice_real(real_source, target, "%s", buf); |
| 142 |
} |
| 143 |
|
| 144 |
static void (*topic_sts_real)(channel_t *c, user_t *source, const char *setter, time_t ts, time_t prevts, const char *topic); |
| 145 |
|
| 146 |
static void |
| 147 |
bs_topic_sts(channel_t *c, user_t *source, const char *setter, time_t ts, time_t prevts, const char *topic) |
| 148 |
{ |
| 149 |
mychan_t *mc; |
| 150 |
botserv_bot_t *bot = NULL; |
| 151 |
|
| 152 |
return_if_fail(source != NULL); |
| 153 |
return_if_fail(c != NULL); |
| 154 |
return_if_fail(setter != NULL); |
| 155 |
return_if_fail(topic != NULL); |
| 156 |
|
| 157 |
if (source == chansvs.me->me && (mc = mychan_from(c)) != NULL) |
| 158 |
bot = bs_mychan_find_bot(mc); |
| 159 |
|
| 160 |
topic_sts_real(c, bot ? bot->me->me : source, setter, ts, prevts, topic); |
| 161 |
} |
| 162 |
|
| 163 |
static void |
| 164 |
bs_modestack_mode_simple(const char *source, channel_t *channel, int dir, int flags) |
| 165 |
{ |
| 166 |
mychan_t *mc; |
| 167 |
metadata_t *bs; |
| 168 |
user_t *bot = NULL; |
| 169 |
|
| 170 |
return_if_fail(source != NULL); |
| 171 |
return_if_fail(channel != NULL); |
| 172 |
|
| 173 |
if (source != NULL && chansvs.nick != NULL && |
| 174 |
!strcmp(source, chansvs.nick) && |
| 175 |
(mc = mychan_from(channel)) != NULL && |
| 176 |
(bs = metadata_find(mc, "private:botserv:bot-assigned")) != NULL) |
| 177 |
bot = user_find_named(bs->value); |
| 178 |
|
| 179 |
modestack_mode_simple_real(bot ? bot->nick : source, channel, dir, flags); |
| 180 |
} |
| 181 |
|
| 182 |
static void |
| 183 |
bs_modestack_mode_limit(const char *source, channel_t *channel, int dir, unsigned int limit) |
| 184 |
{ |
| 185 |
mychan_t *mc; |
| 186 |
metadata_t *bs; |
| 187 |
user_t *bot = NULL; |
| 188 |
|
| 189 |
return_if_fail(source != NULL); |
| 190 |
return_if_fail(channel != NULL); |
| 191 |
|
| 192 |
if (source != NULL && chansvs.nick != NULL && |
| 193 |
!strcmp(source, chansvs.nick) && |
| 194 |
(mc = mychan_from(channel)) != NULL && |
| 195 |
(bs = metadata_find(mc, "private:botserv:bot-assigned")) != NULL) |
| 196 |
bot = user_find_named(bs->value); |
| 197 |
|
| 198 |
modestack_mode_limit_real(bot ? bot->nick : source, channel, dir, limit); |
| 199 |
} |
| 200 |
|
| 201 |
static void |
| 202 |
bs_modestack_mode_ext(const char *source, channel_t *channel, int dir, unsigned int i, const char *value) |
| 203 |
{ |
| 204 |
mychan_t *mc; |
| 205 |
metadata_t *bs; |
| 206 |
user_t *bot = NULL; |
| 207 |
|
| 208 |
return_if_fail(source != NULL); |
| 209 |
return_if_fail(channel != NULL); |
| 210 |
|
| 211 |
if (source != NULL && chansvs.nick != NULL && |
| 212 |
!strcmp(source, chansvs.nick) && |
| 213 |
(mc = mychan_from(channel)) != NULL && |
| 214 |
(bs = metadata_find(mc, "private:botserv:bot-assigned")) != NULL) |
| 215 |
bot = user_find_named(bs->value); |
| 216 |
|
| 217 |
modestack_mode_ext_real(bot ? bot->nick : source, channel, dir, i, value); |
| 218 |
} |
| 219 |
|
| 220 |
static void |
| 221 |
bs_modestack_mode_param(const char *source, channel_t *channel, int dir, char type, const char *value) |
| 222 |
{ |
| 223 |
mychan_t *mc; |
| 224 |
metadata_t *bs; |
| 225 |
user_t *bot = NULL; |
| 226 |
|
| 227 |
return_if_fail(source != NULL); |
| 228 |
return_if_fail(channel != NULL); |
| 229 |
|
| 230 |
if (source != NULL && chansvs.nick != NULL && |
| 231 |
!strcmp(source, chansvs.nick) && |
| 232 |
(mc = mychan_from(channel)) != NULL && |
| 233 |
(bs = metadata_find(mc, "private:botserv:bot-assigned")) != NULL) |
| 234 |
bot = user_find_named(bs->value); |
| 235 |
|
| 236 |
modestack_mode_param_real(bot ? bot->nick : source, channel, dir, type, value); |
| 237 |
} |
| 238 |
|
| 239 |
static void |
| 240 |
bs_try_kick(user_t *source, channel_t *chan, user_t *target, const char *reason) |
| 241 |
{ |
| 242 |
mychan_t *mc; |
| 243 |
metadata_t *bs; |
| 244 |
user_t *bot = NULL; |
| 245 |
|
| 246 |
return_if_fail(source != NULL); |
| 247 |
return_if_fail(chan != NULL); |
| 248 |
|
| 249 |
if (source != chansvs.me->me) |
| 250 |
return try_kick_real(source, chan, target, reason); |
| 251 |
|
| 252 |
if ((mc = mychan_from(chan)) != NULL && (bs = metadata_find(mc, "private:botserv:bot-assigned")) != NULL) |
| 253 |
bot = user_find_named(bs->value); |
| 254 |
|
| 255 |
try_kick_real(bot ? bot : source, chan, target, reason); |
| 256 |
} |
| 257 |
|
| 258 |
/* ******************************************************************** */ |
| 259 |
|
| 260 |
static void |
| 261 |
bs_join_registered(bool all) |
| 262 |
{ |
| 263 |
mychan_t *mc; |
| 264 |
mowgli_patricia_iteration_state_t state; |
| 265 |
metadata_t *md; |
| 266 |
int cs = 0; |
| 267 |
|
| 268 |
if ((chansvs.me != NULL) && (chansvs.me->me != NULL)) |
| 269 |
cs = 1; |
| 270 |
|
| 271 |
MOWGLI_PATRICIA_FOREACH(mc, &state, mclist) |
| 272 |
{ |
| 273 |
if ((md = metadata_find(mc, "private:botserv:bot-assigned")) == NULL) |
| 274 |
continue; |
| 275 |
|
| 276 |
if (all) |
| 277 |
{ |
| 278 |
join(mc->name, md->value); |
| 279 |
if (mc->chan && cs && chanuser_find(mc->chan, chansvs.me->me)) |
| 280 |
part(mc->name, chansvs.nick); |
| 281 |
continue; |
| 282 |
} |
| 283 |
else if (mc->chan != NULL && mc->chan->members.count != 0) |
| 284 |
{ |
| 285 |
join(mc->name, md->value); |
| 286 |
if (mc->chan && cs && chanuser_find(mc->chan, chansvs.me->me)) |
| 287 |
part(mc->name, chansvs.nick); |
| 288 |
continue; |
| 289 |
} |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
/* ******************************************************************** */ |
| 294 |
|
| 295 |
static void bs_channel_drop(mychan_t *mc) |
| 296 |
{ |
| 297 |
botserv_bot_t *bot; |
| 298 |
|
| 299 |
if ((bot = bs_mychan_find_bot(mc)) == NULL) |
| 300 |
return; |
| 301 |
|
| 302 |
metadata_delete(mc, "private:botserv:bot-assigned"); |
| 303 |
metadata_delete(mc, "private:botserv:bot-handle-fantasy"); |
| 304 |
part(mc->name, bot->nick); |
| 305 |
} |
| 306 |
|
| 307 |
/* ******************************************************************** */ |
| 308 |
|
| 309 |
/* botserv: bot handler: channel commands only. */ |
| 310 |
static void |
| 311 |
botserv_channel_handler(sourceinfo_t *si, int parc, char *parv[]) |
| 312 |
{ |
| 313 |
metadata_t *md; |
| 314 |
mychan_t *mc = NULL; |
| 315 |
char orig[BUFSIZE]; |
| 316 |
char newargs[BUFSIZE]; |
| 317 |
char *cmd; |
| 318 |
char *args; |
| 319 |
service_t *sptr = NULL; |
| 320 |
|
| 321 |
/* this should never happen */ |
| 322 |
if (parv[parc - 2][0] == '&') |
| 323 |
{ |
| 324 |
slog(LG_ERROR, "services(): got parv with local channel: %s", parv[0]); |
| 325 |
return; |
| 326 |
} |
| 327 |
|
| 328 |
/* is this a fantasy command? respect global fantasy settings. */ |
| 329 |
if (chansvs.fantasy == false) |
| 330 |
{ |
| 331 |
/* *all* fantasy disabled */ |
| 332 |
return; |
| 333 |
} |
| 334 |
|
| 335 |
mc = mychan_find(parv[parc - 2]); |
| 336 |
if (!mc) |
| 337 |
{ |
| 338 |
/* unregistered, NFI how we got this message, but let's leave it alone! */ |
| 339 |
slog(LG_DEBUG, "botserv_channel_handler(): received message for %s (unregistered channel?)", parv[parc - 2]); |
| 340 |
return; |
| 341 |
} |
| 342 |
|
| 343 |
md = metadata_find(mc, "disable_fantasy"); |
| 344 |
if (md) |
| 345 |
{ |
| 346 |
/* fantasy disabled on this channel. don't message them, just bail. */ |
| 347 |
return; |
| 348 |
} |
| 349 |
|
| 350 |
md = metadata_find(mc, "private:botserv:bot-assigned"); |
| 351 |
if (md == NULL) |
| 352 |
{ |
| 353 |
/* we received this, but have no record of a bot assigned. WTF */ |
| 354 |
slog(LG_DEBUG, "botserv_channel_handler(): received a message for a bot, but %s has no bots assigned.", mc->name); |
| 355 |
return; |
| 356 |
} |
| 357 |
|
| 358 |
md = metadata_find(mc, "private:botserv:bot-handle-fantasy"); |
| 359 |
if (md == NULL || irccasecmp(si->service->me->nick, md->value)) |
| 360 |
return; |
| 361 |
|
| 362 |
/* make a copy of the original for debugging */ |
| 363 |
mowgli_strlcpy(orig, parv[parc - 1], BUFSIZE); |
| 364 |
|
| 365 |
/* lets go through this to get the command */ |
| 366 |
cmd = strtok(parv[parc - 1], " "); |
| 367 |
|
| 368 |
if (!cmd) |
| 369 |
return; |
| 370 |
if (*orig == '\001') |
| 371 |
{ |
| 372 |
handle_ctcp_common(si, cmd, strtok(NULL, "")); |
| 373 |
return; |
| 374 |
} |
| 375 |
|
| 376 |
/* take the command through the hash table, handling both !prefix and Bot, ... styles */ |
| 377 |
metadata_t *mdp = metadata_find(mc, "private:prefix"); |
| 378 |
const char *prefix = (mdp ? mdp->value : chansvs.trigger); |
| 379 |
|
| 380 |
if ((sptr = service_find("chanserv")) == NULL) |
| 381 |
return; |
| 382 |
|
| 383 |
if (strlen(cmd) >= 2 && strchr(prefix, cmd[0]) && isalpha((unsigned char)*++cmd)) |
| 384 |
{ |
| 385 |
const char *realcmd = service_resolve_alias(chansvs.me, NULL, cmd); |
| 386 |
|
| 387 |
/* XXX not really nice to look up the command twice |
| 388 |
* -- jilles */ |
| 389 |
if (command_find(sptr->commands, realcmd) == NULL) |
| 390 |
return; |
| 391 |
if (floodcheck(si->su, si->service->me)) |
| 392 |
return; |
| 393 |
/* construct <channel> <args> */ |
| 394 |
mowgli_strlcpy(newargs, parv[parc - 2], sizeof newargs); |
| 395 |
args = strtok(NULL, ""); |
| 396 |
if (args != NULL) |
| 397 |
{ |
| 398 |
mowgli_strlcat(newargs, " ", sizeof newargs); |
| 399 |
mowgli_strlcat(newargs, args, sizeof newargs); |
| 400 |
} |
| 401 |
/* let the command know it's called as fantasy cmd */ |
| 402 |
si->c = mc->chan; |
| 403 |
/* fantasy commands are always verbose |
| 404 |
* (a little ugly but this way we can !set verbose) |
| 405 |
*/ |
| 406 |
mc->flags |= MC_FORCEVERBOSE; |
| 407 |
command_exec_split(si->service, si, realcmd, newargs, sptr->commands); |
| 408 |
mc->flags &= ~MC_FORCEVERBOSE; |
| 409 |
} |
| 410 |
else if (!strncasecmp(cmd, si->service->me->nick, strlen(si->service->me->nick)) && (cmd = strtok(NULL, "")) != NULL) |
| 411 |
{ |
| 412 |
const char *realcmd; |
| 413 |
char *pptr; |
| 414 |
|
| 415 |
mowgli_strlcpy(newargs, parv[parc - 2], sizeof newargs); |
| 416 |
if ((pptr = strchr(cmd, ' ')) != NULL) |
| 417 |
{ |
| 418 |
*pptr = '\0'; |
| 419 |
|
| 420 |
mowgli_strlcat(newargs, " ", sizeof newargs); |
| 421 |
mowgli_strlcat(newargs, ++pptr, sizeof newargs); |
| 422 |
} |
| 423 |
|
| 424 |
realcmd = service_resolve_alias(chansvs.me, NULL, cmd); |
| 425 |
|
| 426 |
if (command_find(sptr->commands, realcmd) == NULL) |
| 427 |
return; |
| 428 |
if (floodcheck(si->su, si->service->me)) |
| 429 |
return; |
| 430 |
|
| 431 |
si->c = mc->chan; |
| 432 |
|
| 433 |
/* fantasy commands are always verbose |
| 434 |
* (a little ugly but this way we can !set verbose) |
| 435 |
*/ |
| 436 |
mc->flags |= MC_FORCEVERBOSE; |
| 437 |
command_exec_split(si->service, si, realcmd, newargs, sptr->commands); |
| 438 |
mc->flags &= ~MC_FORCEVERBOSE; |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
/* ******************************************************************** */ |
| 443 |
|
| 444 |
static void botserv_config_ready(void *unused) |
| 445 |
{ |
| 446 |
if (me.connected) |
| 447 |
bs_join_registered(!config_options.leave_chans); |
| 448 |
|
| 449 |
hook_del_config_ready(botserv_config_ready); |
| 450 |
} |
| 451 |
|
| 452 |
/* ******************************************************************** */ |
| 453 |
|
| 454 |
void botserv_save_database(database_handle_t *db) |
| 455 |
{ |
| 456 |
mowgli_node_t *n; |
| 457 |
|
| 458 |
/* iterate through and write all the metadata */ |
| 459 |
MOWGLI_ITER_FOREACH(n, bs_bots.head) |
| 460 |
{ |
| 461 |
botserv_bot_t *bot = (botserv_bot_t *) n->data; |
| 462 |
|
| 463 |
db_start_row(db, "BOT"); |
| 464 |
db_write_word(db, bot->nick); |
| 465 |
db_write_word(db, bot->user); |
| 466 |
db_write_word(db, bot->host); |
| 467 |
db_write_uint(db, bot->private); |
| 468 |
db_write_time(db, bot->registered); |
| 469 |
db_write_str(db, bot->real); |
| 470 |
db_commit_row(db); |
| 471 |
} |
| 472 |
|
| 473 |
db_start_row(db, "BOT-COUNT"); |
| 474 |
db_write_uint(db, bs_bots.count); |
| 475 |
db_commit_row(db); |
| 476 |
} |
| 477 |
|
| 478 |
static void db_h_bot(database_handle_t *db, const char *type) |
| 479 |
{ |
| 480 |
const char *nick = db_sread_word(db); |
| 481 |
const char *user = db_sread_word(db); |
| 482 |
const char *host = db_sread_word(db); |
| 483 |
int private = db_sread_int(db); |
| 484 |
time_t registered = db_sread_time(db); |
| 485 |
const char *real = db_sread_str(db); |
| 486 |
botserv_bot_t *bot; |
| 487 |
|
| 488 |
bot = scalloc(sizeof(botserv_bot_t), 1); |
| 489 |
bot->nick = sstrdup(nick); |
| 490 |
|
| 491 |
if (!is_valid_username(user)) |
| 492 |
user = "botserv"; |
| 493 |
|
| 494 |
bot->user = sstrndup(user, USERLEN - 1); |
| 495 |
bot->host = sstrdup(host); |
| 496 |
bot->real = sstrdup(real); |
| 497 |
bot->private = private; |
| 498 |
bot->registered = registered; |
| 499 |
bot->me = service_add_static(bot->nick, bot->user, bot->host, bot->real, botserv_channel_handler, chansvs.me); |
| 500 |
service_set_chanmsg(bot->me, true); |
| 501 |
mowgli_node_add(bot, &bot->bnode, &bs_bots); |
| 502 |
} |
| 503 |
|
| 504 |
static void db_h_bot_count(database_handle_t *db, const char *type) |
| 505 |
{ |
| 506 |
unsigned int i = db_sread_uint(db); |
| 507 |
|
| 508 |
if (i != MOWGLI_LIST_LENGTH(&bs_bots)) |
| 509 |
slog(LG_ERROR, "botserv_load_database(): inconsistency: database defines %d objects, I only deserialized %zu.", i, bs_bots.count); |
| 510 |
} |
| 511 |
|
| 512 |
/* ******************************************************************** */ |
| 513 |
|
| 514 |
botserv_bot_t* botserv_bot_find(char *name) |
| 515 |
{ |
| 516 |
mowgli_node_t *n; |
| 517 |
|
| 518 |
if (name == NULL) |
| 519 |
return NULL; |
| 520 |
|
| 521 |
MOWGLI_ITER_FOREACH(n, bs_bots.head) |
| 522 |
{ |
| 523 |
botserv_bot_t *bot = (botserv_bot_t *) n->data; |
| 524 |
|
| 525 |
if (!irccasecmp(name, bot->nick)) |
| 526 |
return bot; |
| 527 |
} |
| 528 |
|
| 529 |
return NULL; |
| 530 |
} |
| 531 |
|
| 532 |
/* ******************************************************************** */ |
| 533 |
|
| 534 |
/* BOT CMD nick user host real */ |
| 535 |
static void bs_cmd_bot(sourceinfo_t *si, int parc, char *parv[]) |
| 536 |
{ |
| 537 |
if (parc < 1 || parv[0] == NULL) |
| 538 |
{ |
| 539 |
command_fail(si, fault_needmoreparams, STR_INVALID_PARAMS, "BOT"); |
| 540 |
command_fail(si, fault_needmoreparams, _("Syntax: BOT ADD <nick> <user> <host> <real>")); |
| 541 |
command_fail(si, fault_needmoreparams, _("Syntax: BOT CHANGE <oldnick> <newnick> [<user> [<host> [<real>]]]")); |
| 542 |
command_fail(si, fault_needmoreparams, _("Syntax: BOT DEL <nick>")); |
| 543 |
return; |
| 544 |
} |
| 545 |
|
| 546 |
if (!irccasecmp(parv[0], "ADD")) |
| 547 |
{ |
| 548 |
bs_cmd_add(si, parc - 1, parv + 1); |
| 549 |
} |
| 550 |
else if(!irccasecmp(parv[0], "CHANGE")) |
| 551 |
{ |
| 552 |
bs_cmd_change(si, parc - 1, parv + 1); |
| 553 |
} |
| 554 |
else if(!irccasecmp(parv[0], "DEL")) |
| 555 |
{ |
| 556 |
bs_cmd_delete(si, parc - 1, parv + 1); |
| 557 |
} |
| 558 |
else |
| 559 |
{ |
| 560 |
command_fail(si, fault_badparams, STR_INVALID_PARAMS, "BOT"); |
| 561 |
command_fail(si, fault_badparams, _("Syntax: BOT ADD <nick> <user> <host> <real>")); |
| 562 |
command_fail(si, fault_badparams, _("Syntax: BOT CHANGE <oldnick> <newnick> [<user> [<host> [<real>]]]")); |
| 563 |
command_fail(si, fault_badparams, _("Syntax: BOT DEL <nick>")); |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
/* ******************************************************************** */ |
| 568 |
|
| 569 |
static bool valid_misc_field(const char *field, size_t maxlen) |
| 570 |
{ |
| 571 |
if (strlen(field) > maxlen) |
| 572 |
return false; |
| 573 |
|
| 574 |
/* Never ever allow @!?* as they have special meaning in all ircds */ |
| 575 |
/* Empty, space anywhere and colon at the start break the protocol */ |
| 576 |
/* Also disallow ASCII 1-31 and "' as no sane IRCd allows them in n, u or h */ |
| 577 |
if (strchr(field, '@') || strchr(field, '!') || strchr(field, '?') || strchr(field, '/') || |
| 578 |
strchr(field, '*') || strchr(field, '\'') || strchr(field, ' ') || |
| 579 |
strchr(field, '"') || *field == ':' || *field == '\0' || |
| 580 |
has_ctrl_chars(field)) |
| 581 |
return false; |
| 582 |
|
| 583 |
return true; |
| 584 |
} |
| 585 |
|
| 586 |
/* CHANGE oldnick nick [user [host [real]]] */ |
| 587 |
static void bs_cmd_change(sourceinfo_t *si, int parc, char *parv[]) |
| 588 |
{ |
| 589 |
botserv_bot_t *bot; |
| 590 |
mowgli_patricia_iteration_state_t state; |
| 591 |
mychan_t *mc; |
| 592 |
metadata_t *md; |
| 593 |
|
| 594 |
if (parc < 2 || parv[0] == NULL || parv[1] == NULL) |
| 595 |
{ |
| 596 |
command_fail(si, fault_needmoreparams, STR_INVALID_PARAMS, "BOT CHANGE"); |
| 597 |
command_fail(si, fault_needmoreparams, _("Syntax: BOT CHANGE <oldnick> <newnick> [<user> [<host> [<real>]]]")); |
| 598 |
return; |
| 599 |
} |
| 600 |
|
| 601 |
bot = botserv_bot_find(parv[0]); |
| 602 |
if (bot == NULL) |
| 603 |
{ |
| 604 |
command_fail(si, fault_nosuch_target, _("\2%s\2 is not a bot"), parv[0]); |
| 605 |
return; |
| 606 |
} |
| 607 |
|
| 608 |
if (nicksvs.no_nick_ownership ? myuser_find(parv[1]) != NULL : mynick_find(parv[1]) != NULL) |
| 609 |
{ |
| 610 |
command_fail(si, fault_alreadyexists, _("\2%s\2 is a registered nick."), parv[1]); |
| 611 |
return; |
| 612 |
} |
| 613 |
|
| 614 |
if (irccasecmp(parv[0], parv[1])) |
| 615 |
{ |
| 616 |
if (botserv_bot_find(parv[1]) || service_find_nick(parv[1])) |
| 617 |
{ |
| 618 |
command_fail(si, fault_alreadyexists, |
| 619 |
_("\2%s\2 is already a bot or service."), |
| 620 |
parv[1]); |
| 621 |
return; |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
if (parc >= 2 && (!is_valid_nick(parv[1]))) |
| 626 |
{ |
| 627 |
command_fail(si, fault_badparams, _("\2%s\2 is an invalid nickname."), parv[1]); |
| 628 |
return; |
| 629 |
} |
| 630 |
|
| 631 |
if (parc >= 4 && !check_vhost_validity(si, parv[3])) |
| 632 |
return; |
| 633 |
|
| 634 |
service_delete(bot->me); |
| 635 |
switch(parc) |
| 636 |
{ |
| 637 |
case 5: |
| 638 |
if (strlen(parv[4]) < GECOSLEN) |
| 639 |
{ |
| 640 |
free(bot->real); |
| 641 |
bot->real = sstrdup(parv[4]); |
| 642 |
} |
| 643 |
else |
| 644 |
command_fail(si, fault_badparams, _("\2%s\2 is an invalid realname, not changing it"), parv[4]); |
| 645 |
case 4: |
| 646 |
free(bot->host); |
| 647 |
bot->host = sstrdup(parv[3]); |
| 648 |
case 3: |
| 649 |
/* XXX: we really need an is_valid_user(), but this is close enough. --nenolod */ |
| 650 |
if (is_valid_username(parv[2])) { |
| 651 |
free(bot->user); |
| 652 |
bot->user = sstrndup(parv[2], USERLEN - 1); |
| 653 |
} else |
| 654 |
command_fail(si, fault_badparams, _("\2%s\2 is an invalid username, not changing it."), parv[2]); |
| 655 |
case 2: |
| 656 |
free(bot->nick); |
| 657 |
bot->nick = sstrdup(parv[1]); |
| 658 |
break; |
| 659 |
default: |
| 660 |
command_fail(si, fault_needmoreparams, STR_INVALID_PARAMS, "BOT CHANGE"); |
| 661 |
command_fail(si, fault_needmoreparams, _("Syntax: BOT CHANGE <oldnick> <newnick> [<user> [<host> [<real>]]]")); |
| 662 |
return; |
| 663 |
} |
| 664 |
bot->registered = CURRTIME; |
| 665 |
bot->me = service_add_static(bot->nick, bot->user, bot->host, bot->real, botserv_channel_handler, chansvs.me); |
| 666 |
service_set_chanmsg(bot->me, true); |
| 667 |
|
| 668 |
/* join it back and also update the metadata */ |
| 669 |
MOWGLI_PATRICIA_FOREACH(mc, &state, mclist) |
| 670 |
{ |
| 671 |
if ((md = metadata_find(mc, "private:botserv:bot-assigned")) == NULL) |
| 672 |
continue; |
| 673 |
|
| 674 |
if (!irccasecmp(md->value, parv[0])) |
| 675 |
{ |
| 676 |
metadata_add(mc, "private:botserv:bot-assigned", parv[1]); |
| 677 |
metadata_add(mc, "private:botserv:bot-handle-fantasy", parv[1]); |
| 678 |
if (!config_options.leave_chans || (mc->chan != NULL && MOWGLI_LIST_LENGTH(&mc->chan->members) > 0)) |
| 679 |
join(mc->name, parv[1]); |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
logcommand(si, CMDLOG_ADMIN, "BOT:CHANGE: \2%s\2 (\2%s\2@\2%s\2) [\2%s\2]", bot->nick, bot->user, bot->host, bot->real); |
| 684 |
command_success_nodata(si, "\2%s\2 (\2%s\2@\2%s\2) [\2%s\2] changed.", bot->nick, bot->user, bot->host, bot->real); |
| 685 |
} |
| 686 |
|
| 687 |
/* ******************************************************************** */ |
| 688 |
|
| 689 |
/* ADD nick user host real */ |
| 690 |
static void bs_cmd_add(sourceinfo_t *si, int parc, char *parv[]) |
| 691 |
{ |
| 692 |
botserv_bot_t *bot; |
| 693 |
char buf[BUFSIZE]; |
| 694 |
|
| 695 |
if (parc < 4) |
| 696 |
{ |
| 697 |
command_fail(si, fault_needmoreparams, STR_INVALID_PARAMS, "BOT ADD"); |
| 698 |
command_fail(si, fault_needmoreparams, _("Syntax: BOT ADD <nick> <user> <host> <real>")); |
| 699 |
return; |
| 700 |
} |
| 701 |
|
| 702 |
if (botserv_bot_find(parv[0]) || service_find(parv[0]) || service_find_nick(parv[0])) |
| 703 |
{ |
| 704 |
command_fail(si, fault_alreadyexists, |
| 705 |
_("\2%s\2 is already a bot or service."), |
| 706 |
parv[0]); |
| 707 |
return; |
| 708 |
} |
| 709 |
|
| 710 |
if (nicksvs.no_nick_ownership ? myuser_find(parv[0]) != NULL : mynick_find(parv[0]) != NULL) |
| 711 |
{ |
| 712 |
command_fail(si, fault_alreadyexists, _("\2%s\2 is a registered nick."), parv[0]); |
| 713 |
return; |
| 714 |
} |
| 715 |
|
| 716 |
if (parc > 4 && parv[4] != NULL) |
| 717 |
snprintf(buf, sizeof(buf), "%s %s", parv[3], parv[4]); |
| 718 |
else |
| 719 |
snprintf(buf, sizeof(buf), "%s", parv[3]); |
| 720 |
|
| 721 |
if (!is_valid_nick(parv[0])) |
| 722 |
{ |
| 723 |
command_fail(si, fault_badparams, _("\2%s\2 is an invalid nickname."), parv[0]); |
| 724 |
return; |
| 725 |
} |
| 726 |
|
| 727 |
if (!is_valid_username(parv[1])) |
| 728 |
{ |
| 729 |
command_fail(si, fault_badparams, _("\2%s\2 is an invalid username."), parv[1]); |
| 730 |
return; |
| 731 |
} |
| 732 |
|
| 733 |
if (!check_vhost_validity(si, parv[2])) |
| 734 |
return; |
| 735 |
|
| 736 |
if (strlen(parv[3]) >= GECOSLEN) |
| 737 |
{ |
| 738 |
command_fail(si, fault_badparams, _("\2%s\2 is an invalid realname."), parv[3]); |
| 739 |
return; |
| 740 |
} |
| 741 |
|
| 742 |
bot = scalloc(sizeof(botserv_bot_t), 1); |
| 743 |
bot->nick = sstrdup(parv[0]); |
| 744 |
bot->user = sstrndup(parv[1], USERLEN - 1); |
| 745 |
bot->host = sstrdup(parv[2]); |
| 746 |
bot->real = sstrdup(buf); |
| 747 |
bot->private = false; |
| 748 |
bot->registered = CURRTIME; |
| 749 |
bot->me = service_add_static(bot->nick, bot->user, bot->host, bot->real, botserv_channel_handler, chansvs.me); |
| 750 |
service_set_chanmsg(bot->me, true); |
| 751 |
mowgli_node_add(bot, &bot->bnode, &bs_bots); |
| 752 |
|
| 753 |
logcommand(si, CMDLOG_ADMIN, "BOT:ADD: \2%s\2 (\2%s\2@\2%s\2) [\2%s\2]", bot->nick, bot->user, bot->host, bot->real); |
| 754 |
command_success_nodata(si, "\2%s\2 (\2%s\2@\2%s\2) [\2%s\2] created.", bot->nick, bot->user, bot->host, bot->real); |
| 755 |
} |
| 756 |
|
| 757 |
/* ******************************************************************** */ |
| 758 |
|
| 759 |
/* DELETE nick */ |
| 760 |
static void bs_cmd_delete(sourceinfo_t *si, int parc, char *parv[]) |
| 761 |
{ |
| 762 |
botserv_bot_t *bot = botserv_bot_find(parv[0]); |
| 763 |
mowgli_patricia_iteration_state_t state; |
| 764 |
mychan_t *mc; |
| 765 |
metadata_t *md; |
| 766 |
|
| 767 |
if (parc < 1) |
| 768 |
{ |
| 769 |
command_fail(si, fault_needmoreparams, STR_INVALID_PARAMS, "BOT DEL"); |
| 770 |
command_fail(si, fault_needmoreparams, _("Syntax: BOT DEL <nick>")); |
| 771 |
return; |
| 772 |
} |
| 773 |
|
| 774 |
if (bot == NULL) |
| 775 |
{ |
| 776 |
command_fail(si, fault_nosuch_target, _("\2%s\2 is not a bot"), parv[0]); |
| 777 |
return; |
| 778 |
} |
| 779 |
|
| 780 |
MOWGLI_PATRICIA_FOREACH(mc, &state, mclist) |
| 781 |
{ |
| 782 |
if ((md = metadata_find(mc, "private:botserv:bot-assigned")) == NULL) |
| 783 |
continue; |
| 784 |
|
| 785 |
if (!irccasecmp(md->value, bot->nick)) |
| 786 |
{ |
| 787 |
if (mc->flags & MC_GUARD && |
| 788 |
(!config_options.leave_chans || |
| 789 |
(mc->chan != NULL && |
| 790 |
MOWGLI_LIST_LENGTH(&mc->chan->members) > 1))) |
| 791 |
join(mc->name, chansvs.nick); |
| 792 |
|
| 793 |
metadata_delete(mc, "private:botserv:bot-assigned"); |
| 794 |
metadata_delete(mc, "private:botserv:bot-handle-fantasy"); |
| 795 |
} |
| 796 |
} |
| 797 |
|
| 798 |
mowgli_node_delete(&bot->bnode, &bs_bots); |
| 799 |
service_delete(bot->me); |
| 800 |
free(bot->nick); |
| 801 |
free(bot->user); |
| 802 |
free(bot->real); |
| 803 |
free(bot->host); |
| 804 |
free(bot); |
| 805 |
|
| 806 |
logcommand(si, CMDLOG_ADMIN, "BOT:DEL: \2%s\2", parv[0]); |
| 807 |
command_success_nodata(si, "Bot \2%s\2 has been deleted.", parv[0]); |
| 808 |
} |
| 809 |
|
| 810 |
/* ******************************************************************** */ |
| 811 |
|
| 812 |
/* LIST */ |
| 813 |
static void bs_cmd_botlist(sourceinfo_t *si, int parc, char *parv[]) |
| 814 |
{ |
| 815 |
int i = 0; |
| 816 |
mowgli_node_t *n; |
| 817 |
|
| 818 |
command_success_nodata(si, _("Listing of bots available on \2%s\2:"), me.netname); |
| 819 |
|
| 820 |
MOWGLI_ITER_FOREACH(n, bs_bots.head) |
| 821 |
{ |
| 822 |
botserv_bot_t *bot = (botserv_bot_t *) n->data; |
| 823 |
|
| 824 |
if (!bot->private) |
| 825 |
command_success_nodata(si, "\2%d:\2 %s (%s@%s) [%s]", ++i, bot->nick, bot->user, bot->host, bot->real); |
| 826 |
} |
| 827 |
|
| 828 |
command_success_nodata(si, _("\2%d\2 bots available."), i); |
| 829 |
if (si->su != NULL && has_priv(si, PRIV_CHAN_ADMIN)) |
| 830 |
{ |
| 831 |
i = 0; |
| 832 |
command_success_nodata(si, _("Listing of private bots available on \2%s\2:"), me.netname); |
| 833 |
MOWGLI_ITER_FOREACH(n, bs_bots.head) |
| 834 |
{ |
| 835 |
botserv_bot_t *bot = (botserv_bot_t *) n->data; |
| 836 |
|
| 837 |
if (bot->private) |
| 838 |
command_success_nodata(si, "\2%d:\2 %s (%s@%s) [%s]", ++i, bot->nick, bot->user, bot->host, bot->real); |
| 839 |
} |
| 840 |
command_success_nodata(si, _("\2%d\2 private bots available."), i); |
| 841 |
} |
| 842 |
command_success_nodata(si, "Use \2/msg %s ASSIGN #chan botnick\2 to assign one to your channel.", si->service->me->nick); |
| 843 |
} |
| 844 |
|
| 845 |
/* ******************************************************************** */ |
| 846 |
|
| 847 |
/* ASSIGN #channel nick */ |
| 848 |
static void bs_cmd_assign(sourceinfo_t *si, int parc, char *parv[]) |
| 849 |
{ |
| 850 |
char *channel = parv[0]; |
| 851 |
channel_t *c = channel_find(channel); |
| 852 |
mychan_t *mc = mychan_from(c); |
| 853 |
metadata_t *md; |
| 854 |
botserv_bot_t *bot; |
| 855 |
|
| 856 |
if (!parv[0] || !parv[1]) |
| 857 |
{ |
| 858 |
command_fail(si, fault_needmoreparams, STR_INVALID_PARAMS, "ASSIGN"); |
| 859 |
command_fail(si, fault_needmoreparams, _("Syntax: ASSIGN <#channel> <nick>")); |
| 860 |
return; |
| 861 |
} |
| 862 |
|
| 863 |
if (mc == NULL) |
| 864 |
{ |
| 865 |
command_fail(si, fault_nosuch_target, _("\2%s\2 is not registered."), parv[0]); |
| 866 |
return; |
| 867 |
} |
| 868 |
|
| 869 |
if ((c != NULL ? c->members.count : 0) < min_users) |
| 870 |
{ |
| 871 |
command_fail(si, fault_noprivs, _("There are not enough users in \2%s\2 to be able to assign a bot."), mc->name); |
| 872 |
return; |
| 873 |
} |
| 874 |
|
| 875 |
if (metadata_find(mc, "private:botserv:no-bot") != NULL) |
| 876 |
{ |
| 877 |
command_fail(si, fault_noprivs, _("You cannot assign bots to \2%s\2."), mc->name); |
| 878 |
return; |
| 879 |
} |
| 880 |
|
| 881 |
if (!chanacs_source_has_flag(mc, si, CA_SET)) |
| 882 |
{ |
| 883 |
command_fail(si, fault_noprivs, _("You are not authorized to assign bots on \2%s\2."), mc->name); |
| 884 |
return; |
| 885 |
} |
| 886 |
|
| 887 |
md = metadata_find(mc, "private:botserv:bot-assigned"); |
| 888 |
|
| 889 |
bot = botserv_bot_find(parv[1]); |
| 890 |
if (bot == NULL) |
| 891 |
{ |
| 892 |
command_fail(si, fault_nosuch_target, _("\2%s\2 is not a bot"), parv[1]); |
| 893 |
return; |
| 894 |
} |
| 895 |
|
| 896 |
if (bot->private && !has_priv(si, PRIV_CHAN_ADMIN)) |
| 897 |
{ |
| 898 |
command_fail(si, fault_noprivs, _("You are not authorized to assign the bot \2%s\2 to a channel."), bot->nick); |
| 899 |
return; |
| 900 |
} |
| 901 |
|
| 902 |
if (md != NULL && !irccasecmp(md->value, parv[1])) |
| 903 |
{ |
| 904 |
command_fail(si, fault_nosuch_target, _("\2%s\2 is already assigned to \2%s\2."), bot->nick, parv[0]); |
| 905 |
return; |
| 906 |
} |
| 907 |
|
| 908 |
if (md == NULL || irccasecmp(md->value, parv[1])) |
| 909 |
{ |
| 910 |
join(mc->name, parv[1]); |
| 911 |
if (md != NULL) |
| 912 |
part(mc->name, md->value); |
| 913 |
} |
| 914 |
|
| 915 |
if (!(mc->chan->flags & CHAN_LOG) && chanuser_find(mc->chan, chansvs.me->me)) |
| 916 |
part(mc->name, chansvs.nick); |
| 917 |
|
| 918 |
metadata_add(mc, "private:botserv:bot-assigned", parv[1]); |
| 919 |
metadata_add(mc, "private:botserv:bot-handle-fantasy", parv[1]); |
| 920 |
|
| 921 |
logcommand(si, CMDLOG_SET, "BOT:ASSIGN: \2%s\2 to \2%s\2", parv[1], parv[0]); |
| 922 |
command_success_nodata(si, _("Assigned the bot \2%s\2 to \2%s\2."), parv[1], parv[0]); |
| 923 |
} |
| 924 |
|
| 925 |
/* ******************************************************************** */ |
| 926 |
|
| 927 |
/* UNASSIGN #channel */ |
| 928 |
static void bs_cmd_unassign(sourceinfo_t *si, int parc, char *parv[]) |
| 929 |
{ |
| 930 |
mychan_t *mc = mychan_find(parv[0]); |
| 931 |
metadata_t *md; |
| 932 |
|
| 933 |
if (!parv[0]) |
| 934 |
{ |
| 935 |
command_fail(si, fault_needmoreparams, STR_INVALID_PARAMS, "UNASSIGN"); |
| 936 |
command_fail(si, fault_needmoreparams, _("Syntax: UNASSIGN <#channel>")); |
| 937 |
return; |
| 938 |
} |
| 939 |
|
| 940 |
if (mc == NULL) |
| 941 |
{ |
| 942 |
command_fail(si, fault_nosuch_target, _("\2%s\2 is not registered."), parv[0]); |
| 943 |
return; |
| 944 |
} |
| 945 |
|
| 946 |
if (!chanacs_source_has_flag(mc, si, CA_SET)) |
| 947 |
{ |
| 948 |
command_fail(si, fault_noprivs, _("You are not authorized to unassign a bot on \2%s\2."), mc->name); |
| 949 |
return; |
| 950 |
} |
| 951 |
|
| 952 |
if ((md = metadata_find(mc, "private:botserv:bot-assigned")) == NULL) |
| 953 |
{ |
| 954 |
command_fail(si, fault_nosuch_key, _("\2%s\2 does not have a bot assigned."), mc->name); |
| 955 |
return; |
| 956 |
} |
| 957 |
|
| 958 |
if (mc->flags & MC_GUARD && (!config_options.leave_chans || |
| 959 |
(mc->chan != NULL && |
| 960 |
MOWGLI_LIST_LENGTH(&mc->chan->members) > 1))) |
| 961 |
join(mc->name, chansvs.nick); |
| 962 |
part(mc->name, md->value); |
| 963 |
metadata_delete(mc, "private:botserv:bot-assigned"); |
| 964 |
metadata_delete(mc, "private:botserv:bot-handle-fantasy"); |
| 965 |
logcommand(si, CMDLOG_SET, "BOT:UNASSIGN: \2%s\2", parv[0]); |
| 966 |
command_success_nodata(si, _("Unassigned the bot from \2%s\2."), parv[0]); |
| 967 |
} |
| 968 |
|
| 969 |
/* ******************************************************************** */ |
| 970 |
|
| 971 |
void _modinit(module_t *m) |
| 972 |
{ |
| 973 |
if (!module_find_published("backend/opensex")) |
| 974 |
{ |
| 975 |
slog(LG_INFO, "Module %s requires use of the OpenSEX database backend, refusing to load.", m->name); |
| 976 |
m->mflags = MODTYPE_FAIL; |
| 977 |
return; |
| 978 |
} |
| 979 |
|
| 980 |
hook_add_event("config_ready"); |
| 981 |
hook_add_config_ready(botserv_config_ready); |
| 982 |
|
| 983 |
hook_add_db_write(botserv_save_database); |
| 984 |
db_register_type_handler("BOT", db_h_bot); |
| 985 |
db_register_type_handler("BOT-COUNT", db_h_bot_count); |
| 986 |
|
| 987 |
hook_add_event("channel_drop"); |
| 988 |
hook_add_channel_drop(bs_channel_drop); |
| 989 |
|
| 990 |
hook_add_event("shutdown"); |
| 991 |
hook_add_shutdown(on_shutdown); |
| 992 |
|
| 993 |
botsvs = service_add("botserv", NULL); |
| 994 |
|
| 995 |
add_uint_conf_item("MIN_USERS", &botsvs->conf_table, 0, &min_users, 0, 65535, 0); |
| 996 |
service_bind_command(botsvs, &bs_bot); |
| 997 |
service_bind_command(botsvs, &bs_assign); |
| 998 |
service_bind_command(botsvs, &bs_unassign); |
| 999 |
service_bind_command(botsvs, &bs_botlist); |
| 1000 |
hook_add_event("channel_join"); |
| 1001 |
hook_add_event("channel_part"); |
| 1002 |
hook_add_event("channel_register"); |
| 1003 |
hook_add_event("channel_add"); |
| 1004 |
hook_add_event("channel_can_change_topic"); |
| 1005 |
hook_add_event("operserv_info"); |
| 1006 |
hook_add_operserv_info(osinfo_hook); |
| 1007 |
hook_add_first_channel_join(bs_join); |
| 1008 |
hook_add_channel_part(bs_part); |
| 1009 |
|
| 1010 |
modestack_mode_simple = bs_modestack_mode_simple; |
| 1011 |
modestack_mode_limit = bs_modestack_mode_limit; |
| 1012 |
modestack_mode_ext = bs_modestack_mode_ext; |
| 1013 |
modestack_mode_param = bs_modestack_mode_param; |
| 1014 |
try_kick = bs_try_kick; |
| 1015 |
topic_sts_real = topic_sts; |
| 1016 |
topic_sts = bs_topic_sts; |
| 1017 |
msg_real = msg; |
| 1018 |
msg = bs_msg; |
| 1019 |
notice_real = notice; |
| 1020 |
notice = bs_notice; |
| 1021 |
} |
| 1022 |
|
| 1023 |
void _moddeinit(module_unload_intent_t intent) |
| 1024 |
{ |
| 1025 |
mowgli_node_t *n, *tn; |
| 1026 |
|
| 1027 |
MOWGLI_ITER_FOREACH_SAFE(n, tn, bs_bots.head) |
| 1028 |
{ |
| 1029 |
botserv_bot_t *bot = (botserv_bot_t *) n->data; |
| 1030 |
|
| 1031 |
mowgli_node_delete(&bot->bnode, &bs_bots); |
| 1032 |
service_delete(bot->me); |
| 1033 |
free(bot->nick); |
| 1034 |
free(bot->user); |
| 1035 |
free(bot->real); |
| 1036 |
free(bot->host); |
| 1037 |
free(bot); |
| 1038 |
} |
| 1039 |
service_unbind_command(botsvs, &bs_bot); |
| 1040 |
service_unbind_command(botsvs, &bs_assign); |
| 1041 |
service_unbind_command(botsvs, &bs_unassign); |
| 1042 |
service_unbind_command(botsvs, &bs_botlist); |
| 1043 |
del_conf_item("MIN_USERS", &botsvs->conf_table); |
| 1044 |
hook_del_channel_join(bs_join); |
| 1045 |
hook_del_channel_part(bs_part); |
| 1046 |
hook_del_channel_drop(bs_channel_drop); |
| 1047 |
hook_del_shutdown(on_shutdown); |
| 1048 |
hook_del_config_ready(botserv_config_ready); |
| 1049 |
hook_del_operserv_info(osinfo_hook); |
| 1050 |
hook_del_db_write(botserv_save_database); |
| 1051 |
db_unregister_type_handler("BOT"); |
| 1052 |
db_unregister_type_handler("BOT-COUNT"); |
| 1053 |
|
| 1054 |
service_delete(botsvs); |
| 1055 |
|
| 1056 |
modestack_mode_simple = modestack_mode_simple_real; |
| 1057 |
modestack_mode_limit = modestack_mode_limit_real; |
| 1058 |
modestack_mode_ext = modestack_mode_ext_real; |
| 1059 |
modestack_mode_param = modestack_mode_param_real; |
| 1060 |
try_kick = try_kick_real; |
| 1061 |
topic_sts = topic_sts_real; |
| 1062 |
msg = msg_real; |
| 1063 |
notice = notice_real; |
| 1064 |
} |
| 1065 |
|
| 1066 |
/* ******************************************************************** */ |
| 1067 |
|
| 1068 |
static void osinfo_hook(sourceinfo_t *si) |
| 1069 |
{ |
| 1070 |
return_if_fail(si != NULL); |
| 1071 |
|
| 1072 |
command_success_nodata(si, "Minimum number of users that must be in a channel for a bot to be assigned: %u", min_users); |
| 1073 |
} |
| 1074 |
|
| 1075 |
static void on_shutdown(void *unused) |
| 1076 |
{ |
| 1077 |
mowgli_node_t *n; |
| 1078 |
|
| 1079 |
MOWGLI_ITER_FOREACH(n, bs_bots.head) |
| 1080 |
{ |
| 1081 |
botserv_bot_t *bot = (botserv_bot_t *) n->data; |
| 1082 |
quit_sts(bot->me->me, "shutting down"); |
| 1083 |
} |
| 1084 |
} |
| 1085 |
|
| 1086 |
static void bs_join(hook_channel_joinpart_t *hdata) |
| 1087 |
{ |
| 1088 |
chanuser_t *cu = hdata->cu; |
| 1089 |
channel_t *chan; |
| 1090 |
mychan_t *mc; |
| 1091 |
botserv_bot_t *bot; |
| 1092 |
metadata_t *md; |
| 1093 |
user_t *u; |
| 1094 |
|
| 1095 |
if (cu == NULL || is_internal_client(cu->user)) |
| 1096 |
return; |
| 1097 |
u = cu->user; |
| 1098 |
chan = cu->chan; |
| 1099 |
|
| 1100 |
/* first check if this is a registered channel at all */ |
| 1101 |
mc = mychan_from(chan); |
| 1102 |
if (mc == NULL) |
| 1103 |
return; |
| 1104 |
|
| 1105 |
/* chanserv's function handles those */ |
| 1106 |
if (metadata_find(mc, "private:botserv:bot-assigned") == NULL) |
| 1107 |
return; |
| 1108 |
|
| 1109 |
bot = bs_mychan_find_bot(mc); |
| 1110 |
if (bot == NULL) |
| 1111 |
{ |
| 1112 |
if (chan->nummembers == 1 && mc->flags & MC_GUARD) |
| 1113 |
join(chan->name, chansvs.nick); |
| 1114 |
} |
| 1115 |
else |
| 1116 |
{ |
| 1117 |
if (chan->nummembers == 1) |
| 1118 |
join(chan->name, bot->nick); |
| 1119 |
|
| 1120 |
if (u->server->flags & SF_EOB && |
| 1121 |
(md = metadata_find(mc, "private:entrymsg")) != NULL) |
| 1122 |
{ |
| 1123 |
if (!u->myuser || !(u->myuser->flags & MU_NOGREET)) |
| 1124 |
notice(bot->nick, u->nick, "[%s] %s", mc->name, md->value); |
| 1125 |
} |
| 1126 |
} |
| 1127 |
} |
| 1128 |
|
| 1129 |
/* ******************************************************************** */ |
| 1130 |
|
| 1131 |
static void |
| 1132 |
bs_part(hook_channel_joinpart_t *hdata) |
| 1133 |
{ |
| 1134 |
chanuser_t *cu; |
| 1135 |
mychan_t *mc; |
| 1136 |
botserv_bot_t *bot; |
| 1137 |
|
| 1138 |
cu = hdata->cu; |
| 1139 |
if (cu == NULL) |
| 1140 |
return; |
| 1141 |
|
| 1142 |
mc = mychan_from(cu->chan); |
| 1143 |
if (mc == NULL) |
| 1144 |
return; |
| 1145 |
|
| 1146 |
/* chanserv's function handles those */ |
| 1147 |
if (metadata_find(mc, "private:botserv:bot-assigned") == NULL) |
| 1148 |
return; |
| 1149 |
|
| 1150 |
bot = bs_mychan_find_bot(mc); |
| 1151 |
if (CURRTIME - mc->used >= 3600) |
| 1152 |
if (chanacs_user_flags(mc, cu->user) & CA_USEDUPDATE) |
| 1153 |
mc->used = CURRTIME; |
| 1154 |
/* |
| 1155 |
* When channel_part is fired, we haven't yet removed the |
| 1156 |
* user from the room. So, the channel will have two members |
| 1157 |
* if ChanServ is joining channels: the triggering user and |
| 1158 |
* itself. |
| 1159 |
* |
| 1160 |
* Do not part if we're enforcing an akick/close in an otherwise |
| 1161 |
* empty channel (MC_INHABIT). -- jilles |
| 1162 |
*/ |
| 1163 |
if (config_options.leave_chans |
| 1164 |
&& !(mc->flags & MC_INHABIT) |
| 1165 |
&& (cu->chan->nummembers - cu->chan->numsvcmembers == 1) |
| 1166 |
&& !is_internal_client(cu->user)) |
| 1167 |
{ |
| 1168 |
if (bot) |
| 1169 |
part(cu->chan->name, bot->nick); |
| 1170 |
else |
| 1171 |
part(cu->chan->name, chansvs.nick); |
| 1172 |
} |
| 1173 |
} |
| 1174 |
|
| 1175 |
/* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs |
| 1176 |
* vim:ts=8 |
| 1177 |
* vim:sw=8 |
| 1178 |
* vim:noexpandtab |
| 1179 |
*/ |