From 0d5ea045fd84f1cd89a674c4e416448b67e0d9cb Mon Sep 17 00:00:00 2001 From: "hikari.ftg" Date: Thu, 31 Mar 2022 11:33:21 +0000 Subject: [PATCH] Actual 1.0.12 with changes --- hikka/inline/core.py | 6 ++-- hikka/inline/events.py | 25 ++++++++++---- hikka/inline/gallery.py | 74 ++++++++++++++--------------------------- hikka/security.py | 12 ++----- 4 files changed, 49 insertions(+), 68 deletions(-) diff --git a/hikka/inline/core.py b/hikka/inline/core.py index 646bece..ed1c06b 100644 --- a/hikka/inline/core.py +++ b/hikka/inline/core.py @@ -25,7 +25,7 @@ from .bot_interaction import BotInteractions from .events import Events from .token_obtainment import TokenObtainment -from typing import Union +from typing import Union, Callable import inspect from .. import security @@ -119,11 +119,11 @@ class InlineManager(Gallery, Form, BotInteractions, Events, TokenObtainment): await asyncio.sleep(5) - def _find_caller_sec_map(self) -> Union[int, None]: + def _find_caller_sec_map(self) -> Union[Callable, None]: try: return next( next( - self._db.get(security.__name__, "masks", {}).get( + lambda: self._db.get(security.__name__, "masks", {}).get( f"{getattr(cls_, stack_entry.function).__module__}.{getattr(cls_, stack_entry.function).__name__}", getattr( getattr(cls_, stack_entry.function), diff --git a/hikka/inline/events.py b/hikka/inline/events.py index 028209f..379cea4 100644 --- a/hikka/inline/events.py +++ b/hikka/inline/events.py @@ -88,8 +88,7 @@ class Events(InlineUnit): """Checks if user is able to execute command with provided security mask""" return await self._client.dispatcher.security._check( message=message, - func=None, - mask=mask, + func=mask, user=user, ) @@ -138,8 +137,8 @@ class Events(InlineUnit): await self._check_inline_sec_by_mask( mask=form.get( "perms_map", - self._client.dispatcher.security._default, - ), + lambda: self._client.dispatcher.security._default, + )(), user=query.from_user.id, message=form["message"], ) @@ -196,8 +195,22 @@ class Events(InlineUnit): if query.data in self._custom_map: if ( self._custom_map[query.data].get("force_me", False) - and query.from_user.id != self._me - and query.from_user.id not in self._client.dispatcher.security._owner + and query.from_user.id == self._me + ) or ( + await self._check_inline_sec_by_mask( + mask=self._custom_map[query.data].get( + "perms_map", + lambda: self._client.dispatcher.security._default, + )(), + user=query.from_user.id, + message=self._custom_map[query.data]["message"], + ) + if "message" in self._custom_map[query.data] + else False + ): + pass + elif ( + query.from_user.id not in self._client.dispatcher.security._owner and query.from_user.id not in self._custom_map[query.data].get("always_allow", []) ): diff --git a/hikka/inline/gallery.py b/hikka/inline/gallery.py index 07e07cb..2bffa8d 100644 --- a/hikka/inline/gallery.py +++ b/hikka/inline/gallery.py @@ -45,7 +45,7 @@ class Gallery(InlineUnit): next_handler: Union[FunctionType, List[str]], caption: Union[str, FunctionType] = "", *, - force_me: bool = True, + force_me: bool = False, always_allow: Union[list, None] = None, ttl: Union[int, bool] = False, on_unload: Union[FunctionType, None] = None, @@ -168,6 +168,27 @@ class Gallery(InlineUnit): **({"gif": gif} if gif else {}), **({"always_allow": always_allow} if always_allow else {}), **({"perms_map": perms_map} if perms_map else {}), + **({"message": message} if isinstance(message, Message) else {}), + } + + default_map = { + **( + {"always_allow": self._galleries[gallery_uid]["always_allow"]} + if "always_allow" in self._galleries[gallery_uid] + else {} + ), + **( + {"force_me": self._galleries[gallery_uid]["force_me"]} + if "force_me" in self._galleries[gallery_uid] + else {} + ), + **( + {"ttl": self._galleries[gallery_uid]["ttl"]} + if "ttl" in self._galleries[gallery_uid] + else {} + ), + **({"perms_map": perms_map} if perms_map else {}), + **({"message": message} if isinstance(message, Message) else {}), } self._custom_map[btn_call_data[0]] = { @@ -178,22 +199,7 @@ class Gallery(InlineUnit): gallery_uid=gallery_uid, ) ), - **( - {"always_allow": self._galleries[gallery_uid]["always_allow"]} - if "always_allow" in self._galleries[gallery_uid] - else {} - ), - **( - {"force_me": self._galleries[gallery_uid]["force_me"]} - if "force_me" in self._galleries[gallery_uid] - else {} - ), - **( - {"ttl": self._galleries[gallery_uid]["ttl"]} - if "ttl" in self._galleries[gallery_uid] - else {} - ), - **({"perms_map": perms_map} if perms_map else {}), + **default_map } self._custom_map[btn_call_data[1]] = { @@ -204,22 +210,7 @@ class Gallery(InlineUnit): gallery_uid=gallery_uid, ) ), - **( - {"always_allow": self._galleries[gallery_uid]["always_allow"]} - if "always_allow" in self._galleries[gallery_uid] - else {} - ), - **( - {"force_me": self._galleries[gallery_uid]["force_me"]} - if "force_me" in self._galleries[gallery_uid] - else {} - ), - **( - {"ttl": self._galleries[gallery_uid]["ttl"]} - if "ttl" in self._galleries[gallery_uid] - else {} - ), - **({"perms_map": perms_map} if perms_map else {}), + **default_map } self._custom_map[btn_call_data[2]] = { @@ -231,22 +222,7 @@ class Gallery(InlineUnit): gallery_uid=gallery_uid, ) ), - **( - {"always_allow": self._galleries[gallery_uid]["always_allow"]} - if "always_allow" in self._galleries[gallery_uid] - else {} - ), - **( - {"force_me": self._galleries[gallery_uid]["force_me"]} - if "force_me" in self._galleries[gallery_uid] - else {} - ), - **( - {"ttl": self._galleries[gallery_uid]["ttl"]} - if "ttl" in self._galleries[gallery_uid] - else {} - ), - **({"perms_map": perms_map} if perms_map else {}), + **default_map } if isinstance(message, Message): diff --git a/hikka/security.py b/hikka/security.py index 29640e9..002f709 100755 --- a/hikka/security.py +++ b/hikka/security.py @@ -186,21 +186,13 @@ class SecurityManager: async def _check( self, message: Message, - func: FunctionType = None, - mask: int = None, + func: FunctionType, user: int = None, ) -> bool: """Checks if message sender is permitted to execute certain function""" - if not func and not mask: - return False - self._reload_rights() - config = ( - self.get_flags(func) - if func - else (mask & self._db.get(__name__, "bounding_mask", DEFAULT_PERMISSIONS)) - ) + config = self.get_flags(func) if not config: return False