mirror of
https://github.com/jlu5/PyLink.git
synced 2024-11-24 03:29:28 +01:00
ServiceBot: modularize join() for explicit channel joining (#326)
This commit is contained in:
parent
0cce6ca488
commit
a040c3c7d2
@ -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
|
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
|
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':
|
if name != 'pylink' and irc.protoname == 'clientbot':
|
||||||
# Prefix service bots spawned on Clientbot to prevent possible nick collisions.
|
# Prefix service bots spawned on Clientbot to prevent possible nick collisions.
|
||||||
nick = 'PyLinkService@' + nick
|
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
|
# TODO: channels should be tracked in a central database, not hardcoded
|
||||||
# in conf.
|
# in conf.
|
||||||
channels = set(irc.serverdata.get('channels', [])) | sbot.extra_channels.get(irc.name, set())
|
channels = set(irc.serverdata.get('channels', [])) | sbot.extra_channels.get(irc.name, set())
|
||||||
|
sbot.join(irc, channels)
|
||||||
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)
|
|
||||||
|
|
||||||
utils.add_hook(spawn_service, 'PYLINK_NEW_SERVICE')
|
utils.add_hook(spawn_service, 'PYLINK_NEW_SERVICE')
|
||||||
|
|
||||||
|
@ -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)
|
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)
|
relayJoins(irc, channel, irc.channels[channel].users, irc.channels[channel].ts)
|
||||||
|
|
||||||
world.services['pylink'].extra_channels[irc.name].add(channel)
|
world.services['pylink'].join(irc, channel)
|
||||||
if irc.pseudoclient and irc.pseudoclient.uid not in irc.channels[channel].users:
|
|
||||||
irc.proto.join(irc.pseudoclient.uid, channel)
|
|
||||||
|
|
||||||
def removeChannel(irc, channel):
|
def removeChannel(irc, channel):
|
||||||
"""Destroys a relay channel by parting all of its users."""
|
"""Destroys a relay channel by parting all of its users."""
|
||||||
|
38
utils.py
38
utils.py
@ -235,6 +235,44 @@ class ServiceBot():
|
|||||||
else:
|
else:
|
||||||
raise NotImplementedError("Network specific plugins not supported yet.")
|
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):
|
def reply(self, irc, text, notice=False, private=False):
|
||||||
"""Replies to a message as the service in question."""
|
"""Replies to a message as the service in question."""
|
||||||
servuid = self.uids.get(irc.name)
|
servuid = self.uids.get(irc.name)
|
||||||
|
Loading…
Reference in New Issue
Block a user