]> git.netwichtig.de Git - user/henk/code/inspircd.git/commitdiff
Add a method for getting the name of a MessageTarget.
authorSadie Powell <sadie@witchery.services>
Thu, 23 Jan 2020 17:16:17 +0000 (17:16 +0000)
committerSadie Powell <sadie@witchery.services>
Thu, 23 Jan 2020 17:22:25 +0000 (17:22 +0000)
This fixes a minor bug in the filter module where the target would
be blank in messages when a server-targetted message matches a
filter.

include/message.h
src/modules/m_censor.cpp
src/modules/m_filter.cpp

index 39ddfaa406e6f36aab59a44fe1194c0bd4a39228..2e3cd9bb1c88c4ef5f819d2eb8837de2cc40e090 100644 (file)
@@ -154,4 +154,18 @@ class CoreExport MessageTarget
        {
                return static_cast<T*>(dest);
        }
+
+       /** Retrieves the name of the target of this message. */
+       const std::string& GetName() const
+       {
+               switch (type)
+               {
+                       case TYPE_CHANNEL:
+                               return Get<Channel>()->name;
+                       case TYPE_USER:
+                               return Get<User>()->nick;
+                       case TYPE_SERVER:
+                               return *Get<std::string>();
+               }
+       }
 };
index 436b9ae53a8329587bd07adf60940ed75f68af3b..0a83707e916b491648c3c73cace0a54bd3c5078e 100644 (file)
@@ -52,8 +52,6 @@ class ModuleCensor : public Module
                        return MOD_RES_PASSTHRU;
 
                int numeric = 0;
-               const char* targetname = NULL;
-
                switch (target.type)
                {
                        case MessageTarget::TYPE_USER:
@@ -63,7 +61,6 @@ class ModuleCensor : public Module
                                        return MOD_RES_PASSTHRU;
 
                                numeric = ERR_CANTSENDTOUSER;
-                               targetname = targuser->nick.c_str();
                                break;
                        }
 
@@ -78,7 +75,6 @@ class ModuleCensor : public Module
                                        return MOD_RES_PASSTHRU;
 
                                numeric = ERR_CANNOTSENDTOCHAN;
-                               targetname = targchan->name.c_str();
                                break;
                        }
 
@@ -93,7 +89,7 @@ class ModuleCensor : public Module
                        {
                                if (index->second.empty())
                                {
-                                       user->WriteNumeric(numeric, targetname, "Your message contained a censored word (" + index->first + "), and was blocked");
+                                       user->WriteNumeric(numeric, target.GetName(), "Your message contained a censored word (" + index->first + "), and was blocked");
                                        return MOD_RES_DENY;
                                }
 
index 136ea18c5c9abe013f2f0ae0da27ce1114008d43..4e297c1e68dc45eeca9951626b7e126e18d834de 100644 (file)
@@ -385,7 +385,6 @@ ModResult ModuleFilter::OnUserPreMessage(User* user, const MessageTarget& msgtar
        if (f)
        {
                bool is_selfmsg = false;
-               std::string target;
                switch (msgtarget.type)
                {
                        case MessageTarget::TYPE_USER:
@@ -397,8 +396,6 @@ ModResult ModuleFilter::OnUserPreMessage(User* user, const MessageTarget& msgtar
 
                                if (user == t)
                                        is_selfmsg = true;
-
-                               target = t->nick;
                                break;
                        }
                        case MessageTarget::TYPE_CHANNEL:
@@ -406,8 +403,6 @@ ModResult ModuleFilter::OnUserPreMessage(User* user, const MessageTarget& msgtar
                                Channel* t = msgtarget.Get<Channel>();
                                if (exemptedchans.count(t->name))
                                        return MOD_RES_PASSTHRU;
-
-                               target = t->name;
                                break;
                        }
                        case MessageTarget::TYPE_SERVER:
@@ -423,19 +418,19 @@ ModResult ModuleFilter::OnUserPreMessage(User* user, const MessageTarget& msgtar
                else if (f->action == FA_WARN)
                {
                        ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("WARNING: %s's message to %s matched %s (%s)",
-                               user->nick.c_str(), target.c_str(), f->freeform.c_str(), f->reason.c_str()));
+                               user->nick.c_str(), msgtarget.GetName().c_str(), f->freeform.c_str(), f->reason.c_str()));
                        return MOD_RES_PASSTHRU;
                }
                else if (f->action == FA_BLOCK)
                {
                        ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s had their message to %s filtered as it matched %s (%s)",
-                               user->nick.c_str(), target.c_str(), f->freeform.c_str(), f->reason.c_str()));
+                               user->nick.c_str(), msgtarget.GetName().c_str(), f->freeform.c_str(), f->reason.c_str()));
                        if (notifyuser)
                        {
                                if (msgtarget.type == MessageTarget::TYPE_CHANNEL)
-                                       user->WriteNumeric(ERR_CANNOTSENDTOCHAN, target, InspIRCd::Format("Message to channel blocked and opers notified (%s)", f->reason.c_str()));
+                                       user->WriteNumeric(ERR_CANNOTSENDTOCHAN, msgtarget.GetName(), InspIRCd::Format("Message to channel blocked and opers notified (%s)", f->reason.c_str()));
                                else
-                                       user->WriteNotice("Your message to "+target+" was blocked and opers notified: "+f->reason);
+                                       user->WriteNotice("Your message to "+msgtarget.GetName()+" was blocked and opers notified: "+f->reason);
                        }
                        else
                                details.echo_original = true;
