]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_nickflood.cpp
Fix the cloaking module on C++98 compilers.
[user/henk/code/inspircd.git] / src / modules / m_nickflood.cpp
index f74a1842206f9772817292e7554aaf4ca26185f2..e20ab1258113c19b50542f222fb5f01afd180c6a 100644 (file)
@@ -1,8 +1,12 @@
 /*
  * InspIRCd -- Internet Relay Chat Daemon
  *
+ *   Copyright (C) 2013, 2016-2019 Sadie Powell <sadie@witchery.services>
+ *   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
+ *   Copyright (C) 2012, 2014 Attila Molnar <attilamolnar@hush.com>
+ *   Copyright (C) 2010 Craig Edwards <brain@inspircd.org>
  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
- *   Copyright (C) 20072009 Robin Burchell <robin+git@viroteck.net>
+ *   Copyright (C) 2007-2009 Robin Burchell <robin+git@viroteck.net>
  *
  * This file is part of InspIRCd.  InspIRCd is free software: you can
  * redistribute it and/or modify it under the terms of the GNU General Public
 
 
 #include "inspircd.h"
+#include "modules/exemption.h"
+
+// The number of seconds nickname changing will be blocked for.
+static unsigned int duration;
 
 /** Holds settings and state associated with channel mode +F
  */
@@ -50,10 +58,6 @@ class nickfloodsettings
 
        bool shouldlock()
        {
-               /* XXX HACK: using counter + 1 here now to allow the counter to only be incremented
-                * on successful nick changes; this will be checked before the counter is
-                * incremented.
-                */
                return ((ServerInstance->Time() <= reset) && (counter == this->nicks));
        }
 
@@ -72,7 +76,7 @@ class nickfloodsettings
 
        void lock()
        {
-               unlocktime = ServerInstance->Time() + 60;
+               unlocktime = ServerInstance->Time() + duration;
        }
 };
 
@@ -84,24 +88,25 @@ class NickFlood : public ParamMode<NickFlood, SimpleExtItem<nickfloodsettings> >
        NickFlood(Module* Creator)
                : ParamMode<NickFlood, SimpleExtItem<nickfloodsettings> >(Creator, "nickflood", 'F')
        {
+               syntax = "<nick-changes>:<seconds>";
        }
 
-       ModeAction OnSet(User* source, Channel* channel, std::string& parameter)
+       ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
        {
                std::string::size_type colon = parameter.find(':');
                if ((colon == std::string::npos) || (parameter.find('-') != std::string::npos))
                {
-                       source->WriteNumeric(608, "%s :Invalid flood parameter",channel->name.c_str());
+                       source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
                        return MODEACTION_DENY;
                }
 
                /* Set up the flood parameters for this channel */
-               unsigned int nnicks = ConvToInt(parameter.substr(0, colon));
-               unsigned int nsecs = ConvToInt(parameter.substr(colon+1));
+               unsigned int nnicks = ConvToNum<unsigned int>(parameter.substr(0, colon));
+               unsigned int nsecs = ConvToNum<unsigned int>(parameter.substr(colon+1));
 
                if ((nnicks<1) || (nsecs<1))
                {
-                       source->WriteNumeric(608, "%s :Invalid flood parameter",channel->name.c_str());
+                       source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
                        return MODEACTION_DENY;
                }
 
@@ -118,17 +123,25 @@ class NickFlood : public ParamMode<NickFlood, SimpleExtItem<nickfloodsettings> >
 
 class ModuleNickFlood : public Module
 {
+       CheckExemption::EventProvider exemptionprov;
        NickFlood nf;
 
  public:
        ModuleNickFlood()
-               : nf(this)
+               : exemptionprov(this)
+               , nf(this)
+       {
+       }
+
+       void ReadConfig(ConfigStatus&) CXX11_OVERRIDE
        {
+               ConfigTag* tag = ServerInstance->Config->ConfValue("nickflood");
+               duration = tag->getDuration("duration", 60, 10, 600);
        }
 
-       ModResult OnUserPreNick(User* user, const std::string &newnick) CXX11_OVERRIDE
+       ModResult OnUserPreNick(LocalUser* user, const std::string& newnick) CXX11_OVERRIDE
        {
-               for (UCListIter i = user->chans.begin(); i != user->chans.end(); i++)
+               for (User::ChanList::iterator i = user->chans.begin(); i != user->chans.end(); i++)
                {
                        Channel* channel = (*i)->chan;
                        ModResult res;
@@ -136,13 +149,13 @@ class ModuleNickFlood : public Module
                        nickfloodsettings *f = nf.ext.get(channel);
                        if (f)
                        {
-                               res = ServerInstance->OnCheckExemption(user,channel,"nickflood");
+                               res = CheckExemption::Call(exemptionprov, user, channel, "nickflood");
                                if (res == MOD_RES_ALLOW)
                                        continue;
 
                                if (f->islocked())
                                {
-                                       user->WriteNumeric(ERR_CANTCHANGENICK, ":%s has been locked for nickchanges for 60 seconds because there have been more than %u nick changes in %u seconds", channel->name.c_str(), f->nicks, f->secs);
+                                       user->WriteNumeric(ERR_CANTCHANGENICK, InspIRCd::Format("%s has been locked for nickchanges for %u seconds because there have been more than %u nick changes in %u seconds", channel->name.c_str(), duration, f->nicks, f->secs));
                                        return MOD_RES_DENY;
                                }
 
@@ -150,7 +163,7 @@ class ModuleNickFlood : public Module
                                {
                                        f->clear();
                                        f->lock();
-                                       channel->WriteChannelWithServ((char*)ServerInstance->Config->ServerName.c_str(), "NOTICE %s :No nick changes are allowed for 60 seconds because there have been more than %u nick changes in %u seconds.", channel->name.c_str(), f->nicks, f->secs);
+                                       channel->WriteNotice(InspIRCd::Format("No nick changes are allowed for %u seconds because there have been more than %u nick changes in %u seconds.", duration, f->nicks, f->secs));
                                        return MOD_RES_DENY;
                                }
                        }
@@ -167,7 +180,7 @@ class ModuleNickFlood : public Module
                if (isdigit(user->nick[0])) /* allow switches to UID */
                        return;
 
-               for (UCListIter i = user->chans.begin(); i != user->chans.end(); ++i)
+               for (User::ChanList::iterator i = user->chans.begin(); i != user->chans.end(); ++i)
                {
                        Channel* channel = (*i)->chan;
                        ModResult res;
@@ -175,7 +188,7 @@ class ModuleNickFlood : public Module
                        nickfloodsettings *f = nf.ext.get(channel);
                        if (f)
                        {
-                               res = ServerInstance->OnCheckExemption(user,channel,"nickflood");
+                               res = CheckExemption::Call(exemptionprov, user, channel, "nickflood");
                                if (res == MOD_RES_ALLOW)
                                        return;
 
@@ -190,7 +203,7 @@ class ModuleNickFlood : public Module
 
        Version GetVersion() CXX11_OVERRIDE
        {
-               return Version("Channel mode F - nick flood protection", VF_VENDOR);
+               return Version("Adds channel mode F (nickflood) which helps protect against spammers which mass-change nicknames.", VF_VENDOR);
        }
 };