]> git.netwichtig.de Git - user/henk/code/inspircd.git/commitdiff
m_cap Add Capability::OnRequest() hook
authorAttila Molnar <attilamolnar@hush.com>
Sat, 5 Dec 2015 14:35:05 +0000 (15:35 +0100)
committerAttila Molnar <attilamolnar@hush.com>
Sat, 5 Dec 2015 14:35:05 +0000 (15:35 +0100)
include/modules/cap.h
src/modules/m_cap.cpp

index 9dd44a4aa35e4dd8a1bd0003e125524bacb6d6e8..4ca3911a5512e6bb31f4575231be21373b353ed9 100644 (file)
@@ -65,6 +65,8 @@ namespace Cap
         *
         * 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.
         * The capability can be deactivated and reactivated with the SetActive() method. Deactivated caps behave as if they don't exist.
+        *
+        * It is possible to implement special behavior by inheriting from this class and overriding some of its methods.
         */
        class Capability : public ServiceProvider, private dynamic_reference_base::CaptureHook
        {
@@ -187,5 +189,15 @@ namespace Cap
                 * @return True if the cap is registered in the manager, false otherwise
                 */
                bool IsRegistered() const { return (extitem != NULL); }
+
+               /** Called when a user requests to turn this capability on or off.
+                * @param user User requesting to change the state of the cap
+                * @param add True if requesting to turn the cap on, false if requesting to turn it off
+                * @return True to allow the request, false to reject it
+                */
+               virtual bool OnRequest(LocalUser* user, bool add)
+               {
+                       return true;
+               }
        };
 }
index 9e857ad28c30e548b5b26b0b53dfe0f8b400349d..bb5a506c95c0b928e23fb1c70a53d80092debf2a 100644 (file)
@@ -32,6 +32,14 @@ class Cap::ManagerImpl : public Cap::Manager
        ExtItem capext;
        CapMap caps;
 
+       static bool CanRequest(LocalUser* user, Ext usercaps, Capability* cap, bool adding)
+       {
+               if ((usercaps & cap->GetMask()) == adding)
+                       return true;
+
+               return cap->OnRequest(user, adding);
+       }
+
        Capability::Bit AllocateBit() const
        {
                Capability::Bit used = 0;
@@ -118,7 +126,7 @@ class Cap::ManagerImpl : public Cap::Manager
                                capname.erase(capname.begin());
 
                        Capability* cap = ManagerImpl::Find(capname);
-                       if (!cap)
+                       if ((!cap) || (!CanRequest(user, usercaps, cap, !remove)))
                                return false;
 
                        if (remove)