From 6e37e1c05d8fb770aa987210e06c1b8f7d9d79fd Mon Sep 17 00:00:00 2001 From: James Lu Date: Wed, 15 Jul 2015 13:49:12 -0700 Subject: [PATCH] make Irc.connected a threaded event object, setting it to True ONLY when we receive server capabilities from our uplink The previous behavior set this to True as soon as we ran connect(), but this caused problems because the default capabilities (i.e. nicklen) that Irc() initializes won't match the real value of the network. --- main.py | 8 ++++---- protocols/inspircd.py | 2 ++ 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index c6edd6b..515a946 100755 --- a/main.py +++ b/main.py @@ -16,7 +16,7 @@ import utils class Irc(): def __init__(self, netname, proto, conf): # Initialize some variables - self.connected = False + self.connected = threading.Event() self.name = netname.lower() self.conf = conf # Server, channel, and user indexes to be populated by our protocol module @@ -62,11 +62,10 @@ class Irc(): log.error('(%s) Failed to connect to IRC: %s: %s', self.name, type(e).__name__, str(e)) self.disconnect() - self.connected = True self.run() def disconnect(self): - self.connected = False + self.connected.clear() self.socket.close() autoconnect = self.serverdata.get('autoconnect') if autoconnect is not None and autoconnect >= 0: @@ -77,7 +76,7 @@ class Irc(): def run(self): buf = "" data = "" - while self.connected: + while True: try: data = self.socket.recv(2048).decode("utf-8") buf += data @@ -91,6 +90,7 @@ class Irc(): log.error('(%s) Disconnected from IRC: %s: %s', self.name, type(e).__name__, str(e)) self.disconnect() + break def send(self, data): # Safeguard against newlines in input!! Otherwise, each line gets diff --git a/protocols/inspircd.py b/protocols/inspircd.py index 261ba70..11bd14c 100644 --- a/protocols/inspircd.py +++ b/protocols/inspircd.py @@ -540,6 +540,8 @@ def handle_events(irc, data): irc.umodes['*A'], irc.umodes['*B'], irc.umodes['*C'], irc.umodes['*D'] \ = caps['USERMODES'].split(',') irc.prefixmodes = re.search(r'\((.*?)\)', caps['PREFIX']).group(1) + # Sanity check: set this AFTER we fetch the capabilities for the network! + irc.connected.set() try: real_args = [] for arg in args: