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, UINT_MAX)
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");
153 irc::spacesepstream defaultstream(optionstag->getString("exemptchanops"));
154 insp::flat_map<std::string, char> exempts;
155 while (defaultstream.GetToken(current))
157 std::string::size_type pos = current.find(':');
158 if (pos == std::string::npos || (pos + 2) > current.size())
159 throw ModuleException("Invalid exemptchanops value '" + current + "' at " + optionstag->getTagLocation());
161 const std::string restriction = current.substr(0, pos);
162 const char prefix = current[pos + 1];
164 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Exempting prefix %c from %s", prefix, restriction.c_str());
165 exempts[restriction] = prefix;
168 ConfigTag* securitytag = ServerInstance->Config->ConfValue("security");
169 const std::string announceinvites = securitytag->getString("announceinvites", "dynamic");
170 Invite::AnnounceState newannouncestate;
171 if (stdalgo::string::equalsci(announceinvites, "none"))
172 newannouncestate = Invite::ANNOUNCE_NONE;
173 else if (stdalgo::string::equalsci(announceinvites, "all"))
174 newannouncestate = Invite::ANNOUNCE_ALL;
175 else if (stdalgo::string::equalsci(announceinvites, "ops"))
176 newannouncestate = Invite::ANNOUNCE_OPS;
177 else if (stdalgo::string::equalsci(announceinvites, "dynamic"))
178 newannouncestate = Invite::ANNOUNCE_DYNAMIC;
180 throw ModuleException(announceinvites + " is an invalid <security:announceinvites> value, at " + securitytag->getTagLocation());
182 // Config is valid, apply it
184 // Validates and applies <maxlist> tags, so do it first
187 exemptions.swap(exempts);
188 // In 2.0 we allowed limits of 0 to be set. This is non-standard behaviour
189 // and will be removed in the next major release.
190 limitmode.minlimit = optionstag->getBool("allowzerolimit", true) ? 0 : 1;;
191 cmdinvite.announceinvites = newannouncestate;
192 joinhook.modefromuser = optionstag->getBool("cyclehostsfromuser");
194 Implementation events[] = { I_OnCheckKey, I_OnCheckLimit, I_OnCheckChannelBan };
195 if (optionstag->getBool("invitebypassmodes", true))
196 ServerInstance->Modules.Attach(events, this, sizeof(events)/sizeof(Implementation));
199 for (unsigned int i = 0; i < sizeof(events)/sizeof(Implementation); i++)
200 ServerInstance->Modules.Detach(events[i], this);
204 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
206 tokens["KEYLEN"] = ConvToStr(ModeChannelKey::maxkeylen);
208 // Build a map of limits to their mode character.
209 insp::flat_map<int, std::string> limits;
210 const ModeParser::ListModeList& listmodes = ServerInstance->Modes->GetListModes();
211 for (ModeParser::ListModeList::const_iterator iter = listmodes.begin(); iter != listmodes.end(); ++iter)
213 const unsigned int limit = (*iter)->GetLowerLimit();
214 limits[limit].push_back((*iter)->GetModeChar());
217 // Generate the MAXLIST token from the limits map.
218 std::string& buffer = tokens["MAXLIST"];
219 for (insp::flat_map<int, std::string>::const_iterator iter = limits.begin(); iter != limits.end(); ++iter)
222 buffer.push_back(',');
224 std::string modes(iter->second);
225 std::sort(modes.begin(), modes.end());
227 buffer.append(modes);
228 buffer.push_back(':');
229 buffer.append(ConvToStr(iter->first));
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)