ForcePilot/backend/package/yuxi/channel/extensions/whatsapp/security.py

67 lines
2.2 KiB
Python
Raw Normal View History

"""WhatsApp Security 适配器 — DM/群组安全策略"""
import logging
logger = logging.getLogger(__name__)
class WhatsAppSecurity:
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) -> bool:
from yuxi.channel.extensions.whatsapp.config import WhatsAppConfigAdapter
adapter = WhatsAppConfigAdapter()
account = await adapter.resolve_account("default")
if not account.get("configured"):
return True
dm_policy = account.get("dm_policy", "pairing")
if dm_policy == "open":
return True
if dm_policy == "disabled":
return False
allow_from = account.get("allow_from", [])
normalized_peer = peer_id.strip().lstrip("+")
return any(entry.strip().lstrip("+") == normalized_peer for entry in allow_from)
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 = []
group_policy = account.get("group_policy", "") if account else config.get("group_policy", "")
if group_policy == "open" and not config.get("groups"):
warnings.append("groupPolicy is 'open' without groups allowlist")
return warnings