]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - include/modules/cap.h
Allow HAProxy to specify that a client is connecting with SSL.
[user/henk/code/inspircd.git] / include / modules / cap.h
index e242720b58dec7c6ed0823bd837217c6868e26b7..6dcb9f3bc4d037d5a9055cfe7472824e8adcb190 100644 (file)
@@ -28,7 +28,14 @@ namespace Cap
        static const unsigned int MAX_VALUE_LENGTH = 100;
 
        typedef intptr_t Ext;
-       typedef LocalIntExt ExtItem;
+       class ExtItem : public LocalIntExt
+       {
+        public:
+               ExtItem(Module* mod);
+               std::string serialize(SerializeFormat format, const Extensible* container, void* item) const CXX11_OVERRIDE;
+               void unserialize(SerializeFormat format, Extensible* container, const std::string& value) CXX11_OVERRIDE;
+       };
+
        class Capability;
 
        enum Protocol
@@ -42,6 +49,26 @@ namespace Cap
                CAP_302
        };
 
+       class EventListener : public Events::ModuleEventListener
+       {
+        public:
+               EventListener(Module* mod)
+                       : ModuleEventListener(mod, "event/cap")
+               {
+               }
+
+               /** Called whenever a new client capability becomes available or unavailable
+                * @param cap Capability being added or removed
+                * @param add If true, the capability is being added, otherwise its being removed
+                */
+               virtual void OnCapAddDel(Capability* cap, bool add) = 0;
+
+               /** Called whenever the value of a cap changes.
+                * @param cap Capability whose value changed
+                */
+               virtual void OnCapValueChange(Capability* cap) { }
+       };
+
        class Manager : public DataProvider
        {
         public:
@@ -67,6 +94,11 @@ namespace Cap
                 * @return Capability object pointer if found, NULL otherwise
                 */
                virtual Capability* Find(const std::string& name) const = 0;
+
+               /** Notify manager when a value of a cap changed
+                * @param cap Cap whose value changed
+                */
+               virtual void NotifyValueChange(Capability* cap) = 0;
        };
 
        /** Represents a client capability.
@@ -120,6 +152,16 @@ namespace Cap
 
                friend class ManagerImpl;
 
+        protected:
+               /** Notify the manager that the value of the capability changed.
+                * Must be called if the value of the cap changes for any reason.
+                */
+               void NotifyValueChange()
+               {
+                       if (IsRegistered())
+                               manager->NotifyValueChange(this);
+               }
+
         public:
                /** Constructor, initializes the capability.
                 * Caps are active by default.
@@ -155,7 +197,7 @@ namespace Cap
                        if (!IsRegistered())
                                return false;
                        Ext caps = extitem->get(user);
-                       return (caps & GetMask());
+                       return ((caps & GetMask()) != 0);
                }
 
                /** Turn the capability on/off for a user. If the cap is not registered this method has no effect.
@@ -243,4 +285,51 @@ namespace Cap
                        return NULL;
                }
        };
+
+       /** Reference to a cap. The cap may be provided by another module.
+        */
+       class Reference
+       {
+               dynamic_reference_nocheck<Capability> ref;
+
+        public:
+               /** Constructor, initializes the capability reference
+                * @param mod Module creating this object
+                * @param Name Raw name of the cap as used in the protocol (CAP LS, etc.)
+                */
+               Reference(Module* mod, const std::string& Name)
+                       : ref(mod, "cap/" + Name)
+               {
+               }
+
+               /** Check whether a user has the referenced capability turned on.
+                * @param user User to check
+                * @return True if the user is using the referenced capability, false otherwise
+                */
+               bool get(LocalUser* user)
+               {
+                       if (ref)
+                               return ref->get(user);
+                       return false;
+               }
+       };
+
+       class MessageBase : public ClientProtocol::Message
+       {
+        public:
+               MessageBase(const std::string& subcmd)
+                       : ClientProtocol::Message("CAP", ServerInstance->Config->ServerName)
+               {
+                       PushParamPlaceholder();
+                       PushParam(subcmd);
+               }
+
+               void SetUser(LocalUser* user)
+               {
+                       if (user->registered & REG_NICK)
+                               ReplaceParamRef(0, user->nick);
+                       else
+                               ReplaceParam(0, "*");
+               }
+       };
 }