]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/modules/ircv3.h
Remove the Kiwi links from the readme.
[user/henk/code/inspircd.git] / include / modules / ircv3.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2018-2019 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2015, 2018 Attila Molnar <attilamolnar@hush.com>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 #pragma once
22
23 #include "modules/cap.h"
24
25 namespace IRCv3
26 {
27         class WriteNeighborsWithCap;
28         template <typename T>
29         class CapTag;
30 }
31
32 class IRCv3::WriteNeighborsWithCap : public User::ForEachNeighborHandler
33 {
34         const Cap::Capability& cap;
35         ClientProtocol::Event& protoev;
36
37         void Execute(LocalUser* user) CXX11_OVERRIDE
38         {
39                 if (cap.get(user))
40                         user->Send(protoev);
41         }
42
43  public:
44         WriteNeighborsWithCap(User* user, ClientProtocol::Event& ev, const Cap::Capability& capability, bool include_self = false)
45                 : cap(capability)
46                 , protoev(ev)
47         {
48                 user->ForEachNeighbor(*this, include_self);
49         }
50 };
51
52 /** Base class for simple message tags.
53  * Message tags provided by classes derived from this class will be sent to clients that have negotiated
54  * a client capability, also managed by this class.
55  *
56  * Derived classes specify the name of the capability and the message tag and provide a public GetValue()
57  * method with the following signature: const std::string* GetValue(ClientProtocol::Message& msg).
58  * The returned value determines whether to attach the tag to the message. If it is NULL, the tag won't
59  * be attached. If it is non-NULL the tag will be attached with the value in the string. If the string is
60  * empty the tag is attached without a value.
61  *
62  * Providers inheriting from this class don't accept incoming tags by default.
63  *
64  * For more control, inherit from ClientProtocol::MessageTagProvider directly.
65  *
66  * Template parameter T is the derived class.
67  */
68 template <typename T>
69 class IRCv3::CapTag : public ClientProtocol::MessageTagProvider
70 {
71  protected:
72         Cap::Capability cap;
73         const std::string tagname;
74
75         bool ShouldSendTag(LocalUser* user, const ClientProtocol::MessageTagData& tagdata) CXX11_OVERRIDE
76         {
77                 return cap.get(user);
78         }
79
80         void OnPopulateTags(ClientProtocol::Message& msg) CXX11_OVERRIDE
81         {
82                 T& tag = static_cast<T&>(*this);
83                 const std::string* const val = tag.GetValue(msg);
84                 if (val)
85                         msg.AddTag(tagname, this, *val);
86         }
87
88  public:
89         /** Constructor.
90          * @param mod Module that owns the tag.
91          * @param capname Name of the client capability.
92          * A client capability with this name will be created. It will be available to all clients and it won't
93          * have a value.
94          * See Cap::Capability for more info on client capabilities.
95          * @param Tagname Name of the message tag, to use in the protocol.
96          */
97         CapTag(Module* mod, const std::string& capname, const std::string& Tagname)
98                 : ClientProtocol::MessageTagProvider(mod)
99                 , cap(mod, capname)
100                 , tagname(Tagname)
101         {
102         }
103 };