diff --git a/cloudbot.py b/cloudbot.py index 50ed779..f567315 100755 --- a/cloudbot.py +++ b/cloudbot.py @@ -32,8 +32,8 @@ def exit_gracefully(signum, frame): original_sigint = signal.getsignal(signal.SIGINT) signal.signal(signal.SIGINT, exit_gracefully) -# create a bot thread and start it -cloudbot = bot.Bot() +# create a bot master and start it +cloudbot = bot.CloudBot() cloudbot.start() # watch to see if the bot stops running or needs a restart diff --git a/core/bot.py b/core/bot.py index 5730efc..95d0dfe 100644 --- a/core/bot.py +++ b/core/bot.py @@ -46,30 +46,30 @@ def get_logger(): return logger -class Bot(threading.Thread): +class CloudBot(threading.Thread): def __init__(self): # basic variables self.start_time = time.time() self.running = True self.do_restart = False - self.connections = [] + + # stores each instance of the + self.instances = [] # set up config and logging self.setup() self.logger.debug("Bot setup completed.") - # start IRC connections - self.connect() - print(self.connections) + # start bot instances + self.create() - for conn in self.connections: - conn.permissions = PermissionManager(self, conn) - print(conn) + for instance in self.instances: + instance.permissions = PermissionManager(self, instance) # run plugin loader self.plugins = collections.defaultdict(list) - """ plugins format + """ self.plugins format {'PLUGIN_TYPE': [(, {PLUGIN_ARGS}), (, @@ -89,19 +89,19 @@ class Bot(threading.Thread): """recieves input from the IRC engine and processes it""" self.logger.info("Starting main thread.") while self.running: - for conn in self.connections: + for instance in self.instances: try: - incoming = conn.parsed_queue.get_nowait() + incoming = instance.parsed_queue.get_nowait() if incoming == StopIteration: print("StopIteration") # IRC engine has signalled timeout, so reconnect (ugly) - conn.connection.reconnect() - main.main(self, conn, incoming) + instance.connection.reconnect() + main.main(self, instance, incoming) except queue.Empty: pass # 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) def setup(self): @@ -126,26 +126,23 @@ class Bot(threading.Thread): self.db_session = scoped_session(db_factory) self.logger.debug("Database system initalised.") - def connect(self): - """connect to all the networks defined in the bot config""" - for conf in self.config['connections']: + def create(self): + """ Create a BotInstance for all the networks defined in the config """ + for conf in self.config['instances']: + # strip all spaces and capitalization from the connection name name = clean_name(conf['name']) nick = conf['nick'] server = conf['connection']['server'] 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): """quits all networks and shuts the bot down""" diff --git a/core/config.py b/core/config.py index d4ad6a0..9d8d010 100644 --- a/core/config.py +++ b/core/config.py @@ -38,9 +38,9 @@ class Config(dict): self.logger.info("Config loaded from file.") # reload permissions - if self.bot.connections: - for conn in self.bot.connections: - conn.permissions.reload() + if self.bot.instances: + for instance in self.bot.instances: + instance.permissions.reload() def save_config(self): """saves the contents of the config dict to the config file""" diff --git a/core/irc.py b/core/irc.py index 32044dc..88357d5 100644 --- a/core/irc.py +++ b/core/irc.py @@ -202,13 +202,14 @@ class SSLIRCConnection(IRCConnection): CERT_REQUIRED) -class IRC(object): - """handles the IRC protocol""" +class BotInstance(object): + """ 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.channels = channels self.config = config + self.ssl = ssl self.server = server self.port = port self.logger = logger @@ -240,8 +241,12 @@ class IRC(object): self.parse_thread.start() def create_connection(self): - return IRCConnection(self.name, self.server, self.port, - self.input_queue, self.output_queue) + if self.ssl: + 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): self.connection.stop() @@ -287,15 +292,4 @@ class IRC(object): except: # if this doesn't work, no big deal pass - 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) + self.output_queue.put(string) \ No newline at end of file diff --git a/plugins/namegen.py b/plugins/namegen.py index 261acc5..5f5a169 100644 --- a/plugins/namegen.py +++ b/plugins/namegen.py @@ -14,7 +14,7 @@ def get_generator(_json): @hook.command(autohelp=False) -def namegen(input, conn): +def namegen(input, instance, bot): """namegen [generator] -- Generates some names using the chosen generator. 'namegen list' will display a list of all generators."""