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