From c9d6cf14cbe20455a54a85fb4803942bd2d002c1 Mon Sep 17 00:00:00 2001 From: Vamshi99 Date: Wed, 28 Mar 2018 12:56:36 +0530 Subject: [PATCH] filters.py: Filter required users If RESP_ONLY_REQ_USERS configuration is true, then stop responding to users other than those in the REQUIRED_USERS list in config. Closes https://github.com/coala/corobo/issues/515 --- config.py | 4 ++++ tests/filters_test.py | 14 ++++++++++++++ utils/filters.py | 5 ++++- 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 tests/filters_test.py diff --git a/config.py b/config.py index 657e7140..fc686c9e 100644 --- a/config.py +++ b/config.py @@ -87,6 +87,10 @@ IGNORE_USERNAMES = os.environ.get("IGNORE_USERNAMES", 'co-robo coala-bot').split() +RESP_ONLY_REQ_USERS = False + +REQUIRED_USERS = [] + DIVERT_TO_PRIVATE = ('help', ) ROOMS_TO_JOIN = ( diff --git a/tests/filters_test.py b/tests/filters_test.py new file mode 100644 index 00000000..6dfb215b --- /dev/null +++ b/tests/filters_test.py @@ -0,0 +1,14 @@ +from tests.isolated_testcase import IsolatedTestCase + + +class FiltersTest(IsolatedTestCase): + + def test_filter_users(self): + self.bot_config.RESP_ONLY_REQ_USERS = False + self.bot_config.REQUIRED_USERS = [] + self.assertCommand('!help', 'All commands') + self.bot_config.RESP_ONLY_REQ_USERS = False + self.bot_config.REQUIRED_USERS = ['testuser'] + self.bot_config.BOT_IDENTITY['nickname'] = 'testuser' + with self.assertRaises(queue.Empty): + self.assertCommand('!help', 'All commands') diff --git a/utils/filters.py b/utils/filters.py index 9b3c14a7..0eac8582 100644 --- a/utils/filters.py +++ b/utils/filters.py @@ -23,7 +23,10 @@ def filters(self, msg, cmd, args, dry_run): return msg, cmd, args @cmdfilter - def filter_ignored_users(self, msg, cmd, args, dry_run): + def filter_users(self, msg, cmd, args, dry_run): if msg.frm.nick in self.bot_config.IGNORE_USERNAMES: return None, None, None + if (msg.frm.nick not in self.bot_config.REQUIRED_USERS and + self.bot_config.RESP_ONLY_REQ_USER): + return None, None, None return msg, cmd, args