コード例 #1
0
ファイル: client.py プロジェクト: ps00k/WeedBot
    def __init__(self, bot, name, nick, *, channels=None, config=None):
        """
        :type bot: cloudbot.bot.CloudBot
        :type name: str
        :type nick: str
        :type channels: list[str]
        :type config: dict[str, unknown]
        """
        self.bot = bot
        self.loop = bot.loop
        self.name = name
        self.nick = nick

        if channels is None:
            self.channels = []
        else:
            self.channels = channels

        if config is None:
            self.config = {}
        else:
            self.config = config
        self.vars = {}
        self.history = {}

        # create permissions manager
        self.permissions = PermissionManager(self)
コード例 #2
0
    def __init__(self, bot, name, nick, *, channels=None, config=None):
        """
        :type bot: cloudbot.bot.CloudBot
        :type name: str
        :type nick: str
        :type channels: list[str]
        :type config: dict[str, unknown]
        """
        self.bot = bot
        self.loop = bot.loop
        self.name = name
        self.nick = nick

        if channels is None:
            self.channels = []
        else:
            self.channels = channels

        if config is None:
            self.config = {}
        else:
            self.config = config
        self.vars = {}
        self.history = {}

        # create permissions manager
        self.permissions = PermissionManager(self)

        # for plugins to abuse
        self.memory = collections.defaultdict()

        # set when on_load in core_misc is done
        self.ready = False

        self._active = False
コード例 #3
0
def test_mix_case_group():
    from cloudbot.permissions import PermissionManager
    manager = PermissionManager(
        MockConn(
            'testconn', {
                'permissions': {
                    'Admins': {
                        'users': ['*!*@host'],
                        'perms': ['testperm']
                    }
                }
            }))

    assert manager.group_exists('admins')
    manager.remove_group_user('admins', 'user!name@host')
    manager.reload()
    assert manager.user_in_group('user!name@host', 'admins')
コード例 #4
0
ファイル: client.py プロジェクト: obrbot/bot-with-history
    def __init__(self, bot, name, bot_nick, *, config):
        """
        :type bot: cloudbot.bot.CloudBot
        :type name: str
        :type bot_nick: str
        :type config: dict[str, unknown]
        """
        self.bot = bot
        self.loop = bot.loop
        self.name = name
        self.bot_nick = bot_nick

        self.channels = CaseInsensitiveDict()

        self.config = config

        # create permissions manager
        self.permissions = PermissionManager(self)

        self.waiting_messages = dict()
コード例 #5
0
ファイル: client.py プロジェクト: TotallyNotRobots/mycroft
class Client:
    """
    A Client representing each connection the bot makes to a single server
    :type bot: cloudbot.bot.CloudBot
    :type loop: asyncio.events.AbstractEventLoop
    :type name: str
    :type channels: list[str]
    :type config: dict[str, unknown]
    :type nick: str
    :type vars: dict
    :type history: dict[str, list[tuple]]
    :type permissions: PermissionManager
    """
    def __init__(self, bot, _type, name, nick, *, channels=None, config=None):
        """
        :type bot: cloudbot.bot.CloudBot
        :type name: str
        :type nick: str
        :type channels: list[str]
        :type config: dict[str, unknown]
        """
        self.bot = bot
        self.loop = bot.loop
        self.name = name
        self.nick = nick
        self._type = _type

        self.channels = []

        if channels is None:
            self.config_channels = []
        else:
            self.config_channels = channels

        if config is None:
            self.config = {}
        else:
            self.config = config
        self.vars = {}
        self.history = {}

        # create permissions manager
        self.permissions = PermissionManager(self)

        # for plugins to abuse
        self.memory = collections.defaultdict()

        # set when on_load in core_misc is done
        self.ready = False

        self._active = False

        self.cancelled_future = async_util.create_future(self.loop)

    def describe_server(self):
        raise NotImplementedError

    async def auto_reconnect(self):
        if not self._active:
            return

        await self.try_connect()

    async def try_connect(self):
        timeout = 30
        while self.active and not self.connected:
            try:
                await self.connect(timeout)
            except Exception:
                logger.exception("[%s] Error occurred while connecting.",
                                 self.name)
            else:
                break

            await asyncio.sleep(random.randrange(timeout))

    async def connect(self, timeout=None):
        """
        Connects to the server, or reconnects if already connected.
        """
        raise NotImplementedError

    def quit(self, reason=None, set_inactive=True):
        """
        Gracefully disconnects from the server with reason <reason>, close() should be called shortly after.
        """
        raise NotImplementedError

    def close(self):
        """
        Disconnects from the server, only for use when this Client object will *not* ever be connected again
        """
        raise NotImplementedError

    def message(self, target, *text):
        """
        Sends a message to the given target
        :type target: str
        :type text: str
        """
        raise NotImplementedError

    def admin_log(self, text, console=True):
        """
        Log a message to the configured admin channel
        :type text: str
        :type console: bool
        """
        raise NotImplementedError

    def action(self, target, text):
        """
        Sends an action (or /me) to the given target channel
        :type target: str
        :type text: str
        """
        raise NotImplementedError

    def notice(self, target, text):
        """
        Sends a notice to the given target
        :type target: str
        :type text: str
        """
        raise NotImplementedError

    def set_nick(self, nick):
        """
        Sets the bot's nickname
        :type nick: str
        """
        raise NotImplementedError

    def join(self, channel, key=None):
        """
        Joins a given channel
        :type channel: str
        :type key: str
        """
        raise NotImplementedError

    def part(self, channel):
        """
        Parts a given channel
        :type channel: str
        """
        raise NotImplementedError

    def is_nick_valid(self, nick):
        """
        Determines if a nick is valid for this connection
        :param nick: The nick to check
        :return: True if it is valid, otherwise false
        """
        raise NotImplementedError

    @property
    def connected(self):
        raise NotImplementedError

    @property
    def type(self):
        return self._type

    @property
    def active(self):
        return self._active

    @active.setter
    def active(self, value):
        self._active = value

    def reload(self):
        self.permissions.reload()
