新增 Twitter 和 Viber 两个渠道扩展。 Twitter 渠道扩展功能模块: - auth: OAuth 认证管理 - config: 渠道配置管理 - gateway: SSE/WebSocket 网关接入 - webhook: Webhook 事件处理 - outbound: 外发消息管理 - streaming: 流式消息处理 - pairing: 用户配对与绑定 - security: 安全校验 - dedupe: 消息去重 - monitor: 渠道状态监控 - status: 会话状态管理 - session: 会话管理 - tweets: 推文管理 - social: 社交互动 - reactions: 表情反应 - media: 媒体资源处理 Viber 渠道扩展功能模块: - config: 渠道配置管理 - gateway: SSE/WebSocket 网关接入 - webhook: Webhook 事件处理 - outbound: 外发消息管理 - streaming: 流式消息处理 - pairing: 用户配对与绑定 - security: 安全校验 - dedupe: 消息去重 - monitor: 渠道状态监控 - status: 会话状态管理 - rate_limiter: 速率限制 - media: 媒体资源处理
90 lines
3.0 KiB
Python
90 lines
3.0 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
DM_POLICIES = ("pairing", "allowlist", "open", "disabled")
|
|
GROUP_POLICIES = ("open", "allowlist", "disabled")
|
|
|
|
|
|
class TwitterSecurity:
|
|
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", [])}
|
|
|
|
def is_allowed_dm(self, account: dict, peer_id: str) -> tuple[bool, str | None]:
|
|
mode = account.get("dm_policy", "pairing")
|
|
if mode == "disabled":
|
|
return False, "DM disabled"
|
|
if mode == "open":
|
|
return True, None
|
|
allow_from = account.get("allow_from", [])
|
|
normalized = self._normalize_peer(peer_id)
|
|
if mode == "allowlist":
|
|
if self._check_allowlist(allow_from, normalized):
|
|
return True, None
|
|
return False, "not-in-allowlist"
|
|
if mode == "pairing":
|
|
if self._check_allowlist(allow_from, normalized):
|
|
return True, "paired"
|
|
return True, "pairing-required"
|
|
return False, "unknown-policy"
|
|
|
|
def is_allowed_group(
|
|
self,
|
|
account: dict,
|
|
peer_id: str,
|
|
group_id: str | None = None,
|
|
is_mentioned: bool = False,
|
|
) -> tuple[bool, str | None]:
|
|
group_policy = account.get("group_policy", "disabled")
|
|
if group_policy == "disabled":
|
|
return False, "group-disabled"
|
|
if group_policy == "open":
|
|
if is_mentioned:
|
|
return True, None
|
|
return False, "mention-required"
|
|
if group_policy == "allowlist":
|
|
allow_from = account.get("group_allow_from", [])
|
|
normalized = self._normalize_peer(peer_id)
|
|
if self._check_allowlist(allow_from, normalized):
|
|
return True, None
|
|
return False, "not-in-group-allowlist"
|
|
return False, "unknown-policy"
|
|
|
|
def collect_warnings(
|
|
self,
|
|
config: dict,
|
|
account_id: str | None = None,
|
|
account: dict | None = None,
|
|
) -> list[str]:
|
|
warnings = []
|
|
if not account:
|
|
return warnings
|
|
if account.get("dm_policy") == "open" and not account.get("allow_from"):
|
|
warnings.append(
|
|
"dmPolicy is 'open' without allowFrom — anyone can DM the bot"
|
|
)
|
|
return warnings
|
|
|
|
@staticmethod
|
|
def _normalize_peer(peer_id: str) -> str:
|
|
for prefix in ("x:", "twitter:"):
|
|
if peer_id.startswith(prefix):
|
|
return peer_id[len(prefix) :]
|
|
return str(peer_id)
|
|
|
|
@staticmethod
|
|
def _check_allowlist(allow_from: list[str], peer_id: str) -> bool:
|
|
if "*" in allow_from:
|
|
return True
|
|
normalized = str(peer_id)
|
|
for entry in allow_from:
|
|
if TwitterSecurity._normalize_peer(str(entry)) == normalized:
|
|
return True
|
|
return False
|