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