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