Skip to content

Commit

Permalink
Merge pull request #2116 from sopel-irc/chghost-support
Browse files Browse the repository at this point in the history
coretasks, test: support CHGHOST command/capability
  • Loading branch information
dgw authored Jul 3, 2021
2 parents fc85888 + 4edb9ef commit 707a157
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
27 changes: 27 additions & 0 deletions sopel/coretasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,7 @@ def receive_cap_ls_reply(bot, trigger):
'echo-message',
'multi-prefix',
'away-notify',
'chghost',
'cap-notify',
'server-time',
'userhost-in-names',
Expand Down Expand Up @@ -1287,6 +1288,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 @@ -72,7 +72,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 @@ -452,3 +452,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(":Alex!~alex@test.local 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(":Alex!~alex@test.local CHGHOST alex is a boss")
mockbot.on_message(":Bob!bob@grills.burgers CHGHOST rarely")
mockbot.on_message(":Cheryl!~carol@danger.zone 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]

0 comments on commit 707a157

Please sign in to comment.