2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2019 Robby <robby@chatbelgie.be>
5 * Copyright (C) 2018 linuxdaemon <linuxdaemon.irc@gmail.com>
6 * Copyright (C) 2018 Dylan Frank <b00mx0r@aureus.pw>
7 * Copyright (C) 2017-2020 Sadie Powell <sadie@witchery.services>
8 * Copyright (C) 2014-2015, 2018 Attila Molnar <attilamolnar@hush.com>
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.
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
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/>.
25 #include "core_channel.h"
31 /** Hook that sends a MODE after a JOIN if the user in the JOIN has some modes prefix set.
32 * This happens e.g. when modules such as operprefix explicitly set prefix modes on the joining
33 * user, or when a member with prefix modes does a host cycle.
35 class JoinHook : public ClientProtocol::EventHook
37 ClientProtocol::Messages::Mode modemsg;
38 Modes::ChangeList modechangelist;
39 const User* joininguser;
42 /** If true, MODE changes after JOIN will be sourced from the user, rather than the server
47 : ClientProtocol::EventHook(mod, "JOIN")
51 void OnEventInit(const ClientProtocol::Event& ev) CXX11_OVERRIDE
53 const ClientProtocol::Events::Join& join = static_cast<const ClientProtocol::Events::Join&>(ev);
54 const Membership& memb = *join.GetMember();
56 modechangelist.clear();
57 for (std::string::const_iterator i = memb.modes.begin(); i != memb.modes.end(); ++i)
59 PrefixMode* const pm = ServerInstance->Modes.FindPrefixMode(*i);
61 continue; // Shouldn't happen
62 modechangelist.push_add(pm, memb.user->nick);
65 if (modechangelist.empty())
67 // Member got no modes on join
72 joininguser = memb.user;
74 // Prepare a mode protocol event that we can append to the message list in OnPreEventSend()
75 modemsg.SetParams(memb.chan, NULL, modechangelist);
77 modemsg.SetSource(join);
79 modemsg.SetSourceUser(ServerInstance->FakeClient);
82 ModResult OnPreEventSend(LocalUser* user, const ClientProtocol::Event& ev, ClientProtocol::MessageList& messagelist) CXX11_OVERRIDE
84 // If joininguser is NULL then they didn't get any modes on join, skip.
85 // Also don't show their own modes to them, they get that in the NAMES list not via MODE.
86 if ((joininguser) && (user != joininguser))
87 messagelist.push_back(&modemsg);
88 return MOD_RES_PASSTHRU;
94 class CoreModChannel : public Module, public CheckExemption::EventListener
96 Invite::APIImpl invapi;
97 CommandInvite cmdinvite;
100 CommandNames cmdnames;
101 CommandTopic cmdtopic;
102 Events::ModuleEventProvider evprov;
105 ModeChannelBan banmode;
106 SimpleChannelModeHandler inviteonlymode;
107 ModeChannelKey keymode;
108 ModeChannelLimit limitmode;
109 SimpleChannelModeHandler moderatedmode;
110 SimpleChannelModeHandler noextmsgmode;
111 ModeChannelOp opmode;
112 SimpleChannelModeHandler privatemode;
113 SimpleChannelModeHandler secretmode;
114 SimpleChannelModeHandler topiclockmode;
115 ModeChannelVoice voicemode;
117 insp::flat_map<std::string, char> exemptions;
119 ModResult IsInvited(User* user, Channel* chan)
121 LocalUser* localuser = IS_LOCAL(user);
122 if ((localuser) && (invapi.IsInvited(localuser, chan)))
123 return MOD_RES_ALLOW;
124 return MOD_RES_PASSTHRU;
129 : CheckExemption::EventListener(this, UINT_MAX)
131 , cmdinvite(this, invapi)
136 , evprov(this, "event/channel")
139 , inviteonlymode(this, "inviteonly", 'i')
142 , moderatedmode(this, "moderated", 'm')
143 , noextmsgmode(this, "noextmsg", 'n')
145 , privatemode(this, "private", 'p')
146 , secretmode(this, "secret", 's')
147 , topiclockmode(this, "topiclock", 't')
152 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
154 ConfigTag* optionstag = ServerInstance->Config->ConfValue("options");
157 irc::spacesepstream defaultstream(optionstag->getString("exemptchanops"));
158 insp::flat_map<std::string, char> exempts;
159 while (defaultstream.GetToken(current))
161 std::string::size_type pos = current.find(':');
162 if (pos == std::string::npos || (pos + 2) > current.size())
163 throw ModuleException("Invalid exemptchanops value '" + current + "' at " + optionstag->getTagLocation());
165 const std::string restriction = current.substr(0, pos);
166 const char prefix = current[pos + 1];
168 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Exempting prefix %c from %s", prefix, restriction.c_str());
169 exempts[restriction] = prefix;
172 ConfigTag* securitytag = ServerInstance->Config->ConfValue("security");
173 const std::string announceinvites = securitytag->getString("announceinvites", "dynamic", 1);
174 Invite::AnnounceState newannouncestate;
175 if (stdalgo::string::equalsci(announceinvites, "none"))
176 newannouncestate = Invite::ANNOUNCE_NONE;
177 else if (stdalgo::string::equalsci(announceinvites, "all"))
178 newannouncestate = Invite::ANNOUNCE_ALL;
179 else if (stdalgo::string::equalsci(announceinvites, "ops"))
180 newannouncestate = Invite::ANNOUNCE_OPS;
181 else if (stdalgo::string::equalsci(announceinvites, "dynamic"))
182 newannouncestate = Invite::ANNOUNCE_DYNAMIC;
184 throw ModuleException(announceinvites + " is an invalid <security:announceinvites> value, at " + securitytag->getTagLocation());
186 // Config is valid, apply it
188 // Validates and applies <maxlist> tags, so do it first
191 exemptions.swap(exempts);
192 // In 2.0 we allowed limits of 0 to be set. This is non-standard behaviour
193 // and will be removed in the next major release.
194 limitmode.minlimit = optionstag->getBool("allowzerolimit", true) ? 0 : 1;;
195 cmdinvite.announceinvites = newannouncestate;
196 joinhook.modefromuser = optionstag->getBool("cyclehostsfromuser");
198 Implementation events[] = { I_OnCheckKey, I_OnCheckLimit, I_OnCheckChannelBan };
199 if (optionstag->getBool("invitebypassmodes", true))
200 ServerInstance->Modules.Attach(events, this, sizeof(events)/sizeof(Implementation));
203 for (unsigned int i = 0; i < sizeof(events)/sizeof(Implementation); i++)
204 ServerInstance->Modules.Detach(events[i], this);
208 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
210 tokens["KEYLEN"] = ConvToStr(ModeChannelKey::maxkeylen);
212 std::vector<std::string> limits;
214 const ModeParser::ListModeList& listmodes = ServerInstance->Modes->GetListModes();
215 for (ModeParser::ListModeList::const_iterator iter = listmodes.begin(); iter != listmodes.end(); ++iter)
217 ListModeBase* lm = *iter;
218 limits.push_back(InspIRCd::Format("%c:%u", lm->GetModeChar(), lm->GetLowerLimit()));
219 if (lm->HasVariableLength())
220 vlist.push_back(lm->GetModeChar());
223 std::sort(limits.begin(), limits.end());
224 tokens["MAXLIST"] = stdalgo::string::join(limits, ',');
228 tokens["VBANLIST"]; // deprecated
229 tokens["VLIST"] = vlist;
233 ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string&, std::string&, const std::string& keygiven) CXX11_OVERRIDE
236 return MOD_RES_PASSTHRU;
238 // Check whether the channel key is correct.
239 const std::string ckey = chan->GetModeParameter(&keymode);
242 ModResult MOD_RESULT;
243 FIRST_MOD_RESULT(OnCheckKey, MOD_RESULT, (user, chan, keygiven));
244 if (!MOD_RESULT.check(InspIRCd::TimingSafeCompare(ckey, keygiven)))
246 // If no key provided, or key is not the right one, and can't bypass +k (not invited or option not enabled)
247 user->WriteNumeric(ERR_BADCHANNELKEY, chan->name, "Cannot join channel (incorrect channel key)");
252 // Check whether the invite only mode is set.
253 if (chan->IsModeSet(inviteonlymode))
255 ModResult MOD_RESULT;
256 FIRST_MOD_RESULT(OnCheckInvite, MOD_RESULT, (user, chan));
257 if (MOD_RESULT != MOD_RES_ALLOW)
259 user->WriteNumeric(ERR_INVITEONLYCHAN, chan->name, "Cannot join channel (invite only)");
264 // Check whether the limit would be exceeded by this user joining.
265 if (chan->IsModeSet(limitmode))
267 ModResult MOD_RESULT;
268 FIRST_MOD_RESULT(OnCheckLimit, MOD_RESULT, (user, chan));
269 if (!MOD_RESULT.check(chan->GetUserCounter() < static_cast<size_t>(limitmode.ext.get(chan))))
271 user->WriteNumeric(ERR_CHANNELISFULL, chan->name, "Cannot join channel (channel is full)");
276 // Everything looks okay.
277 return MOD_RES_PASSTHRU;
280 void OnPostJoin(Membership* memb) CXX11_OVERRIDE
282 Channel* const chan = memb->chan;
283 LocalUser* const localuser = IS_LOCAL(memb->user);
286 // Remove existing invite, if any
287 invapi.Remove(localuser, chan);
289 if (chan->topic.length())
290 Topic::ShowTopic(localuser, chan);
292 // Show all members of the channel, including invisible (+i) users
293 cmdnames.SendNames(localuser, chan, true);
297 ModResult OnCheckKey(User* user, Channel* chan, const std::string& keygiven) CXX11_OVERRIDE
299 // Hook only runs when being invited bypasses +bkl
300 return IsInvited(user, chan);
303 ModResult OnCheckChannelBan(User* user, Channel* chan) CXX11_OVERRIDE
305 // Hook only runs when being invited bypasses +bkl
306 return IsInvited(user, chan);
309 ModResult OnCheckLimit(User* user, Channel* chan) CXX11_OVERRIDE
311 // Hook only runs when being invited bypasses +bkl
312 return IsInvited(user, chan);
315 ModResult OnCheckInvite(User* user, Channel* chan) CXX11_OVERRIDE
318 return IsInvited(user, chan);
321 void OnUserDisconnect(LocalUser* user) CXX11_OVERRIDE
323 invapi.RemoveAll(user);
326 void OnChannelDelete(Channel* chan) CXX11_OVERRIDE
328 // Make sure the channel won't appear in invite lists from now on, don't wait for cull to unset the ext
329 invapi.RemoveAll(chan);
332 ModResult OnCheckExemption(User* user, Channel* chan, const std::string& restriction) CXX11_OVERRIDE
334 if (!exemptions.count(restriction))
335 return MOD_RES_PASSTHRU;
337 unsigned int mypfx = chan->GetPrefixValue(user);
338 char minmode = exemptions[restriction];
340 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(minmode);
341 if (mh && mypfx >= mh->GetPrefixRank())
342 return MOD_RES_ALLOW;
343 if (mh || minmode == '*')
345 return MOD_RES_PASSTHRU;
348 void Prioritize() CXX11_OVERRIDE
350 ServerInstance->Modules.SetPriority(this, I_OnPostJoin, PRIORITY_FIRST);
351 ServerInstance->Modules.SetPriority(this, I_OnUserPreJoin, PRIORITY_LAST);
354 Version GetVersion() CXX11_OVERRIDE
356 return Version("Provides the INVITE, JOIN, KICK, NAMES, and TOPIC commands", VF_VENDOR|VF_CORE);
360 MODULE_INIT(CoreModChannel)