diff options
Diffstat (limited to 'src/botserv.cpp')
-rw-r--r-- | src/botserv.cpp | 252 |
1 files changed, 104 insertions, 148 deletions
diff --git a/src/botserv.cpp b/src/botserv.cpp index 2dd96706d..624d1df83 100644 --- a/src/botserv.cpp +++ b/src/botserv.cpp @@ -7,8 +7,6 @@ * * Based on the original code of Epona by Lara. * Based on the original code of Services by Andy Church. - * - * */ /*************************************************************************/ @@ -17,19 +15,20 @@ #include "modules.h" #include "language.h" -static UserData *get_user_data(Channel * c, User * u); +static UserData *get_user_data(Channel *c, User *u); -static void check_ban(ChannelInfo * ci, User * u, int ttbtype); -static void bot_kick(ChannelInfo * ci, User * u, int message, ...); +static void check_ban(ChannelInfo *ci, User *u, int ttbtype); +static void bot_kick(ChannelInfo *ci, User *u, int message, ...); E void moduleAddBotServCmds(); /*************************************************************************/ -/* *INDENT-OFF* */ -void moduleAddBotServCmds() { + +void moduleAddBotServCmds() +{ ModuleManager::LoadModuleList(Config.BotServCoreModules); } -/* *INDENT-ON* */ + /*************************************************************************/ /*************************************************************************/ @@ -39,7 +38,7 @@ void get_botserv_stats(long *nrec, long *memuse) { long count = 0, mem = 0; - for (botinfo_map::const_iterator it = BotListByNick.begin(); it != BotListByNick.end(); ++it) + for (botinfo_map::const_iterator it = BotListByNick.begin(), it_end = BotListByNick.end(); it != it_end; ++it) { BotInfo *bi = it->second; @@ -62,9 +61,8 @@ void get_botserv_stats(long *nrec, long *memuse) void bs_init() { - if (Config.s_BotServ) { + if (Config.s_BotServ) moduleAddBotServCmds(); - } } /*************************************************************************/ @@ -75,7 +73,7 @@ void botserv(User *u, BotInfo *bi, const std::string &buf) { if (!u || !bi || buf.empty()) return; - + if (buf.find("\1PING ", 0, 6) != std::string::npos && buf[buf.length() - 1] == '\1') { std::string command = buf; @@ -84,23 +82,20 @@ void botserv(User *u, BotInfo *bi, const std::string &buf) ircdproto->SendCTCP(bi, u->nick.c_str(), "%s", command.c_str()); } else - { mod_run_cmd(bi, u, buf); - } } /*************************************************************************/ /* Handles all messages that are sent to registered channels where a * bot is on. - * */ void botchanmsgs(User *u, ChannelInfo *ci, const std::string &buf) { if (!u || !ci || !ci->c || buf.empty()) return; - + /* Answer to ping if needed */ if (buf.find("\1PING ", 0, 6) != std::string::npos && buf[buf.length() - 1] == '\1') { @@ -180,17 +175,16 @@ void botchanmsgs(User *u, ChannelInfo *ci, const std::string &buf) /* Caps kicker */ if (ci->botflags.HasFlag(BS_KICK_CAPS) && realbuf.length() >= ci->capsmin) { - int i = 0; - int l = 0; + int i = 0, l = 0; - for (unsigned j = 0; j < realbuf.length(); ++j) + for (unsigned j = 0, end = realbuf.length(); j < end; ++j) { if (isupper(realbuf[j])) ++i; else if (islower(realbuf[j])) ++l; } - + /* i counts uppercase chars, l counts lowercase chars. Only * alphabetic chars (so islower || isupper) qualify for the * percentage of caps to kick for; the rest is ignored. -GD @@ -212,33 +206,24 @@ void botchanmsgs(User *u, ChannelInfo *ci, const std::string &buf) /* Normalize the buffer */ const char *nbuf = normalizeBuffer(realbuf.c_str()); - for (unsigned i = 0; i < ci->GetBadWordCount(); ++i) + for (unsigned i = 0, end = ci->GetBadWordCount(); i < end; ++i) { BadWord *bw = ci->GetBadWord(i); if (bw->type == BW_ANY && ((Config.BSCaseSensitive && strstr(nbuf, bw->word.c_str())) || (!Config.BSCaseSensitive && stristr(nbuf, bw->word.c_str())))) - { mustkick = true; - } else if (bw->type == BW_SINGLE) { size_t len = bw->word.length(); - if ((Config.BSCaseSensitive && nbuf == bw->word) || (!Config.BSCaseSensitive && (!stricmp(nbuf, bw->word.c_str())))) - { + if ((Config.BSCaseSensitive && nbuf == bw->word) || (!Config.BSCaseSensitive && (!stricmp(nbuf, bw->word.c_str())))) mustkick = true; - } - else if ((strchr(nbuf, ' ') == nbuf + len) && ((Config.BSCaseSensitive && nbuf == bw->word) - || (!Config.BSCaseSensitive && (stristr(nbuf, bw->word.c_str()) == nbuf)))) - { + else if ((strchr(nbuf, ' ') == nbuf + len) && ((Config.BSCaseSensitive && nbuf == bw->word) || (!Config.BSCaseSensitive && (stristr(nbuf, bw->word.c_str()) == nbuf)))) mustkick = true; - } else { if ((strrchr(nbuf, ' ') == nbuf + strlen(nbuf) - len - 1) && ((Config.BSCaseSensitive && (strstr(nbuf, bw->word.c_str()) == nbuf + strlen(nbuf) - len)) || (!Config.BSCaseSensitive && (stristr(nbuf, bw->word.c_str()) == nbuf + strlen(nbuf) - len)))) - { mustkick = true; - } else { char *wordbuf = new char[len + 3]; @@ -248,10 +233,8 @@ void botchanmsgs(User *u, ChannelInfo *ci, const std::string &buf) wordbuf[len + 2] = '\0'; memcpy(wordbuf + 1, bw->word.c_str(), len); - if ((Config.BSCaseSensitive && (strstr(nbuf, wordbuf))) || (!Config.BSCaseSensitive && (stristr(nbuf, wordbuf)))) - { + if ((Config.BSCaseSensitive && strstr(nbuf, wordbuf)) || (!Config.BSCaseSensitive && stristr(nbuf, wordbuf))) mustkick = true; - } delete [] wordbuf; } @@ -261,10 +244,8 @@ void botchanmsgs(User *u, ChannelInfo *ci, const std::string &buf) { size_t len = bw->word.length(); - if ((Config.BSCaseSensitive && (!strncmp(nbuf, bw->word.c_str(), len))) || (!Config.BSCaseSensitive && (!strnicmp(nbuf, bw->word.c_str(), len)))) - { + if ((Config.BSCaseSensitive && !strncmp(nbuf, bw->word.c_str(), len)) || (!Config.BSCaseSensitive && !strnicmp(nbuf, bw->word.c_str(), len))) mustkick = true; - } else { char *wordbuf = new char[len + 2]; @@ -273,10 +254,8 @@ void botchanmsgs(User *u, ChannelInfo *ci, const std::string &buf) wordbuf[0] = ' '; wordbuf[len + 1] = '\0'; - if ((Config.BSCaseSensitive && (strstr(nbuf, wordbuf))) || (!Config.BSCaseSensitive && (stristr(nbuf, wordbuf)))) - { + if ((Config.BSCaseSensitive && strstr(nbuf, wordbuf)) || (!Config.BSCaseSensitive && stristr(nbuf, wordbuf))) mustkick = true; - } delete [] wordbuf; } @@ -285,11 +264,8 @@ void botchanmsgs(User *u, ChannelInfo *ci, const std::string &buf) { size_t len = bw->word.length(); - if ((Config.BSCaseSensitive && (!strncmp(nbuf + strlen(nbuf) - len, bw->word.c_str(), len))) - || (!Config.BSCaseSensitive && (!strnicmp(nbuf + strlen(nbuf) - len, bw->word.c_str(), len)))) - { + if ((Config.BSCaseSensitive && !strncmp(nbuf + strlen(nbuf) - len, bw->word.c_str(), len)) || (!Config.BSCaseSensitive && !strnicmp(nbuf + strlen(nbuf) - len, bw->word.c_str(), len))) mustkick = true; - } else { char *wordbuf = new char[len + 2]; @@ -298,10 +274,8 @@ void botchanmsgs(User *u, ChannelInfo *ci, const std::string &buf) wordbuf[len] = ' '; wordbuf[len + 1] = '\0'; - if ((Config.BSCaseSensitive && (strstr(nbuf, wordbuf))) || (!Config.BSCaseSensitive && (stristr(nbuf, wordbuf)))) - { + if ((Config.BSCaseSensitive && strstr(nbuf, wordbuf)) || (!Config.BSCaseSensitive && stristr(nbuf, wordbuf))) mustkick = true; - } delete [] wordbuf; } @@ -341,7 +315,7 @@ void botchanmsgs(User *u, ChannelInfo *ci, const std::string &buf) ud->lines = 0; } - ud->lines++; + ++ud->lines; if (ud->lines >= ci->floodlines) { check_ban(ci, u, TTB_FLOOD); @@ -356,7 +330,7 @@ void botchanmsgs(User *u, ChannelInfo *ci, const std::string &buf) UserData *ud = get_user_data(ci->c, u); if (!ud) return; - + if (ud->lastline && stricmp(ud->lastline, buf.c_str())) { delete [] ud->lastline; @@ -367,7 +341,7 @@ void botchanmsgs(User *u, ChannelInfo *ci, const std::string &buf) { if (!ud->lastline) ud->lastline = sstrdup(buf.c_str()); - ud->times++; + ++ud->times; } if (ud->times >= ci->repeattimes) @@ -381,9 +355,7 @@ void botchanmsgs(User *u, ChannelInfo *ci, const std::string &buf) /* return if the user is on the ignore list */ if (get_ignore(u->nick.c_str()) != NULL) - { return; - } /* Fantaisist commands */ if (ci->botflags.HasFlag(BS_FANTASY) && buf[0] == *Config.BSFantasyCharacter && !was_action) @@ -391,7 +363,7 @@ void botchanmsgs(User *u, ChannelInfo *ci, const std::string &buf) spacesepstream sep(buf); std::string token; - if (sep.GetToken(token) && (token[0] == *Config.BSFantasyCharacter)) + if (sep.GetToken(token) && token[0] == *Config.BSFantasyCharacter) { /* Strip off the fantasy character */ token.erase(token.begin()); @@ -461,13 +433,12 @@ BotInfo *findbot(const ci::string &nick) return NULL; } - /*************************************************************************/ /* Returns ban data associated with an user if it exists, allocates it otherwise. */ -static BanData *get_ban_data(Channel * c, User * u) +static BanData *get_ban_data(Channel *c, User *u) { char mask[BUFSIZE]; BanData *bd, *next; @@ -476,11 +447,12 @@ static BanData *get_ban_data(Channel * c, User * u) if (!c || !u) return NULL; - snprintf(mask, sizeof(mask), "%s@%s", u->GetIdent().c_str(), - u->GetDisplayedHost().c_str()); + snprintf(mask, sizeof(mask), "%s@%s", u->GetIdent().c_str(), u->GetDisplayedHost().c_str()); - for (bd = c->bd; bd; bd = next) { - if (now - bd->last_use > Config.BSKeepData) { + for (bd = c->bd; bd; bd = next) + { + if (now - bd->last_use > Config.BSKeepData) + { if (bd->next) bd->next->prev = bd->prev; if (bd->prev) @@ -493,7 +465,8 @@ static BanData *get_ban_data(Channel * c, User * u) delete bd; continue; } - if (!stricmp(bd->mask, mask)) { + if (!stricmp(bd->mask, mask)) + { bd->last_use = now; return bd; } @@ -527,7 +500,7 @@ static UserData *get_user_data(Channel *c, User *u) if (!c || !u) return NULL; - for (CUserList::iterator it = c->users.begin(); it != c->users.end(); ++it) + for (CUserList::iterator it = c->users.begin(), it_end = c->users.end(); it != it_end; ++it) { UserContainer *uc = *it; @@ -574,31 +547,22 @@ void bot_join(ChannelInfo * ci) next = ban->next; if (entry_match(ban, ci->bi->nick.c_str(), ci->bi->user.c_str(), ci->bi->host.c_str(), 0)) - { ci->c->RemoveMode(NULL, CMODE_BAN, ban->mask); - } } } std::string Limit; int limit = 0; if (ci->c->GetParam(CMODE_LIMIT, Limit)) - { limit = atoi(Limit.c_str()); - } /* Should we be invited? */ - if (ci->c->HasMode(CMODE_INVITE) - || (limit && ci->c->users.size() >= limit)) - ircdproto->SendNoticeChanops(ci->bi, ci->c, - "%s invited %s into the channel.", - ci->bi->nick.c_str(), ci->bi->nick.c_str()); + if (ci->c->HasMode(CMODE_INVITE) || (limit && ci->c->users.size() >= limit)) + ircdproto->SendNoticeChanops(ci->bi, ci->c, "%s invited %s into the channel.", ci->bi->nick.c_str(), ci->bi->nick.c_str()); } ircdproto->SendJoin(ci->bi, ci->c->name.c_str(), ci->c->creation_time); - for (std::list<ChannelModeStatus *>::iterator it = BotModes.begin(); it != BotModes.end(); ++it) - { + for (std::list<ChannelModeStatus *>::iterator it = BotModes.begin(), it_end = BotModes.end(); it != it_end; ++it) ci->c->SetMode(ci->bi, *it, ci->bi->nick, false); - } FOREACH_MOD(I_OnBotJoin, OnBotJoin(ci, ci->bi)); } @@ -620,7 +584,7 @@ static void check_ban(ChannelInfo *ci, User *u, int ttbtype) if (u->server->IsULined()) return; - bd->ttb[ttbtype]++; + ++bd->ttb[ttbtype]; if (ci->ttb[ttbtype] && bd->ttb[ttbtype] >= ci->ttb[ttbtype]) { /* Should not use == here because bd->ttb[ttbtype] could possibly be > ci->ttb[ttbtype] @@ -642,7 +606,7 @@ static void check_ban(ChannelInfo *ci, User *u, int ttbtype) /* This makes a bot kick an user. Works somewhat like notice_lang in fact ;) */ -static void bot_kick(ChannelInfo * ci, User * u, int message, ...) +static void bot_kick(ChannelInfo *ci, User *u, int message, ...) { va_list args; char buf[1024]; @@ -665,7 +629,7 @@ static void bot_kick(ChannelInfo * ci, User * u, int message, ...) /* Makes a simple ban and kicks the target */ -void bot_raw_ban(User * requester, ChannelInfo * ci, char *nick, const char *reason) +void bot_raw_ban(User *requester, ChannelInfo *ci, char *nick, const char *reason) { char mask[BUFSIZE]; User *u = finduser(nick); @@ -673,23 +637,19 @@ void bot_raw_ban(User * requester, ChannelInfo * ci, char *nick, const char *rea if (!u) return; - if ((ModeManager::FindUserModeByName(UMODE_PROTECTED))) + if (ModeManager::FindUserModeByName(UMODE_PROTECTED) && u->IsProtected() && requester != u) { - if (u->IsProtected() && (requester != u)) { - ircdproto->SendPrivmsg(ci->bi, ci->name.c_str(), "%s", getstring(ACCESS_DENIED)); - return; - } + ircdproto->SendPrivmsg(ci->bi, ci->name.c_str(), "%s", getstring(ACCESS_DENIED)); + return; } - if (ci->HasFlag(CI_PEACE) && stricmp(requester->nick.c_str(), nick) && (get_access(u, ci) >= get_access(requester, ci))) + if (ci->HasFlag(CI_PEACE) && stricmp(requester->nick.c_str(), nick) && get_access(u, ci) >= get_access(requester, ci)) return; - if (ModeManager::FindChannelModeByName(CMODE_EXCEPT)) + if (ModeManager::FindChannelModeByName(CMODE_EXCEPT) && is_excepted(ci, u) == 1) { - if (is_excepted(ci, u) == 1) { - ircdproto->SendPrivmsg(ci->bi, ci->name.c_str(), "%s", getstring(BOT_EXCEPT)); - return; - } + ircdproto->SendPrivmsg(ci->bi, ci->name.c_str(), "%s", getstring(BOT_EXCEPT)); + return; } get_idealban(ci, u, mask, sizeof(mask)); @@ -707,22 +667,20 @@ void bot_raw_ban(User * requester, ChannelInfo * ci, char *nick, const char *rea /* Makes a kick with a "dynamic" reason ;) */ -void bot_raw_kick(User * requester, ChannelInfo * ci, char *nick, const char *reason) +void bot_raw_kick(User *requester, ChannelInfo *ci, char *nick, const char *reason) { User *u = finduser(nick); if (!u || !ci->c->FindUser(u)) return; - if ((ModeManager::FindUserModeByName(UMODE_PROTECTED))) + if (ModeManager::FindUserModeByName(UMODE_PROTECTED) && u->IsProtected() && requester != u) { - if (u->IsProtected() && (requester != u)) { - ircdproto->SendPrivmsg(ci->bi, ci->name.c_str(), "%s", getstring(ACCESS_DENIED)); - return; - } + ircdproto->SendPrivmsg(ci->bi, ci->name.c_str(), "%s", getstring(ACCESS_DENIED)); + return; } - if (ci->HasFlag(CI_PEACE) && stricmp(requester->nick.c_str(), nick) && (get_access(u, ci) >= get_access(requester, ci))) + if (ci->HasFlag(CI_PEACE) && stricmp(requester->nick.c_str(), nick) && get_access(u, ci) >= get_access(requester, ci)) return; if (ci->HasFlag(CI_SIGNKICK) || (ci->HasFlag(CI_SIGNKICK_LEVEL) && !check_access(requester, ci, CA_SIGNKICK))) @@ -735,12 +693,11 @@ void bot_raw_kick(User * requester, ChannelInfo * ci, char *nick, const char *re /* Makes a mode operation on a channel for a nick */ -void bot_raw_mode(User * requester, ChannelInfo * ci, const char *mode, char *nick) +void bot_raw_mode(User *requester, ChannelInfo *ci, const char *mode, char *nick) { - char buf[BUFSIZE]; + char buf[BUFSIZE] = ""; User *u; - *buf = '\0'; u = finduser(nick); if (!u || !ci->c->FindUser(u)) @@ -748,15 +705,13 @@ void bot_raw_mode(User * requester, ChannelInfo * ci, const char *mode, char *ni snprintf(buf, BUFSIZE - 1, "%ld", static_cast<long>(time(NULL))); - if ((ModeManager::FindUserModeByName(UMODE_PROTECTED))) { - if (u->IsProtected() && *mode == '-' && (requester != u)) { - ircdproto->SendPrivmsg(ci->bi, ci->name.c_str(), "%s", getstring(ACCESS_DENIED)); - return; - } + if (ModeManager::FindUserModeByName(UMODE_PROTECTED) && u->IsProtected() && *mode == '-' && requester != u) + { + ircdproto->SendPrivmsg(ci->bi, ci->name.c_str(), "%s", getstring(ACCESS_DENIED)); + return; } - if (*mode == '-' && ci->HasFlag(CI_PEACE) - && stricmp(requester->nick.c_str(), nick) && (get_access(u, ci) >= get_access(requester, ci))) + if (*mode == '-' && ci->HasFlag(CI_PEACE) && stricmp(requester->nick.c_str(), nick) && get_access(u, ci) >= get_access(requester, ci)) return; ci->c->SetModes(NULL, "%s %s", mode, nick); @@ -776,69 +731,70 @@ char *normalizeBuffer(const char *buf) len = strlen(buf); newbuf = new char[len + 1]; - for (i = 0; i < len; i++) { - switch (buf[i]) { + for (i = 0; i < len; ++i) + { + switch (buf[i]) + { /* ctrl char */ - case 1: - break; + case 1: + break; /* Bold ctrl char */ - case 2: - break; + case 2: + break; /* Color ctrl char */ - case 3: - /* If the next character is a digit, its also removed */ - if (isdigit(buf[i + 1])) { - i++; - - /* not the best way to remove colors - * which are two digit but no worse then - * how the Unreal does with +S - TSL - */ - if (isdigit(buf[i + 1])) { - i++; - } - - /* Check for background color code - * and remove it as well - */ - if (buf[i + 1] == ',') { - i++; + case 3: + /* If the next character is a digit, its also removed */ + if (isdigit(buf[i + 1])) + { + ++i; - if (isdigit(buf[i + 1])) { - i++; - } /* not the best way to remove colors * which are two digit but no worse then * how the Unreal does with +S - TSL */ - if (isdigit(buf[i + 1])) { - i++; + if (isdigit(buf[i + 1])) + ++i; + + /* Check for background color code + * and remove it as well + */ + if (buf[i + 1] == ',') + { + ++i; + + if (isdigit(buf[i + 1])) + ++i; + /* not the best way to remove colors + * which are two digit but no worse then + * how the Unreal does with +S - TSL + */ + if (isdigit(buf[i + 1])) + ++i; } } - } - break; + break; /* line feed char */ - case 10: - break; + case 10: + break; /* carriage returns char */ - case 13: - break; + case 13: + break; /* Reverse ctrl char */ - case 22: - break; + case 22: + break; /* Underline ctrl char */ - case 31: - break; + case 31: + break; /* A valid char gets copied into the new buffer */ - default: - newbuf[j] = buf[i]; - j++; + default: + newbuf[j] = buf[i]; + ++j; } } /* Terminate the string */ newbuf[j] = 0; - return (newbuf); + return newbuf; } |