2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2014-2015 Attila Molnar <attilamolnar@hush.com>
6 * This file is part of InspIRCd. InspIRCd is free software: you can
7 * redistribute it and/or modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation, version 2.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "core_channel.h"
27 /** Hook that sends a MODE after a JOIN if the user in the JOIN has some modes prefix set.
28 * This happens e.g. when modules such as operprefix explicitly set prefix modes on the joining
29 * user, or when a member with prefix modes does a host cycle.
31 class JoinHook : public ClientProtocol::EventHook
33 ClientProtocol::Messages::Mode modemsg;
34 Modes::ChangeList modechangelist;
35 const User* joininguser;
38 /** If true, MODE changes after JOIN will be sourced from the user, rather than the server
43 : ClientProtocol::EventHook(mod, "JOIN")
47 void OnEventInit(const ClientProtocol::Event& ev) CXX11_OVERRIDE
49 const ClientProtocol::Events::Join& join = static_cast<const ClientProtocol::Events::Join&>(ev);
50 const Membership& memb = *join.GetMember();
52 modechangelist.clear();
53 for (std::string::const_iterator i = memb.modes.begin(); i != memb.modes.end(); ++i)
55 PrefixMode* const pm = ServerInstance->Modes.FindPrefixMode(*i);
57 continue; // Shouldn't happen
58 modechangelist.push_add(pm, memb.user->nick);
61 if (modechangelist.empty())
63 // Member got no modes on join
68 joininguser = memb.user;
70 // Prepare a mode protocol event that we can append to the message list in OnPreEventSend()
71 modemsg.SetParams(memb.chan, NULL, modechangelist);
73 modemsg.SetSource(join);
75 modemsg.SetSourceUser(ServerInstance->FakeClient);
78 ModResult OnPreEventSend(LocalUser* user, const ClientProtocol::Event& ev, ClientProtocol::MessageList& messagelist) CXX11_OVERRIDE
80 // If joininguser is NULL then they didn't get any modes on join, skip.
81 // Also don't show their own modes to them, they get that in the NAMES list not via MODE.
82 if ((joininguser) && (user != joininguser))
83 messagelist.push_back(&modemsg);
84 return MOD_RES_PASSTHRU;
90 class CoreModChannel : public Module, public CheckExemption::EventListener
92 Invite::APIImpl invapi;
93 CommandInvite cmdinvite;
96 CommandNames cmdnames;
97 CommandTopic cmdtopic;
98 Events::ModuleEventProvider evprov;
101 ModeChannelBan banmode;
102 SimpleChannelModeHandler inviteonlymode;
103 ModeChannelKey keymode;
104 ModeChannelLimit limitmode;
105 SimpleChannelModeHandler moderatedmode;
106 SimpleChannelModeHandler noextmsgmode;
107 ModeChannelOp opmode;
108 SimpleChannelModeHandler privatemode;
109 SimpleChannelModeHandler secretmode;
110 SimpleChannelModeHandler topiclockmode;
111 ModeChannelVoice voicemode;
113 insp::flat_map<std::string, char> exemptions;
115 ModResult IsInvited(User* user, Channel* chan)
117 LocalUser* localuser = IS_LOCAL(user);
118 if ((localuser) && (invapi.IsInvited(localuser, chan)))
119 return MOD_RES_ALLOW;
120 return MOD_RES_PASSTHRU;
125 : CheckExemption::EventListener(this)
127 , cmdinvite(this, invapi)
132 , evprov(this, "event/channel")
135 , inviteonlymode(this, "inviteonly", 'i')
138 , moderatedmode(this, "moderated", 'm')
139 , noextmsgmode(this, "noextmsg", 'n')
141 , privatemode(this, "private", 'p')
142 , secretmode(this, "secret", 's')
143 , topiclockmode(this, "topiclock", 't')
148 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
150 ConfigTag* optionstag = ServerInstance->Config->ConfValue("options");
151 Implementation events[] = { I_OnCheckKey, I_OnCheckLimit, I_OnCheckChannelBan };
152 if (optionstag->getBool("invitebypassmodes", true))
153 ServerInstance->Modules.Attach(events, this, sizeof(events)/sizeof(Implementation));
156 for (unsigned int i = 0; i < sizeof(events)/sizeof(Implementation); i++)
157 ServerInstance->Modules.Detach(events[i], this);
160 joinhook.modefromuser = optionstag->getBool("cyclehostsfromuser");
163 irc::spacesepstream defaultstream(optionstag->getString("exemptchanops"));
164 insp::flat_map<std::string, char> exempts;
165 while (defaultstream.GetToken(current))
167 std::string::size_type pos = current.find(':');
168 if (pos == std::string::npos || (pos + 2) > current.size())
169 throw ModuleException("Invalid exemptchanops value '" + current + "' at " + optionstag->getTagLocation());
171 const std::string restriction = current.substr(0, pos);
172 const char prefix = current[pos + 1];
174 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Exempting prefix %c from %s", prefix, restriction.c_str());
175 exempts[restriction] = prefix;
177 exemptions.swap(exempts);
179 ConfigTag* securitytag = ServerInstance->Config->ConfValue("security");
180 const std::string announceinvites = securitytag->getString("announceinvites", "dynamic");
181 if (stdalgo::string::equalsci(announceinvites, "none"))
182 cmdinvite.announceinvites = Invite::ANNOUNCE_NONE;
183 else if (stdalgo::string::equalsci(announceinvites, "all"))
184 cmdinvite.announceinvites = Invite::ANNOUNCE_ALL;
185 else if (stdalgo::string::equalsci(announceinvites, "ops"))
186 cmdinvite.announceinvites = Invite::ANNOUNCE_OPS;
187 else if (stdalgo::string::equalsci(announceinvites, "dynamic"))
188 cmdinvite.announceinvites = Invite::ANNOUNCE_DYNAMIC;
190 throw ModuleException(announceinvites + " is an invalid <security:announceinvites> value, at " + securitytag->getTagLocation());
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;
199 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
201 tokens["KEYLEN"] = ConvToStr(ModeChannelKey::maxkeylen);
203 // Build a map of limits to their mode character.
204 insp::flat_map<int, std::string> limits;
205 const ModeParser::ListModeList& listmodes = ServerInstance->Modes->GetListModes();
206 for (ModeParser::ListModeList::const_iterator iter = listmodes.begin(); iter != listmodes.end(); ++iter)
208 const unsigned int limit = (*iter)->GetLowerLimit();
209 limits[limit].push_back((*iter)->GetModeChar());
212 // Generate the MAXLIST token from the limits map.
213 std::string& buffer = tokens["MAXLIST"];
214 for (insp::flat_map<int, std::string>::const_iterator iter = limits.begin(); iter != limits.end(); ++iter)
217 buffer.push_back(',');
219 buffer.append(iter->second);
220 buffer.push_back(':');
221 buffer.append(ConvToStr(iter->first));
225 ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string&, std::string&, const std::string& keygiven) CXX11_OVERRIDE
228 return MOD_RES_PASSTHRU;
230 // Check whether the channel key is correct.
231 const std::string ckey = chan->GetModeParameter(&keymode);
234 ModResult MOD_RESULT;
235 FIRST_MOD_RESULT(OnCheckKey, MOD_RESULT, (user, chan, keygiven));
236 if (!MOD_RESULT.check(InspIRCd::TimingSafeCompare(ckey, keygiven)))
238 // If no key provided, or key is not the right one, and can't bypass +k (not invited or option not enabled)
239 user->WriteNumeric(ERR_BADCHANNELKEY, chan->name, "Cannot join channel (Incorrect channel key)");
244 // Check whether the invite only mode is set.
245 if (chan->IsModeSet(inviteonlymode))
247 ModResult MOD_RESULT;
248 FIRST_MOD_RESULT(OnCheckInvite, MOD_RESULT, (user, chan));
249 if (MOD_RESULT != MOD_RES_ALLOW)
251 user->WriteNumeric(ERR_INVITEONLYCHAN, chan->name, "Cannot join channel (Invite only)");
256 // Check whether the limit would be exceeded by this user joining.
257 if (chan->IsModeSet(limitmode))
259 ModResult MOD_RESULT;
260 FIRST_MOD_RESULT(OnCheckLimit, MOD_RESULT, (user, chan));
261 if (!MOD_RESULT.check(chan->GetUserCounter() < static_cast<size_t>(limitmode.ext.get(chan))))
263 user->WriteNumeric(ERR_CHANNELISFULL, chan->name, "Cannot join channel (Channel is full)");
268 // Everything looks okay.
269 return MOD_RES_PASSTHRU;
272 void OnPostJoin(Membership* memb) CXX11_OVERRIDE
274 Channel* const chan = memb->chan;
275 LocalUser* const localuser = IS_LOCAL(memb->user);
278 // Remove existing invite, if any
279 invapi.Remove(localuser, chan);
281 if (chan->topic.length())
282 Topic::ShowTopic(localuser, chan);
284 // Show all members of the channel, including invisible (+i) users
285 cmdnames.SendNames(localuser, chan, true);
289 ModResult OnCheckKey(User* user, Channel* chan, const std::string& keygiven) CXX11_OVERRIDE
291 // Hook only runs when being invited bypasses +bkl
292 return IsInvited(user, chan);
295 ModResult OnCheckChannelBan(User* user, Channel* chan) CXX11_OVERRIDE
297 // Hook only runs when being invited bypasses +bkl
298 return IsInvited(user, chan);
301 ModResult OnCheckLimit(User* user, Channel* chan) CXX11_OVERRIDE
303 // Hook only runs when being invited bypasses +bkl
304 return IsInvited(user, chan);
307 ModResult OnCheckInvite(User* user, Channel* chan) CXX11_OVERRIDE
310 return IsInvited(user, chan);
313 void OnUserDisconnect(LocalUser* user) CXX11_OVERRIDE
315 invapi.RemoveAll(user);
318 void OnChannelDelete(Channel* chan) CXX11_OVERRIDE
320 // Make sure the channel won't appear in invite lists from now on, don't wait for cull to unset the ext
321 invapi.RemoveAll(chan);
324 ModResult OnCheckExemption(User* user, Channel* chan, const std::string& restriction) CXX11_OVERRIDE
326 if (!exemptions.count(restriction))
327 return MOD_RES_PASSTHRU;
329 unsigned int mypfx = chan->GetPrefixValue(user);
330 char minmode = exemptions[restriction];
332 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(minmode);
333 if (mh && mypfx >= mh->GetPrefixRank())
334 return MOD_RES_ALLOW;
335 if (mh || minmode == '*')
337 return MOD_RES_PASSTHRU;
340 void Prioritize() CXX11_OVERRIDE
342 ServerInstance->Modules.SetPriority(this, I_OnPostJoin, PRIORITY_FIRST);
343 ServerInstance->Modules.SetPriority(this, I_OnUserPreJoin, PRIORITY_LAST);
346 Version GetVersion() CXX11_OVERRIDE
348 return Version("Provides the INVITE, JOIN, KICK, NAMES, and TOPIC commands", VF_VENDOR|VF_CORE);
352 MODULE_INIT(CoreModChannel)