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/>.
26 static const unsigned int MAX_CAPS = (sizeof(intptr_t) * 8) - 1;
27 static const intptr_t CAP_302_BIT = (intptr_t)1 << MAX_CAPS;
28 static const unsigned int MAX_VALUE_LENGTH = 100;
31 typedef LocalIntExt ExtItem;
36 /** Supports capability negotiation protocol v3.1, or none
40 /** Supports capability negotiation v3.2
45 class EventListener : public Events::ModuleEventListener
48 EventListener(Module* mod)
49 : ModuleEventListener(mod, "event/cap")
53 /** Called whenever a new client capability becomes available or unavailable
54 * @param cap Capability being added or removed
55 * @param add If true, the capability is being added, otherwise its being removed
57 virtual void OnCapAddDel(Capability* cap, bool add) = 0;
60 class Manager : public DataProvider
64 : DataProvider(mod, "capmanager")
68 /** Register a client capability.
69 * Modules should call Capability::SetActive(true) instead of this method.
70 * @param cap Capability to register
72 virtual void AddCap(Capability* cap) = 0;
74 /** Unregister a client capability.
75 * Modules should call Capability::SetActive(false) instead of this method.
76 * @param cap Capability to unregister
78 virtual void DelCap(Capability* cap) = 0;
80 /** Find a capability by name
81 * @param name Capability to find
82 * @return Capability object pointer if found, NULL otherwise
84 virtual Capability* Find(const std::string& name) const = 0;
87 /** Represents a client capability.
89 * Capabilities offer extensions to the client to server protocol. They must be negotiated with clients before they have any effect on the protocol.
90 * Each cap must have a unique name that is used during capability negotiation.
92 * After construction the cap is ready to be used by clients without any further setup, like other InspIRCd services.
93 * The get() method accepts a user as parameter and can be used to check whether that user has negotiated usage of the cap. This is only known for local users.
95 * The cap module must be loaded for the capability to work. The IsRegistered() method can be used to query whether the cap is actually online or not.
96 * The capability can be deactivated and reactivated with the SetActive() method. Deactivated caps behave as if they don't exist.
98 * It is possible to implement special behavior by inheriting from this class and overriding some of its methods.
100 class Capability : public ServiceProvider, private dynamic_reference_base::CaptureHook
104 /** Bit allocated to this cap, undefined if the cap is unregistered
108 /** Extension containing all caps set by a user. NULL if the cap is unregistered.
112 /** True if the cap is active. Only active caps are registered in the manager.
116 /** Reference to the cap manager object
118 dynamic_reference<Manager> manager;
120 void OnCapture() CXX11_OVERRIDE
132 Ext AddToMask(Ext mask) const { return (mask | GetMask()); }
133 Ext DelFromMask(Ext mask) const { return (mask & (~GetMask())); }
134 Bit GetMask() const { return bit; }
136 friend class ManagerImpl;
139 /** Constructor, initializes the capability.
140 * Caps are active by default.
141 * @param mod Module providing the cap
142 * @param Name Raw name of the cap as used in the protocol (CAP LS, etc.)
144 Capability(Module* mod, const std::string& Name)
145 : ServiceProvider(mod, Name, SERVICE_CUSTOM)
147 , manager(mod, "capmanager")
157 void RegisterService() CXX11_OVERRIDE
159 manager.SetCaptureHook(this);
163 /** Check whether a user has the capability turned on.
164 * This method is safe to call if the cap is unregistered and will return false.
165 * @param user User to check
166 * @return True if the user is using this capability, false otherwise
168 bool get(User* user) const
172 Ext caps = extitem->get(user);
173 return (caps & GetMask());
176 /** Turn the capability on/off for a user. If the cap is not registered this method has no effect.
177 * @param user User to turn the cap on/off for
178 * @param val True to turn the cap on, false to turn it off
180 void set(User* user, bool val)
184 Ext curr = extitem->get(user);
185 extitem->set(user, (val ? AddToMask(curr) : DelFromMask(curr)));
188 /** Activate or deactivate the capability.
189 * If activating, the cap is marked as active and if the manager is available the cap is registered in the manager.
190 * If deactivating, the cap is marked as inactive and if it is registered, it will be unregistered.
191 * Users who had the cap turned on will have it turned off automatically.
192 * @param activate True to activate the cap, false to deactivate it
194 void SetActive(bool activate)
200 manager->AddCap(this);
202 manager->DelCap(this);
206 /** Get the name of the capability that's used in the protocol
207 * @return Name of the capability as used in the protocol
209 const std::string& GetName() const { return name; }
211 /** Check whether the capability is active. The cap must be active and registered to be used by users.
212 * @return True if the cap is active, false if it has been deactivated
214 bool IsActive() const { return active; }
216 /** Check whether the capability is registered
217 * The cap must be active and the manager must be available for a cap to be registered.
218 * @return True if the cap is registered in the manager, false otherwise
220 bool IsRegistered() const { return (extitem != NULL); }
222 /** Get the CAP negotiation protocol version of a user.
223 * The cap must be registered for this to return anything other than CAP_LEGACY.
224 * @param user User whose negotiation protocol version to query
225 * @return One of the Capability::Protocol enum indicating the highest supported capability negotiation protocol version
227 Protocol GetProtocol(LocalUser* user) const
229 return ((IsRegistered() && (extitem->get(user) & CAP_302_BIT)) ? CAP_302 : CAP_LEGACY);
232 /** Called when a user requests to turn this capability on or off.
233 * @param user User requesting to change the state of the cap
234 * @param add True if requesting to turn the cap on, false if requesting to turn it off
235 * @return True to allow the request, false to reject it
237 virtual bool OnRequest(LocalUser* user, bool add)
242 /** Called when a user requests a list of all capabilities and this capability is about to be included in the list.
243 * The default behavior always includes the cap in the list.
244 * @param user User querying a list capabilities
245 * @return True to add this cap to the list sent to the user, false to not list it
247 virtual bool OnList(LocalUser* user)
252 /** Query the value of this capability for a user
253 * @param user User who will get the value of the capability
254 * @return Value to show to the user. If NULL, the capability has no value (default).
256 virtual const std::string* GetValue(LocalUser* user) const