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;
33 insp::flat_map<std::string, char> exemptions;
35 ModResult IsInvited(User* user, Channel* chan)
37 LocalUser* localuser = IS_LOCAL(user);
38 if ((localuser) && (invapi.IsInvited(localuser, chan)))
40 return MOD_RES_PASSTHRU;
45 : CheckExemption::EventListener(this)
47 , cmdinvite(this, invapi)
55 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
57 ConfigTag* optionstag = ServerInstance->Config->ConfValue("options");
58 Implementation events[] = { I_OnCheckKey, I_OnCheckLimit, I_OnCheckChannelBan };
59 if (optionstag->getBool("invitebypassmodes", true))
60 ServerInstance->Modules.Attach(events, this, sizeof(events)/sizeof(Implementation));
63 for (unsigned int i = 0; i < sizeof(events)/sizeof(Implementation); i++)
64 ServerInstance->Modules.Detach(events[i], this);
68 irc::spacesepstream defaultstream(optionstag->getString("exemptchanops"));
69 insp::flat_map<std::string, char> exempts;
70 while (defaultstream.GetToken(current))
72 std::string::size_type pos = current.find(':');
73 if (pos == std::string::npos || (pos + 2) > current.size())
74 throw ModuleException("Invalid exemptchanops value '" + current + "' at " + optionstag->getTagLocation());
76 const std::string restriction = current.substr(0, pos);
77 const char prefix = current[pos + 1];
79 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Exempting prefix %c from %s", prefix, restriction.c_str());
80 exempts[restriction] = prefix;
82 exemptions.swap(exempts);
85 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
87 // Build a map of limits to their mode character.
88 insp::flat_map<int, std::string> limits;
89 const ModeParser::ListModeList& listmodes = ServerInstance->Modes->GetListModes();
90 for (ModeParser::ListModeList::const_iterator iter = listmodes.begin(); iter != listmodes.end(); ++iter)
92 const unsigned int limit = (*iter)->GetLowerLimit();
93 limits[limit].push_back((*iter)->GetModeChar());
96 // Generate the MAXLIST token from the limits map.
97 std::string& buffer = tokens["MAXLIST"];
98 for (insp::flat_map<int, std::string>::const_iterator iter = limits.begin(); iter != limits.end(); ++iter)
101 buffer.push_back(',');
103 buffer.append(iter->second);
104 buffer.push_back(':');
105 buffer.append(ConvToStr(iter->first));
109 void OnPostJoin(Membership* memb) CXX11_OVERRIDE
111 Channel* const chan = memb->chan;
112 LocalUser* const localuser = IS_LOCAL(memb->user);
115 // Remove existing invite, if any
116 invapi.Remove(localuser, chan);
119 Topic::ShowTopic(localuser, chan);
121 // Show all members of the channel, including invisible (+i) users
122 cmdnames.SendNames(localuser, chan, true);
126 ModResult OnCheckKey(User* user, Channel* chan, const std::string& keygiven) CXX11_OVERRIDE
128 // Hook only runs when being invited bypasses +bkl
129 return IsInvited(user, chan);
132 ModResult OnCheckChannelBan(User* user, Channel* chan) CXX11_OVERRIDE
134 // Hook only runs when being invited bypasses +bkl
135 return IsInvited(user, chan);
138 ModResult OnCheckLimit(User* user, Channel* chan) CXX11_OVERRIDE
140 // Hook only runs when being invited bypasses +bkl
141 return IsInvited(user, chan);
144 ModResult OnCheckInvite(User* user, Channel* chan) CXX11_OVERRIDE
147 return IsInvited(user, chan);
150 void OnUserDisconnect(LocalUser* user) CXX11_OVERRIDE
152 invapi.RemoveAll(user);
155 void OnChannelDelete(Channel* chan) CXX11_OVERRIDE
157 // Make sure the channel won't appear in invite lists from now on, don't wait for cull to unset the ext
158 invapi.RemoveAll(chan);
161 ModResult OnCheckExemption(User* user, Channel* chan, const std::string& restriction) CXX11_OVERRIDE
163 if (!exemptions.count(restriction))
164 return MOD_RES_PASSTHRU;
166 unsigned int mypfx = chan->GetPrefixValue(user);
167 char minmode = exemptions[restriction];
169 PrefixMode* mh = ServerInstance->Modes->FindPrefixMode(minmode);
170 if (mh && mypfx >= mh->GetPrefixRank())
171 return MOD_RES_ALLOW;
172 if (mh || minmode == '*')
174 return MOD_RES_PASSTHRU;
177 void Prioritize() CXX11_OVERRIDE
179 ServerInstance->Modules.SetPriority(this, I_OnPostJoin, PRIORITY_FIRST);
182 Version GetVersion() CXX11_OVERRIDE
184 return Version("Provides the INVITE, JOIN, KICK, NAMES, and TOPIC commands", VF_VENDOR|VF_CORE);
188 MODULE_INIT(CoreModChannel)