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

60 lines
1.6 KiB
Python
Raw Normal View History

from __future__ import annotations
import logging
from typing import Any
from yuxi.channel.extensions.qqbot.types import QQBotAccountConfig, QQBotChatType
logger = logging.getLogger(__name__)
class QQBotSecurity:
def __init__(self, account: QQBotAccountConfig):
self._account = account
def check_dm_access(self, sender_id: str) -> bool:
policy = self._account.dm_policy
if policy == "open":
if not self._account.allow_from or "*" in self._account.allow_from:
return True
return sender_id in self._account.allow_from
if policy == "allowlist":
return sender_id in self._account.allow_from
if policy == "disabled":
return False
return True
def check_group_access(self, sender_id: str, group_openid: str) -> bool:
policy = self._account.group_policy
if policy == "open":
return True
if policy == "allowlist":
allowed = self._account.group_allow_from or self._account.allow_from
return sender_id in allowed
if policy == "disabled":
return False
return True
def resolve_dm_policy(self) -> dict:
return {
"mode": self._account.dm_policy,
"allow_from": self._account.allow_from,
}
def normalize_allow_entry(self, entry: str) -> str:
if entry.startswith("qqbot:"):
parts = entry.split(":", 1)
if len(parts) >= 2:
return parts[1]
return entry
def is_allow_all(self) -> bool:
return "*" in self._account.allow_from