From 662dfa6c181a8c1d97a0c65499679e0eb1b399e2 Mon Sep 17 00:00:00 2001 From: Attila Molnar Date: Sat, 24 Jan 2015 14:49:10 +0100 Subject: Add User::ForEachNeighbor() --- src/users.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'src/users.cpp') diff --git a/src/users.cpp b/src/users.cpp index 34986a183..cb1bc901e 100644 --- a/src/users.cpp +++ b/src/users.cpp @@ -936,6 +936,59 @@ void User::WriteCommonQuit(const std::string &normal_text, const std::string &op } } +void User::ForEachNeighbor(ForEachNeighborHandler& handler, bool include_self) +{ + // 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. + + // 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 exceptions; + exceptions[this] = include_self; + FOREACH_MOD(OnBuildNeighborList, (this, include_chans, exceptions)); + + // Get next id, guaranteed to differ from the already_sent field of all users + const already_sent_t newid = ++LocalUser::already_sent_id; + + // Handle exceptions first + for (std::map::const_iterator i = exceptions.begin(); i != exceptions.end(); ++i) + { + LocalUser* curr = IS_LOCAL(i->first); + if (curr) + { + // 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); + } + } + + // Now consider the real neighbors + for (IncludeChanList::const_iterator i = include_chans.begin(); i != include_chans.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* curr = IS_LOCAL(j->first); + // User not yet visited? + if ((curr) && (curr->already_sent != newid)) + { + // Mark as visited and execute function + curr->already_sent = newid; + handler.Execute(curr); + } + } + } +} + void LocalUser::SendText(const std::string& line) { Write(line); -- cgit v1.3.1-10-gc9f91 From cf728bd8a3de0942c2294ca5b49745092565d87c Mon Sep 17 00:00:00 2001 From: Attila Molnar Date: Sat, 24 Jan 2015 14:53:03 +0100 Subject: Implement User::WriteCommonQuit() using ForEachNeighbor() in UserManager --- include/users.h | 7 ------- src/usermanager.cpp | 26 +++++++++++++++++++++++++- src/users.cpp | 40 ---------------------------------------- 3 files changed, 25 insertions(+), 48 deletions(-) (limited to 'src/users.cpp') diff --git a/include/users.h b/include/users.h index 6f319018f..fa8f610bc 100644 --- a/include/users.h +++ b/include/users.h @@ -548,13 +548,6 @@ class CoreExport User : public Extensible */ void WriteCommon(const char* text, ...) CUSTOM_PRINTF(2, 3); - /** Write a quit message to all common users, as in User::WriteCommonExcept but with a specific - * quit message for opers only. - * @param normal_text Normal user quit message - * @param oper_text Oper only quit message - */ - void WriteCommonQuit(const std::string &normal_text, const std::string &oper_text); - /** Execute a function once for each local neighbor of this user. By default, the neighbors of a user are the users * who have at least one common channel with the user. Modules are allowed to alter the set of neighbors freely. * This function is used for example to send something conditionally to neighbors, or to send different messages diff --git a/src/usermanager.cpp b/src/usermanager.cpp index 52cb4989f..5d07c4d79 100644 --- a/src/usermanager.cpp +++ b/src/usermanager.cpp @@ -24,6 +24,30 @@ #include "xline.h" #include "iohook.h" +namespace +{ + class WriteCommonQuit : public User::ForEachNeighborHandler + { + std::string line; + std::string operline; + + void Execute(LocalUser* user) CXX11_OVERRIDE + { + user->Write(user->IsOper() ? operline : line); + } + + public: + WriteCommonQuit(User* user, const std::string& msg, const std::string& opermsg) + : line(":" + user->GetFullHost() + " QUIT :") + , operline(line) + { + line += msg; + operline += opermsg; + user->ForEachNeighbor(*this, false); + } + }; +} + UserManager::UserManager() : unregistered_count(0) { @@ -180,7 +204,7 @@ void UserManager::QuitUser(User* user, const std::string& quitreason, const std: if (user->registered == REG_ALL) { FOREACH_MOD(OnUserQuit, (user, reason, *operreason)); - user->WriteCommonQuit(reason, *operreason); + WriteCommonQuit(user, reason, *operreason); } else unregistered_count--; diff --git a/src/users.cpp b/src/users.cpp index cb1bc901e..5be3963b4 100644 --- a/src/users.cpp +++ b/src/users.cpp @@ -896,46 +896,6 @@ void User::WriteCommonRaw(const std::string &line, bool include_self) } } -void User::WriteCommonQuit(const std::string &normal_text, const std::string &oper_text) -{ - if (this->registered != REG_ALL) - return; - - already_sent_t uniq_id = ++LocalUser::already_sent_id; - - 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 exceptions; - - FOREACH_MOD(OnBuildNeighborList, (this, include_c, exceptions)); - - for (std::map::iterator i = exceptions.begin(); i != exceptions.end(); ++i) - { - LocalUser* u = IS_LOCAL(i->first); - if (u && !u->quitting) - { - u->already_sent = uniq_id; - if (i->second) - u->Write(u->IsOper() ? operMessage : normalMessage); - } - } - for (IncludeChanList::const_iterator v = include_c.begin(); v != include_c.end(); ++v) - { - const Channel::MemberMap& ulist = (*v)->chan->GetUsers(); - for (Channel::MemberMap::const_iterator i = ulist.begin(); i != ulist.end(); i++) - { - LocalUser* u = IS_LOCAL(i->first); - if (u && (u->already_sent != uniq_id)) - { - u->already_sent = uniq_id; - u->Write(u->IsOper() ? operMessage : normalMessage); - } - } - } -} - void User::ForEachNeighbor(ForEachNeighborHandler& handler, bool include_self) { // The basic logic for visiting the neighbors of a user is to iterate the channel list of the user -- cgit v1.3.1-10-gc9f91 From 30bcf4894f1da0db8dde15329260c801f748a019 Mon Sep 17 00:00:00 2001 From: Attila Molnar Date: Sat, 24 Jan 2015 14:55:10 +0100 Subject: Implement User::WriteCommonRaw() using ForEachNeighbor() --- src/users.cpp | 54 +++++++++++++++++++++--------------------------------- 1 file changed, 21 insertions(+), 33 deletions(-) (limited to 'src/users.cpp') diff --git a/src/users.cpp b/src/users.cpp index 5be3963b4..4dffe6056 100644 --- a/src/users.cpp +++ b/src/users.cpp @@ -845,6 +845,25 @@ void User::WriteFrom(User *user, const char* text, ...) this->WriteFrom(user, textbuffer); } +namespace +{ + 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, ...) { if (this->registered != REG_ALL || quitting) @@ -861,39 +880,8 @@ 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 exceptions; - - exceptions[this] = include_self; - - FOREACH_MOD(OnBuildNeighborList, (this, include_c, exceptions)); - - for (std::map::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::ForEachNeighbor(ForEachNeighborHandler& handler, bool include_self) -- cgit v1.3.1-10-gc9f91 From 4d4306d8a91387baf642d6e4703f87246c688d2f Mon Sep 17 00:00:00 2001 From: Attila Molnar Date: Sat, 24 Jan 2015 15:02:25 +0100 Subject: Remove needless checks from User::WriteCommon()/WriteCommonRaw() --- src/users.cpp | 6 ------ 1 file changed, 6 deletions(-) (limited to 'src/users.cpp') diff --git a/src/users.cpp b/src/users.cpp index 4dffe6056..12243c64b 100644 --- a/src/users.cpp +++ b/src/users.cpp @@ -866,9 +866,6 @@ namespace void User::WriteCommon(const char* text, ...) { - if (this->registered != REG_ALL || quitting) - return; - std::string textbuffer; VAFORMAT(textbuffer, text, text); textbuffer = ":" + this->GetFullHost() + " " + textbuffer; @@ -877,9 +874,6 @@ void User::WriteCommon(const char* text, ...) void User::WriteCommonRaw(const std::string &line, bool include_self) { - if (this->registered != REG_ALL || quitting) - return; - WriteCommonRawHandler handler(line); ForEachNeighbor(handler, include_self); } -- cgit v1.3.1-10-gc9f91