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

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. NICK = cmd_fn("NICK", "{0}")
  21. NOTICE = cmd_fn("NOTICE", "{0} :{1}")
  22. PONG = cmd_fn("PONG", ":{0}")
  23. PRIVMSG = cmd_fn("PRIVMSG", "{0} :{1}")
  24. QUIT = cmd_fn("QUIT", ":{0}")
  25. TOPIC = cmd_fn("TOPIC", "{0} :{1}")
  26. # Error replies
  27. ERR_NOSUCHNICK = reply_fn(401, "{0} :No such nick/channel")
  28. ERR_NOSUCHSERVER = reply_fn(402, "{0} :No such server")
  29. ERR_NOSUCHCHANNEL = reply_fn(403, "{0} :No such channel")
  30. ERR_CANNOTSENDTOCHAN = reply_fn(404, "{0} :Cannot send to channel")
  31. ERR_TOOMANYCHANNELS = reply_fn(405, "{0} :You have joined too many channels")
  32. ERR_NOTEXTTOSEND = reply_fn(412, ":No text to send")
  33. ERR_NONICKNAMEGIVEN = reply_fn(431, ":No nickname given")
  34. ERR_NICKNAMEINUSE = reply_fn(433, "{0} :Nickname is already in use")
  35. ERR_NOTONCHANNEL = reply_fn(442, "{0} :You're not on that channel")
  36. ERR_NOTREGISTERED = reply_fn(451, ":You have not registered")
  37. ERR_NEEDMOREPARAMS = reply_fn(461, "{0} :Not enough parameters")
  38. ERR_ALREADYREGISTRED = reply_fn(462, ":Unauthorized command (already registered)")
  39. ERR_CHANOPRIVSNEEDED = reply_fn(482, "{0} :You're not channel operator")
  40. ERR_USERSDONTMATCH = reply_fn(502, ":Cannot change mode for other users")
  41. # Command responses
  42. RPL_WELCOME = reply_fn("001", "Welcome to the Internet Relay Network {0}")
  43. RPL_YOURHOST = reply_fn("002", "Your host is {0}, running version {1}")
  44. RPL_CREATED = reply_fn("003", "This server was created {0}")
  45. RPL_MYINFO = reply_fn("004", "{0} {1} {2} {3}")
  46. RPL_UMODEIS = reply_fn(221, "{0}")
  47. RPL_AWAY = reply_fn(301, "{0} :{1}")
  48. RPL_UNAWAY = reply_fn(305, ":You are no longer marked as being away")
  49. RPL_NOWAWAY = reply_fn(306, ":You have been marked as being away")
  50. RPL_WHOISUSER = reply_fn(311, "{0} {1} {2} * :{3}")
  51. RPL_ENDOFWHO = reply_fn(315, "{0} :End of /WHO list")
  52. RPL_ENDOFWHOIS = reply_fn(318, "{0} :End of /WHOIS list")
  53. RPL_CHANNELMODEIS = reply_fn(324, "{0} {1}{2}")
  54. RPL_NOTOPIC = reply_fn(331, "{0} :No topic is set")
  55. RPL_TOPIC = reply_fn(332, "{0} :{1}")
  56. RPL_WHOREPLY = reply_fn(352, "{0} ~{1} {2} {3} {4} {5} :{6} {7}")
  57. RPL_NAMREPLY = reply_fn(353, "{0} {1} :{2}")
  58. RPL_ENDOFNAMES = reply_fn(366, "{0} :End of /NAMES list")