Skip to content

Commit

Permalink
plugins.rules: ignore messages tagged as coming from a bot
Browse files Browse the repository at this point in the history
Only PRIVMSG and NOTICE messages from bots are filtered out, so this
change doesn't affect Sopel's user tracking. JOINs, PARTs, etc. can be
included later, once there exists a decorator that event handlers can
use to turn this `bot` filter off.

Currently supports the `draft/bot` tag AND the `bot` tag that will be
used when the relevant IRCv3 extension leaves draft status.

It seems like the future-proofing is of minimal risk here.
  • Loading branch information
dgw committed Sep 18, 2021
1 parent 71d64fc commit 7547fb4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
8 changes: 8 additions & 0 deletions sopel/plugins/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,13 @@ def match_preconditions(self, bot, pretrigger):
event = pretrigger.event
intent = pretrigger.tags.get('intent')
nick = pretrigger.nick
is_bot_message = (
(
'draft/bot' in pretrigger.tags or # can be removed...someday
'bot' in pretrigger.tags
) and
event in ["PRIVMSG", "NOTICE"]
)
is_echo_message = (
nick.lower() == bot.nick.lower() and
event in ["PRIVMSG", "NOTICE"]
Expand All @@ -967,6 +974,7 @@ def match_preconditions(self, bot, pretrigger):
return (
self.match_event(event) and
self.match_intent(intent) and
(not is_bot_message or (is_echo_message and self.allow_echo())) and
(not is_echo_message or self.allow_echo())
)

Expand Down
17 changes: 17 additions & 0 deletions test/plugins/test_plugins_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,23 @@ def test_rule_match_privmsg_action(mockbot):
assert not list(rule.match(mockbot, pretrigger))


def test_rule_match_privmsg_bot_tag(mockbot):
regex = re.compile(r'.*')
rule = rules.Rule([regex])

line = '@draft/bot :TestBot!sopel@example.com PRIVMSG #sopel :Hi!'
pretrigger = trigger.PreTrigger(mockbot.nick, line)
assert not list(rule.match(mockbot, pretrigger)), (
'Line with `draft/bot` tag must be ignored'
)

line = '@bot :TestBot!sopel@example.com PRIVMSG #sopel :Hi!'
pretrigger = trigger.PreTrigger(mockbot.nick, line)
assert not list(rule.match(mockbot, pretrigger)), (
'Line with final/ratified `bot` tag must be ignored'
)


def test_rule_match_privmsg_echo(mockbot):
line = ':TestBot!sopel@example.com PRIVMSG #sopel :Hi!'
pretrigger = trigger.PreTrigger(mockbot.nick, line)
Expand Down

0 comments on commit 7547fb4

Please sign in to comment.