From 2ab2d28f3fe372f91cfa6a1dd8ad6d4ff06866e1 Mon Sep 17 00:00:00 2001 From: Forest Belton Date: Tue, 22 Jun 2021 04:01:31 -0400 Subject: [PATCH] Add more error responses --- paircd/client.py | 1 + paircd/handler/join.py | 16 +++++++++------- paircd/handler/privmsg.py | 21 ++++++++++++++++----- paircd/reply.py | 10 +++++++++- 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/paircd/client.py b/paircd/client.py index b0777a1..ce8defc 100644 --- a/paircd/client.py +++ b/paircd/client.py @@ -18,6 +18,7 @@ class Client: username: str = "" realname: str = "" registered: bool = False + away: str = "" channels: Set[str] = field(default_factory=set) diff --git a/paircd/handler/join.py b/paircd/handler/join.py index f4b1420..ef9e66f 100644 --- a/paircd/handler/join.py +++ b/paircd/handler/join.py @@ -1,5 +1,11 @@ -import logging -from paircd.reply import JOIN, RPL_ENDOFNAMES, RPL_NAMREPLY, RPL_TOPIC +from paircd.reply import ( + ERR_NOSUCHCHANNEL, + ERR_NOTREGISTERED, + JOIN, + RPL_ENDOFNAMES, + RPL_NAMREPLY, + RPL_TOPIC, +) from paircd.client import Client from paircd.command_handler import CommandHandler @@ -12,13 +18,9 @@ class JoinHandler(CommandHandler): super().__init__("JOIN", 1) async def handle(self, server: Server, client: Client, msg: Message) -> None: - if not client.registered: - client.log("join: not registered", level=logging.WARN) - return - channel_name = msg.args[0] if not channel_name.startswith("#"): - client.log("tried to join invalid channel", level=logging.WARN) + client.write_message(ERR_NOSUCHCHANNEL(client.nickname, channel_name)) return channel = server.get_channel_by_name(channel_name) diff --git a/paircd/handler/privmsg.py b/paircd/handler/privmsg.py index 59eb9dd..ef25b4f 100644 --- a/paircd/handler/privmsg.py +++ b/paircd/handler/privmsg.py @@ -1,9 +1,13 @@ -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.reply import ( + ERR_NOSUCHNICK, + ERR_NOTEXTTOSEND, + ERR_NOTREGISTERED, + PRIVMSG, + RPL_AWAY, +) from paircd.server import Server @@ -13,10 +17,17 @@ class PrivmsgHandler(CommandHandler): async def handle(self, server: Server, client: Client, msg: Message) -> None: recipient, raw_msg = msg.args - out = PRIVMSG(recipient, raw_msg, prefix=client.id()) + if len(raw_msg) == 0: + client.write_message(ERR_NOTEXTTOSEND(client.nickname)) + return + out = PRIVMSG(recipient, raw_msg, prefix=client.id()) for name, other_client in server.clients_by_nick.items(): if name == recipient: + if other_client.away: + client.write_message( + RPL_AWAY(client.nickname, name, other_client.away) + ) other_client.write_message(out) return @@ -25,4 +36,4 @@ class PrivmsgHandler(CommandHandler): channel.write_message(out) return - client.log("unknown recipient", level=logging.WARN) + client.write_message(ERR_NOSUCHNICK(client.nickname, recipient)) diff --git a/paircd/reply.py b/paircd/reply.py index 8b52cc8..461af08 100644 --- a/paircd/reply.py +++ b/paircd/reply.py @@ -31,13 +31,21 @@ 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") +ERR_NOSUCHSERVER = reply_fn(402, "{0} :No such server") +ERR_NOSUCHCHANNEL = reply_fn(403, "{0} :No such channel") +ERR_CANNOTSENDTOCHAN = reply_fn(404, "{0} :Cannot send to channel") +ERR_TOOMANYCHANNELS = reply_fn(405, "{0} :You have joined too many channels") +ERR_NOTEXTTOSEND = reply_fn(412, ":No text to send") +ERR_NOTREGISTERED = reply_fn(451, ":You have not registered") # Command responses RPL_WELCOME = reply_fn("001", "Welcome to the Internet Relay Network {0}") RPL_YOURHOST = reply_fn("002", "Your host is {0}, running version {1}") RPL_CREATED = reply_fn("003", "This server was created {0}") RPL_MYINFO = reply_fn("004", "{0} {1} {2} {3}") +RPL_AWAY = reply_fn(301, "{0} :{1}") +RPL_UNAWAY = reply_fn(305, ":You are no longer marked as being away") +RPL_NOWAWAY = reply_fn(306, ":You have been marked as being away") RPL_WHOISUSER = reply_fn(311, "{0} {1} {2} * :{3}") RPL_ENDOFWHO = reply_fn(315, "{0} :End of /WHO list") RPL_ENDOFWHOIS = reply_fn(318, "{0} :End of /WHOIS list")