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