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

2 years ago
  1. from typing import Any, Callable, List, Optional, Union
  2. from paircd.message import Message
  3. def cmd_fn(cmd: str, tmpl: str) -> Callable:
  4. def fn(*args: List[Any], prefix: Optional[str] = None) -> Message:
  5. msg = tmpl.format(*args)
  6. return (
  7. Message(cmd=cmd, args=[msg], prefix=prefix)
  8. if prefix is not None
  9. else Message(cmd=cmd, args=[msg])
  10. )
  11. return fn
  12. def reply_fn(cmd: Union[int, str], tmpl: str) -> Callable:
  13. def fn(target: str, *args: List[Any]) -> Message:
  14. msg = f"{target} {tmpl.format(*args)}"
  15. return Message(cmd=str(cmd), args=[msg])
  16. return fn
  17. # Commands
  18. JOIN = cmd_fn("JOIN", "{0}")
  19. MODE = cmd_fn("MODE", "{0} {1}")
  20. PONG = cmd_fn("PONG", ":{0}")
  21. PRIVMSG = cmd_fn("PRIVMSG", "{0} :{1}")
  22. # Error replies
  23. ERR_NOSUCHNICK = reply_fn(401, "{0} :No such nick/channel")
  24. ERR_NOSUCHSERVER = reply_fn(402, "{0} :No such server")
  25. ERR_NOSUCHCHANNEL = reply_fn(403, "{0} :No such channel")
  26. ERR_CANNOTSENDTOCHAN = reply_fn(404, "{0} :Cannot send to channel")
  27. ERR_TOOMANYCHANNELS = reply_fn(405, "{0} :You have joined too many channels")
  28. ERR_NOTEXTTOSEND = reply_fn(412, ":No text to send")
  29. ERR_NOTREGISTERED = reply_fn(451, ":You have not registered")
  30. # Command responses
  31. RPL_WELCOME = reply_fn("001", "Welcome to the Internet Relay Network {0}")
  32. RPL_YOURHOST = reply_fn("002", "Your host is {0}, running version {1}")
  33. RPL_CREATED = reply_fn("003", "This server was created {0}")
  34. RPL_MYINFO = reply_fn("004", "{0} {1} {2} {3}")
  35. RPL_AWAY = reply_fn(301, "{0} :{1}")
  36. RPL_UNAWAY = reply_fn(305, ":You are no longer marked as being away")
  37. RPL_NOWAWAY = reply_fn(306, ":You have been marked as being away")
  38. RPL_WHOISUSER = reply_fn(311, "{0} {1} {2} * :{3}")
  39. RPL_ENDOFWHO = reply_fn(315, "{0} :End of /WHO list")
  40. RPL_ENDOFWHOIS = reply_fn(318, "{0} :End of /WHOIS list")
  41. RPL_CHANNELMODEIS = reply_fn(324, "{0} {1}{2}")
  42. RPL_TOPIC = reply_fn(332, "{0} :{1}")
  43. RPL_WHOREPLY = reply_fn(352, "{0} ~{1} {2} {3} {4} {5} :{6} {7}")
  44. RPL_NAMREPLY = reply_fn(353, "{0} {1} :{2}")
  45. RPL_ENDOFNAMES = reply_fn(366, "{0} :End of /NAMES list")