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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
/* Main processing code for Services.
*
* (C) 2003-2024 Anope Team
* Contact us at team@anope.org
*
* Please read COPYING and README for further details.
*
* Based on the original code of Epona by Lara.
* Based on the original code of Services by Andy Church.
*/
#include "services.h"
#include "modules.h"
#include "protocol.h"
#include "servers.h"
#include "users.h"
#include "regchannel.h"
void Anope::Process(const Anope::string &buffer)
{
/* If debugging, log the buffer */
Log(LOG_RAWIO) << "Received: " << buffer;
if (buffer.empty())
return;
Anope::map<Anope::string> tags;
Anope::string source, command;
std::vector<Anope::string> params;
if (!IRCD->Parse(buffer, tags, source, command, params))
return;
if (Anope::ProtocolDebug)
{
if (tags.empty())
Log() << "No tags";
else
for (Anope::map<Anope::string>::const_iterator it = tags.begin(); it != tags.end(); ++it)
Log() << "tags " << it->first << ": " << it->second;
Log() << "Source : " << (source.empty() ? "No source" : source);
Log() << "Command: " << command;
if (params.empty())
Log() << "No params";
else
for (unsigned i = 0; i < params.size(); ++i)
Log() << "params " << i << ": " << params[i];
}
static const Anope::string proto_name = ModuleManager::FindFirstOf(PROTOCOL) ? ModuleManager::FindFirstOf(PROTOCOL)->name : "";
MessageSource src(source);
EventReturn MOD_RESULT;
FOREACH_RESULT(OnMessage, MOD_RESULT, (src, command, params, tags));
if (MOD_RESULT == EVENT_STOP)
return;
ServiceReference<IRCDMessage> m("IRCDMessage", proto_name + "/" + command.lower());
if (!m)
{
Log(LOG_DEBUG) << "unknown message from server (" << buffer << ")";
return;
}
if (m->HasFlag(IRCDMESSAGE_SOFT_LIMIT) ? (params.size() < m->GetParamCount()) : (params.size() != m->GetParamCount()))
Log(LOG_DEBUG) << "invalid parameters for " << command << ": " << params.size() << " != " << m->GetParamCount();
else if (m->HasFlag(IRCDMESSAGE_REQUIRE_USER) && !src.GetUser())
Log(LOG_DEBUG) << "unexpected non-user source " << source << " for " << command;
else if (m->HasFlag(IRCDMESSAGE_REQUIRE_SERVER) && !source.empty() && !src.GetServer())
Log(LOG_DEBUG) << "unexpected non-server source " << source << " for " << command;
else
m->Run(src, params, tags);
}
bool IRCDProto::Parse(const Anope::string &buffer, Anope::map<Anope::string> &tags, Anope::string &source, Anope::string &command, std::vector<Anope::string> ¶ms)
{
MessageTokenizer tokens(buffer);
// This will always exist because of the check in Anope::Process.
Anope::string token;
tokens.GetMiddle(token);
if (token[0] == '@')
{
// The line begins with message tags.
sepstream tagstream(token.substr(1), ';');
while (tagstream.GetToken(token))
{
const Anope::string::size_type valsep = token.find('=');
if (valsep == Anope::string::npos)
{
// Tag has no value.
tags[token];
}
else
{
// Tag has a value
tags[token.substr(0, valsep)] = token.substr(valsep + 1);
}
}
if (!tokens.GetMiddle(token))
return false;
}
if (token[0] == ':')
{
source = token.substr(1);
if (!tokens.GetMiddle(token))
return false;
}
// Store the command name.
command = token;
// Retrieve all of the parameters.
while (tokens.GetTrailing(token))
params.push_back(token);
return true;
}
bool IRCDProto::Format(Anope::string &message, const Anope::map<Anope::string> &tags, const MessageSource &source, const Anope::string &command, const std::vector<Anope::string> ¶ms)
{
std::stringstream buffer;
if (CanSendTags && !tags.empty())
{
char separator = '@';
for (const auto &[tname, tvalue] : tags)
{
buffer << separator << tname;
if (!tvalue.empty())
buffer << '=' << tvalue;
separator = ';';
}
buffer << ' ';
}
if (source.GetServer())
{
const auto *s = source.GetServer();
if (s != Me && !s->IsJuped())
{
Log(LOG_DEBUG) << "Attempted to send \"" << command << "\" from " << s->GetName() << " who is not from me";
return false;
}
buffer << ':' << s->GetSID() << ' ';
}
else if (source.GetUser())
{
const auto *u = source.GetUser();
if (u->server != Me && !u->server->IsJuped())
{
Log(LOG_DEBUG) << "Attempted to send \"" << command << "\" from " << u->nick << " who is not from me";
return false;
}
const auto *bi = source.GetBot();
if (bi && !bi->introduced)
{
Log(LOG_DEBUG) << "Attempted to send \"" << command << "\" from " << bi->nick << " when not introduced";
return false;
}
buffer << ':' << u->GetUID() << ' ';
}
buffer << command;
if (!params.empty())
{
buffer << ' ';
for (auto it = params.begin(); it != params.end() - 1; ++it)
buffer << *it << ' ';
const auto &last = params.back();
if (last.empty() || last[0] == ':' || last.find(' ') != std::string::npos)
buffer << ':';
buffer << last;
}
message = buffer.str();
return true;
}
MessageTokenizer::MessageTokenizer(const Anope::string &msg)
: message(msg)
{
}
bool MessageTokenizer::GetMiddle(Anope::string &token)
{
// If we are past the end of the string we can't do anything.
if (position >= message.length())
{
token.clear();
return false;
}
// If we can't find another separator this is the last token in the message.
Anope::string::size_type separator = message.find(' ', position);
if (separator == Anope::string::npos)
{
token = message.substr(position);
position = message.length();
return true;
}
token = message.substr(position, separator - position);
position = message.find_first_not_of(' ', separator);
return true;
}
bool MessageTokenizer::GetTrailing(Anope::string &token)
{
// If we are past the end of the string we can't do anything.
if (position >= message.length())
{
token.clear();
return false;
}
// If this is true then we have a <trailing> token!
if (message[position] == ':')
{
token = message.substr(position + 1);
position = message.length();
return true;
}
// There is no <trailing> token so it must be a <middle> token.
return GetMiddle(token);
}
|