]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_repeat.cpp
Implemented configurable kick message for the repeat module (#1835).
[user/henk/code/inspircd.git] / src / modules / m_repeat.cpp
index 75105ca0d5796718b5bb6738e5a3c52a63a93650..e6568732b68aa74d6cd32d21b92033bec17175a8 100644 (file)
@@ -1,6 +1,12 @@
 /*
  * InspIRCd -- Internet Relay Chat Daemon
  *
+ *   Copyright (C) 2019 Robby <robby@chatbelgie.be>
+ *   Copyright (C) 2018-2019 linuxdaemon <linuxdaemon.irc@gmail.com>
+ *   Copyright (C) 2018 Matt Schatz <genius3000@g3k.solutions>
+ *   Copyright (C) 2017-2019 Sadie Powell <sadie@witchery.services>
+ *   Copyright (C) 2015 James Lu <GLolol@overdrivenetworks.com>
+ *   Copyright (C) 2013-2015 Attila Molnar <attilamolnar@hush.com>
  *   Copyright (C) 2013 Daniel Vassdal <shutter@canternet.org>
  *
  * This file is part of InspIRCd.  InspIRCd is free software: you can
@@ -34,7 +40,7 @@ class ChannelSettings
        unsigned int Backlog;
        unsigned int Lines;
        unsigned int Diff;
-       unsigned int Seconds;
+       unsigned long Seconds;
 
        void serialize(std::string& out) const
        {
@@ -84,6 +90,7 @@ class RepeatMode : public ParamMode<RepeatMode, SimpleExtItem<ChannelSettings> >
                unsigned int MaxBacklog;
                unsigned int MaxDiff;
                unsigned int MaxMessageSize;
+               std::string KickMessage;
                ModuleSettings() : MaxLines(0), MaxSecs(0), MaxBacklog(0), MaxDiff() { }
        };
 
@@ -125,9 +132,10 @@ class RepeatMode : public ParamMode<RepeatMode, SimpleExtItem<ChannelSettings> >
                : ParamMode<RepeatMode, SimpleExtItem<ChannelSettings> >(Creator, "repeat", 'E')
                , MemberInfoExt("repeat_memb", ExtensionItem::EXT_MEMBERSHIP, Creator)
        {
+               syntax = "[~|*]<lines>:<sec>[:<difference>][:<backlog>]";
        }
 
-       void OnUnset(User* source, Channel* chan)
+       void OnUnset(User* source, Channel* chan) CXX11_OVERRIDE
        {
                // Unset the per-membership extension when the mode is removed
                const Channel::MemberMap& users = chan->GetUsers();
@@ -140,18 +148,19 @@ class RepeatMode : public ParamMode<RepeatMode, SimpleExtItem<ChannelSettings> >
                ChannelSettings settings;
                if (!ParseSettings(source, parameter, settings))
                {
-                       source->WriteNotice("*** Invalid syntax. Syntax is {[~*]}[lines]:[time]{:[difference]}{:[backlog]}");
+                       source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
                        return MODEACTION_DENY;
                }
 
                if ((settings.Backlog > 0) && (settings.Lines > settings.Backlog))
                {
-                       source->WriteNotice("*** You can't set needed lines higher than backlog");
+                       source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter,
+                               "You can't set lines higher than backlog."));
                        return MODEACTION_DENY;
                }
 
                LocalUser* localsource = IS_LOCAL(source);
-               if ((localsource) && (!ValidateSettings(localsource, settings)))
+               if ((localsource) && (!ValidateSettings(localsource, channel, parameter, settings)))
                        return MODEACTION_DENY;
 
                ext.set(channel, settings);
@@ -231,18 +240,20 @@ class RepeatMode : public ParamMode<RepeatMode, SimpleExtItem<ChannelSettings> >
        void ReadConfig()
        {
                ConfigTag* conf = ServerInstance->Config->ConfValue("repeat");
-               ms.MaxLines = conf->getInt("maxlines", 20);
-               ms.MaxBacklog = conf->getInt("maxbacklog", 20);
-               ms.MaxSecs = conf->getDuration("maxtime", conf->getInt("maxsecs", 0));
+               ms.MaxLines = conf->getUInt("maxlines", 20);
+               ms.MaxBacklog = conf->getUInt("maxbacklog", 20);
+               ms.MaxSecs = conf->getDuration("maxtime", conf->getDuration("maxsecs", 0));
 
-               ms.MaxDiff = conf->getInt("maxdistance", 50);
+               ms.MaxDiff = conf->getUInt("maxdistance", 50);
                if (ms.MaxDiff > 100)
                        ms.MaxDiff = 100;
 
-               unsigned int newsize = conf->getInt("size", 512);
+               unsigned int newsize = conf->getUInt("size", 512);
                if (newsize > ServerInstance->Config->Limits.MaxLine)
                        newsize = ServerInstance->Config->Limits.MaxLine;
                Resize(newsize);
+
+               ms.KickMessage = conf->getString("kickmessage", "Repeat flood");
        }
 
        std::string GetModuleSettings() const
@@ -250,6 +261,11 @@ class RepeatMode : public ParamMode<RepeatMode, SimpleExtItem<ChannelSettings> >
                return ConvToStr(ms.MaxLines) + ":" + ConvToStr(ms.MaxSecs) + ":" + ConvToStr(ms.MaxDiff) + ":" + ConvToStr(ms.MaxBacklog);
        }
 
+       std::string GetKickMessage() const
+       {
+               return ms.KickMessage;
+       }
+
        void SerializeParam(Channel* chan, const ChannelSettings* chset, std::string& out)
        {
                chset->serialize(out);
@@ -272,10 +288,10 @@ class RepeatMode : public ParamMode<RepeatMode, SimpleExtItem<ChannelSettings> >
                else
                        settings.Action = ChannelSettings::ACT_KICK;
 
-               if ((settings.Lines = ConvToInt(item)) == 0)
+               if ((settings.Lines = ConvToNum<unsigned int>(item)) == 0)
                        return false;
 
-               if ((!stream.GetToken(item)) || ((settings.Seconds = InspIRCd::Duration(item)) == 0))
+               if ((!stream.GetToken(item)) || !InspIRCd::Duration(item, settings.Seconds) || (settings.Seconds == 0))
                        // Required parameter missing
                        return false;
 
@@ -284,13 +300,13 @@ class RepeatMode : public ParamMode<RepeatMode, SimpleExtItem<ChannelSettings> >
                if (stream.GetToken(item))
                {
                        // There is a diff parameter, see if it's valid (> 0)
-                       if ((settings.Diff = ConvToInt(item)) == 0)
+                       if ((settings.Diff = ConvToNum<unsigned int>(item)) == 0)
                                return false;
 
                        if (stream.GetToken(item))
                        {
                                // There is a backlog parameter, see if it's valid
-                               if ((settings.Backlog = ConvToInt(item)) == 0)
+                               if ((settings.Backlog = ConvToNum<unsigned int>(item)) == 0)
                                        return false;
 
                                // If there are still tokens, then it's invalid because we allow only 4
@@ -302,36 +318,42 @@ class RepeatMode : public ParamMode<RepeatMode, SimpleExtItem<ChannelSettings> >
                return true;
        }
 
-       bool ValidateSettings(LocalUser* source, const ChannelSettings& settings)
+       bool ValidateSettings(LocalUser* source, Channel* channel, const std::string& parameter, const ChannelSettings& settings)
        {
-               if (settings.Backlog && !ms.MaxBacklog)
+               if (ms.MaxLines && settings.Lines > ms.MaxLines)
                {
-                       source->WriteNotice("*** The server administrator has disabled backlog matching");
+                       source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter, InspIRCd::Format(
+                               "The line number you specified is too big. Maximum allowed is %u.", ms.MaxLines)));
                        return false;
                }
 
-               if (settings.Diff)
+               if (ms.MaxSecs && settings.Seconds > ms.MaxSecs)
                {
-                       if (settings.Diff > ms.MaxDiff)
-                       {
-                               if (ms.MaxDiff == 0)
-                                       source->WriteNotice("*** The server administrator has disabled matching on edit distance");
-                               else
-                                       source->WriteNotice("*** The distance you specified is too great. Maximum allowed is " + ConvToStr(ms.MaxDiff));
-                               return false;
-                       }
+                       source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter, InspIRCd::Format(
+                               "The seconds you specified are too big. Maximum allowed is %u.", ms.MaxSecs)));
+                       return false;
+               }
 
-                       if (ms.MaxLines && settings.Lines > ms.MaxLines)
-                       {
-                               source->WriteNotice("*** The line number you specified is too great. Maximum allowed is " + ConvToStr(ms.MaxLines));
-                               return false;
-                       }
+               if (settings.Diff && settings.Diff > ms.MaxDiff)
+               {
+                       if (ms.MaxDiff == 0)
+                               source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter,
+                                       "The server administrator has disabled matching on edit distance."));
+                       else
+                               source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter, InspIRCd::Format(
+                                       "The distance you specified is too big. Maximum allowed is %u.", ms.MaxDiff)));
+                       return false;
+               }
 
-                       if (ms.MaxSecs && settings.Seconds > ms.MaxSecs)
-                       {
-                               source->WriteNotice("*** The seconds you specified is too great. Maximum allowed is " + ConvToStr(ms.MaxSecs));
-                               return false;
-                       }
+               if (settings.Backlog && settings.Backlog > ms.MaxBacklog)
+               {
+                       if (ms.MaxBacklog == 0)
+                               source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter,
+                                       "The server administrator has disabled backlog matching."));
+                       else
+                               source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter, InspIRCd::Format(
+                                       "The backlog you specified is too big. Maximum allowed is %u.", ms.MaxBacklog)));
+                       return false;
                }
 
                return true;
@@ -355,12 +377,12 @@ class RepeatModule : public Module
                rm.ReadConfig();
        }
 
-       ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList& exempt_list, MessageType msgtype) CXX11_OVERRIDE
+       ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
        {
-               if (target_type != TYPE_CHANNEL || !IS_LOCAL(user))
+               if (target.type != MessageTarget::TYPE_CHANNEL || !IS_LOCAL(user))
                        return MOD_RES_PASSTHRU;
 
-               Channel* chan = reinterpret_cast<Channel*>(dest);
+               Channel* chan = target.Get<Channel>();
                ChannelSettings* settings = rm.ext.get(chan);
                if (!settings)
                        return MOD_RES_PASSTHRU;
@@ -373,7 +395,7 @@ class RepeatModule : public Module
                if (res == MOD_RES_ALLOW)
                        return MOD_RES_PASSTHRU;
 
-               if (rm.MatchLine(memb, settings, text))
+               if (rm.MatchLine(memb, settings, details.text))
                {
                        if (settings->Action == ChannelSettings::ACT_BLOCK)
                        {
@@ -388,7 +410,7 @@ class RepeatModule : public Module
                                ServerInstance->Modes->Process(ServerInstance->FakeClient, chan, NULL, changelist);
                        }
 
-                       memb->chan->KickUser(ServerInstance->FakeClient, user, "Repeat flood");
+                       memb->chan->KickUser(ServerInstance->FakeClient, user, rm.GetKickMessage());
                        return MOD_RES_DENY;
                }
                return MOD_RES_PASSTHRU;
@@ -401,7 +423,7 @@ class RepeatModule : public Module
 
        Version GetVersion() CXX11_OVERRIDE
        {
-               return Version("Provides the +E channel mode - for blocking of similar messages", VF_COMMON|VF_VENDOR, rm.GetModuleSettings());
+               return Version("Adds channel mode E (repeat) which helps protect against spammers which spam the same message repeatedly.", VF_COMMON|VF_VENDOR, rm.GetModuleSettings());
        }
 };