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 * ---------------------------------------------------
26 /* $ModDesc: Provides channel mode +f (message flood protection) */
30 class floodsettings : public classbase
37 std::map<userrec*,int> counters;
39 floodsettings() : ban(0), secs(0), lines(0) {};
40 floodsettings(bool a, int b, int c) : ban(a), secs(b), lines(c)
42 reset = time(NULL) + secs;
45 void addmessage(userrec* who)
47 std::map<userrec*,int>::iterator iter = counters.find(who);
48 if (iter != counters.end())
56 if (time(NULL) > reset)
59 reset = time(NULL) + secs;
63 bool shouldkick(userrec* who)
65 std::map<userrec*,int>::iterator iter = counters.find(who);
66 if (iter != counters.end())
68 return (iter->second >= this->lines);
73 void clear(userrec* who)
75 std::map<userrec*,int>::iterator iter = counters.find(who);
76 if (iter != counters.end())
83 class MsgFlood : public ModeHandler
86 MsgFlood(InspIRCd* Instance) : ModeHandler(Instance, 'f', 1, 0, false, MODETYPE_CHANNEL, false) { }
88 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
91 if (channel->GetExt("flood",x))
92 return std::make_pair(true, (x->ban ? "*" : "")+ConvToStr(x->lines)+":"+ConvToStr(x->secs));
94 return std::make_pair(false, parameter);
97 bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
99 /* When TS is equal, the alphabetically later one wins */
100 return (their_param < our_param);
103 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
111 strlcpy(ndata,parameter.c_str(),MAXBUF);
137 /* Set up the flood parameters for this channel */
138 int nlines = atoi(lines);
139 int nsecs = atoi(secs);
140 if ((nlines<1) || (nsecs<1))
142 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
144 return MODEACTION_DENY;
148 if (!channel->GetExt("flood", f))
150 parameter = ConvToStr(nlines) + ":" +ConvToStr(nsecs);
151 floodsettings *f = new floodsettings(ban,nsecs,nlines);
152 channel->Extend("flood",f);
153 channel->SetMode('f', true);
154 channel->SetModeParam('f', parameter.c_str(), true);
155 return MODEACTION_ALLOW;
159 if (((nlines != f->lines) || (nsecs != f->secs)) && ((nsecs > 0) && (nlines > 0)))
162 floodsettings *f = new floodsettings(ban,nsecs,nlines);
163 parameter = ConvToStr(nlines) + ":" +ConvToStr(nsecs);
164 channel->Shrink("flood");
165 channel->Extend("flood",f);
166 channel->SetModeParam('f', parameter.c_str(), true);
167 return MODEACTION_ALLOW;
174 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
176 return MODEACTION_DENY;
181 if (channel->GetExt("flood", f))
184 channel->Shrink("flood");
185 channel->SetMode('f', false);
186 return MODEACTION_ALLOW;
190 return MODEACTION_DENY;
194 class ModuleMsgFlood : public Module
201 ModuleMsgFlood(InspIRCd* Me)
205 mf = new MsgFlood(ServerInstance);
206 ServerInstance->AddMode(mf, 'f');
209 void ProcessMessages(userrec* user,chanrec* dest, const std::string &text)
214 if (dest->GetExt("flood", f))
217 if (f->shouldkick(user))
219 /* Youre outttta here! */
223 const char* parameters[3];
224 parameters[0] = dest->name;
225 parameters[1] = "+b";
226 parameters[2] = user->MakeWildHost();
227 ServerInstance->SendMode(parameters,3,user);
228 std::deque<std::string> n;
229 /* Propogate the ban to other servers.
230 * We dont know what protocol we may be using,
231 * so this event is picked up by our protocol
232 * module and formed into a ban command that
233 * suits the protocol in use.
235 n.push_back(dest->name);
237 n.push_back(user->MakeWildHost());
238 Event rmode((char *)&n, NULL, "send_mode");
239 rmode.Send(ServerInstance);
241 dest->ServerKickUser(user, "Channel flood triggered (mode +f)", true);
247 virtual void OnUserMessage(userrec* user, void* dest, int target_type, const std::string &text, char status)
249 if (target_type == TYPE_CHANNEL)
251 ProcessMessages(user,(chanrec*)dest,text);
255 virtual void OnUserNotice(userrec* user, void* dest, int target_type, const std::string &text, char status)
257 if (target_type == TYPE_CHANNEL)
259 ProcessMessages(user,(chanrec*)dest,text);
263 void OnChannelDelete(chanrec* chan)
266 if (chan->GetExt("flood", f))
269 chan->Shrink("flood");
273 void Implements(char* List)
275 List[I_OnChannelDelete] = List[I_OnUserNotice] = List[I_OnUserMessage] = 1;
278 virtual ~ModuleMsgFlood()
280 ServerInstance->Modes->DelMode(mf);
284 virtual Version GetVersion()
286 return Version(1, 0, 0, 0, VF_COMMON | VF_VENDOR);
291 class ModuleMsgFloodFactory : public ModuleFactory
294 ModuleMsgFloodFactory()
298 ~ModuleMsgFloodFactory()
302 virtual Module * CreateModule(InspIRCd* Me)
304 return new ModuleMsgFlood(Me);
310 extern "C" void * init_module( void )
312 return new ModuleMsgFloodFactory;