]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_nickflood.cpp
Delete some obsolete comments.
[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 #include "modules/exemption.h"
23
24 // The number of seconds nickname changing will be blocked for.
25 static unsigned int duration;
26
27 /** Holds settings and state associated with channel mode +F
28  */
29 class nickfloodsettings
30 {
31  public:
32         unsigned int secs;
33         unsigned int nicks;
34         time_t reset;
35         time_t unlocktime;
36         unsigned int counter;
37
38         nickfloodsettings(unsigned int b, unsigned int c)
39                 : secs(b), nicks(c), unlocktime(0), counter(0)
40         {
41                 reset = ServerInstance->Time() + secs;
42         }
43
44         void addnick()
45         {
46                 if (ServerInstance->Time() > reset)
47                 {
48                         counter = 1;
49                         reset = ServerInstance->Time() + secs;
50                 }
51                 else
52                         counter++;
53         }
54
55         bool shouldlock()
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() + duration;
76         }
77 };
78
79 /** Handles channel mode +F
80  */
81 class NickFlood : public ParamMode<NickFlood, SimpleExtItem<nickfloodsettings> >
82 {
83  public:
84         NickFlood(Module* Creator)
85                 : ParamMode<NickFlood, SimpleExtItem<nickfloodsettings> >(Creator, "nickflood", 'F')
86         {
87         }
88
89         ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
90         {
91                 std::string::size_type colon = parameter.find(':');
92                 if ((colon == std::string::npos) || (parameter.find('-') != std::string::npos))
93                 {
94                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
95                         return MODEACTION_DENY;
96                 }
97
98                 /* Set up the flood parameters for this channel */
99                 unsigned int nnicks = ConvToNum<unsigned int>(parameter.substr(0, colon));
100                 unsigned int nsecs = ConvToNum<unsigned int>(parameter.substr(colon+1));
101
102                 if ((nnicks<1) || (nsecs<1))
103                 {
104                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
105                         return MODEACTION_DENY;
106                 }
107
108                 ext.set(channel, new nickfloodsettings(nsecs, nnicks));
109                 return MODEACTION_ALLOW;
110         }
111
112         void SerializeParam(Channel* chan, const nickfloodsettings* nfs, std::string& out)
113         {
114                 out.append(ConvToStr(nfs->nicks)).push_back(':');
115                 out.append(ConvToStr(nfs->secs));
116         }
117 };
118
119 class ModuleNickFlood : public Module
120 {
121         CheckExemption::EventProvider exemptionprov;
122         NickFlood nf;
123
124  public:
125         ModuleNickFlood()
126                 : exemptionprov(this)
127                 , nf(this)
128         {
129         }
130
131         void ReadConfig(ConfigStatus&) CXX11_OVERRIDE
132         {
133                 ConfigTag* tag = ServerInstance->Config->ConfValue("nickflood");
134                 duration = tag->getDuration("duration", 60, 10, 600);
135         }
136
137         ModResult OnUserPreNick(LocalUser* user, const std::string& newnick) CXX11_OVERRIDE
138         {
139                 for (User::ChanList::iterator i = user->chans.begin(); i != user->chans.end(); i++)
140                 {
141                         Channel* channel = (*i)->chan;
142                         ModResult res;
143
144                         nickfloodsettings *f = nf.ext.get(channel);
145                         if (f)
146                         {
147                                 res = CheckExemption::Call(exemptionprov, user, channel, "nickflood");
148                                 if (res == MOD_RES_ALLOW)
149                                         continue;
150
151                                 if (f->islocked())
152                                 {
153                                         user->WriteNumeric(ERR_CANTCHANGENICK, InspIRCd::Format("%s has been locked for nickchanges for %u seconds because there have been more than %u nick changes in %u seconds", channel->name.c_str(), duration, f->nicks, f->secs));
154                                         return MOD_RES_DENY;
155                                 }
156
157                                 if (f->shouldlock())
158                                 {
159                                         f->clear();
160                                         f->lock();
161                                         channel->WriteNotice(InspIRCd::Format("No nick changes are allowed for %u seconds because there have been more than %u nick changes in %u seconds.", duration, f->nicks, f->secs));
162                                         return MOD_RES_DENY;
163                                 }
164                         }
165                 }
166
167                 return MOD_RES_PASSTHRU;
168         }
169
170         /*
171          * 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.
172          */
173         void OnUserPostNick(User* user, const std::string &oldnick) CXX11_OVERRIDE
174         {
175                 if (isdigit(user->nick[0])) /* allow switches to UID */
176                         return;
177
178                 for (User::ChanList::iterator i = user->chans.begin(); i != user->chans.end(); ++i)
179                 {
180                         Channel* channel = (*i)->chan;
181                         ModResult res;
182
183                         nickfloodsettings *f = nf.ext.get(channel);
184                         if (f)
185                         {
186                                 res = CheckExemption::Call(exemptionprov, user, channel, "nickflood");
187                                 if (res == MOD_RES_ALLOW)
188                                         return;
189
190                                 /* moved this here to avoid incrementing the counter for nick
191                                  * changes that are denied for some other reason (bans, +N, etc.)
192                                  * per bug #874.
193                                  */
194                                 f->addnick();
195                         }
196                 }
197         }
198
199         Version GetVersion() CXX11_OVERRIDE
200         {
201                 return Version("Provides channel mode +F, nick flood protection", VF_VENDOR);
202         }
203 };
204
205 MODULE_INIT(ModuleNickFlood)