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: support setname CAP / SETNAME command #2578

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
23 changes: 23 additions & 0 deletions sopel/coretasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def _handle_sasl_capability(
CAP_ACCOUNT_TAG = plugin.capability(
'account-tag', handler=_handle_account_and_extjoin_capabilities)
CAP_SASL = plugin.capability('sasl', handler=_handle_sasl_capability)
CAP_SETNAME = plugin.capability('setname')


def setup(bot: Sopel):
Expand Down Expand Up @@ -845,6 +846,28 @@ def track_nicks(bot, trigger):
LOGGER.info("User named %r is now known as %r.", str(old), str(new))


@plugin.rule('(.*)')
@plugin.event('SETNAME')
@plugin.thread(False)
@plugin.unblockable
@plugin.priority('medium')
def handle_setname(bot, trigger):
"""Update a user's realname when notified by the IRC server."""
user = bot.users.get(trigger.nick)
if not user:
LOGGER.debug(
"Discarding SETNAME (%r) received for unknown user %s.",
trigger, trigger.nick,
)
return

LOGGER.info(
"User named %r changed realname to %r.",
str(user.realname), str(trigger),
)
user.realname = trigger


@plugin.rule('(.*)')
@plugin.event('PART')
@plugin.thread(False)
Expand Down
43 changes: 43 additions & 0 deletions test/test_coretasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,3 +675,46 @@ def test_handle_who_reply_botmode(mockbot):
':End of /WHO list.')

assert mockbot.users['Internets'].is_bot is True


def test_handle_setname(mockbot):
"""Make sure Sopel updates user's realname from SETNAME message"""
# add one user
mockbot.on_message(
':some.irc.network 352 Sopel #channel '
'internets services.irc.network * Internets Hr* '
':0 Network Services Bot')
mockbot.on_message(
':some.irc.network 315 Sopel #channel '
':End of /WHO list.')

assert 'Internets' in mockbot.users
assert mockbot.users['Internets'].realname == 'Network Services Bot'

# have the user change their realname
mockbot.on_message(
':[email protected] '
'SETNAME :Bot du service réseau'
)

assert mockbot.users['Internets'].realname == 'Bot du service réseau'

# change user's realname again to something not requiring a colon
# (this corresponds to an example specifically shown in the `setname` spec)
mockbot.on_message(
':[email protected] '
'SETNAME ServiceBot'
)

assert mockbot.users['Internets'].realname == 'ServiceBot'


def test_handle_setname_no_user(mockbot, caplog):
"""Make sure Sopel ignores SETNAME message for unknown user"""
caplog.set_level(logging.DEBUG)
mockbot.on_message(':[email protected] SETNAME :Bun Bazooka')

assert len(caplog.messages) == 1
assert caplog.messages[0] == (
"Discarding SETNAME ('Bun Bazooka') received for unknown user Akarin.")
assert caplog.record_tuples[0][1] == logging.DEBUG