1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
24 #include "helperfuncs.h"
26 /* $ModDesc: Provides channel mode +f (message flood protection) */
35 std::map<userrec*,int> counters;
37 floodsettings() : ban(0), secs(0), lines(0) {};
38 floodsettings(bool a, int b, int c) : ban(a), secs(b), lines(c)
40 reset = time(NULL) + secs;
41 log(DEBUG,"Create new floodsettings: %lu %lu",time(NULL),reset);
44 void addmessage(userrec* who)
46 std::map<userrec*,int>::iterator iter = counters.find(who);
47 if (iter != counters.end())
50 log(DEBUG,"Count for %s is now %d",who->nick,iter->second);
55 log(DEBUG,"Count for %s is now *1*",who->nick);
57 if (time(NULL) > reset)
59 log(DEBUG,"floodsettings timer Resetting.");
61 reset = time(NULL) + secs;
65 bool shouldkick(userrec* who)
67 std::map<userrec*,int>::iterator iter = counters.find(who);
68 if (iter != counters.end())
70 log(DEBUG,"should kick? %d, %d",iter->second,this->lines);
71 return (iter->second >= this->lines);
76 void clear(userrec* who)
78 std::map<userrec*,int>::iterator iter = counters.find(who);
79 if (iter != counters.end())
86 class ModuleMsgFlood : public Module
92 ModuleMsgFlood(Server* Me)
96 Srv->AddExtendedMode('f',MT_CHANNEL,false,1,0);
99 virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list ¶ms)
101 if ((modechar == 'f') && (type == MT_CHANNEL))
105 std::string FloodParams = params[0];
106 chanrec* c = (chanrec*)target;
109 strlcpy(ndata,FloodParams.c_str(),MAXBUF);
135 /* Set up the flood parameters for this channel */
136 int nlines = atoi(lines);
137 int nsecs = atoi(secs);
138 if ((nlines<1) || (nsecs<1))
140 WriteServ(user->fd,"608 %s %s :Invalid flood parameter",user->nick,c->name);
145 if (!c->GetExt("flood"))
147 floodsettings *f = new floodsettings(ban,nlines,nsecs);
148 c->Extend("flood",(char*)f);
155 WriteServ(user->fd,"608 %s %s :Invalid flood parameter",user->nick,c->name);
162 chanrec* c = (chanrec*)target;
163 if (c->GetExt("flood"))
165 floodsettings *f = (floodsettings*)c->GetExt("flood");
175 void ProcessMessages(userrec* user,chanrec* dest,std::string &text)
177 floodsettings *f = (floodsettings*)dest->GetExt("flood");
181 if (f->shouldkick(user))
183 /* Youre outttta here! */
188 parameters[0] = dest->name;
189 parameters[1] = "+b";
190 parameters[2] = user->MakeWildHost();
191 Srv->SendMode(parameters,3,user);
193 Srv->KickUser(NULL, user, dest, "Channel flood triggered (mode +f)");
198 virtual void OnUserMessage(userrec* user, void* dest, int target_type, std::string text, char status)
200 if (target_type == TYPE_CHANNEL)
202 ProcessMessages(user,(chanrec*)dest,text);
206 virtual void OnUserNotice(userrec* user, void* dest, int target_type, std::string text, char status)
208 if (target_type == TYPE_CHANNEL)
210 ProcessMessages(user,(chanrec*)dest,text);
214 void OnChannelDelete(chanrec* chan)
216 if (chan->GetExt("flood"))
218 floodsettings *f = (floodsettings*)chan->GetExt("flood");
220 chan->Shrink("flood");
224 void Implements(char* List)
226 List[I_On005Numeric] = List[I_OnExtendedMode] = List[I_OnChannelDelete] = List[I_OnUserNotice] = List[I_OnUserMessage] = 1;
229 virtual void On005Numeric(std::string &output)
231 InsertMode(output, "f", 3);
234 virtual ~ModuleMsgFlood()
238 virtual Version GetVersion()
240 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
245 class ModuleMsgFloodFactory : public ModuleFactory
248 ModuleMsgFloodFactory()
252 ~ModuleMsgFloodFactory()
256 virtual Module * CreateModule(Server* Me)
258 return new ModuleMsgFlood(Me);
264 extern "C" void * init_module( void )
266 return new ModuleMsgFloodFactory;