]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_nickflood.cpp
3db6f5e641ad84b87d4a7b42b202a7df250c0347
[user/henk/code/inspircd.git] / src / modules / m_nickflood.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2007, 2009 Robin Burchell <robin+git@viroteck.net>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #include "inspircd.h"
22
23 /* $ModDesc: Provides channel mode +F (nick flood protection) */
24
25 /** Holds settings and state associated with channel mode +F
26  */
27 class nickfloodsettings
28 {
29  public:
30         int secs;
31         int nicks;
32         time_t reset;
33         time_t unlocktime;
34         int counter;
35         bool locked;
36
37         nickfloodsettings(int b, int c) : secs(b), nicks(c)
38         {
39                 reset = ServerInstance->Time() + secs;
40                 counter = 0;
41                 locked = false;
42         };
43
44         void addnick()
45         {
46                 counter++;
47                 if (ServerInstance->Time() > reset)
48                 {
49                         counter = 0;
50                         reset = ServerInstance->Time() + secs;
51                 }
52         }
53
54         bool shouldlock()
55         {
56                 /* XXX HACK: using counter + 1 here now to allow the counter to only be incremented
57                  * on successful nick changes; this will be checked before the counter is
58                  * incremented.
59                  */
60                 return (counter + 1 >= this->nicks);
61         }
62
63         void clear()
64         {
65                 counter = 0;
66         }
67
68         bool islocked()
69         {
70                 if (locked)
71                 {
72                         if (ServerInstance->Time() > unlocktime)
73                         {
74                                 locked = false;
75                                 return false;
76                         }
77                         else
78                         {
79                                 return true;
80                         }
81                 }
82                 return false;
83         }
84
85         void lock()
86         {
87                 locked = true;
88                 unlocktime = ServerInstance->Time() + 60;
89         }
90
91 };
92
93 /** Handles channel mode +j
94  */
95 class NickFlood : public ModeHandler
96 {
97  public:
98         SimpleExtItem<nickfloodsettings> ext;
99         NickFlood(Module* Creator) : ModeHandler(Creator, "nickflood", 'F', PARAM_SETONLY, MODETYPE_CHANNEL),
100                 ext("nickflood", Creator) { }
101
102         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
103         {
104                 nickfloodsettings *f = ext.get(channel);
105                 if (adding)
106                 {
107                         char ndata[MAXBUF];
108                         char* data = ndata;
109                         strlcpy(ndata,parameter.c_str(),MAXBUF);
110                         char* nicks = data;
111                         char* secs = NULL;
112                         while (*data)
113                         {
114                                 if (*data == ':')
115                                 {
116                                         *data = 0;
117                                         data++;
118                                         secs = data;
119                                         break;
120                                 }
121                                 else data++;
122                         }
123                         if (secs)
124                         {
125                                 /* Set up the flood parameters for this channel */
126                                 int nnicks = atoi(nicks);
127                                 int nsecs = atoi(secs);
128                                 if ((nnicks<1) || (nsecs<1))
129                                 {
130                                         source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
131                                         parameter.clear();
132                                         return MODEACTION_DENY;
133                                 }
134                                 else
135                                 {
136                                         if (!f)
137                                         {
138                                                 parameter = ConvToStr(nnicks) + ":" +ConvToStr(nsecs);
139                                                 f = new nickfloodsettings(nsecs, nnicks);
140                                                 ext.set(channel, f);
141                                                 channel->SetModeParam('F', parameter);
142                                                 return MODEACTION_ALLOW;
143                                         }
144                                         else
145                                         {
146                                                 std::string cur_param = channel->GetModeParameter('F');
147                                                 parameter = ConvToStr(nnicks) + ":" +ConvToStr(nsecs);
148                                                 if (cur_param == parameter)
149                                                 {
150                                                         // mode params match
151                                                         return MODEACTION_DENY;
152                                                 }
153                                                 else
154                                                 {
155                                                         // new mode param, replace old with new
156                                                         if ((nsecs > 0) && (nnicks > 0))
157                                                         {
158                                                                 f = new nickfloodsettings(nsecs, nnicks);
159                                                                 ext.set(channel, f);
160                                                                 channel->SetModeParam('F', parameter);
161                                                                 return MODEACTION_ALLOW;
162                                                         }
163                                                         else
164                                                         {
165                                                                 return MODEACTION_DENY;
166                                                         }
167                                                 }
168                                         }
169                                 }
170                         }
171                         else
172                         {
173                                 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
174                                 return MODEACTION_DENY;
175                         }
176                 }
177                 else
178                 {
179                         if (f)
180                         {
181                                 ext.unset(channel);
182                                 channel->SetModeParam('F', "");
183                                 return MODEACTION_ALLOW;
184                         }
185                 }
186                 return MODEACTION_DENY;
187         }
188 };
189
190 class ModuleNickFlood : public Module
191 {
192         NickFlood nf;
193
194  public:
195
196         ModuleNickFlood()
197                 : nf(this)
198         {
199                 if (!ServerInstance->Modes->AddMode(&nf))
200                         throw ModuleException("Could not add new modes!");
201                 ServerInstance->Extensions.Register(&nf.ext);
202                 Implementation eventlist[] = { I_OnUserPreNick, I_OnUserPostNick };
203                 ServerInstance->Modules->Attach(eventlist, this, 2);
204         }
205
206         ModResult OnUserPreNick(User* user, const std::string &newnick)
207         {
208                 if (ServerInstance->NICKForced.get(user)) /* Allow forced nick changes */
209                         return MOD_RES_PASSTHRU;
210
211                 for (UCListIter i = user->chans.begin(); i != user->chans.end(); i++)
212                 {
213                         Channel *channel = *i;
214                         ModResult res;
215
216                         nickfloodsettings *f = nf.ext.get(channel);
217                         if (f)
218                         {
219                                 res = ServerInstance->OnCheckExemption(user,channel,"nickflood");
220                                 if (res == MOD_RES_ALLOW)
221                                         continue;
222
223                                 if (f->islocked())
224                                 {
225                                         user->WriteNumeric(447, "%s :%s has been locked for nickchanges for 60 seconds because there have been more than %d nick changes in %d seconds", user->nick.c_str(), channel->name.c_str(), f->nicks, f->secs);
226                                         return MOD_RES_DENY;
227                                 }
228
229                                 if (f->shouldlock())
230                                 {
231                                         f->clear();
232                                         f->lock();
233                                         channel->WriteChannelWithServ((char*)ServerInstance->Config->ServerName.c_str(), "NOTICE %s :No nick changes are allowed for 60 seconds because there have been more than %d nick changes in %d seconds.", channel->name.c_str(), f->nicks, f->secs);
234                                         return MOD_RES_DENY;
235                                 }
236                         }
237                 }
238
239                 return MOD_RES_PASSTHRU;
240         }
241
242         /*
243          * XXX: HACK: We do the increment on the *POST* event here (instead of all together) because we have no way of knowing whether other modules would block a nickchange.
244          */
245         void OnUserPostNick(User* user, const std::string &oldnick)
246         {
247                 if (isdigit(user->nick[0])) /* allow switches to UID */
248                         return;
249
250                 for (UCListIter i = user->chans.begin(); i != user->chans.end(); ++i)
251                 {
252                         Channel *channel = *i;
253                         ModResult res;
254
255                         nickfloodsettings *f = nf.ext.get(channel);
256                         if (f)
257                         {
258                                 res = ServerInstance->OnCheckExemption(user,channel,"nickflood");
259                                 if (res == MOD_RES_ALLOW)
260                                         return;
261                                 
262                                 /* moved this here to avoid incrementing the counter for nick
263                                  * changes that are denied for some other reason (bans, +N, etc.)
264                                  * per bug #874.
265                                  */
266                                 f->addnick();
267                         }
268                 }
269                 return;
270         }
271
272         ~ModuleNickFlood()
273         {
274         }
275
276         Version GetVersion()
277         {
278                 return Version("Channel mode F - nick flood protection", VF_VENDOR);
279         }
280 };
281
282 MODULE_INIT(ModuleNickFlood)