]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/users.cpp
Add UserManager::NextAlreadySentId() and convert all code to use it
[user/henk/code/inspircd.git] / src / users.cpp
index 8d22d166ced3500ccfdc7c57ebe367bb5c9cabbb..2585443126c8f85a40cc5617d23679e2a3c72eb9 100644 (file)
@@ -97,8 +97,6 @@ LocalUser::LocalUser(int myfd, irc::sockets::sockaddrs* client, irc::sockets::so
 
 User::~User()
 {
-       if (ServerInstance->FindUUID(uuid))
-               ServerInstance->Logs->Log("USERS", LOG_DEFAULT, "User destructor for %s called without cull", uuid.c_str());
 }
 
 const std::string& User::MakeHost()
@@ -141,28 +139,6 @@ const std::string& User::GetFullRealHost()
        return this->cached_fullrealhost;
 }
 
-InviteList& LocalUser::GetInviteList()
-{
-       RemoveExpiredInvites();
-       return invites;
-}
-
-bool LocalUser::RemoveInvite(Channel* chan)
-{
-       Invitation* inv = Invitation::Find(chan, this);
-       if (inv)
-       {
-               delete inv;
-               return true;
-       }
-       return false;
-}
-
-void LocalUser::RemoveExpiredInvites()
-{
-       Invitation::Find(NULL, this);
-}
-
 bool User::HasModePermission(unsigned char, ModeType)
 {
        return true;
@@ -279,7 +255,7 @@ void UserIOHandler::OnDataReady()
                return;
 eol_found:
                // just found a newline. Terminate the string, and pull it out of recvq
-               recvq = recvq.substr(qpos);
+               recvq.erase(0, qpos);
 
                // TODO should this be moved to when it was inserted in recvq?
                ServerInstance->stats.Recv += qpos;
@@ -330,7 +306,6 @@ CullResult User::cull()
 
 CullResult LocalUser::cull()
 {
-       ClearInvites();
        eh.cull();
        return User::cull();
 }
@@ -340,7 +315,7 @@ CullResult FakeUser::cull()
        // Fake users don't quit, they just get culled.
        quitting = true;
        // Fake users are not inserted into UserManager::clientlist, they're only in the uuidlist
-       ServerInstance->Users->uuidlist.erase(uuid);
+       // and they are removed from there by the linking mod when the server splits
        return User::cull();
 }
 
@@ -451,21 +426,16 @@ void User::UnOper()
 
 
        /* Remove all oper only modes from the user when the deoper - Bug #466*/
-       std::string moderemove("-");
-
-       for (unsigned char letter = 'A'; letter <= 'z'; letter++)
+       Modes::ChangeList changelist;
+       const ModeParser::ModeHandlerMap& usermodes = ServerInstance->Modes->GetModes(MODETYPE_USER);
+       for (ModeParser::ModeHandlerMap::const_iterator i = usermodes.begin(); i != usermodes.end(); ++i)
        {
-               ModeHandler* mh = ServerInstance->Modes->FindMode(letter, MODETYPE_USER);
-               if (mh && mh->NeedsOper())
-                       moderemove += letter;
+               ModeHandler* mh = i->second;
+               if (mh->NeedsOper())
+                       changelist.push_remove(mh);
        }
 
-
-       std::vector<std::string> parameters;
-       parameters.push_back(this->nick);
-       parameters.push_back(moderemove);
-
-       ServerInstance->Modes->Process(parameters, this);
+       ServerInstance->Modes->Process(this, NULL, this, changelist);
 
        // Remove the user from the oper list
        stdalgo::vector::swaperase(ServerInstance->Users->all_opers, this);
@@ -771,7 +741,7 @@ void LocalUser::Write(const std::string& text)
        if (text.length() > ServerInstance->Config->Limits.MaxLine - 2)
        {
                // this should happen rarely or never. Crop the string at 512 and try again.
-               std::string try_again = text.substr(0, ServerInstance->Config->Limits.MaxLine - 2);
+               std::string try_again(text, 0, ServerInstance->Config->Limits.MaxLine - 2);
                Write(try_again);
                return;
        }
@@ -852,11 +822,27 @@ void User::WriteFrom(User *user, const char* text, ...)
        this->WriteFrom(user, textbuffer);
 }
 
