diff options
author | attilamolnar <attilamolnar@hush.com> | 2013-07-20 20:30:18 +0200 |
---|---|---|
committer | attilamolnar <attilamolnar@hush.com> | 2013-07-24 18:10:20 +0200 |
commit | c265641c0a9a9b0a4686e1ea313876c16b4700ad (patch) | |
tree | 42c89bf0a397bd26d2c9e2870ff6dbebf562ea7e | |
parent | 909cba9b756179bfa16d985916db1084cb7b4a9d (diff) |
Rewrite SnomaskManager::SendMessage() and Flush(), split code into functions
-rw-r--r-- | include/snomasks.h | 47 | ||||
-rw-r--r-- | src/snomasks.cpp | 124 |
2 files changed, 99 insertions, 72 deletions
diff --git a/include/snomasks.h b/include/snomasks.h index c5d594eeb..db3989225 100644 --- a/include/snomasks.h +++ b/include/snomasks.h @@ -22,28 +22,50 @@ #pragma once +class SnomaskManager; class Snomask { - public: + /** Description of this snomask, e.g.: OPER, ANNOUNCEMENT, XLINE + */ std::string Description; + + /** Information about the last sent message, + * used for sending "last message repeated X times" messages + */ std::string LastMessage; - int Count; - bool LastBlocked; char LastLetter; + unsigned int Count; + /** Log and send a message to all opers who have the given snomask set + * @param letter The target users of this message + * @param desc The description of this snomask, will be prepended to the message + * @param msg The message to send + */ + static void Send(char letter, const std::string& desc, const std::string& msg); + + public: /** Create a new Snomask */ - Snomask() : Count(0), LastBlocked(false), LastLetter(0) - { - } + Snomask(); /** Sends a message to all opers with this snomask. + * @param message The message to send + * @param remote If true the message will go to the uppercase variant of this snomask */ - void SendMessage(const std::string &message, char letter); + void SendMessage(const std::string& message, char letter); /** Sends out the (last message repeated N times) message */ void Flush(); + + /** Returns the description of this snomask + * @param remote If true the description of the uppercase (remote) variant of this snomask + * will be returned (i.e.: "REMOTE" will be prepended to the description). + * @return The description of this snomask + */ + std::string GetDescription(char letter) const; + + friend class SnomaskManager; }; /** Snomask manager handles routing of SNOMASK (usermode +s) messages to opers. @@ -52,9 +74,9 @@ class Snomask */ class CoreExport SnomaskManager { - public: Snomask masks[26]; + public: /** Create a new SnomaskManager */ SnomaskManager(); @@ -94,7 +116,6 @@ class CoreExport SnomaskManager */ void WriteGlobalSno(char letter, const char* text, ...) CUSTOM_PRINTF(3, 4); - /** Called once per 5 seconds from the mainloop, this flushes any cached * snotices. The way the caching works is as follows: * Calls to WriteToSnoMask write to a cache, if the call is the same as it was @@ -104,4 +125,12 @@ class CoreExport SnomaskManager * is not particularly significant, in order to keep notices going out. */ void FlushSnotices(); + + /** Check whether a given character is an enabled (initialized) snomask. + * Valid snomask chars are lower- or uppercase letters and have a description. + * Snomasks are initialized with EnableSnomask(). + * @param ch The character to check + * @return True if the given char is allowed to be set via +s. + */ + bool IsSnomaskUsable(char ch) const; }; diff --git a/src/snomasks.cpp b/src/snomasks.cpp index 0a0d3cab2..fa8e5ab60 100644 --- a/src/snomasks.cpp +++ b/src/snomasks.cpp @@ -76,45 +76,35 @@ SnomaskManager::SnomaskManager() EnableSnomask('t',"STATS"); /* Local or remote stats request */ } -/*************************************************************************************/ +bool SnomaskManager::IsSnomaskUsable(char ch) const +{ + return ((isalpha(ch)) && (!masks[tolower(ch) - 'a'].Description.empty())); +} + +Snomask::Snomask() + : Count(0) +{ +} -void Snomask::SendMessage(const std::string &message, char mysnomask) +void Snomask::SendMessage(const std::string& message, char letter) { - if (ServerInstance->Config->NoSnoticeStack || message != LastMessage || mysnomask != LastLetter) + if ((!ServerInstance->Config->NoSnoticeStack) && (message == LastMessage) && (letter == LastLetter)) { - this->Flush(); - LastMessage = message; - LastLetter = mysnomask; - - std::string desc = Description; - if (desc.empty()) - desc = std::string("SNO-") + (char)tolower(mysnomask); - if (isupper(mysnomask)) - desc = "REMOTE" + desc; - ModResult MOD_RESULT; - ServerInstance->Logs->Log("snomask", LOG_DEFAULT, "%s: %s", desc.c_str(), message.c_str()); - - FIRST_MOD_RESULT(OnSendSnotice, MOD_RESULT, (mysnomask, desc, message)); - - LastBlocked = (MOD_RESULT == MOD_RES_DENY); - - if (!LastBlocked) - { - /* Only opers can receive snotices, so we iterate the oper list */ - std::list<User*>::iterator i = ServerInstance->Users->all_opers.begin(); - - while (i != ServerInstance->Users->all_opers.end()) - { - User* a = *i; - if (IS_LOCAL(a) && a->IsModeSet('s') && a->IsNoticeMaskSet(mysnomask) && !a->quitting) - { - a->WriteNotice("*** " + desc + ": " + message); - } - - i++; - } - } + Count++; + return; } + + this->Flush(); + + std::string desc = GetDescription(letter); + ModResult MOD_RESULT; + FIRST_MOD_RESULT(OnSendSnotice, MOD_RESULT, (letter, desc, message)); + if (MOD_RESULT == MOD_RES_DENY) + return; + + Snomask::Send(letter, desc, message); + LastMessage = message; + LastLetter = letter; Count++; } @@ -122,36 +112,44 @@ void Snomask::Flush() { if (Count > 1) { - std::string desc = Description; - if (desc.empty()) - desc = std::string("SNO-") + (char)tolower(LastLetter); - if (isupper(LastLetter)) - desc = "REMOTE" + desc; - std::string mesg = "(last message repeated "+ConvToStr(Count)+" times)"; - - ServerInstance->Logs->Log("snomask", LOG_DEFAULT, "%s: %s", desc.c_str(), mesg.c_str()); - - FOREACH_MOD(I_OnSendSnotice, OnSendSnotice(LastLetter, desc, mesg)); - - if (!LastBlocked) - { - /* Only opers can receive snotices, so we iterate the oper list */ - std::list<User*>::iterator i = ServerInstance->Users->all_opers.begin(); - - while (i != ServerInstance->Users->all_opers.end()) - { - User* a = *i; - if (IS_LOCAL(a) && a->IsModeSet('s') && a->IsNoticeMaskSet(LastLetter) && !a->quitting) - { - a->WriteNotice("*** " + desc + ": " + mesg); - } - - i++; - } - } + std::string desc = GetDescription(LastLetter); + std::string msg = "(last message repeated " + ConvToStr(Count) + " times)"; + FOREACH_MOD(I_OnSendSnotice, OnSendSnotice(LastLetter, desc, msg)); + Snomask::Send(LastLetter, desc, msg); } + LastMessage.clear(); - LastBlocked = false; Count = 0; } + +void Snomask::Send(char letter, const std::string& desc, const std::string& msg) +{ + std::string log = desc; + log.append(": ").append(msg); + ServerInstance->Logs->Log("snomask", LOG_DEFAULT, log); + + std::string finalmsg = "*** "; + finalmsg.append(log); + /* Only opers can receive snotices, so we iterate the oper list */ + const std::list<User*>& opers = ServerInstance->Users->all_opers; + for (std::list<User*>::const_iterator i = opers.begin(); i != opers.end(); ++i) + { + User* user = *i; + // IsNoticeMaskSet() returns false for opers who aren't +s, no need to check for it seperately + if (IS_LOCAL(user) && user->IsNoticeMaskSet(letter)) + user->WriteNotice(finalmsg); + } +} + +std::string Snomask::GetDescription(char letter) const +{ + std::string ret; + if (isupper(letter)) + ret = "REMOTE"; + if (!Description.empty()) + ret += Description; + else + ret += std::string("SNO-") + (char)tolower(letter); + return ret; +} |