X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;ds=sidebyside;f=include%2Fnumericbuilder.h;h=73bcc2c97f67ec28a7d0c187c3e8ae55f19adc99;hb=235a986964ff09dcc8662ea078518cad6bfbf11b;hp=726aeff3f5c78f8cff1ff7a6c59d5d810e49caf9;hpb=761e6d75ba37b984998952940ed681e79e456142;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/include/numericbuilder.h b/include/numericbuilder.h index 726aeff3f..73bcc2c97 100644 --- a/include/numericbuilder.h +++ b/include/numericbuilder.h @@ -1,7 +1,8 @@ /* * InspIRCd -- Internet Relay Chat Daemon * - * Copyright (C) 2015 Attila Molnar + * Copyright (C) 2018-2019 Sadie Powell + * Copyright (C) 2015-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 @@ -22,12 +23,19 @@ namespace Numeric { class WriteNumericSink; + class WriteRemoteNumericSink; template class GenericBuilder; template class Builder; + + template + class GenericParamBuilder; + + template + class ParamBuilder; } class Numeric::WriteNumericSink @@ -46,6 +54,22 @@ class Numeric::WriteNumericSink } }; +class Numeric::WriteRemoteNumericSink +{ + User* const user; + + public: + WriteRemoteNumericSink(User* u) + : user(u) + { + } + + void operator()(Numeric& numeric) const + { + user->WriteRemoteNumeric(numeric); + } +}; + template class Numeric::GenericBuilder { @@ -62,7 +86,7 @@ class Numeric::GenericBuilder 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 - 9) + , max(ServerInstance->Config->Limits.MaxLine - ServerInstance->Config->ServerName.size() - additionalsize - 10) { if (addparam) numeric.push(std::string()); @@ -113,3 +137,140 @@ class Numeric::Builder : public GenericBuilder { } }; + +template +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 +class Numeric::ParamBuilder : public GenericParamBuilder +{ + public: + ParamBuilder(LocalUser* user, unsigned int num) + : ::Numeric::GenericParamBuilder(WriteNumericSink(user), num, user->nick.size()) + { + } +}; + +namespace Numerics +{ + class InvalidModeParameter; + class NoSuchChannel; + class NoSuchNick; +} + +/* 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"); + } +};