]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_joinflood.cpp
Windows support. Tested and working to compile on freebsd and linux. Next step is...
[user/henk/code/inspircd.git] / src / modules / m_joinflood.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include <stdio.h>
15 #include <map>
16 #include "users.h"
17 #include "channels.h"
18 #include "modules.h"
19 #include "configreader.h"
20 #include "inspircd.h"
21
22 /* $ModDesc: Provides channel mode +j (join flood protection) */
23
24 /** Holds settings and state associated with channel mode +j
25  */
26 class joinfloodsettings : public classbase
27 {
28  public:
29
30         int secs;
31         int joins;
32         time_t reset;
33         time_t unlocktime;
34         int counter;
35         bool locked;
36     InspIRCd* ServerInstance;
37     
38         joinfloodsettings() : secs(0), joins(0) {};
39
40         joinfloodsettings(int b, int c) : secs(b), joins(c)
41         {
42                 reset = time(NULL) + secs;
43                 counter = 0;
44                 locked = false;
45         };
46
47         void addjoin()
48         {
49                 counter++;
50                 if (time(NULL) > reset)
51                 {
52                         counter = 0;
53                         reset = time(NULL) + secs;
54                 }
55         }
56
57         bool shouldlock()
58         {
59                 return (counter >= this->joins);
60         }
61
62         void clear()
63         {
64                 counter = 0;
65         }
66
67         bool islocked()
68         {
69                 if (locked)
70                 {
71                         if (time(NULL) > unlocktime)
72                         {
73                                 locked = false;
74                                 return false;
75                         }
76                         else
77                         {
78                                 return true;
79                         }
80                 }
81                 return false;
82         }
83
84         void lock()
85         {
86                 locked = true;
87                 unlocktime = time(NULL) + 60;
88         }
89
90 };
91
92 /** Handles channel mode +j
93  */
94 class JoinFlood : public ModeHandler
95 {
96  public:
97         JoinFlood(InspIRCd* Instance) : ModeHandler(Instance, 'j', 1, 0, false, MODETYPE_CHANNEL, false) { }
98
99         ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string &parameter)
100         {
101                 joinfloodsettings* x;
102                 if (channel->GetExt("joinflood",x))
103                         return std::make_pair(true, ConvToStr(x->joins)+":"+ConvToStr(x->secs));
104                 else
105                         return std::make_pair(false, parameter);
106         } 
107
108         bool CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
109         {
110                 /* When TS is equal, the alphabetically later one wins */
111                 return (their_param < our_param);
112         }
113
114         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
115         {
116                 joinfloodsettings* dummy;
117
118                 if (adding)
119                 {
120                         char ndata[MAXBUF];
121                         char* data = ndata;
122                         strlcpy(ndata,parameter.c_str(),MAXBUF);
123                         char* joins = data;
124                         char* secs = NULL;
125                         while (*data)
126                         {
127                                 if (*data == ':')
128                                 {
129                                         *data = 0;
130                                         data++;
131                                         secs = data;
132                                         break;
133                                 }
134                                 else data++;
135                         }
136                         if (secs)
137
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                                         else
160                                         {
161                                                 std::string cur_param = channel->GetModeParameter('j');
162                                                 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
163                                                 if (cur_param == parameter)
164                                                 {
165                                                         // mode params match
166                                                         return MODEACTION_DENY;
167                                                 }
168                                                 else
169                                                 {
170                                                         // new mode param, replace old with new
171                                                         if ((nsecs > 0) && (njoins > 0))
172                                                         {
173                                                                 joinfloodsettings* f;
174                                                                 channel->GetExt("joinflood", f);
175                                                                 delete f;
176                                                                 f = new joinfloodsettings(nsecs,njoins);
177                                                                 channel->Shrink("joinflood");
178                                                                 channel->Extend("joinflood", f);
179                                                                 channel->SetModeParam('j', cur_param.c_str(), false);
180                                                                 channel->SetModeParam('j', parameter.c_str(), true);
181                                                                 return MODEACTION_ALLOW;
182                                                         }
183                                                         else
184                                                         {
185                                                                 return MODEACTION_DENY;
186                                                         }
187                                                 }
188                                         }
189                                 }
190                         }
191                         else
192                         {
193                                 source->WriteServ("608 %s %s :Invalid flood parameter",source->nick,channel->name);
194                                 return MODEACTION_DENY;
195                         }
196                 }
197                 else
198                 {
199                         if (channel->GetExt("joinflood", dummy))
200                         {
201                                 joinfloodsettings *f;
202                                 channel->GetExt("joinflood", f);
203                                 DELETE(f);
204                                 channel->Shrink("joinflood");
205                                 channel->SetMode('j', false);
206                                 return MODEACTION_ALLOW;
207                         }
208                 }
209                 return MODEACTION_DENY;
210         }
211 };
212
213 class ModuleJoinFlood : public Module
214 {
215         
216         JoinFlood* jf;
217         
218  public:
219  
220         ModuleJoinFlood(InspIRCd* Me)
221                 : Module(Me)
222         {
223                 
224                 jf = new JoinFlood(ServerInstance);
225                 if (!ServerInstance->AddMode(jf, 'j'))
226                         throw ModuleException("Could not add new modes!");
227         }
228         
229         virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname, std::string &privs)
230         {
231                 if (chan)
232                 {
233                         joinfloodsettings *f;
234                         if (chan->GetExt("joinflood", f))
235                         {
236                                 if (f->islocked())
237                                 {
238                                         user->WriteServ("609 %s %s :This channel is temporarily unavailable (+j). Please try again later.",user->nick,chan->name);
239                                         return 1;
240                                 }
241                         }
242                 }
243                 return 0;
244         }
245
246         virtual void OnUserJoin(userrec* user, chanrec* channel, bool &silent)
247         {
248                 joinfloodsettings *f;
249                 if (channel->GetExt("joinflood",f))
250                 {
251                         f->addjoin();
252                         if (f->shouldlock())
253                         {
254                                 f->clear();
255                                 f->lock();
256                                 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);
257                         }
258                 }
259         }
260
261         void OnChannelDelete(chanrec* chan)
262         {
263                 joinfloodsettings *f;
264                 if (chan->GetExt("joinflood",f))
265                 {
266                         DELETE(f);
267                         chan->Shrink("joinflood");
268                 }
269         }
270
271         void Implements(char* List)
272         {
273                 List[I_OnChannelDelete] = List[I_OnUserPreJoin] = List[I_OnUserJoin] = 1;
274         }
275
276         virtual ~ModuleJoinFlood()
277         {
278                 ServerInstance->Modes->DelMode(jf);
279                 DELETE(jf);
280         }
281         
282         virtual Version GetVersion()
283         {
284                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
285         }
286 };
287
288
289 class ModuleJoinFloodFactory : public ModuleFactory
290 {
291  public:
292         ModuleJoinFloodFactory()
293         {
294         }
295         
296         ~ModuleJoinFloodFactory()
297         {
298         }
299         
300         virtual Module * CreateModule(InspIRCd* Me)
301         {
302                 return new ModuleJoinFlood(Me);
303         }
304         
305 };
306
307
308 extern "C" DllExport void * init_module( void )
309 {
310         return new ModuleJoinFloodFactory;
311 }
312