]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_kicknorejoin.cpp
cdf16a3a1bb0d90e8c636cf0f5de5f588839244c
[user/henk/code/inspircd.git] / src / modules / m_kicknorejoin.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2018 A_D <A-UNDERSCORE-D@users.noreply.github.com>
5  *   Copyright (C) 2014 Daniel Vassdal <shutter@canternet.org>
6  *   Copyright (C) 2013, 2017-2019 Sadie Powell <sadie@witchery.services>
7  *   Copyright (C) 2012-2015 Attila Molnar <attilamolnar@hush.com>
8  *   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
9  *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
10  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
11  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
12  *   Copyright (C) 2006, 2010 Craig Edwards <brain@inspircd.org>
13  *
14  * This file is part of InspIRCd.  InspIRCd is free software: you can
15  * redistribute it and/or modify it under the terms of the GNU General Public
16  * License as published by the Free Software Foundation, version 2.
17  *
18  * This program is distributed in the hope that it will be useful, but WITHOUT
19  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
21  * details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25  */
26
27
28 #include "inspircd.h"
29 #include "modules/invite.h"
30
31 enum
32 {
33         // From RFC 2182.
34         ERR_UNAVAILRESOURCE = 437
35 };
36
37
38 class KickRejoinData
39 {
40         struct KickedUser
41         {
42                 std::string uuid;
43                 time_t expire;
44
45                 KickedUser(User* user, unsigned int Delay)
46                         : uuid(user->uuid)
47                         , expire(ServerInstance->Time() + Delay)
48                 {
49                 }
50         };
51
52         typedef std::vector<KickedUser> KickedList;
53
54         mutable KickedList kicked;
55
56  public:
57         const unsigned int delay;
58
59         KickRejoinData(unsigned int Delay) : delay(Delay) { }
60
61         bool canjoin(LocalUser* user) const
62         {
63                 for (KickedList::iterator i = kicked.begin(); i != kicked.end(); )
64                 {
65                         KickedUser& rec = *i;
66                         if (rec.expire > ServerInstance->Time())
67                         {
68                                 if (rec.uuid == user->uuid)
69                                         return false;
70                                 ++i;
71                         }
72                         else
73                         {
74                                 // Expired record, remove.
75                                 stdalgo::vector::swaperase(kicked, i);
76                                 if (kicked.empty())
77                                         break;
78                         }
79                 }
80                 return true;
81         }
82
83         void add(User* user)
84         {
85                 // One user can be in the list multiple times if the user gets kicked, force joins
86                 // (skipping OnUserPreJoin) and gets kicked again, but that's okay because canjoin()
87                 // works correctly in this case as well
88                 kicked.push_back(KickedUser(user, delay));
89         }
90 };
91
92 /** Handles channel mode +J
93  */
94 class KickRejoin : public ParamMode<KickRejoin, SimpleExtItem<KickRejoinData> >
95 {
96         const unsigned int max;
97  public:
98         KickRejoin(Module* Creator)
99                 : ParamMode<KickRejoin, SimpleExtItem<KickRejoinData> >(Creator, "kicknorejoin", 'J')
100                 , max(60)
101         {
102                 syntax = "<seconds>";
103         }
104
105         ModeAction OnSet(User* source, Channel* channel, std::string& parameter) CXX11_OVERRIDE
106         {
107                 unsigned int v = ConvToNum<unsigned int>(parameter);
108                 if (v <= 0)
109                 {
110                         source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
111                         return MODEACTION_DENY;
112                 }
113
114                 if (IS_LOCAL(source) && v > max)
115                         v = max;
116
117                 ext.set(channel, new KickRejoinData(v));
118                 return MODEACTION_ALLOW;
119         }
120
121         void SerializeParam(Channel* chan, const KickRejoinData* krd, std::string& out)
122         {
123                 out.append(ConvToStr(krd->delay));
124         }
125
126         std::string GetModuleSettings() const
127         {
128                 return ConvToStr(max);
129         }
130 };
131
132 class ModuleKickNoRejoin : public Module
133 {
134         KickRejoin kr;
135         Invite::API invapi;
136
137 public:
138         ModuleKickNoRejoin()
139                 : kr(this)
140                 , invapi(this)
141         {
142         }
143
144         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
145         {
146                 if (chan)
147                 {
148                         const KickRejoinData* data = kr.ext.get(chan);
149                         if ((data) && !invapi->IsInvited(user, chan) && (!data->canjoin(user)))
150                         {
151                                 user->WriteNumeric(ERR_UNAVAILRESOURCE, chan->name, InspIRCd::Format("You must wait %u seconds after being kicked to rejoin (+J is set)", data->delay));
152                                 return MOD_RES_DENY;
153                         }
154                 }
155                 return MOD_RES_PASSTHRU;
156         }
157
158         void OnUserKick(User* source, Membership* memb, const std::string &reason, CUList& excepts) CXX11_OVERRIDE
159         {
160                 if ((!IS_LOCAL(memb->user)) || (source == memb->user))
161                         return;
162
163                 KickRejoinData* data = kr.ext.get(memb->chan);
164                 if (data)
165                 {
166                         data->add(memb->user);
167                 }
168         }
169
170         Version GetVersion() CXX11_OVERRIDE
171         {
172                 return Version("Adds channel mode J (kicknorejoin) which prevents users from rejoining after being kicked from a channel.", VF_VENDOR | VF_COMMON, kr.GetModuleSettings());
173         }
174 };
175
176 MODULE_INIT(ModuleKickNoRejoin)