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-2019 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");
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 insp::flat_map<int, 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;
219 const unsigned int limit = lm->GetLowerLimit();
220 limits[limit].push_back(lm->GetModeChar());
222 if (lm->HasVariableLength())
223 vlist.push_back(lm->GetModeChar());
226 std::string& buffer = tokens["MAXLIST"];
227 for (insp::flat_map<int, std::string>::const_iterator iter = limits.begin(); iter != limits.end(); ++iter)
230 buffer.push_back(',');
232 std::string modes(iter->second);
233 std::sort(modes.begin(), modes.end());
235 buffer.append(modes);
236 buffer.push_back(':');
237 buffer.append(ConvToStr(iter->first));
242 tokens["VBANLIST"]; // deprecated
243 tokens["VLIST"] = vlist;
247 ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string&, std::string&, const std::string& keygiven) CXX11_OVERRIDE
250 return MOD_RES_PASSTHRU;
252 // Check whether the channel key is correct.
253 const std::string ckey = chan->GetModeParameter(&keymode);
256 ModResult MOD_RESULT;
257 FIRST_MOD_RESULT(OnCheckKey, MOD_RESULT, (user, chan, keygiven));
258 if (!MOD_RESULT.check(InspIRCd::TimingSafeCompare(ckey, keygiven)))
260 // If no key provided, or key is not the right one, and can't bypass +k (not invited or option not enabled)
261 user->WriteNumeric(ERR_BADCHANNELKEY, chan->name, "Cannot join channel (incorrect channel key)");
266 // Check whether the invite only mode is set.
267 if (chan->IsModeSet(inviteonlymode))
269 ModResult MOD_RESULT;
270 FIRST_MOD_RESULT(OnCheckInvite, MOD_RESULT, (user, chan));
271 if (MOD_RESULT != MOD_RES_ALLOW)
273 user->WriteNumeric(ERR_INVITEONLYCHAN, chan->name, "Cannot join channel (invite only)");
278 // Check whether the limit would be exceeded by this user joining.
279 if (chan->IsModeSet(limitmode))
281 ModResult MOD_RESULT;
282 FIRST_MOD_RESULT(OnCheckLimit, MOD_RESULT, (user, chan));
283 if (!MOD_RESULT.check(chan->GetUserCounter() < static_cast<size_t>(limitmode.ext.get(chan))))
285 user->WriteNumeric(ERR_CHANNELISFULL, chan->name, "Cannot join channel (channel is full)");
290 // Everything looks okay.
291 return MOD_RES_PASSTHRU;
294 void OnPostJoin(Membership* memb) CXX11_OVERRIDE
296 Channel* const chan = memb->chan;
297 LocalUser* const localuser = IS_LOCAL(memb->user);
300 // Remove existing invite, if any
301 invapi.Remove(localuser, chan);
303 if (chan->topic.length())
304 Topic::ShowTopic(localuser, chan);
306 // Show all members of the channel, including invisible (+i) users
307 cmdnames.SendNames(localuser, chan, true);
311 ModResult OnCheckKey(User* user, Channel* chan, const std::string& keygiven) CXX11_OVERRIDE
313 // Hook only runs when being invited bypasses +bkl
314 return IsInvited(user, chan);
317 ModResult OnCheckChannelBan(User* user, Channel* chan) CXX11_OVERRIDE
319 // Hook only runs when being invited bypasses +bkl
320 return IsInvited(user, chan);
323 ModResult OnCheckLimit(User* user, Channel* chan) CXX11_OVERRIDE
325 // Hook only runs when being invited bypasses +bkl
326 return IsInvited(user, chan);
329 ModResult OnCheckInvite(User* user, Channel* chan) CXX11_OVERRIDE
332 return IsInvited(user, chan);
335 void OnUserDisconnect(LocalUser* user) CXX11_OVERRIDE
337 invapi.RemoveAll(user);
340 void OnChannelDelete(Channel* chan) CXX11_OVERRIDE
342 // Make sure the channel won't appear in invite lists from now on, don't wait for cull to unset the ext
343 invapi.RemoveAll(chan);
346 ModResult OnCheckExemption(User* user, Channel* chan, const std::string& restriction) CXX11_OVERRIDE
348 if (!exemptions.count(restriction))
349 return MOD_RES_PASSTHRU;
351 unsigned int mypfx = chan->GetPrefixValue(user);
352 char minmode = exemptions[restriction];
354 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(minmode);
355 if (mh && mypfx >= mh->GetPrefixRank())
356 return MOD_RES_ALLOW;
357 if (mh || minmode == '*')
359 return MOD_RES_PASSTHRU;
362 void Prioritize() CXX11_OVERRIDE
364 ServerInstance->Modules.SetPriority(this, I_OnPostJoin, PRIORITY_FIRST);
365 ServerInstance->Modules.SetPriority(this, I_OnUserPreJoin, PRIORITY_LAST);
368 Version GetVersion() CXX11_OVERRIDE
370 return Version("Provides the INVITE, JOIN, KICK, NAMES, and TOPIC commands", VF_VENDOR|VF_CORE);
374 MODULE_INIT(CoreModChannel)