Exemple #1
0
    def __init__(self, handler: Callable, about: str, allow_selfact=False):
        self.about = about
        self.handler = handler

        self.usraccess = AccessList(allow_others=False,
                                    allow_selfact=allow_selfact)
        self.chataccess = AccessList(allow_others=True,
                                     allow_selfact=allow_selfact)
Exemple #2
0
 def test_me(self):
     al = AccessList()
     me = IahrConfig.ME
     assert al.is_allowed(me)
     al.ban(me)
     assert al.is_allowed(me)
     al = AccessList(allow_others=True)
     assert al.is_allowed(me)
Exemple #3
0
    def test_operation_when_already_done_for_all(self, allow_others):
        operation = 'allow' if allow_others else 'ban'
        al = AccessList(allow_others)
        lst = al.whitelist if allow_others else al.blacklist

        getattr(al, operation)(self.ENT)
        assert (len(lst) == 0)
Exemple #4
0
async def perm_format(event, ent_perms: dict):
    res = ''
    for ent, perms in ent_perms.items():
        perms = '\n  '.join(perm + (enabled if flag else disabled)
                            for perm, flag in perms.items())
        if not AccessList.is_special(ent):
            ent = await event.client.get_entity(ent)
            ent = ent.username
        res += '**{}**:\n  {}\n'.format(ent, perms)
    return res
Exemple #5
0
async def __process_entities(event, entities: str):
    entities = process_list(str(entities))

    for i, entity in enumerate(entities):
        if not AccessList.is_special(entity):
            if not __is_integer(entity):
                entity = await event.client.get_entity(entity)
                entities[i] = entity.id
            # make sure everything is being stored as an int
            entities[i] = int(entities[i])

    return entities
Exemple #6
0
    def test_others(self, operation):
        should_be = operation == 'allow'
        opposite = 'allow' if operation == 'ban' else 'ban'
        al = AccessList()
        others = IahrConfig.OTHERS

        assert not al.is_allowed(self.ENT)
        getattr(al, operation)(others)
        assert al.is_allowed(self.ENT) == should_be

        getattr(al, opposite)(self.ENT)
        assert not (al.is_allowed(self.ENT) == should_be)
        getattr(al, operation)(others)
        assert al.is_allowed(self.ENT) == should_be
Exemple #7
0
    def __init__(self):
        """
            Load state from file and register dumping to file
            atexit
        """
        # for new message events only(commands)
        self.commands = {}
        # for all other types of events, plain handlers, can't be combined
        self.handlers = {
            prefix: {}
            for prefix in IahrConfig.REVERSE_PREFIXES.keys()
        }
        # tags for quick search
        self.tags = {}
        # for errignore on chat level
        self.chatlist = AccessList(allow_others=False)
        # state for dumping and loading from file
        self.commands_state, self.handlers_state = self.load()

        atexit.register(self.dump)
Exemple #8
0
class Routine:
    """
        Class that contains raw command handler and 
        manages permissions to use it in chats and by users
    """
    def __init__(self, handler: Callable, about: str, allow_selfact=False):
        self.about = about
        self.handler = handler

        self.usraccess = AccessList(allow_others=False,
                                    allow_selfact=allow_selfact)
        self.chataccess = AccessList(allow_others=True,
                                     allow_selfact=allow_selfact)

    def help(self):
        return self.about

    ##################################################
    # Rights to run this handler
    ##################################################

    def allow_usr(self, usr: str):
        self.usraccess.allow(usr)
        print(usr, self.usraccess)

    def ban_usr(self, usr: str):
        self.usraccess.ban(usr)

    def is_allowed_usr(self, usr: str):
        return self.usraccess.is_allowed(usr)

    def allow_chat(self, chat: str):
        self.chataccess.allow(chat)

    def ban_chat(self, chat: str):
        self.chataccess.ban(chat)

    def is_allowed_chat(self, chat: str):
        return self.chataccess.is_allowed(chat)

    def get_handler(self, usr: str, chat: str):
        """
            Try to get handler if allowed
        """
        IahrConfig.LOGGER.debug(f'getting handler:usr={usr}:chat={chat}')

        if not self.is_allowed_chat(chat) or not self.is_allowed_usr(usr):
            if not (self.chataccess.is_self(usr)
                    and self.usraccess.is_self(usr)):
                return

        return self.handler

    ##################################################
    # Session managing
    ##################################################

    def get_state(self):
        return {
            'usraccess': self.usraccess,
            'chataccess': self.chataccess,
        }

    def set_state(self, state):
        self.usraccess = state['usraccess']
        self.chataccess = state['chataccess']

    JSON_ENCODER = AccessList.ALEncoder
    JSON_DECODER = AccessList.ALDecoder

    def __repr__(self):
        return f'Routine(usraccess={self.usraccess}, chataccess={self.chataccess})'
Exemple #9
0
 def test_simple_operation(self, operation, should_be):
     for allow_others in (True, False):
         al = AccessList(allow_others)
         getattr(al, operation)(self.ENT)
         assert al.is_allowed(self.ENT) == should_be