]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_joinflood.cpp
MetaData rework
[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         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding, bool)
101         {
102                 joinfloodsettings* dummy;
103
104                 if (adding)
105                 {
106                         char ndata[MAXBUF];
107                         char* data = ndata;
108                         strlcpy(ndata,parameter.c_str(),MAXBUF);
109                         char* joins = data;
110                         char* secs = NULL;
111                         while (*data)
112                         {
113                                 if (*data == ':')
114                                 {
115                                         *data = 0;
116                                         data++;
117                                         secs = data;
118                                         break;
119                                 }
120                                 else data++;
121                         }
122                         if (secs)
123
124                         {
125                                 /* Set up the flood parameters for this channel */
126                                 int njoins = atoi(joins);
127                                 int nsecs = atoi(secs);
128                                 if ((njoins<1) || (nsecs<1))
129                                 {
130                                         source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
131                                         parameter.clear();
132                                         return MODEACTION_DENY;
133                                 }
134                                 else
135                                 {
136                                         if (!channel->GetExt("joinflood", dummy))
137                                         {
138                                                 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
139                                                 joinfloodsettings *f = new joinfloodsettings(ServerInstance, nsecs, njoins);
140                                                 channel->Extend("joinflood", f);
141                                                 channel->SetModeParam('j', parameter);
142                                                 return MODEACTION_ALLOW;
143                                         }
144                                         else
145                                         {
146                                                 std::string cur_param = channel->GetModeParameter('j');
147                                                 parameter = ConvToStr(njoins) + ":" +ConvToStr(nsecs);
148                                                 if (cur_param == parameter)
149                                                 {
150                                                         // mode params match
151                                                         return MODEACTION_DENY;
152                                                 }
153                                                 else
154                                                 {
155                                                         // new mode param, replace old with new
156                                                         if ((nsecs > 0) && (njoins > 0))
157                                                         {
158                                                                 joinfloodsettings* f;
159                                                                 channel->GetExt("joinflood", f);
160                                                                 delete f;
161                                                                 f = new joinfloodsettings(ServerInstance, nsecs, njoins);
162                                                                 channel->Shrink("joinflood");
163                                                                 channel->Extend("joinflood", f);
164                                                                 channel->SetModeParam('j', parameter);
165                                                                 return MODEACTION_ALLOW;
166                                                         }
167                                                         else
168                                                         {
169                                                                 return MODEACTION_DENY;
170                                                         }
171                                                 }
172                                         }
173                                 }
174                         }
175                         else
176                         {
177                                 source->WriteNumeric(608, "%s %s :Invalid flood parameter",source->nick.c_str(),channel->name.c_str());
178                                 return MODEACTION_DENY;
179                         }
180                 }
181                 else
182                 {
183                         if (channel->GetExt("joinflood", dummy))
184                         {
185                                 joinfloodsettings *f;
186                                 channel->GetExt("joinflood", f);
187                                 delete f;
188                                 channel->Shrink("joinflood");
189                                 channel->SetModeParam('j', "");
190                                 return MODEACTION_ALLOW;
191                         }
192                 }
193                 return MODEACTION_DENY;
194         }
195 };
196
197 class ModuleJoinFlood : public Module
198 {
199
200         JoinFlood jf;
201
202  public:
203
204         ModuleJoinFlood(InspIRCd* Me)
205                 : Module(Me), jf(Me)
206         {
207
208                 if (!ServerInstance->Modes->AddMode(&jf))
209                         throw ModuleException("Could not add new modes!");
210                 Implementation eventlist[] = { I_OnChannelDelete, I_OnUserPreJoin, I_OnUserJoin };
211                 ServerInstance->Modules->Attach(eventlist, this, 3);
212         }
213
214         virtual int OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
215         {
216                 if (chan)
217                 {
218                         joinfloodsettings *f;
219                         if (chan->GetExt("joinflood", f))
220                         {
221                                 if (f->islocked())
222                                 {
223                                         user->WriteNumeric(609, "%s %s :This channel is temporarily unavailable (+j). Please try again later.",user->nick.c_str(),chan->name.c_str());
224                                         return 1;
225                                 }
226                         }
227                 }
228                 return 0;
229         }
230
231         virtual void OnUserJoin(User* user, Channel* channel, bool sync, bool &silent, bool created)
232         {
233                 joinfloodsettings *f;
234
235                 /* We arent interested in JOIN events caused by a network burst */
236                 if (sync)
237                         return;
238
239                 /* But all others are OK */
240                 if (channel->GetExt("joinflood",f))
241                 {
242                         f->addjoin();
243                         if (f->shouldlock())
244                         {
245                                 f->clear();
246                                 f->lock();
247                                 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);
248                         }
249                 }
250         }
251
252         void OnChannelDelete(Channel* chan)
253         {
254                 joinfloodsettings *f;
255                 if (chan->GetExt("joinflood",f))
256                 {
257                         delete f;
258                         chan->Shrink("joinflood");
259                 }
260         }
261
262
263         virtual ~ModuleJoinFlood()
264         {
265                 ServerInstance->Modes->DelMode(&jf);
266         }
267
268         virtual Version GetVersion()
269         {
270                 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
271         }
272 };
273
274 MODULE_INIT(ModuleJoinFlood)