from __future__ import annotations import logging from yuxi.channel.sdk import ( AllowlistHitReason, ) from .config import _apply_env_overrides, _dict_to_account from .utils import is_room_id, is_matrix_user_id logger = logging.getLogger(__name__) async def check_allowlist(peer_id: str, channel_type: str, config: dict = None, account_id: str = None) -> dict: from yuxi.channel.sdk import get_allowlist_checker config = config or {} checker = await get_allowlist_checker() aid = account_id or "default" account_data = config.get("accounts", {}).get(aid, {}) account = _dict_to_account(account_data) account = _apply_env_overrides(account) if is_room_id(peer_id): if account.group_policy == "disabled": return {"allowed": False, "reason": AllowlistHitReason.GROUP_DISABLED.value} if account.group_policy == "open": return {"allowed": True, "reason": AllowlistHitReason.GROUP_OPEN.value} if not account.group_allow_from: return {"allowed": True, "reason": AllowlistHitReason.ALLOW_FROM_EMPTY.value} result = checker.check_group(peer_id) return {"allowed": result.allowed, "reason": result.reason.value, "matched_entry": result.matched_entry} if is_matrix_user_id(peer_id): if account.dm_policy == "disabled": return {"allowed": False, "reason": AllowlistHitReason.DM_DISABLED.value} if account.dm_policy == "open": return {"allowed": True, "reason": AllowlistHitReason.DM_OPEN.value} if account.dm_policy == "pairing": result = checker.check_dm(peer_id) return { "allowed": result.allowed, "reason": result.reason.value, "matched_entry": result.matched_entry, "pairing_required": result.pairing_required, } result = checker.check_dm(peer_id) return {"allowed": result.allowed, "reason": result.reason.value, "matched_entry": result.matched_entry} return {"allowed": False, "reason": "unknown_peer_type"} def resolve_dm_policy(config: dict = None, account_id: str = None) -> dict: config = config or {} aid = account_id or "default" account_data = config.get("accounts", {}).get(aid, {}) account = _dict_to_account(account_data) account = _apply_env_overrides(account) return {"dm_policy": account.dm_policy} def apply_config_fixes(config: dict) -> dict: return config def collect_warnings(config, account_id=None, account=None) -> list[str]: warnings = [] if account: acct = _dict_to_account(account) if isinstance(account, dict) else account acct = _apply_env_overrides(acct) if acct.dm_policy == "open": warnings.append("DM policy is set to 'open' — anyone can DM the bot") if acct.group_policy == "open": warnings.append("Group policy is set to 'open' — anyone can trigger the bot") return warnings def collect_audit_findings(config, account_id=None, account=None) -> list[dict]: return []