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