]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_joinflood.cpp
6cc54db685fa90a79409a5821bf865bfeb6a4a9a
[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                                                 joinfloodsettings *f = new joinfloodsettings(nsecs,njoins);
138                                                 channel->Extend("joinflood",(char*)f);
139                                                 return MODEACTION_ALLOW;
140                                         }
141                                 }
142                         }
143                         else
144                         {
145                                 WriteServ(source->fd,"608 %s %s :Invalid flood parameter",source->nick,channel->name);
146                                 return MODEACTION_DENY;
147                         }
148                 }
149                 else
150                 {
151                         if (channel->GetExt("joinflood"))
152                         {
153                                 joinfloodsettings *f = (joinfloodsettings*)channel->GetExt("joinflood");
154                                 DELETE(f);
155                                 channel->Shrink("joinflood");
156                                 return MODEACTION_ALLOW;
157                         }
158                 }
159                 return MODEACTION_DENY;
160         }
161 };
162
163 class ModuleJoinFlood : public Module
164 {
165         Server *Srv;
166         JoinFlood* jf;
167         
168  public:
169  
170         ModuleJoinFlood(Server* Me)
171                 : Module::Module(Me)
172         {
173                 Srv = Me;
174                 jf = new JoinFlood();
175                 Srv->AddMode(jf, 'j');
176         }
177         
178         virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname)
179         {
180                 if (chan)
181                 {
182                         joinfloodsettings *f = (joinfloodsettings*)chan->GetExt("joinflood");
183                         if (f)
184                         {
185                                 if (f->islocked())
186                                 {
187                                         WriteServ(user->fd,"609 %s %s :This channel is temporarily unavailable (+j). Please try again later.",user->nick,chan->name);
188                                         return 1;
189                                 }
190                         }
191                 }
192                 return 0;
193         }
194
195         virtual void OnUserJoin(userrec* user, chanrec* channel)
196         {
197                 joinfloodsettings *f = (joinfloodsettings*)channel->GetExt("joinflood");
198                 if (f)
199                 {
200                         f->addjoin();
201                         if (f->shouldlock())
202                         {
203                                 f->clear();
204                                 f->lock();
205                                 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);
206                         }
207                 }
208         }
209
210         void OnChannelDelete(chanrec* chan)
211         {
212                 if (chan->GetExt("joinflood"))
213                 {
214                         joinfloodsettings *f = (joinfloodsettings*)chan->GetExt("joinflood");
215                         DELETE(f);
216                         chan->Shrink("joinflood");
217                 }
218         }
219
220         void Implements(char* List)
221         {
222                 List[I_On005Numeric] = List[I_OnChannelDelete] = List[I_OnUserPreJoin] = List[I_OnUserJoin] = 1;
223         }
224
225         virtual void On005Numeric(std::string &output)
226         {
227                 InsertMode(output, "j", 3);
228         }
229
230         virtual ~ModuleJoinFlood()
231         {
232                 DELETE(jf);
233         }
234         
235         virtual Version GetVersion()
236         {
237                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
238         }
239 };
240
241
242 class ModuleJoinFloodFactory : public ModuleFactory
243 {
244  public:
245         ModuleJoinFloodFactory()
246         {
247         }
248         
249         ~ModuleJoinFloodFactory()
250         {
251         }
252         
253         virtual Module * CreateModule(Server* Me)
254         {
255                 return new ModuleJoinFlood(Me);
256         }
257         
258 };
259
260
261 extern "C" void * init_module( void )
262 {
263         return new ModuleJoinFloodFactory;
264 }
265