small tweaks to the refresh code
This commit is contained in:
parent
e84b181f80
commit
3fde4692d1
5 changed files with 23 additions and 32 deletions
|
@ -42,6 +42,7 @@ while True:
|
||||||
else:
|
else:
|
||||||
if cloudbot.do_restart:
|
if cloudbot.do_restart:
|
||||||
# create a new bot thread and start it
|
# create a new bot thread and start it
|
||||||
|
del cloudbot
|
||||||
cloudbot = bot.Bot()
|
cloudbot = bot.Bot()
|
||||||
cloudbot.start()
|
cloudbot.start()
|
||||||
continue
|
continue
|
||||||
|
|
16
core/bot.py
16
core/bot.py
|
@ -45,7 +45,6 @@ def get_logger():
|
||||||
logger.addHandler(sh)
|
logger.addHandler(sh)
|
||||||
return logger
|
return logger
|
||||||
|
|
||||||
|
|
||||||
class Bot(threading.Thread):
|
class Bot(threading.Thread):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
# basic variables
|
# basic variables
|
||||||
|
@ -74,7 +73,6 @@ class Bot(threading.Thread):
|
||||||
|
|
||||||
threading.Thread.__init__(self)
|
threading.Thread.__init__(self)
|
||||||
|
|
||||||
|
|
||||||
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.")
|
||||||
|
@ -94,7 +92,6 @@ class Bot(threading.Thread):
|
||||||
while self.running and all(c.parsed_queue.empty() for c in self.connections):
|
while self.running and all(c.parsed_queue.empty() for c in self.connections):
|
||||||
time.sleep(.1)
|
time.sleep(.1)
|
||||||
|
|
||||||
|
|
||||||
def setup(self):
|
def setup(self):
|
||||||
"""create the logger and config objects"""
|
"""create the logger and config objects"""
|
||||||
# logging
|
# logging
|
||||||
|
@ -117,7 +114,6 @@ class Bot(threading.Thread):
|
||||||
self.db_session = scoped_session(db_factory)
|
self.db_session = scoped_session(db_factory)
|
||||||
self.logger.debug("Database system initalised.")
|
self.logger.debug("Database system initalised.")
|
||||||
|
|
||||||
|
|
||||||
def connect(self):
|
def connect(self):
|
||||||
"""connect to all the networks defined in the bot config"""
|
"""connect to all the networks defined in the bot config"""
|
||||||
for conf in self.config['connections']:
|
for conf in self.config['connections']:
|
||||||
|
@ -130,16 +126,15 @@ class Bot(threading.Thread):
|
||||||
self.logger.debug("({}) Creating connection to {}.".format(name, server))
|
self.logger.debug("({}) Creating connection to {}.".format(name, server))
|
||||||
|
|
||||||
if conf['connection'].get('ssl'):
|
if conf['connection'].get('ssl'):
|
||||||
self.connections.append(irc.SSLIRC(name, server, nick, config = conf,
|
self.connections.append(irc.SSLIRC(name, server, nick, config=conf,
|
||||||
port = port, channels = conf['channels'],
|
port=port, channels=conf['channels'],
|
||||||
ignore_certificate_errors=conf['connection'].get('ignore_cert', True)))
|
ignore_certificate_errors=conf['connection'].get('ignore_cert', True)))
|
||||||
self.logger.debug("({}) Created SSL connection.".format(name))
|
self.logger.debug("({}) Created SSL connection.".format(name))
|
||||||
else:
|
else:
|
||||||
self.connections.append(irc.IRC(name, server, nick, config = conf,
|
self.connections.append(irc.IRC(name, server, nick, config=conf,
|
||||||
port = port, channels = conf['channels']))
|
port=port, channels=conf['channels']))
|
||||||
self.logger.debug("({}) Created connection.".format(name))
|
self.logger.debug("({}) Created connection.".format(name))
|
||||||
|
|
||||||
|
|
||||||
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.")
|
||||||
|
@ -165,7 +160,6 @@ class Bot(threading.Thread):
|
||||||
|
|
||||||
self.running = False
|
self.running = False
|
||||||
|
|
||||||
|
|
||||||
def restart(self, reason=None):
|
def restart(self, reason=None):
|
||||||
"""shuts the bot down and restarts it"""
|
"""shuts the bot down and restarts it"""
|
||||||
self.do_restart = True
|
self.do_restart = True
|
||||||
|
|
33
core/irc.py
33
core/irc.py
|
@ -29,10 +29,10 @@ def censor(text):
|
||||||
|
|
||||||
class ReceiveThread(threading.Thread):
|
class ReceiveThread(threading.Thread):
|
||||||
"""receives messages from IRC and puts them in the input_queue"""
|
"""receives messages from IRC and puts them in the input_queue"""
|
||||||
def __init__(self, socket, input_queue, timeout):
|
def __init__(self, sock, input_queue, timeout):
|
||||||
self.input_buffer = ""
|
self.input_buffer = ""
|
||||||
self.input_queue = input_queue
|
self.input_queue = input_queue
|
||||||
self.socket = socket
|
self.socket = sock
|
||||||
self.timeout = timeout
|
self.timeout = timeout
|
||||||
|
|
||||||
self.shutdown = False
|
self.shutdown = False
|
||||||
|
@ -76,8 +76,8 @@ class ReceiveThread(threading.Thread):
|
||||||
|
|
||||||
|
|
||||||
class SSLReceiveThread(ReceiveThread):
|
class SSLReceiveThread(ReceiveThread):
|
||||||
def __init__(self, socket, input_queue, timeout):
|
def __init__(self, sock, input_queue, timeout):
|
||||||
ReceiveThread.Thread.__init__(self, socket, input_queue, timeout)
|
ReceiveThread.__init__(self, sock, input_queue, timeout)
|
||||||
|
|
||||||
def recv_from_socket(self, nbytes):
|
def recv_from_socket(self, nbytes):
|
||||||
return self.socket.read(nbytes)
|
return self.socket.read(nbytes)
|
||||||
|
@ -94,11 +94,11 @@ class SSLReceiveThread(ReceiveThread):
|
||||||
|
|
||||||
class SendThread(threading.Thread):
|
class SendThread(threading.Thread):
|
||||||
"""sends messages from output_queue to IRC"""
|
"""sends messages from output_queue to IRC"""
|
||||||
def __init__(self, socket, conn_name, output_queue):
|
def __init__(self, sock, conn_name, output_queue):
|
||||||
self.output_buffer = ""
|
self.output_buffer = ""
|
||||||
self.output_queue = output_queue
|
self.output_queue = output_queue
|
||||||
self.conn_name = conn_name
|
self.conn_name = conn_name
|
||||||
self.socket = socket
|
self.socket = sock
|
||||||
|
|
||||||
self.shutdown = False
|
self.shutdown = False
|
||||||
threading.Thread.__init__(self)
|
threading.Thread.__init__(self)
|
||||||
|
@ -148,11 +148,11 @@ class ParseThread(threading.Thread):
|
||||||
lastparam = paramlist[-1]
|
lastparam = paramlist[-1]
|
||||||
# put the parsed message in the response queue
|
# put the parsed message in the response queue
|
||||||
self.parsed_queue.put([msg, prefix, command, params, nick, user, host,
|
self.parsed_queue.put([msg, prefix, command, params, nick, user, host,
|
||||||
mask, paramlist, lastparam])
|
mask, paramlist, lastparam])
|
||||||
# if the server pings us, pong them back
|
# if the server pings us, pong them back
|
||||||
if command == "PING":
|
if command == "PING":
|
||||||
str = "PONG :" + paramlist[0]
|
string = "PONG :" + paramlist[0]
|
||||||
self.output_queue.put(str)
|
self.output_queue.put(string)
|
||||||
|
|
||||||
|
|
||||||
class IRCConnection(object):
|
class IRCConnection(object):
|
||||||
|
@ -198,7 +198,7 @@ class SSLIRCConnection(IRCConnection):
|
||||||
|
|
||||||
def create_socket(self):
|
def create_socket(self):
|
||||||
return wrap_socket(IRCConnection.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)
|
||||||
|
|
||||||
|
|
||||||
|
@ -229,18 +229,17 @@ class IRC(object):
|
||||||
self.set_pass(self.config.get('server_password'))
|
self.set_pass(self.config.get('server_password'))
|
||||||
self.set_nick(self.nick)
|
self.set_nick(self.nick)
|
||||||
self.cmd("USER",
|
self.cmd("USER",
|
||||||
[self.config.get('user', 'cloudbot'), "3", "*", self.config.get('realname',
|
[self.config.get('user', 'cloudbot'), "3", "*",
|
||||||
'CloudBot - http://git.io/cloudbot')])
|
self.config.get('realname', 'CloudBot - http://git.io/cloudbot')])
|
||||||
|
|
||||||
self.parse_thread = ParseThread(self.input_queue, self.output_queue,
|
self.parse_thread = ParseThread(self.input_queue, self.output_queue,
|
||||||
self.parsed_queue)
|
self.parsed_queue)
|
||||||
self.parse_thread.daemon = True
|
self.parse_thread.daemon = True
|
||||||
self.parse_thread.start()
|
self.parse_thread.start()
|
||||||
|
|
||||||
|
|
||||||
def create_connection(self):
|
def create_connection(self):
|
||||||
return IRCConnection(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):
|
||||||
self.connection.stop()
|
self.connection.stop()
|
||||||
|
@ -280,8 +279,8 @@ class IRC(object):
|
||||||
else:
|
else:
|
||||||
self.send(command)
|
self.send(command)
|
||||||
|
|
||||||
def send(self, str):
|
def send(self, string):
|
||||||
self.output_queue.put(str)
|
self.output_queue.put(string)
|
||||||
|
|
||||||
|
|
||||||
class SSLIRC(IRC):
|
class SSLIRC(IRC):
|
||||||
|
@ -292,4 +291,4 @@ class SSLIRC(IRC):
|
||||||
|
|
||||||
def create_connection(self):
|
def create_connection(self):
|
||||||
return SSLIRCConnection(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)
|
||||||
|
|
|
@ -39,12 +39,10 @@ class PluginLoader(object):
|
||||||
|
|
||||||
self.load_all()
|
self.load_all()
|
||||||
|
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
"""shuts down the plugin reloader"""
|
"""shuts down the plugin reloader"""
|
||||||
self.observer.stop()
|
self.observer.stop()
|
||||||
|
|
||||||
|
|
||||||
def load_all(self):
|
def load_all(self):
|
||||||
"""runs load_file() on all python files in the plugins folder"""
|
"""runs load_file() on all python files in the plugins folder"""
|
||||||
files = set(glob.glob(os.path.join(self.path, '*.py')))
|
files = set(glob.glob(os.path.join(self.path, '*.py')))
|
||||||
|
@ -52,7 +50,6 @@ class PluginLoader(object):
|
||||||
self.load_file(f, rebuild=True)
|
self.load_file(f, rebuild=True)
|
||||||
self.rebuild()
|
self.rebuild()
|
||||||
|
|
||||||
|
|
||||||
def load_file(self, path, rebuild=False):
|
def load_file(self, path, rebuild=False):
|
||||||
"""loads (or reloads) all valid plugins from a specified file"""
|
"""loads (or reloads) all valid plugins from a specified file"""
|
||||||
filename = os.path.basename(path)
|
filename = os.path.basename(path)
|
||||||
|
@ -118,7 +115,6 @@ class PluginLoader(object):
|
||||||
|
|
||||||
self.rebuild()
|
self.rebuild()
|
||||||
|
|
||||||
|
|
||||||
def rebuild(self):
|
def rebuild(self):
|
||||||
"""rebuilds the cloudbot command and event hook lists"""
|
"""rebuilds the cloudbot command and event hook lists"""
|
||||||
self.bot.commands = {}
|
self.bot.commands = {}
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
sqlalchemy
|
||||||
oauth2
|
oauth2
|
||||||
pygeoip
|
pygeoip
|
||||||
tweepy
|
tweepy
|
||||||
|
|
Reference in a new issue