|
@ -1,7 +1,7 @@ |
|
|
from paircd.client import Client |
|
|
from paircd.client import Client |
|
|
from paircd.command_handler import CommandHandler |
|
|
from paircd.command_handler import CommandHandler |
|
|
from paircd.message import Message |
|
|
from paircd.message import Message |
|
|
from paircd.reply import ERR_NICKNAMEINUSE |
|
|
|
|
|
|
|
|
from paircd.reply import ERR_NICKNAMEINUSE, ERR_NONICKNAMEGIVEN, NICK |
|
|
from paircd.server import Server |
|
|
from paircd.server import Server |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -11,6 +11,10 @@ class NickHandler(CommandHandler): |
|
|
|
|
|
|
|
|
async def handle(self, server: Server, client: Client, msg: Message) -> None: |
|
|
async def handle(self, server: Server, client: Client, msg: Message) -> None: |
|
|
nickname = msg.args[0] |
|
|
nickname = msg.args[0] |
|
|
|
|
|
if nickname == "": |
|
|
|
|
|
target = client.nickname if client.nickname else nickname |
|
|
|
|
|
client.write_message(ERR_NONICKNAMEGIVEN(target)) |
|
|
|
|
|
return |
|
|
|
|
|
|
|
|
existing_client = server.get_client_by_name(nickname) |
|
|
existing_client = server.get_client_by_name(nickname) |
|
|
if existing_client is not None: |
|
|
if existing_client is not None: |
|
@ -19,19 +23,22 @@ class NickHandler(CommandHandler): |
|
|
client.write_message(ERR_NICKNAMEINUSE(target, nickname)) |
|
|
client.write_message(ERR_NICKNAMEINUSE(target, nickname)) |
|
|
return |
|
|
return |
|
|
|
|
|
|
|
|
# Remove stale references if they exist |
|
|
|
|
|
if client.nickname: |
|
|
|
|
|
server.remove_client_by_name(client.nickname) |
|
|
|
|
|
for channel_name in client.channels: |
|
|
|
|
|
channel = server.get_channel_by_name(channel_name) |
|
|
|
|
|
channel.remove_client_by_nick(client.nickname) |
|
|
|
|
|
|
|
|
rename = client.nickname != "" |
|
|
|
|
|
old_nick = client.nickname |
|
|
|
|
|
old_id = client.id() |
|
|
|
|
|
|
|
|
client.nickname = nickname |
|
|
client.nickname = nickname |
|
|
|
|
|
if rename: |
|
|
|
|
|
server.rename_client(old_nick, nickname) |
|
|
|
|
|
else: |
|
|
|
|
|
server.add_client(client) |
|
|
|
|
|
|
|
|
# Add references for client |
|
|
|
|
|
server.add_client(client) |
|
|
|
|
|
for channel_name in client.channels: |
|
|
for channel_name in client.channels: |
|
|
channel = server.get_channel_by_name(channel_name) |
|
|
channel = server.get_channel_by_name(channel_name) |
|
|
channel.remove_client_by_nick(client.nickname) |
|
|
|
|
|
|
|
|
if rename: |
|
|
|
|
|
channel.rename_client(old_nick, nickname) |
|
|
|
|
|
channel.write_message(NICK(nickname, prefix=old_id)) |
|
|
|
|
|
else: |
|
|
|
|
|
raise RuntimeError("shouldn't be in any channels without a nick") |
|
|
|
|
|
|
|
|
client.register() |
|
|
client.register() |