from logging import WARN
|
|
|
|
|
|
from typing import Dict
|
|
|
|
from paircd.client import Client
|
|
from paircd.command_handler import CommandHandler
|
|
from paircd.message import Message
|
|
from paircd.server import Server
|
|
|
|
from paircd.handler.away import AwayHandler
|
|
from paircd.handler.join import JoinHandler
|
|
from paircd.handler.mode import ModeHandler
|
|
from paircd.handler.nick import NickHandler
|
|
from paircd.handler.notice import NoticeHandler
|
|
from paircd.handler.ping import PingHandler
|
|
from paircd.handler.privmsg import PrivmsgHandler
|
|
|
|
from paircd.handler.quit import QuitHandler
|
|
from paircd.handler.topic import TopicHandler
|
|
from paircd.handler.user import UserHandler
|
|
from paircd.handler.who import WhoHandler
|
|
|
|
HANDLER_CLASSES = [
|
|
AwayHandler,
|
|
JoinHandler,
|
|
ModeHandler,
|
|
NickHandler,
|
|
NoticeHandler,
|
|
PingHandler,
|
|
PrivmsgHandler,
|
|
QuitHandler,
|
|
TopicHandler,
|
|
UserHandler,
|
|
WhoHandler,
|
|
]
|
|
|
|
CMD_HANDLERS: Dict[str, CommandHandler] = {}
|
|
|
|
|
|
def register_cmd_handlers() -> None:
|
|
for handler_cls in HANDLER_CLASSES:
|
|
handler = handler_cls() # type: ignore
|
|
CMD_HANDLERS[handler.cmd] = handler
|
|
|
|
|
|
async def handle_cmd(server: Server, client: Client, msg: Message) -> None:
|
|
cmd = msg.cmd.upper()
|
|
if cmd not in CMD_HANDLERS:
|
|
client.log(f"used unknown command {cmd}", level=WARN)
|
|
return
|
|
|
|
handler = CMD_HANDLERS[cmd]
|
|
if handler.argc is not None and handler.argc != len(msg.args):
|
|
client.log(
|
|
"got {len(msg.args)} arguments for {msg.cmd}, expected {handler.argc}",
|
|
level=WARN,
|
|
)
|
|
return
|
|
|
|
await handler.handle(server, client, msg)
|