3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-12-28 13:42:37 +01:00

coremods/handlers: implement cleanup code for visible-state-only servers

Closes #536.
Closes #517.
This commit is contained in:
James Lu 2017-10-07 21:49:17 -07:00
parent de5ab051aa
commit eca40a3d7c
2 changed files with 28 additions and 11 deletions

View File

@ -163,3 +163,29 @@ def handle_time(irc, source, command, args):
timestring = time.ctime()
irc.numeric(irc.sid, 391, source, '%s :%s' % (irc.hostname(), timestring))
utils.add_hook(handle_time, 'TIME')
def _state_cleanup_core(irc, source, channel):
"""
Handles PART and KICK on clientbot-like networks (where only the users and channels we see are available)
by deleting channels when we leave and users when they leave all shared channels.
"""
if irc.has_cap('visible-state-only'):
# Delete channels that we were removed from.
if irc.pseudoclient and source == irc.pseudoclient.uid:
log.debug('(%s) state_cleanup: removing channel %s since we have left', irc.name, channel)
del irc._channels[channel]
# Delete users no longer sharing a channel with us.
if not irc.users[source].channels:
log.debug('(%s) state_cleanup: removing user %s/%s who no longer shares a channel with us',
irc.name, source, irc.users[source].nick)
irc._remove_client(source)
def stats_cleanup_part(irc, source, command, args):
for channel in args['channels']:
_state_cleanup_core(irc, source, channel)
utils.add_hook(stats_cleanup_part, 'PART', priority=-100)
def stats_cleanup_kick(irc, source, command, args):
_state_cleanup_core(irc, args['target'], args['channel'])
utils.add_hook(stats_cleanup_kick, 'KICK', priority=-100)

View File

@ -916,11 +916,7 @@ class ClientbotWrapperProtocol(IRCCommonProtocol):
if (not self.is_internal_client(source)) and not self.is_internal_server(source):
# Don't repeat hooks if we're the kicker.
self.call_hooks([source, 'KICK', {'channel': channel, 'target': target, 'text': reason}])
# Delete channels that we were kicked from, for better state keeping.
if self.pseudoclient and target == self.pseudoclient.uid:
del self._channels[channel]
return {'channel': channel, 'target': target, 'text': reason}
def handle_mode(self, source, command, args):
"""Handles MODE changes."""
@ -1019,12 +1015,7 @@ class ClientbotWrapperProtocol(IRCCommonProtocol):
self._channels[channel].remove_user(source)
self.users[source].channels -= set(channels)
self.call_hooks([source, 'PART', {'channels': channels, 'text': reason}])
# Clear channels that are empty, or that we're parting.
for channel in channels:
if (self.pseudoclient and source == self.pseudoclient.uid) or not self._channels[channel].users:
del self._channels[channel]
return {'channels': channels, 'text': reason}
def handle_ping(self, source, command, args):
"""