Files

28 lines
1.2 KiB
Python

import asyncio
from ..globs import literal_glob
from .constants import PREFIXES
from .context import CommandContext
class TargetError(ValueError):
pass
async def resolve_reply_target(ctx: CommandContext, escape_globs: bool, whole_host_hint: bool) -> tuple[str, str]:
message = ctx.message
prefix, name = PREFIXES.get(message.backend, "/"), ctx.command.name
if not message.reply_to or ctx.deps.lookup is None:
raise TargetError(f"Host fehlt. Nutze {prefix}{name} <host> ... oder antworte auf eine Alarmnachricht.")
found = await asyncio.to_thread(ctx.deps.lookup.services_for_message, message.backend, message.target,
message.reply_to)
if not found:
raise TargetError("Die Nachricht gehört zu keinem bekannten Alarm.")
if len(found) > 1:
hint = f"{prefix}{name} {found[0][0]} <service> <dauer>"
if whole_host_hint:
hint += f" oder {prefix}{name} {found[0][0]} <dauer> für den ganzen Host"
raise TargetError(f"Die Nachricht enthält mehrere Services. Nutze {hint}.")
host, service = found[0]
return (literal_glob(host), literal_glob(service)) if escape_globs else (host, service)