import logging from collections import defaultdict logger = logging.getLogger(__name__) class TaobaoSecurity: def __init__(self): self._allowlists: dict[str, set[str]] = defaultdict(set) def load_config(self, account_id: str, account: dict) -> None: allow_from = account.get("allow_from", []) self._allowlists[account_id] = {entry.strip().lower() for entry in allow_from if entry and entry.strip()} async def check_allowlist(self, peer_id: str, channel_type: str, account_id: str | None = None, account: dict | None = None) -> bool: if account is None: return True policy = account.get("dm_policy", "open") if policy == "disabled": return False if policy in ("allowlist", "pairing"): if account_id: allow_set = self._allowlists.get(account_id, set()) if not allow_set: allow_from = account.get("allow_from", []) allow_set = {entry.strip().lower() for entry in allow_from if entry and entry.strip()} return peer_id in allow_set return peer_id in account.get("allow_from", []) return True def resolve_dm_policy(self, account_id: str | None = None, account: dict | None = None) -> dict: if account is None: return {"mode": "open", "allow_from": []} return { "mode": account.get("dm_policy", "open"), "allow_from": account.get("allow_from", []), } def collect_warnings(self, config: dict, account_id: str | None = None, account: dict | None = None) -> list[str]: warnings = [] acc = account or {} if acc.get("dm_policy") == "open": warnings.append("DM 策略为 'open',任何人可发送消息") if not acc.get("callback_url"): warnings.append("未配置 Webhook 回调 URL,无法接收消息") return warnings