From 10bd22734ddd324f10b04c64c52afa5bfeb9a44f Mon Sep 17 00:00:00 2001 From: Les De Ridder Date: Fri, 17 Mar 2017 02:17:23 +0100 Subject: [PATCH] Check nick availability --- source/ircd/connection.d | 19 ++++++++++++++++++- source/ircd/server.d | 5 +++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/source/ircd/connection.d b/source/ircd/connection.d index 86b001a..8764a2f 100644 --- a/source/ircd/connection.d +++ b/source/ircd/connection.d @@ -133,7 +133,19 @@ class Connection void onNick(Message message) { + if(message.parameters.length == 0) + { + sendErrNoNickGiven(); + } + auto newNick = message.parameters[0]; + + if(!_server.isNickAvailable(newNick)) + { + send(Message(_server.name, "433", [nick, newNick, "Nickname already in use"])); + return; + } + if(nick !is null) { sendToPeers(Message(nick, "NICK", [newNick])); @@ -254,7 +266,7 @@ class Connection } else { - //is this the right response? + //is this the right reply? sendErrNoSuchNick(target); } } @@ -269,6 +281,11 @@ class Connection send(Message(_server.name, "401", [nick, name, "No such nick/channel"], true)); } + void sendErrNoNickGiven() + { + send(Message(_server.name, "431", [nick, "No nickname given"], true)); + } + string getHost() { auto address = parseAddress(_connection.peerAddress); diff --git a/source/ircd/server.d b/source/ircd/server.d index 4412070..81309c8 100644 --- a/source/ircd/server.d +++ b/source/ircd/server.d @@ -64,6 +64,11 @@ class Server return !name.startsWith('#') && !name.startsWith('&') && name.length <= 9; } + bool isNickAvailable(string nick) + { + return !connections.canFind!(c => c.nick == nick); + } + void join(Connection connection, string channelName) { auto channelRange = channels.find!(c => c.name == channelName);