chanserv.cpp

Go to the documentation of this file.
00001 /* ChanServ 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 ExpireCallback : public CallBack
00017 {
00018  public:
00019         ExpireCallback(Module *owner) : CallBack(owner, Config->ExpireTimeout, Anope::CurTime, true) { }
00020 
00021         void Tick(time_t) anope_override
00022         {
00023                 if (!Config->CSExpire || noexpire || readonly)
00024                         return;
00025 
00026                 for (registered_channel_map::const_iterator it = RegisteredChannelList->begin(), it_end = RegisteredChannelList->end(); it != it_end; )
00027                 {
00028                         ChannelInfo *ci = it->second;
00029                         ++it;
00030 
00031                         bool expire = false;
00032 
00033                         if (!ci->c && Config->CSExpire && Anope::CurTime - ci->last_used >= Config->CSExpire)
00034                                 expire = true;
00035 
00036                         if (ci->HasFlag(CI_NO_EXPIRE))
00037                                 expire = false;
00038 
00039                         FOREACH_MOD(I_OnPreChanExpire, OnPreChanExpire(ci, expire));
00040 
00041                         if (expire)
00042                         {
00043                                 Anope::string extra;
00044                                 if (ci->HasFlag(CI_SUSPENDED))
00045                                         extra = "suspended ";
00046 
00047                                 Log(LOG_NORMAL, "chanserv/expire") << "Expiring " << extra  << "channel " << ci->name << " (founder: " << (ci->GetFounder() ? ci->GetFounder()->display : "(none)") << ")";
00048                                 FOREACH_MOD(I_OnChanExpire, OnChanExpire(ci));
00049                                 ci->destroy();
00050                         }
00051                 }
00052         }
00053 };
00054 
00055 class ChanServCore : public Module
00056 {
00057         ExpireCallback expires;
00058 
00059  public:
00060         ChanServCore(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, CORE), expires(this)
00061         {
00062                 this->SetAuthor("Anope");
00063 
00064                 const BotInfo *ChanServ = findbot(Config->ChanServ);
00065                 if (ChanServ == NULL)
00066                         throw ModuleException("No bot named " + Config->ChanServ);
00067 
00068                 Implementation i[] = { I_OnBotPrivmsg, I_OnDelCore, I_OnPreHelp, I_OnPostHelp, I_OnCheckModes };
00069                 ModuleManager::Attach(i, this, sizeof(i) / sizeof(Implementation));
00070         }
00071 
00072         EventReturn OnBotPrivmsg(User *u, BotInfo *bi, Anope::string &message) anope_override
00073         {
00074                 if (Config->CSOpersOnly && !u->HasMode(UMODE_OPER) && bi->nick == Config->ChanServ)
00075                 {
00076                         u->SendMessage(bi, ACCESS_DENIED);
00077                         return EVENT_STOP;
00078                 }
00079 
00080                 return EVENT_CONTINUE;
00081         }
00082 
00083         void OnDelCore(NickCore *nc) anope_override
00084         {
00085                 // XXX this is slightly inefficient
00086                 for (registered_channel_map::const_iterator it = RegisteredChannelList->begin(), it_end = RegisteredChannelList->end(); it != it_end;)
00087                 {
00088                         ChannelInfo *ci = it->second;
00089                         ++it;
00090 
00091                         if (ci->GetFounder() == nc)
00092                         {
00093                                 NickCore *newowner = NULL;
00094                                 if (ci->successor && (ci->successor->IsServicesOper() || !Config->CSMaxReg || ci->successor->channelcount < Config->CSMaxReg))
00095                                         newowner = ci->successor;
00096                                 else
00097                                 {
00098                                         const ChanAccess *highest = NULL;
00099                                         for (unsigned j = 0; j < ci->GetAccessCount(); ++j)
00100                                         {
00101                                                 const ChanAccess *ca = ci->GetAccess(j);
00102                                                 const NickCore *anc = findcore(ca->mask);
00103 
00104                                                 if (!anc || (!anc->IsServicesOper() && Config->CSMaxReg && anc->channelcount >= Config->CSMaxReg) || (anc == nc))
00105                                                         continue;
00106                                                 if (!highest || *ca > *highest)
00107                                                         highest = ca;
00108                                         }
00109                                         if (highest)
00110                                                 newowner = findcore(highest->mask);
00111                                 }
00112 
00113                                 if (newowner)
00114                                 {
00115                                         Log(LOG_NORMAL, "chanserv/expire") << "Transferring foundership of " << ci->name << " from deleted nick " << nc->display << " to " << newowner->display;
00116                                         ci->SetFounder(newowner);
00117                                         ci->successor = NULL;
00118                                 }
00119                                 else
00120                                 {
00121                                         Log(LOG_NORMAL, "chanserv/expire") << "Deleting channel " << ci->name << " owned by deleted nick " << nc->display;
00122 
00123                                         ci->destroy();
00124                                         continue;
00125                                 }
00126                         }
00127 
00128                         if (ci->successor == nc)
00129                                 ci->successor = NULL;
00130 
00131                         for (unsigned j = 0; j < ci->GetAccessCount(); ++j)
00132                         {
00133                                 const ChanAccess *ca = ci->GetAccess(j);
00134                                 const NickCore *anc = findcore(ca->mask);
00135 
00136                                 if (anc && anc == nc)
00137                                 {
00138                                         ci->EraseAccess(j);
00139                                         break;
00140                                 }
00141                         }
00142 
00143                         for (unsigned j = ci->GetAkickCount(); j > 0; --j)
00144                         {
00145                                 const AutoKick *akick = ci->GetAkick(j - 1);
00146                                 if (akick->HasFlag(AK_ISNICK) && akick->nc == nc)
00147                                         ci->EraseAkick(j - 1);
00148                         }
00149                 }
00150         }
00151 
00152         EventReturn OnPreHelp(CommandSource &source, const std::vector<Anope::string> &params) anope_override
00153         {
00154                 if (!params.empty() || source.c || source.service->nick != Config->ChanServ)
00155                         return EVENT_CONTINUE;
00156                 source.Reply(_("\002%s\002 allows you to register and control various\n"
00157                         "aspects of channels. %s can often prevent\n"
00158                         "malicious users from \"taking over\" channels by limiting\n"
00159                         "who is allowed channel operator privileges. Available\n"
00160                         "commands are listed below; to use them, type\n"
00161                         "\002%s%s \037command\037\002. For more information on a\n"
00162                         "specific command, type \002%s%s HELP \037command\037\002.\n "),
00163                         Config->ChanServ.c_str(), Config->ChanServ.c_str(), Config->UseStrictPrivMsgString.c_str(), Config->ChanServ.c_str(), Config->UseStrictPrivMsgString.c_str(), Config->ChanServ.c_str(), Config->ChanServ.c_str(), source.command.c_str());
00164                 return EVENT_CONTINUE;
00165         }
00166 
00167         void OnPostHelp(CommandSource &source, const std::vector<Anope::string> &params) anope_override
00168         {
00169                 if (!params.empty() || source.c || source.service->nick != Config->ChanServ)
00170                         return;
00171                 if (Config->CSExpire >= 86400)
00172                         source.Reply(_(" \n"
00173                                 "Note that any channel which is not used for %d days\n"
00174                                 "(i.e. which no user on the channel's access list enters\n"
00175                                 "for that period of time) will be automatically dropped."), Config->CSExpire / 86400);
00176                 if (source.IsServicesOper())
00177                         source.Reply(_(" \n"
00178                                 "Services Operators can also, depending on their access drop\n"
00179                                 "any channel, view (and modify) the access, levels and akick\n"
00180                                 "lists and settings for any channel."));
00181         }
00182 
00183         EventReturn OnCheckModes(Channel *c) anope_override
00184         {
00185                 if (!Config->CSRequire.empty())
00186                 {
00187                         if (c->ci)
00188                                 c->SetModes(NULL, false, "+%s", Config->CSRequire.c_str());
00189                         else
00190                                 c->SetModes(NULL, false, "-%s", Config->CSRequire.c_str());
00191                 }
00192 
00193                 return EVENT_CONTINUE;
00194         }
00195 };
00196 
00197 MODULE_INIT(ChanServCore)
00198