dccd
This commit is contained in:
parent
8e4ce31c0e
commit
d863628139
5 changed files with 41 additions and 50 deletions
|
@ -32,8 +32,8 @@ 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)
|
||||||
|
|
||||||
# create a bot thread and start it
|
# create a bot master and start it
|
||||||
cloudbot = bot.Bot()
|
cloudbot = bot.CloudBot()
|
||||||
cloudbot.start()
|
cloudbot.start()
|
||||||
|
|
||||||
# watch to see if the bot stops running or needs a restart
|
# watch to see if the bot stops running or needs a restart
|
||||||
|
|
51
core/bot.py
51
core/bot.py
|
@ -46,30 +46,30 @@ def get_logger():
|
||||||
return logger
|
return logger
|
||||||
|
|
||||||
|
|
||||||
class Bot(threading.Thread):
|
class CloudBot(threading.Thread):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
# basic variables
|
# basic variables
|
||||||
self.start_time = time.time()
|
self.start_time = time.time()
|
||||||
self.running = True
|
self.running = True
|
||||||
self.do_restart = False
|
self.do_restart = False
|
||||||
self.connections = []
|
|
||||||
|
# stores each instance of the
|
||||||
|
self.instances = []
|
||||||
|
|
||||||
# set up config and logging
|
# set up config and logging
|
||||||
self.setup()
|
self.setup()
|
||||||
self.logger.debug("Bot setup completed.")
|
self.logger.debug("Bot setup completed.")
|
||||||
|
|
||||||
# start IRC connections
|
# start bot instances
|
||||||
self.connect()
|
self.create()
|
||||||
print(self.connections)
|
|
||||||
|
|
||||||
for conn in self.connections:
|
for instance in self.instances:
|
||||||
conn.permissions = PermissionManager(self, conn)
|
instance.permissions = PermissionManager(self, instance)
|
||||||
print(conn)
|
|
||||||
|
|
||||||
# run plugin loader
|
# run plugin loader
|
||||||
self.plugins = collections.defaultdict(list)
|
self.plugins = collections.defaultdict(list)
|
||||||
|
|
||||||
""" plugins format
|
""" self.plugins format
|
||||||
{'PLUGIN_TYPE': [(<COMPILED_PLUGIN_FUNTION>,
|
{'PLUGIN_TYPE': [(<COMPILED_PLUGIN_FUNTION>,
|
||||||
{PLUGIN_ARGS}),
|
{PLUGIN_ARGS}),
|
||||||
(<COMPILED_PLUGIN_FUNTION>,
|
(<COMPILED_PLUGIN_FUNTION>,
|
||||||
|
@ -89,19 +89,19 @@ class Bot(threading.Thread):
|
||||||
"""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 self.running:
|
while self.running:
|
||||||
for conn in self.connections:
|
for instance in self.instances:
|
||||||
try:
|
try:
|
||||||
incoming = conn.parsed_queue.get_nowait()
|
incoming = instance.parsed_queue.get_nowait()
|
||||||
if incoming == StopIteration:
|
if incoming == StopIteration:
|
||||||
print("StopIteration")
|
print("StopIteration")
|
||||||
# IRC engine has signalled timeout, so reconnect (ugly)
|
# IRC engine has signalled timeout, so reconnect (ugly)
|
||||||
conn.connection.reconnect()
|
instance.connection.reconnect()
|
||||||
main.main(self, conn, incoming)
|
main.main(self, instance, incoming)
|
||||||
except queue.Empty:
|
except queue.Empty:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# if no messages are in the incoming queue, sleep
|
# if no messages are in the incoming queue, sleep
|
||||||
while self.running and all(c.parsed_queue.empty() for c in self.connections):
|
while self.running and all(i.parsed_queue.empty() for i in self.instances):
|
||||||
time.sleep(.1)
|
time.sleep(.1)
|
||||||
|
|
||||||
def setup(self):
|
def setup(self):
|
||||||
|
@ -126,26 +126,23 @@ 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 create(self):
|
||||||
"""connect to all the networks defined in the bot config"""
|
""" Create a BotInstance for all the networks defined in the config """
|
||||||
for conf in self.config['connections']:
|
for conf in self.config['instances']:
|
||||||
|
|
||||||
# strip all spaces and capitalization from the connection name
|
# strip all spaces and capitalization from the connection name
|
||||||
name = clean_name(conf['name'])
|
name = clean_name(conf['name'])
|
||||||
nick = conf['nick']
|
nick = conf['nick']
|
||||||
server = conf['connection']['server']
|
server = conf['connection']['server']
|
||||||
port = conf['connection'].get('port', 6667)
|
port = conf['connection'].get('port', 6667)
|
||||||
|
|
||||||
self.logger.debug("({}) Creating connection to {}.".format(name, server))
|
self.logger.debug("Creating BotInstance for {}.".format(name))
|
||||||
|
|
||||||
|
self.instances.append(irc.BotInstance(name, server, nick, config=conf,
|
||||||
|
port=port, logger=self.logger, channels=conf['channels'],
|
||||||
|
ssl=conf['connection'].get('ssl', False)))
|
||||||
|
self.logger.debug("({}) Created connection.".format(name))
|
||||||
|
|
||||||
if conf['connection'].get('ssl'):
|
|
||||||
self.connections.append(irc.SSLIRC(name, server, nick, config=conf,
|
|
||||||
port=port, logger=self.logger, channels=conf['channels'],
|
|
||||||
ignore_certificate_errors=conf['connection'].get('ignore_cert', True)))
|
|
||||||
self.logger.debug("({}) Created SSL connection.".format(name))
|
|
||||||
else:
|
|
||||||
self.connections.append(irc.IRC(name, server, nick, config=conf,
|
|
||||||
port=port, logger=self.logger, channels=conf['channels']))
|
|
||||||
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"""
|
||||||
|
|
|
@ -38,9 +38,9 @@ class Config(dict):
|
||||||
self.logger.info("Config loaded from file.")
|
self.logger.info("Config loaded from file.")
|
||||||
|
|
||||||
# reload permissions
|
# reload permissions
|
||||||
if self.bot.connections:
|
if self.bot.instances:
|
||||||
for conn in self.bot.connections:
|
for instance in self.bot.instances:
|
||||||
conn.permissions.reload()
|
instance.permissions.reload()
|
||||||
|
|
||||||
def save_config(self):
|
def save_config(self):
|
||||||
"""saves the contents of the config dict to the config file"""
|
"""saves the contents of the config dict to the config file"""
|
||||||
|
|
28
core/irc.py
28
core/irc.py
|
@ -202,13 +202,14 @@ class SSLIRCConnection(IRCConnection):
|
||||||
CERT_REQUIRED)
|
CERT_REQUIRED)
|
||||||
|
|
||||||
|
|
||||||
class IRC(object):
|
class BotInstance(object):
|
||||||
"""handles the IRC protocol"""
|
""" A BotInstance represents each connection the bot makes to an IRC server """
|
||||||
|
|
||||||
def __init__(self, name, server, nick, port=6667, logger=None, channels=[], config={}):
|
def __init__(self, name, server, nick, port=6667, ssl=False, logger=None, channels=[], config={}):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.channels = channels
|
self.channels = channels
|
||||||
self.config = config
|
self.config = config
|
||||||
|
self.ssl = ssl
|
||||||
self.server = server
|
self.server = server
|
||||||
self.port = port
|
self.port = port
|
||||||
self.logger = logger
|
self.logger = logger
|
||||||
|
@ -240,8 +241,12 @@ class IRC(object):
|
||||||
self.parse_thread.start()
|
self.parse_thread.start()
|
||||||
|
|
||||||
def create_connection(self):
|
def create_connection(self):
|
||||||
return IRCConnection(self.name, self.server, self.port,
|
if self.ssl:
|
||||||
self.input_queue, self.output_queue)
|
return SSLIRCConnection(self.name, self.server, self.port, self.input_queue,
|
||||||
|
self.output_queue, True)
|
||||||
|
else:
|
||||||
|
return IRCConnection(self.name, self.server, self.port,
|
||||||
|
self.input_queue, self.output_queue)
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
self.connection.stop()
|
self.connection.stop()
|
||||||
|
@ -287,15 +292,4 @@ class IRC(object):
|
||||||
except:
|
except:
|
||||||
# if this doesn't work, no big deal
|
# if this doesn't work, no big deal
|
||||||
pass
|
pass
|
||||||
self.output_queue.put(string)
|
self.output_queue.put(string)
|
||||||
|
|
||||||
|
|
||||||
class SSLIRC(IRC):
|
|
||||||
def __init__(self, name, server, nick, port=6667, logger=None, channels=[], config={},
|
|
||||||
ignore_certificate_errors=True):
|
|
||||||
self.ignore_cert_errors = ignore_certificate_errors
|
|
||||||
IRC.__init__(self, name, server, nick, port, logger, channels, config)
|
|
||||||
|
|
||||||
def create_connection(self):
|
|
||||||
return SSLIRCConnection(self.name, self.server, self.port, self.input_queue,
|
|
||||||
self.output_queue, self.ignore_cert_errors)
|
|
|
@ -14,7 +14,7 @@ def get_generator(_json):
|
||||||
|
|
||||||
|
|
||||||
@hook.command(autohelp=False)
|
@hook.command(autohelp=False)
|
||||||
def namegen(input, conn):
|
def namegen(input, instance, bot):
|
||||||
"""namegen [generator] -- Generates some names using the chosen generator.
|
"""namegen [generator] -- Generates some names using the chosen generator.
|
||||||
'namegen list' will display a list of all generators."""
|
'namegen list' will display a list of all generators."""
|
||||||
|
|
||||||
|
|
Reference in a new issue