Browse Source

Clean up MODE handler

master
Forest Belton 3 years ago
parent
commit
05d84b0c60
2 changed files with 29 additions and 9 deletions
  1. +5
    -1
      paircd/channel.py
  2. +24
    -8
      paircd/handler/mode.py

+ 5
- 1
paircd/channel.py View File

@ -10,6 +10,7 @@ from paircd.message import Message
class Channel: class Channel:
name: str name: str
topic: str = "" topic: str = ""
modes: Set[str] = field(default_factory=set)
clients_by_nick: Dict[str, Client] = field(default_factory=dict) clients_by_nick: Dict[str, Client] = field(default_factory=dict)
modes_by_nick: Dict[str, Set[str]] = field(default_factory=lambda: defaultdict(set)) modes_by_nick: Dict[str, Set[str]] = field(default_factory=lambda: defaultdict(set))
@ -30,7 +31,10 @@ class Channel:
client.write_message(msg) client.write_message(msg)
def get_modes(self) -> Set[str]: def get_modes(self) -> Set[str]:
return set()
return self.modes
def get_mode_settings(self) -> str:
return f"+{''.join(sorted(self.modes))}"
def get_user_modes_by_nick(self, nick: str) -> Set[str]: def get_user_modes_by_nick(self, nick: str) -> Set[str]:
return self.modes_by_nick[nick] return self.modes_by_nick[nick]

+ 24
- 8
paircd/handler/mode.py View File

@ -1,4 +1,3 @@
import logging
from paircd.reply import ERR_USERSDONTMATCH, MODE, RPL_CHANNELMODEIS, RPL_UMODEIS from paircd.reply import ERR_USERSDONTMATCH, MODE, RPL_CHANNELMODEIS, RPL_UMODEIS
from paircd.client import Client from paircd.client import Client
@ -13,19 +12,36 @@ class ModeHandler(CommandHandler):
async def handle(self, server: Server, client: Client, msg: Message) -> None: async def handle(self, server: Server, client: Client, msg: Message) -> None:
name = msg.args[0] name = msg.args[0]
if not name.startswith("#"):
if name == client.nickname:
settings = client.get_mode_settings()
client.write_message(RPL_UMODEIS(client.nickname, settings))
else:
client.write_message(ERR_USERSDONTMATCH(client.nickname))
if name.startswith("#"):
self.handle_channel(server, client, msg)
else:
self.handle_user(client, msg)
def handle_user(self, client: Client, msg: Message) -> None:
name = msg.args[0]
if name != client.nickname:
client.write_message(ERR_USERSDONTMATCH(client.nickname))
return return
# Set user mode
if len(msg.args) == 2:
client.log("TODO: implement user mode set")
settings = client.get_mode_settings()
client.write_message(RPL_UMODEIS(client.nickname, settings))
def handle_channel(self, server: Server, client: Client, msg: Message) -> None:
name = msg.args[0]
channel = server.get_channel_by_name(name) channel = server.get_channel_by_name(name)
if len(msg.args) == 2:
client.log("TODO: implement channel mode set")
modes = "".join(channel.get_modes()) modes = "".join(channel.get_modes())
if modes != "": if modes != "":
modes = f"+{modes}" modes = f"+{modes}"
client.log("TODO: implement channel modes", level=logging.WARN)
client.write_message(MODE(name, modes)) client.write_message(MODE(name, modes))
client.write_message(RPL_CHANNELMODEIS(client.nickname, name, modes, "")) client.write_message(RPL_CHANNELMODEIS(client.nickname, name, modes, ""))

Loading…
Cancel
Save