]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_nickflood.cpp
Remove "servermode" parameter, replace with IS_FAKE() which is more reliable
[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, Module* Creator) : ModeHandler(Instance, Creator, '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         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
105         {
106                 nickfloodsettings* dummy;
107
108                 if (adding)
109                 {
110                         char ndata[MAXBUF];
111                         char* data = ndata;
112                         strlcpy(ndata,parameter.c_str(),MAXBUF);
113                         char* nicks = data;
114                         char* secs = NULL;
115                         while (*data)
116                         {
117                                 if (*data == ':')
118                                 {
119                                         *data = 0;
120                                         data++;
121                                         secs = data;
122                                         break;
123                                 }
124                                 else data++;
125                         }
126                         if (secs)
127
128                         {
129                                 /* Set up the flood parameters for this channel */
130                                 int nnicks = atoi(nicks);
131                                 int nsecs = atoi(secs);
132                                 if ((nnicks<1) || (nsecs<1))
133                                 {
134                                         source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
135                                         parameter.clear();
136                                         return MODEACTION_DENY;
137                                 }
138                                 else
139                                 {
140                                         if (!channel->GetExt("nickflood", dummy))
141                                         {
142                                                 parameter = ConvToStr(nnicks) + ":" +ConvToStr(nsecs);
143                                                 nickfloodsettings *f = new nickfloodsettings(ServerInstance, nsecs, nnicks);
144                                                 channel->Extend("nickflood", f);
145                                                 channel->SetModeParam('F', parameter);
146                                                 return MODEACTION_ALLOW;
147                                         }
148                                         else
149                                         {
150                                                 std::string cur_param = channel->GetModeParameter('F');
151                                                 parameter = ConvToStr(nnicks) + ":" +ConvToStr(nsecs);
152                                                 if (cur_param == parameter)
153                                                 {
154                                                         // mode params match
155                                                         return MODEACTION_DENY;
156                                                 }
157                                                 else
158                                                 {
159                                                         // new mode param, replace old with new
160                                                         if ((nsecs > 0) && (nnicks > 0))
161                                                         {
162                                                                 nickfloodsettings* f;
163                                                                 channel->GetExt("nickflood", f);
164                                                                 delete f;
165                                                                 f = new nickfloodsettings(ServerInstance, nsecs, nnicks);
166                                                                 channel->Shrink("nickflood");
167                                                                 channel->Extend("nickflood", f);
168                                                                 channel->SetModeParam('F', parameter);
169                                                                 return MODEACTION_ALLOW;
170                                                         }
171                                                         else
172                                                         {
173                                                                 return MODEACTION_DENY;
174                                                         }
175                                                 }
176                                         }
177                                 }
178                         }
179                         else
180                         {
181                                 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
182                                 return MODEACTION_DENY;
183                         }
184                 }
185                 else
186                 {
187                         if (channel->GetExt("nickflood", dummy))
188                         {
189                                 nickfloodsettings *f;
190                                 channel->GetExt("nickflood", f);
191                                 delete f;
192                                 channel->Shrink("nickflood");
193                                 channel->SetModeParam('F', "");
194                                 return MODEACTION_ALLOW;
195                         }
196                 }
197                 return MODEACTION_DENY;
198         }
199 };
200
201 class ModuleNickFlood : public Module
202 {
203         NickFlood jf;
204
205  public:
206
207         ModuleNickFlood(InspIRCd* Me)
208                 : Module(Me), jf(Me, this)
209         {
210                 if (!ServerInstance->Modes->AddMode(&jf))
211                         throw ModuleException("Could not add new modes!");
212                 Implementation eventlist[] = { I_OnChannelDelete, I_OnUserPreNick, I_OnUserPostNick };
213                 ServerInstance->Modules->Attach(eventlist, this, 3);
214         }
215
216         virtual int OnUserPreNick(User* user, const std::string &newnick)
217         {
218                 if (isdigit(newnick[0])) /* allow switches to UID */
219                         return 0;
220
221                 for (UCListIter i = user->chans.begin(); i != user->chans.end(); i++)
222                 {
223                         Channel *channel = i->first;
224
225                         nickfloodsettings *f;
226                         if (channel->GetExt("nickflood", f))
227                         {
228                                 if (CHANOPS_EXEMPT(ServerInstance, 'F') && channel->GetStatus(user) == STATUS_OP)
229                                         continue;
230
231                                 if (f->islocked())
232                                 {
233                                         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);
234                                         return 1;
235                                 }
236
237                                 if (f->shouldlock())
238                                 {
239                                         f->clear();
240                                         f->lock();
241                                         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);
242                                         return 1;
243                                 }
244                         }
245                 }
246
247                 return 0;
248         }
249
250         /*
251          * 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.
252          */
253         virtual void OnUserPostNick(User* user, const std::string &oldnick)
254         {
255                 if (isdigit(user->nick[0])) /* allow switches to UID */
256                         return;
257
258                 for (UCListIter i = user->chans.begin(); i != user->chans.end(); ++i)
259                 {
260                         Channel *channel = i->first;
261
262                         nickfloodsettings *f;
263                         if (channel->GetExt("nickflood", f))
264                         {
265                                 if (CHANOPS_EXEMPT(ServerInstance, 'F') && channel->GetStatus(user) == STATUS_OP)
266                                         return;
267                                 
268                                 /* moved this here to avoid incrementing the counter for nick
269                                  * changes that are denied for some other reason (bans, +N, etc.)
270                                  * per bug #874.
271                                  */
272                                 f->addnick();
273                         }
274                 }
275                 return;
276         }
277
278         void OnChannelDelete(Channel* chan)
279         {
280                 nickfloodsettings *f;
281                 if (chan->GetExt("nickflood",f))
282                 {
283                         delete f;
284                         chan->Shrink("nickflood");
285                 }
286         }
287
288
289         virtual ~ModuleNickFlood()
290         {
291                 ServerInstance->Modes->DelMode(&jf);
292         }
293
294         virtual Version GetVersion()
295         {
296                 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
297         }
298 };
299
300 MODULE_INIT(ModuleNickFlood)