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 bool addmessage(User* who)
48 if (ServerInstance->Time() > reset)
51 reset = ServerInstance->Time() + secs;
54 return (++counters[who] >= this->lines);
59 std::map<User*,int>::iterator iter = counters.find(who);
60 if (iter != counters.end())
67 /** Handles channel mode +f
69 class MsgFlood : public ModeHandler
72 SimpleExtItem<floodsettings> ext;
73 MsgFlood(Module* Creator) : ModeHandler(Creator, "flood", 'f', PARAM_SETONLY, MODETYPE_CHANNEL),
74 ext("messageflood", Creator) { }
76 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
78 floodsettings *f = ext.get(channel);
84 strlcpy(ndata,parameter.c_str(),MAXBUF);
110 /* Set up the flood parameters for this channel */
111 int nlines = atoi(lines);
112 int nsecs = atoi(secs);
113 if ((nlines<2) || (nsecs<1))
115 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
117 return MODEACTION_DENY;
123 parameter = std::string(ban ? "*" : "") + ConvToStr(nlines) + ":" +ConvToStr(nsecs);
124 f = new floodsettings(ban,nsecs,nlines);
126 channel->SetModeParam('f', parameter);
127 return MODEACTION_ALLOW;
131 std::string cur_param = channel->GetModeParameter('f');
132 parameter = std::string(ban ? "*" : "") + ConvToStr(nlines) + ":" +ConvToStr(nsecs);
133 if (cur_param == parameter)
136 return MODEACTION_DENY;
140 if ((((nlines != f->lines) || (nsecs != f->secs) || (ban != f->ban))) && (((nsecs > 0) && (nlines > 0))))
142 floodsettings *fs = new floodsettings(ban,nsecs,nlines);
143 ext.set(channel, fs);
144 channel->SetModeParam('f', parameter);
145 return MODEACTION_ALLOW;
149 return MODEACTION_DENY;
157 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
159 return MODEACTION_DENY;
167 channel->SetModeParam('f', "");
168 return MODEACTION_ALLOW;
172 return MODEACTION_DENY;
176 class ModuleMsgFlood : public Module
185 if (!ServerInstance->Modes->AddMode(&mf))
186 throw ModuleException("Could not add new modes!");
187 ServerInstance->Extensions.Register(&mf.ext);
188 Implementation eventlist[] = { I_OnUserPreNotice, I_OnUserPreMessage };
189 ServerInstance->Modules->Attach(eventlist, this, 2);
192 ModResult ProcessMessages(User* user,Channel* dest, const std::string &text)
194 ModResult res = ServerInstance->OnCheckExemption(user,dest,"flood");
195 if (!IS_LOCAL(user) || res == MOD_RES_ALLOW)
196 return MOD_RES_PASSTHRU;
198 floodsettings *f = mf.ext.get(dest);
201 if (f->addmessage(user))
203 /* Youre outttta here! */
207 std::vector<std::string> parameters;
208 parameters.push_back(dest->name);
209 parameters.push_back("+b");
210 parameters.push_back(user->MakeWildHost());
211 ServerInstance->SendGlobalMode(parameters, ServerInstance->FakeClient);
214 char kickmessage[MAXBUF];
215 snprintf(kickmessage, MAXBUF, "Channel flood triggered (limit is %d lines in %d secs)", f->lines, f->secs);
217 dest->KickUser(ServerInstance->FakeClient, user, kickmessage);
223 return MOD_RES_PASSTHRU;
226 ModResult OnUserPreMessage(User *user, void *dest, int target_type, std::string &text, char status, CUList &exempt_list)
228 if (target_type == TYPE_CHANNEL)
229 return ProcessMessages(user,(Channel*)dest,text);
231 return MOD_RES_PASSTHRU;
234 ModResult OnUserPreNotice(User *user, void *dest, int target_type, std::string &text, char status, CUList &exempt_list)
236 if (target_type == TYPE_CHANNEL)
237 return ProcessMessages(user,(Channel*)dest,text);
239 return MOD_RES_PASSTHRU;
248 return Version("Provides channel mode +f (message flood protection)", VF_VENDOR);
252 MODULE_INIT(ModuleMsgFlood)