]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_chanprotect.cpp
kick_channel -> chanrec::KickUser(), server_kick_channel -> chanrec::ServerKickUser()
[user/henk/code/inspircd.git] / src / modules / m_chanprotect.cpp
index 40cd4f39a0296069b505d49c8075f56a239e96b9..a4bbeee14f16b0b138ccc0cb9a95cc229b7243a3 100644 (file)
@@ -26,16 +26,47 @@ const char* fakevalue = "on";
 class ChanFounder : public ModeHandler
 {
        Server* Srv;
+       char* dummyptr;
+
  public:
        ChanFounder(Server* s) : ModeHandler('q', 1, 1, true, MODETYPE_CHANNEL, false), Srv(s) { }
 
+       ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string &parameter)
+       {
+               userrec* x = Find(parameter);
+               if (x)
+               {
+                       if (!channel->HasUser(x))
+                       {
+                               return std::make_pair(false, parameter);
+                       }
+                       else
+                       {
+                               std::string founder = "cm_founder_"+std::string(channel->name);
+                               if (x->GetExt(founder,dummyptr))
+                               {
+                                       return std::make_pair(true, x->nick);
+                               }
+                               else
+                               {
+                                       return std::make_pair(false, parameter);
+                               }
+                       }
+               }
+               return std::make_pair(false, parameter);
+       }
+
+
        ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
        {
                userrec* theuser = Srv->FindNick(parameter);
 
+               log(DEBUG,"ChanFounder::OnModeChange");
+
                // cant find the user given as the parameter, eat the mode change.
                if (!theuser)
                {
+                       log(DEBUG,"No such user in ChanFounder");
                        parameter = "";
                        return MODEACTION_DENY;
                }
@@ -43,28 +74,39 @@ class ChanFounder : public ModeHandler
                // given user isnt even on the channel, eat the mode change
                if (!channel->HasUser(theuser))
                {
+                       log(DEBUG,"Channel doesn't have user in ChanFounder");
                        parameter = "";
                        return MODEACTION_DENY;
                }
 
+               std::string protect = "cm_protect_"+std::string(channel->name);
+               std::string founder = "cm_founder_"+std::string(channel->name);
+
                 // source is a server, or ulined, we'll let them +-q the user.
-               if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!*source->server))
+               if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!*source->server) || (!IS_LOCAL(source)))
                {
+                       log(DEBUG,"Allowing remote mode change in ChanFounder");
                        if (adding)
                        {
-                               if (!theuser->GetExt("cm_founder_"+std::string(channel->name)))
+                               if (!theuser->GetExt(founder,dummyptr))
                                {
-                                       theuser->Extend("cm_founder_"+std::string(channel->name),fakevalue);
+                                       log(DEBUG,"Does not have the ext item in ChanFounder");
+                                       if (!theuser->Extend(founder,fakevalue))
+                                               log(DEBUG,"COULD NOT EXTEND!!!");
                                        // Tidy the nickname (make case match etc)
                                        parameter = theuser->nick;
+                                       if (theuser->GetExt(founder, dummyptr))
+                                               log(DEBUG,"Extended!");
+                                       else
+                                               log(DEBUG,"Not extended :(");
                                        return MODEACTION_ALLOW;
                                }
                        }
                        else
                        {
-                               if (theuser->GetExt("cm_founder_"+std::string(channel->name)))
+                               if (theuser->GetExt(founder, dummyptr))
                                {
-                                       theuser->Shrink("cm_founder_"+std::string(channel->name));
+                                       theuser->Shrink(founder);
                                        // Tidy the nickname (make case match etc)
                                        parameter = theuser->nick;
                                        return MODEACTION_ALLOW;
@@ -84,9 +126,10 @@ class ChanFounder : public ModeHandler
        void DisplayList(userrec* user, chanrec* channel)
        {
                chanuserlist cl = Srv->GetUsers(channel);
+               std::string founder = "cm_founder_"+std::string(channel->name);
                for (unsigned int i = 0; i < cl.size(); i++)
                {
-                       if (cl[i]->GetExt("cm_founder_"+std::string(channel->name)))
+                       if (cl[i]->GetExt(founder, dummyptr))
                        {
                                WriteServ(user->fd,"386 %s %s %s",user->nick, channel->name,cl[i]->nick);
                        }
@@ -99,9 +142,35 @@ class ChanFounder : public ModeHandler
 class ChanProtect : public ModeHandler
 {
        Server* Srv;
+       char* dummyptr;
  public:
        ChanProtect(Server* s) : ModeHandler('a', 1, 1, true, MODETYPE_CHANNEL, false), Srv(s) { }
 
+        ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string &parameter)
+        {
+                userrec* x = Find(parameter);
+                if (x)
+                {
+                        if (!channel->HasUser(x))
+                        {
+                                return std::make_pair(false, parameter);
+                        }
+                        else
+                        {
+                                std::string founder = "cm_protect_"+std::string(channel->name);
+                                if (x->GetExt(founder,dummyptr))
+                                {
+                                        return std::make_pair(true, x->nick);
+                                }
+                                else
+                                {
+                                        return std::make_pair(false, parameter);
+                                }
+                        }
+                }
+                return std::make_pair(false, parameter);
+        }
+
        ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
        {
                userrec* theuser = Srv->FindNick(parameter);
@@ -114,20 +183,23 @@ class ChanProtect : public ModeHandler
                }
 
                // given user isnt even on the channel, eat the mode change
-               if (!chan->HasUser(theuser))
+               if (!channel->HasUser(theuser))
                {
                        parameter = "";
                        return MODEACTION_DENY;
                }
 
+               std::string protect = "cm_protect_"+std::string(channel->name);
+               std::string founder = "cm_founder_"+std::string(channel->name);
+
                // source has +q, is a server, or ulined, we'll let them +-a the user.
-               if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!*source->server) || (source->GetExt("cm_founder_"+std::string(channel->name))))
+               if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!*source->server) || (source->GetExt(founder,dummyptr)) || (!IS_LOCAL(source)))
                {
                        if (adding)
                        {
-                               if (!theuser->GetExt("cm_protect_"+std::string(channel->name)))
+                               if (!theuser->GetExt(protect,dummyptr))
                                {
-                                       theuser->Extend("cm_protect_"+std::string(channel->name),fakevalue);
+                                       theuser->Extend(protect,fakevalue);
                                        // Tidy the nickname (make case match etc)
                                        parameter = theuser->nick;
                                        return MODEACTION_ALLOW;
@@ -135,9 +207,9 @@ class ChanProtect : public ModeHandler
                        }
                        else
                        {
-                               if (theuser->GetExt("cm_protect_"+std::string(channel->name)))
+                               if (theuser->GetExt(protect,dummyptr))
                                {
-                                       theuser->Shrink("cm_protect_"+std::string(channel->name));
+                                       theuser->Shrink(protect);
                                        // Tidy the nickname (make case match etc)
                                        parameter = theuser->nick;
                                        return MODEACTION_ALLOW;
@@ -148,7 +220,7 @@ class ChanProtect : public ModeHandler
                else
                {
                        // bzzzt, wrong answer!
-                       WriteServ(user->fd,"482 %s %s :You are not a channel founder",user->nick, chan->name);
+                       WriteServ(source->fd,"482 %s %s :You are not a channel founder",source->nick, channel->name);
                        return MODEACTION_DENY;
                }
        }
@@ -156,9 +228,10 @@ class ChanProtect : public ModeHandler
        virtual void DisplayList(userrec* user, chanrec* channel)
        {
                chanuserlist cl = Srv->GetUsers(channel);
+               std::string protect = "cm_protect_"+std::string(channel->name);
                for (unsigned int i = 0; i < cl.size(); i++)
                {
-                       if (cl[i]->GetExt("cm_protect_"+std::string(channel->name)))
+                       if (cl[i]->GetExt(protect,dummyptr))
                        {
                                WriteServ(user->fd,"388 %s %s %s",user->nick, channel->name,cl[i]->nick);
                        }
@@ -172,25 +245,24 @@ class ModuleChanProtect : public Module
 {
        Server *Srv;
        bool FirstInGetsFounder;
-       ConfigReader *Conf;
        ChanProtect* cp;
        ChanFounder* cf;
+       char* dummyptr;
        
  public:
  
        ModuleChanProtect(Server* Me) : Module::Module(Me), Srv(Me)
        {       
                /* Initialise module variables */
-               Conf = new ConfigReader;
 
                cp = new ChanProtect(Me);
                cf = new ChanFounder(Me);
                
                Srv->AddMode(cp, 'a');
-               Srv->AdDMode(cf, 'q');
+               Srv->AddMode(cf, 'q');
                
-               // read our config options (main config file)
-               FirstInGetsFounder = Conf->ReadFlag("options","noservices",0);
+               /* Load config stuff */
+               OnRehash("");
        }
 
        void Implements(char* List)
@@ -219,11 +291,14 @@ class ModuleChanProtect : public Module
 
        virtual void OnRehash(const std::string &parameter)
        {
-               // on a rehash we delete our classes for good measure and create them again.
-               DELETE(Conf);
-               Conf = new ConfigReader;
-               // re-read our config options on a rehash
-               FirstInGetsFounder = Conf->ReadFlag("options","noservices",0);
+               /* Create a configreader class and read our flag,
+                * in old versions this was heap-allocated and the
+                * object was kept between rehashes...now we just
+                * stack-allocate it locally.
+                */
+               ConfigReader Conf;
+               
+               FirstInGetsFounder = Conf.ReadFlag("options","noservices",0);
        }
        
        virtual void OnUserJoin(userrec* user, chanrec* channel)
@@ -257,6 +332,7 @@ class ModuleChanProtect : public Module
                // etc of protected users. There are many types of access check, we're going to handle
                // a relatively small number of them relevent to our module using a switch statement.
        
+               log(DEBUG,"chanprotect OnAccessCheck %d",access_type);
                // don't allow action if:
                // (A) Theyre founder (no matter what)
                // (B) Theyre protected, and you're not
@@ -271,16 +347,25 @@ class ModuleChanProtect : public Module
                        return ACR_ALLOW;
                }
 
+               std::string founder = "cm_founder_"+std::string(channel->name);
+               std::string protect = "cm_protect_"+std::string(channel->name);
+
                switch (access_type)
                {
                        // a user has been deopped. Do we let them? hmmm...
                        case AC_DEOP:
-                               if (dest->GetExt("cm_founder_"+std::string(channel->name)))
+                               log(DEBUG,"OnAccessCheck AC_DEOP");
+                               if (dest->GetExt(founder,dummyptr))
                                {
+                                       log(DEBUG,"Has %s",founder.c_str());
                                        Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're a channel founder");
                                        return ACR_DENY;
                                }
-                               if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
+                               else
+                               {
+                                       log(DEBUG,"Doesnt have %s",founder.c_str());
+                               }
+                               if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
                                {
                                        Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're protected (+a)");
                                        return ACR_DENY;
@@ -289,12 +374,13 @@ class ModuleChanProtect : public Module
 
                        // a user is being kicked. do we chop off the end of the army boot?
                        case AC_KICK:
-                               if (dest->GetExt("cm_founder_"+std::string(channel->name)))
+                               log(DEBUG,"OnAccessCheck AC_KICK");
+                               if (dest->GetExt(founder,dummyptr))
                                {
                                        Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're a channel founder");
                                        return ACR_DENY;
                                }
-                               if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
+                               if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
                                {
                                        Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're protected (+a)");
                                        return ACR_DENY;
@@ -303,12 +389,12 @@ class ModuleChanProtect : public Module
 
                        // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
                        case AC_DEHALFOP:
-                               if (dest->GetExt("cm_founder_"+std::string(channel->name)))
+                               if (dest->GetExt(founder,dummyptr))
                                {
                                        Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as they're a channel founder");
                                        return ACR_DENY;
                                }
-                               if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
+                               if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
                                {
                                        Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as they're protected (+a)");
                                        return ACR_DENY;
@@ -317,12 +403,12 @@ class ModuleChanProtect : public Module
 
                        // same with devoice.
                        case AC_DEVOICE:
-                               if (dest->GetExt("cm_founder_"+std::string(channel->name)))
+                               if (dest->GetExt(founder,dummyptr))
                                {
                                        Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're a channel founder");
                                        return ACR_DENY;
                                }
-                               if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
+                               if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
                                {
                                        Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're protected (+a)");
                                        return ACR_DENY;
@@ -336,7 +422,6 @@ class ModuleChanProtect : public Module
        
        virtual ~ModuleChanProtect()
        {
-               DELETE(Conf);
                DELETE(cp);
                DELETE(cf);
        }
@@ -353,13 +438,15 @@ class ModuleChanProtect : public Module
                // know whos +q/+a on the channel.
                chanuserlist cl = Srv->GetUsers(chan);
                string_list commands;
+               std::string founder = "cm_founder_"+std::string(chan->name);
+               std::string protect = "cm_protect_"+std::string(chan->name);
                for (unsigned int i = 0; i < cl.size(); i++)
                {
-                       if (cl[i]->GetExt("cm_founder_"+std::string(chan->name)))
+                       if (cl[i]->GetExt(founder,dummyptr))
                        {
                                proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+q "+std::string(cl[i]->nick));
                        }
-                       if (cl[i]->GetExt("cm_protect_"+std::string(chan->name)))
+                       if (cl[i]->GetExt(protect,dummyptr))
                        {
                                proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+a "+std::string(cl[i]->nick));
                        }