]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_nickflood.cpp
e281713c3c45ae631f24b9a74b1511992905ec7e
[user/henk/code/inspircd.git] / src / modules / m_nickflood.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/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                 return (counter >= this->nicks);
52         }
53
54         void clear()
55         {
56                 counter = 0;
57         }
58
59         bool islocked()
60         {
61                 if (locked)
62                 {
63                         if (ServerInstance->Time() > unlocktime)
64                         {
65                                 locked = false;
66                                 return false;
67                         }
68                         else
69                         {
70                                 return true;
71                         }
72                 }
73                 return false;
74         }
75
76         void lock()
77         {
78                 locked = true;
79                 unlocktime = ServerInstance->Time() + 60;
80         }
81
82 };
83
84 /** Handles channel mode +j
85  */
86 class NickFlood : public ModeHandler
87 {
88  public:
89         NickFlood(InspIRCd* Instance) : ModeHandler(Instance, 'F', 1, 0, false, MODETYPE_CHANNEL, false) { }
90
91         ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string &parameter)
92         {
93                 nickfloodsettings* x;
94                 if (channel->GetExt("nickflood",x))
95                         return std::make_pair(true, ConvToStr(x->nicks)+":"+ConvToStr(x->secs));
96                 else
97                         return std::make_pair(false, parameter);
98         }
99
100         bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, Channel* channel)
101         {
102                 /* When TS is equal, the alphabetically later one wins */
103                 return (their_param < our_param);
104         }
105
106         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding, bool)
107         {
108                 nickfloodsettings* dummy;
109
110                 if (adding)
111                 {
112                         char ndata[MAXBUF];
113                         char* data = ndata;
114                         strlcpy(ndata,parameter.c_str(),MAXBUF);
115                         char* nicks = data;
116                         char* secs = NULL;
117                         while (*data)
118                         {
119                                 if (*data == ':')
120                                 {
121                                         *data = 0;
122                                         data++;
123                                         secs = data;
124                                         break;
125                                 }
126                                 else data++;
127                         }
128                         if (secs)
129
130                         {
131                                 /* Set up the flood parameters for this channel */
132                                 int nnicks = atoi(nicks);
133                                 int nsecs = atoi(secs);
134                                 if ((nnicks<1) || (nsecs<1))
135                                 {
136                                         source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
137                                         parameter.clear();
138                                         return MODEACTION_DENY;
139                                 }
140                                 else
141                                 {
142                                         if (!channel->GetExt("nickflood", dummy))
143                                         {
144                                                 parameter = ConvToStr(nnicks) + ":" +ConvToStr(nsecs);
145                                                 nickfloodsettings *f = new nickfloodsettings(ServerInstance, nsecs, nnicks);
146                                                 channel->Extend("nickflood", f);
147                                                 channel->SetMode('F', true);
148                                                 channel->SetModeParam('F', parameter.c_str(), true);
149                                                 return MODEACTION_ALLOW;
150                                         }
151                                         else
152                                         {
153                                                 std::string cur_param = channel->GetModeParameter('F');
154                                                 parameter = ConvToStr(nnicks) + ":" +ConvToStr(nsecs);
155                                                 if (cur_param == parameter)
156                                                 {
157                                                         // mode params match
158                                                         return MODEACTION_DENY;
159                                                 }
160                                                 else
161                                                 {
162                                                         // new mode param, replace old with new
163                                                         if ((nsecs > 0) && (nnicks > 0))
164                                                         {
165                                                                 nickfloodsettings* f;
166                                                                 channel->GetExt("nickflood", f);
167                                                                 delete f;
168                                                                 f = new nickfloodsettings(ServerInstance, nsecs, nnicks);
169                                                                 channel->Shrink("nickflood");
170                                                                 channel->Extend("nickflood", f);
171                                                                 channel->SetModeParam('F', cur_param.c_str(), false);
172                                                                 channel->SetModeParam('F', parameter.c_str(), true);
173                                                                 return MODEACTION_ALLOW;
174                                                         }
175                                                         else
176                                                         {
177                                                                 return MODEACTION_DENY;
178                                                         }
179                                                 }
180                                         }
181                                 }
182                         }
183                         else
184                         {
185                                 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
186                                 return MODEACTION_DENY;
187                         }
188                 }
189                 else
190                 {
191                         if (channel->GetExt("nickflood", dummy))
192                         {
193                                 nickfloodsettings *f;
194                                 channel->GetExt("nickflood", f);
195                                 delete f;
196                                 channel->Shrink("nickflood");
197                                 channel->SetMode('F', false);
198                                 return MODEACTION_ALLOW;
199                         }
200                 }
201                 return MODEACTION_DENY;
202         }
203 };
204
205 class ModuleNickFlood : public Module
206 {
207         NickFlood* jf;
208
209  public:
210
211         ModuleNickFlood(InspIRCd* Me)
212                 : Module(Me)
213         {
214
215                 jf = new NickFlood(ServerInstance);
216                 if (!ServerInstance->Modes->AddMode(jf))
217                         throw ModuleException("Could not add new modes!");
218                 Implementation eventlist[] = { I_OnChannelDelete, I_OnUserPreNick };
219                 ServerInstance->Modules->Attach(eventlist, this, 2);
220         }
221
222         virtual int OnUserPreNick(User* user, const std::string &newnick)
223         {
224                 if (isdigit(newnick[0])) /* allow switches to UID */
225                         return 0;
226
227                 for (UCListIter i = user->chans.begin(); i != user->chans.end(); i++)
228                 {
229                         Channel *channel = i->first;
230
231                         nickfloodsettings *f;
232                         if (channel->GetExt("nickflood", f))
233                         {
234                                 if (CHANOPS_EXEMPT(ServerInstance, 'F') && channel->GetStatus(user) == STATUS_OP)
235                                         continue;
236
237                                 if (f->islocked())
238                                 {
239                                         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);
240                                         return 1;
241                                 }
242
243                                 f->addnick();
244                                 if (f->shouldlock())
245                                 {
246                                         f->clear();
247                                         f->lock();
248                                         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);
249                                         return 1;
250                                 }
251                         }
252                 }
253
254                 return 0;
255         }
256
257         void OnChannelDelete(Channel* chan)
258         {
259                 nickfloodsettings *f;
260                 if (chan->GetExt("nickflood",f))
261                 {
262                         delete f;
263                         chan->Shrink("nickflood");
264                 }
265         }
266
267
268         virtual ~ModuleNickFlood()
269         {
270                 ServerInstance->Modes->DelMode(jf);
271                 delete jf;
272         }
273
274         virtual Version GetVersion()
275         {
276                 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
277         }
278 };
279
280 MODULE_INIT(ModuleNickFlood)