def __init__(self, manager, plugins_dir, dirname): self.plugins_dir = plugins_dir self.dirname = dirname self.manager = manager self.bot = manager.bot try: self.lang = Language(self.bot.settings.get('Bot', 'language')) except NoOptionError: self.lang = Language('english')
class Plugin(object): """ This class represents a (loaded) plugin """ def __init__(self, manager, plugins_dir, dirname): self.plugins_dir = plugins_dir self.dirname = dirname self.manager = manager self.bot = manager.bot try: self.lang = Language(self.bot.settings.get('Bot', 'language')) except NoOptionError: self.lang = Language('english') def load(self): """ (Re)Loads the module instance of this plugin """ if hasattr(self, 'commands'): del self.commands if hasattr(self, 'regexps'): del self.regexps if hasattr(self, 'message_handler'): del self.message_handler self.commands = {} self.regexps = [] self.message_handler = None # read plugin meta data self.plugin_info = self._read_metadata() self.plugin_info['dirname'] = self.dirname self._load_language() if hasattr(self, 'initialize'): self.initialize() def _load_language(self): """ Checks if the plugin has a language file, and reads it, if there's one """ if os.path.exists(os.path.join(self.plugins_dir, self.dirname, 'language.ini')): self.lang.read_language(os.path.join(self.plugins_dir, self.dirname, 'language.ini')) def unload(self): """ Unloads the plugin """ if hasattr(self, 'destroy'): self.destroy() del self.commands del self.regexps del self.message_handler del self.plugin_info def _read_metadata(self): """ Reads some info about the plugin in the plugininfo file @rtype: dict @return: Dictionary containing all keys and values """ if not os.path.exists(os.path.join(self.plugins_dir, self.dirname, 'plugininfo')): raise PluginException, 'Plugin does not have a plugininfo file', self.dirname f = open(os.path.join(self.plugins_dir, self.dirname, 'plugininfo')) infodict = {} for line in f: try: key, val = line.split("=",1) infodict[key.strip().lower()] = eval(val) except ValueError: pass # this happens on blank lines return infodict def register_command(self, name, callback, **keywords): """ Register a new bot command @type name: string @param name: The name of the command @type callback: function @param callback: The function to call when the command is used @type keywords: dictionary @param keywords: List of extra data you want to give with. Special keywords are: help: A short string explaining the command args: List of arguments """ if name in self.commands: return self.commands[name] = (callback, keywords) self.manager.register_command(name, self.dirname) def register_regexp(self, regexp, type, callback, userdata = None): """ Register a new regular expression to match against the incoming data or user message @type regexp: string @param regexp: The regular expression I{object} which will be used for matching @type type: int @param type: Where to match against: The Data (REGEXP_RAW) our the user message (REGEXP_MESSAGE) @type callback: function @param callback: The function to call when the regexp matches @param userdata: Any object you want to give with the callback """ self.regexps.append((regexp, type, callback, userdata)) self.manager.register_regexp(self.dirname) def register_message_handler(self, callback): """ Registers a 'message handler', the given function will be called on EACH server and/or channel message, allowing the plugin to check for JOIN/PART/QUIT and other events. @type callback: function @param callback: The function to call when we receive a server/channel message """ self.message_handler = callback self.manager.register_message_handler(self.dirname) def call(self, name, message): """ Call the the command callback @type name: string @param name: The command name @rtype: bool @return: True when the command exists, else false """ if name in self.commands: callback, keywords = self.commands[name] callback(message, keywords) return True else: return False