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