]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_nickflood.cpp
708d30e6beee622155de397b8c964a2f2f6def2b
[user/henk/code/inspircd.git] / src / modules / m_nickflood.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 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         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
96         {
97                 nickfloodsettings *f = ext.get(channel);
98                 if (adding)
99                 {
100                         char ndata[MAXBUF];
101                         char* data = ndata;
102                         strlcpy(ndata,parameter.c_str(),MAXBUF);
103                         char* nicks = data;
104                         char* secs = NULL;
105                         while (*data)
106                         {
107                                 if (*data == ':')
108                                 {
109                                         *data = 0;
110                                         data++;
111                                         secs = data;
112                                         break;
113                                 }
114                                 else data++;
115                         }
116                         if (secs)
117                         {
118                                 /* Set up the flood parameters for this channel */
119                                 int nnicks = atoi(nicks);
120                                 int nsecs = atoi(secs);
121                                 if ((nnicks<1) || (nsecs<1))
122                                 {
123                                         source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
124                                         parameter.clear();
125                                         return MODEACTION_DENY;
126                                 }
127                                 else
128                                 {
129                                         if (!f)
130                                         {
131                                                 parameter = ConvToStr(nnicks) + ":" +ConvToStr(nsecs);
132                                                 f = new nickfloodsettings(nsecs, nnicks);
133                                                 ext.set(channel, f);
134                                                 channel->SetModeParam('F', parameter);
135                                                 return MODEACTION_ALLOW;
136                                         }
137                                         else
138                                         {
139                                                 std::string cur_param = channel->GetModeParameter('F');
140                                                 parameter = ConvToStr(nnicks) + ":" +ConvToStr(nsecs);
141                                                 if (cur_param == parameter)
142                                                 {
143                                                         // mode params match
144                                                         return MODEACTION_DENY;
145                                                 }
146                                                 else
147                                                 {
148                                                         // new mode param, replace old with new
149                                                         if ((nsecs > 0) && (nnicks > 0))
150                                                         {
151                                                                 f = new nickfloodsettings(nsecs, nnicks);
152                                                                 ext.set(channel, f);
153                                                                 channel->SetModeParam('F', parameter);
154                                                                 return MODEACTION_ALLOW;
155                                                         }
156                                                         else
157                                                         {
158                                                                 return MODEACTION_DENY;
159                                                         }
160                                                 }
161                                         }
162                                 }
163                         }
164                         else
165                         {
166                                 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
167                                 return MODEACTION_DENY;
168                         }
169                 }
170                 else
171                 {
172                         if (f)
173                         {
174                                 ext.unset(channel);
175                                 channel->SetModeParam('F', "");
176                                 return MODEACTION_ALLOW;
177                         }
178                 }
179                 return MODEACTION_DENY;
180         }
181 };
182
183 class ModuleNickFlood : public Module
184 {
185         NickFlood nf;
186
187  public:
188
189         ModuleNickFlood()
190                 : nf(this)
191         {
192                 if (!ServerInstance->Modes->AddMode(&nf))
193                         throw ModuleException("Could not add new modes!");
194                 ServerInstance->Extensions.Register(&nf.ext);
195                 Implementation eventlist[] = { I_OnUserPreNick, I_OnUserPostNick };
196                 ServerInstance->Modules->Attach(eventlist, this, 2);
197         }
198
199         ModResult OnUserPreNick(User* user, const std::string &newnick)
200         {
201                 if (ServerInstance->NICKForced.get(user)) /* Allow forced nick changes */
202                         return MOD_RES_PASSTHRU;
203
204                 for (UCListIter i = user->chans.begin(); i != user->chans.end(); i++)
205                 {
206                         Channel *channel = *i;
207                         ModResult res;
208
209                         nickfloodsettings *f = nf.ext.get(channel);
210                         if (f)
211                         {
212                                 res = ServerInstance->OnCheckExemption(user,channel,"nickflood");
213                                 if (res == MOD_RES_ALLOW)
214                                         continue;
215
216                                 if (f->islocked())
217                                 {
218                                         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);
219                                         return MOD_RES_DENY;
220                                 }
221
222                                 if (f->shouldlock())
223                                 {
224                                         f->clear();
225                                         f->lock();
226                                         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);
227                                         return MOD_RES_DENY;
228                                 }
229                         }
230                 }
231
232                 return MOD_RES_PASSTHRU;
233         }
234
235         /*
236          * 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.
237          */
238         void OnUserPostNick(User* user, const std::string &oldnick)
239         {
240                 if (isdigit(user->nick[0])) /* allow switches to UID */
241                         return;
242
243                 for (UCListIter i = user->chans.begin(); i != user->chans.end(); ++i)
244                 {
245                         Channel *channel = *i;
246                         ModResult res;
247
248                         nickfloodsettings *f = nf.ext.get(channel);
249                         if (f)
250                         {
251                                 res = ServerInstance->OnCheckExemption(user,channel,"nickflood");
252                                 if (res == MOD_RES_ALLOW)
253                                         return;
254                                 
255                                 /* moved this here to avoid incrementing the counter for nick
256                                  * changes that are denied for some other reason (bans, +N, etc.)
257                                  * per bug #874.
258                                  */
259                                 f->addnick();
260                         }
261                 }
262                 return;
263         }
264
265         ~ModuleNickFlood()
266         {
267         }
268
269         Version GetVersion()
270         {
271                 return Version("Channel mode F - nick flood protection", VF_VENDOR);
272         }
273 };
274
275 MODULE_INIT(ModuleNickFlood)