more updates :D

This commit is contained in:
Luke Rogers 2013-10-03 23:15:06 +13:00
parent c71ca0632a
commit 59890e740a
4 changed files with 28 additions and 13 deletions

View file

@ -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()

View file

@ -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)

View file

@ -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):

View file

@ -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])