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