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 return (iter->second >= this->lines);
75 void clear(userrec* who)
77 std::map<userrec*,int>::iterator iter = counters.find(who);
78 if (iter != counters.end())
85 class ModuleMsgFlood : public Module
91 ModuleMsgFlood(Server* Me)
95 Srv->AddExtendedMode('f',MT_CHANNEL,false,1,0);
98 virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list ¶ms)
100 if ((modechar == 'f') && (type == MT_CHANNEL))
104 std::string FloodParams = params[0];
105 chanrec* c = (chanrec*)target;
108 strlcpy(ndata,FloodParams.c_str(),MAXBUF);
134 /* Set up the flood parameters for this channel */
135 int nlines = atoi(lines);
136 int nsecs = atoi(secs);
137 if ((nlines<1) || (nsecs<1))
139 WriteServ(user->fd,"608 %s %s :Invalid flood parameter",user->nick,c->name);
144 if (!c->GetExt("flood"))
146 floodsettings *f = new floodsettings(ban,nlines,nsecs);
147 c->Extend("flood",(char*)f);
154 WriteServ(user->fd,"608 %s %s :Invalid flood parameter",user->nick,c->name);
161 chanrec* c = (chanrec*)target;
162 if (c->GetExt("flood"))
164 floodsettings *f = (floodsettings*)c->GetExt("flood");
174 int ProcessMessages(userrec* user,chanrec* dest,std::string &text)
176 floodsettings *f = (floodsettings*)dest->GetExt("flood");
180 if (f->shouldkick(user))
182 /* Youre outttta here! */
187 parameters[0] = dest->name;
188 parameters[1] = "+b";
189 parameters[2] = user->MakeWildHost();
190 Srv->SendMode(parameters,3,user);
192 Srv->KickUser(NULL, user, dest, "Channel flood triggered (mode +f)");
199 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
201 if (target_type == TYPE_CHANNEL)
203 return ProcessMessages(user,(chanrec*)dest,text);
208 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
210 if (target_type == TYPE_CHANNEL)
212 return 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_OnUserPreNotice] = List[I_OnUserPreMessage] = 1;
232 virtual void On005Numeric(std::string &output)
234 std::stringstream line(output);
235 std::string temp1, temp2;
239 if (temp1.substr(0,10) == "CHANMODES=")
241 // By doing this we're *assuming* no other module has fucked up the CHANMODES=
242 // section of the 005 numeric. If they have, we're going DOWN in a blaze of glory,
243 // with a honking great EXCEPTION :)
244 temp1.insert(temp1.find(",")+1,"f");
246 temp2 = temp2 + temp1 + " ";
249 output = temp2.substr(0,temp2.length()-1);
252 virtual ~ModuleMsgFlood()
256 virtual Version GetVersion()
258 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
263 class ModuleMsgFloodFactory : public ModuleFactory
266 ModuleMsgFloodFactory()
270 ~ModuleMsgFloodFactory()
274 virtual Module * CreateModule(Server* Me)
276 return new ModuleMsgFlood(Me);
282 extern "C" void * init_module( void )
284 return new ModuleMsgFloodFactory;