]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_joinflood.cpp
Add debug to m_spanningtree, verifies that inspircd works correctly on FMODE (after...
[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
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         joinfloodsettings(int b, int c) : secs(b), joins(c)
44         {
45                 reset = time(NULL) + secs;
46                 counter = 0;
47                 locked = false;
48         };
49
50         void addjoin()
51         {
52                 counter++;
53                 if (time(NULL) > reset)
54                 {
55                         counter = 0;
56                         reset = time(NULL) + secs;
57                 }
58         }
59
60         bool shouldlock()
61         {
62                 return (counter >= this->joins);
63         }
64
65         void clear()
66         {
67                 counter = 0;
68         }
69
70         bool islocked()
71         {
72                 if (locked)
73                 {
74                         if (time(NULL) > unlocktime)
75                         {
76                                 locked = false;
77                                 return false;
78                         }
79                         else
80                         {
81                                 return true;
82                         }
83                 }
84                 return false;
85         }
86
87         void lock()
88         {
89                 locked = true;
90                 unlocktime = time(NULL) + 60;
91         }
92
93 };
94
95 class JoinFlood : public ModeHandler
96 {
97  public:
98         JoinFlood(InspIRCd* Instance) : ModeHandler(Instance, 'j', 1, 0, false, MODETYPE_CHANNEL, false) { }
99
100         ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string &parameter)
101         {
102                 joinfloodsettings* x;
103                 if (channel->GetExt("joinflood",x))
104                         return std::make_pair(true, ConvToStr(x->joins)+":"+ConvToStr(x->secs));
105                 else
106                         return std::make_pair(false, parameter);
107         } 
108
109         bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
110         {
111                 /* When TS is equal, the alphabetically later one wins */
112                 return (their_param < our_param);
113         }
114
115         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
116         {
117                 joinfloodsettings* dummy;
118
119                 if (adding)
120                 {
121                         ServerInstance->Log(DEBUG,"Got parameter: '%s'",parameter.c_str());
122                         char ndata[MAXBUF];
123                         char* data = ndata;
124                         strlcpy(ndata,parameter.c_str(),MAXBUF);
125                         char* joins = data;
126                         char* secs = NULL;
127                         while (*data)
128                         {
129                                 if (*data == ':')
130                                 {
131                                         *data = 0;
132                                         data++;
133                                         secs = data;
134                                         break;
135                                 }
136                                 else data++;
137                         }
138                         if (secs)
139                         {
140                                 /* Set up the flood parameters for this channel */
141                                 int njoins = atoi(joins);
142                                 int nsecs = atoi(secs);
143                                 if ((njoins<1) || (nsecs<1))
144                                 {
145                                         source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
146                                         parameter = "";
147                                         return MODEACTION_DENY;
148                                 }
149                                 else
150                                 {
151                                         if (!channel->GetExt("joinflood", dummy))
152                                         {
153                                                 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
154                                                 joinfloodsettings *f = new joinfloodsettings(nsecs,njoins);
155                                                 channel->Extend("joinflood", f);
156                                                 channel->SetMode('j', true);
157                                                 channel->SetModeParam('j', parameter.c_str(), true);
158                                                 return MODEACTION_ALLOW;
159                                         }
160                                         else
161                                         {
162                                                 std::string cur_param = channel->GetModeParameter('j');
163                                                 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
164                                                 if (cur_param == parameter)
165                                                 {
166                                                         // mode params match
167                                                         return MODEACTION_DENY;
168                                                 }
169                                                 else
170                                                 {
171                                                         // new mode param, replace old with new
172                                                         if ((nsecs > 0) && (njoins > 0))
173                                                         {
174                                                                 joinfloodsettings *f = new joinfloodsettings(nsecs,njoins);
175                                                                 channel->Shrink("joinflood");
176                                                                 channel->Extend("joinflood", f);
177                                                                 channel->SetModeParam('j', cur_param.c_str(), false);
178                                                                 channel->SetModeParam('j', parameter.c_str(), true);
179                                                                 return MODEACTION_ALLOW;
180                                                         }
181                                                         else
182                                                         {
183                                                                 return MODEACTION_DENY;
184                                                         }
185                                                 }
186                                         }
187                                 }
188                         }
189                         else
190                         {
191                                 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
192                                 return MODEACTION_DENY;
193                         }
194                 }
195                 else
196                 {
197                         if (channel->GetExt("joinflood", dummy))
198                         {
199                                 joinfloodsettings *f;
200                                 channel->GetExt("joinflood", f);
201                                 DELETE(f);
202                                 channel->Shrink("joinflood");
203                                 channel->SetMode('j', false);
204                                 return MODEACTION_ALLOW;
205                         }
206                 }
207                 return MODEACTION_DENY;
208         }
209 };
210
211 class ModuleJoinFlood : public Module
212 {
213         
214         JoinFlood* jf;
215         
216  public:
217  
218         ModuleJoinFlood(InspIRCd* Me)
219                 : Module::Module(Me)
220         {
221                 
222                 jf = new JoinFlood(ServerInstance);
223                 ServerInstance->AddMode(jf, 'j');
224         }
225         
226         virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname)
227         {
228                 if (chan)
229                 {
230                         joinfloodsettings *f;
231                         if (chan->GetExt("joinflood", f))
232                         {
233                                 if (f->islocked())
234                                 {
235                                         user->WriteServ("609 %s %s :This channel is temporarily unavailable (+j). Please try again later.",user->nick,chan->name);
236                                         return 1;
237                                 }
238                         }
239                 }
240                 return 0;
241         }
242
243         virtual void OnUserJoin(userrec* user, chanrec* channel)
244         {
245                 joinfloodsettings *f;
246                 if (channel->GetExt("joinflood",f))
247                 {
248                         f->addjoin();
249                         if (f->shouldlock())
250                         {
251                                 f->clear();
252                                 f->lock();
253                                 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);
254                         }
255                 }
256         }
257
258         void OnChannelDelete(chanrec* chan)
259         {
260                 joinfloodsettings *f;
261                 if (chan->GetExt("joinflood",f))
262                 {
263                         DELETE(f);
264                         chan->Shrink("joinflood");
265                 }
266         }
267
268         void Implements(char* List)
269         {
270                 List[I_OnChannelDelete] = List[I_OnUserPreJoin] = List[I_OnUserJoin] = 1;
271         }
272
273         virtual ~ModuleJoinFlood()
274         {
275                 ServerInstance->Modes->DelMode(jf);
276                 DELETE(jf);
277         }
278         
279         virtual Version GetVersion()
280         {
281                 return Version(1, 0, 0, 0, VF_COMMON | VF_VENDOR);
282         }
283 };
284
285
286 class ModuleJoinFloodFactory : public ModuleFactory
287 {
288  public:
289         ModuleJoinFloodFactory()
290         {
291         }
292         
293         ~ModuleJoinFloodFactory()
294         {
295         }
296         
297         virtual Module * CreateModule(InspIRCd* Me)
298         {
299                 return new ModuleJoinFlood(Me);
300         }
301         
302 };
303
304
305 extern "C" void * init_module( void )
306 {
307         return new ModuleJoinFloodFactory;
308 }
309