]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/modules/cap.h
m_cap Add Capability::GetCapValue(), list capabilities with values
[user/henk/code/inspircd.git] / include / modules / cap.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2015 Attila Molnar <attilamolnar@hush.com>
5  *
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.
9  *
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
13  * details.
14  *
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/>.
17  */
18
19
20 #pragma once
21
22 #include "event.h"
23
24 namespace Cap
25 {
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;
29
30         typedef intptr_t Ext;
31         typedef LocalIntExt ExtItem;
32         class Capability;
33
34         enum Protocol
35         {
36                 /** Supports capability negotiation protocol v3.1, or none
37                  */
38                 CAP_LEGACY,
39
40                 /** Supports capability negotiation v3.2
41                  */
42                 CAP_302
43         };
44
45         class Manager : public DataProvider
46         {
47          public:
48                 Manager(Module* mod)
49                         : DataProvider(mod, "capmanager")
50                 {
51                 }
52
53                 /** Register a client capability.
54                  * Modules should call Capability::SetActive(true) instead of this method.
55                  * @param cap Capability to register
56                  */
57                 virtual void AddCap(Capability* cap) = 0;
58
59                 /** Unregister a client capability.
60                  * Modules should call Capability::SetActive(false) instead of this method.
61                  * @param cap Capability to unregister
62                  */
63                 virtual void DelCap(Capability* cap) = 0;
64
65                 /** Find a capability by name
66                  * @param name Capability to find
67                  * @return Capability object pointer if found, NULL otherwise
68                  */
69                 virtual Capability* Find(const std::string& name) const = 0;
70         };
71
72         /** Represents a client capability.
73          *
74          * Capabilities offer extensions to the client to server protocol. They must be negotiated with clients before they have any effect on the protocol.
75          * Each cap must have a unique name that is used during capability negotiation.
76          *
77          * After construction the cap is ready to be used by clients without any further setup, like other InspIRCd services.
78          * 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.
79          *
80          * 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.
81          * The capability can be deactivated and reactivated with the SetActive() method. Deactivated caps behave as if they don't exist.
82          *
83          * It is possible to implement special behavior by inheriting from this class and overriding some of its methods.
84          */
85         class Capability : public ServiceProvider, private dynamic_reference_base::CaptureHook
86         {
87                 typedef size_t Bit;
88
89                 /** Bit allocated to this cap, undefined if the cap is unregistered
90                  */
91                 Bit bit;
92
93                 /** Extension containing all caps set by a user. NULL if the cap is unregistered.
94                  */
95                 ExtItem* extitem;
96
97                 /** True if the cap is active. Only active caps are registered in the manager.
98                  */
99                 bool active;
100
101                 /** Reference to the cap manager object
102                  */
103                 dynamic_reference<Manager> manager;
104
105                 void OnCapture() CXX11_OVERRIDE
106                 {
107                         if (active)
108                                 SetActive(true);
109                 }
110
111                 void Unregister()
112                 {
113                         bit = 0;
114                         extitem = NULL;
115                 }
116
117                 Ext AddToMask(Ext mask) const { return (mask | GetMask()); }
118                 Ext DelFromMask(Ext mask) const { return (mask & (~GetMask())); }
119                 Bit GetMask() const { return bit; }
120
121                 friend class ManagerImpl;
122
123          public:
124                 /** Constructor, initializes the capability.
125                  * Caps are active by default.
126                  * @param mod Module providing the cap
127                  * @param Name Raw name of the cap as used in the protocol (CAP LS, etc.)
128                  */
129                 Capability(Module* mod, const std::string& Name)
130                         : ServiceProvider(mod, Name, SERVICE_CUSTOM)
131                         , active(true)
132                         , manager(mod, "capmanager")
133                 {
134                         Unregister();
135                 }
136
137                 ~Capability()
138                 {
139                         SetActive(false);
140                 }
141
142                 void RegisterService() CXX11_OVERRIDE
143                 {
144                         manager.SetCaptureHook(this);
145                         SetActive(true);
146                 }
147
148                 /** Check whether a user has the capability turned on.
149                  * This method is safe to call if the cap is unregistered and will return false.
150                  * @param user User to check
151                  * @return True if the user is using this capability, false otherwise
152                  */
153                 bool get(User* user) const
154                 {
155                         if (!IsRegistered())
156                                 return false;
157                         Ext caps = extitem->get(user);
158                         return (caps & GetMask());
159                 }
160
161                 /** Turn the capability on/off for a user. If the cap is not registered this method has no effect.
162                  * @param user User to turn the cap on/off for
163                  * @param val True to turn the cap on, false to turn it off
164                  */
165                 void set(User* user, bool val)
166                 {
167                         if (!IsRegistered())
168                                 return;
169                         Ext curr = extitem->get(user);
170                         extitem->set(user, (val ? AddToMask(curr) : DelFromMask(curr)));
171                 }
172
173                 /** Activate or deactivate the capability.
174                  * If activating, the cap is marked as active and if the manager is available the cap is registered in the manager.
175                  * If deactivating, the cap is marked as inactive and if it is registered, it will be unregistered.
176                  * Users who had the cap turned on will have it turned off automatically.
177                  * @param activate True to activate the cap, false to deactivate it
178                  */
179                 void SetActive(bool activate)
180                 {
181                         active = activate;
182                         if (manager)
183                         {
184                                 if (activate)
185                                         manager->AddCap(this);
186                                 else
187                                         manager->DelCap(this);
188                         }
189                 }
190
191                 /** Get the name of the capability that's used in the protocol
192                  * @return Name of the capability as used in the protocol
193                  */
194                 const std::string& GetName() const { return name; }
195
196                 /** Check whether the capability is active. The cap must be active and registered to be used by users.
197                  * @return True if the cap is active, false if it has been deactivated
198                  */
199                 bool IsActive() const { return active; }
200
201                 /** Check whether the capability is registered
202                  * The cap must be active and the manager must be available for a cap to be registered.
203                  * @return True if the cap is registered in the manager, false otherwise
204                  */
205                 bool IsRegistered() const { return (extitem != NULL); }
206
207                 /** Get the CAP negotiation protocol version of a user.
208                  * The cap must be registered for this to return anything other than CAP_LEGACY.
209                  * @param user User whose negotiation protocol version to query
210                  * @return One of the Capability::Protocol enum indicating the highest supported capability negotiation protocol version
211                  */
212                 Protocol GetProtocol(LocalUser* user) const
213                 {
214                         return ((IsRegistered() && (extitem->get(user) & CAP_302_BIT)) ? CAP_302 : CAP_LEGACY);
215                 }
216
217                 /** Called when a user requests to turn this capability on or off.
218                  * @param user User requesting to change the state of the cap
219                  * @param add True if requesting to turn the cap on, false if requesting to turn it off
220                  * @return True to allow the request, false to reject it
221                  */
222                 virtual bool OnRequest(LocalUser* user, bool add)
223                 {
224                         return true;
225                 }
226
227                 /** Called when a user requests a list of all capabilities and this capability is about to be included in the list.
228                  * The default behavior always includes the cap in the list.
229                  * @param user User querying a list capabilities
230                  * @return True to add this cap to the list sent to the user, false to not list it
231                  */
232                 virtual bool OnList(LocalUser* user)
233                 {
234                         return true;
235                 }
236
237                 /** Query the value of this capability for a user
238                  * @param user User who will get the value of the capability
239                  * @return Value to show to the user. If NULL, the capability has no value (default).
240                  */
241                 virtual const std::string* GetValue(LocalUser* user) const
242                 {
243                         return NULL;
244                 }
245         };
246 }