]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_joinflood.cpp
Fixed a rather obvious crashbug in m_spanningtree (FJOIN)
[user/henk/code/inspircd.git] / src / modules / m_joinflood.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include <stdio.h>
20 #include <map>
21 #include "users.h"
22 #include "channels.h"
23 #include "modules.h"
24 #include "configreader.h"
25 #include "inspircd.h"
26
27 /* $ModDesc: Provides channel mode +j (join flood protection) */
28
29 /** Holds settings and state associated with channel mode +j
30  */
31 class joinfloodsettings : public classbase
32 {
33  public:
34
35         int secs;
36         int joins;
37         time_t reset;
38         time_t unlocktime;
39         int counter;
40         bool locked;
41
42         joinfloodsettings() : secs(0), joins(0) {};
43
44         joinfloodsettings(int b, int c) : secs(b), joins(c)
45         {
46                 reset = time(NULL) + secs;
47                 counter = 0;
48                 locked = false;
49         };
50
51         void addjoin()
52         {
53                 counter++;
54                 if (time(NULL) > reset)
55                 {
56                         counter = 0;
57                         reset = time(NULL) + secs;
58                 }
59         }
60
61         bool shouldlock()
62         {
63                 return (counter >= this->joins);
64         }
65
66         void clear()
67         {
68                 counter = 0;
69         }
70
71         bool islocked()
72         {
73                 if (locked)
74                 {
75                         if (time(NULL) > unlocktime)
76                         {
77                                 locked = false;
78                                 return false;
79                         }
80                         else
81                         {
82                                 return true;
83                         }
84                 }
85                 return false;
86         }
87
88         void lock()
89         {
90                 locked = true;
91                 unlocktime = time(NULL) + 60;
92         }
93
94 };
95
96 /** Handles channel mode +j
97  */
98 class JoinFlood : public ModeHandler
99 {
100  public:
101         JoinFlood(InspIRCd* Instance) : ModeHandler(Instance, 'j', 1, 0, false, MODETYPE_CHANNEL, false) { }
102
103         ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string &parameter)
104         {
105                 joinfloodsettings* x;
106                 if (channel->GetExt("joinflood",x))
107                         return std::make_pair(true, ConvToStr(x->joins)+":"+ConvToStr(x->secs));
108                 else
109                         return std::make_pair(false, parameter);
110         } 
111
112         bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
113         {
114                 /* When TS is equal, the alphabetically later one wins */
115                 return (their_param < our_param);
116         }
117
118         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
119         {
120                 joinfloodsettings* dummy;
121
122                 if (adding)
123                 {
124                         ServerInstance->Log(DEBUG,"Got parameter: '%s'",parameter.c_str());
125                         char ndata[MAXBUF];
126                         char* data = ndata;
127                         strlcpy(ndata,parameter.c_str(),MAXBUF);
128                         char* joins = data;
129                         char* secs = NULL;
130                         while (*data)
131                         {
132                                 if (*data == ':')
133                                 {
134                                         *data = 0;
135                                         data++;
136                                         secs = data;
137                                         break;
138                                 }
139                                 else data++;
140                         }
141                         if (secs)
142                         {
143                                 /* Set up the flood parameters for this channel */
144                                 int njoins = atoi(joins);
145                                 int nsecs = atoi(secs);
146                                 if ((njoins<1) || (nsecs<1))
147                                 {
148                                         source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
149                                         parameter = "";
150                                         return MODEACTION_DENY;
151                                 }
152                                 else
153                                 {
154                                         if (!channel->GetExt("joinflood", dummy))
155                                         {
156                                                 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
157                                                 joinfloodsettings *f = new joinfloodsettings(nsecs,njoins);
158                                                 channel->Extend("joinflood", f);
159                                                 channel->SetMode('j', true);
160                                                 channel->SetModeParam('j', parameter.c_str(), true);
161                                                 return MODEACTION_ALLOW;
162                                         }
163                                         else
164                                         {
165                                                 std::string cur_param = channel->GetModeParameter('j');
166                                                 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
167                                                 if (cur_param == parameter)
168                                                 {
169                                                         // mode params match
170                                                         return MODEACTION_DENY;
171                                                 }
172                                                 else
173                                                 {
174                                                         // new mode param, replace old with new
175                                                         if ((nsecs > 0) && (njoins > 0))
176                                                         {
177                                                                 joinfloodsettings* f;
178                                                                 channel->GetExt("joinflood", f);
179                                                                 delete f;
180                                                                 f = new joinfloodsettings(nsecs,njoins);
181                                                                 channel->Shrink("joinflood");
182                                                                 channel->Extend("joinflood", f);
183                                                                 channel->SetModeParam('j', cur_param.c_str(), false);
184                                                                 channel->SetModeParam('j', parameter.c_str(), true);
185                                                                 return MODEACTION_ALLOW;
186                                                         }
187                                                         else
188                                                         {
189                                                                 return MODEACTION_DENY;
190                                                         }
191                                                 }
192                                         }
193                                 }
194                         }
195                         else
196                         {
197                                 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
198                                 return MODEACTION_DENY;
199                         }
200                 }
201                 else
202                 {
203                         if (channel->GetExt("joinflood", dummy))
204                         {
205                                 joinfloodsettings *f;
206                                 channel->GetExt("joinflood", f);
207                                 DELETE(f);
208                                 channel->Shrink("joinflood");
209                                 channel->SetMode('j', false);
210                                 return MODEACTION_ALLOW;
211                         }
212                 }
213                 return MODEACTION_DENY;
214         }
215 };
216
217 class ModuleJoinFlood : public Module
218 {
219         
220         JoinFlood* jf;
221         
222  public:
223  
224         ModuleJoinFlood(InspIRCd* Me)
225                 : Module::Module(Me)
226         {
227                 
228                 jf = new JoinFlood(ServerInstance);
229                 ServerInstance->AddMode(jf, 'j');
230         }
231         
232         virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname, std::string &privs)
233         {
234                 if (chan)
235                 {
236                         joinfloodsettings *f;
237                         if (chan->GetExt("joinflood", f))
238                         {
239                                 if (f->islocked())
240                                 {
241                                         user->WriteServ("609 %s %s :This channel is temporarily unavailable (+j). Please try again later.",user->nick,chan->name);
242                                         return 1;
243                                 }
244                         }
245                 }
246                 return 0;
247         }
248
249         virtual void OnUserJoin(userrec* user, chanrec* channel)
250         {
251                 joinfloodsettings *f;
252                 if (channel->GetExt("joinflood",f))
253                 {
254                         f->addjoin();
255                         if (f->shouldlock())
256                         {
257                                 f->clear();
258                                 f->lock();
259                                 channel->WriteChannelWithServ((char*)ServerInstance->Config->ServerName, "NOTICE %s :This channel has been closed to new users for 60 seconds because there have been more than %d joins in %d seconds.", channel->name, f->joins, f->secs);
260                         }
261                 }
262         }
263
264         void OnChannelDelete(chanrec* chan)
265         {
266                 joinfloodsettings *f;
267                 if (chan->GetExt("joinflood",f))
268                 {
269                         DELETE(f);
270                         chan->Shrink("joinflood");
271                 }
272         }
273
274         void Implements(char* List)
275         {
276                 List[I_OnChannelDelete] = List[I_OnUserPreJoin] = List[I_OnUserJoin] = 1;
277         }
278
279         virtual ~ModuleJoinFlood()
280         {
281                 ServerInstance->Modes->DelMode(jf);
282                 DELETE(jf);
283         }
284         
285         virtual Version GetVersion()
286         {
287                 return Version(1, 0, 0, 0, VF_COMMON | VF_VENDOR);
288         }
289 };
290
291
292 class ModuleJoinFloodFactory : public ModuleFactory
293 {
294  public:
295         ModuleJoinFloodFactory()
296         {
297         }
298         
299         ~ModuleJoinFloodFactory()
300         {
301         }
302         
303         virtual Module * CreateModule(InspIRCd* Me)
304         {
305                 return new ModuleJoinFlood(Me);
306         }
307         
308 };
309
310
311 extern "C" void * init_module( void )
312 {
313         return new ModuleJoinFloodFactory;
314 }
315