diff options
author | Adam <Adam@anope.org> | 2011-08-05 05:35:31 -0400 |
---|---|---|
committer | Adam <Adam@anope.org> | 2011-08-05 05:35:31 -0400 |
commit | e66063e6304538d34c40460ca0aa2be5ddb6bdec (patch) | |
tree | f50fe31097160f8f794669809e4f4ef87f477672 /modules/commands/bs_bot.cpp | |
parent | 9ec18a3b020932eee6242c878149c484f49b13cb (diff) |
Rewrote the example configurations and split them
up into seperate files for each pseudo client.
Also reorganized how the modules are stored, and
made most of the old "extra" modules "core"
Diffstat (limited to 'modules/commands/bs_bot.cpp')
-rw-r--r-- | modules/commands/bs_bot.cpp | 414 |
1 files changed, 414 insertions, 0 deletions
diff --git a/modules/commands/bs_bot.cpp b/modules/commands/bs_bot.cpp new file mode 100644 index 000000000..7500d1926 --- /dev/null +++ b/modules/commands/bs_bot.cpp @@ -0,0 +1,414 @@ +/* BotServ core functions + * + * (C) 2003-2011 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 CommandBSBot : public Command +{ + private: + void DoAdd(CommandSource &source, const std::vector<Anope::string> ¶ms) + { + const Anope::string &nick = params[1]; + const Anope::string &user = params[2]; + const Anope::string &host = params[3]; + const Anope::string &real = params[4]; + BotInfo *bi; + + if (findbot(nick)) + { + source.Reply(_("Bot \002%s\002 already exists."), nick.c_str()); + return; + } + + if (nick.length() > Config->NickLen) + { + source.Reply(_("Bot Nicks may only contain valid nick characters.")); + return; + } + + if (user.length() > Config->UserLen) + { + source.Reply(_("Bot Idents may only contain %d characters."), Config->UserLen); + return; + } + + if (host.length() > Config->HostLen) + { + source.Reply(_("Bot Hosts may only contain %d characters."), Config->HostLen); + return; + } + + /* Check the nick is valid re RFC 2812 */ + if (isdigit(nick[0]) || nick[0] == '-') + { + source.Reply(_("Bot Nicks may only contain valid nick characters.")); + return; + } + + for (unsigned i = 0, end = nick.length(); i < end && i < Config->NickLen; ++i) + if (!isvalidnick(nick[i])) + { + source.Reply(_("Bot Nicks may only contain valid nick characters.")); + return; + } + + /* check for hardcored ircd forbidden nicks */ + if (!ircdproto->IsNickValid(nick)) + { + source.Reply(_("Bot Nicks may only contain valid nick characters.")); + return; + } + + /* Check the host is valid re RFC 2812 */ + if (!isValidHost(host, 3)) + { + source.Reply(_("Bot Hosts may only contain valid host characters.")); + return; + } + + for (unsigned i = 0, end = user.length(); i < end && i < Config->UserLen; ++i) + if (!isalnum(user[i])) + { + source.Reply(_("Bot Idents may only contain valid characters."), Config->UserLen); + return; + } + + /* We check whether the nick is registered, and inform the user + * if so. You need to drop the nick manually before you can use + * it as a bot nick from now on -GD + */ + if (findnick(nick)) + { + source.Reply(NICK_ALREADY_REGISTERED, nick.c_str()); + return; + } + + bi = new BotInfo(nick, user, host, real); + + Log(LOG_ADMIN, source.u, this) << "ADD " << bi->GetMask() << " " << bi->realname; + + source.Reply(_("%s!%s@%s (%s) added to the bot list."), bi->nick.c_str(), bi->GetIdent().c_str(), bi->host.c_str(), bi->realname.c_str()); + + FOREACH_MOD(I_OnBotCreate, OnBotCreate(bi)); + return; + } + + void DoChange(CommandSource &source, const std::vector<Anope::string> ¶ms) + { + const Anope::string &oldnick = params[1]; + const Anope::string &nick = params.size() > 2 ? params[2] : ""; + const Anope::string &user = params.size() > 3 ? params[3] : ""; + const Anope::string &host = params.size() > 4 ? params[4] : ""; + const Anope::string &real = params.size() > 5 ? params[5] : ""; + BotInfo *bi; + + if (oldnick.empty() || nick.empty()) + { + this->OnSyntaxError(source, "CHANGE"); + return; + } + + if (!(bi = findbot(oldnick))) + { + source.Reply(BOT_DOES_NOT_EXIST, oldnick.c_str()); + return; + } + + if (!oldnick.equals_ci(nick) && nickIsServices(oldnick, false)) + { + source.Reply(BOT_DOES_NOT_EXIST, oldnick.c_str()); + return; + } + + if (nick.length() > Config->NickLen) + { + source.Reply(_("Bot Nicks may only contain valid nick characters.")); + return; + } + + if (!user.empty() && user.length() > Config->UserLen) + { + source.Reply(_("Bot Idents may only contain %d characters."), Config->UserLen); + return; + } + + if (!host.empty() && host.length() > Config->HostLen) + { + source.Reply(_("Bot Hosts may only contain %d characters."), Config->HostLen); + return; + } + + if (!oldnick.equals_ci(nick) && nickIsServices(nick, false)) + { + source.Reply(BOT_DOES_NOT_EXIST, oldnick.c_str()); + return; + } + + /* Checks whether there *are* changes. + * Case sensitive because we may want to change just the case. + * And we must finally check that the nick is not already + * taken by another bot. + */ + if (nick.equals_cs(bi->nick) && (!user.empty() ? user.equals_cs(bi->GetIdent()) : 1) && (!host.empty() ? host.equals_cs(bi->host) : 1) && (!real.empty() ? real.equals_cs(bi->realname) : 1)) + { + source.Reply(_("Old info is equal to the new one.")); + return; + } + + /* Check the nick is valid re RFC 2812 */ + if (isdigit(nick[0]) || nick[0] == '-') + { + source.Reply(_("Bot Nicks may only contain valid nick characters.")); + return; + } + + for (unsigned i = 0, end = nick.length(); i < end && i < Config->NickLen; ++i) + if (!isvalidnick(nick[i])) + { + source.Reply(_("Bot Nicks may only contain valid nick characters.")); + return; + } + + /* check for hardcored ircd forbidden nicks */ + if (!ircdproto->IsNickValid(nick)) + { + source.Reply(_("Bot Nicks may only contain valid nick characters.")); + return; + } + + if (!host.empty() && !isValidHost(host, 3)) + { + source.Reply(_("Bot Hosts may only contain valid host characters.")); + return; + } + + if (!user.empty()) + for (unsigned i = 0, end = user.length(); i < end && i < Config->UserLen; ++i) + if (!isalnum(user[i])) + { + source.Reply(_("Bot Idents may only contain valid characters."), Config->UserLen); + return; + } + + if (!nick.equals_ci(bi->nick) && findbot(nick)) + { + source.Reply(_("Bot \002%s\002 already exists."), nick.c_str()); + return; + } + + if (!nick.equals_ci(bi->nick)) + { + /* We check whether the nick is registered, and inform the user + * if so. You need to drop the nick manually before you can use + * it as a bot nick from now on -GD + */ + if (findnick(nick)) + { + source.Reply(NICK_ALREADY_REGISTERED, nick.c_str()); + return; + } + + /* The new nick is really different, so we remove the Q line for the old nick. */ + if (ircd->sqline) + { + XLine x(bi->nick); + ircdproto->SendSQLineDel(&x); + } + + /* Add a Q line for the new nick */ + XLine x(nick, "Reserved for services"); + ircdproto->SendSQLine(NULL, &x); + } + + if (!user.empty()) + ircdproto->SendQuit(bi, "Quit: Be right back"); + else + { + ircdproto->SendChangeBotNick(bi, nick); + } + + if (!nick.equals_cs(bi->nick)) + bi->SetNewNick(nick); + + if (!user.empty() && !user.equals_cs(bi->GetIdent())) + bi->SetIdent(user); + if (!host.empty() && !host.equals_cs(bi->host)) + bi->host = host; + if (!real.empty() && !real.equals_cs(bi->realname)) + bi->realname = real; + + if (!user.empty()) + { + ircdproto->SendClientIntroduction(bi, ircd->pseudoclient_mode); + bi->RejoinAll(); + } + + source.Reply(_("Bot \002%s\002 has been changed to %s!%s@%s (%s)"), oldnick.c_str(), bi->nick.c_str(), bi->GetIdent().c_str(), bi->host.c_str(), bi->realname.c_str()); + Log(LOG_ADMIN, source.u, this) << "CHANGE " << oldnick << " to " << bi->GetMask() << " " << bi->realname; + + FOREACH_MOD(I_OnBotChange, OnBotChange(bi)); + return; + } + + void DoDel(CommandSource &source, const std::vector<Anope::string> ¶ms) + { + const Anope::string &nick = params[1]; + BotInfo *bi; + + if (nick.empty()) + { + this->OnSyntaxError(source, "DEL"); + return; + } + + if (!(bi = findbot(nick))) + { + source.Reply(BOT_DOES_NOT_EXIST, nick.c_str()); + return; + } + + if (nickIsServices(nick, false)) + { + source.Reply(BOT_DOES_NOT_EXIST, nick.c_str()); + return; + } + + FOREACH_MOD(I_OnBotDelete, OnBotDelete(bi)); + + Log(LOG_ADMIN, source.u, this) << "DEL " << bi->nick; + + source.Reply(_("Bot \002%s\002 has been deleted."), nick.c_str()); + delete bi; + return; + } + public: + CommandBSBot(Module *creator) : Command(creator, "botserv/bot", 1, 6) + { + this->SetDesc(_("Maintains network bot list")); + this->SetSyntax(_("\002ADD \037nick\037 \037user\037 \037host\037 \037real\037\002")); + this->SetSyntax(_("\002CHANGE \037oldnick\037 \037newnick\037 [\037user\037 [\037host\037 [\037real\037]]]\002")); + this->SetSyntax(_("\002DEL \037nick\037\002")); + } + + void Execute(CommandSource &source, const std::vector<Anope::string> ¶ms) + { + const Anope::string &cmd = params[0]; + User *u = source.u; + + if (readonly) + { + source.Reply(_("Sorry, bot modification is temporarily disabled.")); + return; + } + + if (cmd.equals_ci("ADD")) + { + // ADD nick user host real - 5 + if (!u->HasCommand("botserv/bot/add")) + { + source.Reply(ACCESS_DENIED); + return; + } + + if (params.size() < 5) + { + this->OnSyntaxError(source, "ADD"); + return; + } + + std::vector<Anope::string> tempparams = params; + // ADD takes less params than CHANGE, so we need to take 6 if given and append it with a space to 5. + if (tempparams.size() >= 6) + tempparams[4] = tempparams[4] + " " + tempparams[5]; + + return this->DoAdd(source, tempparams); + } + else if (cmd.equals_ci("CHANGE")) + { + // CHANGE oldn newn user host real - 6 + // but only oldn and newn are required + if (!u->HasCommand("botserv/bot/change")) + { + source.Reply(ACCESS_DENIED); + return; + } + + if (params.size() < 3) + { + this->OnSyntaxError(source, "CHANGE"); + return; + } + + return this->DoChange(source, params); + } + else if (cmd.equals_ci("DEL")) + { + // DEL nick + if (!u->HasCommand("botserv/bot/del")) + { + source.Reply(ACCESS_DENIED); + return; + } + + if (params.size() < 1) + { + this->OnSyntaxError(source, "DEL"); + return; + } + + return this->DoDel(source, params); + } + else + this->OnSyntaxError(source, ""); + + return; + } + + bool OnHelp(CommandSource &source, const Anope::string &subcommand) + { + this->SendSyntax(source); + source.Reply(" "); + source.Reply(_("Allows Services Operators to create, modify, and delete\n" + "bots that users will be able to use on their own\n" + "channels.\n" + " \n" + "\002BOT ADD\002 adds a bot with the given nickname, username,\n" + "hostname and realname. Since no integrity checks are done \n" + "for these settings, be really careful.\n" + "\002BOT CHANGE\002 allows to change nickname, username, hostname\n" + "or realname of a bot without actually delete it (and all\n" + "the data associated with it).\n" + "\002BOT DEL\002 removes the given bot from the bot list. \n" + " \n" + "\002Note\002: you cannot create a bot that has a nick that is\n" + "currently registered. If an unregistered user is currently\n" + "using the nick, they will be killed.")); + return true; + } +}; + +class BSBot : public Module +{ + CommandBSBot commandbsbot; + + public: + BSBot(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, CORE), + commandbsbot(this) + { + this->SetAuthor("Anope"); + + ModuleManager::RegisterService(&commandbsbot); + } +}; + +MODULE_INIT(BSBot) |