3
0
mirror of https://github.com/jlu5/PyLink.git synced 2024-12-24 19:52:53 +01:00

relay + Irc: keep track of network disconnects

This introduces a new PYLINK_DISCONNECT meta-hook that is called when a network disconnects. Also, reset the state variables (i.e. servers, users, etc.) on disconnect! (this was missed before)

Closes #60.
This commit is contained in:
James Lu 2015-07-17 16:55:44 -07:00
parent bc9863d9e0
commit 3f6f78be9a
2 changed files with 26 additions and 8 deletions

21
main.py
View File

@ -16,11 +16,8 @@ import utils
import coreplugin
class Irc():
def __init__(self, netname, proto, conf):
# Initialize some variables
self.connected = threading.Event()
self.name = netname.lower()
self.conf = conf
def initVars(self):
# Server, channel, and user indexes to be populated by our protocol module
self.servers = {}
self.users = {}
@ -50,6 +47,14 @@ class Irc():
# Uplink SID (filled in by protocol module)
self.uplink = None
def __init__(self, netname, proto, conf):
# Initialize some variables
self.connected = threading.Event()
self.name = netname.lower()
self.conf = conf
self.initVars()
self.serverdata = conf['servers'][netname]
self.sid = self.serverdata["sid"]
self.botdata = conf['bot']
@ -80,7 +85,7 @@ class Irc():
except (socket.error, classes.ProtocolError, ConnectionError) as e:
log.warning('(%s) Disconnected from IRC: %s: %s',
self.name, type(e).__name__, str(e))
self.disconnect()
self.disconnect()
autoconnect = self.serverdata.get('autoconnect')
log.debug('(%s) Autoconnect delay set to %s seconds.', self.name, autoconnect)
if autoconnect is not None and autoconnect >= 0:
@ -98,6 +103,9 @@ class Irc():
self.pingTimer.cancel()
except: # Socket timed out during creation; ignore
pass
self.callHooks([None, 'PYLINK_DISCONNECT', {}])
# Reset all our variables - this is important!
self.initVars()
def run(self):
buf = ""
@ -108,6 +116,7 @@ class Irc():
data = self.socket.recv(2048).decode("utf-8")
buf += data
if self.connected and not data:
log.warn('(%s) No data received and self.connected is not set; disconnecting!', self.name)
break
while '\n' in buf:
line, buf = buf.split('\n', 1)

View File

@ -88,8 +88,8 @@ def getPrefixModes(irc, remoteirc, channel, user):
return modes
def getRemoteUser(irc, remoteirc, user):
# If the user (stored here as {(netname, UID):
# {network1: UID1, network2: UID2}}) exists, don't spawn it
# If the user (stored here as {('netname', 'UID'):
# {'network1': 'UID1', 'network2': 'UID2'}}) exists, don't spawn it
# again!
try:
if user == remoteirc.pseudoclient.uid:
@ -698,3 +698,12 @@ def main():
def handle_endburst(irc, numeric, command, args):
initializeAll(irc)
utils.add_hook(handle_endburst, "ENDBURST")
def handle_disconnect(irc, numeric, command, args):
for k, v in relayusers.copy().items():
if irc.name in v:
del relayusers[k][irc.name]
if k[0] == irc.name:
handle_quit(irc, k[1], 'PYLINK_DISCONNECT', {'text': 'Home network lost connection.'})
utils.add_hook(handle_disconnect, "PYLINK_DISCONNECT")