]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_nickflood.cpp
6de6b235cf1bbf6076c0b4dd39468bf94ffc8cc1
[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                         return MODEACTION_ALLOW;
119                 }
120                 else
121                 {
122                         if (!channel->IsModeSet(this))
123                                 return MODEACTION_DENY;
124
125                         ext.unset(channel);
126                         return MODEACTION_ALLOW;
127                 }
128         }
129 };
130
131 class ModuleNickFlood : public Module
132 {
133         NickFlood nf;
134
135  public:
136         ModuleNickFlood()
137                 : nf(this)
138         {
139         }
140
141         void init() CXX11_OVERRIDE
142         {
143                 ServerInstance->Modules->AddService(nf);
144                 ServerInstance->Modules->AddService(nf.ext);
145                 Implementation eventlist[] = { I_OnUserPreNick, I_OnUserPostNick };
146                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
147         }
148
149         ModResult OnUserPreNick(User* user, const std::string &newnick) CXX11_OVERRIDE
150         {
151                 for (UCListIter i = user->chans.begin(); i != user->chans.end(); i++)
152                 {
153                         Channel *channel = *i;
154                         ModResult res;
155
156                         nickfloodsettings *f = nf.ext.get(channel);
157                         if (f)
158                         {
159                                 res = ServerInstance->OnCheckExemption(user,channel,"nickflood");
160                                 if (res == MOD_RES_ALLOW)
161                                         continue;
162
163                                 if (f->islocked())
164                                 {
165                                         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);
166                                         return MOD_RES_DENY;
167                                 }
168
169                                 if (f->shouldlock())
170                                 {
171                                         f->clear();
172                                         f->lock();
173                                         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);
174                                         return MOD_RES_DENY;
175                                 }
176                         }
177                 }
178
179                 return MOD_RES_PASSTHRU;
180         }
181
182         /*
183          * 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.
184          */
185         void OnUserPostNick(User* user, const std::string &oldnick) CXX11_OVERRIDE
186         {
187                 if (isdigit(user->nick[0])) /* allow switches to UID */
188                         return;
189
190                 for (UCListIter i = user->chans.begin(); i != user->chans.end(); ++i)
191                 {
192                         Channel *channel = *i;
193                         ModResult res;
194
195                         nickfloodsettings *f = nf.ext.get(channel);
196                         if (f)
197                         {
198                                 res = ServerInstance->OnCheckExemption(user,channel,"nickflood");
199                                 if (res == MOD_RES_ALLOW)
200                                         return;
201
202                                 /* moved this here to avoid incrementing the counter for nick
203                                  * changes that are denied for some other reason (bans, +N, etc.)
204                                  * per bug #874.
205                                  */
206                                 f->addnick();
207                         }
208                 }
209         }
210
211         Version GetVersion() CXX11_OVERRIDE
212         {
213                 return Version("Channel mode F - nick flood protection", VF_VENDOR);
214         }
215 };
216
217 MODULE_INIT(ModuleNickFlood)