Esempio n. 1
0
    def plugin_config(self, mess, args):
        """ configure or get the configuration / configuration template for a specific plugin
        ie.
        !plugin config ExampleBot
        could return a template if it is not configured:
        {'LOGIN': '******', 'PASSWORD': '******', 'DIRECTORY': '/toto'}
        Copy paste, adapt so can configure the plugin :
        !plugin config ExampleBot {'LOGIN': '******', 'PASSWORD': '******', 'DIRECTORY': '/tmp'}
        It will then reload the plugin with this config.
        You can at any moment retreive the current values:
        !plugin config ExampleBot
        should return :
        {'LOGIN': '******', 'PASSWORD': '******', 'DIRECTORY': '/tmp'}
        """
        plugin_name = args[0]
        if self._bot.is_plugin_blacklisted(plugin_name):
            return 'Load this plugin first with ' + self._bot.prefix + 'load %s' % plugin_name
        obj = self._bot.get_plugin_obj_by_name(plugin_name)
        if obj is None:
            return 'Unknown plugin or the plugin could not load %s' % plugin_name
        template_obj = obj.get_configuration_template()
        if template_obj is None:
            return 'This plugin is not configurable.'

        if len(args) == 1:
            response = ("Default configuration for this plugin (you can copy and paste "
                        "this directly as a command):\n{prefix}plugin config {plugin_name} \n{config}").format(
                prefix=self._bot.prefix, plugin_name=plugin_name, config=md_escape(pformat(template_obj)))

            current_config = self._bot.get_plugin_configuration(plugin_name)
            if current_config:
                response += "\n\nCurrent configuration:\n{prefix}plugin config {plugin_name} \n{config}".format(
                    prefix=self._bot.prefix, plugin_name=plugin_name, config=md_escape(pformat(current_config)))
            return response

        # noinspection PyBroadException
        try:
            real_config_obj = literal_eval(' '.join(args[1:]))
        except Exception:
            self.log.exception('Invalid expression for the configuration of the plugin')
            return 'Syntax error in the given configuration'
        if type(real_config_obj) != type(template_obj):
            return 'It looks fishy, your config type is not the same as the template !'

        self._bot.set_plugin_configuration(plugin_name, real_config_obj)
        self._bot.deactivate_plugin(plugin_name)
        try:
            self._bot.activate_plugin(plugin_name)
        except PluginConfigurationException as ce:
            self.log.debug('Invalid configuration for the plugin, reverting the plugin to unconfigured')
            self._bot.set_plugin_configuration(plugin_name, None)
            return 'Incorrect plugin configuration: %s' % ce
        return 'Plugin configuration done.'
Esempio n. 2
0
    def repohook_route(self, message, args):
        """Map a repository to a chatroom, essentially creating a route.

        This takes two or three arguments: author/repo, a chatroom and
        optionally a list of events.

        If you do not specify a list of events the route will default to
        receiving the events configured as 'default_events'.
        """
        if len(args) >= 2:
            repo = args[0]
            room = args[1]
            # Slicing on an index that, potentially, doesn't exist returns
            # an empty list instead of raising an IndexError
            events = args[2:]

            if not self.has_route(repo, room):
                self.set_route(repo, room)

            if events:
                for event in events[:]:
                    if event not in SUPPORTED_EVENTS:
                        events.remove(event)
                        yield EVENT_UNKNOWN.format(event)
            else:
                events = self.get_defaults()
            self.set_events(repo, room, events)
            yield ('Done. Relaying messages from `{0}` to `{1}` for '
                   'events: {2}'.format(repo, room, md_escape(' '.join(events))))
            if self.get_token(repo) is None:
                yield ("Don't forget to set the token for `{0}`. Instructions "
                       "on how to do so and why can be found "
                       "at: {1}.".format(repo, README))
        else:
            yield HELP_MSG
Esempio n. 3
0
 def show_repo_config(self, repo):
     """Builds up a complete list of rooms and events for a repository."""
     if self.has_repo(repo):
         message = ['Routing `{0}` to:'.format(repo)]
         for room in self.get_routes(repo):
             message.append(' • `{0}` for events: {1}'.format(
                 room, md_escape(' '.join(self.get_events(repo, room)))))
         return '\n'.join(message)
     else:
         return REPO_UNKNOWN.format(repo)
Esempio n. 4
0
 def repohook_defaults(self, message, args):
     """Get or set what events are relayed by default for new routes."""
     if args:
         events = []
         for event in args:
             if event in SUPPORTED_EVENTS:
                 events.append(event)
             else:
                 yield EVENT_UNKNOWN.format(event)
         self.set_defaults(events)
         yield ('Done. Newly created routes will default to '
                'receiving: {0}.'.format(' '.join(events)))
     else:
         yield ('Events routed by default: '
                '{0}.'.format(md_escape(' '.join(self.get_defaults()))))
