# HG changeset patch # User David Demelier # Date 1522084921 -7200 # Node ID 8a9cac75608fe5b275b91d7d4060f6b674d89cde # Parent 23fd8bad40064122163f97146ac9a4c91e669f0c Irccd: move parse_message from string_util to server_util, closes #783 @5m While here, fix some style issue. diff -r 23fd8bad4006 -r 8a9cac75608f libcommon/irccd/string_util.cpp --- a/libcommon/irccd/string_util.cpp Sat Mar 24 07:55:14 2018 +0100 +++ b/libcommon/irccd/string_util.cpp Mon Mar 26 19:22:01 2018 +0200 @@ -335,45 +335,6 @@ return result; } -message_pack parse_message(std::string message, const std::string& cc, const std::string& name) -{ - auto result = message; - auto iscommand = false; - - // handle special commands "! command" - if (cc.length() > 0) { - auto pos = result.find_first_of(" \t"); - auto fullcommand = cc + name; - - /* - * If the message that comes is "!foo" without spaces we - * compare the command char + the plugin name. If there - * is a space, we check until we find a space, if not - * typing "!foo123123" will trigger foo plugin. - */ - if (pos == std::string::npos) - iscommand = result == fullcommand; - else - iscommand = result.length() >= fullcommand.length() && result.compare(0, pos, fullcommand) == 0; - - if (iscommand) { - /* - * If no space is found we just set the message to "" otherwise - * the plugin name will be passed through onCommand - */ - if (pos == std::string::npos) - result = ""; - else - result = message.substr(pos + 1); - } - } - - return { - iscommand ? message_pack::type::command : message_pack::type::message, - result - }; -} - bool is_boolean(std::string value) noexcept { std::transform(value.begin(), value.end(), value.begin(), [] (auto c) { diff -r 23fd8bad4006 -r 8a9cac75608f libcommon/irccd/string_util.hpp --- a/libcommon/irccd/string_util.hpp Sat Mar 24 07:55:14 2018 +0100 +++ b/libcommon/irccd/string_util.hpp Mon Mar 26 19:22:01 2018 +0200 @@ -45,30 +45,6 @@ namespace string_util { /** - * \brief Pack a message and its type - * - * On channels and queries, you may have a special command or a standard message - * depending on the beginning of the message. - * - * Example: `!reminder help' may invoke the command event if a plugin reminder - * exists. - */ -struct message_pack { - /** - * \brief Describe which type of message has been received - */ - enum class type { - command, //!< special command - message //!< standard message - } type; - - /** - * Message content. - */ - std::string message; -}; - -/** * \brief Disable or enable some features. */ enum class subst_flags : std::uint8_t { @@ -278,21 +254,6 @@ } /** - * 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 - */ -IRCCD_EXPORT message_pack parse_message(std::string message, - const std::string& cchar, - const std::string& plugin); - -/** * Server and identities must have strict names. This function can * be used to ensure that they are valid. * diff -r 23fd8bad4006 -r 8a9cac75608f libirccd/irccd/daemon/server_util.cpp --- a/libirccd/irccd/daemon/server_util.cpp Sat Mar 24 07:55:14 2018 +0100 +++ b/libirccd/irccd/daemon/server_util.cpp Mon Mar 26 19:22:01 2018 +0200 @@ -234,6 +234,45 @@ return sv; } +message_pack parse_message(std::string message, const std::string& cc, const std::string& name) +{ + auto result = message; + auto iscommand = false; + + // handle special commands "! command" + if (cc.length() > 0) { + auto pos = result.find_first_of(" \t"); + auto fullcommand = cc + name; + + /* + * If the message that comes is "!foo" without spaces we + * compare the command char + the plugin name. If there + * is a space, we check until we find a space, if not + * typing "!foo123123" will trigger foo plugin. + */ + if (pos == std::string::npos) + iscommand = result == fullcommand; + else + iscommand = result.length() >= fullcommand.length() && result.compare(0, pos, fullcommand) == 0; + + if (iscommand) { + /* + * If no space is found we just set the message to "" otherwise + * the plugin name will be passed through onCommand + */ + if (pos == std::string::npos) + result = ""; + else + result = message.substr(pos + 1); + } + } + + return { + iscommand ? message_pack::type::command : message_pack::type::message, + result + }; +} + } // !server_util } // !irccd diff -r 23fd8bad4006 -r 8a9cac75608f libirccd/irccd/daemon/server_util.hpp --- a/libirccd/irccd/daemon/server_util.hpp Sat Mar 24 07:55:14 2018 +0100 +++ b/libirccd/irccd/daemon/server_util.hpp Mon Mar 26 19:22:01 2018 +0200 @@ -48,6 +48,30 @@ namespace server_util { /** + * \brief Pack a message and its type + * + * On channels and queries, you may have a special command or a standard message + * depending on the beginning of the message. + * + * Example: `!reminder help' may invoke the command event if a plugin reminder + * exists. + */ +struct message_pack { + /** + * \brief Describe which type of message has been received + */ + enum class type { + command, //!< special command + message //!< standard message + } type; + + /** + * Message content. + */ + std::string message; +}; + +/** * Convert a JSON object to a server. * * \param service the io service @@ -71,6 +95,21 @@ 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 message, + const std::string& cchar, + const std::string& plugin); + } // !server_util } // !irccd diff -r 23fd8bad4006 -r 8a9cac75608f libirccd/irccd/daemon/service/server_service.cpp --- a/libirccd/irccd/daemon/service/server_service.cpp Sat Mar 24 07:55:14 2018 +0100 +++ b/libirccd/irccd/daemon/service/server_service.cpp Mon Mar 26 19:22:01 2018 +0200 @@ -65,9 +65,8 @@ void server_service::handle_connect(const connect_event& ev) { irccd_.log().debug() << "server " << ev.server->get_name() << ": event onConnect" << std::endl; - irccd_.transports().broadcast(nlohmann::json::object({ - { "event", "onConnect" }, + { "event", "onConnect" }, { "server", ev.server->get_name() } })); @@ -88,7 +87,7 @@ irccd_.log().debug() << "server " << ev.server->get_name() << ": event onDisconnect" << std::endl; irccd_.transports().broadcast(nlohmann::json::object({ - { "event", "onDisconnect" }, + { "event", "onDisconnect" }, { "server", ev.server->get_name() } })); @@ -110,10 +109,10 @@ irccd_.log().debug() << " target: " << ev.nickname << std::endl; irccd_.transports().broadcast(nlohmann::json::object({ - { "event", "onInvite" }, + { "event", "onInvite" }, { "server", ev.server->get_name() }, - { "origin", ev.origin }, - { "channel", ev.channel } + { "origin", ev.origin }, + { "channel", ev.channel } })); dispatch(irccd_, ev.server->get_name(), ev.origin, ev.channel, @@ -133,10 +132,10 @@ irccd_.log().debug() << " channel: " << ev.channel << std::endl; irccd_.transports().broadcast(nlohmann::json::object({ - { "event", "onJoin" }, + { "event", "onJoin" }, { "server", ev.server->get_name() }, - { "origin", ev.origin }, - { "channel", ev.channel } + { "origin", ev.origin }, + { "channel", ev.channel } })); dispatch(irccd_, ev.server->get_name(), ev.origin, ev.channel, @@ -158,12 +157,12 @@ irccd_.log().debug() << " reason: " << ev.reason << std::endl; irccd_.transports().broadcast(nlohmann::json::object({ - { "event", "onKick" }, + { "event", "onKick" }, { "server", ev.server->get_name() }, - { "origin", ev.origin }, - { "channel", ev.channel }, - { "target", ev.target }, - { "reason", ev.reason } + { "origin", ev.origin }, + { "channel", ev.channel }, + { "target", ev.target }, + { "reason", ev.reason } })); dispatch(irccd_, ev.server->get_name(), ev.origin, ev.channel, @@ -184,28 +183,32 @@ irccd_.log().debug() << " message: " << ev.message << std::endl; irccd_.transports().broadcast(nlohmann::json::object({ - { "event", "onMessage" }, + { "event", "onMessage" }, { "server", ev.server->get_name() }, - { "origin", ev.origin }, - { "channel", ev.channel }, - { "message", ev.message } + { "origin", ev.origin }, + { "channel", ev.channel }, + { "message", ev.message } })); dispatch(irccd_, ev.server->get_name(), ev.origin, ev.channel, [=] (plugin& plugin) -> std::string { - return string_util::parse_message( + return server_util::parse_message( ev.message, ev.server->get_command_char(), plugin.get_name() - ).type == string_util::message_pack::type::command ? "onCommand" : "onMessage"; + ).type == server_util::message_pack::type::command ? "onCommand" : "onMessage"; }, [=] (plugin& plugin) mutable { auto copy = ev; - auto pack = string_util::parse_message(copy.message, copy.server->get_command_char(), plugin.get_name()); + auto pack = server_util::parse_message( + copy.message, + copy.server->get_command_char(), + plugin.get_name() + ); copy.message = pack.message; - if (pack.type == string_util::message_pack::type::command) + if (pack.type == server_util::message_pack::type::command) plugin.handle_command(irccd_, copy); else plugin.handle_message(irccd_, copy); @@ -221,11 +224,11 @@ irccd_.log().debug() << " message: " << ev.message << std::endl; irccd_.transports().broadcast(nlohmann::json::object({ - { "event", "onMe" }, + { "event", "onMe" }, { "server", ev.server->get_name() }, - { "origin", ev.origin }, - { "target", ev.channel }, - { "message", ev.message } + { "origin", ev.origin }, + { "target", ev.channel }, + { "message", ev.message } })); dispatch(irccd_, ev.server->get_name(), ev.origin, ev.channel, @@ -249,14 +252,14 @@ irccd_.log().debug() << " mask: " << ev.mask << std::endl; irccd_.transports().broadcast(nlohmann::json::object({ - { "event", "onMode" }, + { "event", "onMode" }, { "server", ev.server->get_name() }, - { "origin", ev.origin }, - { "channel", ev.channel }, - { "mode", ev.mode }, - { "limit", ev.limit }, - { "user", ev.user }, - { "mask", ev.mask } + { "origin", ev.origin }, + { "channel", ev.channel }, + { "mode", ev.mode }, + { "limit", ev.limit }, + { "user", ev.user }, + { "mask", ev.mask } })); dispatch(irccd_, ev.server->get_name(), ev.origin, /* channel */ "", @@ -281,10 +284,10 @@ names.push_back(v); irccd_.transports().broadcast(nlohmann::json::object({ - { "event", "onNames" }, + { "event", "onNames" }, { "server", ev.server->get_name() }, - { "channel", ev.channel }, - { "names", std::move(names) } + { "channel", ev.channel }, + { "names", std::move(names) } })); dispatch(irccd_, ev.server->get_name(), /* origin */ "", ev.channel, @@ -304,10 +307,10 @@ irccd_.log().debug() << " nickname: " << ev.nickname << std::endl; irccd_.transports().broadcast(nlohmann::json::object({ - { "event", "onNick" }, + { "event", "onNick" }, { "server", ev.server->get_name() }, - { "origin", ev.origin }, - { "nickname", ev.nickname } + { "origin", ev.origin }, + { "nickname", ev.nickname } })); dispatch(irccd_, ev.server->get_name(), ev.origin, /* channel */ "", @@ -328,11 +331,11 @@ irccd_.log().debug() << " message: " << ev.message << std::endl; irccd_.transports().broadcast(nlohmann::json::object({ - { "event", "onNotice" }, + { "event", "onNotice" }, { "server", ev.server->get_name() }, - { "origin", ev.origin }, - { "channel", ev.channel }, - { "message", ev.message } + { "origin", ev.origin }, + { "channel", ev.channel }, + { "message", ev.message } })); dispatch(irccd_, ev.server->get_name(), ev.origin, /* channel */ "", @@ -353,11 +356,11 @@ irccd_.log().debug() << " reason: " << ev.reason << std::endl; irccd_.transports().broadcast(nlohmann::json::object({ - { "event", "onPart" }, + { "event", "onPart" }, { "server", ev.server->get_name() }, - { "origin", ev.origin }, - { "channel", ev.channel }, - { "reason", ev.reason } + { "origin", ev.origin }, + { "channel", ev.channel }, + { "reason", ev.reason } })); dispatch(irccd_, ev.server->get_name(), ev.origin, ev.channel, @@ -378,11 +381,11 @@ irccd_.log().debug() << " topic: " << ev.topic << std::endl; irccd_.transports().broadcast(nlohmann::json::object({ - { "event", "onTopic" }, + { "event", "onTopic" }, { "server", ev.server->get_name() }, - { "origin", ev.origin }, - { "channel", ev.channel }, - { "topic", ev.topic } + { "origin", ev.origin }, + { "channel", ev.channel }, + { "topic", ev.topic } })); dispatch(irccd_, ev.server->get_name(), ev.origin, ev.channel, @@ -405,12 +408,12 @@ irccd_.log().debug() << " channels: " << string_util::join(ev.whois.channels, ", ") << std::endl; irccd_.transports().broadcast(nlohmann::json::object({ - { "event", "onWhois" }, + { "event", "onWhois" }, { "server", ev.server->get_name() }, - { "nickname", ev.whois.nick }, - { "username", ev.whois.user }, - { "host", ev.whois.host }, - { "realname", ev.whois.realname } + { "nickname", ev.whois.nick }, + { "username", ev.whois.user }, + { "host", ev.whois.host }, + { "realname", ev.whois.realname } })); dispatch(irccd_, ev.server->get_name(), /* origin */ "", /* channel */ "",