]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_nickflood.cpp
In the grand tradition of huge fucking commits:
[user/henk/code/inspircd.git] / src / modules / m_nickflood.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 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  public:
23
24         int secs;
25         int nicks;
26         time_t reset;
27         time_t unlocktime;
28         int counter;
29         bool locked;
30         InspIRCd* ServerInstance;
31
32         nickfloodsettings() : secs(0), nicks(0) {};
33
34         nickfloodsettings(int b, int c) : secs(b), nicks(c)
35         {
36                 reset = time(NULL) + secs;
37                 counter = 0;
38                 locked = false;
39         };
40
41         void addnick()
42         {
43                 counter++;
44                 if (time(NULL) > reset)
45                 {
46                         counter = 0;
47                         reset = time(NULL) + secs;
48                 }
49         }
50
51         bool shouldlock()
52         {
53                 return (counter >= this->nicks);
54         }
55
56         void clear()
57         {
58                 counter = 0;
59         }
60
61         bool islocked()
62         {
63                 if (locked)
64                 {
65                         if (time(NULL) > 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 = time(NULL) + 60;
82         }
83
84 };
85
86 /** Handles channel mode +j
87  */
88 class NickFlood : public ModeHandler
89 {
90  public:
91         NickFlood(InspIRCd* Instance) : ModeHandler(Instance, 'F', 1, 0, false, MODETYPE_CHANNEL, false) { }
92
93         ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string &parameter)
94         {
95                 nickfloodsettings* x;
96                 if (channel->GetExt("nickflood",x))
97                         return std::make_pair(true, ConvToStr(x->nicks)+":"+ConvToStr(x->secs));
98                 else
99                         return std::make_pair(false, parameter);
100         } 
101
102         bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, Channel* channel)
103         {
104                 /* When TS is equal, the alphabetically later one wins */
105                 return (their_param < our_param);
106         }
107
108         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
109         {
110                 nickfloodsettings* dummy;
111
112                 if (adding)
113                 {
114                         char ndata[MAXBUF];
115                         char* data = ndata;
116                         strlcpy(ndata,parameter.c_str(),MAXBUF);
117                         char* nicks = data;
118                         char* secs = NULL;
119                         while (*data)
120                         {
121                                 if (*data == ':')
122                                 {
123                                         *data = 0;
124                                         data++;
125                                         secs = data;
126                                         break;
127                                 }
128                                 else data++;
129                         }
130                         if (secs)
131
132                         {
133                                 /* Set up the flood parameters for this channel */
134                                 int nnicks = atoi(nicks);
135                                 int nsecs = atoi(secs);
136                                 if ((nnicks<1) || (nsecs<1))
137                                 {
138                                         source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
139                                         parameter.clear();
140                                         return MODEACTION_DENY;
141                                 }
142                                 else
143                                 {
144                                         if (!channel->GetExt("nickflood", dummy))
145                                         {
146                                                 parameter = ConvToStr(nnicks) + ":" +ConvToStr(nsecs);
147                                                 nickfloodsettings *f = new nickfloodsettings(nsecs,nnicks);
148                                                 channel->Extend("nickflood", f);
149                                                 channel->SetMode('F', true);
150                                                 channel->SetModeParam('F', parameter.c_str(), true);
151                                                 return MODEACTION_ALLOW;
152                                         }
153                                         else
154                                         {
155                                                 std::string cur_param = channel->GetModeParameter('F');
156                                                 parameter = ConvToStr(nnicks) + ":" +ConvToStr(nsecs);
157                                                 if (cur_param == parameter)
158                                                 {
159                                                         // mode params match
160                                                         return MODEACTION_DENY;
161                                                 }
162                                                 else
163                                                 {
164                                                         // new mode param, replace old with new
165                                                         if ((nsecs > 0) && (nnicks > 0))
166                                                         {
167                                                                 nickfloodsettings* f;
168                                                                 channel->GetExt("nickflood", f);
169                                                                 delete f;
170                                                                 f = new nickfloodsettings(nsecs, nnicks);
171                                                                 channel->Shrink("nickflood");
172                                                                 channel->Extend("nickflood", f);
173                                                                 channel->SetModeParam('F', cur_param.c_str(), false);
174                                                                 channel->SetModeParam('F', parameter.c_str(), true);
175                                                                 return MODEACTION_ALLOW;
176                                                         }
177                                                         else
178                                                         {
179                                                                 return MODEACTION_DENY;
180                                                         }
181                                                 }
182                                         }
183                                 }
184                         }
185                         else
186                         {
187                                 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
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->SetMode('F', false);
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->AddMode(jf, 'F'))
219                         throw ModuleException("Could not add new modes!");
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->WriteServ("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, channel->name, 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, 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         void Implements(char* List)
268         {
269                 List[I_OnChannelDelete] = List[I_OnUserPreNick] = 1;
270         }
271
272         virtual ~ModuleNickFlood()
273         {
274                 ServerInstance->Modes->DelMode(jf);
275                 DELETE(jf);
276         }
277         
278         virtual Version GetVersion()
279         {
280                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
281         }
282 };
283
284 MODULE_INIT(ModuleNickFlood)