]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - include/modules/cap.h
Allow converting a Cap::Reference to a Cap::Capability*.
[user/henk/code/inspircd.git] / include / modules / cap.h
index 6f91f5aeefbb6575029c2109654eccb14c281f05..664120d39de1014a7e2399954bdf7325a4d30700 100644 (file)
@@ -1,7 +1,8 @@
 /*
  * InspIRCd -- Internet Relay Chat Daemon
  *
- *   Copyright (C) 2015 Attila Molnar <attilamolnar@hush.com>
+ *   Copyright (C) 2018-2019 Sadie Powell <sadie@witchery.services>
+ *   Copyright (C) 2015-2016, 2018 Attila Molnar <attilamolnar@hush.com>
  *
  * This file is part of InspIRCd.  InspIRCd is free software: you can
  * redistribute it and/or modify it under the terms of the GNU General Public
@@ -28,7 +29,15 @@ 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);
+               void FromInternal(Extensible* container, const std::string& value) CXX11_OVERRIDE;
+               std::string ToHuman(const Extensible* container, void* item) const CXX11_OVERRIDE;
+               std::string ToInternal(const Extensible* container, void* item) const CXX11_OVERRIDE;
+       };
+
        class Capability;
 
        enum Protocol
@@ -190,7 +199,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.
@@ -278,4 +287,57 @@ 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)
+               {
+               }
+
+               /** Retrieves the underlying cap. */
+               operator const Cap::Capability*() const
+               {
+                       return ref ? *ref : NULL;
+               }
+
+               /** 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->GetServerName())
+               {
+                       PushParamPlaceholder();
+                       PushParam(subcmd);
+               }
+
+               void SetUser(LocalUser* user)
+               {
+                       if (user->registered & REG_NICK)
+                               ReplaceParamRef(0, user->nick);
+                       else
+                               ReplaceParam(0, "*");
+               }
+       };
 }