ForcePilot/backend/package/yuxi/channel/extensions/line/security.py
Kris 7fd658af57 feat(channel): 添加 LINE 渠道扩展
新增 LINE 渠道扩展,支持在 Yuxi 平台中集成 LINE 即时通讯渠道。

包含以下功能模块:
- bot: LINE Bot 客户端封装
- config: 渠道配置管理
- gateway: SSE/WebSocket 网关接入
- webhook: Webhook 事件处理
- outbound: 外发消息管理
- streaming: 流式消息处理
- pairing: 用户配对与绑定
- security: 安全校验
- signature: 请求签名验证
- token_manager: Token 管理
- dedupe: 消息去重
- monitor: 渠道状态监控
- status: 会话状态管理
- session: 会话管理
- flex_templates: Flex 模板消息
- card_command: 卡片指令处理
- template_messages: 模板消息
- rich_menu: 富菜单管理
- actions: 动作处理
- directives: 指令处理
- delivery: 消息送达确认
- loading: 加载动画
- media: 媒体资源处理
- types: 类型定义
2026-05-21 11:16:16 +08:00

95 lines
3.1 KiB
Python

from __future__ import annotations
import logging
logger = logging.getLogger(__name__)
class LineSecurityAdapter:
DM_POLICY_OPTIONS = ["pairing", "allowlist", "open", "disabled"]
GROUP_POLICY_OPTIONS = ["open", "allowlist", "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 resolve_group_policy(self) -> dict:
return {"mode": "allowlist", "group_allow_from": []}
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 "*" in allow_from
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
def is_allowed_group(self, group_id: str, group_allow_from: list[str], policy: str) -> bool:
if policy == "open":
return True
if policy == "disabled":
return False
if policy == "allowlist":
return self._match_group(group_id, group_allow_from)
return False
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("channels", {}).get("line", {}).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
@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("line:user:"):
target = entry[10:]
elif entry.startswith("line:"):
target = entry[5:]
else:
target = entry
if target == peer_id:
return True
return False
@staticmethod
def _match_group(group_id: str, group_allow_from: list[str]) -> bool:
for entry in group_allow_from:
entry = entry.strip()
if entry == "*":
return True
if entry.startswith("group:"):
target = entry[6:]
elif entry.startswith("room:"):
target = entry[5:]
else:
target = entry
if target == group_id:
return True
return False