Esempio n. 5
0
 def repohook_help(self, *args):
     """Output help."""
     message = []
     message.append('This plugin has multiple commands: ')
     message.append(' • config: to display the full configuration of '
                    'this plugin (not human friendly)')
     message.append(' • route `<repo> <room>`: to relay messages from '
                    '`<repo>` to `<room>` for events '
                    '{0}'.format(md_escape(' '.join(self.get_defaults()))))
     message.append(' • route `<repo> <room> <events>`: to relay '
                    'messages from `<repo>` to `<room>` for `<events>`')
     message.append(' • routes `<repo>`: show routes for this repository')
     message.append(' • routes: to display all routes')
     message.append(' • defaults `<events>`: to configure the events we '
                    'should forward by default')
     message.append(' • defaults: to show the events to be forwarded '
                    'by default')
     message.append(' • token `<repo>`: to configure the repository '
                    'secret')
     message.append('Please see {0} for more information.'.format(README))
     return '\n'.join(message)
Esempio n. 6
0
 def test_escaping(self):
     mdc = text()
     original = '#not a title\n*not italic*\n`not code`\ntoto{not annotation}'
     escaped = md_escape(original)
     self.assertEquals(original, mdc.convert(escaped))
Esempio n. 7
0
def test_escaping():
    mdc = rendering.text()
    original = "#not a title\n*not italic*\n`not code`\ntoto{not annotation}"
    escaped = rendering.md_escape(original)
    assert original == mdc.convert(escaped)
Esempio n. 8
0
    def plugin_config(self, mess, args):
        """ configure or get the configuration / configuration template for a specific plugin
        ie.
        !plugin config ExampleBot
        could return a template if it is not configured:
        {'LOGIN': '******', 'PASSWORD': '******', 'DIRECTORY': '/toto'}
        Copy paste, adapt so can configure the plugin :
        !plugin config ExampleBot {'LOGIN': '******', 'PASSWORD': '******', 'DIRECTORY': '/tmp'}
        It will then reload the plugin with this config.
        You can at any moment retreive the current values:
        !plugin config ExampleBot
        should return :
        {'LOGIN': '******', 'PASSWORD': '******', 'DIRECTORY': '/tmp'}
        """
        plugin_name = args[0]
        if self._bot.is_plugin_blacklisted(plugin_name):
            return 'Load this plugin first with ' + self._bot.prefix + 'load %s' % plugin_name
        obj = self._bot.get_plugin_obj_by_name(plugin_name)
        if obj is None:
            return 'Unknown plugin or the plugin could not load %s' % plugin_name
        template_obj = obj.get_configuration_template()
        if template_obj is None:
            return 'This plugin is not configurable.'

        if len(args) == 1:
            response = (
                "Default configuration for this plugin (you can copy and paste "
                "this directly as a command):\n{prefix}plugin config {plugin_name} \n{config}"
            ).format(prefix=self._bot.prefix,
                     plugin_name=plugin_name,
                     config=md_escape(pformat(template_obj)))

            current_config = self._bot.get_plugin_configuration(plugin_name)
            if current_config:
                response += "\n\nCurrent configuration:\n{prefix}plugin config {plugin_name} \n{config}".format(
                    prefix=self._bot.prefix,
                    plugin_name=plugin_name,
                    config=md_escape(pformat(current_config)))
            return response

        # noinspection PyBroadException
        try:
            real_config_obj = literal_eval(' '.join(args[1:]))
        except Exception:
            self.log.exception(
                'Invalid expression for the configuration of the plugin')
            return 'Syntax error in the given configuration'
        if type(real_config_obj) != type(template_obj):
            return 'It looks fishy, your config type is not the same as the template !'

        self._bot.set_plugin_configuration(plugin_name, real_config_obj)
        self._bot.deactivate_plugin(plugin_name)
        try:
            self._bot.activate_plugin(plugin_name)
        except PluginConfigurationException as ce:
            self.log.debug(
                'Invalid configuration for the plugin, reverting the plugin to unconfigured'
            )
            self._bot.set_plugin_configuration(plugin_name, None)
            return 'Incorrect plugin configuration: %s' % ce
        return 'Plugin configuration done.'
Esempio n. 9
0
def test_escaping():
    mdc = rendering.text()
    original = '#not a title\n*not italic*\n`not code`\ntoto{not annotation}'
    escaped = rendering.md_escape(original)
    assert original == mdc.convert(escaped)