From 4a2b9ebcf38d6c0a2860b966421b3af125438488 Mon Sep 17 00:00:00 2001 From: Adam Date: Tue, 25 May 2010 00:57:25 -0500 Subject: Renamed all of source files from .c to .cpp --- src/core/bs_badwords.cpp | 282 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 282 insertions(+) create mode 100644 src/core/bs_badwords.cpp (limited to 'src/core/bs_badwords.cpp') diff --git a/src/core/bs_badwords.cpp b/src/core/bs_badwords.cpp new file mode 100644 index 000000000..ce026e9c4 --- /dev/null +++ b/src/core/bs_badwords.cpp @@ -0,0 +1,282 @@ +/* BotServ core functions + * + * (C) 2003-2010 Anope Team + * Contact us at team@anope.org + * + * Please read COPYING and README for further details. + * + * Based on the original code of Epona by Lara. + * Based on the original code of Services by Andy Church. + * + * + */ +/*************************************************************************/ + +#include "module.h" + +class BadwordsListCallback : public NumberList +{ + User *u; + ChannelInfo *ci; + bool SentHeader; + public: + BadwordsListCallback(User *_u, ChannelInfo *_ci, const std::string &list) : NumberList(list), u(_u), ci(_ci), SentHeader(false) + { + } + + ~BadwordsListCallback() + { + if (!SentHeader) + notice_lang(Config.s_BotServ, u, BOT_BADWORDS_NO_MATCH, ci->name.c_str()); + } + + void HandleNumber(unsigned Number) + { + if (Number > ci->GetBadWordCount()) + return; + + if (!SentHeader) + { + SentHeader = true; + notice_lang(Config.s_BotServ, u, BOT_BADWORDS_LIST_HEADER, ci->name.c_str()); + } + + DoList(u, ci, Number - 1, ci->GetBadWord(Number - 1)); + } + + static void DoList(User *u, ChannelInfo *ci, unsigned Number, BadWord *bw) + { + notice_lang(Config.s_BotServ, u, BOT_BADWORDS_LIST_FORMAT, Number + 1, bw->word.c_str(), ((bw->type == BW_SINGLE) ? "(SINGLE)" : ((bw->type == BW_START) ? "(START)" : ((bw->type == BW_END) ? "(END)" : "")))); + } +}; + +class BadwordsDelCallback : public NumberList +{ + User *u; + ChannelInfo *ci; + unsigned Deleted; + public: + BadwordsDelCallback(User *_u, ChannelInfo *_ci, const std::string &list) : NumberList(list), u(_u), ci(_ci), Deleted(0) + { + } + + ~BadwordsDelCallback() + { + if (!Deleted) + notice_lang(Config.s_BotServ, u, BOT_BADWORDS_NO_MATCH, ci->name.c_str()); + else if (Deleted == 1) + notice_lang(Config.s_BotServ, u, BOT_BADWORDS_DELETED_ONE, ci->name.c_str()); + else + notice_lang(Config.s_BotServ, u, BOT_BADWORDS_DELETED_SEVERAL, Deleted, ci->name.c_str()); + } + + void HandleNumber(unsigned Number) + { + if (Number > ci->GetBadWordCount()) + return; + + ++Deleted; + ci->EraseBadWord(Number - 1); + } +}; + +class CommandBSBadwords : public Command +{ + private: + CommandReturn DoList(User *u, ChannelInfo *ci, const ci::string &word) + { + if (!ci->GetBadWordCount()) + { + notice_lang(Config.s_BotServ, u, BOT_BADWORDS_LIST_EMPTY, ci->name.c_str()); + } + else if (!word.empty() && strspn(word.c_str(), "1234567890,-") == word.length()) + (new BadwordsListCallback(u, ci, word.c_str()))->Process(); + else + { + bool SentHeader = false; + + for (unsigned i = 0; i < ci->GetBadWordCount(); i++) + { + BadWord *bw = ci->GetBadWord(i); + + if (!word.empty() && !Anope::Match(bw->word, word.c_str(), false)) + continue; + + if (!SentHeader) + { + SentHeader = true; + notice_lang(Config.s_BotServ, u, BOT_BADWORDS_LIST_HEADER, ci->name.c_str()); + } + + BadwordsListCallback::DoList(u, ci, i, bw); + } + + if (!SentHeader) + notice_lang(Config.s_BotServ, u, BOT_BADWORDS_NO_MATCH, ci->name.c_str()); + } + + return MOD_CONT; + } + + CommandReturn DoAdd(User *u, ChannelInfo *ci, const ci::string &word) + { + size_t pos = word.find_last_of(" "); + BadWordType type = BW_ANY; + ci::string realword = word; + + if (pos != ci::string::npos) + { + ci::string opt = ci::string(word, pos + 1); + if (!opt.empty()) + { + if (opt == "SINGLE") + type = BW_SINGLE; + else if (opt == "START") + type = BW_START; + else if (opt == "END") + type = BW_END; + } + realword = ci::string(word, 0, pos); + } + + if (ci->GetBadWordCount() >= Config.BSBadWordsMax) + { + notice_lang(Config.s_BotServ, u, BOT_BADWORDS_REACHED_LIMIT, Config.BSBadWordsMax); + return MOD_CONT; + } + + for (unsigned i = 0; i < ci->GetBadWordCount(); ++i) + { + BadWord *bw = ci->GetBadWord(i); + + if (!bw->word.empty() && (Config.BSCaseSensitive && !stricmp(bw->word.c_str(), realword.c_str()) + || (!Config.BSCaseSensitive && bw->word == realword.c_str()))) + { + notice_lang(Config.s_BotServ, u, BOT_BADWORDS_ALREADY_EXISTS, bw->word.c_str(), ci->name.c_str()); + return MOD_CONT; + } + } + + ci->AddBadWord(realword.c_str(), type); + + notice_lang(Config.s_BotServ, u, BOT_BADWORDS_ADDED, realword.c_str(), ci->name.c_str()); + + return MOD_CONT; + } + + CommandReturn DoDelete(User *u, ChannelInfo *ci, const ci::string &word) + { + /* Special case: is it a number/list? Only do search if it isn't. */ + if (!word.empty() && isdigit(word[0]) && strspn(word.c_str(), "1234567890,-") == word.length()) + (new BadwordsDelCallback(u, ci, word.c_str()))->Process(); + else + { + unsigned i; + BadWord *badword; + + for (i = 0; i < ci->GetBadWordCount(); ++i) + { + badword = ci->GetBadWord(i); + + if (badword->word == word) + break; + } + + if (i == ci->GetBadWordCount()) + { + notice_lang(Config.s_BotServ, u, BOT_BADWORDS_NOT_FOUND, word.c_str(), ci->name.c_str()); + return MOD_CONT; + } + + ci->EraseBadWord(i); + + notice_lang(Config.s_BotServ, u, BOT_BADWORDS_DELETED, badword->word.c_str(), ci->name.c_str()); + } + + return MOD_CONT; + } + + CommandReturn DoClear(User *u, ChannelInfo *ci) + { + ci->ClearBadWords(); + notice_lang(Config.s_BotServ, u, BOT_BADWORDS_CLEAR); + return MOD_CONT; + } + public: + CommandBSBadwords() : Command("BADWORDS", 2, 3) + { + } + + CommandReturn Execute(User *u, const std::vector ¶ms) + { + const char *chan = params[0].c_str(); + ci::string cmd = params[1]; + ci::string word = params.size() > 2 ? params[2].c_str() : ""; + ChannelInfo *ci; + bool need_args = cmd == "LIST" || cmd == "CLEAR"; + + if (!need_args && word.empty()) + { + this->OnSyntaxError(u, cmd); + return MOD_CONT; + } + + ci = cs_findchan(chan); + + if (!check_access(u, ci, CA_BADWORDS) && (!need_args || !u->Account()->HasPriv("botserv/administration"))) + { + notice_lang(Config.s_BotServ, u, ACCESS_DENIED); + return MOD_CONT; + } + + if (readonly) + { + notice_lang(Config.s_BotServ, u, BOT_BADWORDS_DISABLED); + return MOD_CONT; + } + + if (cmd == "ADD") + return this->DoAdd(u, ci, word); + else if (cmd == "DEL") + return this->DoDelete(u, ci, word); + else if (cmd == "LIST") + return this->DoList(u, ci, word); + else if (cmd == "CLEAR") + return this->DoClear(u, ci); + else + this->OnSyntaxError(u, ""); + + return MOD_CONT; + } + + bool OnHelp(User *u, const ci::string &subcommand) + { + notice_help(Config.s_BotServ, u, BOT_HELP_BADWORDS); + return true; + } + + void OnSyntaxError(User *u, const ci::string &subcommand) + { + syntax_error(Config.s_BotServ, u, "BADWORDS", BOT_BADWORDS_SYNTAX); + } +}; + +class BSBadwords : public Module +{ + public: + BSBadwords(const std::string &modname, const std::string &creator) : Module(modname, creator) + { + this->SetAuthor("Anope"); + this->SetVersion(VERSION_STRING); + this->SetType(CORE); + this->AddCommand(BotServ, new CommandBSBadwords); + + ModuleManager::Attach(I_OnBotServHelp, this); + } + void OnBotServHelp(User *u) + { + notice_lang(Config.s_BotServ, u, BOT_HELP_CMD_BADWORDS); + } +}; + +MODULE_INIT(BSBadwords) -- cgit From f0a44ba89471e006ab10e398e9635adbc73eb674 Mon Sep 17 00:00:00 2001 From: Adam Date: Tue, 25 May 2010 13:50:17 -0500 Subject: Made NumberList take an arg to determin if it should pass numbers in descending order, fixes listing specific ranges being returned in descending order --- src/core/bs_badwords.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/core/bs_badwords.cpp') diff --git a/src/core/bs_badwords.cpp b/src/core/bs_badwords.cpp index ce026e9c4..d329aa8e0 100644 --- a/src/core/bs_badwords.cpp +++ b/src/core/bs_badwords.cpp @@ -20,7 +20,7 @@ class BadwordsListCallback : public NumberList ChannelInfo *ci; bool SentHeader; public: - BadwordsListCallback(User *_u, ChannelInfo *_ci, const std::string &list) : NumberList(list), u(_u), ci(_ci), SentHeader(false) + BadwordsListCallback(User *_u, ChannelInfo *_ci, const std::string &list) : NumberList(list, false), u(_u), ci(_ci), SentHeader(false) { } @@ -56,7 +56,7 @@ class BadwordsDelCallback : public NumberList ChannelInfo *ci; unsigned Deleted; public: - BadwordsDelCallback(User *_u, ChannelInfo *_ci, const std::string &list) : NumberList(list), u(_u), ci(_ci), Deleted(0) + BadwordsDelCallback(User *_u, ChannelInfo *_ci, const std::string &list) : NumberList(list, true), u(_u), ci(_ci), Deleted(0) { } -- cgit From c43666e344250e4d559d116a73a9d4a313cf1645 Mon Sep 17 00:00:00 2001 From: Adam Date: Thu, 27 May 2010 19:41:16 -0400 Subject: Rewrote cs_access to be more C++ish, changed NumberList to be more C++ish and fixed some compiler warnings on 64bit systems --- src/core/bs_badwords.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/core/bs_badwords.cpp') diff --git a/src/core/bs_badwords.cpp b/src/core/bs_badwords.cpp index d329aa8e0..62a9ef665 100644 --- a/src/core/bs_badwords.cpp +++ b/src/core/bs_badwords.cpp @@ -149,7 +149,7 @@ class CommandBSBadwords : public Command { BadWord *bw = ci->GetBadWord(i); - if (!bw->word.empty() && (Config.BSCaseSensitive && !stricmp(bw->word.c_str(), realword.c_str()) + if (!bw->word.empty() && ((Config.BSCaseSensitive && !stricmp(bw->word.c_str(), realword.c_str())) || (!Config.BSCaseSensitive && bw->word == realword.c_str()))) { notice_lang(Config.s_BotServ, u, BOT_BADWORDS_ALREADY_EXISTS, bw->word.c_str(), ci->name.c_str()); -- cgit From 6cd88494662c5c4af2da891926dcc82367596cb9 Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 30 May 2010 17:21:44 -0400 Subject: Moved the *Serv help functions to Command, will come in use later with subcommands --- src/core/bs_badwords.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'src/core/bs_badwords.cpp') diff --git a/src/core/bs_badwords.cpp b/src/core/bs_badwords.cpp index 62a9ef665..7a9c6ebdf 100644 --- a/src/core/bs_badwords.cpp +++ b/src/core/bs_badwords.cpp @@ -259,6 +259,11 @@ class CommandBSBadwords : public Command { syntax_error(Config.s_BotServ, u, "BADWORDS", BOT_BADWORDS_SYNTAX); } + + void OnServHelp(User *u) + { + notice_lang(Config.s_BotServ, u, BOT_HELP_CMD_BADWORDS); + } }; class BSBadwords : public Module @@ -270,12 +275,6 @@ class BSBadwords : public Module this->SetVersion(VERSION_STRING); this->SetType(CORE); this->AddCommand(BotServ, new CommandBSBadwords); - - ModuleManager::Attach(I_OnBotServHelp, this); - } - void OnBotServHelp(User *u) - { - notice_lang(Config.s_BotServ, u, BOT_HELP_CMD_BADWORDS); } }; -- cgit