]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_nickflood.cpp
93fbbfaaa7c9838471823675f4b1a33756783225
[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 /** Holds settings and state associated with channel mode +F
24  */
25 class nickfloodsettings
26 {
27  public:
28         unsigned int secs;
29         unsigned int nicks;
30         time_t reset;
31         time_t unlocktime;
32         unsigned int counter;
33
34         nickfloodsettings(unsigned int b, unsigned int c)
35                 : secs(b), nicks(c), unlocktime(0), counter(0)
36         {
37                 reset = ServerInstance->Time() + secs;
38         }
39
40         void addnick()
41         {
42                 if (ServerInstance->Time() > reset)
43                 {
44                         counter = 1;
45                         reset = ServerInstance->Time() + secs;
46                 }
47                 else
48                         counter++;
49         }
50
51         bool shouldlock()
52         {
53                 /* XXX HACK: using counter + 1 here now to allow the counter to only be incremented
54                  * on successful nick changes; this will be checked before the counter is
55                  * incremented.
56                  */
57                 return ((ServerInstance->Time() <= reset) && (counter == this->nicks));
58         }
59
60         void clear()
61         {
62                 counter = 0;
63         }
64
65         bool islocked()
66         {
67                 if (ServerInstance->Time() > unlocktime)
68                         unlocktime = 0;
69
70                 return (unlocktime != 0);
71         }
72
73         void lock()
74         {
75                 unlocktime = ServerInstance->Time() + 60;
76         }
77 };
78
79 /** Handles channel mode +F
80  */
81 class NickFlood : public ModeHandler
82 {
83  public:
84         SimpleExtItem<nickfloodsettings> ext;
85         NickFlood(Module* Creator) : ModeHandler(Creator, "nickflood", 'F', PARAM_SETONLY, MODETYPE_CHANNEL),
86                 ext("nickflood", Creator) { }
87
88         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
89         {
90                 if (adding)
91                 {
92                         std::string::size_type colon = parameter.find(':');
93                         if ((colon == std::string::npos) || (parameter.find('-') != std::string::npos))
94                         {
95                                 source->WriteNumeric(608, "%s :Invalid flood parameter",channel->name.c_str());
96                                 return MODEACTION_DENY;
97                         }
98
99                         /* Set up the flood parameters for this channel */
100                         unsigned int nnicks = ConvToInt(parameter.substr(0, colon));
101                         unsigned int nsecs = ConvToInt(parameter.substr(colon+1));
102
103                         if ((nnicks<1) || (nsecs<1))
104                         {
105                                 source->WriteNumeric(608, "%s :Invalid flood parameter",channel->name.c_str());
106                                 return MODEACTION_DENY;
107                         }
108
109                         nickfloodsettings* f = ext.get(channel);
110                         if ((f) && (nnicks == f->nicks) && (nsecs == f->secs))
111                                 // mode params match
112                                 return MODEACTION_DENY;
113
114                         ext.set(channel, new nickfloodsettings(nsecs, nnicks));
115                         parameter = ConvToStr(nnicks) + ":" + ConvToStr(nsecs);
116                         return MODEACTION_ALLOW;
117                 }
118                 else
119                 {
120                         if (!channel->IsModeSet(this))
121                                 return MODEACTION_DENY;
122
123                         ext.unset(channel);
124                         return MODEACTION_ALLOW;
125                 }
126         }
127 };
128
129 class ModuleNickFlood : public Module
130 {
131         NickFlood nf;
132
133  public:
134         ModuleNickFlood()
135                 : nf(this)
136         {
137         }
138
139         ModResult OnUserPreNick(User* user, const std::string &newnick) CXX11_OVERRIDE
140         {
141                 for (UCListIter i = user->chans.begin(); i != user->chans.end(); i++)
142                 {
143                         Channel* channel = (*i)->chan;
144                         ModResult res;
145
146                         nickfloodsettings *f = nf.ext.get(channel);
147                         if (f)
148                         {
149                                 res = ServerInstance->OnCheckExemption(user,channel,"nickflood");
150                                 if (res == MOD_RES_ALLOW)
151                                         continue;
152
153                                 if (f->islocked())
154                                 {
155                                         user->WriteNumeric(ERR_CANTCHANGENICK, ":%s has been locked for nickchanges for 60 seconds because there have been more than %u nick changes in %u seconds", channel->name.c_str(), f->nicks, f->secs);
156                                         return MOD_RES_DENY;
157                                 }
158
159                                 if (f->shouldlock())
160                                 {
161                                         f->clear();
162                                         f->lock();
163                                         channel->WriteChannelWithServ((char*)ServerInstance->Config->ServerName.c_str(), "NOTICE %s :No nick changes are allowed for 60 seconds because there have been more than %u nick changes in %u seconds.", channel->name.c_str(), f->nicks, f->secs);
164                                         return MOD_RES_DENY;
165                                 }
166                         }
167                 }
168
169                 return MOD_RES_PASSTHRU;
170         }
171
172         /*
173          * 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.
174          */
175         void OnUserPostNick(User* user, const std::string &oldnick) CXX11_OVERRIDE
176         {
177                 if (isdigit(user->nick[0])) /* allow switches to UID */
178                         return;
179
180                 for (UCListIter i = user->chans.begin(); i != user->chans.end(); ++i)
181                 {
182                         Channel* channel = (*i)->chan;
183                         ModResult res;
184
185                         nickfloodsettings *f = nf.ext.get(channel);
186                         if (f)
187                         {
188                                 res = ServerInstance->OnCheckExemption(user,channel,"nickflood");
189                                 if (res == MOD_RES_ALLOW)
190                                         return;
191
192                                 /* moved this here to avoid incrementing the counter for nick
193                                  * changes that are denied for some other reason (bans, +N, etc.)
194                                  * per bug #874.
195                                  */
196                                 f->addnick();
197                         }
198                 }
199         }
200
201         Version GetVersion() CXX11_OVERRIDE
202         {
203                 return Version("Channel mode F - nick flood protection", VF_VENDOR);
204         }
205 };
206
207 MODULE_INIT(ModuleNickFlood)