From c1097cdfae2c4c787e5c59b1990b46b26ece57ae Mon Sep 17 00:00:00 2001 From: Forest Belton <65484+forestbelton@users.noreply.github.com> Date: Tue, 22 Jun 2021 02:47:11 -0400 Subject: [PATCH] Create message reply factories --- paircd/handler/join.py | 6 ++---- paircd/handler/mode.py | 4 ++-- paircd/handler/privmsg.py | 6 +++--- paircd/reply.py | 19 ++++++++++++++++++- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/paircd/handler/join.py b/paircd/handler/join.py index 07ebcc3..f4b1420 100644 --- a/paircd/handler/join.py +++ b/paircd/handler/join.py @@ -1,5 +1,5 @@ import logging -from paircd.reply import RPL_ENDOFNAMES, RPL_NAMREPLY, RPL_TOPIC +from paircd.reply import JOIN, RPL_ENDOFNAMES, RPL_NAMREPLY, RPL_TOPIC from paircd.client import Client from paircd.command_handler import CommandHandler @@ -27,9 +27,7 @@ class JoinHandler(CommandHandler): client.channels.add(channel_name) client.log(f"joined {channel_name}") - channel.write_message( - Message(cmd="JOIN", args=[channel_name], prefix=client.id()) - ) + channel.write_message(JOIN(channel_name, prefix=client.id())) if channel.topic != "": client.write_message( diff --git a/paircd/handler/mode.py b/paircd/handler/mode.py index 22d18c0..7124ac7 100644 --- a/paircd/handler/mode.py +++ b/paircd/handler/mode.py @@ -1,5 +1,5 @@ import logging -from paircd.reply import RPL_CHANNELMODEIS +from paircd.reply import MODE, RPL_CHANNELMODEIS from paircd.client import Client from paircd.command_handler import CommandHandler @@ -23,5 +23,5 @@ class ModeHandler(CommandHandler): modes = f"+{modes}" client.log("TODO: implement channel modes", level=logging.WARN) - client.write_message(Message("MODE", args=[name, modes])) + client.write_message(MODE(name, modes)) client.write_message(RPL_CHANNELMODEIS(client.nickname, name, modes, "")) diff --git a/paircd/handler/privmsg.py b/paircd/handler/privmsg.py index 9736570..59eb9dd 100644 --- a/paircd/handler/privmsg.py +++ b/paircd/handler/privmsg.py @@ -3,6 +3,7 @@ import logging from paircd.client import Client from paircd.command_handler import CommandHandler from paircd.message import Message +from paircd.reply import PRIVMSG from paircd.server import Server @@ -11,9 +12,8 @@ class PrivmsgHandler(CommandHandler): super().__init__("PRIVMSG", 2) async def handle(self, server: Server, client: Client, msg: Message) -> None: - recipient = msg.args[0] - raw_msg = msg.args[1] - out = Message("PRIVMSG", [recipient, f":{raw_msg}"], prefix=client.id()) + recipient, raw_msg = msg.args + out = PRIVMSG(recipient, raw_msg, prefix=client.id()) for name, other_client in server.clients_by_nick.items(): if name == recipient: diff --git a/paircd/reply.py b/paircd/reply.py index b709f50..e902a01 100644 --- a/paircd/reply.py +++ b/paircd/reply.py @@ -1,8 +1,20 @@ -from typing import Any, Callable, List +from typing import Any, Callable, List, Optional from paircd.message import Message +def cmd_fn(cmd: str, tmpl: str) -> Callable: + def fn(prefix: Optional[str] = None, *args: List[Any]) -> Message: + msg = tmpl.format(*args) + return ( + Message(cmd=cmd, args=[msg], prefix=prefix) + if prefix is not None + else Message(cmd=str(cmd), args=[msg]) + ) + + return fn + + def reply_fn(cmd: int, tmpl: str) -> Callable: def fn(target: str, *args: List[Any]) -> Message: msg = f"{target} {tmpl.format(*args)}" @@ -11,6 +23,11 @@ def reply_fn(cmd: int, tmpl: str) -> Callable: return fn +# Commands +JOIN = cmd_fn("JOIN", "{0}") +MODE = cmd_fn("MODE", "{0} {1}") +PRIVMSG = cmd_fn("PRIVMSG", "{0} :{1}") + # Error replies ERR_NOSUCHNICK = reply_fn(401, "{0} :No such nick/channel") ERR_NOSUCHSERVER = reply_fn(402, "{0}: No such server")