3
0
mirror of https://github.com/jlu5/PyLink.git synced 2025-01-11 12:42:34 +01:00

protocols: use KeyedDefaultdict to enumerate UID generators

This commit is contained in:
James Lu 2016-04-24 21:16:41 -07:00
parent 7f46e1c35c
commit f8c2ee3ed3
6 changed files with 27 additions and 19 deletions

View File

@ -92,18 +92,16 @@ class HybridProtocol(TS6Protocol):
def spawnClient(self, nick, ident='null', host='null', realhost=None, modes=set(),
server=None, ip='0.0.0.0', realname=None, ts=None, opertype=None,
manipulatable=False):
"""Spawns a client with nick <nick> on the given IRC connection.
"""Spawns a client with the given options.
Note: No nick collision / valid nickname checks are done here; it is
up to plugins to make sure they don't introduce anything invalid."""
server = server or self.irc.sid
if not self.irc.isInternalServer(server):
raise ValueError('Server %r is not a PyLink internal PseudoServer!' % server)
# Create an UIDGenerator instance for every SID, so that each gets
# distinct values.
uid = self.uidgen.setdefault(server, TS6UIDGenerator(server)).next_uid()
# EUID:
# parameters: nickname, hopcount, nickTS, umodes, username,
# visible hostname, IP address, UID, real hostname, account name, gecos
raise ValueError('Server %r is not a PyLink server!' % server)
uid = self.uidgen[server].next_uid()
ts = ts or int(time.time())
realname = realname or self.irc.botdata['realname']
realhost = realhost or host

View File

@ -44,19 +44,22 @@ class InspIRCdProtocol(TS6BaseProtocol):
Note: No nick collision / valid nickname checks are done here; it is
up to plugins to make sure they don't introduce anything invalid."""
server = server or self.irc.sid
if not self.irc.isInternalServer(server):
raise ValueError('Server %r is not a PyLink server!' % server)
# Create an UIDGenerator instance for every SID, so that each gets
# distinct values.
uid = self.uidgen.setdefault(server, TS6UIDGenerator(server)).next_uid()
uid = self.uidgen[server].next_uid()
ts = ts or int(time.time())
realname = realname or self.irc.botdata['realname']
realhost = realhost or host
raw_modes = utils.joinModes(modes)
u = self.irc.users[uid] = IrcUser(nick, ts, uid, ident=ident, host=host, realname=realname,
realhost=realhost, ip=ip, manipulatable=manipulatable, opertype=opertype)
utils.applyModes(self.irc, uid, modes)
self.irc.servers[server].users.add(uid)
self._send(server, "UID {uid} {ts} {nick} {realhost} {host} {ident} {ip}"
" {ts} {modes} + :{realname}".format(ts=ts, host=host,
nick=nick, ident=ident, uid=uid,

View File

@ -68,8 +68,8 @@ class P10Protocol(Protocol):
def __init__(self, irc):
super().__init__(irc)
# Dictionary of UID generators (one for each server) that the protocol module will fill in.
self.uidgen = {}
# Dictionary of UID generators (one for each server).
self.uidgen = structures.KeyedDefaultdict(P10UIDGenerator)
# SID generator for P10.
self.sidgen = P10SIDGenerator(irc)

View File

@ -36,11 +36,12 @@ class TS6Protocol(TS6BaseProtocol):
Note: No nick collision / valid nickname checks are done here; it is
up to plugins to make sure they don't introduce anything invalid."""
server = server or self.irc.sid
if not self.irc.isInternalServer(server):
raise ValueError('Server %r is not a PyLink server!' % server)
# Create an UIDGenerator instance for every SID, so that each gets
# distinct values.
uid = self.uidgen.setdefault(server, TS6UIDGenerator(server)).next_uid()
uid = self.uidgen[server].next_uid()
# EUID:
# parameters: nickname, hopcount, nickTS, umodes, username,
# visible hostname, IP address, UID, real hostname, account name, gecos
@ -50,13 +51,16 @@ class TS6Protocol(TS6BaseProtocol):
raw_modes = utils.joinModes(modes)
u = self.irc.users[uid] = IrcUser(nick, ts, uid, ident=ident, host=host, realname=realname,
realhost=realhost, ip=ip, manipulatable=manipulatable, opertype=opertype)
utils.applyModes(self.irc, uid, modes)
self.irc.servers[server].users.add(uid)
self._send(server, "EUID {nick} 1 {ts} {modes} {ident} {host} {ip} {uid} "
"{realhost} * :{realname}".format(ts=ts, host=host,
nick=nick, ident=ident, uid=uid,
modes=raw_modes, ip=ip, realname=realname,
realhost=realhost))
return u
def join(self, client, channel):

View File

@ -13,6 +13,7 @@ sys.path += [curdir, os.path.dirname(curdir)]
import utils
from log import log
from classes import *
import structures
class TS6SIDGenerator():
"""
@ -107,8 +108,8 @@ class TS6BaseProtocol(Protocol):
def __init__(self, irc):
super().__init__(irc)
# Dictionary of UID generators (one for each server) that the protocol module will fill in.
self.uidgen = {}
# Dictionary of UID generators (one for each server).
self.uidgen = structures.KeyedDefaultdict(TS6UIDGenerator)
# SID generator for TS6.
self.sidgen = TS6SIDGenerator(irc)

View File

@ -70,9 +70,11 @@ class UnrealProtocol(TS6BaseProtocol):
server = server or self.irc.sid
if not self.irc.isInternalServer(server):
raise ValueError('Server %r is not a PyLink server!' % server)
# Unreal 4.0 uses TS6-style UIDs. They don't start from AAAAAA like other IRCd's
# do, but that doesn't matter to us...
uid = self.uidgen.setdefault(server, TS6UIDGenerator(server)).next_uid()
uid = self.uidgen[server].next_uid()
ts = ts or int(time.time())
realname = realname or self.irc.botdata['realname']
realhost = realhost or host