]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_messageflood.cpp
Sync helpop chmodes s and p with docs
[user/henk/code/inspircd.git] / src / modules / m_messageflood.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2013, 2017-2019 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2012-2014, 2016 Attila Molnar <attilamolnar@hush.com>
6  *   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
7  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
8  *   Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
9  *   Copyright (C) 2007 John Brooks <special@inspircd.org>
10  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
11  *   Copyright (C) 2006, 2008, 2010 Craig Edwards <brain@inspircd.org>
12  *
13  * This file is part of InspIRCd.  InspIRCd is free software: you can
14  * redistribute it and/or modify it under the terms of the GNU General Public
15  * License as published by the Free Software Foundation, version 2.
16  *
17  * This program is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24  */
25
26
27 #include "inspircd.h"
28 #include "modules/ctctags.h"
29 #include "modules/exemption.h"
30
31 /** Holds flood settings and state for mode +f
32  */
33 class floodsettings
34 {
35  public:
36         bool ban;
37         unsigned int secs;
38         unsigned int lines;
39         time_t reset;
40         insp::flat_map<User*, double> counters;
41
42         floodsettings(bool a, unsigned int b, unsigned int c)
43                 : ban(a)
44                 , secs(b)
45                 , lines(c)
46         {
47                 reset = ServerInstance->Time() + secs;
48         }
49
50         bool addmessage(User* who, double weight)
51         {
52                 if (ServerInstance->Time() > reset)
53                 {
54                         counters.clear();
55                         reset = ServerInstance->Time() + secs;
56                 }
57
58                 counters[who] += weight;
59                 return (counters[who] >= this->lines);
60         }
61
62         void clear(User* who)
63         {
64                 counters.erase(who);
65         }
66 };
67
68 /** Handles channel mode +f
69  */
70 class MsgFlood : public ParamMode<MsgFlood, SimpleExtItem<floodsettings> >
71 {
72  public:
73         MsgFlood(Module* Creator)
74                 : ParamMode<MsgFlood, SimpleExtItem<floodsettings> >(Creator, "flood", 'f')
75         {
76                 syntax = "[*]<messages>:<seconds>";
77         }
78
79         ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
80         {
81                 std::string::size_type colon = parameter.find(':');
82                 if ((colon == std::string::npos) || (parameter.find('-') != std::string::npos))
83                 {
84                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
85                         return MODEACTION_DENY;
86                 }
87
88                 /* Set up the flood parameters for this channel */
89                 bool ban = (parameter[0] == '*');
90                 unsigned int nlines = ConvToNum<unsigned int>(parameter.substr(ban ? 1 : 0, ban ? colon-1 : colon));
91                 unsigned int nsecs = ConvToNum<unsigned int>(parameter.substr(colon+1));
92
93                 if ((nlines<2) || (nsecs<1))
94                 {
95                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
96                         return MODEACTION_DENY;
97                 }
98
99                 ext.set(channel, new floodsettings(ban, nsecs, nlines));
100                 return MODEACTION_ALLOW;
101         }
102
103         void SerializeParam(Channel* chan, const floodsettings* fs, std::string& out)
104         {
105                 if (fs->ban)
106                         out.push_back('*');
107                 out.append(ConvToStr(fs->lines)).push_back(':');
108                 out.append(ConvToStr(fs->secs));
109         }
110 };
111
112 class ModuleMsgFlood
113         : public Module
114         , public CTCTags::EventListener
115 {
116 private:
117         CheckExemption::EventProvider exemptionprov;
118         MsgFlood mf;
119         double notice;
120         double privmsg;
121         double tagmsg;
122
123  public:
124         ModuleMsgFlood()
125                 : CTCTags::EventListener(this)
126                 , exemptionprov(this)
127                 , mf(this)
128         {
129         }
130
131         void ReadConfig(ConfigStatus&) CXX11_OVERRIDE
132         {
133                 ConfigTag* tag = ServerInstance->Config->ConfValue("messageflood");
134                 notice = tag->getFloat("notice", 1.0);
135                 privmsg = tag->getFloat("privmsg", 1.0);
136                 tagmsg = tag->getFloat("tagmsg", 0.2);
137         }
138
139         ModResult HandleMessage(User* user, const MessageTarget& target, double weight)
140         {
141                 if (target.type != MessageTarget::TYPE_CHANNEL)
142                         return MOD_RES_PASSTHRU;
143
144                 Channel* dest = target.Get<Channel>();
145                 if ((!IS_LOCAL(user)) || !dest->IsModeSet(mf))
146                         return MOD_RES_PASSTHRU;
147
148                 ModResult res = CheckExemption::Call(exemptionprov, user, dest, "flood");
149                 if (res == MOD_RES_ALLOW)
150                         return MOD_RES_PASSTHRU;
151
152                 floodsettings *f = mf.ext.get(dest);
153                 if (f)
154                 {
155                         if (f->addmessage(user, weight))
156                         {
157                                 /* Youre outttta here! */
158                                 f->clear(user);
159                                 if (f->ban)
160                                 {
161                                         Modes::ChangeList changelist;
162                                         changelist.push_add(ServerInstance->Modes->FindMode('b', MODETYPE_CHANNEL), "*!*@" + user->GetDisplayedHost());
163                                         ServerInstance->Modes->Process(ServerInstance->FakeClient, dest, NULL, changelist);
164                                 }
165
166                                 const std::string kickMessage = "Channel flood triggered (trigger is " + ConvToStr(f->lines) +
167                                         " lines in " + ConvToStr(f->secs) + " secs)";
168
169                                 dest->KickUser(ServerInstance->FakeClient, user, kickMessage);
170
171                                 return MOD_RES_DENY;
172                         }
173                 }
174
175                 return MOD_RES_PASSTHRU;
176         }
177
178         ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
179         {
180                 return HandleMessage(user, target, (details.type == MSG_PRIVMSG ? privmsg : notice));
181         }
182
183         ModResult OnUserPreTagMessage(User* user, const MessageTarget& target, CTCTags::TagMessageDetails& details) CXX11_OVERRIDE
184         {
185                 return HandleMessage(user, target, tagmsg);
186         }
187
188         void Prioritize() CXX11_OVERRIDE
189         {
190                 // we want to be after all modules that might deny the message (e.g. m_muteban, m_noctcp, m_blockcolor, etc.)
191                 ServerInstance->Modules->SetPriority(this, I_OnUserPreMessage, PRIORITY_LAST);
192         }
193
194         Version GetVersion() CXX11_OVERRIDE
195         {
196                 return Version("Adds channel mode f (flood) which helps protect against spammers which mass-message channels.", VF_VENDOR);
197         }
198 };
199
200 MODULE_INIT(ModuleMsgFlood)