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