62 lines
1.7 KiB
Python
62 lines
1.7 KiB
Python
|
|
import logging
|
||
|
|
|
||
|
|
from yuxi.channel.extensions.bluebubbles.config import BlueBubblesConfigAdapter
|
||
|
|
|
||
|
|
logger = logging.getLogger(__name__)
|
||
|
|
|
||
|
|
config_adapter = BlueBubblesConfigAdapter()
|
||
|
|
|
||
|
|
|
||
|
|
class BlueBubblesSecurity:
|
||
|
|
def __init__(self):
|
||
|
|
self._config_adapter = config_adapter
|
||
|
|
|
||
|
|
async def check_dm_access(self, config: dict, peer_id: str) -> bool:
|
||
|
|
channel_config = self._config_adapter._get_channel_config(config)
|
||
|
|
dm_policy = channel_config.get("dmPolicy", "pairing")
|
||
|
|
|
||
|
|
if dm_policy == "disabled":
|
||
|
|
return False
|
||
|
|
|
||
|
|
if dm_policy == "open":
|
||
|
|
return True
|
||
|
|
|
||
|
|
allow_from = channel_config.get("allowFrom", [])
|
||
|
|
if peer_id in allow_from:
|
||
|
|
return True
|
||
|
|
|
||
|
|
if dm_policy == "pairing":
|
||
|
|
return False
|
||
|
|
|
||
|
|
return False
|
||
|
|
|
||
|
|
async def check_group_access(self, config: dict, chat_guid: str, sender_handle: str) -> bool:
|
||
|
|
channel_config = self._config_adapter._get_channel_config(config)
|
||
|
|
group_policy = channel_config.get("groupPolicy", "allowlist")
|
||
|
|
|
||
|
|
if group_policy == "disabled":
|
||
|
|
return False
|
||
|
|
|
||
|
|
if group_policy == "open":
|
||
|
|
return True
|
||
|
|
|
||
|
|
group_allow_from = channel_config.get("groupAllowFrom", [])
|
||
|
|
if sender_handle in group_allow_from:
|
||
|
|
return True
|
||
|
|
|
||
|
|
groups = channel_config.get("groups", {})
|
||
|
|
if "*" in groups:
|
||
|
|
return True
|
||
|
|
|
||
|
|
if chat_guid in groups:
|
||
|
|
return True
|
||
|
|
|
||
|
|
return False
|
||
|
|
|
||
|
|
def resolve_dm_policy(self, config: dict) -> dict:
|
||
|
|
channel_config = self._config_adapter._get_channel_config(config)
|
||
|
|
return {
|
||
|
|
"mode": channel_config.get("dmPolicy", "pairing"),
|
||
|
|
"allow_from": channel_config.get("allowFrom", []),
|
||
|
|
}
|