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

49 lines
1.5 KiB
Python
Raw Normal View History

from __future__ import annotations
import logging
logger = logging.getLogger(__name__)
class ViberSecurityAdapter:
DM_POLICY_OPTIONS = ["pairing", "allowlist", "open", "disabled"]
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:
return True
def is_allowed_dm(self, peer_id: str, allow_from: list[str], policy: str) -> bool:
if "*" in allow_from:
return True
if policy == "open":
return True
if policy == "disabled":
return False
if policy == "allowlist":
return self._match_peer(peer_id, allow_from)
if policy == "pairing":
return self._match_peer(peer_id, allow_from)
return False
@staticmethod
def _match_peer(peer_id: str, allow_from: list[str]) -> bool:
for entry in allow_from:
entry = entry.strip()
if entry == "*":
return True
if entry.startswith("viber:user:"):
target = entry[11:]
elif entry.startswith("viber:"):
target = entry[6:]
else:
target = entry
if target == peer_id:
return True
return False