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