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 MsgFlood : public ModeHandler
89 MsgFlood() : ModeHandler('f', 1, 0, false, MODETYPE_CHANNEL, false) { }
91 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
97 strlcpy(ndata,parameter.c_str(),MAXBUF);
123 /* Set up the flood parameters for this channel */
124 int nlines = atoi(lines);
125 int nsecs = atoi(secs);
126 if ((nlines<1) || (nsecs<1))
128 WriteServ(source->fd,"608 %s %s :Invalid flood parameter",source->nick,channel->name);
130 return MODEACTION_DENY;
134 if (!channel->GetExt("flood"))
136 parameter = ConvToStr(nlines) + ":" +ConvToStr(nsecs);
137 floodsettings *f = new floodsettings(ban,nsecs,nlines);
138 channel->Extend("flood",(char*)f);
139 channel->SetMode('f', true);
140 channel->SetModeParam('f', parameter.c_str(), true);
141 return MODEACTION_ALLOW;
147 WriteServ(source->fd,"608 %s %s :Invalid flood parameter",source->nick,channel->name);
149 return MODEACTION_DENY;
154 if (channel->GetExt("flood"))
156 floodsettings *f = (floodsettings*)channel->GetExt("flood");
158 channel->Shrink("flood");
159 channel->SetMode('f', false);
160 return MODEACTION_ALLOW;
164 return MODEACTION_DENY;
168 class ModuleMsgFlood : public Module
175 ModuleMsgFlood(Server* Me)
180 Srv->AddMode(mf, 'f');
183 void ProcessMessages(userrec* user,chanrec* dest, const std::string &text)
187 floodsettings *f = (floodsettings*)dest->GetExt("flood");
191 if (f->shouldkick(user))
193 /* Youre outttta here! */
198 parameters[0] = dest->name;
199 parameters[1] = "+b";
200 parameters[2] = user->MakeWildHost();
201 Srv->SendMode(parameters,3,user);
202 std::deque<std::string> n;
203 /* Propogate the ban to other servers.
204 * We dont know what protocol we may be using,
205 * so this event is picked up by our protocol
206 * module and formed into a ban command that
207 * suits the protocol in use.
209 n.push_back(dest->name);
211 n.push_back(user->MakeWildHost());
212 Event rmode((char *)&n, NULL, "send_mode");
215 Srv->KickUser(NULL, user, dest, "Channel flood triggered (mode +f)");
221 virtual void OnUserMessage(userrec* user, void* dest, int target_type, const std::string &text, char status)
223 if (target_type == TYPE_CHANNEL)
225 ProcessMessages(user,(chanrec*)dest,text);
229 virtual void OnUserNotice(userrec* user, void* dest, int target_type, const std::string &text, char status)
231 if (target_type == TYPE_CHANNEL)
233 ProcessMessages(user,(chanrec*)dest,text);
237 void OnChannelDelete(chanrec* chan)
239 if (chan->GetExt("flood"))
241 floodsettings *f = (floodsettings*)chan->GetExt("flood");
243 chan->Shrink("flood");
247 void Implements(char* List)
249 List[I_On005Numeric] = List[I_OnChannelDelete] = List[I_OnUserNotice] = List[I_OnUserMessage] = 1;
252 virtual void On005Numeric(std::string &output)
254 InsertMode(output, "f", 3);
257 virtual ~ModuleMsgFlood()
262 virtual Version GetVersion()
264 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
269 class ModuleMsgFloodFactory : public ModuleFactory
272 ModuleMsgFloodFactory()
276 ~ModuleMsgFloodFactory()
280 virtual Module * CreateModule(Server* Me)
282 return new ModuleMsgFlood(Me);
288 extern "C" void * init_module( void )
290 return new ModuleMsgFloodFactory;