Browse Source

Simplify handler registration

master
Forest Belton 2 years ago
parent
commit
abdc5f4508
9 changed files with 73 additions and 40 deletions
  1. +2
    -0
      .flake8
  2. +18
    -0
      paircd/command_handler.py
  3. +0
    -27
      paircd/handle.py
  4. +4
    -2
      paircd/handler/join.py
  5. +4
    -0
      paircd/handler/nick.py
  6. +5
    -1
      paircd/handler/privmsg.py
  7. +4
    -0
      paircd/handler/user.py
  8. +34
    -0
      paircd/handlers.py
  9. +2
    -10
      paircd/main.py

+ 2
- 0
.flake8 View File

@ -0,0 +1,2 @@
[flake8]
max-line-length = 100

+ 18
- 0
paircd/command_handler.py View File

@ -0,0 +1,18 @@
from dataclasses import dataclass
from typing import Awaitable, Callable
from paircd.client import Client
from paircd.message import Message
from paircd.server import Server
HandlerFunc = Callable[
[Server, Client, Message],
Awaitable[None],
]
@dataclass
class CommandHandler:
cmd: str
argc: int
handler: HandlerFunc

+ 0
- 27
paircd/handle.py View File

@ -1,27 +0,0 @@
import logging
from typing import Any, Awaitable, Callable, Dict
from paircd.client import Client
from paircd.log import log_client
from paircd.message import Message
from paircd.server import Server
CMD_HANDLERS: Dict[str, Any] = {}
CMD_EXPECTED_ARGC: Dict[str, int] = {}
CmdHandler = Callable[
[Server, Client, Message],
Awaitable[None],
]
def register_cmd_handler(cmd: str, argc: int, handler: CmdHandler) -> None:
CMD_EXPECTED_ARGC[cmd] = argc
CMD_HANDLERS[cmd] = handler
async def handle_cmd(server: Server, client: Client, msg: Message) -> None:
if msg.cmd not in CMD_HANDLERS:
log_client(client, f"used unknown command {msg.cmd}", level=logging.WARN)
return
await CMD_HANDLERS[msg.cmd](server, client, msg)

+ 4
- 2
paircd/handler/join.py View File

@ -1,8 +1,7 @@
from asyncio import create_task
import logging
from paircd.channel import Channel
from paircd.client import Client
from paircd.command_handler import CommandHandler
from paircd.log import log_client
from paircd.message import Message
from paircd.server import Server
@ -27,3 +26,6 @@ async def handle_join(server: Server, client: Client, msg: Message) -> None:
await channel.msg_queue.put(
Message(cmd="JOIN", args=[channel_name], prefix=client.id()).encode()
)
JOIN = CommandHandler("JOIN", 1, handle_join)

+ 4
- 0
paircd/handler/nick.py View File

@ -1,4 +1,5 @@
from paircd.client import Client
from paircd.command_handler import CommandHandler
from paircd.log import log_client
from paircd.message import Message
from paircd.server import Server
@ -17,3 +18,6 @@ async def handle_nick(server: Server, client: Client, msg: Message) -> None:
if client.username and client.realname:
client.registered = True
log_client(client, "registered")
NICK = CommandHandler("NICK", 1, handle_nick)

+ 5
- 1
paircd/handler/privmsg.py View File

@ -1,7 +1,8 @@
import logging
from paircd.log import log_client
from paircd.client import Client
from paircd.command_handler import CommandHandler
from paircd.log import log_client
from paircd.message import Message
from paircd.server import Server
@ -23,3 +24,6 @@ async def handle_privmsg(server: Server, client: Client, msg: Message) -> None:
return
log_client(client, "unknown recipient", level=logging.WARN)
PRIVMSG = CommandHandler("PRIVMSG", 2, handle_privmsg)

+ 4
- 0
paircd/handler/user.py View File

@ -1,6 +1,7 @@
import logging
from paircd.client import Client
from paircd.command_handler import CommandHandler
from paircd.log import log_client
from paircd.message import Message
from paircd.server import Server
@ -17,3 +18,6 @@ async def handle_user(server: Server, client: Client, msg: Message) -> None:
if client.nickname:
client.registered = True
log_client(client, "registered")
USER = CommandHandler("USER", 4, handle_user)

+ 34
- 0
paircd/handlers.py View File

@ -0,0 +1,34 @@
from logging import WARN
from typing import Dict
from paircd.client import Client
from paircd.command_handler import CommandHandler
from paircd.log import log_client
from paircd.message import Message
from paircd.server import Server
from paircd.handler.join import JOIN
from paircd.handler.nick import NICK
from paircd.handler.privmsg import PRIVMSG
from paircd.handler.user import USER
ALL_HANDLERS = [
JOIN,
NICK,
PRIVMSG,
USER,
]
CMD_HANDLERS: Dict[str, CommandHandler] = {}
def register_cmd_handlers() -> None:
for handler in ALL_HANDLERS:
CMD_HANDLERS[handler.cmd] = handler
async def handle_cmd(server: Server, client: Client, msg: Message) -> None:
if msg.cmd not in CMD_HANDLERS:
log_client(client, f"used unknown command {msg.cmd}", level=WARN)
return
await CMD_HANDLERS[msg.cmd].handler(server, client, msg)

+ 2
- 10
paircd/main.py View File

@ -4,11 +4,7 @@ import logging
import os
from paircd.client import Client
from paircd.handle import handle_cmd, register_cmd_handler
from paircd.handler.join import handle_join
from paircd.handler.nick import handle_nick
from paircd.handler.privmsg import handle_privmsg
from paircd.handler.user import handle_user
from paircd.handlers import handle_cmd, register_cmd_handlers
from paircd.message import parse_message
from paircd.server import Server
@ -30,12 +26,8 @@ async def main() -> None:
bind_addr = os.getenv("BIND_ADDR") or "0.0.0.0"
port = os.getenv("PORT") or 6667
register_cmd_handler("JOIN", 1, handle_join)
register_cmd_handler("NICK", 1, handle_nick)
register_cmd_handler("PRIVMSG", 2, handle_privmsg)
register_cmd_handler("USER", 4, handle_user)
irc_server = Server()
register_cmd_handlers()
async def register_client(reader: StreamReader, writer: StreamWriter) -> None:
client = Client(

Loading…
Cancel
Save