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