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

68 lines
2.1 KiB
Python
Raw Normal View History

from yuxi.channel.extensions.tlon.utils import normalize_ship, is_owner
def check_dm_allowlist(ship: str, dm_allowlist: set[str]) -> bool:
if not dm_allowlist:
return False
return normalize_ship(ship) in dm_allowlist
def check_channel_authorization(ship: str, channel_rules: dict | None,
default_authorized_ships: list[str],
owner_ship: str | None) -> bool:
if is_owner(ship, owner_ship):
return True
normalized = normalize_ship(ship)
if channel_rules:
mode = channel_rules.get("mode", "open")
if mode == "open":
return True
allowed = channel_rules.get("allowedShips", [])
allowed_normalized = {normalize_ship(s) for s in allowed}
if normalized in allowed_normalized:
return True
return False
default_set = {normalize_ship(s) for s in default_authorized_ships}
if default_set:
return normalized in default_set
return False
def check_group_invite_allowlist(ship: str,
group_invite_allowlist: set[str]) -> bool:
if not group_invite_allowlist:
return False
return normalize_ship(ship) in group_invite_allowlist
def is_ship_blocked_check(ship: str, blocked_ships: list[str]) -> bool:
return normalize_ship(ship) in {normalize_ship(s) for s in blocked_ships}
async def block_ship(api, ship: str) -> None:
await api.poke("chat", "chat-block-ship", {"ship": normalize_ship(ship)})
async def unblock_ship(api, ship: str) -> None:
await api.poke("chat", "chat-unblock-ship", {"ship": normalize_ship(ship)})
async def get_blocked_ships(api) -> list[str]:
try:
blocked = await api.scry("/chat/blocked.json")
return blocked.get("blocked", [])
except Exception:
return []
def detect_multi_user_dm_session(dm_messages: list[dict]) -> bool:
ships = set()
for msg in dm_messages:
author = msg.get("author", "")
if author:
ships.add(normalize_ship(author))
return len(ships) > 2