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;
44 log(DEBUG,"Create new floodsettings: %lu %lu",time(NULL),reset);
47 void addmessage(userrec* who)
49 std::map<userrec*,int>::iterator iter = counters.find(who);
50 if (iter != counters.end())
53 log(DEBUG,"Count for %s is now %d",who->nick,iter->second);
58 log(DEBUG,"Count for %s is now *1*",who->nick);
60 if (time(NULL) > reset)
62 log(DEBUG,"floodsettings timer Resetting.");
64 reset = time(NULL) + secs;
68 bool shouldkick(userrec* who)
70 std::map<userrec*,int>::iterator iter = counters.find(who);
71 if (iter != counters.end())
73 log(DEBUG,"should kick? %d, %d",iter->second,this->lines);
74 return (iter->second >= this->lines);
79 void clear(userrec* who)
81 std::map<userrec*,int>::iterator iter = counters.find(who);
82 if (iter != counters.end())
89 class MsgFlood : public ModeHandler
92 MsgFlood(InspIRCd* Instance) : ModeHandler(Instance, 'f', 1, 0, false, MODETYPE_CHANNEL, false) { }
94 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
97 if (channel->GetExt("flood",x))
98 return std::make_pair(true, (x->ban ? "*" : "")+ConvToStr(x->lines)+":"+ConvToStr(x->secs));
100 return std::make_pair(false, parameter);
103 bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
105 /* When TS is equal, the alphabetically later one wins */
106 return (their_param < our_param);
109 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
117 strlcpy(ndata,parameter.c_str(),MAXBUF);
143 /* Set up the flood parameters for this channel */
144 int nlines = atoi(lines);
145 int nsecs = atoi(secs);
146 if ((nlines<1) || (nsecs<1))
148 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
150 return MODEACTION_DENY;
154 if (!channel->GetExt("flood", f))
156 parameter = ConvToStr(nlines) + ":" +ConvToStr(nsecs);
157 floodsettings *f = new floodsettings(ban,nsecs,nlines);
158 channel->Extend("flood",f);
159 channel->SetMode('f', true);
160 channel->SetModeParam('f', parameter.c_str(), true);
161 return MODEACTION_ALLOW;
167 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
169 return MODEACTION_DENY;
174 if (channel->GetExt("flood", f))
177 channel->Shrink("flood");
178 channel->SetMode('f', false);
179 return MODEACTION_ALLOW;
183 return MODEACTION_DENY;
187 class ModuleMsgFlood : public Module
194 ModuleMsgFlood(InspIRCd* Me)
198 mf = new MsgFlood(ServerInstance);
199 ServerInstance->AddMode(mf, 'f');
202 void ProcessMessages(userrec* user,chanrec* dest, const std::string &text)
207 if (dest->GetExt("flood", f))
210 if (f->shouldkick(user))
212 /* Youre outttta here! */
216 const char* parameters[3];
217 parameters[0] = dest->name;
218 parameters[1] = "+b";
219 parameters[2] = user->MakeWildHost();
220 ServerInstance->SendMode(parameters,3,user);
221 std::deque<std::string> n;
222 /* Propogate the ban to other servers.
223 * We dont know what protocol we may be using,
224 * so this event is picked up by our protocol
225 * module and formed into a ban command that
226 * suits the protocol in use.
228 n.push_back(dest->name);
230 n.push_back(user->MakeWildHost());
231 Event rmode((char *)&n, NULL, "send_mode");
232 rmode.Send(ServerInstance);
234 dest->ServerKickUser(user, "Channel flood triggered (mode +f)", true);
240 virtual void OnUserMessage(userrec* user, void* dest, int target_type, const std::string &text, char status)
242 if (target_type == TYPE_CHANNEL)
244 ProcessMessages(user,(chanrec*)dest,text);
248 virtual void OnUserNotice(userrec* user, void* dest, int target_type, const std::string &text, char status)
250 if (target_type == TYPE_CHANNEL)
252 ProcessMessages(user,(chanrec*)dest,text);
256 void OnChannelDelete(chanrec* chan)
259 if (chan->GetExt("flood", f))
262 chan->Shrink("flood");
266 void Implements(char* List)
268 List[I_On005Numeric] = List[I_OnChannelDelete] = List[I_OnUserNotice] = List[I_OnUserMessage] = 1;
271 virtual void On005Numeric(std::string &output)
273 ServerInstance->ModeGrok->InsertMode(output, "f", 3);
276 virtual ~ModuleMsgFlood()
281 virtual Version GetVersion()
283 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
288 class ModuleMsgFloodFactory : public ModuleFactory
291 ModuleMsgFloodFactory()
295 ~ModuleMsgFloodFactory()
299 virtual Module * CreateModule(InspIRCd* Me)
301 return new ModuleMsgFlood(Me);
307 extern "C" void * init_module( void )
309 return new ModuleMsgFloodFactory;