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