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) */
29 class floodsettings : public classbase
36 std::map<userrec*,int> counters;
38 floodsettings() : ban(0), secs(0), lines(0) {};
39 floodsettings(bool a, int b, int c) : ban(a), secs(b), lines(c)
41 reset = time(NULL) + secs;
42 log(DEBUG,"Create new floodsettings: %lu %lu",time(NULL),reset);
45 void addmessage(userrec* who)
47 std::map<userrec*,int>::iterator iter = counters.find(who);
48 if (iter != counters.end())
51 log(DEBUG,"Count for %s is now %d",who->nick,iter->second);
56 log(DEBUG,"Count for %s is now *1*",who->nick);
58 if (time(NULL) > reset)
60 log(DEBUG,"floodsettings timer Resetting.");
62 reset = time(NULL) + secs;
66 bool shouldkick(userrec* who)
68 std::map<userrec*,int>::iterator iter = counters.find(who);
69 if (iter != counters.end())
71 log(DEBUG,"should kick? %d, %d",iter->second,this->lines);
72 return (iter->second >= this->lines);
77 void clear(userrec* who)
79 std::map<userrec*,int>::iterator iter = counters.find(who);
80 if (iter != counters.end())
87 class MsgFlood : public ModeHandler
90 MsgFlood() : ModeHandler('f', 1, 0, false, MODETYPE_CHANNEL, false) { }
92 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
95 if (channel->GetExt("flood",x))
96 return std::make_pair(true, (x->ban ? "*" : "")+ConvToStr(x->lines)+":"+ConvToStr(x->secs));
98 return std::make_pair(false, parameter);
101 bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
103 /* When TS is equal, the alphabetically later one wins */
104 return (their_param < our_param);
107 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
115 strlcpy(ndata,parameter.c_str(),MAXBUF);
141 /* Set up the flood parameters for this channel */
142 int nlines = atoi(lines);
143 int nsecs = atoi(secs);
144 if ((nlines<1) || (nsecs<1))
146 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
148 return MODEACTION_DENY;
152 if (!channel->GetExt("flood", f))
154 parameter = ConvToStr(nlines) + ":" +ConvToStr(nsecs);
155 floodsettings *f = new floodsettings(ban,nsecs,nlines);
156 channel->Extend("flood",f);
157 channel->SetMode('f', true);
158 channel->SetModeParam('f', parameter.c_str(), true);
159 return MODEACTION_ALLOW;
165 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
167 return MODEACTION_DENY;
172 if (channel->GetExt("flood", f))
175 channel->Shrink("flood");
176 channel->SetMode('f', false);
177 return MODEACTION_ALLOW;
181 return MODEACTION_DENY;
185 class ModuleMsgFlood : public Module
192 ModuleMsgFlood(Server* Me)
197 Srv->AddMode(mf, 'f');
200 void ProcessMessages(userrec* user,chanrec* dest, const std::string &text)
205 if (dest->GetExt("flood", f))
208 if (f->shouldkick(user))
210 /* Youre outttta here! */
214 const char* parameters[3];
215 parameters[0] = dest->name;
216 parameters[1] = "+b";
217 parameters[2] = user->MakeWildHost();
218 Srv->SendMode(parameters,3,user);
219 std::deque<std::string> n;
220 /* Propogate the ban to other servers.
221 * We dont know what protocol we may be using,
222 * so this event is picked up by our protocol
223 * module and formed into a ban command that
224 * suits the protocol in use.
226 n.push_back(dest->name);
228 n.push_back(user->MakeWildHost());
229 Event rmode((char *)&n, NULL, "send_mode");
232 dest->ServerKickUser(user, "Channel flood triggered (mode +f)", true);
238 virtual void OnUserMessage(userrec* user, void* dest, int target_type, const std::string &text, char status)
240 if (target_type == TYPE_CHANNEL)
242 ProcessMessages(user,(chanrec*)dest,text);
246 virtual void OnUserNotice(userrec* user, void* dest, int target_type, const std::string &text, char status)
248 if (target_type == TYPE_CHANNEL)
250 ProcessMessages(user,(chanrec*)dest,text);
254 void OnChannelDelete(chanrec* chan)
257 if (chan->GetExt("flood", f))
260 chan->Shrink("flood");
264 void Implements(char* List)
266 List[I_On005Numeric] = List[I_OnChannelDelete] = List[I_OnUserNotice] = List[I_OnUserMessage] = 1;
269 virtual void On005Numeric(std::string &output)
271 InsertMode(output, "f", 3);
274 virtual ~ModuleMsgFlood()
279 virtual Version GetVersion()
281 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
286 class ModuleMsgFloodFactory : public ModuleFactory
289 ModuleMsgFloodFactory()
293 ~ModuleMsgFloodFactory()
297 virtual Module * CreateModule(Server* Me)
299 return new ModuleMsgFlood(Me);
305 extern "C" void * init_module( void )
307 return new ModuleMsgFloodFactory;