]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_messageflood.cpp
m_swhois Switch to OnPostOper hook instead of using OnPostCommand
[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         int secs;
37         int lines;
38         time_t reset;
39         std::map<User*,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         void addmessage(User* who)
47         {
48                 std::map<User*,int>::iterator iter = counters.find(who);
49                 if (iter != counters.end())
50                 {
51                         iter->second++;
52                 }
53                 else
54                 {
55                         counters[who] = 1;
56                 }
57                 if (ServerInstance->Time() > reset)
58                 {
59                         counters.clear();
60                         reset = ServerInstance->Time() + secs;
61                 }
62         }
63
64         bool shouldkick(User* who)
65         {
66                 std::map<User*,int>::iterator iter = counters.find(who);
67                 if (iter != counters.end())
68                 {
69                         return (iter->second >= this->lines);
70                 }
71                 else return false;
72         }
73
74         void clear(User* who)
75         {
76                 std::map<User*,int>::iterator iter = counters.find(who);
77                 if (iter != counters.end())
78                 {
79                         counters.erase(iter);
80                 }
81         }
82 };
83
84 /** Handles channel mode +f
85  */
86 class MsgFlood : public ModeHandler
87 {
88  public:
89         SimpleExtItem<floodsettings> ext;
90         MsgFlood(Module* Creator) : ModeHandler(Creator, "flood", 'f', PARAM_SETONLY, MODETYPE_CHANNEL),
91                 ext("messageflood", Creator) { }
92
93         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
94         {
95                 floodsettings *f = ext.get(channel);
96
97                 if (adding)
98                 {
99                         char ndata[MAXBUF];
100                         char* data = ndata;
101                         strlcpy(ndata,parameter.c_str(),MAXBUF);
102                         char* lines = data;
103                         char* secs = NULL;
104                         bool ban = false;
105                         if (*data == '*')
106                         {
107                                 ban = true;
108                                 lines++;
109                         }
110                         else
111                         {
112                                 ban = false;
113                         }
114                         while (*data)
115                         {
116                                 if (*data == ':')
117                                 {
118                                         *data = 0;
119                                         data++;
120                                         secs = data;
121                                         break;
122                                 }
123                                 else data++;
124                         }
125                         if (secs)
126                         {
127                                 /* Set up the flood parameters for this channel */
128                                 int nlines = atoi(lines);
129                                 int nsecs = atoi(secs);
130                                 if ((nlines<2) || (nsecs<1))
131                                 {
132                                         source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
133                                         parameter.clear();
134                                         return MODEACTION_DENY;
135                                 }
136                                 else
137                                 {
138                                         if (!f)
139                                         {
140                                                 parameter = std::string(ban ? "*" : "") + ConvToStr(nlines) + ":" +ConvToStr(nsecs);
141                                                 f = new floodsettings(ban,nsecs,nlines);
142                                                 ext.set(channel, f);
143                                                 channel->SetModeParam('f', parameter);
144                                                 return MODEACTION_ALLOW;
145                                         }
146                                         else
147                                         {
148                                                 std::string cur_param = channel->GetModeParameter('f');
149                                                 parameter = std::string(ban ? "*" : "") + ConvToStr(nlines) + ":" +ConvToStr(nsecs);
150                                                 if (cur_param == parameter)
151                                                 {
152                                                         // mode params match
153                                                         return MODEACTION_DENY;
154                                                 }
155                                                 else
156                                                 {
157                                                         if ((((nlines != f->lines) || (nsecs != f->secs) || (ban != f->ban))) && (((nsecs > 0) && (nlines > 0))))
158                                                         {
159                                                                 floodsettings *fs = new floodsettings(ban,nsecs,nlines);
160                                                                 ext.set(channel, fs);
161                                                                 channel->SetModeParam('f', parameter);
162                                                                 return MODEACTION_ALLOW;
163                                                         }
164                                                         else
165                                                         {
166                                                                 return MODEACTION_DENY;
167                                                         }
168                                                 }
169                                         }
170                                 }
171                         }
172                         else
173                         {
174                                 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
175                                 parameter.clear();
176                                 return MODEACTION_DENY;
177                         }
178                 }
179                 else
180                 {
181                         if (f)
182                         {
183                                 ext.unset(channel);
184                                 channel->SetModeParam('f', "");
185                                 return MODEACTION_ALLOW;
186                         }
187                 }
188
189                 return MODEACTION_DENY;
190         }
191 };
192
193 class ModuleMsgFlood : public Module
194 {
195         MsgFlood mf;
196
197  public:
198
199         ModuleMsgFlood()
200                 : mf(this)
201         {
202                 if (!ServerInstance->Modes->AddMode(&mf))
203                         throw ModuleException("Could not add new modes!");
204                 ServerInstance->Extensions.Register(&mf.ext);
205                 Implementation eventlist[] = { I_OnUserPreNotice, I_OnUserPreMessage };
206                 ServerInstance->Modules->Attach(eventlist, this, 2);
207         }
208
209         ModResult ProcessMessages(User* user,Channel* dest, const std::string &text)
210         {
211                 ModResult res = ServerInstance->OnCheckExemption(user,dest,"flood");
212                 if (!IS_LOCAL(user) || res == MOD_RES_ALLOW)
213                         return MOD_RES_PASSTHRU;
214
215                 floodsettings *f = mf.ext.get(dest);
216                 if (f)
217                 {
218                         f->addmessage(user);
219                         if (f->shouldkick(user))
220                         {
221                                 /* Youre outttta here! */
222                                 f->clear(user);
223                                 if (f->ban)
224                                 {
225                                         std::vector<std::string> parameters;
226                                         parameters.push_back(dest->name);
227                                         parameters.push_back("+b");
228                                         parameters.push_back(user->MakeWildHost());
229                                         ServerInstance->SendGlobalMode(parameters, ServerInstance->FakeClient);
230                                 }
231
232                                 char kickmessage[MAXBUF];
233                                 snprintf(kickmessage, MAXBUF, "Channel flood triggered (limit is %d lines in %d secs)", f->lines, f->secs);
234
235                                 dest->KickUser(ServerInstance->FakeClient, user, kickmessage);
236
237                                 return MOD_RES_DENY;
238                         }
239                 }
240
241                 return MOD_RES_PASSTHRU;
242         }
243
244         ModResult OnUserPreMessage(User *user, void *dest, int target_type, std::string &text, char status, CUList &exempt_list)
245         {
246                 if (target_type == TYPE_CHANNEL)
247                         return ProcessMessages(user,(Channel*)dest,text);
248
249                 return MOD_RES_PASSTHRU;
250         }
251
252         ModResult OnUserPreNotice(User *user, void *dest, int target_type, std::string &text, char status, CUList &exempt_list)
253         {
254                 if (target_type == TYPE_CHANNEL)
255                         return ProcessMessages(user,(Channel*)dest,text);
256
257                 return MOD_RES_PASSTHRU;
258         }
259
260         ~ModuleMsgFlood()
261         {
262         }
263
264         Version GetVersion()
265         {
266                 return Version("Provides channel mode +f (message flood protection)", VF_VENDOR);
267         }
268 };
269
270 MODULE_INIT(ModuleMsgFlood)