2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2018-2020 Sadie Powell <sadie@witchery.services>
5 * Copyright (C) 2015-2016 Attila Molnar <attilamolnar@hush.com>
7 * This file is part of InspIRCd. InspIRCd is free software: you can
8 * redistribute it and/or modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation, version 2.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 class WriteNumericSink;
26 class WriteRemoteNumericSink;
28 template <char Sep, bool SendEmpty, typename Sink>
31 template <char Sep = ',', bool SendEmpty = false>
34 template <unsigned int NumStaticParams, bool SendEmpty, typename Sink>
35 class GenericParamBuilder;
37 template <unsigned int NumStaticParams, bool SendEmpty = false>
41 class Numeric::WriteNumericSink
43 LocalUser* const user;
46 WriteNumericSink(LocalUser* u)
51 void operator()(Numeric& numeric) const
53 user->WriteNumeric(numeric);
57 class Numeric::WriteRemoteNumericSink
62 WriteRemoteNumericSink(User* u)
67 void operator()(Numeric& numeric) const
69 user->WriteRemoteNumeric(numeric);
73 template <char Sep, bool SendEmpty, typename Sink>
74 class Numeric::GenericBuilder
78 const std::string::size_type max;
80 bool HasRoom(const std::string::size_type additional) const
82 return (numeric.GetParams().back().size() + additional <= max);
86 GenericBuilder(Sink s, unsigned int num, bool addparam = true, size_t additionalsize = 0)
89 , max(ServerInstance->Config->Limits.MaxLine - ServerInstance->Config->ServerName.size() - additionalsize - 10)
92 numeric.push(std::string());
95 Numeric& GetNumeric() { return numeric; }
97 void Add(const std::string& entry)
99 if (!HasRoom(entry.size()))
101 numeric.GetParams().back().append(entry).push_back(Sep);
104 void Add(const std::string& entry1, const std::string& entry2)
106 if (!HasRoom(entry1.size() + entry2.size()))
108 numeric.GetParams().back().append(entry1).append(entry2).push_back(Sep);
113 std::string& data = numeric.GetParams().back();
121 data.erase(data.size()-1);
128 bool IsEmpty() const { return (numeric.GetParams().back().empty()); }
131 template <char Sep, bool SendEmpty>
132 class Numeric::Builder : public GenericBuilder<Sep, SendEmpty, WriteNumericSink>
135 Builder(LocalUser* user, unsigned int num, bool addparam = true, size_t additionalsize = 0)
136 : ::Numeric::GenericBuilder<Sep, SendEmpty, WriteNumericSink>(WriteNumericSink(user), num, addparam, additionalsize + user->nick.size())
141 template <unsigned int NumStaticParams, bool SendEmpty, typename Sink>
142 class Numeric::GenericParamBuilder
146 std::string::size_type currlen;
147 std::string::size_type max;
149 bool HasRoom(const std::string::size_type additional) const
151 return (currlen + additional <= max);
155 GenericParamBuilder(Sink s, unsigned int num, size_t additionalsize)
159 , max(ServerInstance->Config->Limits.MaxLine - ServerInstance->Config->ServerName.size() - additionalsize - 10)
163 void AddStatic(const std::string& entry)
165 max -= (entry.length() + 1);
166 numeric.GetParams().push_back(entry);
169 void Add(const std::string& entry)
171 if (!HasRoom(entry.size()))
174 currlen += entry.size() + 1;
175 numeric.GetParams().push_back(entry);
180 if ((!SendEmpty) && (IsEmpty()))
185 numeric.GetParams().erase(numeric.GetParams().begin() + NumStaticParams, numeric.GetParams().end());
188 bool IsEmpty() const { return (numeric.GetParams().size() <= NumStaticParams); }
191 template <unsigned int NumStaticParams, bool SendEmpty>
192 class Numeric::ParamBuilder : public GenericParamBuilder<NumStaticParams, SendEmpty, WriteNumericSink>
195 ParamBuilder(LocalUser* user, unsigned int num)
196 : ::Numeric::GenericParamBuilder<NumStaticParams, SendEmpty, WriteNumericSink>(WriteNumericSink(user), num, user->nick.size())
204 class InvalidModeParameter;
209 /** Builder for the ERR_CANNOTSENDTOCHAN and ERR_CANTSENDTOUSER numerics. */
210 class Numerics::CannotSendTo : public Numeric::Numeric
213 CannotSendTo(Channel* chan, const std::string& message)
214 : Numeric(ERR_CANNOTSENDTOCHAN)
220 CannotSendTo(Channel* chan, const std::string& what, ModeHandler* mh)
221 : Numeric(ERR_CANNOTSENDTOCHAN)
224 push(InspIRCd::Format("You cannot send %s to this channel whilst the +%c (%s) mode is set.",
225 what.c_str(), mh->GetModeChar(), mh->name.c_str()));
228 CannotSendTo(Channel* chan, const std::string& what, char extban, const std::string& extbandesc)
229 : Numeric(ERR_CANNOTSENDTOCHAN)
232 push(InspIRCd::Format("You cannot send %s to this channel whilst %s %c: (%s) extban is set matching you.",
233 what.c_str(), strchr("AEIOUaeiou", extban) ? "an" : "a", extban, extbandesc.c_str()));
236 CannotSendTo(User* user, const std::string& message)
237 : Numeric(ERR_CANTSENDTOUSER)
239 push(user->registered & REG_NICK ? user->nick : "*");
243 CannotSendTo(User* user, const std::string& what, ModeHandler* mh, bool self = false)
244 : Numeric(ERR_CANTSENDTOUSER)
246 push(user->registered & REG_NICK ? user->nick : "*");
247 push(InspIRCd::Format("You cannot send %s to this user whilst %s have the +%c (%s) mode set.",
248 what.c_str(), self ? "you" : "they", mh->GetModeChar(), mh->name.c_str()));
252 /* Builder for the ERR_INVALIDMODEPARAM numeric. */
253 class Numerics::InvalidModeParameter : public Numeric::Numeric
256 void push_message(ModeHandler* mode, const std::string& message)
258 if (!message.empty())
260 // The caller has specified their own message.
265 const std::string& syntax = mode->GetSyntax();
268 // If the mode has a syntax hint we include it in the message.
269 push(InspIRCd::Format("Invalid %s mode parameter. Syntax: %s.", mode->name.c_str(), syntax.c_str()));
273 // Otherwise, send it without.
274 push(InspIRCd::Format("Invalid %s mode parameter.", mode->name.c_str()));
279 InvalidModeParameter(Channel* chan, ModeHandler* mode, const std::string& parameter, const std::string& message = "")
280 : Numeric(ERR_INVALIDMODEPARAM)
283 push(mode->GetModeChar());
285 push_message(mode, message);
288 InvalidModeParameter(User* user, ModeHandler* mode, const std::string& parameter, const std::string& message = "")
289 : Numeric(ERR_INVALIDMODEPARAM)
291 push(user->registered & REG_NICK ? user->nick : "*");
292 push(mode->GetModeChar());
294 push_message(mode, message);
298 /** Builder for the ERR_NOSUCHCHANNEL numeric. */
299 class Numerics::NoSuchChannel : public Numeric::Numeric
302 NoSuchChannel(const std::string& chan)
303 : Numeric(ERR_NOSUCHCHANNEL)
305 push(chan.empty() ? "*" : chan);
306 push("No such channel");
310 /** Builder for the ERR_NOSUCHNICK numeric. */
311 class Numerics::NoSuchNick : public Numeric::Numeric
314 NoSuchNick(const std::string& nick)
315 : Numeric(ERR_NOSUCHNICK)
317 push(nick.empty() ? "*" : nick);
318 push("No such nick");