ForcePilot/backend/package/yuxi/channels/adapters/irc/ctcp.py

51 lines
1.4 KiB
Python
Raw Normal View History

from __future__ import annotations
from yuxi.utils.logging_config import logger
from .protocol import format_ctcp, is_ctcp_message, parse_ctcp
from .send import send_raw_line
async def handle_ctcp(
send_fn,
source_nick: str,
text: str,
) -> bool:
if not is_ctcp_message(text):
return False
command, args = parse_ctcp(text)
logger.debug(f"IRC CTCP from {source_nick}: {command} {args}")
if command == "PING":
reply = format_ctcp("PING", args)
send_raw_line(send_fn, f"NOTICE {source_nick} :{reply}")
elif command == "VERSION":
reply = format_ctcp("VERSION", "ForcePilot IRC Bot v1.0")
send_raw_line(send_fn, f"NOTICE {source_nick} :{reply}")
elif command == "TIME":
import time as _time
reply = format_ctcp("TIME", f"{_time.time():.0f}")
send_raw_line(send_fn, f"NOTICE {source_nick} :{reply}")
elif command == "CLIENTINFO":
reply = format_ctcp("CLIENTINFO", "PING VERSION TIME ACTION")
send_raw_line(send_fn, f"NOTICE {source_nick} :{reply}")
elif command == "ACTION":
return True
return False
def extract_ctcp_action(text: str) -> str | None:
if not is_ctcp_message(text):
return None
command, args = parse_ctcp(text)
if command == "ACTION":
return args
return None
def supports_ctcp() -> bool:
return True