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;
161 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
163 return MODEACTION_DENY;
168 if (channel->GetExt("flood", f))
171 channel->Shrink("flood");
172 channel->SetMode('f', false);
173 return MODEACTION_ALLOW;
177 return MODEACTION_DENY;
181 class ModuleMsgFlood : public Module
188 ModuleMsgFlood(InspIRCd* Me)
192 mf = new MsgFlood(ServerInstance);
193 ServerInstance->AddMode(mf, 'f');
196 void ProcessMessages(userrec* user,chanrec* dest, const std::string &text)
201 if (dest->GetExt("flood", f))
204 if (f->shouldkick(user))
206 /* Youre outttta here! */
210 const char* parameters[3];
211 parameters[0] = dest->name;
212 parameters[1] = "+b";
213 parameters[2] = user->MakeWildHost();
214 ServerInstance->SendMode(parameters,3,user);
215 std::deque<std::string> n;
216 /* Propogate the ban to other servers.
217 * We dont know what protocol we may be using,
218 * so this event is picked up by our protocol
219 * module and formed into a ban command that
220 * suits the protocol in use.
222 n.push_back(dest->name);
224 n.push_back(user->MakeWildHost());
225 Event rmode((char *)&n, NULL, "send_mode");
226 rmode.Send(ServerInstance);
228 dest->ServerKickUser(user, "Channel flood triggered (mode +f)", true);
234 virtual void OnUserMessage(userrec* user, void* dest, int target_type, const std::string &text, char status)
236 if (target_type == TYPE_CHANNEL)
238 ProcessMessages(user,(chanrec*)dest,text);
242 virtual void OnUserNotice(userrec* user, void* dest, int target_type, const std::string &text, char status)
244 if (target_type == TYPE_CHANNEL)
246 ProcessMessages(user,(chanrec*)dest,text);
250 void OnChannelDelete(chanrec* chan)
253 if (chan->GetExt("flood", f))
256 chan->Shrink("flood");
260 void Implements(char* List)
262 List[I_OnChannelDelete] = List[I_OnUserNotice] = List[I_OnUserMessage] = 1;
265 virtual ~ModuleMsgFlood()
270 virtual Version GetVersion()
272 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
277 class ModuleMsgFloodFactory : public ModuleFactory
280 ModuleMsgFloodFactory()
284 ~ModuleMsgFloodFactory()
288 virtual Module * CreateModule(InspIRCd* Me)
290 return new ModuleMsgFlood(Me);
296 extern "C" void * init_module( void )
298 return new ModuleMsgFloodFactory;