changeset 793:292482f36454

irccd: make full support for origin in rule, #947 @1h
author David Demelier <markand@malikania.fr>
date Sun, 11 Nov 2018 10:39:10 +0100
parents 498661c1c8f2
children 6e108be0835d
files CHANGES.md libirccd/irccd/daemon/rule.cpp libirccd/irccd/daemon/rule.hpp libirccd/irccd/daemon/rule_service.cpp libirccd/irccd/daemon/rule_util.cpp tests/src/libirccd/rules/main.cpp
diffstat 6 files changed, 69 insertions(+), 67 deletions(-) [+]
line wrap: on
line diff
--- a/CHANGES.md	Fri Nov 09 20:58:00 2018 +0100
+++ b/CHANGES.md	Sun Nov 11 10:39:10 2018 +0100
@@ -4,44 +4,43 @@
 irccd current
 ----------------------
 
-Irccd:
+irccd:
 
-- The default server identity now uses the system account name if possible by
-  default (#607),
 - New sections `[paths]` and `[paths.plugin]` have been added to control
   standard paths for both irccd and plugins (#611),
 - If Mercurial is found, the version is bundled in `irccd --version`,
-- Irccd no longer supports uid, gid, pid and daemon features (#846).
+- Irccd no longer supports uid, gid, pid and daemon features (#846),
 - Sections `[identity]` and `[server]` have been merged (#905),
-- Local transports support SSL too (#939).
+- Local transports support SSL (#939),
+- The origin in rule is now first class value (#947).
 
-Irccd test:
+irccd-test:
 
 - A brand new `irccd-test` program has been added to tests plugins on the
   command line (#569).
 
-CMake:
+cmake:
 
 - CMake no longer create a fake installation directory while building (#674),
 - All targets are placed into the `bin` directory while building (#715).
 
-Network API:
+network API:
 
 - Network commands return an error code instead of a string (#739).
 
-Javascript API:
+javascript API:
 
 - The Irccd.Timer API now runs on top of Boost.Asio and no longer have custom
   buggy code (#595),
 - New Irccd.Server.isSelf function (#735).
 
-Internal:
+internal:
 
 - The code is now based on Boost for many internal parts of the core, (#593),
   (#594), (#595), (#681), (#697),
 - The libircclient has been replaced by a simple homemade library (#581).
 
-Plugins:
+plugins:
 
 - Introduce brand new joke plugin (#609),
 - Introduce brand new tictactoe plugin (#393),
@@ -81,41 +80,41 @@
 irccd 2.1.0 2017-02-01
 ----------------------
 
-Irccd:
+irccd:
 
 - Add SSL support in transports,
 - Add authentication support in transports,
 - Fix a warning about daemon on macOS.
 
-Javascript API:
+javascript API:
 
 - New Irccd.File.lines function,
 - Various improvements in Irccd.File API.
 
-Plugins:
+plugins:
 
 - Add new format section for plugins,
 - Add unit tests for plugins.
 
-Irccdctl:
+irccdctl:
 
 - Added brand new plugin-config command,
 - Added aliases,
 - Added unit tests for irccdctl commands.
 
-Libraries:
+libraries:
 
 - Replaced jansson with Niels Lohmann's JSON library,
 - Updated Duktape to 1.5.1.
 
-Misc:
+misc:
 
 - Patterns can now use shell escape sequences,
 - Added .editorconfig file,
 - Split documentation into topics,
 - The code is now split into several individual libraries.
 
-Windows:
+windows:
 
 - Get rid of QtIFW and uses NSIS, WIX on Windows,
 - Installer have components,
--- a/libirccd/irccd/daemon/rule.cpp	Fri Nov 09 20:58:00 2018 +0100
+++ b/libirccd/irccd/daemon/rule.cpp	Sun Nov 11 10:39:10 2018 +0100
@@ -25,7 +25,7 @@
 
 auto rule::match_set(const set& set, const std::string& value) const noexcept -> bool
 {
-	return value.empty() || set.empty() || set.count(value) == 1;
+	return set.empty() || set.count(value) == 1;
 }
 
 auto rule::match(std::string_view server,
--- a/libirccd/irccd/daemon/rule.hpp	Fri Nov 09 20:58:00 2018 +0100
+++ b/libirccd/irccd/daemon/rule.hpp	Sun Nov 11 10:39:10 2018 +0100
@@ -76,14 +76,14 @@
 	 *
 	 * \param server the server
 	 * \param channel the channel
-	 * \param nick the origin
+	 * \param origin the origin
 	 * \param plugin the plugin
 	 * \param event the event
 	 * \return true if match
 	 */
 	auto match(std::string_view server,
 	           std::string_view channel,
-	           std::string_view nick,
+	           std::string_view origin,
 	           std::string_view plugin,
 	           std::string_view event) const noexcept -> bool;
 };
--- a/libirccd/irccd/daemon/rule_service.cpp	Fri Nov 09 20:58:00 2018 +0100
+++ b/libirccd/irccd/daemon/rule_service.cpp	Sun Nov 11 10:39:10 2018 +0100
@@ -81,28 +81,9 @@
 {
 	bool result = true;
 
-	irccd_.get_log().debug("rule", "")
-		<< "solving for server=" << server
-		<< ", channel=" << channel
-		<< ", origin=" << origin
-		<< ", plugin=" << plugin
-		<< ", event=" << event << std::endl;
-
-	int i = 0;
-	for (const auto& rule : rules_) {
-		auto action = rule.action == rule::action_type::accept ? "accept" : "drop";
-
-		irccd_.get_log().debug(rule) << "candidate "  << i++ << ":" << std::endl;
-		irccd_.get_log().debug(rule) << "  servers: "  << string_util::join(rule.servers) << std::endl;
-		irccd_.get_log().debug(rule) << "  channels: " << string_util::join(rule.channels) << std::endl;
-		irccd_.get_log().debug(rule) << "  origins: "  << string_util::join(rule.origins) << std::endl;
-		irccd_.get_log().debug(rule) << "  plugins: "  << string_util::join(rule.plugins) << std::endl;
-		irccd_.get_log().debug(rule) << "  events: "   << string_util::join(rule.events) << std::endl;
-		irccd_.get_log().debug(rule) << "  action: "   << action << std::endl;
-
+	for (const auto& rule : rules_)
 		if (rule.match(server, channel, origin, plugin, event))
 			result = rule.action == rule::action_type::accept;
-	}
 
 	return result;
 }
--- a/libirccd/irccd/daemon/rule_util.cpp	Fri Nov 09 20:58:00 2018 +0100
+++ b/libirccd/irccd/daemon/rule_util.cpp	Sun Nov 11 10:39:10 2018 +0100
@@ -138,6 +138,7 @@
 	return {
 		{ "servers",    join(rule.servers)      },
 		{ "channels",   join(rule.channels)     },
+		{ "origins",    join(rule.origins)      },
 		{ "plugins",    join(rule.plugins)      },
 		{ "events",     join(rule.events)       },
 		{ "action",     str(rule.action)        }
--- a/tests/src/libirccd/rules/main.cpp	Fri Nov 09 20:58:00 2018 +0100
+++ b/tests/src/libirccd/rules/main.cpp	Sun Nov 11 10:39:10 2018 +0100
@@ -136,8 +136,8 @@
 	/*
 	 * [rule]
 	 */
-	BOOST_REQUIRE(m.match("freenode", "#test", "a", "", ""));
-	BOOST_REQUIRE(m.match("", "", "", "", ""));
+	BOOST_TEST(m.match("freenode", "#test", "a", "", ""));
+	BOOST_TEST(m.match("", "", "", "", ""));
 }
 
 BOOST_AUTO_TEST_CASE(basic_match2)
@@ -149,9 +149,9 @@
 	 * servers	= "freenode"
 	 */
 
-	BOOST_REQUIRE(m.match("freenode", "#test", "a", "", ""));
-	BOOST_REQUIRE(!m.match("malikania", "#test", "a", "", ""));
-	BOOST_REQUIRE(m.match("freenode", "", "jean", "", "onMessage"));
+	BOOST_TEST(m.match("freenode", "#test", "a", "", ""));
+	BOOST_TEST(!m.match("malikania", "#test", "a", "", ""));
+	BOOST_TEST(m.match("freenode", "", "jean", "", "onMessage"));
 }
 
 BOOST_AUTO_TEST_CASE(basic_match3)
@@ -164,9 +164,9 @@
 	 * channels	= "#staff"
 	 */
 
-	BOOST_REQUIRE(m.match("freenode", "#staff", "a", "", ""));
-	BOOST_REQUIRE(!m.match("freenode", "#test", "a", "", ""));
-	BOOST_REQUIRE(!m.match("malikania", "#staff", "a", "", ""));
+	BOOST_TEST(m.match("freenode", "#staff", "a", "", ""));
+	BOOST_TEST(!m.match("freenode", "#test", "a", "", ""));
+	BOOST_TEST(!m.match("malikania", "#staff", "a", "", ""));
 }
 
 BOOST_AUTO_TEST_CASE(basic_match4)
@@ -180,9 +180,9 @@
 	 * plugins	= "a"
 	 */
 
-	BOOST_REQUIRE(m.match("malikania", "#staff", "a", "",""));
-	BOOST_REQUIRE(!m.match("malikania", "#staff", "b", "", ""));
-	BOOST_REQUIRE(!m.match("freenode", "#staff", "a", "", ""));
+	BOOST_TEST(m.match("malikania", "#staff", "a", "",""));
+	BOOST_TEST(!m.match("malikania", "#staff", "b", "", ""));
+	BOOST_TEST(!m.match("freenode", "#staff", "a", "", ""));
 }
 
 BOOST_AUTO_TEST_CASE(complex_match1)
@@ -194,53 +194,74 @@
 	 * servers	= "malikania freenode"
 	 */
 
-	BOOST_REQUIRE(m.match("malikania", "", "", "", ""));
-	BOOST_REQUIRE(m.match("freenode", "", "", "", ""));
-	BOOST_REQUIRE(!m.match("no", "", "", "", ""));
+	BOOST_TEST(m.match("malikania", "", "", "", ""));
+	BOOST_TEST(m.match("freenode", "", "", "", ""));
+	BOOST_TEST(!m.match("no", "", "", "", ""));
+}
+
+BOOST_AUTO_TEST_CASE(origin_match)
+{
+	rule m{
+		rule::set{"malikania"},
+		rule::set{},
+		rule::set{"markand"},
+		rule::set{},
+		rule::set{},
+		rule::action_type::accept
+	};
+
+	/*
+	 * [rule]
+	 * servers = "malikania"
+	 * origins = "markand"
+	 */
+	BOOST_TEST(m.match("malikania", "#staff", "markand", "system", "onCommand"));
+	BOOST_TEST(!m.match("malikania", "#staff", "", "system", "onNames"));
+	BOOST_TEST(!m.match("malikania", "#staff", "jean", "system", "onMessage"));
 }
 
 BOOST_AUTO_TEST_CASE(basic_solve)
 {
 	/* Allowed */
-	BOOST_REQUIRE(rules_.solve("malikania", "#staff", "", "a", "onMessage"));
+	BOOST_TEST(rules_.solve("malikania", "#staff", "", "a", "onMessage"));
 
 	/* Allowed */
-	BOOST_REQUIRE(rules_.solve("freenode", "#staff", "", "b", "onTopic"));
+	BOOST_TEST(rules_.solve("freenode", "#staff", "", "b", "onTopic"));
 
 	/* Not allowed */
-	BOOST_REQUIRE(!rules_.solve("malikania", "#staff", "", "", "onCommand"));
+	BOOST_TEST(!rules_.solve("malikania", "#staff", "", "", "onCommand"));
 
 	/* Not allowed */
-	BOOST_REQUIRE(!rules_.solve("freenode", "#staff", "", "c", "onCommand"));
+	BOOST_TEST(!rules_.solve("freenode", "#staff", "", "c", "onCommand"));
 
 	/* Allowed */
-	BOOST_REQUIRE(rules_.solve("unsafe", "#staff", "", "c", "onCommand"));
+	BOOST_TEST(rules_.solve("unsafe", "#staff", "", "c", "onCommand"));
 }
 
 BOOST_AUTO_TEST_CASE(games_solve)
 {
 	/* Allowed */
-	BOOST_REQUIRE(rules_.solve("malikania", "#games", "", "game", "onMessage"));
+	BOOST_TEST(rules_.solve("malikania", "#games", "", "game", "onMessage"));
 
 	/* Allowed */
-	BOOST_REQUIRE(rules_.solve("localhost", "#games", "", "game", "onMessage"));
+	BOOST_TEST(rules_.solve("localhost", "#games", "", "game", "onMessage"));
 
 	/* Allowed */
-	BOOST_REQUIRE(rules_.solve("malikania", "#games", "", "game", "onCommand"));
+	BOOST_TEST(rules_.solve("malikania", "#games", "", "game", "onCommand"));
 
 	/* Not allowed */
-	BOOST_REQUIRE(!rules_.solve("malikania", "#games", "", "game", "onQuery"));
+	BOOST_TEST(!rules_.solve("malikania", "#games", "", "game", "onQuery"));
 
 	/* Not allowed */
-	BOOST_REQUIRE(!rules_.solve("freenode", "#no", "", "game", "onMessage"));
+	BOOST_TEST(!rules_.solve("freenode", "#no", "", "game", "onMessage"));
 
 	/* Not allowed */
-	BOOST_REQUIRE(!rules_.solve("malikania", "#test", "", "game", "onMessage"));
+	BOOST_TEST(!rules_.solve("malikania", "#test", "", "game", "onMessage"));
 }
 
 BOOST_AUTO_TEST_CASE(fix_645)
 {
-	BOOST_REQUIRE(!rules_.solve("MALIKANIA", "#STAFF", "", "SYSTEM", "onCommand"));
+	BOOST_TEST(!rules_.solve("MALIKANIA", "#STAFF", "", "SYSTEM", "onCommand"));
 }
 
 BOOST_AUTO_TEST_SUITE_END()