]> git.netwichtig.de Git - user/henk/code/inspircd.git/commitdiff
Merge pull request #984 from Renegade334/modules-exempt-uline
authorAttila Molnar <attilamolnar@hush.com>
Thu, 5 Feb 2015 23:16:46 +0000 (00:16 +0100)
committerAttila Molnar <attilamolnar@hush.com>
Thu, 5 Feb 2015 23:16:46 +0000 (00:16 +0100)
Allow U-lined services to bypass m_silence (configurable)

include/numerics.h
include/users.h
src/modules/extra/m_mysql.cpp
src/modules/m_ircv3.cpp
src/modules/m_xline_db.cpp
src/usermanager.cpp
src/users.cpp
src/xline.cpp

index 2418730d27afd7abf630cf86a9b556c3b4d7d73e..0447df35308a3ab1a8202a6028926e974ea892f4 100644 (file)
@@ -144,6 +144,7 @@ enum Numerics
        ERR_NOTREGISTERED               = 451,
        ERR_NEEDMOREPARAMS              = 461,
        ERR_ALREADYREGISTERED           = 462,
+       ERR_YOUREBANNEDCREEP            = 465,
        ERR_UNKNOWNMODE                 = 472,
 
        /*
index ceee4396bece7ef01ffe2479dfc139a5a73ebe06..fa8f610bc915c9c4b57af2f56573bf6e13bbbc0d 100644 (file)
@@ -248,6 +248,19 @@ class CoreExport User : public Extensible
        std::bitset<ModeParser::MODEID_MAX> 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<Membership> ChanList;
@@ -535,12 +548,15 @@ 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
+       /** 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 WriteCommonQuit(const std::string &normal_text, const std::string &oper_text);
+       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
index 1cb3635bbe85a3c5f8fa5c9c359880da5406e11a..d8dda27a4ef6c91cb4d609f32e8ddfe97ea707ec 100644 (file)
@@ -255,6 +255,12 @@ class SQLConnection : public SQLProvider
                bool rv = mysql_real_connect(connection, host.c_str(), user.c_str(), pass.c_str(), dbname.c_str(), port, NULL, 0);
                if (!rv)
                        return rv;
+
+               // Enable character set settings
+               std::string charset = config->getString("charset");
+               if ((!charset.empty()) && (mysql_set_character_set(connection, charset.c_str())))
+                       ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "WARNING: Could not set character set to \"%s\"", charset.c_str());
+
                std::string initquery;
                if (config->readString("initialquery", initquery))
                {
index 4eb54d2a6f7af6413999b3968868ff23740a6d0a..b1c04cdf55359fce13df2fce2236f9a166b9818f 100644 (file)
 #include "modules/account.h"
 #include "modules/cap.h"
 
+class WriteNeighboursWithExt : public User::ForEachNeighborHandler
+{
+       const LocalIntExt& ext;
+       const std::string& msg;
+
+       void Execute(LocalUser* user) CXX11_OVERRIDE
+       {
+               if (ext.get(user))
+                       user->Write(msg);
+       }
+
+ public:
+       WriteNeighboursWithExt(User* user, const std::string& message, const LocalIntExt& extension)
+               : ext(extension)
+               , msg(message)
+       {
+               user->ForEachNeighbor(*this, false);
+       }
+};
+
 class ModuleIRCv3 : public Module
 {
        GenericCap cap_accountnotify;
@@ -31,44 +51,6 @@ class ModuleIRCv3 : public Module
 
        CUList last_excepts;
 
-       void WriteNeighboursWithExt(User* user, const std::string& line, const LocalIntExt& ext)
-       {
-               IncludeChanList chans(user->chans.begin(), user->chans.end());
-
-               std::map<User*, bool> exceptions;
-               FOREACH_MOD(OnBuildNeighborList, (user, chans, exceptions));
-
-               // Send it to all local users who were explicitly marked as neighbours by modules and have the required ext
-               for (std::map<User*, bool>::const_iterator i = exceptions.begin(); i != exceptions.end(); ++i)
-               {
-                       LocalUser* u = IS_LOCAL(i->first);
-                       if ((u) && (i->second) && (ext.get(u)))
-                               u->Write(line);
-               }
-
-               // Now consider sending it to all other users who has at least a common channel with the user
-               std::set<User*> already_sent;
-               for (IncludeChanList::const_iterator i = chans.begin(); i != chans.end(); ++i)
-               {
-                       const Channel::MemberMap& userlist = (*i)->chan->GetUsers();
-                       for (Channel::MemberMap::const_iterator m = userlist.begin(); m != userlist.end(); ++m)
-                       {
-                               /*
-                                * Send the line if the channel member in question meets all of the following criteria:
-                                * - local
-                                * - not the user who is doing the action (i.e. whose channels we're iterating)
-                                * - has the given extension
-                                * - not on the except list built by modules
-                                * - we haven't sent the line to the member yet
-                                *
-                                */
-                               LocalUser* member = IS_LOCAL(m->first);
-                               if ((member) && (member != user) && (ext.get(member)) && (exceptions.find(member) == exceptions.end()) && (already_sent.insert(member).second))
-                                       member->Write(line);
-                       }
-               }
-       }
-
  public:
        ModuleIRCv3() : cap_accountnotify(this, "account-notify"),
                                        cap_awaynotify(this, "away-notify"),
