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 Capability::Bit AllocateBit() const
37 Capability::Bit used = 0;
38 for (CapMap::const_iterator i = caps.begin(); i != caps.end(); ++i)
40 Capability* cap = i->second;
41 used |= cap->GetMask();
44 for (unsigned int i = 0; i < MAX_CAPS; i++)
46 Capability::Bit bit = (1 << i);
50 throw ModuleException("Too many caps");
54 ManagerImpl(Module* mod)
56 , capext("caps", ExtensionItem::EXT_USER, mod)
62 for (CapMap::iterator i = caps.begin(); i != caps.end(); ++i)
64 Capability* cap = i->second;
69 void AddCap(Cap::Capability* cap) CXX11_OVERRIDE
71 // No-op if the cap is already registered.
72 // This allows modules to call SetActive() on a cap without checking if it's active first.
73 if (cap->IsRegistered())
76 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Registering cap %s", cap->GetName().c_str());
77 cap->bit = AllocateBit();
78 cap->extitem = &capext;
79 caps.insert(std::make_pair(cap->GetName(), cap));
82 void DelCap(Cap::Capability* cap) CXX11_OVERRIDE
84 // No-op if the cap is not registered, see AddCap() above
85 if (!cap->IsRegistered())
88 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Unregistering cap %s", cap->GetName().c_str());
90 // Turn off the cap for all users
91 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
92 for (UserManager::LocalList::const_iterator i = list.begin(); i != list.end(); ++i)
95 cap->set(user, false);
99 caps.erase(cap->GetName());
102 Capability* Find(const std::string& capname) const CXX11_OVERRIDE
104 CapMap::const_iterator it = caps.find(capname);
105 if (it != caps.end())
110 bool HandleReq(LocalUser* user, const std::string& reqlist)
112 Ext usercaps = capext.get(user);
113 irc::spacesepstream ss(reqlist);
114 for (std::string capname; ss.GetToken(capname); )
116 bool remove = (capname[0] == '-');
118 capname.erase(capname.begin());
120 Capability* cap = ManagerImpl::Find(capname);
125 usercaps = cap->DelFromMask(usercaps);
127 usercaps = cap->AddToMask(usercaps);
130 capext.set(user, usercaps);
134 void HandleList(std::string& out, LocalUser* user, bool show_all, bool minus_prefix = false) const
136 Ext show_caps = (show_all ? ~0 : capext.get(user));
138 for (CapMap::const_iterator i = caps.begin(); i != caps.end(); ++i)
140 Capability* cap = i->second;
141 if (!(show_caps & cap->GetMask()))
146 out.append(cap->GetName()).push_back(' ');
150 void HandleClear(LocalUser* user, std::string& result)
152 HandleList(result, user, false, true);
157 class CommandCap : public SplitCommand
159 Cap::ManagerImpl manager;
161 static void DisplayResult(LocalUser* user, std::string& result)
163 if (result.size() > 5)
164 result.erase(result.end()-1);
165 user->WriteCommand("CAP", result);
171 CommandCap(Module* mod)
172 : SplitCommand(mod, "CAP", 1)
174 , holdext("cap_hold", ExtensionItem::EXT_USER, mod)
176 works_before_reg = true;
179 CmdResult HandleLocal(const std::vector<std::string>& parameters, LocalUser* user) CXX11_OVERRIDE
181 if (user->registered != REG_ALL)
182 holdext.set(user, 1);
184 std::string subcommand(parameters[0].length(), ' ');
185 std::transform(parameters[0].begin(), parameters[0].end(), subcommand.begin(), ::toupper);
187 if (subcommand == "REQ")
189 if (parameters.size() < 2)
192 std::string result = (manager.HandleReq(user, parameters[1]) ? "ACK :" : "NAK :");
193 result.append(parameters[1]);
194 user->WriteCommand("CAP", result);
196 else if (subcommand == "END")
200 else if ((subcommand == "LS") || (subcommand == "LIST"))
202 const bool is_ls = (subcommand.length() == 2);
204 std::string result = subcommand + " :";
205 manager.HandleList(result, user, is_ls);
206 DisplayResult(user, result);
208 else if (subcommand == "CLEAR")
210 std::string result = "ACK :";
211 manager.HandleClear(user, result);
212 DisplayResult(user, result);
216 user->WriteNumeric(ERR_INVALIDCAPSUBCOMMAND, "%s :Invalid CAP subcommand", subcommand.c_str());
224 class ModuleCap : public Module
234 ModResult OnCheckReady(LocalUser* user) CXX11_OVERRIDE
236 return (cmd.holdext.get(user) ? MOD_RES_DENY : MOD_RES_PASSTHRU);
239 Version GetVersion() CXX11_OVERRIDE
241 return Version("Provides support for CAP capability negotiation", VF_VENDOR);
245 MODULE_INIT(ModuleCap)