summaryrefslogtreecommitdiff
path: root/src/modules/m_spanningtree/commandbuilder.h
blob: 527b535847109d9267799128f3fc592e638f903f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * InspIRCd -- Internet Relay Chat Daemon
 *
 *   Copyright (C) 2019 Sadie Powell <sadie@witchery.services>
 *   Copyright (C) 2013-2015 Attila Molnar <attilamolnar@hush.com>
 *   Copyright (C) 2013 Adam <Adam@anope.org>
 *
 * 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
 * License as published by the Free Software Foundation, version 2.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#pragma once

#include "utils.h"

class TreeServer;

class CmdBuilder
{
 protected:
	/** The raw message contents. */
	std::string content;

	/** Tags which have been added to this message. */
	ClientProtocol::TagMap tags;

	/** The size of tags within the contents. */
	size_t tagsize;

	/** Fires the ServerProtocol::MessageEventListener::OnBuildMessage event for a server target. */
	void FireEvent(Server* target, const char* cmd, ClientProtocol::TagMap& taglist);

	/** Fires the ServerProtocol::MessageEventListener::OnBuildMessage event for a user target. */
	void FireEvent(User* target, const char* cmd, ClientProtocol::TagMap& taglist);

	/** Updates the tag string within the buffer. */
	void UpdateTags();

 public:
	CmdBuilder(const char* cmd)
		: content(1, ':')
		, tagsize(0)
	{
		content.append(ServerInstance->Config->GetSID());
		push(cmd);
		FireEvent(ServerInstance->FakeClient->server, cmd, tags);
	}

	CmdBuilder(TreeServer* src, const char* cmd)
		: content(1, ':')
		, tagsize(0)
	{
		content.append(src->GetId());
		push(cmd);
		FireEvent(src, cmd, tags);
	}

	CmdBuilder(User* src, const char* cmd)
		: content(1, ':')
		, tagsize(0)
	{
		content.append(src->uuid);
		push(cmd);
		if (InspIRCd::IsSID(src->uuid))
			FireEvent(src->server, cmd, tags);
		else
			FireEvent(src, cmd, tags);
	}

	CmdBuilder& push_raw(const std::string& s)
	{
		content.append(s);
		return *this;
	}

	CmdBuilder& push_raw(const char* s)
	{
		content.append(s);
		return *this;
	}

	CmdBuilder& push_raw(char c)
	{
		content.push_back(c);
		return *this;
	}

	template <typename T>
	CmdBuilder& push_raw_int(T i)
	{
		content.append(ConvToStr(i));
		return *this;
	}

	template <typename InputIterator>
	CmdBuilder& push_raw(InputIterator first, InputIterator last)
	{
		content.append(first, last);
		return *this;
	}

	CmdBuilder& push(const std::string& s)
	{
		content.push_back(' ');
		content.append(s);
		return *this;
	}

	CmdBuilder& push(const char* s)
	{
		content.push_back(' ');
		content.append(s);
		return *this;
	}

	CmdBuilder& push(char c)
	{
		content.push_back(' ');
		content.push_back(c);
		return *this;
	}

	template <typename T>
	CmdBuilder& push_int(T i)
	{
		content.push_back(' ');
		content.append(ConvToStr(i));
		return *this;
	}

	CmdBuilder& push_last(const std::string& s)
	{
		content.push_back(' ');
		content.push_back(':');
		content.append(s);
		return *this;
	}

	CmdBuilder& push_tags(ClientProtocol::TagMap newtags)
	{
		// It has to be this way around so new tags get priority.
		newtags.insert(tags.begin(), tags.end());
		std::swap(tags, newtags);
		UpdateTags();
		return *this;
	}

	template<typename T>
	CmdBuilder& insert(const T& cont)
	{
		for (typename T::const_iterator i = cont.begin(); i != cont.end(); ++i)
			push(*i);
		return *this;
	}

	const std::string& str() const { return content; }
	operator const std::string&() const { return str(); }

	void Broadcast() const
	{
		Utils->DoOneToMany(*this);
	}

	void Forward(TreeServer* omit) const
	{
		Utils->DoOneToAllButSender(*this, omit);
	}

	void Unicast(User* target) const
	{
		Utils->DoOneToOne(*this, target->server);
	}
};