Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

irc/bot: implement echo-message support #1072

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions sopel/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,9 @@ def dispatch(self, pretrigger):
if (hasattr(func, 'intents') and
trigger.tags.get('intent') not in func.intents):
continue
if (not (hasattr(func, 'echo') and func.echo is True) and
trigger.nick.lower() == self.nick.lower()):
continue
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd put this check first in the enclosing for loop, but the order isn't really an issue tbh.

if func.thread:
targs = (func, wrapper, trigger)
t = threading.Thread(target=self.call, args=targs)
Expand Down
10 changes: 10 additions & 0 deletions sopel/irc.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ def write(self, args, text=None):
self.send(temp.encode('utf-8'))
finally:
self.writing_lock.release()
# Simulate echo-message
if 'echo-message' not in self.enabled_capabilities:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @maxpowa mentioned in the main PR comments, "As it is right now, it will echo every type of message, but the echo message spec only includes NOTICE and PRIVMSG." Therefore, this block should only fire if the message is one of those types.

# Since we have no way of knowing the hostmask the IRC server uses
# for us, we'll just use something reasonable
host = 'localhost'
if self.config.core.bind_host:
host = self.config.core.bind_host
pretrigger = PreTrigger(self.nick, ':{0}!{1}@{2} {3}'
.format(self.nick, self.user, host, temp))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line made me notice the overuse of a variable named temp in this function. Renaming that to something sensible should happen after this is merged.

self.dispatch(pretrigger)

def run(self, host, port=6667):
try:
Expand Down
17 changes: 17 additions & 0 deletions sopel/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,23 @@ def add_attribute(function):
return add_attribute


def echo(value):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @embolalia:

I don't think the @echo decorator should take a value. The default should be definitely not to get echoes, meaning the decorator only needs to mean "yup this thing can get echo messages".

This decorator should just set the echo function attribute to True if called. That's all it needs to do.

"""Decorate a function to specify if it should receive echo messages.

This decorator can be used to listen in on the messages that Sopel is
sending and react accordingly.

Args:
value: Either True or False. If True the function will receive echo
messages, and if False only messages received from other users.

"""
def add_attribute(function):
function.echo = value
return function
return add_attribute


def commands(*command_list):
"""Decorate a function to set one or more commands to trigger it.

Expand Down
7 changes: 7 additions & 0 deletions test/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ def mock(bot, trigger, match):
assert mock.thread is True


def test_echo():
@module.echo(False)
def mock(bot, trigger, match):
return True
assert mock.echo is False


def test_commands():
@module.commands('sopel')
def mock(bot, trigger, match):
Expand Down