botserv.cpp

Go to the documentation of this file.
00001 /* BotServ core functions
00002  *
00003  * (C) 2003-2012 Anope Team
00004  * Contact us at team@anope.org
00005  *
00006  * Please read COPYING and README for further details.
00007  *
00008  * Based on the original code of Epona by Lara.
00009  * Based on the original code of Services by Andy Church.
00010  */
00011 
00012 /*************************************************************************/
00013 
00014 #include "module.h"
00015 
00016 class BotServCore : public Module
00017 {
00018  public:
00019         BotServCore(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, CORE)
00020         {
00021                 this->SetAuthor("Anope");
00022 
00023                 const BotInfo *BotServ = findbot(Config->BotServ);
00024                 if (BotServ == NULL)
00025                         throw ModuleException("No bot named " + Config->BotServ);
00026 
00027                 Implementation i[] = { I_OnPrivmsg, I_OnJoinChannel, I_OnLeaveChannel,
00028                                         I_OnPreHelp, I_OnPostHelp, I_OnChannelModeSet };
00029                 ModuleManager::Attach(i, this, sizeof(i) / sizeof(Implementation));
00030 
00031         }
00032 
00033         void OnPrivmsg(User *u, Channel *c, Anope::string &msg) anope_override
00034         {
00035                 if (!u || !c || !c->ci || !c->ci->bi || msg.empty())
00036                         return;
00037 
00038                 /* Answer to ping if needed */
00039                 if (msg.substr(0, 6).equals_ci("\1PING ") && msg[msg.length() - 1] == '\1')
00040                 {
00041                         Anope::string ctcp = msg;
00042                         ctcp.erase(ctcp.begin());
00043                         ctcp.erase(ctcp.length() - 1);
00044                         ircdproto->SendCTCP(c->ci->bi, u->nick, "%s", ctcp.c_str());
00045                 }
00046 
00047                 Anope::string realbuf = msg;
00048 
00049                 if (realbuf.substr(0, 8).equals_ci("\1ACTION ") && realbuf[realbuf.length() - 1] == '\1')
00050                 {
00051                         realbuf.erase(0, 8);
00052                         realbuf.erase(realbuf.length() - 1);
00053                         return;
00054                 }
00055         
00056                 if (realbuf.empty() || !c->ci->botflags.HasFlag(BS_FANTASY))
00057                         return;
00058 
00059                 std::vector<Anope::string> params = BuildStringVector(realbuf);
00060 
00061                 if (!realbuf.find(c->ci->bi->nick))
00062                         params.erase(params.begin());
00063                 else if (Config->BSFantasyCharacter.empty())
00064                         ;
00065                 else if (!realbuf.find_first_of(Config->BSFantasyCharacter))
00066                         params[0].erase(params[0].begin());
00067                 else
00068                         return;
00069                 
00070                 if (params.empty())
00071                         return;
00072 
00073                 CommandInfo::map::const_iterator it = Config->Fantasy.end();
00074                 unsigned count = 0;
00075                 for (unsigned max = params.size(); it == Config->Fantasy.end() && max > 0; --max)
00076                 {
00077                         Anope::string full_command;
00078                         for (unsigned i = 0; i < max; ++i)
00079                                 full_command += " " + params[i];
00080                         full_command.erase(full_command.begin());
00081 
00082                         ++count;
00083                         it = Config->Fantasy.find(full_command);
00084                 }
00085 
00086                 if (it == Config->Fantasy.end())
00087                         return;
00088 
00089                 const CommandInfo &info = it->second;
00090                 service_reference<Command> cmd("Command", info.name);
00091                 if (!cmd)
00092                 {
00093                         Log(LOG_DEBUG) << "Fantasy command " << it->first << " exists for nonexistant service " << info.name << "!";
00094                         return;
00095                 }
00096 
00097                 for (unsigned i = 0, j = params.size() - (count - 1); i < j; ++i)
00098                         params.erase(params.begin());
00099 
00100                 while (cmd->MaxParams > 0 && params.size() > cmd->MaxParams)
00101                 {
00102                         params[cmd->MaxParams - 1] += " " + params[cmd->MaxParams];
00103                         params.erase(params.begin() + cmd->MaxParams);
00104                 }
00105 
00106                 /* All ChanServ commands take the channel as a first parameter */
00107                 if (cmd->name.find("chanserv/") == 0 && !cmd->HasFlag(CFLAG_STRIP_CHANNEL))
00108                         params.insert(params.begin(), c->ci->name);
00109 
00110                 // Command requires registered users only
00111                 if (!cmd->HasFlag(CFLAG_ALLOW_UNREGISTERED) && !u->Account())
00112                         return;
00113 
00114                 if (params.size() < cmd->MinParams)
00115                         return;
00116 
00117                 CommandSource source(u->nick, u, u->Account(), u, c->ci->bi);
00118                 source.c = c;
00119                 source.command = it->first;
00120                 source.permission = info.permission;
00121 
00122                 EventReturn MOD_RESULT;
00123                 if (c->ci->AccessFor(u).HasPriv("FANTASIA"))
00124                 {
00125                         FOREACH_RESULT(I_OnBotFantasy, OnBotFantasy(source, cmd, c->ci, params));
00126                 }
00127                 else
00128                 {
00129                         FOREACH_RESULT(I_OnBotNoFantasyAccess, OnBotNoFantasyAccess(source, cmd, c->ci, params));
00130                 }
00131 
00132                 if (MOD_RESULT == EVENT_STOP || !c->ci->AccessFor(u).HasPriv("FANTASIA"))
00133                         return;
00134 
00135                 if (MOD_RESULT != EVENT_ALLOW && !info.permission.empty() && !source.HasCommand(info.permission))
00136                         return;
00137 
00138                 FOREACH_RESULT(I_OnPreCommand, OnPreCommand(source, cmd, params));
00139                 if (MOD_RESULT == EVENT_STOP)
00140                         return;
00141 
00142                 dynamic_reference<User> user_reference(u);
00143                 dynamic_reference<NickCore> nc_reference(u->Account());
00144                 cmd->Execute(source, params);
00145 
00146                 if (user_reference && nc_reference)
00147                 {
00148                         FOREACH_MOD(I_OnPostCommand, OnPostCommand(source, cmd, params));
00149                 }
00150         }
00151 
00152         void OnJoinChannel(User *user, Channel *c) anope_override
00153         {
00154                 if (c->ci && c->ci->bi)
00155                 {
00162                         if (c->users.size() >= Config->BSMinUsers && !c->FindUser(c->ci->bi))
00163                                 c->ci->bi->Join(c, &Config->BotModeList);
00164                         /* Only display the greet if the main uplink we're connected
00165                          * to has synced, or we'll get greet-floods when the net
00166                          * recovers from a netsplit. -GD
00167                          */
00168                         if (c->FindUser(c->ci->bi) && c->ci->botflags.HasFlag(BS_GREET) && user->Account() && !user->Account()->greet.empty() && c->ci->AccessFor(user).HasPriv("GREET") && user->server->IsSynced())
00169                         {
00170                                 ircdproto->SendPrivmsg(c->ci->bi, c->name, "[%s] %s", user->Account()->display.c_str(), user->Account()->greet.c_str());
00171                                 c->ci->bi->lastmsg = Anope::CurTime;
00172                         }
00173                 }
00174         }
00175 
00176         void OnLeaveChannel(User *u, Channel *c) anope_override
00177         {
00178                 /* Channel is persistent, it shouldn't be deleted and the service bot should stay */
00179                 if (c->HasFlag(CH_PERSIST) || (c->ci && c->ci->HasFlag(CI_PERSIST)))
00180                         return;
00181         
00182                 /* Channel is syncing from a netburst, don't destroy it as more users are probably wanting to join immediatly
00183                  * We also don't part the bot here either, if necessary we will part it after the sync
00184                  */
00185                 if (c->HasFlag(CH_SYNCING))
00186                         return;
00187 
00188                 /* Additionally, do not delete this channel if ChanServ/a BotServ bot is inhabiting it */
00189                 if (c->HasFlag(CH_INHABIT))
00190                         return;
00191 
00192                 if (c->ci && c->ci->bi && u != *c->ci->bi && c->users.size() - 1 <= Config->BSMinUsers && c->FindUser(c->ci->bi))
00193                 {
00194                         bool persist = c->HasFlag(CH_PERSIST);
00195                         c->SetFlag(CH_PERSIST);
00196                         c->ci->bi->Part(c->ci->c);
00197                         if (!persist)
00198                                 c->UnsetFlag(CH_PERSIST);
00199                 }
00200         }
00201 
00202         EventReturn OnPreHelp(CommandSource &source, const std::vector<Anope::string> &params) anope_override
00203         {
00204                 if (!params.empty())
00205                         return EVENT_CONTINUE;
00206 
00207                 if (source.c)
00208                 {
00209                         source.Reply(_("\2%s\2 allows you to execute \"fantasy\" commands in the channel.\n"
00210                                         "Fantasy commands are tied to existing commands, usually on \2%s\2,\n"
00211                                         "and provide a more convenient way to execute commands. Commands that\n"
00212                                         "require a channel as a parameter will automatically have that parameter\n"
00213                                         "given.\n"), source.service->nick.c_str(), Config->ChanServ.c_str());
00214                         if (!Config->BSFantasyCharacter.empty())
00215                                 source.Reply(_(" \n"
00216                                                 "Fantasy commands may be prefixed with one of the following characters: %s\n"), Config->BSFantasyCharacter.c_str());
00217                         source.Reply(_(" \n"
00218                                         "Available commands are:"));
00219                 }
00220                 else if (source.service->nick == Config->BotServ)
00221                 {
00222                         source.Reply(_("\002%s\002 allows you to have a bot on your own channel.\n"
00223                                 "It has been created for users that can't host or\n"
00224                                 "configure a bot, or for use on networks that don't\n"
00225                                 "allow user bots. Available commands are listed \n"
00226                                 "below; to use them, type \002%s%s \037command\037\002. For\n"
00227                                 "more information on a specific command, type\n"
00228                                 "\002%s%s %s \037command\037\002.\n "),
00229                                 Config->BotServ.c_str(), Config->UseStrictPrivMsgString.c_str(), Config->BotServ.c_str(),
00230                                 Config->UseStrictPrivMsgString.c_str(), Config->BotServ.c_str(), source.command.c_str());
00231                 }
00232 
00233                 return EVENT_CONTINUE;
00234         }
00235 
00236         void OnPostHelp(CommandSource &source, const std::vector<Anope::string> &params) anope_override
00237         {
00238                 if (!params.empty() || source.c || source.service->nick != Config->BotServ)
00239                         return;
00240 
00241                 source.Reply(_(" \n"
00242                         "Bot will join a channel whenever there is at least\n"
00243                         "\002%d\002 user(s) on it."), Config->BSMinUsers);
00244                 if (!Config->BSFantasyCharacter.empty())
00245                         source.Reply(_("Additionally, all %s commands can be used if fantasy\n"
00246                                 "is enabled by prefixing the command name with one of\n"
00247                                 "the following characters: %s"), Config->ChanServ.c_str(), Config->BSFantasyCharacter.c_str());
00248         }
00249 
00250         EventReturn OnChannelModeSet(Channel *c, MessageSource &, ChannelModeName Name, const Anope::string &param) anope_override
00251         {
00252                 if (Config->BSSmartJoin && Name == CMODE_BAN && c->ci && c->ci->bi && c->FindUser(c->ci->bi))
00253                 {
00254                         BotInfo *bi = c->ci->bi;
00255 
00256                         Entry ban(CMODE_BAN, param);
00257                         if (ban.Matches(bi))
00258                                 c->RemoveMode(bi, CMODE_BAN, param);
00259                 }
00260 
00261                 return EVENT_CONTINUE;
00262         }
00263 };
00264 
00265 MODULE_INIT(BotServCore)
00266