python ircd using asyncio
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

69 lines
2.8 KiB

from typing import Any, Callable, List, Optional, Union
from paircd.message import Message
def cmd_fn(cmd: str, tmpl: str) -> Callable:
def fn(*args: List[Any], prefix: Optional[str] = None) -> Message:
msg = tmpl.format(*args)
return (
Message(cmd=cmd, args=[msg], prefix=prefix)
if prefix is not None
else Message(cmd=cmd, args=[msg])
)
return fn
def reply_fn(cmd: Union[int, str], tmpl: str) -> Callable:
def fn(target: str, *args: List[Any]) -> Message:
msg = f"{target} {tmpl.format(*args)}"
return Message(cmd=str(cmd), args=[msg])
return fn
# Commands
JOIN = cmd_fn("JOIN", "{0}")
MODE = cmd_fn("MODE", "{0} {1}")
NICK = cmd_fn("NICK", "{0}")
NOTICE = cmd_fn("NOTICE", "{0} :{1}")
PONG = cmd_fn("PONG", ":{0}")
PRIVMSG = cmd_fn("PRIVMSG", "{0} :{1}")
QUIT = cmd_fn("QUIT", ":{0}")
TOPIC = cmd_fn("TOPIC", "{0} :{1}")
# Error replies
ERR_NOSUCHNICK = reply_fn(401, "{0} :No such nick/channel")
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_NONICKNAMEGIVEN = reply_fn(431, ":No nickname given")
ERR_NICKNAMEINUSE = reply_fn(433, "{0} :Nickname is already in use")
ERR_NOTONCHANNEL = reply_fn(442, "{0} :You're not on that channel")
ERR_NOTREGISTERED = reply_fn(451, ":You have not registered")
ERR_NEEDMOREPARAMS = reply_fn(461, "{0} :Not enough parameters")
ERR_ALREADYREGISTRED = reply_fn(462, ":Unauthorized command (already registered)")
ERR_CHANOPRIVSNEEDED = reply_fn(482, "{0} :You're not channel operator")
ERR_USERSDONTMATCH = reply_fn(502, ":Cannot change mode for other users")
# 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_UMODEIS = reply_fn(221, "{0}")
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")
RPL_CHANNELMODEIS = reply_fn(324, "{0} {1}{2}")
RPL_NOTOPIC = reply_fn(331, "{0} :No topic is set")
RPL_TOPIC = reply_fn(332, "{0} :{1}")
RPL_WHOREPLY = reply_fn(352, "{0} ~{1} {2} {3} {4} {5} :{6} {7}")
RPL_NAMREPLY = reply_fn(353, "{0} {1} :{2}")
RPL_ENDOFNAMES = reply_fn(366, "{0} :End of /NAMES list")