]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/commandbuilder.h
Fix some confusing logic in sanick.
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / commandbuilder.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2019 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2013-2015 Attila Molnar <attilamolnar@hush.com>
6  *   Copyright (C) 2013 Adam <Adam@anope.org>
7  *
8  * This file is part of InspIRCd.  InspIRCd is free software: you can
9  * redistribute it and/or modify it under the terms of the GNU General Public
10  * License as published by the Free Software Foundation, version 2.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21
22 #pragma once
23
24 #include "utils.h"
25
26 class TreeServer;
27
28 class CmdBuilder
29 {
30  protected:
31         /** The raw message contents. */
32         std::string content;
33
34         /** Tags which have been added to this message. */
35         ClientProtocol::TagMap tags;
36
37         /** The size of tags within the contents. */
38         size_t tagsize;
39
40         /** Fires the ServerProtocol::MessageEventListener::OnBuildMessage event for a server target. */
41         void FireEvent(Server* target, const char* cmd, ClientProtocol::TagMap& taglist);
42
43         /** Fires the ServerProtocol::MessageEventListener::OnBuildMessage event for a user target. */
44         void FireEvent(User* target, const char* cmd, ClientProtocol::TagMap& taglist);
45
46         /** Updates the tag string within the buffer. */
47         void UpdateTags();
48
49  public:
50         CmdBuilder(const char* cmd)
51                 : content(1, ':')
52                 , tagsize(0)
53         {
54                 content.append(ServerInstance->Config->GetSID());
55                 push(cmd);
56                 FireEvent(ServerInstance->FakeClient->server, cmd, tags);
57         }
58
59         CmdBuilder(TreeServer* src, const char* cmd)
60                 : content(1, ':')
61                 , tagsize(0)
62         {
63                 content.append(src->GetId());
64                 push(cmd);
65                 FireEvent(src, cmd, tags);
66         }
67
68         CmdBuilder(User* src, const char* cmd)
69                 : content(1, ':')
70                 , tagsize(0)
71         {
72                 content.append(src->uuid);
73                 push(cmd);
74                 if (InspIRCd::IsSID(src->uuid))
75                         FireEvent(src->server, cmd, tags);
76                 else
77                         FireEvent(src, cmd, tags);
78         }
79
80         CmdBuilder& push_raw(const std::string& s)
81         {
82                 content.append(s);
83                 return *this;
84         }
85
86         CmdBuilder& push_raw(const char* s)
87         {
88                 content.append(s);
89                 return *this;
90         }
91
92         CmdBuilder& push_raw(char c)
93         {
94                 content.push_back(c);
95                 return *this;
96         }
97
98         template <typename T>
99         CmdBuilder& push_raw_int(T i)
100         {
101                 content.append(ConvToStr(i));
102                 return *this;
103         }
104
105         template <typename InputIterator>
106         CmdBuilder& push_raw(InputIterator first, InputIterator last)
107         {
108                 content.append(first, last);
109                 return *this;
110         }
111
112         CmdBuilder& push(const std::string& s)
113         {
114                 content.push_back(' ');
115                 content.append(s);
116                 return *this;
117         }
118
119         CmdBuilder& push(const char* s)
120         {
121                 content.push_back(' ');
122                 content.append(s);
123                 return *this;
124         }
125
126         CmdBuilder& push(char c)
127         {
128                 content.push_back(' ');
129                 content.push_back(c);
130                 return *this;
131         }
132
133         template <typename T>
134         CmdBuilder& push_int(T i)
135         {
136                 content.push_back(' ');
137                 content.append(ConvToStr(i));
138                 return *this;
139         }
140
141         CmdBuilder& push_last(const std::string& s)
142         {
143                 content.push_back(' ');
144                 content.push_back(':');
145                 content.append(s);
146                 return *this;
147         }
148
149         CmdBuilder& push_tags(ClientProtocol::TagMap newtags)
150         {
151                 // It has to be this way around so new tags get priority.
152                 newtags.insert(tags.begin(), tags.end());
153                 std::swap(tags, newtags);
154                 UpdateTags();
155                 return *this;
156         }
157
158         template<typename T>
159         CmdBuilder& insert(const T& cont)
160         {
161                 for (typename T::const_iterator i = cont.begin(); i != cont.end(); ++i)
162                         push(*i);
163                 return *this;
164         }
165
166         const std::string& str() const { return content; }
167         operator const std::string&() const { return str(); }
168
169         void Broadcast() const
170         {
171                 Utils->DoOneToMany(*this);
172         }
173
174         void Forward(TreeServer* omit) const
175         {
176                 Utils->DoOneToAllButSender(*this, omit);
177         }
178
179         void Unicast(User* target) const
180         {
181                 Utils->DoOneToOne(*this, target->server);
182         }
183 };