@@ -445,9 +440,9 @@ ModResult ModuleFilter::OnUserPreMessage(User* user, const MessageTarget& msgtar
                        if (notifyuser)
                        {
                                if (msgtarget.type == MessageTarget::TYPE_CHANNEL)
-                                       user->WriteNumeric(ERR_CANNOTSENDTOCHAN, target, InspIRCd::Format("Message to channel blocked (%s)", f->reason.c_str()));
+                                       user->WriteNumeric(ERR_CANNOTSENDTOCHAN, msgtarget.GetName(), InspIRCd::Format("Message to channel blocked (%s)", f->reason.c_str()));
                                else
-                                       user->WriteNotice("Your message to "+target+" was blocked: "+f->reason);
+                                       user->WriteNotice("Your message to "+msgtarget.GetName()+" was blocked: "+f->reason);
                        }
                        else
                                details.echo_original = true;
@@ -455,7 +450,7 @@ ModResult ModuleFilter::OnUserPreMessage(User* user, const MessageTarget& msgtar
                else if (f->action == FA_KILL)
                {
                        ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s was killed because their message to %s matched %s (%s)",
-                               user->nick.c_str(), target.c_str(), f->freeform.c_str(), f->reason.c_str()));
+                               user->nick.c_str(), msgtarget.GetName().c_str(), f->freeform.c_str(), f->reason.c_str()));
                        ServerInstance->Users->QuitUser(user, "Filtered: " + f->reason);
                }
                else if (f->action == FA_SHUN && (ServerInstance->XLines->GetFactory("SHUN")))
@@ -464,7 +459,7 @@ ModResult ModuleFilter::OnUserPreMessage(User* user, const MessageTarget& msgtar
                        ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s (%s) was shunned for %s (expires on %s) because their message to %s matched %s (%s)",
                                user->nick.c_str(), sh->Displayable().c_str(), InspIRCd::DurationString(f->duration).c_str(),
                                InspIRCd::TimeString(ServerInstance->Time() + f->duration).c_str(),
-                               target.c_str(), f->freeform.c_str(), f->reason.c_str()));
+                               msgtarget.GetName().c_str(), f->freeform.c_str(), f->reason.c_str()));
                        if (ServerInstance->XLines->AddLine(sh, NULL))
                        {
                                ServerInstance->XLines->ApplyLines();
@@ -478,7 +473,7 @@ ModResult ModuleFilter::OnUserPreMessage(User* user, const MessageTarget& msgtar
                        ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s (%s) was G-lined for %s (expires on %s) because their message to %s matched %s (%s)",
                                user->nick.c_str(), gl->Displayable().c_str(), InspIRCd::DurationString(f->duration).c_str(),
                                InspIRCd::TimeString(ServerInstance->Time() + f->duration).c_str(),
-                               target.c_str(), f->freeform.c_str(), f->reason.c_str()));
+                               msgtarget.GetName().c_str(), f->freeform.c_str(), f->reason.c_str()));
                        if (ServerInstance->XLines->AddLine(gl,NULL))
                        {
                                ServerInstance->XLines->ApplyLines();
@@ -492,7 +487,7 @@ ModResult ModuleFilter::OnUserPreMessage(User* user, const MessageTarget& msgtar
                        ServerInstance->SNO->WriteGlobalSno('f', InspIRCd::Format("%s (%s) was Z-lined for %s (expires on %s) because their message to %s matched %s (%s)",
                                user->nick.c_str(), zl->Displayable().c_str(), InspIRCd::DurationString(f->duration).c_str(),
                                InspIRCd::TimeString(ServerInstance->Time() + f->duration).c_str(),
-                               target.c_str(), f->freeform.c_str(), f->reason.c_str()));
+                               msgtarget.GetName().c_str(), f->freeform.c_str(), f->reason.c_str()));
                        if (ServerInstance->XLines->AddLine(zl,NULL))
                        {
                                ServerInstance->XLines->ApplyLines();
@@ -501,7 +496,7 @@ ModResult ModuleFilter::OnUserPreMessage(User* user, const MessageTarget& msgtar
                                delete zl;
                }
 
-               ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, user->nick + " had their message filtered, target was " + target + ": " + f->reason + " Action: " + ModuleFilter::FilterActionToString(f->action));
+               ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, user->nick + " had their message filtered, target was " + msgtarget.GetName() + ": " + f->reason + " Action: " + ModuleFilter::FilterActionToString(f->action));
                return MOD_RES_DENY;
        }
        return MOD_RES_PASSTHRU;