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

coretasks, test: support CHGHOST command/capability #2116

Merged
merged 2 commits into from
Jul 3, 2021
Merged
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
27 changes: 27 additions & 0 deletions sopel/coretasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,7 @@ def receive_cap_ls_reply(bot, trigger):
'echo-message',
'multi-prefix',
'away-notify',
'chghost',
'cap-notify',
'server-time',
]
Expand Down Expand Up @@ -1239,6 +1240,32 @@ def blocks(bot, trigger):
bot.reply(STRINGS['huh'])


@plugin.event('CHGHOST')
@plugin.thread(False)
@plugin.unblockable
@plugin.priority('medium')
def recv_chghost(bot, trigger):
"""Track user/host changes."""
if trigger.nick not in bot.users:
bot.users[trigger.nick] = target.User(
trigger.nick, trigger.user, trigger.host)

try:
new_user, new_host = trigger.args
except ValueError:
LOGGER.warning(
"Ignoring CHGHOST command with %s arguments: %r",
'extra' if len(trigger.args) > 2 else 'insufficient',
trigger.args)
return

bot.users[trigger.nick].user = new_user
bot.users[trigger.nick].host = new_host
LOGGER.info(
"Update user@host for nick %r: %s@%s",
trigger.nick, new_user, new_host)


@module.event('ACCOUNT')
@plugin.thread(False)
@plugin.unblockable
Expand Down
48 changes: 47 additions & 1 deletion test/test_coretasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def test_bot_mixed_mode_removal(mockbot, ircfactory):
irc.mode_set('#test', '-o+o-qa+v', [
'Uvoice', 'Uop', 'Uvoice', 'Uvoice', 'Uvoice'])
assert mockbot.channels["#test"].privileges[Identifier("Uop")] == OP, (
'OP got +o only')
'Uop got +o only')
assert mockbot.channels["#test"].privileges[Identifier("Uvoice")] == VOICE, (
'Uvoice got -o, -q, -a, then +v')

Expand Down Expand Up @@ -382,3 +382,49 @@ def test_sasl_plain_token_generation():
assert (
coretasks._make_sasl_plain_token('sopel', 'sasliscool') ==
'sopel\x00sopel\x00sasliscool')


def test_recv_chghost(mockbot, ircfactory):
"""Ensure that CHGHOST messages are correctly handled."""
irc = ircfactory(mockbot)
irc.channel_joined("#test", ["Alex", "Bob", "Cheryl"])

mockbot.on_message(":[email protected] CHGHOST alex identd.confirmed")

assert mockbot.users[Identifier('Alex')].user == 'alex'
assert mockbot.users[Identifier('Alex')].host == 'identd.confirmed'


def test_recv_chghost_invalid(mockbot, ircfactory, caplog):
"""Ensure that malformed CHGHOST messages are ignored and logged."""
irc = ircfactory(mockbot)
irc.channel_joined("#test", ["Alex", "Bob", "Cheryl"])
alex = Identifier('Alex')
bob = Identifier('Bob')
cheryl = Identifier('Cheryl')

# Mock bot + mock IRC server doesn't populate these on its own
assert mockbot.users[alex].user is None
assert mockbot.users[alex].host is None
assert mockbot.users[bob].user is None
assert mockbot.users[bob].host is None
assert mockbot.users[cheryl].user is None
assert mockbot.users[cheryl].host is None

mockbot.on_message(":[email protected] CHGHOST alex is a boss")
mockbot.on_message(":[email protected] CHGHOST rarely")
mockbot.on_message(":[email protected] CHGHOST")

# These should be unchanged
assert mockbot.users[alex].user is None
assert mockbot.users[alex].host is None
assert mockbot.users[bob].user is None
assert mockbot.users[bob].host is None
assert mockbot.users[cheryl].user is None
assert mockbot.users[cheryl].host is None

# Meanwhile, the malformed input should have generated log lines
assert len(caplog.messages) == 3
assert 'extra arguments' in caplog.messages[0]
assert 'insufficient arguments' in caplog.messages[1]
assert 'insufficient arguments' in caplog.messages[2]