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) */
28 /** Holds flood settings and state for mode +f
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 /** Handles channel mode +f
85 class MsgFlood : public ModeHandler
88 MsgFlood(InspIRCd* Instance) : ModeHandler(Instance, 'f', 1, 0, false, MODETYPE_CHANNEL, false) { }
90 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
93 if (channel->GetExt("flood",x))
94 return std::make_pair(true, (x->ban ? "*" : "")+ConvToStr(x->lines)+":"+ConvToStr(x->secs));
96 return std::make_pair(false, parameter);
99 bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
101 /* When TS is equal, the alphabetically later one wins */
102 return (their_param < our_param);
105 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
113 strlcpy(ndata,parameter.c_str(),MAXBUF);
139 /* Set up the flood parameters for this channel */
140 int nlines = atoi(lines);
141 int nsecs = atoi(secs);
142 if ((nlines<1) || (nsecs<1))
144 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
146 return MODEACTION_DENY;
150 if (!channel->GetExt("flood", f))
152 parameter = ConvToStr(nlines) + ":" +ConvToStr(nsecs);
153 floodsettings *f = new floodsettings(ban,nsecs,nlines);
154 channel->Extend("flood",f);
155 channel->SetMode('f', true);
156 channel->SetModeParam('f', parameter.c_str(), true);
157 return MODEACTION_ALLOW;
161 if (((nlines != f->lines) || (nsecs != f->secs)) && ((nsecs > 0) && (nlines > 0)))
164 floodsettings *f = new floodsettings(ban,nsecs,nlines);
165 parameter = ConvToStr(nlines) + ":" +ConvToStr(nsecs);
166 channel->Shrink("flood");
167 channel->Extend("flood",f);
168 channel->SetModeParam('f', parameter.c_str(), true);
169 return MODEACTION_ALLOW;
176 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
178 return MODEACTION_DENY;
183 if (channel->GetExt("flood", f))
186 channel->Shrink("flood");
187 channel->SetMode('f', false);
188 return MODEACTION_ALLOW;
192 return MODEACTION_DENY;
196 class ModuleMsgFlood : public Module
203 ModuleMsgFlood(InspIRCd* Me)
207 mf = new MsgFlood(ServerInstance);
208 ServerInstance->AddMode(mf, 'f');
211 void ProcessMessages(userrec* user,chanrec* dest, const std::string &text)
216 if (dest->GetExt("flood", f))
219 if (f->shouldkick(user))
221 /* Youre outttta here! */
225 const char* parameters[3];
226 parameters[0] = dest->name;
227 parameters[1] = "+b";
228 parameters[2] = user->MakeWildHost();
229 ServerInstance->SendMode(parameters,3,user);
230 std::deque<std::string> n;
231 /* Propogate the ban to other servers.
232 * We dont know what protocol we may be using,
233 * so this event is picked up by our protocol
234 * module and formed into a ban command that
235 * suits the protocol in use.
237 n.push_back(dest->name);
239 n.push_back(user->MakeWildHost());
240 Event rmode((char *)&n, NULL, "send_mode");
241 rmode.Send(ServerInstance);
243 dest->ServerKickUser(user, "Channel flood triggered (mode +f)", true);
249 virtual void OnUserMessage(userrec* user, void* dest, int target_type, const std::string &text, char status, const CUList &exempt_list)
251 if (target_type == TYPE_CHANNEL)
253 ProcessMessages(user,(chanrec*)dest,text);
257 virtual void OnUserNotice(userrec* user, void* dest, int target_type, const std::string &text, char status, const CUList &exempt_list)
259 if (target_type == TYPE_CHANNEL)
261 ProcessMessages(user,(chanrec*)dest,text);
265 void OnChannelDelete(chanrec* chan)
268 if (chan->GetExt("flood", f))
271 chan->Shrink("flood");
275 void Implements(char* List)
277 List[I_OnChannelDelete] = List[I_OnUserNotice] = List[I_OnUserMessage] = 1;
280 virtual ~ModuleMsgFlood()
282 ServerInstance->Modes->DelMode(mf);
286 virtual Version GetVersion()
288 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
293 class ModuleMsgFloodFactory : public ModuleFactory
296 ModuleMsgFloodFactory()
300 ~ModuleMsgFloodFactory()
304 virtual Module * CreateModule(InspIRCd* Me)
306 return new ModuleMsgFlood(Me);
312 extern "C" void * init_module( void )
314 return new ModuleMsgFloodFactory;