index ae8b209e35f01d8b0fbd14c2b1108725a5115342..c514ffb76e2230dfc919a915a0899205b15fdc8b 100644 (file)
@@ -120,7 +120,7 @@ class ModuleXLineDB : public Module
                                XLine* line = i->second;
                                stream << "LINE " << line->type << " " << line->Displayable() << " "
                                        << ServerInstance->Config->ServerName << " " << line->set_time << " "
-                                       << line->duration << " " << line->reason << std::endl;
+                                       << line->duration << " :" << line->reason << std::endl;
                        }
                }
 
index 52cb4989fbbaf199ff0eee1aa1718d27337e4aae..1966c9b4729f86193e8c8fa87694a3bf9395c936 100644 (file)
 #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)
 {
@@ -112,7 +136,7 @@ void UserManager::AddUser(int socket, ListenSocket* via, irc::sockets::sockaddrs
                        /* user banned */
                        ServerInstance->Logs->Log("BANCACHE", LOG_DEBUG, "BanCache: Positive hit for " + New->GetIPString());
                        if (!ServerInstance->Config->XLineMessage.empty())
-                               New->WriteNotice("*** " +  ServerInstance->Config->XLineMessage);
+                               New->WriteNumeric(ERR_YOUREBANNEDCREEP, ":" + ServerInstance->Config->XLineMessage);
                        this->QuitUser(New, b->Reason);
                        return;
                }
@@ -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--;
index 34986a1833073f4629fccff7b383871acc55185f..12243c64b074df57ab7442f4b5e8a782d79c5858 100644 (file)
@@ -845,11 +845,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;
@@ -858,79 +874,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;
+       // 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.
 
-       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<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 = ++LocalUser::already_sent_id;
 
-       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);
                        }
                }
        }
index 13124a39228ed4b63d06747e727a70c54f3194a6..dedf8c7a9c917a4c838ee31acce445d0eab3a57a 100644 (file)
@@ -531,7 +531,7 @@ void XLine::DefaultApply(User* u, const std::string &line, bool bancache)
        const std::string banReason = line + "-Lined: " + reason;
 
        if (!ServerInstance->Config->XLineMessage.empty())
-               u->WriteNotice("*** " + ServerInstance->Config->XLineMessage);
+               u->WriteNumeric(ERR_YOUREBANNEDCREEP, ":" + ServerInstance->Config->XLineMessage);
 
        if (ServerInstance->Config->HideBans)
                ServerInstance->Users->QuitUser(u, line + "-Lined", &banReason);