Пример #1
0
def on_user_change_nick(revent):
    '''\
    Dispatches :class:`user nick change events <xbotpp.handler.event.user_change_nick>`.
    '''

    for function in dispatch['user_change_nick']:
        try:
            function(revent)
        except Exception as e:
            debug.exception("Exception in `on_user_change_nick` event handler", e)
Пример #2
0
def on_user_part(revent):
    '''\
    Dispatches :class:`user part events <xbotpp.handler.event.user_part>`.
    '''

    for function in dispatch['user_part']:
        try:
            function(revent)
        except Exception as e:
            debug.exception("Exception in `on_user_part` event handler", e)
Пример #3
0
def on_message(revent):
    '''\
    Dispatches :class:`message events <xbotpp.handler.event.message>`.
    '''

    for function in dispatch['message']:
        try:
            function(revent)
        except Exception as e:
            debug.exception("Exception in `on_message` event handler", e)
Пример #4
0
    def unload(self, name):
        '''\
        Unload the module `name`.

        If the module is not loaded, raises :exc:`error.ModuleNotLoaded`.
        '''

        if name not in self.loaded:
            debug.write("Module being unloaded does not exist ({}), raising exception".format(name))
            raise error.ModuleNotLoaded(name)

        try:
            if '_on_unload' in dir(self.loaded[name]['module']):
                self.loaded[name]['module']._on_unload()
        except Exception as e:
            xbotpp.debug.exception("Running module _on_unload(), continuing anyway", e)

        if name in self.moddata:
            del self.moddata[name]

        try:
            # Remove event handlers
            for sid in self.loaded[name]['events']:
                types = [(i, e) for i, e in enumerate(handler.handlers.dispatch)]
                for e_index, e_type in types:
                    debug.write("Loop: Checking type {}".format(e_type))
                    typed = [(i, e) for i, e in enumerate(handler.handlers.dispatch[e_type])]
                    for i, e in typed:
                        debug.write("Loop: handler {0} in type {1}".format(str(handler.handlers.dispatch[e_type][i]), e_type))
                        if handler.handlers.dispatch[e_type][i] == self.loaded[name]['events'][sid][1]:
                            debug.write("Removing event handler {0} ({1})".format(sid, self.loaded[name]['events'][sid][0]))
                            del handler.handlers.dispatch[e_type][i]

            # Remove command handlers
            commands = [(i, e) for i, e in enumerate(self.commands)]
            for index, command in commands:
                if self.commands[command]['module'] == name:
                    debug.write("Removing command {}".format(command))
                    del self.commands[command]

            del self.loaded[name]

            debug.write("Unloaded {} successfully.".format(name))

        except Exception as e:
            debug.exception("Exception while unloading module '{}'.".format(name), e)
            raise
Пример #5
0
    def load(self, name):
        '''\
        Load the module `name`.

        Raises :exc:`error.ModuleLoadingException` on an error.
        '''

        try:
            # handle reloads right
            u = False
            if name in self.loaded:
                if 'reload' in self.loaded[name]:
                    u = True
                    debug.write("Module loaded, going to unload it first")
            if u:
                self.unload(name)

            module = importlib.import_module(name)
            imp.reload(module)

            if module.__xbotpp_module__:
                if module.__xbotpp_module__ not in self.loaded:
                    # if this module binds events, this will be done already
                    debug.write("Creating module table")
                    self.create_table(module.__xbotpp_module__)

                self.loaded[module.__xbotpp_module__]['module'] = module
                self.moddata[module.__xbotpp_module__] = xbotpp.vendor.sqliteshelf.SQLiteShelf(xbotpp.config['modules']['data_db'], module.__xbotpp_module__)

                if '_on_load' in dir(module):
                    module._on_load()
                debug.write("Loaded {} successfully.".format(module.__xbotpp_module__))
                return True
            else:
                raise error.ModuleLoadingException("Not a module: %s" % name)

        except Exception as e:
            debug.exception('Exception while loading module \'%s\'.' % name, e)
            raise error.ModuleLoadingException(e)
Пример #6
0
    def on_message(self, event):
        '''\
        Gets message events, does the command logic with them.
        '''

        if event.message.startswith(xbotpp.config['bot']['prefix']):            
            message_information = {
                'source': event.source,
                'target': event.source if event.type == 'privmsg' or event.type == 'privnotice' else event.target
            }

            debug.write("message_information: {}".format(repr(message_information)))

            commands = []
            temp = []

            try:
                if len(event.message) > 1 and event.message[1] == xbotpp.config['bot']['prefix']:
                    debug.write("using shlex to split command")
                    splitfunc = shlex.split
                    messagetosplit = event.message[2:]
                else:
                    debug.write("using str.split to split command")
                    splitfunc = lambda x: x.split(" ")
                    messagetosplit = event.message[1:]

                if len(messagetosplit) is 0:
                    debug.write('empty message, returning')
                    return

                debug.write('starting split')
                for i in splitfunc(messagetosplit):
                    debug.write('split: {}'.format(i))
                    if i != "|":
                        debug.write('Appending argument to temp array (currently {})'.format(repr(temp)))
                        temp.append(i)
                    else:
                        debug.write('End of command: {}'.format(repr(temp)))
                        commands.append(temp)
                        temp = []
            except Exception as e:
                debug.exception("Exception while parsing command", e)
                xbotpp.state.connection.send_message(message_information['target'], "Error: {}".format(str(e)))
                return

            debug.write('End of command: {}'.format(repr(temp)))
            commands.append(temp)
            del temp

            debug.write('Command sequence: {}'.format(repr(commands)))
            
            buf = ""

            for br in commands:
                if br[0] in self.commands:
                    debug.write('Command {0} found, privlevel {1}'.format(br[0], self.commands[br[0]]['privlevel']))

                    if self.commands[br[0]]['privlevel'] >= 1:
                        if message_information['source'] != xbotpp.config['bot']['owner']:
                            buf = "{}: Not authorized.".format(br[0])
                            debug.write(buf)
                            break
                    try:
                        message_information['command_name'] = br[0]
                        buf = self.commands[br[0]]['function'](message_information, br[1:], buf)
                    except Exception as e:
                        debug.exception("Exception while handling command {}".format(br[0]), e)
                        buf = "Exception in {0}: {1}".format(br[0], e)
                    debug.write('Buffer: {}'.format(buf))
                else:
                    debug.write('Command {} not found'.format(br[0]))
                    return
            
            try:
                for line in buf.split('\n'):
                    xbotpp.state.connection.send_message(message_information['target'], line)
            except Exception as e:
                debug.exception("Exception in writing buffer to target", e)
                message = "Error [{0}]: {1}".format(e.__class__.__name__, e)
                xbotpp.state.connection.send_message(message_information['target'], message)