3
0
mirror of https://github.com/jlu5/PyLink.git synced 2025-06-04 22:07:30 +02:00

clientbot: implement JOIN, uplink enumeration

This commit is contained in:
James Lu 2016-07-17 15:20:57 -07:00
parent c6ed06ba61
commit 58fa769ba0

View File

@ -51,6 +51,7 @@ class ClientbotWrapperProtocol(Protocol):
STUB: Pretends to spawn a new client with a subset of the given options. STUB: Pretends to spawn a new client with a subset of the given options.
""" """
server = server or self.irc.sid
uid = self.uidgen.next_uid() uid = self.uidgen.next_uid()
ts = ts or int(time.time()) ts = ts or int(time.time())
@ -58,7 +59,7 @@ class ClientbotWrapperProtocol(Protocol):
log.debug('(%s) spawnClient stub called, saving nick %s as PUID %s', self.irc.name, nick, uid) log.debug('(%s) spawnClient stub called, saving nick %s as PUID %s', self.irc.name, nick, uid)
u = self.irc.users[uid] = IrcUser(nick, ts, uid, ident=ident, host=host, realname=realname) u = self.irc.users[uid] = IrcUser(nick, ts, uid, ident=ident, host=host, realname=realname)
log.debug('(%s) self.irc.users: %s', self.irc.name, self.irc.users) log.debug('(%s) self.irc.users: %s', self.irc.name, self.irc.users)
self.irc.servers[self.irc.sid].users.add(uid) self.irc.servers[server].users.add(uid)
return u return u
def spawnServer(self, name, sid=None, uplink=None, desc=None, endburst_delay=0): def spawnServer(self, name, sid=None, uplink=None, desc=None, endburst_delay=0):
@ -70,8 +71,18 @@ class ClientbotWrapperProtocol(Protocol):
self.irc.servers[sid] = IrcServer(uplink, name) self.irc.servers[sid] = IrcServer(uplink, name)
return sid return sid
def join(self, *args): def join(self, client, channel):
return """STUB: Joins a user to a channel."""
channel = self.irc.toLower(channel)
self.irc.channels[channel].users.add(client)
self.irc.users[client].channels.add(channel)
if client == self.irc.pseudoclient.uid:
self.irc.send('JOIN %s' % channel)
else:
log.debug('(%s) join: faking JOIN of client %s/%s to %s', self.irc.name, client,
self.irc.getFriendlyName(client), channel)
def ping(self, *args): def ping(self, *args):
return return
@ -80,9 +91,19 @@ class ClientbotWrapperProtocol(Protocol):
"""Event handler for the RFC1459 (clientbot) protocol. """Event handler for the RFC1459 (clientbot) protocol.
""" """
data = data.split(" ") data = data.split(" ")
try:
args = self.parsePrefixedArgs(data) args = self.parsePrefixedArgs(data)
sender = args[0] sender = args[0]
command = args[1]
args = args[1:]
except IndexError:
# Raw command without an explicit sender; assume it's being sent by our uplink.
args = self.parseArgs(data)
sender = self.irc.uplink
command = args[0]
args = args[1:]
else:
# PyLink as a services framework expects UIDs and SIDs for everythiung. Since we connect # PyLink as a services framework expects UIDs and SIDs for everythiung. Since we connect
# as a bot here, there's no explicit user introduction, so we're going to generate # as a bot here, there's no explicit user introduction, so we're going to generate
# pseudo-uids and pseudo-sids as we see prefixes. # pseudo-uids and pseudo-sids as we see prefixes.
@ -94,15 +115,12 @@ class ClientbotWrapperProtocol(Protocol):
idsource = self.spawnServer(sender) idsource = self.spawnServer(sender)
else: else:
# Sender is a nick!user@host prefix. Split it into its relevant parts. # Sender is a nick!user@host prefix. Split it into its relevant parts.
nick, identhost = sender.split('!', 1) nick, ident, host = utils.splitHostmask(sender)
idsource = self.irc.nickToUid(nick) idsource = self.irc.nickToUid(nick)
if not idsource: if not idsource:
ident, host = identhost.split('@', 1) idsource = self.spawnClient(nick, ident, host, server=self.irc.uplink).uid
idsource = self.spawnClient(nick, ident, host).uid
log.debug('(%s) handle_events: idsource is %s', self.irc.name, idsource) log.debug('(%s) handle_events: idsource is %s', self.irc.name, idsource)
command = args[1]
args = args[2:]
try: try:
func = getattr(self, 'handle_'+command.lower()) func = getattr(self, 'handle_'+command.lower())
except AttributeError: # unhandled command except AttributeError: # unhandled command
@ -112,10 +130,20 @@ class ClientbotWrapperProtocol(Protocol):
if parsed_args is not None: if parsed_args is not None:
return [idsource, command, parsed_args] return [idsource, command, parsed_args]
def handle_001(self, source, command, args):
"""
Handles 001 / RPL_WELCOME.
"""
# enumerate our uplink
self.irc.uplink = source
def handle_005(self, source, command, args): def handle_005(self, source, command, args):
""" """
Handles 005 / RPL_ISUPPORT. Handles 005 / RPL_ISUPPORT.
""" """
# TODO: capability negotiation happens here
if not self.irc.connected.is_set():
self.irc.connected.set()
return {'parse_as': 'ENDBURST'} return {'parse_as': 'ENDBURST'}
Class = ClientbotWrapperProtocol Class = ClientbotWrapperProtocol