diff --git a/cloudbot.py b/cloudbot.py index fd46814..7c7ae38 100644 --- a/cloudbot.py +++ b/cloudbot.py @@ -3,6 +3,7 @@ from core import bot import os import sys +import time import signal # check python version @@ -27,8 +28,15 @@ def exit_gracefully(signum, frame): original_sigint = signal.getsignal(signal.SIGINT) signal.signal(signal.SIGINT, exit_gracefully) -# create new bot object -cloudbot = bot.Bot() +# little restart loop +while True: + # create new bot object + cloudbot = bot.Bot() -# start the main loop -cloudbot.run() + cloudbot.run() + if cloudbot.do_restart: + # this kills the bot + # TODO: make it not just kill the bot + sys.exit() + else: + sys.exit() diff --git a/core/bot.py b/core/bot.py index f742297..a1fc554 100644 --- a/core/bot.py +++ b/core/bot.py @@ -1,6 +1,5 @@ import time import logging -import sys import re import os import Queue @@ -46,6 +45,7 @@ class Bot(object): # basic variables self.start_time = time.time() self.running = True + self.do_restart = False # set up config and logging self.setup() @@ -64,13 +64,13 @@ class Bot(object): def run(self): """recieves input from the IRC engine and processes it""" self.logger.info("Starting main thread.") - while True: + while self.running: 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() + conn.connection.reconnect() main.main(self, conn, incoming) except Queue.Empty: pass @@ -123,7 +123,6 @@ class Bot(object): def stop(self, reason=None): """quits all networks and shuts the bot down""" self.logger.info("Stopping bot.") - self.running = False # wait for the bot loop to stop time.sleep(1) @@ -145,4 +144,11 @@ class Bot(object): self.logger.debug("Logging engine stopped") logging.shutdown() - sys.exit() + + self.running = False + + + def restart(self, reason=None): + """shuts the bot down and restarts it""" + self.do_restart = True + self.stop(reason) diff --git a/core/irc.py b/core/irc.py index 1ff2389..2708d1c 100755 --- a/core/irc.py +++ b/core/irc.py @@ -33,13 +33,13 @@ class ReceiveThread(threading.Thread): self.socket = socket self.timeout = timeout + self.shutdown = False threading.Thread.__init__(self) def recv_from_socket(self, nbytes): return self.socket.recv(nbytes) def handle_receive_exception(self, error, last_timestamp): - print error if time.time() - last_timestamp > self.timeout: self.input_queue.put(StopIteration) self.socket.close() @@ -51,7 +51,7 @@ class ReceiveThread(threading.Thread): def run(self): last_timestamp = time.time() - while True: + while not self.shutdown: try: data = self.recv_from_socket(4096) self.input_buffer += data @@ -180,7 +180,8 @@ class IRCConnection(object): def stop(self): self.send_thread.shutdown = True - time.sleep(.1) + self.receive_thread.shutdown = True + time.sleep(0.1) self.socket.close() def reconnect(self): diff --git a/core/main.py b/core/main.py index ade5657..faf2cbf 100755 --- a/core/main.py +++ b/core/main.py @@ -34,7 +34,7 @@ class Input(dict): """sends an ctcp to the current channel/user or a specific channel/user""" conn.ctcp(target, ctcp_type, message) - def notice(message, target=user): + def notice(message, target=nick): """sends a notice to the current channel/user or a specific channel/user""" conn.cmd('NOTICE', [target, message])