2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2015-2016 Attila Molnar <attilamolnar@hush.com>
6 * This file is part of InspIRCd. InspIRCd is free software: you can
7 * redistribute it and/or modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation, version 2.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class WriteNumericSink;
25 class WriteRemoteNumericSink;
27 template <char Sep, bool SendEmpty, typename Sink>
30 template <char Sep = ',', bool SendEmpty = false>
33 template <unsigned int NumStaticParams, bool SendEmpty, typename Sink>
34 class GenericParamBuilder;
36 template <unsigned int NumStaticParams, bool SendEmpty = false>
40 class Numeric::WriteNumericSink
42 LocalUser* const user;
45 WriteNumericSink(LocalUser* u)
50 void operator()(Numeric& numeric) const
52 user->WriteNumeric(numeric);
56 class Numeric::WriteRemoteNumericSink
61 WriteRemoteNumericSink(User* u)
66 void operator()(Numeric& numeric) const
68 user->WriteRemoteNumeric(numeric);
72 template <char Sep, bool SendEmpty, typename Sink>
73 class Numeric::GenericBuilder
77 const std::string::size_type max;
79 bool HasRoom(const std::string::size_type additional) const
81 return (numeric.GetParams().back().size() + additional <= max);
85 GenericBuilder(Sink s, unsigned int num, bool addparam = true, size_t additionalsize = 0)
88 , max(ServerInstance->Config->Limits.MaxLine - ServerInstance->Config->ServerName.size() - additionalsize - 10)
91 numeric.push(std::string());
94 Numeric& GetNumeric() { return numeric; }
96 void Add(const std::string& entry)
98 if (!HasRoom(entry.size()))
100 numeric.GetParams().back().append(entry).push_back(Sep);
103 void Add(const std::string& entry1, const std::string& entry2)
105 if (!HasRoom(entry1.size() + entry2.size()))
107 numeric.GetParams().back().append(entry1).append(entry2).push_back(Sep);
112 std::string& data = numeric.GetParams().back();
120 data.erase(data.size()-1);
127 bool IsEmpty() const { return (numeric.GetParams().back().empty()); }
130 template <char Sep, bool SendEmpty>
131 class Numeric::Builder : public GenericBuilder<Sep, SendEmpty, WriteNumericSink>
134 Builder(LocalUser* user, unsigned int num, bool addparam = true, size_t additionalsize = 0)
135 : ::Numeric::GenericBuilder<Sep, SendEmpty, WriteNumericSink>(WriteNumericSink(user), num, addparam, additionalsize + user->nick.size())
140 template <unsigned int NumStaticParams, bool SendEmpty, typename Sink>
141 class Numeric::GenericParamBuilder
145 std::string::size_type currlen;
146 std::string::size_type max;
148 bool HasRoom(const std::string::size_type additional) const
150 return (currlen + additional <= max);
154 GenericParamBuilder(Sink s, unsigned int num, size_t additionalsize)
158 , max(ServerInstance->Config->Limits.MaxLine - ServerInstance->Config->ServerName.size() - additionalsize - 10)
162 void AddStatic(const std::string& entry)
164 max -= (entry.length() + 1);
165 numeric.GetParams().push_back(entry);
168 void Add(const std::string& entry)
170 if (!HasRoom(entry.size()))
173 currlen += entry.size() + 1;
174 numeric.GetParams().push_back(entry);
179 if ((!SendEmpty) && (IsEmpty()))
184 numeric.GetParams().erase(numeric.GetParams().begin() + NumStaticParams, numeric.GetParams().end());
187 bool IsEmpty() const { return (numeric.GetParams().size() <= NumStaticParams); }
190 template <unsigned int NumStaticParams, bool SendEmpty>
191 class Numeric::ParamBuilder : public GenericParamBuilder<NumStaticParams, SendEmpty, WriteNumericSink>
194 ParamBuilder(LocalUser* user, unsigned int num)
195 : ::Numeric::GenericParamBuilder<NumStaticParams, SendEmpty, WriteNumericSink>(WriteNumericSink(user), num, user->nick.size())
202 class InvalidModeParameter;
207 /* Builder for the ERR_INVALIDMODEPARAM numeric. */
208 class Numerics::InvalidModeParameter : public Numeric::Numeric
211 InvalidModeParameter(Channel* chan, ModeHandler* mode, const std::string& parameter, const std::string& message = "")
212 : Numeric(ERR_INVALIDMODEPARAM)
215 push(mode->GetModeChar());
217 push(message.empty() ? InspIRCd::Format("Invalid %s mode parameter", mode->name.c_str()) : message);
220 InvalidModeParameter(User* user, ModeHandler* mode, const std::string& parameter, const std::string& message = "")
221 : Numeric(ERR_INVALIDMODEPARAM)
223 push(user->registered & REG_NICK ? user->nick : "*");
224 push(mode->GetModeChar());
226 push(message.empty() ? InspIRCd::Format("Invalid %s mode parameter", mode->name.c_str()) : message);
230 /** Builder for the ERR_NOSUCHCHANNEL numeric. */
231 class Numerics::NoSuchChannel : public Numeric::Numeric
234 NoSuchChannel(const std::string& chan)
235 : Numeric(ERR_NOSUCHCHANNEL)
238 push("No such channel");
242 /** Builder for the ERR_NOSUCHNICK numeric. */
243 class Numerics::NoSuchNick : public Numeric::Numeric
246 NoSuchNick(const std::string& nick)
247 : Numeric(ERR_NOSUCHNICK)
250 push("No such nick");