Skip to content

Commit

Permalink
Merge pull request #2089 from sopel-irc/ignore-tagged-bots
Browse files Browse the repository at this point in the history
coretasks: activate `message-tags` and use it to ignore other bots' tagged messages
  • Loading branch information
dgw authored Sep 22, 2021
2 parents eb6dd0b + 7547fb4 commit 1d30887
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions sopel/coretasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,7 @@ def receive_cap_ls_reply(bot, trigger):
'cap-notify',
'server-time',
'userhost-in-names',
'message-tags',
]
for cap in core_caps:
if cap not in bot._cap_reqs:
Expand Down
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 :[email protected] 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 :[email protected] 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 = ':[email protected] PRIVMSG #sopel :Hi!'
pretrigger = trigger.PreTrigger(mockbot.nick, line)
Expand Down

0 comments on commit 1d30887

Please sign in to comment.