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"
27 /* $ModDesc: Provides channel mode +f (message flood protection) */
31 class floodsettings : public classbase
38 std::map<userrec*,int> counters;
40 floodsettings() : ban(0), secs(0), lines(0) {};
41 floodsettings(bool a, int b, int c) : ban(a), secs(b), lines(c)
43 reset = time(NULL) + secs;
46 void addmessage(userrec* who)
48 std::map<userrec*,int>::iterator iter = counters.find(who);
49 if (iter != counters.end())
57 if (time(NULL) > reset)
60 reset = time(NULL) + secs;
64 bool shouldkick(userrec* who)
66 std::map<userrec*,int>::iterator iter = counters.find(who);
67 if (iter != counters.end())
69 return (iter->second >= this->lines);
74 void clear(userrec* who)
76 std::map<userrec*,int>::iterator iter = counters.find(who);
77 if (iter != counters.end())
84 class MsgFlood : public ModeHandler
87 MsgFlood(InspIRCd* Instance) : ModeHandler(Instance, 'f', 1, 0, false, MODETYPE_CHANNEL, false) { }
89 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
92 if (channel->GetExt("flood",x))
93 return std::make_pair(true, (x->ban ? "*" : "")+ConvToStr(x->lines)+":"+ConvToStr(x->secs));
95 return std::make_pair(false, parameter);
98 bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
100 /* When TS is equal, the alphabetically later one wins */
101 return (their_param < our_param);
104 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
112 strlcpy(ndata,parameter.c_str(),MAXBUF);
138 /* Set up the flood parameters for this channel */
139 int nlines = atoi(lines);
140 int nsecs = atoi(secs);
141 if ((nlines<1) || (nsecs<1))
143 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
145 return MODEACTION_DENY;
149 if (!channel->GetExt("flood", f))
151 parameter = ConvToStr(nlines) + ":" +ConvToStr(nsecs);
152 floodsettings *f = new floodsettings(ban,nsecs,nlines);
153 channel->Extend("flood",f);
154 channel->SetMode('f', true);
155 channel->SetModeParam('f', parameter.c_str(), true);
156 return MODEACTION_ALLOW;
162 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
164 return MODEACTION_DENY;
169 if (channel->GetExt("flood", f))
172 channel->Shrink("flood");
173 channel->SetMode('f', false);
174 return MODEACTION_ALLOW;
178 return MODEACTION_DENY;
182 class ModuleMsgFlood : public Module
189 ModuleMsgFlood(InspIRCd* Me)
193 mf = new MsgFlood(ServerInstance);
194 ServerInstance->AddMode(mf, 'f');
197 void ProcessMessages(userrec* user,chanrec* dest, const std::string &text)
202 if (dest->GetExt("flood", f))
205 if (f->shouldkick(user))
207 /* Youre outttta here! */
211 const char* parameters[3];
212 parameters[0] = dest->name;
213 parameters[1] = "+b";
214 parameters[2] = user->MakeWildHost();
215 ServerInstance->SendMode(parameters,3,user);
216 std::deque<std::string> n;
217 /* Propogate the ban to other servers.
218 * We dont know what protocol we may be using,
219 * so this event is picked up by our protocol
220 * module and formed into a ban command that
221 * suits the protocol in use.
223 n.push_back(dest->name);
225 n.push_back(user->MakeWildHost());
226 Event rmode((char *)&n, NULL, "send_mode");
227 rmode.Send(ServerInstance);
229 dest->ServerKickUser(user, "Channel flood triggered (mode +f)", true);
235 virtual void OnUserMessage(userrec* user, void* dest, int target_type, const std::string &text, char status)
237 if (target_type == TYPE_CHANNEL)
239 ProcessMessages(user,(chanrec*)dest,text);
243 virtual void OnUserNotice(userrec* user, void* dest, int target_type, const std::string &text, char status)
245 if (target_type == TYPE_CHANNEL)
247 ProcessMessages(user,(chanrec*)dest,text);
251 void OnChannelDelete(chanrec* chan)
254 if (chan->GetExt("flood", f))
257 chan->Shrink("flood");
261 void Implements(char* List)
263 List[I_On005Numeric] = List[I_OnChannelDelete] = List[I_OnUserNotice] = List[I_OnUserMessage] = 1;
266 virtual void On005Numeric(std::string &output)
268 ServerInstance->Modes->InsertMode(output, "f", 3);
271 virtual ~ModuleMsgFlood()
276 virtual Version GetVersion()
278 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
283 class ModuleMsgFloodFactory : public ModuleFactory
286 ModuleMsgFloodFactory()
290 ~ModuleMsgFloodFactory()
294 virtual Module * CreateModule(InspIRCd* Me)
296 return new ModuleMsgFlood(Me);
302 extern "C" void * init_module( void )
304 return new ModuleMsgFloodFactory;