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.
 

56 lines
2.1 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}")
PONG = cmd_fn("PONG", ":{0}")
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_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")
RPL_CHANNELMODEIS = reply_fn(324, "{0} {1}{2}")
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")