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