From 45cef19eaa99e0a6c18bed09fbaf32b74247b6f1 Mon Sep 17 00:00:00 2001 From: James Lu Date: Sun, 12 Jul 2015 23:28:34 -0700 Subject: [PATCH] Make plugins global, not per IRC instance --- main.py | 51 ++++++++++++++++++++----------------------- protocols/inspircd.py | 6 ++++- utils.py | 1 + 3 files changed, 30 insertions(+), 28 deletions(-) diff --git a/main.py b/main.py index 0ddfedc..87efa26 100755 --- a/main.py +++ b/main.py @@ -55,7 +55,6 @@ class Irc(): self.socket.settimeout(180) self.socket.connect((ip, port)) self.proto.connect(self) - self.loaded = [] reading_thread = threading.Thread(target = self.run) self.connected = True reading_thread.start() @@ -90,33 +89,34 @@ class Irc(): log.debug("(%s) -> %s", self.name, data.decode("utf-8").strip("\n")) self.socket.send(data) - def load_plugins(self): - to_load = conf.conf['plugins'] - plugins_folder = [os.path.join(os.getcwd(), 'plugins')] - # Here, we override the module lookup and import the plugins - # dynamically depending on which were configured. - for plugin in to_load: - try: - moduleinfo = imp.find_module(plugin, plugins_folder) - pl = imp.load_source(plugin, moduleinfo[1]) - self.loaded.append(pl) - except ImportError as e: - if str(e).startswith('No module named'): - log.error('Failed to load plugin %r: the plugin could not be found.', plugin) - else: - log.error('Failed to load plugin %r: import error %s', plugin, str(e)) - else: - if hasattr(pl, 'main'): - log.debug('Calling main() function of plugin %r', pl) - pl.main(irc) - log.info("loaded plugins: %s", self.loaded) - if __name__ == '__main__': log.info('PyLink starting...') if conf.conf['login']['password'] == 'changeme': log.critical("You have not set the login details correctly! Exiting...") sys.exit(2) protocols_folder = [os.path.join(os.getcwd(), 'protocols')] + + # Import plugins first globally, because they can listen for events + # that happen before the connection phase. + to_load = conf.conf['plugins'] + plugins_folder = [os.path.join(os.getcwd(), 'plugins')] + # Here, we override the module lookup and import the plugins + # dynamically depending on which were configured. + for plugin in to_load: + try: + moduleinfo = imp.find_module(plugin, plugins_folder) + pl = imp.load_source(plugin, moduleinfo[1]) + utils.plugins.append(pl) + except ImportError as e: + if str(e).startswith('No module named'): + log.error('Failed to load plugin %r: the plugin could not be found.', plugin) + else: + log.error('Failed to load plugin %r: import error %s', plugin, str(e)) + else: + if hasattr(pl, 'main'): + log.debug('Calling main() function of plugin %r', pl) + pl.main() + for network in conf.conf['servers']: protoname = conf.conf['servers'][network]['protocol'] try: @@ -130,8 +130,5 @@ if __name__ == '__main__': sys.exit(2) else: utils.networkobjects[network] = Irc(network, proto, conf.conf) - # This is a separate loop to make sure that ALL networks have their - # Irc objects added into utils.networkobjects, before we load any plugins - # that may require them. - for irc in utils.networkobjects.values(): - irc.load_plugins() + log.info("loaded plugins: %s", utils.plugins) + diff --git a/protocols/inspircd.py b/protocols/inspircd.py index 2b950dd..43cb7c7 100644 --- a/protocols/inspircd.py +++ b/protocols/inspircd.py @@ -54,6 +54,7 @@ def joinClient(irc, client, channel): channel = channel.lower() server = utils.isInternalClient(irc, client) if not server: + log.error('(%s) Error trying to join client %r to %r (no such pseudoclient exists)', irc.name, client, channel) raise LookupError('No such PyLink PseudoClient exists.') # One channel per line here! _sendFromServer(irc, server, "FJOIN {channel} {ts} {modes} :,{uid}".format( @@ -526,7 +527,7 @@ def handle_events(irc, data): # to plugins and the like. For example, the JOIN handler will return # something like: {'channel': '#whatever', 'users': ['UID1', 'UID2', # 'UID3']}, etc. - if parsed_args: + if parsed_args is not None: # Always make sure TS is sent. if 'ts' not in parsed_args: parsed_args['ts'] = int(time.time()) @@ -636,3 +637,6 @@ def handle_fhost(irc, numeric, command, args): def handle_fname(irc, numeric, command, args): irc.users[numeric].realname = newgecos = args[0] return {'target': numeric, 'newgecos': newgecos} + +def handle_endburst(irc, numeric, command, args): + return {} diff --git a/utils.py b/utils.py index 0fb2509..4b493cc 100644 --- a/utils.py +++ b/utils.py @@ -10,6 +10,7 @@ bot_commands = {} command_hooks = defaultdict(list) networkobjects = {} schedulers = {} +plugins = [] class TS6UIDGenerator(): """TS6 UID Generator module, adapted from InspIRCd source