Check nick availability

This commit is contained in:
Les De Ridder 2017-03-17 02:17:23 +01:00
parent dfe9e65786
commit 10bd22734d
No known key found for this signature in database
GPG Key ID: 5EC132DFA85DB372
2 changed files with 23 additions and 1 deletions

View File

@ -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);

View File

@ -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);