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/reload.h"
22 #include "modules/cap.h"
29 static Cap::ManagerImpl* managerimpl;
31 class Cap::ManagerImpl : public Cap::Manager, public ReloadModule::EventListener
33 /** Stores the cap state of a module being reloaded
40 std::vector<std::string> users;
43 : name(cap->GetName())
47 std::vector<Data> caps;
50 typedef insp::flat_map<std::string, Capability*, irc::insensitive_swo> CapMap;
54 Events::ModuleEventProvider& evprov;
56 static bool CanRequest(LocalUser* user, Ext usercaps, Capability* cap, bool adding)
58 const bool hascap = ((usercaps & cap->GetMask()) != 0);
62 return cap->OnRequest(user, adding);
65 Capability::Bit AllocateBit() const
67 Capability::Bit used = 0;
68 for (CapMap::const_iterator i = caps.begin(); i != caps.end(); ++i)
70 Capability* cap = i->second;
71 used |= cap->GetMask();
74 for (unsigned int i = 0; i < MAX_CAPS; i++)
76 Capability::Bit bit = (1 << i);
80 throw ModuleException("Too many caps");
83 void OnReloadModuleSave(Module* mod, ReloadModule::CustomData& cd) CXX11_OVERRIDE
85 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "OnReloadModuleSave()");
89 CapModData* capmoddata = new CapModData;
90 cd.add(this, capmoddata);
92 for (CapMap::iterator i = caps.begin(); i != caps.end(); ++i)
94 Capability* cap = i->second;
95 // Only save users of caps that belong to the module being reloaded
96 if (cap->creator != mod)
99 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Module being reloaded implements cap %s, saving cap users", cap->GetName().c_str());
100 capmoddata->caps.push_back(CapModData::Data(cap));
101 CapModData::Data& capdata = capmoddata->caps.back();
103 // Populate list with uuids of users who are using the cap
104 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
105 for (UserManager::LocalList::const_iterator j = list.begin(); j != list.end(); ++j)
107 LocalUser* user = *j;
109 capdata.users.push_back(user->uuid);
114 void OnReloadModuleRestore(Module* mod, void* data) CXX11_OVERRIDE
116 CapModData* capmoddata = static_cast<CapModData*>(data);
117 for (std::vector<CapModData::Data>::const_iterator i = capmoddata->caps.begin(); i != capmoddata->caps.end(); ++i)
119 const CapModData::Data& capdata = *i;
120 Capability* cap = ManagerImpl::Find(capdata.name);
123 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Cap %s is no longer available after reload", capdata.name.c_str());
127 // Set back the cap for all users who were using it before the reload
128 for (std::vector<std::string>::const_iterator j = capdata.users.begin(); j != capdata.users.end(); ++j)
130 const std::string& uuid = *j;
131 User* user = ServerInstance->FindUUID(uuid);
134 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "User %s is gone when trying to restore cap %s", uuid.c_str(), capdata.name.c_str());
138 cap->set(user, true);
145 ManagerImpl(Module* mod, Events::ModuleEventProvider& evprovref)
147 , ReloadModule::EventListener(mod)
156 for (CapMap::iterator i = caps.begin(); i != caps.end(); ++i)
158 Capability* cap = i->second;
163 void AddCap(Cap::Capability* cap) CXX11_OVERRIDE
165 // No-op if the cap is already registered.
166 // This allows modules to call SetActive() on a cap without checking if it's active first.
167 if (cap->IsRegistered())
170 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Registering cap %s", cap->GetName().c_str());
171 cap->bit = AllocateBit();
172 cap->extitem = &capext;
173 caps.insert(std::make_pair(cap->GetName(), cap));
174 ServerInstance->Modules.AddReferent("cap/" + cap->GetName(), cap);
176 FOREACH_MOD_CUSTOM(evprov, Cap::EventListener, OnCapAddDel, (cap, true));
179 void DelCap(Cap::Capability* cap) CXX11_OVERRIDE
181 // No-op if the cap is not registered, see AddCap() above
182 if (!cap->IsRegistered())
185 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Unregistering cap %s", cap->GetName().c_str());
187 // Fire the event first so modules can still see who is using the cap which is being unregistered
188 FOREACH_MOD_CUSTOM(evprov, Cap::EventListener, OnCapAddDel, (cap, false));
190 // Turn off the cap for all users
191 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
192 for (UserManager::LocalList::const_iterator i = list.begin(); i != list.end(); ++i)
194 LocalUser* user = *i;
195 cap->set(user, false);
198 ServerInstance->Modules.DelReferent(cap);
200 caps.erase(cap->GetName());
203 Capability* Find(const std::string& capname) const CXX11_OVERRIDE
205 CapMap::const_iterator it = caps.find(capname);
206 if (it != caps.end())
211 void NotifyValueChange(Capability* cap) CXX11_OVERRIDE
213 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Cap %s changed value", cap->GetName().c_str());
214 FOREACH_MOD_CUSTOM(evprov, Cap::EventListener, OnCapValueChange, (cap));
217 Protocol GetProtocol(LocalUser* user) const
219 return ((capext.get(user) & CAP_302_BIT) ? CAP_302 : CAP_LEGACY);
222 void Set302Protocol(LocalUser* user)
224 capext.set(user, capext.get(user) | CAP_302_BIT);
227 bool HandleReq(LocalUser* user, const std::string& reqlist)
229 Ext usercaps = capext.get(user);
230 irc::spacesepstream ss(reqlist);
231 for (std::string capname; ss.GetToken(capname); )
233 bool remove = (capname[0] == '-');
235 capname.erase(capname.begin());
237 Capability* cap = ManagerImpl::Find(capname);
238 if ((!cap) || (!CanRequest(user, usercaps, cap, !remove)))
242 usercaps = cap->DelFromMask(usercaps);
244 usercaps = cap->AddToMask(usercaps);
247 capext.set(user, usercaps);
251 void HandleList(std::string& out, LocalUser* user, bool show_all, bool show_values, bool minus_prefix = false) const
253 Ext show_caps = (show_all ? ~0 : capext.get(user));
255 for (CapMap::const_iterator i = caps.begin(); i != caps.end(); ++i)
257 Capability* cap = i->second;
258 if (!(show_caps & cap->GetMask()))
261 if ((show_all) && (!cap->OnList(user)))
266 out.append(cap->GetName());
270 const std::string* capvalue = cap->GetValue(user);
271 if ((capvalue) && (!capvalue->empty()) && (capvalue->find(' ') == std::string::npos))
274 out.append(*capvalue, 0, MAX_VALUE_LENGTH);
281 void HandleClear(LocalUser* user, std::string& result)
283 HandleList(result, user, false, false, true);
288 Cap::ExtItem::ExtItem(Module* mod)
289 : LocalIntExt("caps", ExtensionItem::EXT_USER, mod)
293 std::string Cap::ExtItem::serialize(SerializeFormat format, const Extensible* container, void* item) const
296 // XXX: Cast away the const because IS_LOCAL() doesn't handle it
297 LocalUser* user = IS_LOCAL(const_cast<User*>(static_cast<const User*>(container)));
298 if ((format == FORMAT_NETWORK) || (!user))
301 // List requested caps
302 managerimpl->HandleList(ret, user, false, false);
304 // Serialize cap protocol version. If building a human-readable string append a new token, otherwise append only a single character indicating the version.
305 Protocol protocol = managerimpl->GetProtocol(user);
306 if (format == FORMAT_USER)
307 ret.append("capversion=3.");
308 else if (!ret.empty())
309 ret.erase(ret.length()-1);
311 if (protocol == CAP_302)
319 void Cap::ExtItem::unserialize(SerializeFormat format, Extensible* container, const std::string& value)
321 if (format == FORMAT_NETWORK)
324 LocalUser* user = IS_LOCAL(static_cast<User*>(container));
326 return; // Can't happen
328 // Process the cap protocol version which is a single character at the end of the serialized string
329 const char verchar = *value.rbegin();
331 managerimpl->Set302Protocol(user);
333 // Remove the version indicator from the string passed to HandleReq
334 std::string caplist(value, 0, value.size()-1);
335 managerimpl->HandleReq(user, caplist);
338 class CommandCap : public SplitCommand
340 Events::ModuleEventProvider evprov;
341 Cap::ManagerImpl manager;
343 static void DisplayResult(LocalUser* user, std::string& result)
345 if (*result.rbegin() == ' ')
346 result.erase(result.end()-1);
347 user->WriteCommand("CAP", result);
353 CommandCap(Module* mod)
354 : SplitCommand(mod, "CAP", 1)
355 , evprov(mod, "event/cap")
356 , manager(mod, evprov)
357 , holdext("cap_hold", ExtensionItem::EXT_USER, mod)
359 works_before_reg = true;
362 CmdResult HandleLocal(const std::vector<std::string>& parameters, LocalUser* user) CXX11_OVERRIDE
364 if (user->registered != REG_ALL)
365 holdext.set(user, 1);
367 std::string subcommand(parameters[0].length(), ' ');
368 std::transform(parameters[0].begin(), parameters[0].end(), subcommand.begin(), ::toupper);
370 if (subcommand == "REQ")
372 if (parameters.size() < 2)
375 std::string result = (manager.HandleReq(user, parameters[1]) ? "ACK :" : "NAK :");
376 result.append(parameters[1]);
377 user->WriteCommand("CAP", result);
379 else if (subcommand == "END")
383 else if ((subcommand == "LS") || (subcommand == "LIST"))
385 const bool is_ls = (subcommand.length() == 2);
386 if ((is_ls) && (parameters.size() > 1) && (parameters[1] == "302"))
387 manager.Set302Protocol(user);
389 std::string result = subcommand + " :";
390 // Show values only if supports v3.2 and doing LS
391 manager.HandleList(result, user, is_ls, ((is_ls) && (manager.GetProtocol(user) != Cap::CAP_LEGACY)));
392 DisplayResult(user, result);
394 else if ((subcommand == "CLEAR") && (manager.GetProtocol(user) == Cap::CAP_LEGACY))
396 std::string result = "ACK :";
397 manager.HandleClear(user, result);
398 DisplayResult(user, result);
402 user->WriteNumeric(ERR_INVALIDCAPSUBCOMMAND, subcommand, "Invalid CAP subcommand");
410 class ModuleCap : public Module
420 ModResult OnCheckReady(LocalUser* user) CXX11_OVERRIDE
422 return (cmd.holdext.get(user) ? MOD_RES_DENY : MOD_RES_PASSTHRU);
425 Version GetVersion() CXX11_OVERRIDE
427 return Version("Provides support for CAP capability negotiation", VF_VENDOR);
431 MODULE_INIT(ModuleCap)