]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_nickflood.cpp
Fix mistakenly using Clang instead of GCC on older FreeBSD versions.
[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
139         ModuleNickFlood()
140                 : nf(this)
141         {
142         }
143
144         void init()
145         {
146                 ServerInstance->Modules->AddService(nf);
147                 ServerInstance->Modules->AddService(nf.ext);
148                 Implementation eventlist[] = { I_OnUserPreNick, I_OnUserPostNick };
149                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
150         }
151
152         ModResult OnUserPreNick(User* user, const std::string &newnick)
153         {
154                 if (ServerInstance->NICKForced.get(user)) /* Allow forced nick changes */
155                         return MOD_RES_PASSTHRU;
156
157                 for (UCListIter i = user->chans.begin(); i != user->chans.end(); i++)
158                 {
159                         Channel *channel = *i;
160                         ModResult res;
161
162                         nickfloodsettings *f = nf.ext.get(channel);
163                         if (f)
164                         {
165                                 res = ServerInstance->OnCheckExemption(user,channel,"nickflood");
166                                 if (res == MOD_RES_ALLOW)
167                                         continue;
168
169                                 if (f->islocked())
170                                 {
171                                         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);
172                                         return MOD_RES_DENY;
173                                 }
174
175                                 if (f->shouldlock())
176                                 {
177                                         f->clear();
178                                         f->lock();
179                                         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);
180                                         return MOD_RES_DENY;
181                                 }
182                         }
183                 }
184
185                 return MOD_RES_PASSTHRU;
186         }
187
188         /*
189          * 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.
190          */
191         void OnUserPostNick(User* user, const std::string &oldnick)
192         {
193                 if (isdigit(user->nick[0])) /* allow switches to UID */
194                         return;
195
196                 for (UCListIter i = user->chans.begin(); i != user->chans.end(); ++i)
197                 {
198                         Channel *channel = *i;
199                         ModResult res;
200
201                         nickfloodsettings *f = nf.ext.get(channel);
202                         if (f)
203                         {
204                                 res = ServerInstance->OnCheckExemption(user,channel,"nickflood");
205                                 if (res == MOD_RES_ALLOW)
206                                         return;
207
208                                 /* moved this here to avoid incrementing the counter for nick
209                                  * changes that are denied for some other reason (bans, +N, etc.)
210                                  * per bug #874.
211                                  */
212                                 f->addnick();
213                         }
214                 }
215         }
216
217         ~ModuleNickFlood()
218         {
219         }
220
221         Version GetVersion()
222         {
223                 return Version("Channel mode F - nick flood protection", VF_VENDOR);
224         }
225 };
226
227 MODULE_INIT(ModuleNickFlood)