]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_messageflood.cpp
Change m_redirect, m_joinflood and m_messageflood to put their modes in the correct...
[user/henk/code/inspircd.git] / src / modules / m_messageflood.cpp
index 980c78dff42da7783d1cf481daf8e7aa766aa25a..f2c55d584608ac825df9c66b8819a91f425434ee 100644 (file)
@@ -17,6 +17,7 @@
 using namespace std;
 
 #include <stdio.h>
+#include <map>
 #include "users.h"
 #include "channels.h"
 #include "modules.h"
@@ -26,13 +27,61 @@ using namespace std;
 
 class floodsettings
 {
+ public:
        bool ban;
        int secs;
        int lines;
+       time_t reset;
+       std::map<userrec*,int> counters;
 
        floodsettings() : ban(0), secs(0), lines(0) {};
-       floodsettings(bool a, int b, int c) : ban(a), secs(b), lines(c) {};
-}
+       floodsettings(bool a, int b, int c) : ban(a), secs(b), lines(c)
+       {
+               reset = time(NULL) + secs;
+               log(DEBUG,"Create new floodsettings: %lu %lu",time(NULL),reset);
+       };
+
+       void addmessage(userrec* who)
+       {
+               std::map<userrec*,int>::iterator iter = counters.find(who);
+               if (iter != counters.end())
+               {
+                       iter->second++;
+                       log(DEBUG,"Count for %s is now %d",who->nick,iter->second);
+               }
+               else
+               {
+                       counters[who] = 1;
+                       log(DEBUG,"Count for %s is now *1*",who->nick);
+               }
+               if (time(NULL) > reset)
+               {
+                       log(DEBUG,"floodsettings timer Resetting.");
+                       counters.clear();
+                       reset = time(NULL) + secs;
+               }
+       }
+
+       bool shouldkick(userrec* who)
+       {
+               std::map<userrec*,int>::iterator iter = counters.find(who);
+               if (iter != counters.end())
+               {
+                       log(DEBUG,"should kick? %d, %d",iter->second,this->lines);
+                       return (iter->second >= this->lines);
+               }
+               else return false;
+       }
+
+       void clear(userrec* who)
+       {
+               std::map<userrec*,int>::iterator iter = counters.find(who);
+               if (iter != counters.end())
+               {
+                       counters.erase(iter);
+               }
+       }
+};
 
 class ModuleMsgFlood : public Module
 {
@@ -55,15 +104,16 @@ class ModuleMsgFlood : public Module
                        {
                                std::string FloodParams = params[0];
                                chanrec* c = (chanrec*)target;
-                               char data[MAXBUF];
-                               strlcpy(data,FloodParams.c_str(),MAXBUF);
+                               char ndata[MAXBUF];
+                               char* data = ndata;
+                               strlcpy(ndata,FloodParams.c_str(),MAXBUF);
                                char* lines = data;
                                char* secs = NULL;
                                bool ban = false;
                                if (*data == '*')
                                {
                                        ban = true;
-                                       data++;
+                                       lines++;
                                }
                                else
                                {
@@ -109,6 +159,7 @@ class ModuleMsgFlood : public Module
                        }
                        else
                        {
+                               chanrec* c = (chanrec*)target;
                                if (c->GetExt("flood"))
                                {
                                        floodsettings *f = (floodsettings*)c->GetExt("flood");
@@ -121,40 +172,64 @@ class ModuleMsgFlood : public Module
                return 0;
        }
 
+       void ProcessMessages(userrec* user,chanrec* dest,std::string &text)
+       {
+               floodsettings *f = (floodsettings*)dest->GetExt("flood");
+               if (f)
+               {
+                       f->addmessage(user);
+                       if (f->shouldkick(user))
+                       {
+                               /* Youre outttta here! */
+                               f->clear(user);
+                               if (f->ban)
+                               {
+                                       char* parameters[3];
+                                       parameters[0] = dest->name;
+                                       parameters[1] = "+b";
+                                       parameters[2] = user->MakeWildHost();
+                                       Srv->SendMode(parameters,3,user);
+                               }
+                               Srv->KickUser(NULL, user, dest, "Channel flood triggered (mode +f)");
+                       }
+               }
+       }
+
+        virtual void OnUserMessage(userrec* user, void* dest, int target_type, std::string text)
+       {
+                if (target_type == TYPE_CHANNEL)
+                {
+                        ProcessMessages(user,(chanrec*)dest,text);
+                }
+       }
+
+       virtual void OnUserNotice(userrec* user, void* dest, int target_type, std::string text)
+       {
+               if (target_type == TYPE_CHANNEL)
+               {
+                       ProcessMessages(user,(chanrec*)dest,text);
+               }
+       }
+
        void OnChannelDelete(chanrec* chan)
        {
-               if (c->GetExt("flood"))
+               if (chan->GetExt("flood"))
                {
-                       floodsettings *f = (floodsettings*)c->GetExt("flood");
+                       floodsettings *f = (floodsettings*)chan->GetExt("flood");
                        delete f;
-                       c->Shrink("flood");
+                       chan->Shrink("flood");
                }
        }
 
        void Implements(char* List)
        {
-               List[I_On005Numeric] = List[I_OnExtendedMode] = List[I_OnChannelDelete] = 1;
+               List[I_On005Numeric] = List[I_OnExtendedMode] = List[I_OnChannelDelete] = List[I_OnUserNotice] = List[I_OnUserMessage] = 1;
        }
 
-        virtual void On005Numeric(std::string &output)
-        {
-                std::stringstream line(output);
-                std::string temp1, temp2;
-                while (!line.eof())
-                {
-                        line >> temp1;
-                        if (temp1.substr(0,10) == "CHANMODES=")
-                        {
-                               // By doing this we're *assuming* no other module has fucked up the CHANMODES=
-                               // section of the 005 numeric. If they have, we're going DOWN in a blaze of glory,
-                               // with a honking great EXCEPTION :)
-                               temp1.insert(temp1.find(",")+1,"L");
-                        }
-                        temp2 = temp2 + temp1 + " ";
-                }
-               if (temp2.length())
-                       output = temp2.substr(0,temp2.length()-1);
-        }
+       virtual void On005Numeric(std::string &output)
+       {
+               InsertMode(output, "f", 3);
+       }
 
        virtual ~ModuleMsgFlood()
        {