Browse Source

Create message reply factories

master
Forest Belton 2 years ago
parent
commit
c1097cdfae
4 changed files with 25 additions and 10 deletions
  1. +2
    -4
      paircd/handler/join.py
  2. +2
    -2
      paircd/handler/mode.py
  3. +3
    -3
      paircd/handler/privmsg.py
  4. +18
    -1
      paircd/reply.py

+ 2
- 4
paircd/handler/join.py View File

@ -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(

+ 2
- 2
paircd/handler/mode.py View File

@ -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, ""))

+ 3
- 3
paircd/handler/privmsg.py View File

@ -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:

+ 18
- 1
paircd/reply.py View File

@ -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")

Loading…
Cancel
Save