# HG changeset patch # User David Demelier # Date 1534184100 -7200 # Node ID 06c5a76fedd2efd41b6cbc3e4de01225166c0e51 # Parent 565a1cef4405274a6a0c0092d10a20b71eeac4fc Irccd: style (server) diff -r 565a1cef4405 -r 06c5a76fedd2 libirccd/irccd/daemon/server_service.cpp --- a/libirccd/irccd/daemon/server_service.cpp Wed Oct 24 21:15:00 2018 +0200 +++ b/libirccd/irccd/daemon/server_service.cpp Mon Aug 13 20:15:00 2018 +0200 @@ -220,15 +220,15 @@ dispatch(ev.server->get_id(), ev.origin, ev.channel, [=] (plugin& plugin) -> std::string { - return server_util::parse_message( + return server_util::message_type::parse( ev.message, ev.server->get_command_char(), plugin.get_id() - ).type == server_util::message_pack::type::command ? "onCommand" : "onMessage"; + ).type == server_util::message_type::type::command ? "onCommand" : "onMessage"; }, [=] (plugin& plugin) mutable { auto copy = ev; - auto pack = server_util::parse_message( + auto pack = server_util::message_type::parse( copy.message, copy.server->get_command_char(), plugin.get_id() @@ -236,7 +236,7 @@ copy.message = pack.message; - if (pack.type == server_util::message_pack::type::command) + if (pack.type == server_util::message_type::type::command) plugin.handle_command(irccd_, copy); else plugin.handle_message(irccd_, copy); diff -r 565a1cef4405 -r 06c5a76fedd2 libirccd/irccd/daemon/server_util.cpp --- a/libirccd/irccd/daemon/server_util.cpp Wed Oct 24 21:15:00 2018 +0200 +++ b/libirccd/irccd/daemon/server_util.cpp Mon Aug 13 20:15:00 2018 +0200 @@ -26,9 +26,7 @@ #include "server.hpp" #include "server_util.hpp" -namespace irccd { - -namespace server_util { +namespace irccd::server_util { namespace { @@ -180,65 +178,9 @@ } // !namespace -std::shared_ptr from_json(boost::asio::io_service& service, const nlohmann::json& object) -{ - // Mandatory parameters. - const json_util::document parser(object); - const auto id = parser.get("name"); - const auto host = parser.get("host"); - - if (!id || !string_util::is_identifier(*id)) - throw server_error(server_error::invalid_identifier); - if (!host || host->empty()) - throw server_error(server_error::invalid_hostname); - - const auto sv = std::make_shared(service, *id, *host); - - from_json_load_options(*sv, parser); - from_json_load_flags(*sv, parser); - - return sv; -} - -std::shared_ptr from_config(boost::asio::io_service& service, - const config& cfg, - const ini::section& sc) -{ - // Mandatory parameters. - const auto id = sc.get("name"); - const auto host = sc.get("hostname"); - - if (!string_util::is_identifier(id.value())) - throw server_error(server_error::invalid_identifier); - if (host.value().empty()) - throw server_error(server_error::invalid_hostname); - - const auto sv = std::make_shared(service, id.value(), host.value()); - - from_config_load_channels(*sv, sc); - from_config_load_flags(*sv, sc); - from_config_load_numeric_parameters(*sv, sc); - from_config_load_options(*sv, sc); - - // Identity is in a separate section - const auto identity = sc.get("identity"); - - if (identity.value().size() > 0) { - const auto it = std::find_if(cfg.begin(), cfg.end(), [&] (const auto& i) { - if (i.key() != "identity") - return false; - - return i.get("name").value() == identity.value(); - }); - - if (it != cfg.end()) - from_config_load_identity(*sv, *it); - } - - return sv; -} - -message_pack parse_message(std::string_view message, std::string_view cchar, std::string_view plugin) +auto message_type::parse(std::string_view message, + std::string_view cchar, + std::string_view plugin) -> message_type { auto result = std::string(message); auto cc = std::string(cchar); @@ -274,11 +216,67 @@ } return { - iscommand ? message_pack::type::command : message_pack::type::message, + iscommand ? message_type::type::command : message_type::type::message, result }; } -} // !server_util +auto from_json(boost::asio::io_service& service, const nlohmann::json& object) -> std::shared_ptr +{ + // Mandatory parameters. + const json_util::document parser(object); + const auto id = parser.get("name"); + const auto host = parser.get("host"); + + if (!id || !string_util::is_identifier(*id)) + throw server_error(server_error::invalid_identifier); + if (!host || host->empty()) + throw server_error(server_error::invalid_hostname); + + const auto sv = std::make_shared(service, *id, *host); + + from_json_load_options(*sv, parser); + from_json_load_flags(*sv, parser); + + return sv; +} + +auto from_config(boost::asio::io_service& service, + const config& cfg, + const ini::section& sc) -> std::shared_ptr +{ + // Mandatory parameters. + const auto id = sc.get("name"); + const auto host = sc.get("hostname"); -} // !irccd + if (!string_util::is_identifier(id.value())) + throw server_error(server_error::invalid_identifier); + if (host.value().empty()) + throw server_error(server_error::invalid_hostname); + + const auto sv = std::make_shared(service, id.value(), host.value()); + + from_config_load_channels(*sv, sc); + from_config_load_flags(*sv, sc); + from_config_load_numeric_parameters(*sv, sc); + from_config_load_options(*sv, sc); + + // Identity is in a separate section. + const auto identity = sc.get("identity"); + + if (identity.value().size() > 0) { + const auto it = std::find_if(cfg.begin(), cfg.end(), [&] (const auto& i) { + if (i.key() != "identity") + return false; + + return i.get("name").value() == identity.value(); + }); + + if (it != cfg.end()) + from_config_load_identity(*sv, *it); + } + + return sv; +} + +} // !irccd::server_util diff -r 565a1cef4405 -r 06c5a76fedd2 libirccd/irccd/daemon/server_util.hpp --- a/libirccd/irccd/daemon/server_util.hpp Wed Oct 24 21:15:00 2018 +0200 +++ b/libirccd/irccd/daemon/server_util.hpp Mon Aug 13 20:15:00 2018 +0200 @@ -25,6 +25,7 @@ */ #include +#include #include @@ -55,8 +56,7 @@ * Example: `!reminder help' may invoke the command event if a plugin reminder * exists. */ -class message_pack { -public: +struct message_type { /** * \brief Describe which type of message has been received */ @@ -69,6 +69,21 @@ * Message content. */ std::string message; + + /** + * Parse IRC message and determine if it's a command or a simple message. + * + * If it's a command, the plugin invocation command is removed from the + * original message, otherwise it is copied verbatime. + * + * \param message the message line + * \param cchar the command char (e.g '!') + * \param plugin the plugin name + * \return the pair + */ + static auto parse(std::string_view message, + std::string_view cchar, + std::string_view plugin) -> message_type; }; /** @@ -79,8 +94,8 @@ * \return the server * \throw server_error on errors */ -std::shared_ptr from_json(boost::asio::io_service& service, - const nlohmann::json& object); +auto from_json(boost::asio::io_service& service, + const nlohmann::json& object) -> std::shared_ptr; /** * Convert a INI section to a server. @@ -91,22 +106,9 @@ * \return the server * \throw server_error on errors */ -std::shared_ptr from_config(boost::asio::io_service& service, - const config& cfg, - const ini::section& sc); - -/** - * Parse IRC message and determine if it's a command or a simple message. - * - * If it's a command, the plugin invocation command is removed from the - * original message, otherwise it is copied verbatime. - * - * \param message the message line - * \param cchar the command char (e.g '!') - * \param plugin the plugin name - * \return the pair - */ -message_pack parse_message(std::string_view message, std::string_view cchar, std::string_view plugin); +auto from_config(boost::asio::io_service& service, + const config& cfg, + const ini::section& sc) -> std::shared_ptr; } // !server_util