]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_joinflood.cpp
a15c48bc9662e1491bffb304bd7defa40a5e1b00
[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
26 /* $ModDesc: Provides channel mode +f (message flood protection) */
27
28 class joinfloodsettings
29 {
30  public:
31
32         int secs;
33         int joins;
34         time_t reset;
35         time_t unlocktime;
36         int counter;
37         bool locked;
38
39         joinfloodsettings() : secs(0), joins(0) {};
40         joinfloodsettings(int b, int c) : secs(b), joins(c)
41         {
42                 reset = time(NULL) + secs;
43                 counter = 0;
44                 locked = false;
45                 log(DEBUG,"Create new joinfloodsettings: %lu %lu",time(NULL),reset);
46         };
47
48         void addjoin()
49         {
50                 counter++;
51                 log(DEBUG,"joinflood counter is %d",counter);
52                 if (time(NULL) > reset)
53                 {
54                         log(DEBUG,"joinflood counter reset");
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                 log(DEBUG,"joinflood counter clear");
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                 log(DEBUG,"joinflood lock");
91                 locked = true;
92                 unlocktime = time(NULL) + 60;
93         }
94
95         
96 };
97
98 class ModuleJoinFlood : public Module
99 {
100         Server *Srv;
101         
102  public:
103  
104         ModuleJoinFlood(Server* Me)
105                 : Module::Module(Me)
106         {
107                 Srv = Me;
108                 Srv->AddExtendedMode('j',MT_CHANNEL,false,1,0);
109         }
110         
111         virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params)
112         {
113                 if ((modechar == 'j') && (type == MT_CHANNEL))
114                 {
115                         if (mode_on)
116                         {
117                                 std::string FloodParams = params[0];
118                                 chanrec* c = (chanrec*)target;
119                                 char ndata[MAXBUF];
120                                 char* data = ndata;
121                                 strlcpy(ndata,FloodParams.c_str(),MAXBUF);
122                                 char* joins = data;
123                                 char* secs = NULL;
124                                 while (*data)
125                                 {
126                                         if (*data == ':')
127                                         {
128                                                 *data = 0;
129                                                 data++;
130                                                 secs = data;
131                                                 break;
132                                         }
133                                         else data++;
134                                 }
135                                 if (secs)
136                                 {
137                                         /* Set up the flood parameters for this channel */
138                                         int njoins = atoi(joins);
139                                         int nsecs = atoi(secs);
140                                         if ((njoins<1) || (nsecs<1))
141                                         {
142                                                 WriteServ(user->fd,"608 %s %s :Invalid flood parameter",user->nick,c->name);
143                                                 return 0;
144                                         }
145                                         else
146                                         {
147                                                 if (!c->GetExt("joinflood"))
148                                                 {
149                                                         joinfloodsettings *f = new joinfloodsettings(nsecs,njoins);
150                                                         c->Extend("joinflood",(char*)f);
151                                                 }
152                                         }
153                                         return 1;
154                                 }
155                                 else
156                                 {
157                                         WriteServ(user->fd,"608 %s %s :Invalid flood parameter",user->nick,c->name);
158                                         return 0;
159                                 }
160                                 
161                         }
162                         else
163                         {
164                                 chanrec* c = (chanrec*)target;
165                                 if (c->GetExt("joinflood"))
166                                 {
167                                         joinfloodsettings *f = (joinfloodsettings*)c->GetExt("joinflood");
168                                         delete f;
169                                         c->Shrink("joinflood");
170                                 }
171                         }
172                         return 1;
173                 }
174                 return 0;
175         }
176
177         virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname)
178         {
179                 if (chan)
180                 {
181                         joinfloodsettings *f = (joinfloodsettings*)chan->GetExt("joinflood");
182                         if (f)
183                         {
184                                 if (f->islocked())
185                                 {
186                                         WriteServ(user->fd,"609 %s %s :This channel is temporarily unavailable (+j). Please try again later.",user->nick,chan->name);
187                                         return 1;
188                                 }
189                         }
190                 }
191                 return 0;
192         }
193
194         virtual void OnUserJoin(userrec* user, chanrec* channel)
195         {
196                 joinfloodsettings *f = (joinfloodsettings*)channel->GetExt("joinflood");
197                 if (f)
198                 {
199                         f->addjoin();
200                         if (f->shouldlock())
201                         {
202                                 f->clear();
203                                 f->lock();
204                                 WriteChannelWithServ((char*)Srv->GetServerName().c_str(), channel, "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);
205                         }
206                 }
207         }
208
209         void OnChannelDelete(chanrec* chan)
210         {
211                 if (chan->GetExt("joinflood"))
212                 {
213                         joinfloodsettings *f = (joinfloodsettings*)chan->GetExt("joinflood");
214                         delete f;
215                         chan->Shrink("joinflood");
216                 }
217         }
218
219         void Implements(char* List)
220         {
221                 List[I_On005Numeric] = List[I_OnExtendedMode] = List[I_OnChannelDelete] = List[I_OnUserPreJoin] = List[I_OnUserJoin] = 1;
222         }
223
224         virtual void On005Numeric(std::string &output)
225         {
226                 std::stringstream line(output);
227                 std::string temp1, temp2;
228                 while (!line.eof())
229                 {
230                         line >> temp1;
231                         if (temp1.substr(0,10) == "CHANMODES=")
232                         {
233                                 // By doing this we're *assuming* no other module has fucked up the CHANMODES=
234                                 // section of the 005 numeric. If they have, we're going DOWN in a blaze of glory,
235                                 // with a honking great EXCEPTION :)
236                                 temp1.insert(temp1.find(",")+1,"j");
237                         }
238                         temp2 = temp2 + temp1 + " ";
239                 }
240                 if (temp2.length())
241                         output = temp2.substr(0,temp2.length()-1);
242         }
243
244         virtual ~ModuleJoinFlood()
245         {
246         }
247         
248         virtual Version GetVersion()
249         {
250                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
251         }
252 };
253
254
255 class ModuleJoinFloodFactory : public ModuleFactory
256 {
257  public:
258         ModuleJoinFloodFactory()
259         {
260         }
261         
262         ~ModuleJoinFloodFactory()
263         {
264         }
265         
266         virtual Module * CreateModule(Server* Me)
267         {
268                 return new ModuleJoinFlood(Me);
269         }
270         
271 };
272
273
274 extern "C" void * init_module( void )
275 {
276         return new ModuleJoinFloodFactory;
277 }
278