Fix numeric replies

This commit is contained in:
Les De Ridder 2017-03-14 17:19:01 +01:00
parent 9758e5a545
commit 6c0f6aa72d
No known key found for this signature in database
GPG Key ID: 5EC132DFA85DB372
3 changed files with 33 additions and 12 deletions

View File

@ -34,7 +34,7 @@ class Channel
{ {
enum channelType = "="; //TODO: Support secret and private channels enum channelType = "="; //TODO: Support secret and private channels
connection.send(Message(_server.name, "353", [channelType, name, members.map!(m => m.nick).join(' ')], true)); connection.send(Message(_server.name, "353", [connection.nick, channelType, name, members.map!(m => m.nick).join(' ')], true));
connection.send(Message(_server.name, "366", [name, "End of NAMES list"], true)); connection.send(Message(_server.name, "366", [connection.nick, name, "End of NAMES list"], true));
} }
} }

View File

@ -27,7 +27,7 @@ class Connection
@property string mask() { return nick ~ "!" ~ user ~ "@" ~ hostname; } @property string mask() { return nick ~ "!" ~ user ~ "@" ~ hostname; }
@property Channel[] channels() { return _server.channels.filter!(c => c.members.canFind(this)).array; } @property auto channels() { return _server.channels.filter!(c => c.members.canFind(this)); }
bool connected; bool connected;
@ -39,6 +39,16 @@ class Connection
connected = _connection.connected; connected = _connection.connected;
} }
override int opCmp(Object o)
{
Connection other;
if((other = cast(Connection)other) !is null)
{
return cmp(nick, other.nick);
}
return 0;
}
void send(Message message) void send(Message message)
{ {
string messageString = message.toString; string messageString = message.toString;
@ -46,6 +56,20 @@ class Connection
_connection.write(messageString ~ "\r\n"); _connection.write(messageString ~ "\r\n");
} }
//sends the message to all clients who have a channel in common with this client
void sendToPeers(Message message)
{
if(channels.empty)
{
return;
}
foreach(connection; channels.map!(c => c.members).fold!((a, b) => a ~ b).sort().uniq.filter!(c => c != this))
{
connection.send(message);
}
}
void onDisconnect() void onDisconnect()
{ {
writeln("client disconnected"); writeln("client disconnected");
@ -96,7 +120,7 @@ class Connection
break; break;
default: default:
writeln("unknown command '", message.command, "'"); writeln("unknown command '", message.command, "'");
send(Message(_server.name, "421", ["Unknown command"])); send(Message(_server.name, "421", [nick, "Unknown command"]));
break; break;
} }
} }
@ -109,10 +133,7 @@ class Connection
auto newNick = message.parameters[0]; auto newNick = message.parameters[0];
if(nick !is null) if(nick !is null)
{ {
foreach(connection; channels.map!(c => c.members).fold!((a, b) => a ~ b).sort().uniq.filter!(c => c != this)) sendToPeers(Message(nick, "NICK", [newNick]));
{
connection.send(Message(nick, "NICK", [newNick]));
}
send(Message(nick, "NICK", [newNick])); send(Message(nick, "NICK", [newNick]));
} }
@ -155,7 +176,7 @@ class Connection
auto channel = message.parameters[0]; auto channel = message.parameters[0];
if(!Server.isValidChannelName(channel)) if(!Server.isValidChannelName(channel))
{ {
send(Message(_server.name, "403", [channel, "No such channel"], true)); send(Message(_server.name, "403", [nick, channel, "No such channel"], true));
} }
else else
{ {
@ -170,11 +191,11 @@ class Connection
auto channel = message.parameters[0]; auto channel = message.parameters[0];
if(!Server.isValidChannelName(channel)) if(!Server.isValidChannelName(channel))
{ {
send(Message(_server.name, "403", [channel, "No such channel"], true)); send(Message(_server.name, "403", [nick, channel, "No such channel"], true));
} }
else if(!channels.canFind!(c => c.name == channel)) else if(!channels.canFind!(c => c.name == channel))
{ {
send(Message(_server.name, "442", [channel, "You're not on that channel"], true)); send(Message(_server.name, "442", [nick, channel, "You're not on that channel"], true));
} }
else else
{ {

View File

@ -83,7 +83,7 @@ class Server
void part(Connection connection, string channelName, string partMessage) void part(Connection connection, string channelName, string partMessage)
{ {
auto channel = connection.channels.find!(c => c.name == channelName)[0]; auto channel = connection.channels.array.find!(c => c.name == channelName)[0];
foreach(member; channel.members) foreach(member; channel.members)
{ {