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