From 28dcc1f9e017152f03b0d9bfbcc494260b015a0a Mon Sep 17 00:00:00 2001 From: Attila Molnar Date: Thu, 25 Feb 2016 15:25:02 +0100 Subject: [PATCH] Add Numeric::Numeric --- include/inspircd.h | 1 + include/numeric.h | 88 ++++++++++++++++++++++++++++++++++++++++ include/numericbuilder.h | 2 +- include/numerics.h | 2 +- src/users.cpp | 16 ++++++++ 5 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 include/numeric.h diff --git a/include/inspircd.h b/include/inspircd.h index 188434620..931148884 100644 --- a/include/inspircd.h +++ b/include/inspircd.h @@ -73,6 +73,7 @@ struct fakederef #include "extensible.h" #include "fileutils.h" #include "numerics.h" +#include "numeric.h" #include "uid.h" #include "server.h" #include "users.h" diff --git a/include/numeric.h b/include/numeric.h new file mode 100644 index 000000000..e7438b53a --- /dev/null +++ b/include/numeric.h @@ -0,0 +1,88 @@ +/* + * InspIRCd -- Internet Relay Chat Daemon + * + * Copyright (C) 2016 Attila Molnar + * + * 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 . + */ + + +#pragma once + +#include "numerics.h" + +namespace Numeric +{ + class Numeric; +} + +class Numeric::Numeric +{ + /** Numeric number + */ + unsigned int numeric; + + /** Parameters of the numeric + */ + std::vector params; + + public: + /** Constructor + * @param num Numeric number (RPL_*, ERR_*) + */ + Numeric(unsigned int num) + : numeric(num) + { + } + + /** Add a parameter to the numeric. The parameter will be converted to a string first with ConvToStr(). + * @param x Parameter to add + */ + template + Numeric& push(const T& x) + { + params.push_back(ConvToStr(x)); + return *this; + } + + /** Get the number of the numeric as an unsigned integer + * @return Numeric number as an unsigned integer + */ + unsigned int GetNumeric() const { return numeric; } + + /** Get the parameters of the numeric + * @return Parameters of the numeric as a const vector of strings + */ + const std::vector& GetParams() const { return params; } + + /** Get the parameters of the numeric + * @return Parameters of the numeric as a vector of strings + */ + std::vector& GetParams() { return params; } +}; + +namespace Numerics +{ + /** ERR_NOSUCHNICK numeric + */ + class NoSuchNick : public Numeric::Numeric + { + public: + NoSuchNick(const std::string& nick) + : Numeric(ERR_NOSUCHNICK) + { + push(nick); + push("No such nick/channel"); + } + }; +} diff --git a/include/numericbuilder.h b/include/numericbuilder.h index 36cfeedb4..371f275a9 100644 --- a/include/numericbuilder.h +++ b/include/numericbuilder.h @@ -113,7 +113,7 @@ class Numeric::Builder : public GenericBuilder { public: Builder(LocalUser* user, unsigned int num, bool addparam = true, size_t additionalsize = 0) - : Numeric::GenericBuilder(WriteNumericSink(user), num, addparam, additionalsize + user->nick.size()) + : ::Numeric::GenericBuilder(WriteNumericSink(user), num, addparam, additionalsize + user->nick.size()) { } }; diff --git a/include/numerics.h b/include/numerics.h index 0447df353..72caaddcc 100644 --- a/include/numerics.h +++ b/include/numerics.h @@ -35,7 +35,7 @@ * Please note that the list may not be exhaustive, it'll be done when I have * nothing better to do with my time. -- w00t (jul 13, 2008) */ -enum Numerics +enum { /* * Reply range of numerics. diff --git a/src/users.cpp b/src/users.cpp index fd4afbcef..915afd8b8 100644 --- a/src/users.cpp +++ b/src/users.cpp @@ -786,6 +786,22 @@ void User::WriteCommand(const char* command, const std::string& text) this->WriteServ(command + (this->registered & REG_NICK ? " " + this->nick : " *") + " " + text); } +namespace +{ + std::string BuildNumeric(const std::string& source, User* targetuser, unsigned int num, const std::vector& params) + { + const char* const target = (targetuser->registered & REG_NICK ? targetuser->nick.c_str() : "*"); + std::string raw = InspIRCd::Format(":%s %03u %s", source.c_str(), num, target); + if (!params.empty()) + { + for (std::vector::const_iterator i = params.begin(); i != params.end()-1; ++i) + raw.append(1, ' ').append(*i); + raw.append(" :").append(params.back()); + } + return raw; + } +} + void User::WriteNumeric(unsigned int numeric, const char* text, ...) { std::string textbuffer; -- 2.39.5