diff --git a/main.py b/main.py index e620d20..4b2d7f2 100755 --- a/main.py +++ b/main.py @@ -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) diff --git a/plugins/relay.py b/plugins/relay.py index dc0f889..2b13a98 100644 --- a/plugins/relay.py +++ b/plugins/relay.py @@ -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")