from __future__ import annotations from dataclasses import dataclass from typing import Any from .settings import AccessControlSettings @dataclass(frozen=True) class EventIdentity: platform_id: str platform_name: str message_type: str session_id: str unified_msg_origin: str self_id: str group_id: str sender_id: str is_private: bool @classmethod def from_event(cls, event: Any) -> "EventIdentity": group_id = _call_string(event, "get_group_id") sender_id = _call_string(event, "get_sender_id") message_type = _message_type(event) is_private = _is_private(event, message_type, group_id) return cls( platform_id=_platform_id(event), platform_name=_platform_name(event), message_type=message_type, session_id=_session_id(event), unified_msg_origin=_unified_origin(event), self_id=_call_string(event, "get_self_id"), group_id=group_id, sender_id=sender_id, is_private=is_private, ) @property def target_kind(self) -> str: return "private" if self.is_private else "group" @property def target_id(self) -> str: return self.sender_id if self.is_private else self.group_id @dataclass(frozen=True) class AccessDecision: allowed: bool reason: str identity: EventIdentity class AccessControl: def __init__(self, settings: AccessControlSettings) -> None: self.settings = settings def check(self, event: Any) -> AccessDecision: identity = EventIdentity.from_event(event) if not self.settings.enabled: return AccessDecision(True, "access control disabled", identity) if _contains( self.settings.deny_platform_ids, identity.platform_id, identity.platform_name, ): return AccessDecision(False, "platform is blacklisted", identity) if _contains_scoped( self.settings.deny_self_ids, identity.self_id, identity.platform_id, identity.platform_name, ): return AccessDecision(False, "self id is blacklisted", identity) if _contains_exact(self.settings.deny_unified_origins, identity.unified_msg_origin): return AccessDecision(False, "unified origin is blacklisted", identity) if identity.is_private: if _contains_scoped( self.settings.deny_private_user_ids, identity.sender_id, identity.platform_id, identity.platform_name, ): return AccessDecision(False, "private user is blacklisted", identity) elif _contains_scoped( self.settings.deny_group_ids, identity.group_id, identity.platform_id, identity.platform_name, ): return AccessDecision(False, "group is blacklisted", identity) if not self.settings.require_whitelist: return AccessDecision(True, "whitelist not required", identity) if _contains( self.settings.allow_platform_ids, identity.platform_id, identity.platform_name, ): return AccessDecision(True, "platform is whitelisted", identity) if _contains_scoped( self.settings.allow_self_ids, identity.self_id, identity.platform_id, identity.platform_name, ): return AccessDecision(True, "self id is whitelisted", identity) if _contains_exact(self.settings.allow_unified_origins, identity.unified_msg_origin): return AccessDecision(True, "unified origin is whitelisted", identity) if identity.is_private: if _contains_scoped( self.settings.allow_private_user_ids, identity.sender_id, identity.platform_id, identity.platform_name, ): return AccessDecision(True, "private user is whitelisted", identity) elif _contains_scoped( self.settings.allow_group_ids, identity.group_id, identity.platform_id, identity.platform_name, ): return AccessDecision(True, "group is whitelisted", identity) return AccessDecision(False, "not in whitelist", identity) def _contains(values: list[str] | None, *candidates: str) -> bool: normalized_values = {value.strip() for value in values or [] if value.strip()} return any(candidate and candidate in normalized_values for candidate in candidates) def _contains_exact(values: list[str] | None, candidate: str) -> bool: normalized_values = {value.strip() for value in values or [] if value.strip()} return bool(candidate and candidate in normalized_values) def _contains_scoped( values: list[str] | None, raw_id: str, platform_id: str, platform_name: str, ) -> bool: if not raw_id: return False candidates = {raw_id} if platform_id: candidates.add(f"{platform_id}:{raw_id}") if platform_name: candidates.add(f"{platform_name}:{raw_id}") return _contains(values, *candidates) def _call_string(event: Any, method_name: str) -> str: method = getattr(event, method_name, None) if not callable(method): return "" try: value = method() except Exception: return "" return _string_value(value) def _call_bool(event: Any, method_name: str) -> bool | None: method = getattr(event, method_name, None) if not callable(method): return None try: return bool(method()) except Exception: return None def _string_value(value: Any) -> str: if value is None: return "" raw = getattr(value, "value", value) return str(raw).strip() def _message_type(event: Any) -> str: value = _call_string(event, "get_message_type") return value or _string_value(getattr(getattr(event, "session", None), "message_type", "")) def _session_id(event: Any) -> str: return ( _call_string(event, "get_session_id") or _string_value(getattr(event, "session_id", "")) or _string_value(getattr(getattr(event, "session", None), "session_id", "")) ) def _is_private(event: Any, message_type: str, group_id: str) -> bool: explicit = _call_bool(event, "is_private_chat") if explicit is not None: return explicit if message_type: return message_type == "FriendMessage" return not bool(group_id) def _unified_origin(event: Any) -> str: return _string_value(getattr(event, "unified_msg_origin", "")) def _platform_id(event: Any) -> str: return _call_string(event, "get_platform_id") or _string_value( getattr(getattr(event, "session", None), "platform_id", ""), ) def _platform_name(event: Any) -> str: return _call_string(event, "get_platform_name")