55 lines
1.8 KiB
Python
55 lines
1.8 KiB
Python
|
|
from __future__ import annotations
|
|||
|
|
|
|||
|
|
from typing import Any
|
|||
|
|
|
|||
|
|
|
|||
|
|
def parse_command(text: str) -> dict[str, Any] | None:
|
|||
|
|
"""解析控制命令。
|
|||
|
|
|
|||
|
|
支持的格式:/cmd <action> [args...]
|
|||
|
|
|
|||
|
|
返回 {"action": str, "args": list[str]} 或 None。
|
|||
|
|
"""
|
|||
|
|
if not text.startswith("/"):
|
|||
|
|
return None
|
|||
|
|
|
|||
|
|
parts = text[1:].strip().split()
|
|||
|
|
if not parts:
|
|||
|
|
return None
|
|||
|
|
|
|||
|
|
action = parts[0].lower()
|
|||
|
|
args = parts[1:] if len(parts) > 1 else []
|
|||
|
|
return {"action": action, "args": args}
|
|||
|
|
|
|||
|
|
|
|||
|
|
def is_authorized_for_commands(sender_handle: str, allow_list: list[str]) -> bool:
|
|||
|
|
"""检查发送者是否有权限执行控制命令。"""
|
|||
|
|
if "*" in allow_list:
|
|||
|
|
return True
|
|||
|
|
sender_clean = sender_handle.strip().replace(" ", "").lstrip("+")
|
|||
|
|
for entry in allow_list:
|
|||
|
|
entry_clean = entry.strip().replace(" ", "").lstrip("+")
|
|||
|
|
if entry_clean == sender_clean:
|
|||
|
|
return True
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
|
|||
|
|
AVAILABLE_COMMANDS = {
|
|||
|
|
"status": {"description": "Show adapter status", "requires_owner": False},
|
|||
|
|
"health": {"description": "Show health check result", "requires_owner": False},
|
|||
|
|
"pairing": {"description": "Show pairing status", "requires_owner": False},
|
|||
|
|
"approve": {"description": "Approve a pending approval request", "requires_owner": True},
|
|||
|
|
"reject": {"description": "Reject a pending approval request", "requires_owner": True},
|
|||
|
|
"allowlist": {"description": "Manage allowlist entries", "requires_owner": True},
|
|||
|
|
"help": {"description": "Show this help message", "requires_owner": False},
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
def get_command_help(include_owner: bool = False) -> str:
|
|||
|
|
lines = ["Available commands:"]
|
|||
|
|
for name, info in AVAILABLE_COMMANDS.items():
|
|||
|
|
if info["requires_owner"] and not include_owner:
|
|||
|
|
continue
|
|||
|
|
lines.append(f" /{name} — {info['description']}")
|
|||
|
|
return "\n".join(lines)
|