]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/users.cpp
Clean up static allocations
[user/henk/code/inspircd.git] / src / users.cpp
index 93e7e89a6e456a8462a7551e0fe8c2e25cfc9032..729213e426f6754c42ddf2597a76d7385822e6e7 100644 (file)
@@ -11,8 +11,6 @@
  * ---------------------------------------------------
  */
 
-/* $Core */
-
 #include "inspircd.h"
 #include <stdarg.h>
 #include "socketengine.h"
 #include "bancache.h"
 #include "commands/cmd_whowas.h"
 
-/* XXX: Used for speeding up WriteCommon operations */
-unsigned long uniq_id = 1;
-
-static unsigned long* already_sent = NULL;
-
-LocalIntExt User::NICKForced("NICKForced", NULL);
-LocalStringExt User::OperQuit("OperQuit", NULL);
-
-void InitializeAlreadySent(SocketEngine* SE)
+typedef unsigned int uniq_id_t;
+class sent
 {
-       already_sent = new unsigned long[SE->GetMaxFds()];
-       memset(already_sent, 0, SE->GetMaxFds() * sizeof(unsigned long));
-}
+       uniq_id_t uniq_id;
+       uniq_id_t* array;
+       void init()
+       {
+               if (!array)
+                       array = new uniq_id_t[ServerInstance->SE->GetMaxFds()];
+               memset(array, 0, ServerInstance->SE->GetMaxFds() * sizeof(uniq_id_t));
+               uniq_id++;
+       }
+ public:
+       sent() : uniq_id(static_cast<uniq_id_t>(-1)), array(NULL) {}
+       inline uniq_id_t operator++()
+       {
+               if (++uniq_id == 0)
+                       init();
+               return uniq_id;
+       }
+       inline uniq_id_t& operator[](int i)
+       {
+               return array[i];
+       }
+       ~sent()
+       {
+               delete array;
+       }
+};
+
+static sent already_sent;
 
 std::string User::ProcessNoticeMasks(const char *sm)
 {
@@ -207,7 +223,7 @@ void User::DecrementModes()
 
 User::User(const std::string &uid)
 {
-       server = ServerInstance->FindServerNamePtr(ServerInstance->Config->ServerName);
+       server = ServerInstance->Config->ServerName;
        age = ServerInstance->Time();
        Penalty = 0;
        lastping = signon = idle_lastmsg = nping = registered = 0;
@@ -839,11 +855,11 @@ void User::FullConnect()
        if (this->CheckLines())
                return;
 
-       this->WriteServ("NOTICE Auth :Welcome to \002%s\002!",ServerInstance->Config->Network);
-       this->WriteNumeric(RPL_WELCOME, "%s :Welcome to the %s IRC Network %s!%s@%s",this->nick.c_str(), ServerInstance->Config->Network, this->nick.c_str(), this->ident.c_str(), this->host.c_str());
-       this->WriteNumeric(RPL_YOURHOSTIS, "%s :Your host is %s, running version InspIRCd-2.0",this->nick.c_str(),ServerInstance->Config->ServerName);
+       this->WriteServ("NOTICE Auth :Welcome to \002%s\002!",ServerInstance->Config->Network.c_str());
+       this->WriteNumeric(RPL_WELCOME, "%s :Welcome to the %s IRC Network %s!%s@%s",this->nick.c_str(), ServerInstance->Config->Network.c_str(), this->nick.c_str(), this->ident.c_str(), this->host.c_str());
+       this->WriteNumeric(RPL_YOURHOSTIS, "%s :Your host is %s, running version InspIRCd-2.0",this->nick.c_str(),ServerInstance->Config->ServerName.c_str());
        this->WriteNumeric(RPL_SERVERCREATED, "%s :This server was created %s %s", this->nick.c_str(), __TIME__, __DATE__);
-       this->WriteNumeric(RPL_SERVERVERSION, "%s %s InspIRCd-2.0 %s %s %s", this->nick.c_str(), ServerInstance->Config->ServerName, ServerInstance->Modes->UserModeList().c_str(), ServerInstance->Modes->ChannelModeList().c_str(), ServerInstance->Modes->ParaModeList().c_str());
+       this->WriteNumeric(RPL_SERVERVERSION, "%s %s InspIRCd-2.0 %s %s %s", this->nick.c_str(), ServerInstance->Config->ServerName.c_str(), ServerInstance->Modes->UserModeList().c_str(), ServerInstance->Modes->ChannelModeList().c_str(), ServerInstance->Modes->ParaModeList().c_str());
 
        ServerInstance->Config->Send005(this);
        this->WriteNumeric(RPL_YOURUUID, "%s %s :your unique ID", this->nick.c_str(), this->uuid.c_str());
@@ -915,9 +931,9 @@ bool User::ForceNickChange(const char* newnick)
 
        this->InvalidateCache();
 
-       NICKForced.set(this, 1);
+       ServerInstance->NICKForced.set(this, 1);
        FIRST_MOD_RESULT(OnUserPreNick, MOD_RESULT, (this, newnick));
-       NICKForced.set(this, 0);
+       ServerInstance->NICKForced.set(this, 0);
 
        if (MOD_RESULT == MOD_RES_DENY)
        {
@@ -931,9 +947,9 @@ bool User::ForceNickChange(const char* newnick)
        {
                std::vector<std::string> parameters;
                parameters.push_back(newnick);
-               NICKForced.set(this, 1);
+               ServerInstance->NICKForced.set(this, 1);
                bool result = (ServerInstance->Parser->CallHandler("NICK", parameters, this) == CMD_SUCCESS);
-               NICKForced.set(this, 0);
+               ServerInstance->NICKForced.set(this, 0);
                return result;
        }
 
@@ -1049,14 +1065,6 @@ const char* User::GetCIDRMask(int range)
        return ""; // unused, but oh well
 }
 
-std::string User::GetServerIP()
-{
-       int port;
-       std::string ip;
-       irc::sockets::satoap(&server_sa, ip, port);
-       return ip;
-}
-
 const char* User::GetIPString()
 {
        int port;
@@ -1118,7 +1126,7 @@ void User::Write(const char *text, ...)
 
 void User::WriteServ(const std::string& text)
 {
-       this->Write(":%s %s",ServerInstance->Config->ServerName,text.c_str());
+       this->Write(":%s %s",ServerInstance->Config->ServerName.c_str(),text.c_str());
 }
 
 /** WriteServ()
@@ -1159,7 +1167,7 @@ void User::WriteNumeric(unsigned int numeric, const std::string &text)
        if (MOD_RESULT == MOD_RES_DENY)
                return;
 
-       snprintf(textbuffer,MAXBUF,":%s %03u %s",ServerInstance->Config->ServerName, numeric, text.c_str());
+       snprintf(textbuffer,MAXBUF,":%s %03u %s",ServerInstance->Config->ServerName.c_str(), numeric, text.c_str());
        this->Write(std::string(textbuffer));
 }
 
@@ -1207,146 +1215,123 @@ void User::WriteTo(User *dest, const std::string &data)
        dest->WriteFrom(this, data);
 }
 
-
 void User::WriteCommon(const char* text, ...)
 {
        char textbuffer[MAXBUF];
        va_list argsPtr;
 
-       if (this->registered != REG_ALL)
+       if (this->registered != REG_ALL || quitting)
                return;
 
+       int len = snprintf(textbuffer,MAXBUF,":%s ",this->GetFullHost().c_str());
+
        va_start(argsPtr, text);
-       vsnprintf(textbuffer, MAXBUF, text, argsPtr);
+       vsnprintf(textbuffer + len, MAXBUF - len, text, argsPtr);
        va_end(argsPtr);
 
-       this->WriteCommon(std::string(textbuffer));
-}
-
-void User::WriteCommon(const std::string &text)
-{
-       bool sent_to_at_least_one = false;
-       char tb[MAXBUF];
-
-       if (this->registered != REG_ALL)
-               return;
-
-       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().c_str(),text.c_str());
-       std::string out = tb;
-
-       for (UCListIter v = this->chans.begin(); v != this->chans.end(); v++)
-       {
-               const UserMembList* ulist = (*v)->GetUsers();
-               for (UserMembList::const_iterator i = ulist->begin(); i != ulist->end(); i++)
-               {
-                       if ((IS_LOCAL(i->first)) && (already_sent[i->first->fd] != uniq_id))
-                       {
-                               already_sent[i->first->fd] = uniq_id;
-                               i->first->Write(out);
-                               sent_to_at_least_one = true;
-                       }
-               }
-       }
-
-       /*
-        * if the user was not in any channels, no users will receive the text. Make sure the user
-        * receives their OWN message for WriteCommon
-        */
-       if (!sent_to_at_least_one)
-       {
-               this->Write(std::string(tb));
-       }
+       this->WriteCommonRaw(std::string(textbuffer), true);
 }
 
-
-/* write a formatted string to all users who share at least one common
- * channel, NOT including the source user e.g. for use in QUIT
- */
-
 void User::WriteCommonExcept(const char* text, ...)
 {
        char textbuffer[MAXBUF];
        va_list argsPtr;
 
+       if (this->registered != REG_ALL || quitting)
+               return;
+
+       int len = snprintf(textbuffer,MAXBUF,":%s ",this->GetFullHost().c_str());
+
        va_start(argsPtr, text);
-       vsnprintf(textbuffer, MAXBUF, text, argsPtr);
+       vsnprintf(textbuffer + len, MAXBUF - len, text, argsPtr);
        va_end(argsPtr);
 
-       this->WriteCommonExcept(std::string(textbuffer));
+       this->WriteCommonRaw(std::string(textbuffer), false);
 }
 
-void User::WriteCommonQuit(const std::string &normal_text, const std::string &oper_text)
+void User::WriteCommonRaw(const std::string &line, bool include_self)
 {
-       char tb1[MAXBUF];
-       char tb2[MAXBUF];
-
-       if (this->registered != REG_ALL)
+       if (this->registered != REG_ALL || quitting)
                return;
 
-       uniq_id++;
+       uniq_id_t uniq_id = ++already_sent;
 
-       if (!already_sent)
-               InitializeAlreadySent(ServerInstance->SE);
+       UserChanList include_c(chans);
+       std::map<User*,bool> exceptions;
 
-       snprintf(tb1,MAXBUF,":%s QUIT :%s",this->GetFullHost().c_str(),normal_text.c_str());
-       snprintf(tb2,MAXBUF,":%s QUIT :%s",this->GetFullHost().c_str(),oper_text.c_str());
-       std::string out1 = tb1;
-       std::string out2 = tb2;
-       already_sent[this->fd] = uniq_id;
+       exceptions[this] = include_self;
 
-       for (UCListIter v = this->chans.begin(); v != this->chans.end(); v++)
+       FOREACH_MOD(I_OnBuildNeighborList,OnBuildNeighborList(this, include_c, exceptions));
+
+       for (std::map<User*,bool>::iterator i = exceptions.begin(); i != exceptions.end(); ++i)
        {
-               const UserMembList* ulist = (*v)->GetUsers();
+               User* u = i->first;
+               if (IS_LOCAL(u) && !u->quitting)
+               {
+                       already_sent[u->fd] = uniq_id;
+                       if (i->second)
+                               u->Write(line);
+               }
+       }
+       for (UCListIter v = include_c.begin(); v != include_c.end(); ++v)
+       {
+               Channel* c = *v;
+               const UserMembList* ulist = c->GetUsers();
                for (UserMembList::const_iterator i = ulist->begin(); i != ulist->end(); i++)
                {
                        User* u = i->first;
-                       if (IS_LOCAL(u) && !u->quitting && (already_sent[u->fd] != uniq_id))
+                       if (IS_LOCAL(u) && !u->quitting && already_sent[u->fd] != uniq_id)
                        {
                                already_sent[u->fd] = uniq_id;
-                               u->Write(IS_OPER(u) ? out2 : out1);
+                               u->Write(line);
                        }
                }
        }
 }
 
-void User::WriteCommonExcept(const std::string &text)
+void User::WriteCommonQuit(const std::string &normal_text, const std::string &oper_text)
 {
        char tb1[MAXBUF];
-       std::string out1;
+       char tb2[MAXBUF];
 
        if (this->registered != REG_ALL)
                return;
 
-       uniq_id++;
+       uniq_id_t uniq_id = ++already_sent;
+
+       snprintf(tb1,MAXBUF,":%s QUIT :%s",this->GetFullHost().c_str(),normal_text.c_str());
+       snprintf(tb2,MAXBUF,":%s QUIT :%s",this->GetFullHost().c_str(),oper_text.c_str());
+       std::string out1 = tb1;
+       std::string out2 = tb2;
 
-       if (!already_sent)
-               InitializeAlreadySent(ServerInstance->SE);
+       UserChanList include_c(chans);
+       std::map<User*,bool> exceptions;
 
-       snprintf(tb1,MAXBUF,":%s %s",this->GetFullHost().c_str(),text.c_str());
-       out1 = tb1;
+       FOREACH_MOD(I_OnBuildNeighborList,OnBuildNeighborList(this, include_c, exceptions));
 
-       for (UCListIter v = this->chans.begin(); v != this->chans.end(); v++)
+       for (std::map<User*,bool>::iterator i = exceptions.begin(); i != exceptions.end(); ++i)
+       {
+               User* u = i->first;
+               if (IS_LOCAL(u) && !u->quitting)
+               {
+                       already_sent[u->fd] = uniq_id;
+                       if (i->second)
+                               u->Write(IS_OPER(u) ? out2 : out1);
+               }
+       }
+       for (UCListIter v = include_c.begin(); v != include_c.end(); ++v)
        {
                const UserMembList* ulist = (*v)->GetUsers();
                for (UserMembList::const_iterator i = ulist->begin(); i != ulist->end(); i++)
                {
-                       if (this != i->first)
+                       User* u = i->first;
+                       if (IS_LOCAL(u) && !u->quitting && (already_sent[u->fd] != uniq_id))
                        {
-                               if ((IS_LOCAL(i->first)) && (already_sent[i->first->fd] != uniq_id))
-                               {
-                                       already_sent[i->first->fd] = uniq_id;
-                                       i->first->Write(out1);
-                               }
+                               already_sent[u->fd] = uniq_id;
+                               u->Write(IS_OPER(u) ? out2 : out1);
                        }
                }
        }
-
 }
 
 void User::WriteWallOps(const std::string &text)
@@ -1428,20 +1413,34 @@ void User::DoHostCycle(const std::string &quitline)
 {
        char buffer[MAXBUF];
 
-       ModResult result = MOD_RES_PASSTHRU;
-       FIRST_MOD_RESULT(OnHostCycle, result, (this));
-
-       if (result == MOD_RES_DENY)
-               return;
-       if (result == MOD_RES_PASSTHRU && !ServerInstance->Config->CycleHosts)
+       if (!ServerInstance->Config->CycleHosts)
                return;
 
-       uniq_id++;
+       uniq_id_t silent_id = ++already_sent;
+       uniq_id_t seen_id = ++already_sent;
+
+       UserChanList include_c(chans);
+       std::map<User*,bool> exceptions;
 
-       if (!already_sent)
-               InitializeAlreadySent(ServerInstance->SE);
+       FOREACH_MOD(I_OnBuildNeighborList,OnBuildNeighborList(this, include_c, exceptions));
 
-       for (UCListIter v = this->chans.begin(); v != this->chans.end(); v++)
+       for (std::map<User*,bool>::iterator i = exceptions.begin(); i != exceptions.end(); ++i)
+       {
+               User* u = i->first;
+               if (IS_LOCAL(u) && !u->quitting)
+               {
+                       if (i->second)
+                       {
+                               already_sent[u->fd] = seen_id;
+                               u->Write(quitline);
+                       }
+                       else
+                       {
+                               already_sent[u->fd] = silent_id;
+                       }
+               }
+       }
+       for (UCListIter v = include_c.begin(); v != include_c.end(); ++v)
        {
                Channel* c = *v;
                snprintf(buffer, MAXBUF, ":%s JOIN %s", GetFullHost().c_str(), c->name.c_str());
@@ -1459,11 +1458,13 @@ void User::DoHostCycle(const std::string &quitline)
                        User* u = i->first;
                        if (u == this || !IS_LOCAL(u))
                                continue;
+                       if (already_sent[u->fd] == silent_id)
+                               continue;
 
-                       if (already_sent[i->first->fd] != uniq_id)
+                       if (already_sent[u->fd] != seen_id)
                        {
                                u->Write(quitline);
-                               already_sent[i->first->fd] = uniq_id;
+                               already_sent[i->first->fd] = seen_id;
                        }
                        u->Write(joinline);
                        if (modeline.length() > 0)
@@ -1565,7 +1566,7 @@ void User::SplitChanList(User* dest, const std::string &cl)
 
        prefix << this->nick << " " << dest->nick << " :";
        line = prefix.str();
-       int namelen = strlen(ServerInstance->Config->ServerName) + 6;
+       int namelen = ServerInstance->Config->ServerName.length() + 6;
 
        for (start = 0; (pos = cl.find(' ', start)) != std::string::npos; start = pos+1)
        {
@@ -1746,7 +1747,7 @@ void User::ShowMOTD()
                this->WriteNumeric(ERR_NOMOTD, "%s :Message of the day file is missing.",this->nick.c_str());
                return;
        }
-       this->WriteNumeric(RPL_MOTDSTART, "%s :%s message of the day", this->nick.c_str(), ServerInstance->Config->ServerName);
+       this->WriteNumeric(RPL_MOTDSTART, "%s :%s message of the day", this->nick.c_str(), ServerInstance->Config->ServerName.c_str());
 
        for (file_cache::iterator i = ServerInstance->Config->MOTD.begin(); i != ServerInstance->Config->MOTD.end(); i++)
                this->WriteNumeric(RPL_MOTD, "%s :- %s",this->nick.c_str(),i->c_str());
@@ -1762,7 +1763,7 @@ void User::ShowRULES()
                return;
        }
 
-       this->WriteNumeric(RPL_RULESTART, "%s :- %s Server Rules -",this->nick.c_str(),ServerInstance->Config->ServerName);
+       this->WriteNumeric(RPL_RULESTART, "%s :- %s Server Rules -",this->nick.c_str(),ServerInstance->Config->ServerName.c_str());
 
        for (file_cache::iterator i = ServerInstance->Config->RULES.begin(); i != ServerInstance->Config->RULES.end(); i++)
                this->WriteNumeric(RPL_RULES, "%s :- %s",this->nick.c_str(),i->c_str());
@@ -1783,19 +1784,19 @@ void User::DecreasePenalty(int decrease)
 void FakeUser::SetFakeServer(std::string name)
 {
        this->nick = name;
-       this->server = nick.c_str();
+       this->server = name;
 }
 
 const std::string FakeUser::GetFullHost()
 {
-       if (*ServerInstance->Config->HideWhoisServer)
+       if (!ServerInstance->Config->HideWhoisServer.empty())
                return ServerInstance->Config->HideWhoisServer;
        return nick;
 }
 
 const std::string FakeUser::GetFullRealHost()
 {
-       if (*ServerInstance->Config->HideWhoisServer)
+       if (!ServerInstance->Config->HideWhoisServer.empty())
                return ServerInstance->Config->HideWhoisServer;
        return nick;
 }