Browse Source

Implement handler for PING command

master
Forest Belton 3 years ago
parent
commit
2e04a2c426
3 changed files with 19 additions and 2 deletions
  1. +14
    -0
      paircd/handler/ping.py
  2. +2
    -0
      paircd/handlers.py
  3. +3
    -2
      paircd/reply.py

+ 14
- 0
paircd/handler/ping.py View File

@ -0,0 +1,14 @@
from paircd.reply import PONG
from paircd.client import Client
from paircd.command_handler import CommandHandler
from paircd.message import Message
from paircd.server import Server
class PingHandler(CommandHandler):
def __init__(self) -> None:
super().__init__("PING", 1)
async def handle(self, server: Server, client: Client, msg: Message) -> None:
token = msg.args[0]
client.write_message(PONG(token))

+ 2
- 0
paircd/handlers.py View File

@ -9,6 +9,7 @@ from paircd.server import Server
from paircd.handler.join import JoinHandler from paircd.handler.join import JoinHandler
from paircd.handler.mode import ModeHandler from paircd.handler.mode import ModeHandler
from paircd.handler.nick import NickHandler from paircd.handler.nick import NickHandler
from paircd.handler.ping import PingHandler
from paircd.handler.privmsg import PrivmsgHandler from paircd.handler.privmsg import PrivmsgHandler
from paircd.handler.user import UserHandler from paircd.handler.user import UserHandler
from paircd.handler.who import WhoHandler from paircd.handler.who import WhoHandler
@ -17,6 +18,7 @@ HANDLER_CLASSES = [
JoinHandler, JoinHandler,
ModeHandler, ModeHandler,
NickHandler, NickHandler,
PingHandler,
PrivmsgHandler, PrivmsgHandler,
UserHandler, UserHandler,
WhoHandler, WhoHandler,

+ 3
- 2
paircd/reply.py View File

@ -4,12 +4,12 @@ from paircd.message import Message
def cmd_fn(cmd: str, tmpl: str) -> Callable: def cmd_fn(cmd: str, tmpl: str) -> Callable:
def fn(prefix: Optional[str] = None, *args: List[Any]) -> Message:
def fn(*args: List[Any], prefix: Optional[str] = None) -> Message:
msg = tmpl.format(*args) msg = tmpl.format(*args)
return ( return (
Message(cmd=cmd, args=[msg], prefix=prefix) Message(cmd=cmd, args=[msg], prefix=prefix)
if prefix is not None if prefix is not None
else Message(cmd=str(cmd), args=[msg])
else Message(cmd=cmd, args=[msg])
) )
return fn return fn
@ -26,6 +26,7 @@ def reply_fn(cmd: Union[int, str], tmpl: str) -> Callable:
# Commands # Commands
JOIN = cmd_fn("JOIN", "{0}") JOIN = cmd_fn("JOIN", "{0}")
MODE = cmd_fn("MODE", "{0} {1}") MODE = cmd_fn("MODE", "{0} {1}")
PONG = cmd_fn("PONG", ":{0}")
PRIVMSG = cmd_fn("PRIVMSG", "{0} :{1}") PRIVMSG = cmd_fn("PRIVMSG", "{0} :{1}")
# Error replies # Error replies

Loading…
Cancel
Save