]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_joinflood.cpp
c4b9336fbc20e501821e110c16dc2912cb886999
[user/henk/code/inspircd.git] / src / modules / m_joinflood.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2013, 2016-2019 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2012-2014 Attila Molnar <attilamolnar@hush.com>
6  *   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
7  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
8  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
9  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
10  *   Copyright (C) 2006-2007, 2010 Craig Edwards <brain@inspircd.org>
11  *
12  * This file is part of InspIRCd.  InspIRCd is free software: you can
13  * redistribute it and/or modify it under the terms of the GNU General Public
14  * License as published by the Free Software Foundation, version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but WITHOUT
17  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
19  * details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23  */
24
25
26 #include "inspircd.h"
27
28 enum
29 {
30         // From RFC 2182.
31         ERR_UNAVAILRESOURCE = 437
32 };
33
34 // The number of seconds the channel will be closed for.
35 static unsigned int duration;
36
37 /** Holds settings and state associated with channel mode +j
38  */
39 class joinfloodsettings
40 {
41  public:
42         unsigned int secs;
43         unsigned int joins;
44         time_t reset;
45         time_t unlocktime;
46         unsigned int counter;
47
48         joinfloodsettings(unsigned int b, unsigned int c)
49                 : secs(b), joins(c), unlocktime(0), counter(0)
50         {
51                 reset = ServerInstance->Time() + secs;
52         }
53
54         void addjoin()
55         {
56                 if (ServerInstance->Time() > reset)
57                 {
58                         counter = 1;
59                         reset = ServerInstance->Time() + secs;
60                 }
61                 else
62                         counter++;
63         }
64
65         bool shouldlock()
66         {
67                 return (counter >= this->joins);
68         }
69
70         void clear()
71         {
72                 counter = 0;
73         }
74
75         bool islocked()
76         {
77                 if (ServerInstance->Time() > unlocktime)
78                         unlocktime = 0;
79
80                 return (unlocktime != 0);
81         }
82
83         void lock()
84         {
85                 unlocktime = ServerInstance->Time() + duration;
86         }
87
88         bool operator==(const joinfloodsettings& other) const
89         {
90                 return ((this->secs == other.secs) && (this->joins == other.joins));
91         }
92 };
93
94 /** Handles channel mode +j
95  */
96 class JoinFlood : public ParamMode<JoinFlood, SimpleExtItem<joinfloodsettings> >
97 {
98  public:
99         JoinFlood(Module* Creator)
100                 : ParamMode<JoinFlood, SimpleExtItem<joinfloodsettings> >(Creator, "joinflood", 'j')
101         {
102                 syntax = "<joins>:<seconds>";
103         }
104
105         ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
106         {
107                 std::string::size_type colon = parameter.find(':');
108                 if ((colon == std::string::npos) || (parameter.find('-') != std::string::npos))
109                 {
110                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
111                         return MODEACTION_DENY;
112                 }
113
114                 /* Set up the flood parameters for this channel */
115                 unsigned int njoins = ConvToNum<unsigned int>(parameter.substr(0, colon));
116                 unsigned int nsecs = ConvToNum<unsigned int>(parameter.substr(colon+1));
117                 if ((njoins<1) || (nsecs<1))
118                 {
119                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
120                         return MODEACTION_DENY;
121                 }
122
123                 ext.set(channel, new joinfloodsettings(nsecs, njoins));
124                 return MODEACTION_ALLOW;
125         }
126
127         void SerializeParam(Channel* chan, const joinfloodsettings* jfs, std::string& out)
128         {
129                 out.append(ConvToStr(jfs->joins)).push_back(':');
130                 out.append(ConvToStr(jfs->secs));
131         }
132 };
133
134 class ModuleJoinFlood : public Module
135 {
136         JoinFlood jf;
137
138  public:
139         ModuleJoinFlood()
140                 : jf(this)
141         {
142         }
143
144         void ReadConfig(ConfigStatus&) CXX11_OVERRIDE
145         {
146                 ConfigTag* tag = ServerInstance->Config->ConfValue("joinflood");
147                 duration = tag->getDuration("duration", 60, 10, 600);
148         }
149
150         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
151         {
152                 if (chan)
153                 {
154                         joinfloodsettings *f = jf.ext.get(chan);
155                         if (f && f->islocked())
156                         {
157                                 user->WriteNumeric(ERR_UNAVAILRESOURCE, chan->name, "This channel is temporarily unavailable (+j is set). Please try again later.");
158                                 return MOD_RES_DENY;
159                         }
160                 }
161                 return MOD_RES_PASSTHRU;
162         }
163
164         void OnUserJoin(Membership* memb, bool sync, bool created, CUList& excepts) CXX11_OVERRIDE
165         {
166                 /* We arent interested in JOIN events caused by a network burst */
167                 if (sync)
168                         return;
169
170                 joinfloodsettings *f = jf.ext.get(memb->chan);
171
172                 /* But all others are OK */
173                 if ((f) && (!f->islocked()))
174                 {
175                         f->addjoin();
176                         if (f->shouldlock())
177                         {
178                                 f->clear();
179                                 f->lock();
180                                 memb->chan->WriteNotice(InspIRCd::Format("This channel has been closed to new users for %u seconds because there have been more than %d joins in %d seconds.", duration, f->joins, f->secs));
181                         }
182                 }
183         }
184
185         Version GetVersion() CXX11_OVERRIDE
186         {
187                 return Version("Adds channel mode j (joinflood) which helps protect against spammers which mass-join channels.", VF_VENDOR);
188         }
189 };
190
191 MODULE_INIT(ModuleJoinFlood)