2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2015 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;
26 template <char Sep, bool SendEmpty, typename Sink>
29 template <char Sep = ',', bool SendEmpty = false>
33 class Numeric::WriteNumericSink
35 LocalUser* const user;
38 WriteNumericSink(LocalUser* u)
43 void operator()(unsigned int numeric, const std::string& params) const
45 user->WriteNumeric(numeric, params);
49 template <char Sep, bool SendEmpty, typename Sink>
50 class Numeric::GenericBuilder
54 const unsigned int numeric;
55 const std::string::size_type max;
56 std::string::size_type beginpos;
58 bool HasRoom(const std::string::size_type additional) const
60 return (data.size() + additional <= max);
64 GenericBuilder(Sink s, unsigned int num, bool addparam = true, size_t additionalsize = 0)
67 , max(ServerInstance->Config->Limits.MaxLine - ServerInstance->Config->ServerName.size() - additionalsize - 9)
74 std::string& GetNumeric() { return data; }
76 void Add(const std::string& entry)
78 if (!HasRoom(entry.size()))
80 data.append(entry).push_back(Sep);
83 void Add(const std::string& entry1, const std::string& entry2)
85 if (!HasRoom(entry1.size() + entry2.size()))
87 data.append(entry1).append(entry2).push_back(Sep);
99 data.erase(data.size()-1);
103 if (data.size() > beginpos)
104 data.erase(beginpos);
107 bool IsEmpty() const { return (data.size() == beginpos); }
108 void SaveBeginPos() { beginpos = data.size(); }
111 template <char Sep, bool SendEmpty>
112 class Numeric::Builder : public GenericBuilder<Sep, SendEmpty, WriteNumericSink>
115 Builder(LocalUser* user, unsigned int num, bool addparam = true, size_t additionalsize = 0)
116 : Numeric::GenericBuilder<Sep, SendEmpty, WriteNumericSink>(WriteNumericSink(user), num, addparam, additionalsize + user->nick.size())