]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_nickflood.cpp
fcc84a414627d4b1c44269d5c74b80dd8928af59
[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 : public classbase
21 {
22  private:
23         InspIRCd* ServerInstance;
24  public:
25         int secs;
26         int nicks;
27         time_t reset;
28         time_t unlocktime;
29         int counter;
30         bool locked;
31
32         nickfloodsettings(InspIRCd *Instance, int b, int c) : ServerInstance(Instance), secs(b), nicks(c)
33         {
34                 reset = Instance->Time() + secs;
35                 counter = 0;
36                 locked = false;
37         };
38
39         void addnick()
40         {
41                 counter++;
42                 if (ServerInstance->Time() > reset)
43                 {
44                         counter = 0;
45                         reset = ServerInstance->Time() + secs;
46                 }
47         }
48
49         bool shouldlock()
50         {
51                 /* XXX HACK: using counter + 1 here now to allow the counter to only be incremented
52                  * on successful nick changes; this will be checked before the counter is
53                  * incremented.
54                  */
55                 return (counter + 1 >= this->nicks);
56         }
57
58         void clear()
59         {
60                 counter = 0;
61         }
62
63         bool islocked()
64         {
65                 if (locked)
66                 {
67                         if (ServerInstance->Time() > unlocktime)
68                         {
69                                 locked = false;
70                                 return false;
71                         }
72                         else
73                         {
74                                 return true;
75                         }
76                 }
77                 return false;
78         }
79
80         void lock()
81         {
82                 locked = true;
83                 unlocktime = ServerInstance->Time() + 60;
84         }
85
86 };
87
88 /** Handles channel mode +j
89  */
90 class NickFlood : public ModeHandler
91 {
92  public:
93         NickFlood(InspIRCd* Instance) : ModeHandler(Instance, 'F', 1, 0, false, MODETYPE_CHANNEL, false) { }
94
95         ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string &parameter)
96         {
97                 nickfloodsettings* x;
98                 if (channel->GetExt("nickflood",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         bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, Channel* channel)
105         {
106                 /* When TS is equal, the alphabetically later one wins */
107                 return (their_param < our_param);
108         }
109
110         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding, bool)
111         {
112                 nickfloodsettings* dummy;
113
114                 if (adding)
115                 {
116                         char ndata[MAXBUF];
117                         char* data = ndata;
118                         strlcpy(ndata,parameter.c_str(),MAXBUF);
119                         char* nicks = data;
120                         char* secs = NULL;
121                         while (*data)
122                         {
123                                 if (*data == ':')
124                                 {
125                                         *data = 0;
126                                         data++;
127                                         secs = data;
128                                         break;
129                                 }
130                                 else data++;
131                         }
132                         if (secs)
133
134                         {
135                                 /* Set up the flood parameters for this channel */
136                                 int nnicks = atoi(nicks);
137                                 int nsecs = atoi(secs);
138                                 if ((nnicks<1) || (nsecs<1))
139                                 {
140                                         source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
141                                         parameter.clear();
142                                         return MODEACTION_DENY;
143                                 }
144                                 else
145                                 {
146                                         if (!channel->GetExt("nickflood", dummy))
147                                         {
148                                                 parameter = ConvToStr(nnicks) + ":" +ConvToStr(nsecs);
149                                                 nickfloodsettings *f = new nickfloodsettings(ServerInstance, nsecs, nnicks);
150                                                 channel->Extend("nickflood", f);
151                                                 channel->SetModeParam('F', parameter);
152                                                 return MODEACTION_ALLOW;
153                                         }
154                                         else
155                                         {
156                                                 std::string cur_param = channel->GetModeParameter('F');
157                                                 parameter = ConvToStr(nnicks) + ":" +ConvToStr(nsecs);
158                                                 if (cur_param == parameter)
159                                                 {
160                                                         // mode params match
161                                                         return MODEACTION_DENY;
162                                                 }
163                                                 else
164                                                 {
165                                                         // new mode param, replace old with new
166                                                         if ((nsecs > 0) && (nnicks > 0))
167                                                         {
168                                                                 nickfloodsettings* f;
169                                                                 channel->GetExt("nickflood", f);
170                                                                 delete f;
171                                                                 f = new nickfloodsettings(ServerInstance, nsecs, nnicks);
172                                                                 channel->Shrink("nickflood");
173                                                                 channel->Extend("nickflood", f);
174                                                                 channel->SetModeParam('F', parameter);
175                                                                 return MODEACTION_ALLOW;
176                                                         }
177                                                         else
178                                                         {
179                                                                 return MODEACTION_DENY;
180                                                         }
181                                                 }
182                                         }
183                                 }
184                         }
185                         else
186                         {
187                                 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
188                                 return MODEACTION_DENY;
189                         }
190                 }
191                 else
192                 {
193                         if (channel->GetExt("nickflood", dummy))
194                         {
195                                 nickfloodsettings *f;
196                                 channel->GetExt("nickflood", f);
197                                 delete f;
198                                 channel->Shrink("nickflood");
199                                 channel->SetModeParam('F', "");
200                                 return MODEACTION_ALLOW;
201                         }
202                 }
203                 return MODEACTION_DENY;
204         }
205 };
206
207 class ModuleNickFlood : public Module
208 {
209         NickFlood* jf;
210
211  public:
212
213         ModuleNickFlood(InspIRCd* Me)
214                 : Module(Me)
215         {
216
217                 jf = new NickFlood(ServerInstance);
218                 if (!ServerInstance->Modes->AddMode(jf))
219                         throw ModuleException("Could not add new modes!");
220                 Implementation eventlist[] = { I_OnChannelDelete, I_OnUserPreNick, I_OnUserPostNick };
221                 ServerInstance->Modules->Attach(eventlist, this, 3);
222         }
223
224         virtual int OnUserPreNick(User* user, const std::string &newnick)
225         {
226                 if (isdigit(newnick[0])) /* allow switches to UID */
227                         return 0;
228
229                 for (UCListIter i = user->chans.begin(); i != user->chans.end(); i++)
230                 {
231                         Channel *channel = i->first;
232
233                         nickfloodsettings *f;
234                         if (channel->GetExt("nickflood", f))
235                         {
236                                 if (CHANOPS_EXEMPT(ServerInstance, 'F') && channel->GetStatus(user) == STATUS_OP)
237                                         continue;
238
239                                 if (f->islocked())
240                                 {
241                                         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);
242                                         return 1;
243                                 }
244
245                                 if (f->shouldlock())
246                                 {
247                                         f->clear();
248                                         f->lock();
249                                         channel->WriteChannelWithServ((char*)ServerInstance->Config->ServerName, "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);
250                                         return 1;
251                                 }
252                         }
253                 }
254
255                 return 0;
256         }
257
258         /*
259          * 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.
260          */
261         virtual void OnUserPostNick(User* user, const std::string oldnick)
262         {
263                 if (isdigit(user->nick[0])) /* allow switches to UID */
264                         return;
265
266                 for (UCListIter i = user->chans.begin(); i != user->chans.end(); ++i)
267                 {
268                         Channel *channel = i->first;
269
270                         nickfloodsettings *f;
271                         if (channel->GetExt("nickflood", f))
272                         {
273                                 if (CHANOPS_EXEMPT(ServerInstance, 'F') && channel->GetStatus(user) == STATUS_OP)
274                                         return;
275                                 
276                                 /* moved this here to avoid incrementing the counter for nick
277                                  * changes that are denied for some other reason (bans, +N, etc.)
278                                  * per bug #874.
279                                  */
280                                 f->addnick();
281                         }
282                 }
283                 return;
284         }
285
286         void OnChannelDelete(Channel* chan)
287         {
288                 nickfloodsettings *f;
289                 if (chan->GetExt("nickflood",f))
290                 {
291                         delete f;
292                         chan->Shrink("nickflood");
293                 }
294         }
295
296
297         virtual ~ModuleNickFlood()
298         {
299                 ServerInstance->Modes->DelMode(jf);
300                 delete jf;
301         }
302
303         virtual Version GetVersion()
304         {
305                 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
306         }
307 };
308
309 MODULE_INIT(ModuleNickFlood)