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() --- include/users.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'include/users.h') diff --git a/include/users.h b/include/users.h index ceee4396b..6f319018f 100644 --- a/include/users.h +++ b/include/users.h @@ -248,6 +248,19 @@ class CoreExport User : public Extensible std::bitset modes; public: + /** To execute a function for each local neighbor of a user, inherit from this class and + * pass an instance of it to User::ForEachNeighbor(). + */ + class ForEachNeighborHandler + { + public: + /** Method to execute for each local neighbor of a user. + * Derived classes must implement this. + * @param user Current neighbor + */ + virtual void Execute(LocalUser* user) = 0; + }; + /** List of Memberships for this user */ typedef insp::intrusive_list ChanList; @@ -542,6 +555,16 @@ class CoreExport User : public Extensible */ 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 + * to different users depending on their oper status. + * @param handler Function object to call, inherited from ForEachNeighborHandler. + * @param include_self True to include this user in the set of neighbors, false otherwise. + * Modules may override this. Has no effect if this user is not local. + */ + void ForEachNeighbor(ForEachNeighborHandler& handler, bool include_self = true); + /** Dump text to a user target, splitting it appropriately to fit * @param linePrefix text to prefix each complete line with * @param textStream the text to send to the user -- cgit v1.2.3 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 'include/users.h') 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.2.3