3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-11-27 13:09:23 +01:00

ServiceBot: modularize join() for explicit channel joining (#326)

This commit is contained in:
James Lu 2016-09-23 23:39:12 -07:00
parent 0cce6ca488
commit a040c3c7d2
3 changed files with 40 additions and 19 deletions

View File

@ -26,10 +26,6 @@ def spawn_service(irc, source, command, args):
nick = irc.serverdata.get("%s_nick" % name) or conf.conf.get(name, {}).get('nick') or sbot.nick or name
ident = irc.serverdata.get("%s_ident" % name) or conf.conf.get(name, {}).get('ident') or sbot.ident or name
# Specify modes to join the services bot with.
joinmodes = irc.serverdata.get("%s_joinmodes" % name) or conf.conf.get(name, {}).get('joinmodes') or ''
joinmodes = ''.join([m for m in joinmodes if m in irc.prefixmodes])
if name != 'pylink' and irc.protoname == 'clientbot':
# Prefix service bots spawned on Clientbot to prevent possible nick collisions.
nick = 'PyLinkService@' + nick
@ -66,18 +62,7 @@ def spawn_service(irc, source, command, args):
# TODO: channels should be tracked in a central database, not hardcoded
# in conf.
channels = set(irc.serverdata.get('channels', [])) | sbot.extra_channels.get(irc.name, set())
for chan in channels:
if utils.isChannel(chan):
log.debug('(%s) Joining services %s to channel %s with modes %r', irc.name, name, chan, joinmodes)
if joinmodes: # Modes on join were specified; use SJOIN to burst our service
irc.proto.sjoin(irc.sid, chan, [(joinmodes, u)])
else:
irc.proto.join(u, chan)
irc.callHooks([irc.sid, 'PYLINK_SERVICE_JOIN', {'channel': chan, 'users': [u]}])
else:
log.warning('(%s) Ignoring invalid autojoin channel %r.', irc.name, chan)
sbot.join(irc, channels)
utils.add_hook(spawn_service, 'PYLINK_NEW_SERVICE')

View File

@ -511,9 +511,7 @@ def initializeChannel(irc, channel):
log.debug('(%s) relay.initializeChannel: joining our (%s) users: %s', irc.name, remotenet, irc.channels[channel].users)
relayJoins(irc, channel, irc.channels[channel].users, irc.channels[channel].ts)
world.services['pylink'].extra_channels[irc.name].add(channel)
if irc.pseudoclient and irc.pseudoclient.uid not in irc.channels[channel].users:
irc.proto.join(irc.pseudoclient.uid, channel)
world.services['pylink'].join(irc, channel)
def removeChannel(irc, channel):
"""Destroys a relay channel by parting all of its users."""

View File

@ -235,6 +235,44 @@ class ServiceBot():
else:
raise NotImplementedError("Network specific plugins not supported yet.")
def join(self, irc, channels, autojoin=True):
"""
Joins the given service bot to the given channel(s).
"""
try:
u = self.uids[irc.name]
except KeyError:
log.debug('(%s/%s) Skipping join(), UID not initialized yet', irc.name, self.name)
return
# Ensure type safety: pluralize strings if only one channel was given, then convert to set.
if type(channels) == str:
channels = [channels]
channels = set(channels)
# Specify modes to join the services bot with.
joinmodes = irc.serverdata.get("%s_joinmodes" % self.name) or conf.conf.get(self.name, {}).get('joinmodes') or ''
joinmodes = ''.join([m for m in joinmodes if m in irc.prefixmodes])
if autojoin:
log.debug('(%s/%s) Adding channels %s to autojoin', irc.name, self.name, channels)
self.extra_channels[irc.name] |= channels
for chan in channels:
if isChannel(chan):
if u in irc.channels[chan].users:
log.debug('(%s) Skipping join of services %s to channel %s - it is already present', irc.name, self.name, chan)
continue
log.debug('(%s) Joining services %s to channel %s with modes %r', irc.name, self.name, chan, joinmodes)
if joinmodes: # Modes on join were specified; use SJOIN to burst our service
irc.proto.sjoin(irc.sid, chan, [(joinmodes, u)])
else:
irc.proto.join(u, chan)
irc.callHooks([irc.sid, 'PYLINK_SERVICE_JOIN', {'channel': chan, 'users': [u]}])
else:
log.warning('(%s) Ignoring invalid autojoin channel %r.', irc.name, chan)
def reply(self, irc, text, notice=False, private=False):
"""Replies to a message as the service in question."""
servuid = self.uids.get(irc.name)