]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_joinflood.cpp
ServerConfig extern moved into class InspIRCd
[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 "helperfuncs.h"
25 #include "inspircd.h"
26
27 /* $ModDesc: Provides channel mode +j (join flood protection) */
28
29 class joinfloodsettings : public classbase
30 {
31  public:
32
33         int secs;
34         int joins;
35         time_t reset;
36         time_t unlocktime;
37         int counter;
38         bool locked;
39
40         joinfloodsettings() : secs(0), joins(0) {};
41         joinfloodsettings(int b, int c) : secs(b), joins(c)
42         {
43                 reset = time(NULL) + secs;
44                 counter = 0;
45                 locked = false;
46                 log(DEBUG,"Create new joinfloodsettings: %lu %lu",time(NULL),reset);
47         };
48
49         void addjoin()
50         {
51                 counter++;
52                 log(DEBUG,"joinflood counter is %d",counter);
53                 if (time(NULL) > reset)
54                 {
55                         log(DEBUG,"joinflood counter reset");
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                 log(DEBUG,"joinflood counter clear");
69                 counter = 0;
70         }
71
72         bool islocked()
73         {
74                 if (locked)
75                 {
76                         if (time(NULL) > 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                 log(DEBUG,"joinflood lock");
92                 locked = true;
93                 unlocktime = time(NULL) + 60;
94         }
95
96 };
97
98 class JoinFlood : public ModeHandler
99 {
100  public:
101         JoinFlood() : ModeHandler('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                         char ndata[MAXBUF];
125                         char* data = ndata;
126                         strlcpy(ndata,parameter.c_str(),MAXBUF);
127                         char* joins = data;
128                         char* secs = NULL;
129                         while (*data)
130                         {
131                                 if (*data == ':')
132                                 {
133                                         *data = 0;
134                                         data++;
135                                         secs = data;
136                                         break;
137                                 }
138                                 else data++;
139                         }
140                         if (secs)
141                         {
142                                 /* Set up the flood parameters for this channel */
143                                 int njoins = atoi(joins);
144                                 int nsecs = atoi(secs);
145                                 if ((njoins<1) || (nsecs<1))
146                                 {
147                                         source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
148                                         parameter = "";
149                                         return MODEACTION_DENY;
150                                 }
151                                 else
152                                 {
153                                         if (!channel->GetExt("joinflood", dummy))
154                                         {
155                                                 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
156                                                 joinfloodsettings *f = new joinfloodsettings(nsecs,njoins);
157                                                 channel->Extend("joinflood", f);
158                                                 channel->SetMode('j', true);
159                                                 channel->SetModeParam('j', parameter.c_str(), true);
160                                                 return MODEACTION_ALLOW;
161                                         }
162                                 }
163                         }
164                         else
165                         {
166                                 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
167                                 return MODEACTION_DENY;
168                         }
169                 }
170                 else
171                 {
172                         if (channel->GetExt("joinflood", dummy))
173                         {
174                                 joinfloodsettings *f;
175                                 channel->GetExt("joinflood", f);
176                                 DELETE(f);
177                                 channel->Shrink("joinflood");
178                                 channel->SetMode('j', false);
179                                 return MODEACTION_ALLOW;
180                         }
181                 }
182                 return MODEACTION_DENY;
183         }
184 };
185
186 class ModuleJoinFlood : public Module
187 {
188         Server *Srv;
189         JoinFlood* jf;
190         
191  public:
192  
193         ModuleJoinFlood(Server* Me)
194                 : Module::Module(Me)
195         {
196                 Srv = Me;
197                 jf = new JoinFlood();
198                 Srv->AddMode(jf, 'j');
199         }
200         
201         virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname)
202         {
203                 if (chan)
204                 {
205                         joinfloodsettings *f;
206                         if (chan->GetExt("joinflood", f))
207                         {
208                                 if (f->islocked())
209                                 {
210                                         user->WriteServ("609 %s %s :This channel is temporarily unavailable (+j). Please try again later.",user->nick,chan->name);
211                                         return 1;
212                                 }
213                         }
214                 }
215                 return 0;
216         }
217
218         virtual void OnUserJoin(userrec* user, chanrec* channel)
219         {
220                 joinfloodsettings *f;
221                 if (channel->GetExt("joinflood",f))
222                 {
223                         f->addjoin();
224                         if (f->shouldlock())
225                         {
226                                 f->clear();
227                                 f->lock();
228                                 channel->WriteChannelWithServ((char*)Srv->GetServerName().c_str(), "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);
229                         }
230                 }
231         }
232
233         void OnChannelDelete(chanrec* chan)
234         {
235                 joinfloodsettings *f;
236                 if (chan->GetExt("joinflood",f))
237                 {
238                         DELETE(f);
239                         chan->Shrink("joinflood");
240                 }
241         }
242
243         void Implements(char* List)
244         {
245                 List[I_On005Numeric] = List[I_OnChannelDelete] = List[I_OnUserPreJoin] = List[I_OnUserJoin] = 1;
246         }
247
248         virtual void On005Numeric(std::string &output)
249         {
250                 InsertMode(output, "j", 3);
251         }
252
253         virtual ~ModuleJoinFlood()
254         {
255                 DELETE(jf);
256         }
257         
258         virtual Version GetVersion()
259         {
260                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
261         }
262 };
263
264
265 class ModuleJoinFloodFactory : public ModuleFactory
266 {
267  public:
268         ModuleJoinFloodFactory()
269         {
270         }
271         
272         ~ModuleJoinFloodFactory()
273         {
274         }
275         
276         virtual Module * CreateModule(Server* Me)
277         {
278                 return new ModuleJoinFlood(Me);
279         }
280         
281 };
282
283
284 extern "C" void * init_module( void )
285 {
286         return new ModuleJoinFloodFactory;
287 }
288