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) { reset = time(NULL) + secs; };
40 void addmessage(userrec* who)
42 std::map<userrec*,int>::iterator iter = counters.find(who);
43 if (iter != counters.end())
46 log(DEBUG,"Count for %s is now %d",who->nick,iter->second);
48 if (reset > time(NULL))
51 reset = time(NULL) + secs;
55 bool shouldkick(userrec* who)
57 std::map<userrec*,int>::iterator iter = counters.find(who);
58 if (iter != counters.end())
60 return (iter->second >= this->lines);
65 void clear(userrec* who)
67 std::map<userrec*,int>::iterator iter = counters.find(who);
68 if (iter != counters.end())
75 class ModuleMsgFlood : public Module
81 ModuleMsgFlood(Server* Me)
85 Srv->AddExtendedMode('f',MT_CHANNEL,false,1,0);
88 virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list ¶ms)
90 if ((modechar == 'f') && (type == MT_CHANNEL))
94 std::string FloodParams = params[0];
95 chanrec* c = (chanrec*)target;
98 strlcpy(ndata,FloodParams.c_str(),MAXBUF);
124 /* Set up the flood parameters for this channel */
125 int nlines = atoi(lines);
126 int nsecs = atoi(secs);
127 if ((nlines<1) || (nsecs<1))
129 WriteServ(user->fd,"608 %s %s :Invalid flood parameter",user->nick,c->name);
134 if (!c->GetExt("flood"))
136 floodsettings *f = new floodsettings(ban,nlines,nsecs);
137 c->Extend("flood",(char*)f);
144 WriteServ(user->fd,"608 %s %s :Invalid flood parameter",user->nick,c->name);
151 chanrec* c = (chanrec*)target;
152 if (c->GetExt("flood"))
154 floodsettings *f = (floodsettings*)c->GetExt("flood");
164 int ProcessMessages(userrec* user,chanrec* dest,std::string &text)
166 floodsettings *f = (floodsettings*)dest->GetExt("flood");
170 if (f->shouldkick(user))
172 /* Youre outttta here! */
174 Srv->KickUser(NULL, user, dest, "Channel flood triggered (mode +f)");
181 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text)
183 if (target_type == TYPE_CHANNEL)
185 return ProcessMessages(user,(chanrec*)dest,text);
190 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text)
192 if (target_type == TYPE_CHANNEL)
194 return ProcessMessages(user,(chanrec*)dest,text);
199 void OnChannelDelete(chanrec* chan)
201 if (chan->GetExt("flood"))
203 floodsettings *f = (floodsettings*)chan->GetExt("flood");
205 chan->Shrink("flood");
209 void Implements(char* List)
211 List[I_On005Numeric] = List[I_OnExtendedMode] = List[I_OnChannelDelete] = 1;
214 virtual void On005Numeric(std::string &output)
216 std::stringstream line(output);
217 std::string temp1, temp2;
221 if (temp1.substr(0,10) == "CHANMODES=")
223 // By doing this we're *assuming* no other module has fucked up the CHANMODES=
224 // section of the 005 numeric. If they have, we're going DOWN in a blaze of glory,
225 // with a honking great EXCEPTION :)
226 temp1.insert(temp1.find(",")+1,"f");
228 temp2 = temp2 + temp1 + " ";
231 output = temp2.substr(0,temp2.length()-1);
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;