from __future__ import annotations import logging logger = logging.getLogger(__name__) class ZaloUserSecurityAdapter: def __init__(self): self._allowlist: dict[str, set] = {} self._dm_policy: str = "pairing" self._group_policy: str = "allowlist" self._allow_from: list[str] = [] self._group_allow_from: list[str] = [] self._groups: dict = {} self._require_mention: bool = True self._dangerously_allow_name_matching: bool = False def set_account_config(self, account: dict) -> None: self._dm_policy = account.get("dm_policy", "pairing") self._group_policy = account.get("group_policy", "allowlist") self._allow_from = account.get("allow_from", []) self._group_allow_from = account.get("group_allow_from", []) self._groups = account.get("groups", {}) self._dangerously_allow_name_matching = account.get("dangerously_allow_name_matching", False) async def check_allowlist(self, peer_id: str, channel_type: str) -> bool: if self._dm_policy == "open": return True if not self._allow_from: return False return peer_id in self._allow_from def resolve_dm_policy(self) -> dict: return {"mode": self._dm_policy, "allow_from": self._allow_from} def resolve_group_policy(self) -> dict: return { "mode": self._group_policy, "groups": self._groups, "group_allow_from": self._group_allow_from, } def is_group_allowed(self, group_id: str) -> bool: policy = self._group_policy if policy == "disabled": return False if policy == "open": return True if not self._groups: return False if group_id in self._groups: entry = self._groups[group_id] if isinstance(entry, bool): return entry if isinstance(entry, dict): return entry.get("enabled", True) return bool(entry) if "*" in self._groups: entry = self._groups["*"] if isinstance(entry, bool): return entry if isinstance(entry, dict): return entry.get("enabled", True) return False def is_sender_allowed_in_group(self, sender_id: str) -> bool: if not self._group_allow_from: return True return sender_id in self._group_allow_from def group_require_mention(self, group_id: str) -> bool: if group_id in self._groups: entry = self._groups[group_id] if isinstance(entry, dict): return entry.get("require_mention", self._require_mention) return self._require_mention def collect_warnings(self, config: dict, account_id: str | None = None, account: dict | None = None) -> list[str]: warnings: list[str] = [] if account: if account.get("dm_policy") == "open": warnings.append( "DM policy is 'open' - anyone can message. Consider setting to 'pairing' or 'allowlist'." ) if account.get("dangerously_allow_name_matching"): warnings.append("dangerously_allow_name_matching is enabled - group names can be spoofed.") return warnings