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"
25 class CoreModChannel : public Module, public CheckExemption::EventListener
27 Invite::APIImpl invapi;
28 CommandInvite cmdinvite;
31 CommandNames cmdnames;
32 CommandTopic cmdtopic;
34 ModeChannelBan banmode;
35 SimpleChannelModeHandler inviteonlymode;
36 ModeChannelKey keymode;
37 ModeChannelLimit limitmode;
38 SimpleChannelModeHandler moderatedmode;
39 SimpleChannelModeHandler noextmsgmode;
41 SimpleChannelModeHandler privatemode;
42 SimpleChannelModeHandler secretmode;
43 SimpleChannelModeHandler topiclockmode;
44 ModeChannelVoice voicemode;
46 insp::flat_map<std::string, char> exemptions;
48 ModResult IsInvited(User* user, Channel* chan)
50 LocalUser* localuser = IS_LOCAL(user);
51 if ((localuser) && (invapi.IsInvited(localuser, chan)))
53 return MOD_RES_PASSTHRU;
58 : CheckExemption::EventListener(this)
60 , cmdinvite(this, invapi)
66 , inviteonlymode(this, "inviteonly", 'i')
69 , moderatedmode(this, "moderated", 'm')
70 , noextmsgmode(this, "noextmsg", 'n')
72 , privatemode(this, "private", 'p')
73 , secretmode(this, "secret", 's')
74 , topiclockmode(this, "topiclock", 't')
79 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
81 ConfigTag* optionstag = ServerInstance->Config->ConfValue("options");
82 Implementation events[] = { I_OnCheckKey, I_OnCheckLimit, I_OnCheckChannelBan };
83 if (optionstag->getBool("invitebypassmodes", true))
84 ServerInstance->Modules.Attach(events, this, sizeof(events)/sizeof(Implementation));
87 for (unsigned int i = 0; i < sizeof(events)/sizeof(Implementation); i++)
88 ServerInstance->Modules.Detach(events[i], this);
92 irc::spacesepstream defaultstream(optionstag->getString("exemptchanops"));
93 insp::flat_map<std::string, char> exempts;
94 while (defaultstream.GetToken(current))
96 std::string::size_type pos = current.find(':');
97 if (pos == std::string::npos || (pos + 2) > current.size())
98 throw ModuleException("Invalid exemptchanops value '" + current + "' at " + optionstag->getTagLocation());
100 const std::string restriction = current.substr(0, pos);
101 const char prefix = current[pos + 1];
103 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Exempting prefix %c from %s", prefix, restriction.c_str());
104 exempts[restriction] = prefix;
106 exemptions.swap(exempts);
108 // In 2.0 we allowed limits of 0 to be set. This is non-standard behaviour
109 // and will be removed in the next major release.
110 limitmode.minlimit = optionstag->getBool("allowzerolimit", true) ? 0 : 1;
115 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
117 tokens["KEYLEN"] = ConvToStr(ModeChannelKey::maxkeylen);
119 // Build a map of limits to their mode character.
120 insp::flat_map<int, std::string> limits;
121 const ModeParser::ListModeList& listmodes = ServerInstance->Modes->GetListModes();
122 for (ModeParser::ListModeList::const_iterator iter = listmodes.begin(); iter != listmodes.end(); ++iter)
124 const unsigned int limit = (*iter)->GetLowerLimit();
125 limits[limit].push_back((*iter)->GetModeChar());
128 // Generate the MAXLIST token from the limits map.
129 std::string& buffer = tokens["MAXLIST"];
130 for (insp::flat_map<int, std::string>::const_iterator iter = limits.begin(); iter != limits.end(); ++iter)
133 buffer.push_back(',');
135 buffer.append(iter->second);
136 buffer.push_back(':');
137 buffer.append(ConvToStr(iter->first));
141 ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string&, std::string&, const std::string& keygiven) CXX11_OVERRIDE
144 return MOD_RES_PASSTHRU;
146 // Check whether the channel key is correct.
147 const std::string ckey = chan->GetModeParameter(&keymode);
150 ModResult MOD_RESULT;
151 FIRST_MOD_RESULT(OnCheckKey, MOD_RESULT, (user, chan, keygiven));
152 if (!MOD_RESULT.check(InspIRCd::TimingSafeCompare(ckey, keygiven)))
154 // If no key provided, or key is not the right one, and can't bypass +k (not invited or option not enabled)
155 user->WriteNumeric(ERR_BADCHANNELKEY, chan->name, "Cannot join channel (Incorrect channel key)");
160 // Check whether the invite only mode is set.
161 if (chan->IsModeSet(inviteonlymode))
163 ModResult MOD_RESULT;
164 FIRST_MOD_RESULT(OnCheckInvite, MOD_RESULT, (user, chan));
165 if (MOD_RESULT != MOD_RES_ALLOW)
167 user->WriteNumeric(ERR_INVITEONLYCHAN, chan->name, "Cannot join channel (Invite only)");
172 // Check whether the limit would be exceeded by this user joining.
173 if (chan->IsModeSet(limitmode))
175 ModResult MOD_RESULT;
176 FIRST_MOD_RESULT(OnCheckLimit, MOD_RESULT, (user, chan));
177 if (!MOD_RESULT.check(chan->GetUserCounter() < static_cast<size_t>(limitmode.ext.get(chan))))
179 user->WriteNumeric(ERR_CHANNELISFULL, chan->name, "Cannot join channel (Channel is full)");
184 // Everything looks okay.
185 return MOD_RES_PASSTHRU;
188 void OnPostJoin(Membership* memb) CXX11_OVERRIDE
190 Channel* const chan = memb->chan;
191 LocalUser* const localuser = IS_LOCAL(memb->user);
194 // Remove existing invite, if any
195 invapi.Remove(localuser, chan);
197 if (chan->topic.length())
198 Topic::ShowTopic(localuser, chan);
200 // Show all members of the channel, including invisible (+i) users
201 cmdnames.SendNames(localuser, chan, true);
205 ModResult OnCheckKey(User* user, Channel* chan, const std::string& keygiven) CXX11_OVERRIDE
207 // Hook only runs when being invited bypasses +bkl
208 return IsInvited(user, chan);
211 ModResult OnCheckChannelBan(User* user, Channel* chan) CXX11_OVERRIDE
213 // Hook only runs when being invited bypasses +bkl
214 return IsInvited(user, chan);
217 ModResult OnCheckLimit(User* user, Channel* chan) CXX11_OVERRIDE
219 // Hook only runs when being invited bypasses +bkl
220 return IsInvited(user, chan);
223 ModResult OnCheckInvite(User* user, Channel* chan) CXX11_OVERRIDE
226 return IsInvited(user, chan);
229 void OnUserDisconnect(LocalUser* user) CXX11_OVERRIDE
231 invapi.RemoveAll(user);
234 void OnChannelDelete(Channel* chan) CXX11_OVERRIDE
236 // Make sure the channel won't appear in invite lists from now on, don't wait for cull to unset the ext
237 invapi.RemoveAll(chan);
240 ModResult OnCheckExemption(User* user, Channel* chan, const std::string& restriction) CXX11_OVERRIDE
242 if (!exemptions.count(restriction))
243 return MOD_RES_PASSTHRU;
245 unsigned int mypfx = chan->GetPrefixValue(user);
246 char minmode = exemptions[restriction];
248 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(minmode);
249 if (mh && mypfx >= mh->GetPrefixRank())
250 return MOD_RES_ALLOW;
251 if (mh || minmode == '*')
253 return MOD_RES_PASSTHRU;
256 void Prioritize() CXX11_OVERRIDE
258 ServerInstance->Modules.SetPriority(this, I_OnPostJoin, PRIORITY_FIRST);
261 Version GetVersion() CXX11_OVERRIDE
263 return Version("Provides the INVITE, JOIN, KICK, NAMES, and TOPIC commands", VF_VENDOR|VF_CORE);
267 MODULE_INIT(CoreModChannel)