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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
/*
* InspIRCd -- Internet Relay Chat Daemon
*
* Copyright (C) 2018-2020 Sadie Powell <sadie@witchery.services>
* Copyright (C) 2015-2016 Attila Molnar <attilamolnar@hush.com>
*
* 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
namespace Numeric
{
class WriteNumericSink;
class WriteRemoteNumericSink;
template <char Sep, bool SendEmpty, typename Sink>
class GenericBuilder;
template <char Sep = ',', bool SendEmpty = false>
class Builder;
template <unsigned int NumStaticParams, bool SendEmpty, typename Sink>
class GenericParamBuilder;
template <unsigned int NumStaticParams, bool SendEmpty = false>
class ParamBuilder;
}
class Numeric::WriteNumericSink
{
LocalUser* const user;
public:
WriteNumericSink(LocalUser* u)
: user(u)
{
}
void operator()(Numeric& numeric) const
{
user->WriteNumeric(numeric);
}
};
class Numeric::WriteRemoteNumericSink
{
User* const user;
public:
WriteRemoteNumericSink(User* u)
: user(u)
{
}
void operator()(Numeric& numeric) const
{
user->WriteRemoteNumeric(numeric);
}
};
template <char Sep, bool SendEmpty, typename Sink>
class Numeric::GenericBuilder
{
Sink sink;
Numeric numeric;
const std::string::size_type max;
bool HasRoom(const std::string::size_type additional) const
{
return (numeric.GetParams().back().size() + additional <= max);
}
public:
GenericBuilder(Sink s, unsigned int num, bool addparam = true, size_t additionalsize = 0)
: sink(s)
, numeric(num)
, max(ServerInstance->Config->Limits.MaxLine - ServerInstance->Config->ServerName.size() - additionalsize - 10)
{
if (addparam)
numeric.push(std::string());
}
Numeric& GetNumeric() { return numeric; }
void Add(const std::string& entry)
{
if (!HasRoom(entry.size()))
Flush();
numeric.GetParams().back().append(entry).push_back(Sep);
}
void Add(const std::string& entry1, const std::string& entry2)
{
if (!HasRoom(entry1.size() + entry2.size()))
Flush();
numeric.GetParams().back().append(entry1).append(entry2).push_back(Sep);
}
void Flush()
{
std::string& data = numeric.GetParams().back();
if (IsEmpty())
{
if (!SendEmpty)
return;
}
else
{
data.erase(data.size()-1);
}
sink(numeric);
data.clear();
}
bool IsEmpty() const { return (numeric.GetParams().back().empty()); }
};
template <char Sep, bool SendEmpty>
class Numeric::Builder : public GenericBuilder<Sep, SendEmpty, WriteNumericSink>
{
public:
Builder(LocalUser* user, unsigned int num, bool addparam = true, size_t additionalsize = 0)
: ::Numeric::GenericBuilder<Sep, SendEmpty, WriteNumericSink>(WriteNumericSink(user), num, addparam, additionalsize + user->nick.size())
{
}
};
template <unsigned int NumStaticParams, bool SendEmpty, typename Sink>
class Numeric::GenericParamBuilder
{
Sink sink;
Numeric numeric;
std::string::size_type currlen;
std::string::size_type max;
bool HasRoom(const std::string::size_type additional) const
{
return (currlen + additional <= max);
}
public:
GenericParamBuilder(Sink s, unsigned int num, size_t additionalsize)
: sink(s)
, numeric(num)
, currlen(0)
, max(ServerInstance->Config->Limits.MaxLine - ServerInstance->Config->ServerName.size() - additionalsize - 10)
{
}
void AddStatic(const std::string& entry)
{
max -= (entry.length() + 1);
numeric.GetParams().push_back(entry);
}
void Add(const std::string& entry)
{
if (!HasRoom(entry.size()))
Flush();
currlen += entry.size() + 1;
numeric.GetParams().push_back(entry);
}
void Flush()
{
if ((!SendEmpty) && (IsEmpty()))
return;
sink(numeric);
currlen = 0;
numeric.GetParams().erase(numeric.GetParams().begin() + NumStaticParams, numeric.GetParams().end());
}
bool IsEmpty() const { return (numeric.GetParams().size() <= NumStaticParams); }
};
template <unsigned int NumStaticParams, bool SendEmpty>
class Numeric::ParamBuilder : public GenericParamBuilder<NumStaticParams, SendEmpty, WriteNumericSink>
{
public:
ParamBuilder(LocalUser* user, unsigned int num)
: ::Numeric::GenericParamBuilder<NumStaticParams, SendEmpty, WriteNumericSink>(WriteNumericSink(user), num, user->nick.size())
{
}
};
namespace Numerics
{
class CannotSendTo;
class InvalidModeParameter;
class NoSuchChannel;
class NoSuchNick;
}
/** Builder for the ERR_CANNOTSENDTOCHAN and ERR_CANTSENDTOUSER numerics. */
class Numerics::CannotSendTo : public Numeric::Numeric
{
public:
CannotSendTo(Channel* chan, const std::string& message)
: Numeric(ERR_CANNOTSENDTOCHAN)
{
push(chan->name);
push(message);
}
CannotSendTo(Channel* chan, const std::string& what, ModeHandler* mh)
: Numeric(ERR_CANNOTSENDTOCHAN)
{
push(chan->name);
push(InspIRCd::Format("You cannot send %s to this channel whilst the +%c (%s) mode is set.",
what.c_str(), mh->GetModeChar(), mh->name.c_str()));
}
CannotSendTo(Channel* chan, const std::string& what, char extban, const std::string& extbandesc)
: Numeric(ERR_CANNOTSENDTOCHAN)
{
push(chan->name);
push(InspIRCd::Format("You cannot send %s to this channel whilst %s %c: (%s) extban is set on you.",
what.c_str(), strchr("AEIOUaeiou", extban) ? "an" : "a", extban, extbandesc.c_str()));
}
CannotSendTo(User* user, const std::string& message)
: Numeric(ERR_CANTSENDTOUSER)
{
push(user->registered & REG_NICK ? user->nick : "*");
push(message);
}
CannotSendTo(User* user, const std::string& what, ModeHandler* mh, bool self = false)
: Numeric(ERR_CANTSENDTOUSER)
{
push(user->registered & REG_NICK ? user->nick : "*");
push(InspIRCd::Format("You cannot send %s to this user whilst %s have the +%c (%s) mode set.",
what.c_str(), self ? "you" : "they", mh->GetModeChar(), mh->name.c_str()));
}
};
/* Builder for the ERR_INVALIDMODEPARAM numeric. */
class Numerics::InvalidModeParameter : public Numeric::Numeric
{
private:
void push_message(ModeHandler* mode, const std::string& message)
{
if (!message.empty())
{
// The caller has specified their own message.
push(message);
return;
}
const std::string& syntax = mode->GetSyntax();
if (!syntax.empty())
{
// If the mode has a syntax hint we include it in the message.
push(InspIRCd::Format("Invalid %s mode parameter. Syntax: %s.", mode->name.c_str(), syntax.c_str()));
}
else
{
// Otherwise, send it without.
push(InspIRCd::Format("Invalid %s mode parameter.", mode->name.c_str()));
}
}
public:
InvalidModeParameter(Channel* chan, ModeHandler* mode, const std::string& parameter, const std::string& message = "")
: Numeric(ERR_INVALIDMODEPARAM)
{
push(chan->name);
push(mode->GetModeChar());
push(parameter);
push_message(mode, message);
}
InvalidModeParameter(User* user, ModeHandler* mode, const std::string& parameter, const std::string& message = "")
: Numeric(ERR_INVALIDMODEPARAM)
{
push(user->registered & REG_NICK ? user->nick : "*");
push(mode->GetModeChar());
push(parameter);
push_message(mode, message);
}
};
/** Builder for the ERR_NOSUCHCHANNEL numeric. */
class Numerics::NoSuchChannel : public Numeric::Numeric
{
public:
NoSuchChannel(const std::string& chan)
: Numeric(ERR_NOSUCHCHANNEL)
{
push(chan.empty() ? "*" : chan);
push("No such channel");
}
};
/** Builder for the ERR_NOSUCHNICK numeric. */
class Numerics::NoSuchNick : public Numeric::Numeric
{
public:
NoSuchNick(const std::string& nick)
: Numeric(ERR_NOSUCHNICK)
{
push(nick.empty() ? "*" : nick);
push("No such nick");
}
};
|