]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_joinflood.cpp
Because these modes take no parameter when being removed, they need no special code...
[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                         ServerInstance->Log(DEBUG,"Got parameter: '%s'",parameter.c_str());
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_OnChannelDelete] = List[I_OnUserPreJoin] = List[I_OnUserJoin] = 1;
244         }
245
246         virtual ~ModuleJoinFlood()
247         {
248                 ServerInstance->Modes->DelMode(jf);
249                 DELETE(jf);
250         }
251         
252         virtual Version GetVersion()
253         {
254                 return Version(1, 0, 0, 0, VF_COMMON | VF_VENDOR);
255         }
256 };
257
258
259 class ModuleJoinFloodFactory : public ModuleFactory
260 {
261  public:
262         ModuleJoinFloodFactory()
263         {
264         }
265         
266         ~ModuleJoinFloodFactory()
267         {
268         }
269         
270         virtual Module * CreateModule(InspIRCd* Me)
271         {
272                 return new ModuleJoinFlood(Me);
273         }
274         
275 };
276
277
278 extern "C" void * init_module( void )
279 {
280         return new ModuleJoinFloodFactory;
281 }
282