little changes

This commit is contained in:
Luke Rogers 2013-10-03 16:40:32 +13:00
parent b08c0a46a9
commit d0bf5ecbc8
3 changed files with 33 additions and 24 deletions

View file

@ -19,7 +19,6 @@ signal.signal(signal.SIGINT, exit_gracefully)
# create new bot object # create new bot object
cloudbot = bot.Bot() cloudbot = bot.Bot()
cloudbot.logger.debug("Bot initalized, starting main loop.")
while cloudbot.running: # start the main loop
cloudbot.loop() cloudbot.run()

View file

@ -61,12 +61,16 @@ class Bot(object):
self.loader = loader.PluginLoader(self) self.loader = loader.PluginLoader(self)
def loop(self): def run(self):
"""recieves input from the IRC engine and processes it""" """recieves input from the IRC engine and processes it"""
self.logger.info("Starting main thread.")
while True:
for conn in self.connections.itervalues(): for conn in self.connections.itervalues():
try: try:
incoming = conn.parsed_queue.get_nowait() incoming = conn.parsed_queue.get_nowait()
if incoming == StopIteration:
# IRC engine has signalled timeout, so reconnect (ugly)
conn.reconnect()
main.main(self, conn, incoming) main.main(self, conn, incoming)
except Queue.Empty: except Queue.Empty:
pass pass

View file

@ -125,9 +125,11 @@ class ParseThread(threading.Thread):
# get a message from the input queue # get a message from the input queue
msg = self.input_queue.get() msg = self.input_queue.get()
#if msg == StopIteration: if msg == StopIteration:
# self.irc.connect() # got a StopIteration from the recieve thread, pass it on
# continue # so the main thread can restart the connection
self.parsed_queue.put(StopIteration)
continue
# parse the message # parse the message
if msg.startswith(":"): # has a prefix if msg.startswith(":"): # has a prefix
@ -151,7 +153,7 @@ class ParseThread(threading.Thread):
self.output_queue.put(str) self.output_queue.put(str)
class Connection(object): class IRCConnection(object):
"""handles an IRC connection""" """handles an IRC connection"""
def __init__(self, name, host, port, input_queue, output_queue): def __init__(self, name, host, port, input_queue, output_queue):
self.output_queue = output_queue # lines to be sent out self.output_queue = output_queue # lines to be sent out
@ -160,7 +162,7 @@ class Connection(object):
self.conn_name = name self.conn_name = name
self.host = host self.host = host
self.port = port self.port = port
self.timeout = 300 self.timeout = 1
def create_socket(self): def create_socket(self):
return socket.socket(socket.AF_INET, socket.TCP_NODELAY) return socket.socket(socket.AF_INET, socket.TCP_NODELAY)
@ -181,16 +183,20 @@ class Connection(object):
time.sleep(.1) time.sleep(.1)
self.socket.close() self.socket.close()
def reconnect(self):
self.stop()
self.connect()
class SSLConnection(Connection):
class SSLIRCConnection(IRCConnection):
"""handles a SSL IRC connection""" """handles a SSL IRC connection"""
def __init__(self, name, host, port, input_queue, output_queue, ignore_cert_errors): def __init__(self, name, host, port, input_queue, output_queue, ignore_cert_errors):
self.ignore_cert_errors = 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): 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_reqs=CERT_NONE if self.ignore_cert_errors else
CERT_REQUIRED) CERT_REQUIRED)
@ -231,7 +237,7 @@ class IRC(object):
self.parse_thread.start() self.parse_thread.start()
def create_connection(self): 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) self.input_queue, self.output_queue)
def stop(self): def stop(self):
@ -284,5 +290,5 @@ class SSLIRC(IRC):
IRC.__init__(self, name, server, nick, port, channels, conf) IRC.__init__(self, name, server, nick, port, channels, conf)
def create_connection(self): 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) self.output_queue, self.ignore_cert_errors)