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