1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
21 /* $ModDesc: Provides channel mode +f (message flood protection) */
23 /** Holds flood settings and state for mode +f
25 class floodsettings : public classbase
32 std::map<userrec*,int> counters;
34 floodsettings() : ban(0), secs(0), lines(0) {};
35 floodsettings(bool a, int b, int c) : ban(a), secs(b), lines(c)
37 reset = time(NULL) + secs;
40 void addmessage(userrec* who)
42 std::map<userrec*,int>::iterator iter = counters.find(who);
43 if (iter != counters.end())
51 if (time(NULL) > reset)
54 reset = time(NULL) + secs;
58 bool shouldkick(userrec* who)
60 std::map<userrec*,int>::iterator iter = counters.find(who);
61 if (iter != counters.end())
63 return (iter->second >= this->lines);
68 void clear(userrec* who)
70 std::map<userrec*,int>::iterator iter = counters.find(who);
71 if (iter != counters.end())
78 /** Handles channel mode +f
80 class MsgFlood : public ModeHandler
83 MsgFlood(InspIRCd* Instance) : ModeHandler(Instance, 'f', 1, 0, false, MODETYPE_CHANNEL, false) { }
85 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
88 if (channel->GetExt("flood",x))
89 return std::make_pair(true, (x->ban ? "*" : "")+ConvToStr(x->lines)+":"+ConvToStr(x->secs));
91 return std::make_pair(false, parameter);
94 bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
96 /* When TS is equal, the alphabetically later one wins */
97 return (their_param < our_param);
100 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
108 strlcpy(ndata,parameter.c_str(),MAXBUF);
134 /* Set up the flood parameters for this channel */
135 int nlines = atoi(lines);
136 int nsecs = atoi(secs);
137 if ((nlines<1) || (nsecs<1))
139 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
141 return MODEACTION_DENY;
145 if (!channel->GetExt("flood", f))
147 parameter = std::string(ban ? "*" : "") + ConvToStr(nlines) + ":" +ConvToStr(nsecs);
148 floodsettings *f = new floodsettings(ban,nsecs,nlines);
149 channel->Extend("flood",f);
150 channel->SetMode('f', true);
151 channel->SetModeParam('f', parameter.c_str(), true);
152 return MODEACTION_ALLOW;
156 if (((nlines != f->lines) || (nsecs != f->secs)) && ((nsecs > 0) && (nlines > 0)) || (ban != f->ban))
159 floodsettings *f = new floodsettings(ban,nsecs,nlines);
160 parameter = std::string(ban ? "*" : "") + ConvToStr(nlines) + ":" +ConvToStr(nsecs);
161 channel->Shrink("flood");
162 channel->Extend("flood",f);
163 channel->SetModeParam('f', parameter.c_str(), true);
164 return MODEACTION_ALLOW;
171 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
173 return MODEACTION_DENY;
178 if (channel->GetExt("flood", f))
181 channel->Shrink("flood");
182 channel->SetMode('f', false);
183 return MODEACTION_ALLOW;
187 return MODEACTION_DENY;
191 class ModuleMsgFlood : public Module
198 ModuleMsgFlood(InspIRCd* Me)
202 mf = new MsgFlood(ServerInstance);
203 if (!ServerInstance->AddMode(mf, 'f'))
204 throw ModuleException("Could not add new modes!");
207 void ProcessMessages(userrec* user,chanrec* dest, const std::string &text)
212 if (dest->GetExt("flood", f))
215 if (f->shouldkick(user))
217 /* Youre outttta here! */
221 const char* parameters[3];
222 parameters[0] = dest->name;
223 parameters[1] = "+b";
224 parameters[2] = user->MakeWildHost();
225 ServerInstance->SendMode(parameters,3,user);
226 std::deque<std::string> n;
227 /* Propogate the ban to other servers.
228 * We dont know what protocol we may be using,
229 * so this event is picked up by our protocol
230 * module and formed into a ban command that
231 * suits the protocol in use.
233 n.push_back(dest->name);
235 n.push_back(user->MakeWildHost());
236 Event rmode((char *)&n, NULL, "send_mode");
237 rmode.Send(ServerInstance);
239 char kickmessage[MAXBUF];
240 snprintf(kickmessage, MAXBUF, "Channel flood triggered (limit is %d lines in %d secs)", f->lines, f->secs);
241 dest->ServerKickUser(user, kickmessage, true);
247 virtual void OnUserMessage(userrec* user, void* dest, int target_type, const std::string &text, char status, const CUList &exempt_list)
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, const CUList &exempt_list)
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, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
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;