]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/users.cpp
Fix bug #509 reported by Casey: bans were not applied on non-opped users (1.2 only)
[user/henk/code/inspircd.git] / src / users.cpp
index 5724e9eab44e27b0db30a9b3d1014f96b8c67628..09b96ce6bf14eebd0de3f0d43b5ee3bd1b8c64b0 100644 (file)
 #include "bancache.h"
 #include "commands/cmd_whowas.h"
 
-static unsigned long already_sent[MAX_DESCRIPTORS] = {0};
+static unsigned long* already_sent = NULL;
+
+
+void InitializeAlreadySent(SocketEngine* SE)
+{
+       already_sent = new unsigned long[SE->GetMaxFds()];
+}
 
 /* XXX: Used for speeding up WriteCommon operations */
 unsigned long uniq_id = 0;
@@ -193,6 +199,9 @@ User::User(InspIRCd* Instance, const std::string &uid) : ServerInstance(Instance
        Visibility = NULL;
        ip = NULL;
        MyClass = NULL;
+       io = NULL;
+       AllowedUserModes = NULL;
+       AllowedChanModes = NULL;
        AllowedOperCommands = NULL;
        chans.clear();
        invites.clear();
@@ -221,7 +230,7 @@ User::~User()
        if (this->MyClass)
        {
                this->MyClass->RefCount--;
-               ServerInstance->Logs->Log("USERS", DEBUG, "User destructor -- connect refcount now: %u", this->MyClass->RefCount);
+               ServerInstance->Logs->Log("USERS", DEBUG, "User destructor -- connect refcount now: %lu", this->MyClass->RefCount);
        }
        if (this->AllowedOperCommands)
        {
@@ -229,6 +238,18 @@ User::~User()
                AllowedOperCommands = NULL;
        }
 
+       if (this->AllowedUserModes)
+       {
+               delete[] AllowedUserModes;
+               AllowedUserModes = NULL;
+       }
+
+       if (this->AllowedChanModes)
+       {
+               delete[] AllowedChanModes;
+               AllowedChanModes = NULL;
+       }
+
        this->InvalidateCache();
        this->DecrementModes();
 
@@ -293,8 +314,11 @@ char* User::MakeHostIP()
 
 void User::CloseSocket()
 {
-       ServerInstance->SE->Shutdown(this, 2);
-       ServerInstance->SE->Close(this);
+       if (this->fd > -1)
+       {
+               ServerInstance->SE->Shutdown(this, 2);
+               ServerInstance->SE->Close(this);
+       }
 }
 
 char* User::GetFullHost()
@@ -437,6 +461,21 @@ void User::RemoveInvite(const irc::string &channel)
        }
 }
 
+bool User::HasModePermission(unsigned char mode, ModeType type)
+{
+       if (!IS_LOCAL(this))
+               return true;
+
+       if (!IS_OPER(this))
+               return false;
+
+       if (!AllowedUserModes || !AllowedChanModes)
+               return false;
+
+       return ((type == MODETYPE_USER ? AllowedUserModes : AllowedChanModes))[(mode - 'A')];
+       
+}
+
 bool User::HasPermission(const std::string &command)
 {
        /*
@@ -490,7 +529,7 @@ bool User::AddBuffer(std::string a)
                if (this->MyClass && (recvq.length() > this->MyClass->GetRecvqMax()))
                {
                        this->SetWriteError("RecvQ exceeded");
-                       ServerInstance->SNO->WriteToSnoMask('A', "User %s RecvQ of %d exceeds connect class maximum of %d",this->nick,recvq.length(),this->MyClass->GetRecvqMax());
+                       ServerInstance->SNO->WriteToSnoMask('A', "User %s RecvQ of %lu exceeds connect class maximum of %lu",this->nick,(unsigned long int)recvq.length(),this->MyClass->GetRecvqMax());
                        return false;
                }
 
@@ -567,7 +606,7 @@ void User::AddWriteBuf(const std::string &data)
                 * to repeatedly add the text to the sendq!
                 */
                this->SetWriteError("SendQ exceeded");
-               ServerInstance->SNO->WriteToSnoMask('A', "User %s SendQ of %d exceeds connect class maximum of %d",this->nick,sendq.length() + data.length(),this->MyClass->GetSendqMax());
+               ServerInstance->SNO->WriteToSnoMask('A', "User %s SendQ of %lu exceeds connect class maximum of %lu",this->nick,(unsigned long int)sendq.length() + data.length(),this->MyClass->GetSendqMax());
                return;
        }
 
