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