コード例 #6
0
def test_add_user_to_group():
    from cloudbot.permissions import PermissionManager
    manager = PermissionManager(MockConn('testconn', {}))
    manager.add_user_to_group('*!*@host', 'admins')
    manager.add_user_to_group('*!*@mask', 'admins')
    manager.reload()
    assert manager.user_in_group('user!name@host', 'admins')
    assert manager.user_in_group('otheruser!name@mask', 'admins')
    manager.add_user_to_group('*!*@mask', 'admins')
    manager.reload()
    assert len(manager.get_group_users('admins')) == 2
コード例 #7
0
def test_manager_load():
    import cloudbot.permissions
    from cloudbot.permissions import PermissionManager
    manager = PermissionManager(MockConn('testconn', {}))

    assert not manager.group_perms
    assert not manager.group_users
    assert not manager.perm_users
    assert not manager.get_groups()
    assert not manager.get_group_permissions('foobar')
    assert not manager.get_group_users('foobar')
    assert not manager.get_user_permissions('foo!bar@baz')
    assert not manager.get_user_groups('foo!bar@baz')
    assert not manager.group_exists('baz')
    assert not manager.user_in_group('foo!bar@baz', 'bing')

    cloudbot.permissions.backdoor = "*!user@host"

    assert manager.has_perm_mask("test!user@host", 'foo', False)
    assert not manager.has_perm_mask("test!otheruser@host", 'foo', False)

    user = '******'
    user_mask = 'user!*@host??om'

    other_user = '******'

    cloudbot.permissions.backdoor = None
    manager = PermissionManager(
        MockConn(
            'testconn', {
                'permissions': {
                    'admins': {
                        'users': [user_mask],
                        'perms': ['testperm']
                    }
                }
            }))
    assert manager.group_exists('admins')
    assert manager.get_groups() == {'admins'}
    assert manager.get_user_groups(user) == ['admins']
    assert not manager.get_user_groups(other_user)
    assert manager.get_group_users('admins') == [user_mask]
    assert manager.get_group_permissions('admins') == ['testperm']
    assert manager.get_user_permissions(user) == {'testperm'}
    assert not manager.get_user_permissions(other_user)
    assert manager.has_perm_mask(user, 'testperm')
    assert manager.user_in_group(user, 'admins')
    assert not manager.user_in_group(other_user, 'admins')

    assert manager.remove_group_user('admins', user) == [user_mask]
    manager.reload()

    assert 'admins' not in manager.get_user_groups(user)
    assert user_mask not in manager.get_group_users('admins')
    assert 'testperm' not in manager.get_user_permissions(user)
    assert not manager.has_perm_mask(user, 'testperm')
    assert not manager.user_in_group(user, 'admins')