-void User::WriteCommon(const char* text, ...)
+namespace
 {
-       if (this->registered != REG_ALL || quitting)
-               return;
+       class WriteCommonRawHandler : public User::ForEachNeighborHandler
+       {
+               const std::string& msg;
 
+               void Execute(LocalUser* user) CXX11_OVERRIDE
+               {
+                       user->Write(msg);
+               }
+
+        public:
+               WriteCommonRawHandler(const std::string& message)
+                       : msg(message)
+               {
+               }
+       };
+}
+
+void User::WriteCommon(const char* text, ...)
+{
        std::string textbuffer;
        VAFORMAT(textbuffer, text, text);
        textbuffer = ":" + this->GetFullHost() + " " + textbuffer;
@@ -865,79 +851,58 @@ void User::WriteCommon(const char* text, ...)
 
 void User::WriteCommonRaw(const std::string &line, bool include_self)
 {
-       if (this->registered != REG_ALL || quitting)
-               return;
-
-       LocalUser::already_sent_id++;
-
-       IncludeChanList include_c(chans.begin(), chans.end());
-       std::map<User*,bool> exceptions;
-
-       exceptions[this] = include_self;
-
-       FOREACH_MOD(OnBuildNeighborList, (this, include_c, exceptions));
-
-       for (std::map<User*,bool>::iterator i = exceptions.begin(); i != exceptions.end(); ++i)
-       {
-               LocalUser* u = IS_LOCAL(i->first);
-               if (u && !u->quitting)
-               {
-                       u->already_sent = LocalUser::already_sent_id;
-                       if (i->second)
-                               u->Write(line);
-               }
-       }
-       for (IncludeChanList::const_iterator v = include_c.begin(); v != include_c.end(); ++v)
-       {
-               Channel* c = (*v)->chan;
-               const Channel::MemberMap& ulist = c->GetUsers();
-               for (Channel::MemberMap::const_iterator i = ulist.begin(); i != ulist.end(); ++i)
-               {
-                       LocalUser* u = IS_LOCAL(i->first);
-                       if (u && u->already_sent != LocalUser::already_sent_id)
-                       {
-                               u->already_sent = LocalUser::already_sent_id;
-                               u->Write(line);
-                       }
-               }
-       }
+       WriteCommonRawHandler handler(line);
+       ForEachNeighbor(handler, include_self);
 }
 
-void User::WriteCommonQuit(const std::string &normal_text, const std::string &oper_text)
+void User::ForEachNeighbor(ForEachNeighborHandler& handler, bool include_self)
 {
-       if (this->registered != REG_ALL)
-               return;
-
-       already_sent_t uniq_id = ++LocalUser::already_sent_id;
+       // The basic logic for visiting the neighbors of a user is to iterate the channel list of the user
+       // and visit all users on those channels. Because two users may share more than one common channel,
+       // we must skip users that we have already visited.
+       // To do this, we make use of a global counter and an integral 'already_sent' field in LocalUser.
+       // The global counter is incremented every time we do something for each neighbor of a user. Then,
+       // before visiting a member we examine user->already_sent. If it's equal to the current counter, we
+       // skip the member. Otherwise, we set it to the current counter and visit the member.
 
-       const std::string normalMessage = ":" + this->GetFullHost() + " QUIT :" + normal_text;
-       const std::string operMessage = ":" + this->GetFullHost() + " QUIT :" + oper_text;
-
-       IncludeChanList include_c(chans.begin(), chans.end());
-       std::map<User*,bool> exceptions;
+       // Ask modules to build a list of exceptions.
+       // Mods may also exclude entire channels by erasing them from include_chans.
+       IncludeChanList include_chans(chans.begin(), chans.end());
+       std::map<User*, bool> exceptions;
+       exceptions[this] = include_self;
+       FOREACH_MOD(OnBuildNeighborList, (this, include_chans, exceptions));
 
-       FOREACH_MOD(OnBuildNeighborList, (this, include_c, exceptions));
+       // Get next id, guaranteed to differ from the already_sent field of all users
+       const already_sent_t newid = ServerInstance->Users.NextAlreadySentId();
 
-       for (std::map<User*,bool>::iterator i = exceptions.begin(); i != exceptions.end(); ++i)
+       // Handle exceptions first
+       for (std::map<User*, bool>::const_iterator i = exceptions.begin(); i != exceptions.end(); ++i)
        {
-               LocalUser* u = IS_LOCAL(i->first);
-               if (u && !u->quitting)
+               LocalUser* curr = IS_LOCAL(i->first);
+               if (curr)
                {
-                       u->already_sent = uniq_id;
-                       if (i->second)
-                               u->Write(u->IsOper() ? operMessage : normalMessage);
+                       // Mark as visited to ensure we won't visit again if there is a common channel
+                       curr->already_sent = newid;
+                       // Always treat quitting users as excluded
+                       if ((i->second) && (!curr->quitting))
+                               handler.Execute(curr);
                }
        }
-       for (IncludeChanList::const_iterator v = include_c.begin(); v != include_c.end(); ++v)
+
+       // Now consider the real neighbors
+       for (IncludeChanList::const_iterator i = include_chans.begin(); i != include_chans.end(); ++i)
        {
-               const Channel::MemberMap& ulist = (*v)->chan->GetUsers();
-               for (Channel::MemberMap::const_iterator i = ulist.begin(); i != ulist.end(); i++)
+               Channel* chan = (*i)->chan;
+               const Channel::MemberMap& userlist = chan->GetUsers();
+               for (Channel::MemberMap::const_iterator j = userlist.begin(); j != userlist.end(); ++j)
                {
-                       LocalUser* u = IS_LOCAL(i->first);
-                       if (u && (u->already_sent != uniq_id))
+                       LocalUser* curr = IS_LOCAL(j->first);
+                       // User not yet visited?
+                       if ((curr) && (curr->already_sent != newid))
                        {
-                               u->already_sent = uniq_id;
-                               u->Write(u->IsOper() ? operMessage : normalMessage);
+                               // Mark as visited and execute function
+                               curr->already_sent = newid;
+                               handler.Execute(curr);
                        }
                }
        }