Browse Source

Add more error responses

master
Forest Belton 2 years ago
parent
commit
2ab2d28f3f
4 changed files with 35 additions and 13 deletions
  1. +1
    -0
      paircd/client.py
  2. +9
    -7
      paircd/handler/join.py
  3. +16
    -5
      paircd/handler/privmsg.py
  4. +9
    -1
      paircd/reply.py

+ 1
- 0
paircd/client.py View File

@ -18,6 +18,7 @@ class Client:
username: str = ""
realname: str = ""
registered: bool = False
away: str = ""
channels: Set[str] = field(default_factory=set)

+ 9
- 7
paircd/handler/join.py View File

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

+ 16
- 5
paircd/handler/privmsg.py View File

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

+ 9
- 1
paircd/reply.py View File

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

Loading…
Cancel
Save