Exemplo n.º 1
0
        # The database setup commands
        if sys.argv[1] == 'setupdb':

            logger.debug('Preparing to do database setup')
            DB.setup()
            logger.info('Database succesfully setup')
            print ' * Setup of database `%s` complete' % db.database

            sys.exit(0)

        # The start of hogar as a daemon. Debug is used
        # to keep the controlling terminal attached
        elif sys.argv[1] == 'start' or sys.argv[1] == 'debug':

            qprint(' * Loading plugins...')
            command_map = PluginLoader.prepare_plugins()

            if not command_map:
                qprint(' * No plugins found. Aborting.')
                sys.exit(1)

            qprint(' * Loaded plugins for {number} message types: {commands}'.
                   format(number=len(command_map.keys()),
                          commands='; '.join(command_map.keys())))

            # Pass the checked command map to the instance
            # of Hogar
            app.set_command_map(command_map)

            # Decide if we should daemonize or stay attached
            if sys.argv[1] == 'start':
Exemplo n.º 2
0
    def run_plugins(self):
        '''
            Run Plugins

            This is the main function responsible for executing
            the plugins that have been identified for this
            message.

            --
            @return None
        '''

        if not self.plugins:
            logger.warning('No plugins matched for this message.')
            return

        # Check with the ACL system what the status is of
        # the user that has sent the message
        can_send = self.check_acl()

        # Load the plugins from the configuration that
        # should not have ACL rules applied to them
        config.read(os.path.join(os.path.dirname(__file__), '../settings.ini'))
        acl_free_plugins = [x.strip() \
                            for x in config.get('advanced', 'no_acl_plugins', '').split(',')]

        for plugin in self.plugins:

            # Check that we are allowed to run this plugin. It
            # should either be bypassed from the ACL system
            # using settings.ini, or the user is allowed
            # to run plugins
            if not plugin['name'] in acl_free_plugins and not can_send:
                continue

            # Getting here, we should run this plugin.
            # Do it!
            try:

                logger.info(
                    'Running plugin: {plugin} for message {message_id}'.format(
                        plugin=plugin['name'],
                        message_id=self.response['message_id']))

                logger.debug(
                    'Loading plugin: {plugin}'.format(plugin=plugin['name']))

                # Find and Load the plugin from the file
                plugin_on_disk = PluginLoader.find_plugin(plugin['name'])
                loaded_plugin = PluginLoader.load_plugin(plugin_on_disk)

                # If we got None from the load, error out
                if not loaded_plugin:
                    logger.critical(
                        'Loading plugin {name} returned nothing.'.format(
                            name=plugin['name']))

                    continue

                # Run the plugins run() method
                plugin_output = loaded_plugin.run(self.response)

            except Exception, e:

                logger.error(
                    'Plugin {plugin} failed with: {error}: {trace}'.format(
                        plugin=plugin['name'],
                        error=str(e),
                        trace=traceback.print_exc()))

                continue

            # If we should be replying to the message,
            # do it.
            if loaded_plugin.should_reply():

                # Check what the reply type should be. Plugins
                # that don't specify one will default to text
                reply_type = 'text'
                if hasattr(loaded_plugin, 'reply_type'):
                    reply_type = loaded_plugin.reply_type()

                Telegram.send_message(self.sender_information, reply_type,
                                      plugin_output)

            # GC the loaded_plugin
            loaded_plugin = None
Exemplo n.º 3
0
    def run_plugins (self):

        '''
            Run Plugins

            This is the main function responsible for executing
            the plugins that have been identified for this
            message.

            --
            @return None
        '''

        if not self.plugins:
            logger.warning('No plugins matched for this message.')
            return

        # Check with the ACL system what the status is of
        # the user that has sent the message
        can_send = self.check_acl()

        # Load the plugins from the configuration that
        # should not have ACL rules applied to them
        config.read(
            os.path.join(os.path.dirname(__file__), '../settings.ini'))
        acl_free_plugins = [x.strip() \
                            for x in config.get('advanced', 'no_acl_plugins', '').split(',')]

        for plugin in self.plugins:

            # Check that we are allowed to run this plugin. It
            # should either be bypassed from the ACL system
            # using settings.ini, or the user is allowed
            # to run plugins
            if not plugin['name'] in acl_free_plugins and not can_send:
                continue

            # Getting here, we should run this plugin.
            # Do it!
            try:

                logger.info('Running plugin: {plugin} for message {message_id}'.format(
                    plugin = plugin['name'],
                    message_id = self.response['message_id']))

                logger.debug('Loading plugin: {plugin}'.format(
                    plugin = plugin['name']))

                # Find and Load the plugin from the file
                plugin_on_disk = PluginLoader.find_plugin(plugin['name'])
                loaded_plugin = PluginLoader.load_plugin(plugin_on_disk)

                # If we got None from the load, error out
                if not loaded_plugin:
                    logger.critical('Loading plugin {name} returned nothing.'.format(
                        name = plugin['name']))

                    continue

                # Run the plugins run() method
                plugin_output = loaded_plugin.run(self.response)

            except Exception, e:

                logger.error('Plugin {plugin} failed with: {error}: {trace}'.format(
                    plugin = plugin['name'],
                    error = str(e),
                    trace = traceback.print_exc()))

                continue

            # If we should be replying to the message,
            # do it.
            if loaded_plugin.should_reply():

                # Check what the reply type should be. Plugins
                # that don't specify one will default to text
                reply_type = 'text'
                if hasattr(loaded_plugin, 'reply_type'):
                    reply_type = loaded_plugin.reply_type()

                Telegram.send_message(self.sender_information, reply_type, plugin_output)

            # GC the loaded_plugin
            loaded_plugin = None
Exemplo n.º 4
0
    banner()

    # prepare a db setup command
    if len(sys.argv) > 1 :
        if sys.argv[1] == 'setupdb':

            logger.debug('Preparing to do database setup')
            DB.setup()
            logger.info('Database succesfully setup')
            print ' * Setup of database `%s` complete' % db.database
            sys.exit(0)

        else:
            print ' * Supported arguments are: setupdb'
            sys.exit(0)

    print ' * Loading plugins...'
    command_map = PluginLoader.prepare_plugins()

    if not command_map:
        print ' * No plugins found. Aborting.'
        sys.exit(1)

    print ' * Loaded plugins for {number} message types: {commands}'.format(
        number = len(command_map.keys()),
        commands = '; '.join(command_map.keys())
    )

    main()