]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_messageflood.cpp
a898fd5d1ba037ebac81d23f2de73d23052c1d4f
[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 /* $ModDesc: Provides channel mode +f (message flood protection) */
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         std::map<User*, unsigned int> counters;
40
41         floodsettings(bool a, int b, int c) : ban(a), secs(b), lines(c)
42         {
43                 reset = ServerInstance->Time() + secs;
44         }
45
46         bool addmessage(User* who)
47         {
48                 if (ServerInstance->Time() > reset)
49                 {
50                         counters.clear();
51                         reset = ServerInstance->Time() + secs;
52                 }
53
54                 return (++counters[who] >= this->lines);
55         }
56
57         void clear(User* who)
58         {
59                 std::map<User*, unsigned int>::iterator iter = counters.find(who);
60                 if (iter != counters.end())
61                 {
62                         counters.erase(iter);
63                 }
64         }
65 };
66
67 /** Handles channel mode +f
68  */
69 class MsgFlood : public ModeHandler
70 {
71  public:
72         SimpleExtItem<floodsettings> ext;
73         MsgFlood(Module* Creator) : ModeHandler(Creator, "flood", 'f', PARAM_SETONLY, MODETYPE_CHANNEL),
74                 ext("messageflood", Creator) { }
75
76         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
77         {
78                 if (adding)
79                 {
80                         std::string::size_type colon = parameter.find(':');
81                         if ((colon == std::string::npos) || (parameter.find('-') != std::string::npos))
82                         {
83                                 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
84                                 return MODEACTION_DENY;
85                         }
86
87                         /* Set up the flood parameters for this channel */
88                         bool ban = (parameter[0] == '*');
89                         unsigned int nlines = ConvToInt(parameter.substr(ban ? 1 : 0, ban ? colon-1 : colon));
90                         unsigned int nsecs = ConvToInt(parameter.substr(colon+1));
91
92                         if ((nlines<2) || (nsecs<1))
93                         {
94                                 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
95                                 return MODEACTION_DENY;
96                         }
97
98                         floodsettings* f = ext.get(channel);
99                         if ((f) && (nlines == f->lines) && (nsecs == f->secs) && (ban == f->ban))
100                                 // mode params match
101                                 return MODEACTION_DENY;
102
103                         ext.set(channel, new floodsettings(ban, nsecs, nlines));
104                         parameter = std::string(ban ? "*" : "") + ConvToStr(nlines) + ":" + ConvToStr(nsecs);
105                         return MODEACTION_ALLOW;
106                 }
107                 else
108                 {
109                         if (!channel->IsModeSet(this))
110                                 return MODEACTION_DENY;
111
112                         ext.unset(channel);
113                         return MODEACTION_ALLOW;
114                 }
115         }
116 };
117
118 class ModuleMsgFlood : public Module
119 {
120         MsgFlood mf;
121
122  public:
123
124         ModuleMsgFlood()
125                 : mf(this)
126         {
127         }
128
129         void init() CXX11_OVERRIDE
130         {
131                 ServerInstance->Modules->AddService(mf);
132                 ServerInstance->Modules->AddService(mf.ext);
133                 ServerInstance->Modules->Attach(I_OnUserPreMessage, this);
134         }
135
136         ModResult ProcessMessages(User* user,Channel* dest, const std::string &text)
137         {
138                 if ((!IS_LOCAL(user)) || !dest->IsModeSet(mf))
139                         return MOD_RES_PASSTHRU;
140
141                 if (ServerInstance->OnCheckExemption(user,dest,"flood") == MOD_RES_ALLOW)
142                         return MOD_RES_PASSTHRU;
143
144                 floodsettings *f = mf.ext.get(dest);
145                 if (f)
146                 {
147                         if (f->addmessage(user))
148                         {
149                                 /* Youre outttta here! */
150                                 f->clear(user);
151                                 if (f->ban)
152                                 {
153                                         std::vector<std::string> parameters;
154                                         parameters.push_back(dest->name);
155                                         parameters.push_back("+b");
156                                         parameters.push_back("*!*@" + user->dhost);
157                                         ServerInstance->Modes->Process(parameters, ServerInstance->FakeClient);
158                                 }
159
160                                 const std::string kickMessage = "Channel flood triggered (limit is " + ConvToStr(f->lines) +
161                                         " in " + ConvToStr(f->secs) + " secs)";
162
163                                 dest->KickUser(ServerInstance->FakeClient, user, kickMessage);
164
165                                 return MOD_RES_DENY;
166                         }
167                 }
168
169                 return MOD_RES_PASSTHRU;
170         }
171
172         ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList& exempt_list, MessageType msgtype) CXX11_OVERRIDE
173         {
174                 if (target_type == TYPE_CHANNEL)
175                         return ProcessMessages(user,(Channel*)dest,text);
176
177                 return MOD_RES_PASSTHRU;
178         }
179
180         void Prioritize()
181         {
182                 // we want to be after all modules that might deny the message (e.g. m_muteban, m_noctcp, m_blockcolor, etc.)
183                 ServerInstance->Modules->SetPriority(this, I_OnUserPreMessage, PRIORITY_LAST);
184         }
185
186         Version GetVersion() CXX11_OVERRIDE
187         {
188                 return Version("Provides channel mode +f (message flood protection)", VF_VENDOR);
189         }
190 };
191
192 MODULE_INIT(ModuleMsgFlood)