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, const std::string &text)
179 floodsettings *f = (floodsettings*)dest->GetExt("flood");
183 if (f->shouldkick(user))
185 /* Youre outttta here! */
190 parameters[0] = dest->name;
191 parameters[1] = "+b";
192 parameters[2] = user->MakeWildHost();
193 Srv->SendMode(parameters,3,user);
195 Srv->KickUser(NULL, user, dest, "Channel flood triggered (mode +f)");
201 virtual void OnUserMessage(userrec* user, void* dest, int target_type, const std::string &text, char status)
203 if (target_type == TYPE_CHANNEL)
205 ProcessMessages(user,(chanrec*)dest,text);
209 virtual void OnUserNotice(userrec* user, void* dest, int target_type, const std::string &text, char status)
211 if (target_type == TYPE_CHANNEL)
213 ProcessMessages(user,(chanrec*)dest,text);
217 void OnChannelDelete(chanrec* chan)
219 if (chan->GetExt("flood"))
221 floodsettings *f = (floodsettings*)chan->GetExt("flood");
223 chan->Shrink("flood");
227 void Implements(char* List)
229 List[I_On005Numeric] = List[I_OnExtendedMode] = List[I_OnChannelDelete] = List[I_OnUserNotice] = List[I_OnUserMessage] = 1;
232 virtual void On005Numeric(std::string &output)
234 InsertMode(output, "f", 3);
237 virtual ~ModuleMsgFlood()
241 virtual Version GetVersion()
243 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
248 class ModuleMsgFloodFactory : public ModuleFactory
251 ModuleMsgFloodFactory()
255 ~ModuleMsgFloodFactory()
259 virtual Module * CreateModule(Server* Me)
261 return new ModuleMsgFlood(Me);
267 extern "C" void * init_module( void )
269 return new ModuleMsgFloodFactory;