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;
34 Events::ModuleEventProvider& evprov;
36 static bool CanRequest(LocalUser* user, Ext usercaps, Capability* cap, bool adding)
38 if ((usercaps & cap->GetMask()) == adding)
41 return cap->OnRequest(user, adding);
44 Capability::Bit AllocateBit() const
46 Capability::Bit used = 0;
47 for (CapMap::const_iterator i = caps.begin(); i != caps.end(); ++i)
49 Capability* cap = i->second;
50 used |= cap->GetMask();
53 for (unsigned int i = 0; i < MAX_CAPS; i++)
55 Capability::Bit bit = (1 << i);
59 throw ModuleException("Too many caps");
63 ManagerImpl(Module* mod, Events::ModuleEventProvider& evprovref)
65 , capext("caps", ExtensionItem::EXT_USER, mod)
72 for (CapMap::iterator i = caps.begin(); i != caps.end(); ++i)
74 Capability* cap = i->second;
79 void AddCap(Cap::Capability* cap) CXX11_OVERRIDE
81 // No-op if the cap is already registered.
82 // This allows modules to call SetActive() on a cap without checking if it's active first.
83 if (cap->IsRegistered())
86 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Registering cap %s", cap->GetName().c_str());
87 cap->bit = AllocateBit();
88 cap->extitem = &capext;
89 caps.insert(std::make_pair(cap->GetName(), cap));
91 FOREACH_MOD_CUSTOM(evprov, Cap::EventListener, OnCapAddDel, (cap, true));
94 void DelCap(Cap::Capability* cap) CXX11_OVERRIDE
96 // No-op if the cap is not registered, see AddCap() above
97 if (!cap->IsRegistered())
100 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Unregistering cap %s", cap->GetName().c_str());
102 // Fire the event first so modules can still see who is using the cap which is being unregistered
103 FOREACH_MOD_CUSTOM(evprov, Cap::EventListener, OnCapAddDel, (cap, false));
105 // Turn off the cap for all users
106 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
107 for (UserManager::LocalList::const_iterator i = list.begin(); i != list.end(); ++i)
109 LocalUser* user = *i;
110 cap->set(user, false);
114 caps.erase(cap->GetName());
117 Capability* Find(const std::string& capname) const CXX11_OVERRIDE
119 CapMap::const_iterator it = caps.find(capname);
120 if (it != caps.end())
125 void NotifyValueChange(Capability* cap) CXX11_OVERRIDE
127 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Cap %s changed value", cap->GetName().c_str());
128 FOREACH_MOD_CUSTOM(evprov, Cap::EventListener, OnCapValueChange, (cap));
131 Protocol GetProtocol(LocalUser* user) const
133 return ((capext.get(user) & CAP_302_BIT) ? CAP_302 : CAP_LEGACY);
136 void Set302Protocol(LocalUser* user)
138 capext.set(user, capext.get(user) | CAP_302_BIT);
141 bool HandleReq(LocalUser* user, const std::string& reqlist)
143 Ext usercaps = capext.get(user);
144 irc::spacesepstream ss(reqlist);
145 for (std::string capname; ss.GetToken(capname); )
147 bool remove = (capname[0] == '-');
149 capname.erase(capname.begin());
151 Capability* cap = ManagerImpl::Find(capname);
152 if ((!cap) || (!CanRequest(user, usercaps, cap, !remove)))
156 usercaps = cap->DelFromMask(usercaps);
158 usercaps = cap->AddToMask(usercaps);
161 capext.set(user, usercaps);
165 void HandleList(std::string& out, LocalUser* user, bool show_all, bool show_values, bool minus_prefix = false) const
167 Ext show_caps = (show_all ? ~0 : capext.get(user));
169 for (CapMap::const_iterator i = caps.begin(); i != caps.end(); ++i)
171 Capability* cap = i->second;
172 if (!(show_caps & cap->GetMask()))
175 if ((show_all) && (!cap->OnList(user)))
180 out.append(cap->GetName());
184 const std::string* capvalue = cap->GetValue(user);
185 if ((capvalue) && (!capvalue->empty()) && (capvalue->find(' ') == std::string::npos))
188 out.append(*capvalue, 0, MAX_VALUE_LENGTH);
195 void HandleClear(LocalUser* user, std::string& result)
197 HandleList(result, user, false, false, true);
202 class CommandCap : public SplitCommand
204 Events::ModuleEventProvider evprov;
205 Cap::ManagerImpl manager;
207 static void DisplayResult(LocalUser* user, std::string& result)
209 if (result.size() > 5)
210 result.erase(result.end()-1);
211 user->WriteCommand("CAP", result);
217 CommandCap(Module* mod)
218 : SplitCommand(mod, "CAP", 1)
219 , evprov(mod, "event/cap")
220 , manager(mod, evprov)
221 , holdext("cap_hold", ExtensionItem::EXT_USER, mod)
223 works_before_reg = true;
226 CmdResult HandleLocal(const std::vector<std::string>& parameters, LocalUser* user) CXX11_OVERRIDE
228 if (user->registered != REG_ALL)
229 holdext.set(user, 1);
231 std::string subcommand(parameters[0].length(), ' ');
232 std::transform(parameters[0].begin(), parameters[0].end(), subcommand.begin(), ::toupper);
234 if (subcommand == "REQ")
236 if (parameters.size() < 2)
239 std::string result = (manager.HandleReq(user, parameters[1]) ? "ACK :" : "NAK :");
240 result.append(parameters[1]);
241 user->WriteCommand("CAP", result);
243 else if (subcommand == "END")
247 else if ((subcommand == "LS") || (subcommand == "LIST"))
249 const bool is_ls = (subcommand.length() == 2);
250 if ((is_ls) && (parameters.size() > 1) && (parameters[1] == "302"))
251 manager.Set302Protocol(user);
253 std::string result = subcommand + " :";
254 // Show values only if supports v3.2 and doing LS
255 manager.HandleList(result, user, is_ls, ((is_ls) && (manager.GetProtocol(user) != Cap::CAP_LEGACY)));
256 DisplayResult(user, result);
258 else if ((subcommand == "CLEAR") && (manager.GetProtocol(user) == Cap::CAP_LEGACY))
260 std::string result = "ACK :";
261 manager.HandleClear(user, result);
262 DisplayResult(user, result);
266 user->WriteNumeric(ERR_INVALIDCAPSUBCOMMAND, "%s :Invalid CAP subcommand", subcommand.c_str());
274 class ModuleCap : public Module
284 ModResult OnCheckReady(LocalUser* user) CXX11_OVERRIDE
286 return (cmd.holdext.get(user) ? MOD_RES_DENY : MOD_RES_PASSTHRU);
289 Version GetVersion() CXX11_OVERRIDE
291 return Version("Provides support for CAP capability negotiation", VF_VENDOR);
295 MODULE_INIT(ModuleCap)