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 /** Holds flood settings and state for mode +f
37 std::map<User*, unsigned int> counters;
39 floodsettings(bool a, int b, int c) : ban(a), secs(b), lines(c)
41 reset = ServerInstance->Time() + secs;
44 bool addmessage(User* who)
46 if (ServerInstance->Time() > reset)
49 reset = ServerInstance->Time() + secs;
52 return (++counters[who] >= this->lines);
57 std::map<User*, unsigned int>::iterator iter = counters.find(who);
58 if (iter != counters.end())
65 /** Handles channel mode +f
67 class MsgFlood : public ModeHandler
70 SimpleExtItem<floodsettings> ext;
71 MsgFlood(Module* Creator) : ModeHandler(Creator, "flood", 'f', PARAM_SETONLY, MODETYPE_CHANNEL),
72 ext("messageflood", Creator) { }
74 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
78 std::string::size_type colon = parameter.find(':');
79 if ((colon == std::string::npos) || (parameter.find('-') != std::string::npos))
81 source->WriteNumeric(608, "%s :Invalid flood parameter", channel->name.c_str());
82 return MODEACTION_DENY;
85 /* Set up the flood parameters for this channel */
86 bool ban = (parameter[0] == '*');
87 unsigned int nlines = ConvToInt(parameter.substr(ban ? 1 : 0, ban ? colon-1 : colon));
88 unsigned int nsecs = ConvToInt(parameter.substr(colon+1));
90 if ((nlines<2) || (nsecs<1))
92 source->WriteNumeric(608, "%s :Invalid flood parameter", channel->name.c_str());
93 return MODEACTION_DENY;
96 floodsettings* f = ext.get(channel);
97 if ((f) && (nlines == f->lines) && (nsecs == f->secs) && (ban == f->ban))
99 return MODEACTION_DENY;
101 ext.set(channel, new floodsettings(ban, nsecs, nlines));
102 parameter = std::string(ban ? "*" : "") + ConvToStr(nlines) + ":" + ConvToStr(nsecs);
103 return MODEACTION_ALLOW;
107 if (!channel->IsModeSet(this))
108 return MODEACTION_DENY;
111 return MODEACTION_ALLOW;
116 class ModuleMsgFlood : public Module
127 ModResult OnUserPreMessage(User* user, void* voiddest, int target_type, std::string& text, char status, CUList& exempt_list, MessageType msgtype) CXX11_OVERRIDE
129 if (target_type != TYPE_CHANNEL)
130 return MOD_RES_PASSTHRU;
132 Channel* dest = static_cast<Channel*>(voiddest);
133 if ((!IS_LOCAL(user)) || !dest->IsModeSet(mf))
134 return MOD_RES_PASSTHRU;
136 if (ServerInstance->OnCheckExemption(user,dest,"flood") == MOD_RES_ALLOW)
137 return MOD_RES_PASSTHRU;
139 floodsettings *f = mf.ext.get(dest);
142 if (f->addmessage(user))
144 /* Youre outttta here! */
148 std::vector<std::string> parameters;
149 parameters.push_back(dest->name);
150 parameters.push_back("+b");
151 parameters.push_back("*!*@" + user->dhost);
152 ServerInstance->Modes->Process(parameters, ServerInstance->FakeClient);
155 const std::string kickMessage = "Channel flood triggered (limit is " + ConvToStr(f->lines) +
156 " in " + ConvToStr(f->secs) + " secs)";
158 dest->KickUser(ServerInstance->FakeClient, user, kickMessage);
164 return MOD_RES_PASSTHRU;
169 // we want to be after all modules that might deny the message (e.g. m_muteban, m_noctcp, m_blockcolor, etc.)
170 ServerInstance->Modules->SetPriority(this, I_OnUserPreMessage, PRIORITY_LAST);
173 Version GetVersion() CXX11_OVERRIDE
175 return Version("Provides channel mode +f (message flood protection)", VF_VENDOR);
179 MODULE_INIT(ModuleMsgFlood)