+
+/** Base class for simple message tags.
+ * Message tags provided by classes derived from this class will be sent to clients that have negotiated
+ * a client capability, also managed by this class.
+ *
+ * Derived classes specify the name of the capability and the message tag and provide a public GetValue()
+ * method with the following signature: const std::string* GetValue(ClientProtocol::Message& msg).
+ * The returned value determines whether to attach the tag to the message. If it is NULL, the tag won't
+ * be attached. If it is non-NULL the tag will be attached with the value in the string. If the string is
+ * empty the tag is attached without a value.
+ *
+ * Providers inheriting from this class don't accept incoming tags by default.
+ *
+ * For more control, inherit from ClientProtocol::MessageTagProvider directly.
+ *
+ * Template parameter T is the derived class.
+ */
+template <typename T>
+class IRCv3::CapTag : public ClientProtocol::MessageTagProvider
+{
+ Cap::Capability cap;
+ const std::string tagname;
+
+ bool ShouldSendTag(LocalUser* user, const ClientProtocol::MessageTagData& tagdata) CXX11_OVERRIDE
+ {
+ return cap.get(user);
+ }
+
+ void OnClientProtocolPopulateTags(ClientProtocol::Message& msg) CXX11_OVERRIDE
+ {
+ T& tag = static_cast<T&>(*this);
+ const std::string* const val = tag.GetValue(msg);
+ if (val)
+ msg.AddTag(tagname, this, *val);
+ }
+
+ public:
+ /** Constructor.
+ * @param mod Module that owns the tag.
+ * @param capname Name of the client capability.
+ * A client capability with this name will be created. It will be available to all clients and it won't
+ * have a value.
+ * See Cap::Capability for more info on client capabilities.
+ * @param Tagname Name of the message tag, to use in the protocol.
+ */
+ CapTag(Module* mod, const std::string& capname, const std::string& Tagname)
+ : ClientProtocol::MessageTagProvider(mod)
+ , cap(mod, capname)
+ , tagname(Tagname)
+ {
+ }
+};