新增了完整的Signal渠道适配器实现,包含RPC客户端、守护进程管理、安全策略、消息处理、安装配置工具等全套功能,支持通过signal-cli与Signal网络进行通信,包含账户管理、消息收发、反应处理、媒体分析、健康检查等能力。
60 lines
1.9 KiB
Python
60 lines
1.9 KiB
Python
from __future__ import annotations
|
|
|
|
from enum import StrEnum
|
|
from typing import Any
|
|
from collections.abc import Awaitable, Callable
|
|
|
|
|
|
class ExecAuthResult(StrEnum):
|
|
APPROVED = "approved"
|
|
DENIED = "denied"
|
|
PENDING = "pending"
|
|
|
|
|
|
class ExecAuthAdapter:
|
|
def __init__(self, auto_approve: bool = False):
|
|
self._auto_approve = auto_approve
|
|
self._pending_requests: dict[str, dict] = {}
|
|
self._request_counter = 0
|
|
self._on_approval_request: Callable[[dict], Awaitable[None]] | None = None
|
|
|
|
def on_approval_request(self, handler: Callable[[dict], Awaitable[None]]) -> None:
|
|
self._on_approval_request = handler
|
|
|
|
async def request_approval(self, action: str, params: dict[str, Any], user_id: str) -> ExecAuthResult:
|
|
if self._auto_approve:
|
|
return ExecAuthResult.APPROVED
|
|
|
|
self._request_counter += 1
|
|
request_id = f"exec_auth_{self._request_counter}"
|
|
request = {
|
|
"id": request_id,
|
|
"action": action,
|
|
"params": params,
|
|
"user_id": user_id,
|
|
"result": ExecAuthResult.PENDING,
|
|
}
|
|
self._pending_requests[request_id] = request
|
|
|
|
if self._on_approval_request:
|
|
await self._on_approval_request(request)
|
|
|
|
return ExecAuthResult.PENDING
|
|
|
|
def approve(self, request_id: str) -> ExecAuthResult:
|
|
request = self._pending_requests.get(request_id)
|
|
if not request:
|
|
return ExecAuthResult.DENIED
|
|
request["result"] = ExecAuthResult.APPROVED
|
|
return ExecAuthResult.APPROVED
|
|
|
|
def deny(self, request_id: str) -> ExecAuthResult:
|
|
request = self._pending_requests.get(request_id)
|
|
if not request:
|
|
return ExecAuthResult.DENIED
|
|
request["result"] = ExecAuthResult.DENIED
|
|
return ExecAuthResult.DENIED
|
|
|
|
def get_pending(self) -> list[dict]:
|
|
return [r for r in self._pending_requests.values() if r["result"] == ExecAuthResult.PENDING]
|