]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_nickflood.cpp
Silence some GCC warnings.
[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                 syntax = "<nick-changes>:<seconds>";
88         }
89
90         ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
91         {
92                 std::string::size_type colon = parameter.find(':');
93                 if ((colon == std::string::npos) || (parameter.find('-') != std::string::npos))
94                 {
95                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
96                         return MODEACTION_DENY;
97                 }
98
99                 /* Set up the flood parameters for this channel */
100                 unsigned int nnicks = ConvToNum<unsigned int>(parameter.substr(0, colon));
101                 unsigned int nsecs = ConvToNum<unsigned int>(parameter.substr(colon+1));
102
103                 if ((nnicks<1) || (nsecs<1))
104                 {
105                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
106                         return MODEACTION_DENY;
107                 }
108
109                 ext.set(channel, new nickfloodsettings(nsecs, nnicks));
110                 return MODEACTION_ALLOW;
111         }
112
113         void SerializeParam(Channel* chan, const nickfloodsettings* nfs, std::string& out)
114         {
115                 out.append(ConvToStr(nfs->nicks)).push_back(':');
116                 out.append(ConvToStr(nfs->secs));
117         }
118 };
119
120 class ModuleNickFlood : public Module
121 {
122         CheckExemption::EventProvider exemptionprov;
123         NickFlood nf;
124
125  public:
126         ModuleNickFlood()
127                 : exemptionprov(this)
128                 , nf(this)
129         {
130         }
131
132         void ReadConfig(ConfigStatus&) CXX11_OVERRIDE
133         {
134                 ConfigTag* tag = ServerInstance->Config->ConfValue("nickflood");
135                 duration = tag->getDuration("duration", 60, 10, 600);
136         }
137
138         ModResult OnUserPreNick(LocalUser* user, const std::string& newnick) CXX11_OVERRIDE
139         {
140                 for (User::ChanList::iterator i = user->chans.begin(); i != user->chans.end(); i++)
141                 {
142                         Channel* channel = (*i)->chan;
143                         ModResult res;
144
145                         nickfloodsettings *f = nf.ext.get(channel);
146                         if (f)
147                         {
148                                 res = CheckExemption::Call(exemptionprov, user, channel, "nickflood");
149                                 if (res == MOD_RES_ALLOW)
150                                         continue;
151
152                                 if (f->islocked())
153                                 {
154                                         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));
155                                         return MOD_RES_DENY;
156                                 }
157
158                                 if (f->shouldlock())
159                                 {
160                                         f->clear();
161                                         f->lock();
162                                         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));
163                                         return MOD_RES_DENY;
164                                 }
165                         }
166                 }
167
168                 return MOD_RES_PASSTHRU;
169         }
170
171         /*
172          * 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.
173          */
174         void OnUserPostNick(User* user, const std::string &oldnick) CXX11_OVERRIDE
175         {
176                 if (isdigit(user->nick[0])) /* allow switches to UID */
177                         return;
178
179                 for (User::ChanList::iterator i = user->chans.begin(); i != user->chans.end(); ++i)
180                 {
181                         Channel* channel = (*i)->chan;
182                         ModResult res;
183
184                         nickfloodsettings *f = nf.ext.get(channel);
185                         if (f)
186                         {
187                                 res = CheckExemption::Call(exemptionprov, user, channel, "nickflood");
188                                 if (res == MOD_RES_ALLOW)
189                                         return;
190
191                                 /* moved this here to avoid incrementing the counter for nick
192                                  * changes that are denied for some other reason (bans, +N, etc.)
193                                  * per bug #874.
194                                  */
195                                 f->addnick();
196                         }
197                 }
198         }
199
200         Version GetVersion() CXX11_OVERRIDE
201         {
202                 return Version("Provides channel mode +F, nick flood protection", VF_VENDOR);
203         }
204 };
205
206 MODULE_INIT(ModuleNickFlood)