]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_messageflood.cpp
Some more text fixes and improvements (#1618).
[user/henk/code/inspircd.git] / src / modules / m_messageflood.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
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>
11  *
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.
15  *
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
19  * details.
20  *
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/>.
23  */
24
25
26 #include "inspircd.h"
27 #include "modules/ctctags.h"
28 #include "modules/exemption.h"
29
30 /** Holds flood settings and state for mode +f
31  */
32 class floodsettings
33 {
34  public:
35         bool ban;
36         unsigned int secs;
37         unsigned int lines;
38         time_t reset;
39         insp::flat_map<User*, unsigned int> counters;
40
41         floodsettings(bool a, unsigned int b, unsigned int c)
42                 : ban(a)
43                 , secs(b)
44                 , lines(c)
45         {
46                 reset = ServerInstance->Time() + secs;
47         }
48
49         bool addmessage(User* who)
50         {
51                 if (ServerInstance->Time() > reset)
52                 {
53                         counters.clear();
54                         reset = ServerInstance->Time() + secs;
55                 }
56
57                 return (++counters[who] >= this->lines);
58         }
59
60         void clear(User* who)
61         {
62                 counters.erase(who);
63         }
64 };
65
66 /** Handles channel mode +f
67  */
68 class MsgFlood : public ParamMode<MsgFlood, SimpleExtItem<floodsettings> >
69 {
70  public:
71         MsgFlood(Module* Creator)
72                 : ParamMode<MsgFlood, SimpleExtItem<floodsettings> >(Creator, "flood", 'f')
73         {
74         }
75
76         ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
77         {
78                 std::string::size_type colon = parameter.find(':');
79                 if ((colon == std::string::npos) || (parameter.find('-') != std::string::npos))
80                 {
81                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
82                         return MODEACTION_DENY;
83                 }
84
85                 /* Set up the flood parameters for this channel */
86                 bool ban = (parameter[0] == '*');
87                 unsigned int nlines = ConvToNum<unsigned int>(parameter.substr(ban ? 1 : 0, ban ? colon-1 : colon));
88                 unsigned int nsecs = ConvToNum<unsigned int>(parameter.substr(colon+1));
89
90                 if ((nlines<2) || (nsecs<1))
91                 {
92                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
93                         return MODEACTION_DENY;
94                 }
95
96                 ext.set(channel, new floodsettings(ban, nsecs, nlines));
97                 return MODEACTION_ALLOW;
98         }
99
100         void SerializeParam(Channel* chan, const floodsettings* fs, std::string& out)
101         {
102                 if (fs->ban)
103                         out.push_back('*');
104                 out.append(ConvToStr(fs->lines)).push_back(':');
105                 out.append(ConvToStr(fs->secs));
106         }
107 };
108
109 class ModuleMsgFlood
110         : public Module
111         , public CTCTags::EventListener
112 {
113 private:
114         CheckExemption::EventProvider exemptionprov;
115         MsgFlood mf;
116
117  public:
118         ModuleMsgFlood()
119                 : CTCTags::EventListener(this)
120                 , exemptionprov(this)
121                 , mf(this)
122         {
123         }
124
125         ModResult HandleMessage(User* user, const MessageTarget& target)
126         {
127                 if (target.type != MessageTarget::TYPE_CHANNEL)
128                         return MOD_RES_PASSTHRU;
129
130                 Channel* dest = target.Get<Channel>();
131                 if ((!IS_LOCAL(user)) || !dest->IsModeSet(mf))
132                         return MOD_RES_PASSTHRU;
133
134                 ModResult res = CheckExemption::Call(exemptionprov, user, dest, "flood");
135                 if (res == MOD_RES_ALLOW)
136                         return MOD_RES_PASSTHRU;
137
138                 floodsettings *f = mf.ext.get(dest);
139                 if (f)
140                 {
141                         if (f->addmessage(user))
142                         {
143                                 /* Youre outttta here! */
144                                 f->clear(user);
145                                 if (f->ban)
146                                 {
147                                         Modes::ChangeList changelist;
148                                         changelist.push_add(ServerInstance->Modes->FindMode('b', MODETYPE_CHANNEL), "*!*@" + user->GetDisplayedHost());
149                                         ServerInstance->Modes->Process(ServerInstance->FakeClient, dest, NULL, changelist);
150                                 }
151
152                                 const std::string kickMessage = "Channel flood triggered (trigger is " + ConvToStr(f->lines) +
153                                         " lines in " + ConvToStr(f->secs) + " secs)";
154
155                                 dest->KickUser(ServerInstance->FakeClient, user, kickMessage);
156
157                                 return MOD_RES_DENY;
158                         }
159                 }
160
161                 return MOD_RES_PASSTHRU;
162         }
163
164         ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
165         {
166                 return HandleMessage(user, target);
167         }
168
169         ModResult OnUserPreTagMessage(User* user, const MessageTarget& target, CTCTags::TagMessageDetails& details) CXX11_OVERRIDE
170         {
171                 return HandleMessage(user, target);
172         }
173
174         void Prioritize() CXX11_OVERRIDE
175         {
176                 // we want to be after all modules that might deny the message (e.g. m_muteban, m_noctcp, m_blockcolor, etc.)
177                 ServerInstance->Modules->SetPriority(this, I_OnUserPreMessage, PRIORITY_LAST);
178         }
179
180         Version GetVersion() CXX11_OVERRIDE
181         {
182                 return Version("Provides channel mode +f, message flood protection", VF_VENDOR);
183         }
184 };
185
186 MODULE_INIT(ModuleMsgFlood)