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

core: Add irc argument to User and Server classes

Also, add a __deepcopy__ override to channel because we cannot clone IRCNetwork objects (locks cannot be pickled).
This commit is contained in:
James Lu 2017-08-25 13:53:45 -07:00
parent 96a202acce
commit 8c0f19422f
10 changed files with 51 additions and 36 deletions

View File

@ -12,7 +12,7 @@ import time
import socket
import ssl
import hashlib
from copy import deepcopy
from copy import copy, deepcopy
import inspect
import ipaddress
import queue
@ -41,7 +41,7 @@ class ChannelState(structures.IRCCaseInsensitiveDict):
if key not in self._data:
log.debug('(%s) ChannelState: creating new channel %s in memory', self._irc.name, key)
self._data[key] = newchan = Channel(key)
self._data[key] = newchan = Channel(self._irc, key)
return newchan
return self._data[key]
@ -1336,7 +1336,7 @@ class IRCNetwork(PyLinkNetworkCoreWithUtils):
log.info('(%s) Enumerating our own SID %s', self.name, self.sid)
host = self.hostname()
self.servers[self.sid] = Server(None, host, internal=True,
self.servers[self.sid] = Server(self, None, host, internal=True,
desc=self.serverdata.get('serverdesc')
or conf.conf['pylink']['serverdesc'])
@ -1515,7 +1515,7 @@ class User():
self.realname = realname
self.modes = set() # Tracks user modes
self.server = server
self.irc = irc
self._irc = irc
# Tracks PyLink identification status
self.account = ''
@ -1527,7 +1527,7 @@ class User():
self.services_account = ''
# Tracks channels the user is in
self.channels = structures.IRCCaseInsensitiveSet(self.irc)
self.channels = structures.IRCCaseInsensitiveSet(self._irc)
# Tracks away message status
self.away = ''
@ -1554,21 +1554,23 @@ class Server():
internal: Whether the server is an internal PyLink server.
"""
def __init__(self, uplink, name, internal=False, desc="(None given)"):
def __init__(self, irc, uplink, name, internal=False, desc="(None given)"):
self.uplink = uplink
self.users = set()
self.internal = internal
self.name = name.lower()
self.desc = desc
self._irc = irc
def __repr__(self):
return 'Server(%s)' % self.name
IrcServer = Server
class Channel(utils.DeprecatedAttributesObject, utils.CamelCaseToSnakeCase):
"""PyLink IRC channel class."""
def __init__(self, name=None):
def __init__(self, irc, name=None):
# Initialize variables, such as the topic, user list, TS, who's opped, etc.
self.users = set()
self.modes = set()
@ -1576,6 +1578,7 @@ class Channel(utils.DeprecatedAttributesObject, utils.CamelCaseToSnakeCase):
self.ts = int(time.time())
self.prefixmodes = {'op': set(), 'halfop': set(), 'voice': set(),
'owner': set(), 'admin': set()}
self._irc = irc
# Determines whether a topic has been set here or not. Protocol modules
# should set this.
@ -1596,8 +1599,20 @@ class Channel(utils.DeprecatedAttributesObject, utils.CamelCaseToSnakeCase):
self.users.discard(target)
removeuser = remove_user
def deepcopy(self):
def __deepcopy__(self, memo):
"""Returns a deep copy of the channel object."""
# XXX: we can't pickle IRCNetwork, so just return a reference of it.
channel_copy = copy(self)
# For everything else, create a copy.
for attr, val in self.__dict__.items():
if not isinstance(val, PyLinkNetworkCore):
setattr(channel_copy, attr, deepcopy(val))
memo[id(self)] = channel_copy
return channel_copy
def deepcopy(self):
return deepcopy(self)
def is_voice(self, uid):

View File

@ -141,7 +141,7 @@ class ClientbotWrapperProtocol(IRCCommonProtocol):
# For others servers, just use the server name as the SID.
sid = name
self.servers[sid] = Server(uplink, name, internal=internal)
self.servers[sid] = Server(self, uplink, name, internal=internal)
return sid
def away(self, source, text):

View File

@ -356,7 +356,7 @@ class InspIRCdProtocol(TS6BaseProtocol):
if not utils.isServerName(name):
raise ValueError('Invalid server name %r' % name)
self._send_with_prefix(uplink, 'SERVER %s * 1 %s :%s' % (name, sid, desc))
self.servers[sid] = Server(uplink, name, internal=True, desc=desc)
self.servers[sid] = Server(self, uplink, name, internal=True, desc=desc)
def endburstf():
# Delay ENDBURST by X seconds if requested.
@ -611,7 +611,7 @@ class InspIRCdProtocol(TS6BaseProtocol):
raise ProtocolError('recvpass from uplink server %s does not match configuration!' % servername)
sdesc = args[-1]
self.servers[numeric] = Server(None, servername, desc=sdesc)
self.servers[numeric] = Server(self, None, servername, desc=sdesc)
self.uplink = numeric
return
@ -620,7 +620,7 @@ class InspIRCdProtocol(TS6BaseProtocol):
servername = args[0].lower()
sid = args[3]
sdesc = args[-1]
self.servers[sid] = Server(numeric, servername, desc=sdesc)
self.servers[sid] = Server(self, numeric, servername, desc=sdesc)
return {'name': servername, 'sid': args[3], 'text': sdesc}

View File

@ -130,7 +130,7 @@ class NgIRCdProtocol(IRCS2SProtocol):
# a number, we can simply use the counter in our PSID generator for this.
server_token = sid.rsplit('@')[-1]
self._send_with_prefix(uplink, 'SERVER %s 1 %s :%s' % (name, server_token, desc))
self.servers[sid] = Server(uplink, name, internal=True, desc=desc)
self.servers[sid] = Server(self, uplink, name, internal=True, desc=desc)
return sid
def away(self, source, text):
@ -524,7 +524,7 @@ class NgIRCdProtocol(IRCS2SProtocol):
serverdesc = args[-1]
# The uplink should be set to None for the uplink; otherwise, set it equal to the sender server.
self.servers[servername] = Server(source if source != servername else None, servername, desc=serverdesc)
self.servers[servername] = Server(self, source if source != servername else None, servername, desc=serverdesc)
if self.uplink is None:
self.uplink = servername

View File

@ -674,7 +674,7 @@ class P10Protocol(IRCS2SProtocol):
self._send_with_prefix(uplink, 'SERVER %s 1 %s %s P10 %s]]] +h6 :%s' % \
(name, self.start_ts, int(time.time()), sid, desc))
self.servers[sid] = Server(uplink, name, internal=True, desc=desc)
self.servers[sid] = Server(self, uplink, name, internal=True, desc=desc)
return sid
def squit(self, source, target, text='No reason given'):
@ -832,7 +832,7 @@ class P10Protocol(IRCS2SProtocol):
servername = args[0].lower()
sid = args[5][:2]
sdesc = args[-1]
self.servers[sid] = Server(source, servername, desc=sdesc)
self.servers[sid] = Server(self, source, servername, desc=sdesc)
if self.uplink is None:
# If we haven't already found our uplink, this is probably it.

View File

@ -400,7 +400,7 @@ class TS6Protocol(TS6BaseProtocol):
# Server name and SID are sent in different messages, so we fill this
# with dummy information until we get the actual sid.
self.servers[numeric] = Server(None, '')
self.servers[numeric] = Server(self, None, '')
self.uplink = numeric
def handle_capab(self, numeric, command, args):

View File

@ -182,7 +182,7 @@ class TS6BaseProtocol(IRCS2SProtocol):
if not utils.isServerName(name):
raise ValueError('Invalid server name %r' % name)
self._send_with_prefix(uplink, 'SID %s 1 %s :%s' % (name, sid, desc))
self.servers[sid] = Server(uplink, name, internal=True, desc=desc)
self.servers[sid] = Server(self, uplink, name, internal=True, desc=desc)
return sid
def away(self, source, text):
@ -232,7 +232,7 @@ class TS6BaseProtocol(IRCS2SProtocol):
# <- :services.int SERVER a.bc 2 :(H) [GL] test jupe
servername = args[0].lower()
sdesc = args[-1]
self.servers[servername] = Server(numeric, servername, desc=sdesc)
self.servers[servername] = Server(self, numeric, servername, desc=sdesc)
return {'name': servername, 'sid': None, 'text': sdesc}
def handle_sid(self, numeric, command, args):
@ -242,7 +242,7 @@ class TS6BaseProtocol(IRCS2SProtocol):
sname = args[0].lower()
sid = args[2]
sdesc = args[-1]
self.servers[sid] = Server(numeric, sname, desc=sdesc)
self.servers[sid] = Server(self, numeric, sname, desc=sdesc)
return {'name': sname, 'sid': sid, 'text': sdesc}
def handle_svsnick(self, source, command, args):

View File

@ -480,7 +480,7 @@ class UnrealProtocol(TS6BaseProtocol):
if protover < self.min_proto_ver:
raise ProtocolError("Protocol version too old! (needs at least %s "
"(Unreal 4.x), got %s)" % (self.min_proto_ver, protover))
self.servers[numeric] = Server(None, sname, desc=sdesc)
self.servers[numeric] = Server(self, None, sname, desc=sdesc)
# Set irc.connected to True, meaning that protocol negotiation passed.
log.debug('(%s) self.connected set!', self.name)