]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_nickflood.cpp
Convert WriteNumeric() calls to pass the parameters of the numeric as method parameters
[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 /** Holds settings and state associated with channel mode +F
24  */
25 class nickfloodsettings
26 {
27  public:
28         unsigned int secs;
29         unsigned int nicks;
30         time_t reset;
31         time_t unlocktime;
32         unsigned int counter;
33
34         nickfloodsettings(unsigned int b, unsigned int c)
35                 : secs(b), nicks(c), unlocktime(0), counter(0)
36         {
37                 reset = ServerInstance->Time() + secs;
38         }
39
40         void addnick()
41         {
42                 if (ServerInstance->Time() > reset)
43                 {
44                         counter = 1;
45                         reset = ServerInstance->Time() + secs;
46                 }
47                 else
48                         counter++;
49         }
50
51         bool shouldlock()
52         {
53                 /* XXX HACK: using counter + 1 here now to allow the counter to only be incremented
54                  * on successful nick changes; this will be checked before the counter is
55                  * incremented.
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() + 60;
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)
90         {
91                 std::string::size_type colon = parameter.find(':');
92                 if ((colon == std::string::npos) || (parameter.find('-') != std::string::npos))
93                 {
94                         source->WriteNumeric(608, channel->name, "Invalid flood parameter");
95                         return MODEACTION_DENY;
96                 }
97
98                 /* Set up the flood parameters for this channel */
99                 unsigned int nnicks = ConvToInt(parameter.substr(0, colon));
100                 unsigned int nsecs = ConvToInt(parameter.substr(colon+1));
101
102                 if ((nnicks<1) || (nsecs<1))
103                 {
104                         source->WriteNumeric(608, channel->name, "Invalid flood 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         NickFlood nf;
122
123  public:
124         ModuleNickFlood()
125                 : nf(this)
126         {
127         }
128
129         ModResult OnUserPreNick(LocalUser* user, const std::string& newnick) CXX11_OVERRIDE
130         {
131                 for (User::ChanList::iterator i = user->chans.begin(); i != user->chans.end(); i++)
132                 {
133                         Channel* channel = (*i)->chan;
134                         ModResult res;
135
136                         nickfloodsettings *f = nf.ext.get(channel);
137                         if (f)
138                         {
139                                 res = ServerInstance->OnCheckExemption(user,channel,"nickflood");
140                                 if (res == MOD_RES_ALLOW)
141                                         continue;
142
143                                 if (f->islocked())
144                                 {
145                                         user->WriteNumeric(ERR_CANTCHANGENICK, InspIRCd::Format("%s has been locked for nickchanges for 60 seconds because there have been more than %u nick changes in %u seconds", channel->name.c_str(), f->nicks, f->secs));
146                                         return MOD_RES_DENY;
147                                 }
148
149                                 if (f->shouldlock())
150                                 {
151                                         f->clear();
152                                         f->lock();
153                                         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);
154                                         return MOD_RES_DENY;
155                                 }
156                         }
157                 }
158
159                 return MOD_RES_PASSTHRU;
160         }
161
162         /*
163          * 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.
164          */
165         void OnUserPostNick(User* user, const std::string &oldnick) CXX11_OVERRIDE
166         {
167                 if (isdigit(user->nick[0])) /* allow switches to UID */
168                         return;
169
170                 for (User::ChanList::iterator i = user->chans.begin(); i != user->chans.end(); ++i)
171                 {
172                         Channel* channel = (*i)->chan;
173                         ModResult res;
174
175                         nickfloodsettings *f = nf.ext.get(channel);
176                         if (f)
177                         {
178                                 res = ServerInstance->OnCheckExemption(user,channel,"nickflood");
179                                 if (res == MOD_RES_ALLOW)
180                                         return;
181
182                                 /* moved this here to avoid incrementing the counter for nick
183                                  * changes that are denied for some other reason (bans, +N, etc.)
184                                  * per bug #874.
185                                  */
186                                 f->addnick();
187                         }
188                 }
189         }
190
191         Version GetVersion() CXX11_OVERRIDE
192         {
193                 return Version("Channel mode F - nick flood protection", VF_VENDOR);
194         }
195 };
196
197 MODULE_INIT(ModuleNickFlood)