From d0bf5ecbc89be7651eff288f7716bbf084f8315d Mon Sep 17 00:00:00 2001 From: Luke Rogers Date: Thu, 3 Oct 2013 16:40:32 +1300 Subject: [PATCH] little changes --- cloudbot.py | 5 ++--- core/bot.py | 26 +++++++++++++++----------- core/irc.py | 26 ++++++++++++++++---------- 3 files changed, 33 insertions(+), 24 deletions(-) diff --git a/cloudbot.py b/cloudbot.py index 3a0d3e5..60c6fb4 100644 --- a/cloudbot.py +++ b/cloudbot.py @@ -19,7 +19,6 @@ signal.signal(signal.SIGINT, exit_gracefully) # create new bot object cloudbot = bot.Bot() -cloudbot.logger.debug("Bot initalized, starting main loop.") -while cloudbot.running: - cloudbot.loop() +# start the main loop +cloudbot.run() diff --git a/core/bot.py b/core/bot.py index 5d4caa9..919ec03 100644 --- a/core/bot.py +++ b/core/bot.py @@ -61,19 +61,23 @@ class Bot(object): self.loader = loader.PluginLoader(self) - def loop(self): + def run(self): """recieves input from the IRC engine and processes it""" + self.logger.info("Starting main thread.") + while True: + for conn in self.connections.itervalues(): + try: + incoming = conn.parsed_queue.get_nowait() + if incoming == StopIteration: + # IRC engine has signalled timeout, so reconnect (ugly) + conn.reconnect() + main.main(self, conn, incoming) + except Queue.Empty: + pass - for conn in self.connections.itervalues(): - try: - incoming = conn.parsed_queue.get_nowait() - main.main(self, conn, incoming) - except Queue.Empty: - pass - - # if no messages are in the incoming queue, sleep - while all(connection.parsed_queue.empty() for connection in self.connections.itervalues()): - time.sleep(.1) + # if no messages are in the incoming queue, sleep + while all(connection.parsed_queue.empty() for connection in self.connections.itervalues()): + time.sleep(.1) def setup(self): diff --git a/core/irc.py b/core/irc.py index ef6c31c..a458af0 100755 --- a/core/irc.py +++ b/core/irc.py @@ -125,9 +125,11 @@ class ParseThread(threading.Thread): # get a message from the input queue msg = self.input_queue.get() - #if msg == StopIteration: - # self.irc.connect() - # continue + if msg == StopIteration: + # got a StopIteration from the recieve thread, pass it on + # so the main thread can restart the connection + self.parsed_queue.put(StopIteration) + continue # parse the message if msg.startswith(":"): # has a prefix @@ -151,7 +153,7 @@ class ParseThread(threading.Thread): self.output_queue.put(str) -class Connection(object): +class IRCConnection(object): """handles an IRC connection""" def __init__(self, name, host, port, input_queue, output_queue): self.output_queue = output_queue # lines to be sent out @@ -160,7 +162,7 @@ class Connection(object): self.conn_name = name self.host = host self.port = port - self.timeout = 300 + self.timeout = 1 def create_socket(self): return socket.socket(socket.AF_INET, socket.TCP_NODELAY) @@ -181,16 +183,20 @@ class Connection(object): time.sleep(.1) self.socket.close() + def reconnect(self): + self.stop() + self.connect() -class SSLConnection(Connection): + +class SSLIRCConnection(IRCConnection): """handles a SSL IRC connection""" def __init__(self, name, host, port, input_queue, output_queue, ignore_cert_errors): self.ignore_cert_errors = ignore_cert_errors - Connection.__init__(self, name, host, port, input_queue, output_queue) + IRCConnection.__init__(self, name, host, port, input_queue, output_queue) def create_socket(self): - return wrap_socket(Connection.create_socket(self), server_side=False, + return wrap_socket(IRCConnection.create_socket(self), server_side=False, cert_reqs=CERT_NONE if self.ignore_cert_errors else CERT_REQUIRED) @@ -231,7 +237,7 @@ class IRC(object): self.parse_thread.start() def create_connection(self): - return Connection(self.name, self.server, self.port, + return IRCConnection(self.name, self.server, self.port, self.input_queue, self.output_queue) def stop(self): @@ -284,5 +290,5 @@ class SSLIRC(IRC): IRC.__init__(self, name, server, nick, port, channels, conf) def create_connection(self): - return SSLConnection(self.name, self.server, self.port, self.input_queue, + return SSLIRCConnection(self.name, self.server, self.port, self.input_queue, self.output_queue, self.ignore_cert_errors)