@@ -669,6 +708,15 @@ void User::Oper(const std::string &opertype, const std::string &opername)
                        else
                                AllowedOperCommands = new std::map<std::string, bool>;
 
+                       if (!AllowedChanModes)
+                               AllowedChanModes = new bool[64];
+
+                       if (!AllowedUserModes)
+                               AllowedUserModes = new bool[64];
+
+                       memset(AllowedUserModes, 0, 64);
+                       memset(AllowedChanModes, 0, 64);
+
                        char* Classes = strdup(iter_opertype->second);
                        char* myclass = strtok_r(Classes," ",&savept);
                        while (myclass)
@@ -676,7 +724,7 @@ void User::Oper(const std::string &opertype, const std::string &opername)
                                operclass_t::iterator iter_operclass = ServerInstance->Config->operclass.find(myclass);
                                if (iter_operclass != ServerInstance->Config->operclass.end())
                                {
-                                       char* CommandList = strdup(iter_operclass->second);
+                                       char* CommandList = strdup(iter_operclass->second.commandlist);
                                        mycmd = strtok_r(CommandList," ",&savept2);
                                        while (mycmd)
                                        {
@@ -684,6 +732,29 @@ void User::Oper(const std::string &opertype, const std::string &opername)
                                                mycmd = strtok_r(NULL," ",&savept2);
                                        }
                                        free(CommandList);
+                                       this->AllowedUserModes['o' - 'A'] = true; // Call me paranoid if you want.
+                                       for (unsigned char* c = (unsigned char*)iter_operclass->second.umodelist; *c; ++c)
+                                       {
+                                               if (*c == '*')
+                                               {
+                                                       memset(this->AllowedUserModes, (int)(true), 64);
+                                               }
+                                               else
+                                               {
+                                                       this->AllowedUserModes[*c - 'A'] = true;
+                                               }
+                                       }
+                                       for (unsigned char* c = (unsigned char*)iter_operclass->second.cmodelist; *c; ++c)
+                                       {
+                                               if (*c == '*')
+                                               {
+                                                       memset(this->AllowedChanModes, (int)(true), 64);
+                                               }
+                                               else
+                                               {
+                                                       this->AllowedChanModes[*c - 'A'] = true;
+                                               }
+                                       }
                                }
                                myclass = strtok_r(NULL," ",&savept);
                        }
@@ -731,6 +802,17 @@ void User::UnOper()
                        delete AllowedOperCommands;
                        AllowedOperCommands = NULL;
                }
+               if (AllowedUserModes)
+               {
+                       delete[] AllowedUserModes;
+                       AllowedUserModes = NULL;
+               }
+               if (AllowedChanModes)
+               {
+                       delete[] AllowedChanModes;
+                       AllowedChanModes = NULL;
+               }
+
        }
 }
 
@@ -834,12 +916,11 @@ void User::FullConnect()
 
        this->WriteServ("NOTICE Auth :Welcome to \002%s\002!",ServerInstance->Config->Network);
        this->WriteNumeric(001, "%s :Welcome to the %s IRC Network %s!%s@%s",this->nick, ServerInstance->Config->Network, this->nick, this->ident, this->host);
-       this->WriteNumeric(002, "%s :Your host is %s, running version %s",this->nick,ServerInstance->Config->ServerName,VERSION);
+       this->WriteNumeric(002, "%s :Your host is %s, running version InspIRCd-1.2",this->nick,ServerInstance->Config->ServerName);
        this->WriteNumeric(003, "%s :This server was created %s %s", this->nick, __TIME__, __DATE__);
-       this->WriteNumeric(004, "%s %s %s %s %s %s", this->nick, ServerInstance->Config->ServerName, VERSION, ServerInstance->Modes->UserModeList().c_str(), ServerInstance->Modes->ChannelModeList().c_str(), ServerInstance->Modes->ParaModeList().c_str());
+       this->WriteNumeric(004, "%s %s InspIRCd-1.2 %s %s %s", this->nick, ServerInstance->Config->ServerName, ServerInstance->Modes->UserModeList().c_str(), ServerInstance->Modes->ChannelModeList().c_str(), ServerInstance->Modes->ParaModeList().c_str());
 
        ServerInstance->Config->Send005(this);
-
        this->WriteNumeric(42, "%s %s :your unique ID", this->nick, this->uuid);
 
 
@@ -1093,14 +1174,14 @@ void User::Write(std::string text)
                return;
        }
 
-       if (ServerInstance->Config->GetIOHook(this->GetPort()))
+       if (this->io)
        {
                /* XXX: The lack of buffering here is NOT a bug, modules implementing this interface have to
                 * implement their own buffering mechanisms
                 */
                try
                {
-                       ServerInstance->Config->GetIOHook(this->GetPort())->OnRawSocketWrite(this->fd, text.data(), text.length());
+                       this->io->OnRawSocketWrite(this->fd, text.data(), text.length());
                }
                catch (CoreException& modexcept)
                {
@@ -1249,6 +1330,9 @@ void User::WriteCommon(const std::string &text)
 
        uniq_id++;
 
+       if (!already_sent)
+               InitializeAlreadySent(ServerInstance->SE);
+
        /* We dont want to be doing this n times, just once */
        snprintf(tb,MAXBUF,":%s %s",this->GetFullHost(),text.c_str());
        std::string out = tb;
@@ -1303,6 +1387,10 @@ void User::WriteCommonQuit(const std::string &normal_text, const std::string &op
                return;
 
        uniq_id++;
+
+       if (!already_sent)
+               InitializeAlreadySent(ServerInstance->SE);
+
        snprintf(tb1,MAXBUF,":%s QUIT :%s",this->GetFullHost(),normal_text.c_str());
        snprintf(tb2,MAXBUF,":%s QUIT :%s",this->GetFullHost(),oper_text.c_str());
        std::string out1 = tb1;
@@ -1334,6 +1422,10 @@ void User::WriteCommonExcept(const std::string &text)
                return;
 
        uniq_id++;
+
+       if (!already_sent)
+               InitializeAlreadySent(ServerInstance->SE);
+
        snprintf(tb1,MAXBUF,":%s %s",this->GetFullHost(),text.c_str());
        out1 = tb1;
 
@@ -1357,7 +1449,7 @@ void User::WriteCommonExcept(const std::string &text)
 
 void User::WriteWallOps(const std::string &text)
 {
-       if (!IS_OPER(this) && IS_LOCAL(this))
+       if (!IS_LOCAL(this))
                return;
 
        std::string wallop("WALLOPS :");
@@ -1373,6 +1465,9 @@ void User::WriteWallOps(const std::string &text)
 
 void User::WriteWallOps(const char* text, ...)
 {
+       if (!IS_LOCAL(this))
+               return;
+
        char textbuffer[MAXBUF];
        va_list argsPtr;
 
@@ -1635,7 +1730,7 @@ ConnectClass* User::SetClass(const std::string &explicit_name)
                /* deny change if change will take class over the limit */
                if (found->limit && (found->RefCount + 1 >= found->limit))
                {
-                       ServerInstance->Logs->Log("USERS", DEBUG, "OOPS: Connect class limit (%u) hit, denying", found->limit);
+                       ServerInstance->Logs->Log("USERS", DEBUG, "OOPS: Connect class limit (%lu) hit, denying", found->limit);
                        return this->MyClass;
                }
 
@@ -1645,12 +1740,12 @@ ConnectClass* User::SetClass(const std::string &explicit_name)
                        if (found == this->MyClass) // no point changing this shit :P
                                return this->MyClass;
                        this->MyClass->RefCount--;
-                       ServerInstance->Logs->Log("USERS", DEBUG, "Untying user from connect class -- refcount: %u", this->MyClass->RefCount);
+                       ServerInstance->Logs->Log("USERS", DEBUG, "Untying user from connect class -- refcount: %lu", this->MyClass->RefCount);
                }
 
                this->MyClass = found;
                this->MyClass->RefCount++;
-               ServerInstance->Logs->Log("USERS", DEBUG, "User tied to new class -- connect refcount now: %u", this->MyClass->RefCount);
+               ServerInstance->Logs->Log("USERS", DEBUG, "User tied to new class -- connect refcount now: %lu", this->MyClass->RefCount);
        }
 
        return this->MyClass;