]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_nickflood.cpp
Remove more classbase
[user/henk/code/inspircd.git] / src / modules / m_nickflood.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15
16 /* $ModDesc: Provides channel mode +F (nick flood protection) */
17
18 /** Holds settings and state associated with channel mode +F
19  */
20 class nickfloodsettings
21 {
22  public:
23         int secs;
24         int nicks;
25         time_t reset;
26         time_t unlocktime;
27         int counter;
28         bool locked;
29
30         nickfloodsettings(int b, int c) : secs(b), nicks(c)
31         {
32                 reset = ServerInstance->Time() + secs;
33                 counter = 0;
34                 locked = false;
35         };
36
37         void addnick()
38         {
39                 counter++;
40                 if (ServerInstance->Time() > reset)
41                 {
42                         counter = 0;
43                         reset = ServerInstance->Time() + secs;
44                 }
45         }
46
47         bool shouldlock()
48         {
49                 /* XXX HACK: using counter + 1 here now to allow the counter to only be incremented
50                  * on successful nick changes; this will be checked before the counter is
51                  * incremented.
52                  */
53                 return (counter + 1 >= this->nicks);
54         }
55
56         void clear()
57         {
58                 counter = 0;
59         }
60
61         bool islocked()
62         {
63                 if (locked)
64                 {
65                         if (ServerInstance->Time() > unlocktime)
66                         {
67                                 locked = false;
68                                 return false;
69                         }
70                         else
71                         {
72                                 return true;
73                         }
74                 }
75                 return false;
76         }
77
78         void lock()
79         {
80                 locked = true;
81                 unlocktime = ServerInstance->Time() + 60;
82         }
83
84 };
85
86 /** Handles channel mode +j
87  */
88 class NickFlood : public ModeHandler
89 {
90  public:
91         SimpleExtItem<nickfloodsettings> ext;
92         NickFlood(Module* Creator) : ModeHandler(Creator, "nickflood", 'F', PARAM_SETONLY, MODETYPE_CHANNEL),
93                 ext("nickflood", Creator) { }
94
95         ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string &parameter)
96         {
97                 nickfloodsettings* x = ext.get(channel);
98                 if (x)
99                         return std::make_pair(true, ConvToStr(x->nicks)+":"+ConvToStr(x->secs));
100                 else
101                         return std::make_pair(false, parameter);
102         }
103
104         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
105         {
106                 nickfloodsettings *f = ext.get(channel);
107                 if (adding)
108                 {
109                         char ndata[MAXBUF];
110                         char* data = ndata;
111                         strlcpy(ndata,parameter.c_str(),MAXBUF);
112                         char* nicks = data;
113                         char* secs = NULL;
114                         while (*data)
115                         {
116                                 if (*data == ':')
117                                 {
118                                         *data = 0;
119                                         data++;
120                                         secs = data;
121                                         break;
122                                 }
123                                 else data++;
124                         }
125                         if (secs)
126                         {
127                                 /* Set up the flood parameters for this channel */
128                                 int nnicks = atoi(nicks);
129                                 int nsecs = atoi(secs);
130                                 if ((nnicks<1) || (nsecs<1))
131                                 {
132                                         source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
133                                         parameter.clear();
134                                         return MODEACTION_DENY;
135                                 }
136                                 else
137                                 {
138                                         if (!f)
139                                         {
140                                                 parameter = ConvToStr(nnicks) + ":" +ConvToStr(nsecs);
141                                                 f = new nickfloodsettings(nsecs, nnicks);
142                                                 ext.set(channel, f);
143                                                 channel->SetModeParam('F', parameter);
144                                                 return MODEACTION_ALLOW;
145                                         }
146                                         else
147                                         {
148                                                 std::string cur_param = channel->GetModeParameter('F');
149                                                 parameter = ConvToStr(nnicks) + ":" +ConvToStr(nsecs);
150                                                 if (cur_param == parameter)
151                                                 {
152                                                         // mode params match
153                                                         return MODEACTION_DENY;
154                                                 }
155                                                 else
156                                                 {
157                                                         // new mode param, replace old with new
158                                                         if ((nsecs > 0) && (nnicks > 0))
159                                                         {
160                                                                 f = new nickfloodsettings(nsecs, nnicks);
161                                                                 ext.set(channel, f);
162                                                                 channel->SetModeParam('F', parameter);
163                                                                 return MODEACTION_ALLOW;
164                                                         }
165                                                         else
166                                                         {
167                                                                 return MODEACTION_DENY;
168                                                         }
169                                                 }
170                                         }
171                                 }
172                         }
173                         else
174                         {
175                                 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
176                                 return MODEACTION_DENY;
177                         }
178                 }
179                 else
180                 {
181                         if (f)
182                         {
183                                 ext.unset(channel);
184                                 channel->SetModeParam('F', "");
185                                 return MODEACTION_ALLOW;
186                         }
187                 }
188                 return MODEACTION_DENY;
189         }
190 };
191
192 class ModuleNickFlood : public Module
193 {
194         NickFlood nf;
195
196  public:
197
198         ModuleNickFlood()
199                 : nf(this)
200         {
201                 if (!ServerInstance->Modes->AddMode(&nf))
202                         throw ModuleException("Could not add new modes!");
203                 ServerInstance->Extensions.Register(&nf.ext);
204                 Implementation eventlist[] = { I_OnUserPreNick, I_OnUserPostNick };
205                 ServerInstance->Modules->Attach(eventlist, this, 2);
206         }
207
208         ModResult OnUserPreNick(User* user, const std::string &newnick)
209         {
210                 if (isdigit(newnick[0])) /* allow switches to UID */
211                         return MOD_RES_PASSTHRU;
212
213                 for (UCListIter i = user->chans.begin(); i != user->chans.end(); i++)
214                 {
215                         Channel *channel = *i;
216                         ModResult res;
217
218                         nickfloodsettings *f = nf.ext.get(channel);
219                         if (f)
220                         {
221                                 FIRST_MOD_RESULT(OnChannelRestrictionApply, res, (user,channel,"nickflood"));
222                                 if (res == MOD_RES_ALLOW)
223                                         continue;
224
225                                 if (f->islocked())
226                                 {
227                                         user->WriteNumeric(447, "%s :%s has been locked for nickchanges for 60 seconds because there have been more than %d nick changes in %d seconds", user->nick.c_str(), channel->name.c_str(), f->nicks, f->secs);
228                                         return MOD_RES_DENY;
229                                 }
230
231                                 if (f->shouldlock())
232                                 {
233                                         f->clear();
234                                         f->lock();
235                                         channel->WriteChannelWithServ((char*)ServerInstance->Config->ServerName.c_str(), "NOTICE %s :No nick changes are allowed for 60 seconds because there have been more than %d nick changes in %d seconds.", channel->name.c_str(), f->nicks, f->secs);
236                                         return MOD_RES_DENY;
237                                 }
238                         }
239                 }
240
241                 return MOD_RES_PASSTHRU;
242         }
243
244         /*
245          * 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.
246          */
247         void OnUserPostNick(User* user, const std::string &oldnick)
248         {
249                 if (isdigit(user->nick[0])) /* allow switches to UID */
250                         return;
251
252                 for (UCListIter i = user->chans.begin(); i != user->chans.end(); ++i)
253                 {
254                         Channel *channel = *i;
255                         ModResult res;
256
257                         nickfloodsettings *f = nf.ext.get(channel);
258                         if (f)
259                         {
260                                 FIRST_MOD_RESULT(OnChannelRestrictionApply, res, (user,channel,"nickflood"));
261                                 if (res == MOD_RES_ALLOW)
262                                         return;
263                                 
264                                 /* moved this here to avoid incrementing the counter for nick
265                                  * changes that are denied for some other reason (bans, +N, etc.)
266                                  * per bug #874.
267                                  */
268                                 f->addnick();
269                         }
270                 }
271                 return;
272         }
273
274         ~ModuleNickFlood()
275         {
276         }
277
278         Version GetVersion()
279         {
280                 return Version("Channel mode F - nick flood protection", VF_COMMON | VF_VENDOR);
281         }
282 };
283
284 MODULE_INIT(ModuleNickFlood)