mirror of
https://github.com/jlu5/PyLink.git
synced 2025-01-13 05:32:33 +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:
parent
bc9863d9e0
commit
3f6f78be9a
21
main.py
21
main.py
@ -16,11 +16,8 @@ import utils
|
|||||||
import coreplugin
|
import coreplugin
|
||||||
|
|
||||||
class Irc():
|
class Irc():
|
||||||
def __init__(self, netname, proto, conf):
|
|
||||||
# Initialize some variables
|
def initVars(self):
|
||||||
self.connected = threading.Event()
|
|
||||||
self.name = netname.lower()
|
|
||||||
self.conf = conf
|
|
||||||
# Server, channel, and user indexes to be populated by our protocol module
|
# Server, channel, and user indexes to be populated by our protocol module
|
||||||
self.servers = {}
|
self.servers = {}
|
||||||
self.users = {}
|
self.users = {}
|
||||||
@ -50,6 +47,14 @@ class Irc():
|
|||||||
# Uplink SID (filled in by protocol module)
|
# Uplink SID (filled in by protocol module)
|
||||||
self.uplink = None
|
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.serverdata = conf['servers'][netname]
|
||||||
self.sid = self.serverdata["sid"]
|
self.sid = self.serverdata["sid"]
|
||||||
self.botdata = conf['bot']
|
self.botdata = conf['bot']
|
||||||
@ -80,7 +85,7 @@ class Irc():
|
|||||||
except (socket.error, classes.ProtocolError, ConnectionError) as e:
|
except (socket.error, classes.ProtocolError, ConnectionError) as e:
|
||||||
log.warning('(%s) Disconnected from IRC: %s: %s',
|
log.warning('(%s) Disconnected from IRC: %s: %s',
|
||||||
self.name, type(e).__name__, str(e))
|
self.name, type(e).__name__, str(e))
|
||||||
self.disconnect()
|
self.disconnect()
|
||||||
autoconnect = self.serverdata.get('autoconnect')
|
autoconnect = self.serverdata.get('autoconnect')
|
||||||
log.debug('(%s) Autoconnect delay set to %s seconds.', self.name, autoconnect)
|
log.debug('(%s) Autoconnect delay set to %s seconds.', self.name, autoconnect)
|
||||||
if autoconnect is not None and autoconnect >= 0:
|
if autoconnect is not None and autoconnect >= 0:
|
||||||
@ -98,6 +103,9 @@ class Irc():
|
|||||||
self.pingTimer.cancel()
|
self.pingTimer.cancel()
|
||||||
except: # Socket timed out during creation; ignore
|
except: # Socket timed out during creation; ignore
|
||||||
pass
|
pass
|
||||||
|
self.callHooks([None, 'PYLINK_DISCONNECT', {}])
|
||||||
|
# Reset all our variables - this is important!
|
||||||
|
self.initVars()
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
buf = ""
|
buf = ""
|
||||||
@ -108,6 +116,7 @@ class Irc():
|
|||||||
data = self.socket.recv(2048).decode("utf-8")
|
data = self.socket.recv(2048).decode("utf-8")
|
||||||
buf += data
|
buf += data
|
||||||
if self.connected and not data:
|
if self.connected and not data:
|
||||||
|
log.warn('(%s) No data received and self.connected is not set; disconnecting!', self.name)
|
||||||
break
|
break
|
||||||
while '\n' in buf:
|
while '\n' in buf:
|
||||||
line, buf = buf.split('\n', 1)
|
line, buf = buf.split('\n', 1)
|
||||||
|
@ -88,8 +88,8 @@ def getPrefixModes(irc, remoteirc, channel, user):
|
|||||||
return modes
|
return modes
|
||||||
|
|
||||||
def getRemoteUser(irc, remoteirc, user):
|
def getRemoteUser(irc, remoteirc, user):
|
||||||
# If the user (stored here as {(netname, UID):
|
# If the user (stored here as {('netname', 'UID'):
|
||||||
# {network1: UID1, network2: UID2}}) exists, don't spawn it
|
# {'network1': 'UID1', 'network2': 'UID2'}}) exists, don't spawn it
|
||||||
# again!
|
# again!
|
||||||
try:
|
try:
|
||||||
if user == remoteirc.pseudoclient.uid:
|
if user == remoteirc.pseudoclient.uid:
|
||||||
@ -698,3 +698,12 @@ def main():
|
|||||||
def handle_endburst(irc, numeric, command, args):
|
def handle_endburst(irc, numeric, command, args):
|
||||||
initializeAll(irc)
|
initializeAll(irc)
|
||||||
utils.add_hook(handle_endburst, "ENDBURST")
|
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")
|
||||||
|
Loading…
Reference in New Issue
Block a user