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