]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_joinflood.cpp
Merge pull request #1168 from SaberUK/master+fix-configure-cache-parser
[user/henk/code/inspircd.git] / src / modules / m_joinflood.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
6  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
7  *   Copyright (C) 2006-2007 Craig Edwards <craigedwards@brainbox.cc>
8  *   Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
9  *
10  * This file is part of InspIRCd.  InspIRCd is free software: you can
11  * redistribute it and/or modify it under the terms of the GNU General Public
12  * License as published by the Free Software Foundation, version 2.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
17  * details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23
24 #include "inspircd.h"
25
26 /** Holds settings and state associated with channel mode +j
27  */
28 class joinfloodsettings
29 {
30  public:
31         unsigned int secs;
32         unsigned int joins;
33         time_t reset;
34         time_t unlocktime;
35         unsigned int counter;
36
37         joinfloodsettings(unsigned int b, unsigned int c)
38                 : secs(b), joins(c), unlocktime(0), counter(0)
39         {
40                 reset = ServerInstance->Time() + secs;
41         }
42
43         void addjoin()
44         {
45                 if (ServerInstance->Time() > reset)
46                 {
47                         counter = 1;
48                         reset = ServerInstance->Time() + secs;
49                 }
50                 else
51                         counter++;
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 (ServerInstance->Time() > unlocktime)
67                         unlocktime = 0;
68
69                 return (unlocktime != 0);
70         }
71
72         void lock()
73         {
74                 unlocktime = ServerInstance->Time() + 60;
75         }
76
77         bool operator==(const joinfloodsettings& other) const
78         {
79                 return ((this->secs == other.secs) && (this->joins == other.joins));
80         }
81 };
82
83 /** Handles channel mode +j
84  */
85 class JoinFlood : public ParamMode<JoinFlood, SimpleExtItem<joinfloodsettings> >
86 {
87  public:
88         JoinFlood(Module* Creator)
89                 : ParamMode<JoinFlood, SimpleExtItem<joinfloodsettings> >(Creator, "joinflood", 'j')
90         {
91         }
92
93         ModeAction OnSet(User* source, Channel* channel, std::string& parameter)
94         {
95                 std::string::size_type colon = parameter.find(':');
96                 if ((colon == std::string::npos) || (parameter.find('-') != std::string::npos))
97                 {
98                         source->WriteNumeric(608, channel->name, "Invalid flood parameter");
99                         return MODEACTION_DENY;
100                 }
101
102                 /* Set up the flood parameters for this channel */
103                 unsigned int njoins = ConvToInt(parameter.substr(0, colon));
104                 unsigned int nsecs = ConvToInt(parameter.substr(colon+1));
105                 if ((njoins<1) || (nsecs<1))
106                 {
107                         source->WriteNumeric(608, channel->name, "Invalid flood parameter");
108                         return MODEACTION_DENY;
109                 }
110
111                 ext.set(channel, new joinfloodsettings(nsecs, njoins));
112                 return MODEACTION_ALLOW;
113         }
114
115         void SerializeParam(Channel* chan, const joinfloodsettings* jfs, std::string& out)
116         {
117                 out.append(ConvToStr(jfs->joins)).push_back(':');
118                 out.append(ConvToStr(jfs->secs));
119         }
120 };
121
122 class ModuleJoinFlood : public Module
123 {
124         JoinFlood jf;
125
126  public:
127         ModuleJoinFlood()
128                 : jf(this)
129         {
130         }
131
132         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
133         {
134                 if (chan)
135                 {
136                         joinfloodsettings *f = jf.ext.get(chan);
137                         if (f && f->islocked())
138                         {
139                                 user->WriteNumeric(609, chan->name, "This channel is temporarily unavailable (+j). Please try again later.");
140                                 return MOD_RES_DENY;
141                         }
142                 }
143                 return MOD_RES_PASSTHRU;
144         }
145
146         void OnUserJoin(Membership* memb, bool sync, bool created, CUList& excepts) CXX11_OVERRIDE
147         {
148                 /* We arent interested in JOIN events caused by a network burst */
149                 if (sync)
150                         return;
151
152                 joinfloodsettings *f = jf.ext.get(memb->chan);
153
154                 /* But all others are OK */
155                 if ((f) && (!f->islocked()))
156                 {
157                         f->addjoin();
158                         if (f->shouldlock())
159                         {
160                                 f->clear();
161                                 f->lock();
162                                 memb->chan->WriteNotice(InspIRCd::Format("This channel has been closed to new users for 60 seconds because there have been more than %d joins in %d seconds.", f->joins, f->secs));
163                         }
164                 }
165         }
166
167         Version GetVersion() CXX11_OVERRIDE
168         {
169                 return Version("Provides channel mode +j (join flood protection)", VF_VENDOR);
170         }
171 };
172
173 MODULE_INIT(ModuleJoinFlood)