import logging from yuxi.channel.extensions.zalo.config import ZaloConfigAdapter from yuxi.channel.extensions.zalo.pairing import ZaloPairing logger = logging.getLogger(__name__) class ZaloSecurity: def __init__(self, config_adapter: ZaloConfigAdapter | None = None): self._config_adapter = config_adapter or ZaloConfigAdapter() def resolve_dm_policy(self) -> dict: return {"mode": "pairing", "allow_from": []} def resolve_dm_policy_for_account(self, account: dict) -> dict: mode = account.get("dm_policy", "pairing") return {"mode": mode, "allow_from": account.get("allow_from", [])} async def check_allowlist(self, peer_id: str, channel_type: str, account_id: str = "default") -> bool: account = await self._config_adapter.resolve_account(account_id) if channel_type == "group": allowed = account.get("group_allow_from", []) else: allowed = account.get("allow_from", []) if not allowed: return False normalized_peer = ZaloPairing.normalize_allow_entry(peer_id) for entry in allowed: if ZaloPairing.normalize_allow_entry(entry) == normalized_peer: return True return False def resolve_require_mention(self, ctx) -> bool | None: config = getattr(ctx, "config", {}) if ctx else {} group_id = getattr(ctx, "group_id", None) if ctx else None if group_id: groups = config.get("groups", {}) group_cfg = groups.get(group_id, {}) if "require_mention" in group_cfg: return group_cfg["require_mention"] return True def resolve_group_intro_hint(self, ctx) -> str | None: return None def resolve_tool_policy(self, ctx) -> dict | None: return None def apply_config_fixes(self, config: dict) -> dict: return config def collect_warnings(self, config: dict, account_id: str | None = None, account: dict | None = None) -> list[str]: warnings = [] if account: dm_policy = account.get("dm_policy", "") if dm_policy == "open": warnings.append("Zalo dmPolicy is 'open' — consider 'pairing' or 'allowlist' for production use") group_policy = account.get("group_policy", "") if group_policy == "open" and not account.get("group_allow_from"): warnings.append("Zalo groupPolicy is 'open' without group allowlist") return warnings