]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_nickflood.cpp
5a59814d4b7fd1c04511649962fc71c212be7db3
[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         SimpleExtItem<nickfloodsettings> ext;
94         NickFlood(InspIRCd* Instance, Module* Creator) : ModeHandler(Creator, 'F', PARAM_SETONLY, MODETYPE_CHANNEL),
95                 ext("nickflood", Creator) { }
96
97         ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string &parameter)
98         {
99                 nickfloodsettings* x = ext.get(channel);
100                 if (x)
101                         return std::make_pair(true, ConvToStr(x->nicks)+":"+ConvToStr(x->secs));
102                 else
103                         return std::make_pair(false, parameter);
104         }
105
106         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
107         {
108                 nickfloodsettings *f = ext.get(channel);
109                 if (adding)
110                 {
111                         char ndata[MAXBUF];
112                         char* data = ndata;
113                         strlcpy(ndata,parameter.c_str(),MAXBUF);
114                         char* nicks = data;
115                         char* secs = NULL;
116                         while (*data)
117                         {
118                                 if (*data == ':')
119                                 {
120                                         *data = 0;
121                                         data++;
122                                         secs = data;
123                                         break;
124                                 }
125                                 else data++;
126                         }
127                         if (secs)
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 (!f)
141                                         {
142                                                 parameter = ConvToStr(nnicks) + ":" +ConvToStr(nsecs);
143                                                 f = new nickfloodsettings(ServerInstance, nsecs, nnicks);
144                                                 ext.set(channel, 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                                                                 f = new nickfloodsettings(ServerInstance, nsecs, nnicks);
163                                                                 ext.set(channel, f);
164                                                                 channel->SetModeParam('F', parameter);
165                                                                 return MODEACTION_ALLOW;
166                                                         }
167                                                         else
168                                                         {
169                                                                 return MODEACTION_DENY;
170                                                         }
171                                                 }
172                                         }
173                                 }
174                         }
175                         else
176                         {
177                                 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
178                                 return MODEACTION_DENY;
179                         }
180                 }
181                 else
182                 {
183                         if (f)
184                         {
185                                 ext.unset(channel);
186                                 channel->SetModeParam('F', "");
187                                 return MODEACTION_ALLOW;
188                         }
189                 }
190                 return MODEACTION_DENY;
191         }
192 };
193
194 class ModuleNickFlood : public Module
195 {
196         NickFlood nf;
197
198  public:
199
200         ModuleNickFlood(InspIRCd* Me)
201                 : Module(Me), nf(Me, this)
202         {
203                 if (!ServerInstance->Modes->AddMode(&nf))
204                         throw ModuleException("Could not add new modes!");
205                 Extensible::Register(&nf.ext);
206                 Implementation eventlist[] = { I_OnUserPreNick, I_OnUserPostNick };
207                 ServerInstance->Modules->Attach(eventlist, this, 2);
208         }
209
210         ModResult OnUserPreNick(User* user, const std::string &newnick)
211         {
212                 if (isdigit(newnick[0])) /* allow switches to UID */
213                         return MOD_RES_PASSTHRU;
214
215                 for (UCListIter i = user->chans.begin(); i != user->chans.end(); i++)
216                 {
217                         Channel *channel = *i;
218
219                         nickfloodsettings *f = nf.ext.get(channel);
220                         if (f)
221                         {
222                                 if (CHANOPS_EXEMPT(ServerInstance, 'F') && channel->GetPrefixValue(user) == OP_VALUE)
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, "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
256                         nickfloodsettings *f = nf.ext.get(channel);
257                         if (f)
258                         {
259                                 if (CHANOPS_EXEMPT(ServerInstance, 'F') && channel->GetPrefixValue(user) == OP_VALUE)
260                                         return;
261                                 
262                                 /* moved this here to avoid incrementing the counter for nick
263                                  * changes that are denied for some other reason (bans, +N, etc.)
264                                  * per bug #874.
265                                  */
266                                 f->addnick();
267                         }
268                 }
269                 return;
270         }
271
272         ~ModuleNickFlood()
273         {
274                 ServerInstance->Modes->DelMode(&nf);
275         }
276
277         Version GetVersion()
278         {
279                 return Version("Channel mode F - nick flood protection", VF_COMMON | VF_VENDOR);
280         }
281 };
282
283 MODULE_INIT(ModuleNickFlood)