Browse Source

Don't allow nicknames to be used more than once

master
Forest Belton 2 years ago
parent
commit
9ecafe1159
3 changed files with 13 additions and 1 deletions
  1. +8
    -0
      paircd/handler/nick.py
  2. +1
    -0
      paircd/reply.py
  3. +4
    -1
      paircd/server.py

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

@ -1,6 +1,7 @@
from paircd.client import Client
from paircd.command_handler import CommandHandler
from paircd.message import Message
from paircd.reply import ERR_NICKNAMEINUSE
from paircd.server import Server
@ -11,6 +12,13 @@ class NickHandler(CommandHandler):
async def handle(self, server: Server, client: Client, msg: Message) -> None:
nickname = msg.args[0]
existing_client = server.get_client_by_name(nickname)
if existing_client is not None:
if client != existing_client:
target = client.nickname if client.nickname else nickname
client.write_message(ERR_NICKNAMEINUSE(target, nickname))
return
# Remove stale references if they exist
if client.nickname:
server.remove_client_by_name(client.nickname)

+ 1
- 0
paircd/reply.py View File

@ -35,6 +35,7 @@ ERR_NOSUCHSERVER = reply_fn(402, "{0} :No such server")
ERR_NOSUCHCHANNEL = reply_fn(403, "{0} :No such channel")
ERR_CANNOTSENDTOCHAN = reply_fn(404, "{0} :Cannot send to channel")
ERR_TOOMANYCHANNELS = reply_fn(405, "{0} :You have joined too many channels")
ERR_NICKNAMEINUSE = reply_fn(433, "{0} :Nickname is already in use")
ERR_NOTEXTTOSEND = reply_fn(412, ":No text to send")
ERR_NOTREGISTERED = reply_fn(451, ":You have not registered")

+ 4
- 1
paircd/server.py View File

@ -1,6 +1,6 @@
from dataclasses import dataclass, field
from datetime import datetime
from typing import Dict
from typing import Dict, Optional
from paircd.client import Client
from paircd.channel import Channel
@ -20,5 +20,8 @@ class Server:
self.channels_by_name[name] = Channel(name=name)
return self.channels_by_name[name]
def get_client_by_name(self, name: str) -> Optional[Client]:
return self.clients_by_nick.get(name)
def remove_client_by_name(self, name: str) -> None:
del self.clients_by_nick[name]

Loading…
Cancel
Save