2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 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 "modules/cap.h"
28 class Cap::ManagerImpl : public Cap::Manager
30 typedef insp::flat_map<std::string, Capability*, irc::insensitive_swo> CapMap;
35 static bool CanRequest(LocalUser* user, Ext usercaps, Capability* cap, bool adding)
37 if ((usercaps & cap->GetMask()) == adding)
40 return cap->OnRequest(user, adding);
43 Capability::Bit AllocateBit() const
45 Capability::Bit used = 0;
46 for (CapMap::const_iterator i = caps.begin(); i != caps.end(); ++i)
48 Capability* cap = i->second;
49 used |= cap->GetMask();
52 for (unsigned int i = 0; i < MAX_CAPS; i++)
54 Capability::Bit bit = (1 << i);
58 throw ModuleException("Too many caps");
62 ManagerImpl(Module* mod)
64 , capext("caps", ExtensionItem::EXT_USER, mod)
70 for (CapMap::iterator i = caps.begin(); i != caps.end(); ++i)
72 Capability* cap = i->second;
77 void AddCap(Cap::Capability* cap) CXX11_OVERRIDE
79 // No-op if the cap is already registered.
80 // This allows modules to call SetActive() on a cap without checking if it's active first.
81 if (cap->IsRegistered())
84 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Registering cap %s", cap->GetName().c_str());
85 cap->bit = AllocateBit();
86 cap->extitem = &capext;
87 caps.insert(std::make_pair(cap->GetName(), cap));
90 void DelCap(Cap::Capability* cap) CXX11_OVERRIDE
92 // No-op if the cap is not registered, see AddCap() above
93 if (!cap->IsRegistered())
96 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Unregistering cap %s", cap->GetName().c_str());
98 // Turn off the cap for all users
99 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
100 for (UserManager::LocalList::const_iterator i = list.begin(); i != list.end(); ++i)
102 LocalUser* user = *i;
103 cap->set(user, false);
107 caps.erase(cap->GetName());
110 Capability* Find(const std::string& capname) const CXX11_OVERRIDE
112 CapMap::const_iterator it = caps.find(capname);
113 if (it != caps.end())
118 Protocol GetProtocol(LocalUser* user) const
120 return ((capext.get(user) & CAP_302_BIT) ? CAP_302 : CAP_LEGACY);
123 void Set302Protocol(LocalUser* user)
125 capext.set(user, capext.get(user) | CAP_302_BIT);
128 bool HandleReq(LocalUser* user, const std::string& reqlist)
130 Ext usercaps = capext.get(user);
131 irc::spacesepstream ss(reqlist);
132 for (std::string capname; ss.GetToken(capname); )
134 bool remove = (capname[0] == '-');
136 capname.erase(capname.begin());
138 Capability* cap = ManagerImpl::Find(capname);
139 if ((!cap) || (!CanRequest(user, usercaps, cap, !remove)))
143 usercaps = cap->DelFromMask(usercaps);
145 usercaps = cap->AddToMask(usercaps);
148 capext.set(user, usercaps);
152 void HandleList(std::string& out, LocalUser* user, bool show_all, bool minus_prefix = false) const
154 Ext show_caps = (show_all ? ~0 : capext.get(user));
156 for (CapMap::const_iterator i = caps.begin(); i != caps.end(); ++i)
158 Capability* cap = i->second;
159 if (!(show_caps & cap->GetMask()))
162 if ((show_all) && (!cap->OnList(user)))
167 out.append(cap->GetName()).push_back(' ');
171 void HandleClear(LocalUser* user, std::string& result)
173 HandleList(result, user, false, true);
178 class CommandCap : public SplitCommand
180 Cap::ManagerImpl manager;
182 static void DisplayResult(LocalUser* user, std::string& result)
184 if (result.size() > 5)
185 result.erase(result.end()-1);
186 user->WriteCommand("CAP", result);
192 CommandCap(Module* mod)
193 : SplitCommand(mod, "CAP", 1)
195 , holdext("cap_hold", ExtensionItem::EXT_USER, mod)
197 works_before_reg = true;
200 CmdResult HandleLocal(const std::vector<std::string>& parameters, LocalUser* user) CXX11_OVERRIDE
202 if (user->registered != REG_ALL)
203 holdext.set(user, 1);
205 std::string subcommand(parameters[0].length(), ' ');
206 std::transform(parameters[0].begin(), parameters[0].end(), subcommand.begin(), ::toupper);
208 if (subcommand == "REQ")
210 if (parameters.size() < 2)
213 std::string result = (manager.HandleReq(user, parameters[1]) ? "ACK :" : "NAK :");
214 result.append(parameters[1]);
215 user->WriteCommand("CAP", result);
217 else if (subcommand == "END")
221 else if ((subcommand == "LS") || (subcommand == "LIST"))
223 const bool is_ls = (subcommand.length() == 2);
224 if ((is_ls) && (parameters.size() > 1) && (parameters[1] == "302"))
225 manager.Set302Protocol(user);
227 std::string result = subcommand + " :";
228 manager.HandleList(result, user, is_ls);
229 DisplayResult(user, result);
231 else if (subcommand == "CLEAR")
233 std::string result = "ACK :";
234 manager.HandleClear(user, result);
235 DisplayResult(user, result);
239 user->WriteNumeric(ERR_INVALIDCAPSUBCOMMAND, "%s :Invalid CAP subcommand", subcommand.c_str());
247 class ModuleCap : public Module
257 ModResult OnCheckReady(LocalUser* user) CXX11_OVERRIDE
259 return (cmd.holdext.get(user) ? MOD_RES_DENY : MOD_RES_PASSTHRU);
262 Version GetVersion() CXX11_OVERRIDE
264 return Version("Provides support for CAP capability negotiation", VF_VENDOR);
268 MODULE_INIT(ModuleCap)