mirror of
https://github.com/jlu5/PyLink.git
synced 2025-01-23 02:34:09 +01:00
Move parseArgs and removeClient into the base Protocol class
These aren't really TS6-specific, and can be useful anywhere.
This commit is contained in:
parent
e92f35018f
commit
c77d170765
38
classes.py
38
classes.py
@ -408,6 +408,44 @@ class Protocol():
|
||||
self.casemapping = 'rfc1459'
|
||||
self.hook_map = {}
|
||||
|
||||
def parseArgs(self, args):
|
||||
"""Parses a string of RFC1459-style arguments split into a list, where ":" may
|
||||
be used for multi-word arguments that last until the end of a line.
|
||||
"""
|
||||
real_args = []
|
||||
for idx, arg in enumerate(args):
|
||||
real_args.append(arg)
|
||||
# If the argument starts with ':' and ISN'T the first argument.
|
||||
# The first argument is used for denoting the source UID/SID.
|
||||
if arg.startswith(':') and idx != 0:
|
||||
# : is used for multi-word arguments that last until the end
|
||||
# of the message. We can use list splicing here to turn them all
|
||||
# into one argument.
|
||||
# Set the last arg to a joined version of the remaining args
|
||||
arg = args[idx:]
|
||||
arg = ' '.join(arg)[1:]
|
||||
# Cut the original argument list right before the multi-word arg,
|
||||
# and then append the multi-word arg.
|
||||
real_args = args[:idx]
|
||||
real_args.append(arg)
|
||||
break
|
||||
return real_args
|
||||
|
||||
def removeClient(self, numeric):
|
||||
"""Internal function to remove a client from our internal state."""
|
||||
for c, v in self.irc.channels.copy().items():
|
||||
v.removeuser(numeric)
|
||||
# Clear empty non-permanent channels.
|
||||
if not (self.irc.channels[c].users or ((self.irc.cmodes.get('permanent'), None) in self.irc.channels[c].modes)):
|
||||
del self.irc.channels[c]
|
||||
assert numeric not in v.users, "IrcChannel's removeuser() is broken!"
|
||||
|
||||
sid = numeric[:3]
|
||||
log.debug('Removing client %s from self.irc.users', numeric)
|
||||
del self.irc.users[numeric]
|
||||
log.debug('Removing client %s from self.irc.servers[%s].users', numeric, sid)
|
||||
self.irc.servers[sid].users.discard(numeric)
|
||||
|
||||
class FakeProto(Protocol):
|
||||
"""Dummy protocol module for testing purposes."""
|
||||
def handle_events(self, data):
|
||||
|
@ -12,29 +12,6 @@ class TS6BaseProtocol(Protocol):
|
||||
"""Sends a TS6-style raw command from a source numeric to the self.irc connection given."""
|
||||
self.irc.send(':%s %s' % (source, msg))
|
||||
|
||||
def parseArgs(self, args):
|
||||
"""Parses a string of RFC1459-style arguments split into a list, where ":" may
|
||||
be used for multi-word arguments that last until the end of a line.
|
||||
"""
|
||||
real_args = []
|
||||
for idx, arg in enumerate(args):
|
||||
real_args.append(arg)
|
||||
# If the argument starts with ':' and ISN'T the first argument.
|
||||
# The first argument is used for denoting the source UID/SID.
|
||||
if arg.startswith(':') and idx != 0:
|
||||
# : is used for multi-word arguments that last until the end
|
||||
# of the message. We can use list splicing here to turn them all
|
||||
# into one argument.
|
||||
# Set the last arg to a joined version of the remaining args
|
||||
arg = args[idx:]
|
||||
arg = ' '.join(arg)[1:]
|
||||
# Cut the original argument list right before the multi-word arg,
|
||||
# and then append the multi-word arg.
|
||||
real_args = args[:idx]
|
||||
real_args.append(arg)
|
||||
break
|
||||
return real_args
|
||||
|
||||
def parseTS6Args(self, args):
|
||||
"""Similar to parseArgs(), but stripping leading colons from the first argument
|
||||
of a line (usually the sender field)."""
|
||||
@ -74,21 +51,6 @@ class TS6BaseProtocol(Protocol):
|
||||
self._send(numeric, 'NICK %s %s' % (newnick, int(time.time())))
|
||||
self.irc.users[numeric].nick = newnick
|
||||
|
||||
def removeClient(self, numeric):
|
||||
"""Internal function to remove a client from our internal state."""
|
||||
for c, v in self.irc.channels.copy().items():
|
||||
v.removeuser(numeric)
|
||||
# Clear empty non-permanent channels.
|
||||
if not (self.irc.channels[c].users or ((self.irc.cmodes.get('permanent'), None) in self.irc.channels[c].modes)):
|
||||
del self.irc.channels[c]
|
||||
assert numeric not in v.users, "IrcChannel's removeuser() is broken!"
|
||||
|
||||
sid = numeric[:3]
|
||||
log.debug('Removing client %s from self.irc.users', numeric)
|
||||
del self.irc.users[numeric]
|
||||
log.debug('Removing client %s from self.irc.servers[%s].users', numeric, sid)
|
||||
self.irc.servers[sid].users.discard(numeric)
|
||||
|
||||
def partClient(self, client, channel, reason=None):
|
||||
"""Sends a part from a PyLink client."""
|
||||
channel = utils.toLower(self.irc, channel)
|
||||
|
Loading…
Reference in New Issue
Block a user