2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
6 * Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
7 * Copyright (C) 2007 John Brooks <john.brooks@dereferenced.net>
8 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
9 * Copyright (C) 2006 Craig Edwards <craigedwards@brainbox.cc>
10 * Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
12 * This file is part of InspIRCd. InspIRCd is free software: you can
13 * redistribute it and/or modify it under the terms of the GNU General Public
14 * License as published by the Free Software Foundation, version 2.
16 * This program is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 /* $ModDesc: Provides channel mode +f (message flood protection) */
30 /** Holds flood settings and state for mode +f
39 std::map<User*,int> counters;
41 floodsettings(bool a, int b, int c) : ban(a), secs(b), lines(c)
43 reset = ServerInstance->Time() + secs;
46 void addmessage(User* who)
48 std::map<User*,int>::iterator iter = counters.find(who);
49 if (iter != counters.end())
57 if (ServerInstance->Time() > reset)
60 reset = ServerInstance->Time() + secs;
64 bool shouldkick(User* who)
66 std::map<User*,int>::iterator iter = counters.find(who);
67 if (iter != counters.end())
69 return (iter->second >= this->lines);
76 std::map<User*,int>::iterator iter = counters.find(who);
77 if (iter != counters.end())
84 /** Handles channel mode +f
86 class MsgFlood : public ModeHandler
89 SimpleExtItem<floodsettings> ext;
90 MsgFlood(Module* Creator) : ModeHandler(Creator, "flood", 'f', PARAM_SETONLY, MODETYPE_CHANNEL),
91 ext("messageflood", Creator) { }
93 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
95 floodsettings *f = ext.get(channel);
101 strlcpy(ndata,parameter.c_str(),MAXBUF);
127 /* Set up the flood parameters for this channel */
128 int nlines = atoi(lines);
129 int nsecs = atoi(secs);
130 if ((nlines<2) || (nsecs<1))
132 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
134 return MODEACTION_DENY;
140 parameter = std::string(ban ? "*" : "") + ConvToStr(nlines) + ":" +ConvToStr(nsecs);
141 f = new floodsettings(ban,nsecs,nlines);
143 channel->SetModeParam('f', parameter);
144 return MODEACTION_ALLOW;
148 std::string cur_param = channel->GetModeParameter('f');
149 parameter = std::string(ban ? "*" : "") + ConvToStr(nlines) + ":" +ConvToStr(nsecs);
150 if (cur_param == parameter)
153 return MODEACTION_DENY;
157 if ((((nlines != f->lines) || (nsecs != f->secs) || (ban != f->ban))) && (((nsecs > 0) && (nlines > 0))))
159 floodsettings *fs = new floodsettings(ban,nsecs,nlines);
160 ext.set(channel, fs);
161 channel->SetModeParam('f', parameter);
162 return MODEACTION_ALLOW;
166 return MODEACTION_DENY;
174 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
176 return MODEACTION_DENY;
184 channel->SetModeParam('f', "");
185 return MODEACTION_ALLOW;
189 return MODEACTION_DENY;
193 class ModuleMsgFlood : public Module
202 if (!ServerInstance->Modes->AddMode(&mf))
203 throw ModuleException("Could not add new modes!");
204 ServerInstance->Extensions.Register(&mf.ext);
205 Implementation eventlist[] = { I_OnUserPreNotice, I_OnUserPreMessage };
206 ServerInstance->Modules->Attach(eventlist, this, 2);
209 ModResult ProcessMessages(User* user,Channel* dest, const std::string &text)
211 ModResult res = ServerInstance->OnCheckExemption(user,dest,"flood");
212 if (!IS_LOCAL(user) || res == MOD_RES_ALLOW)
213 return MOD_RES_PASSTHRU;
215 floodsettings *f = mf.ext.get(dest);
219 if (f->shouldkick(user))
221 /* Youre outttta here! */
225 std::vector<std::string> parameters;
226 parameters.push_back(dest->name);
227 parameters.push_back("+b");
228 parameters.push_back(user->MakeWildHost());
229 ServerInstance->SendGlobalMode(parameters, ServerInstance->FakeClient);
232 char kickmessage[MAXBUF];
233 snprintf(kickmessage, MAXBUF, "Channel flood triggered (limit is %d lines in %d secs)", f->lines, f->secs);
235 dest->KickUser(ServerInstance->FakeClient, user, kickmessage);
241 return MOD_RES_PASSTHRU;
244 ModResult OnUserPreMessage(User *user, void *dest, int target_type, std::string &text, char status, CUList &exempt_list)
246 if (target_type == TYPE_CHANNEL)
247 return ProcessMessages(user,(Channel*)dest,text);
249 return MOD_RES_PASSTHRU;
252 ModResult OnUserPreNotice(User *user, void *dest, int target_type, std::string &text, char status, CUList &exempt_list)
254 if (target_type == TYPE_CHANNEL)
255 return ProcessMessages(user,(Channel*)dest,text);
257 return MOD_RES_PASSTHRU;
266 return Version("Provides channel mode +f (message flood protection)", VF_VENDOR);
270 MODULE_INIT(ModuleMsgFlood)