more updates :D
This commit is contained in:
parent
c71ca0632a
commit
59890e740a
4 changed files with 28 additions and 13 deletions
10
cloudbot.py
10
cloudbot.py
|
@ -3,6 +3,7 @@ from core import bot
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
import time
|
||||||
import signal
|
import signal
|
||||||
|
|
||||||
# check python version
|
# check python version
|
||||||
|
@ -27,8 +28,15 @@ def exit_gracefully(signum, frame):
|
||||||
original_sigint = signal.getsignal(signal.SIGINT)
|
original_sigint = signal.getsignal(signal.SIGINT)
|
||||||
signal.signal(signal.SIGINT, exit_gracefully)
|
signal.signal(signal.SIGINT, exit_gracefully)
|
||||||
|
|
||||||
|
# little restart loop
|
||||||
|
while True:
|
||||||
# create new bot object
|
# create new bot object
|
||||||
cloudbot = bot.Bot()
|
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()
|
||||||
|
|
16
core/bot.py
16
core/bot.py
|
@ -1,6 +1,5 @@
|
||||||
import time
|
import time
|
||||||
import logging
|
import logging
|
||||||
import sys
|
|
||||||
import re
|
import re
|
||||||
import os
|
import os
|
||||||
import Queue
|
import Queue
|
||||||
|
@ -46,6 +45,7 @@ class Bot(object):
|
||||||
# basic variables
|
# basic variables
|
||||||
self.start_time = time.time()
|
self.start_time = time.time()
|
||||||
self.running = True
|
self.running = True
|
||||||
|
self.do_restart = False
|
||||||
|
|
||||||
# set up config and logging
|
# set up config and logging
|
||||||
self.setup()
|
self.setup()
|
||||||
|
@ -64,13 +64,13 @@ class Bot(object):
|
||||||
def run(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.")
|
self.logger.info("Starting main thread.")
|
||||||
while True:
|
while self.running:
|
||||||
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:
|
if incoming == StopIteration:
|
||||||
# IRC engine has signalled timeout, so reconnect (ugly)
|
# IRC engine has signalled timeout, so reconnect (ugly)
|
||||||
conn.reconnect()
|
conn.connection.reconnect()
|
||||||
main.main(self, conn, incoming)
|
main.main(self, conn, incoming)
|
||||||
except Queue.Empty:
|
except Queue.Empty:
|
||||||
pass
|
pass
|
||||||
|
@ -123,7 +123,6 @@ class Bot(object):
|
||||||
def stop(self, reason=None):
|
def stop(self, reason=None):
|
||||||
"""quits all networks and shuts the bot down"""
|
"""quits all networks and shuts the bot down"""
|
||||||
self.logger.info("Stopping bot.")
|
self.logger.info("Stopping bot.")
|
||||||
self.running = False
|
|
||||||
|
|
||||||
# wait for the bot loop to stop
|
# wait for the bot loop to stop
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
@ -145,4 +144,11 @@ class Bot(object):
|
||||||
|
|
||||||
self.logger.debug("Logging engine stopped")
|
self.logger.debug("Logging engine stopped")
|
||||||
logging.shutdown()
|
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)
|
||||||
|
|
|
@ -33,13 +33,13 @@ class ReceiveThread(threading.Thread):
|
||||||
self.socket = socket
|
self.socket = socket
|
||||||
self.timeout = timeout
|
self.timeout = timeout
|
||||||
|
|
||||||
|
self.shutdown = False
|
||||||
threading.Thread.__init__(self)
|
threading.Thread.__init__(self)
|
||||||
|
|
||||||
def recv_from_socket(self, nbytes):
|
def recv_from_socket(self, nbytes):
|
||||||
return self.socket.recv(nbytes)
|
return self.socket.recv(nbytes)
|
||||||
|
|
||||||
def handle_receive_exception(self, error, last_timestamp):
|
def handle_receive_exception(self, error, last_timestamp):
|
||||||
print error
|
|
||||||
if time.time() - last_timestamp > self.timeout:
|
if time.time() - last_timestamp > self.timeout:
|
||||||
self.input_queue.put(StopIteration)
|
self.input_queue.put(StopIteration)
|
||||||
self.socket.close()
|
self.socket.close()
|
||||||
|
@ -51,7 +51,7 @@ class ReceiveThread(threading.Thread):
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
last_timestamp = time.time()
|
last_timestamp = time.time()
|
||||||
while True:
|
while not self.shutdown:
|
||||||
try:
|
try:
|
||||||
data = self.recv_from_socket(4096)
|
data = self.recv_from_socket(4096)
|
||||||
self.input_buffer += data
|
self.input_buffer += data
|
||||||
|
@ -180,7 +180,8 @@ class IRCConnection(object):
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
self.send_thread.shutdown = True
|
self.send_thread.shutdown = True
|
||||||
time.sleep(.1)
|
self.receive_thread.shutdown = True
|
||||||
|
time.sleep(0.1)
|
||||||
self.socket.close()
|
self.socket.close()
|
||||||
|
|
||||||
def reconnect(self):
|
def reconnect(self):
|
||||||
|
|
|
@ -34,7 +34,7 @@ class Input(dict):
|
||||||
"""sends an ctcp to the current channel/user or a specific channel/user"""
|
"""sends an ctcp to the current channel/user or a specific channel/user"""
|
||||||
conn.ctcp(target, ctcp_type, message)
|
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"""
|
"""sends a notice to the current channel/user or a specific channel/user"""
|
||||||
conn.cmd('NOTICE', [target, message])
|
conn.cmd('NOTICE', [target, message])
|
||||||
|
|
||||||
|
|
Reference in a new issue