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