diff --git a/source/ircd/channel.d b/source/ircd/channel.d index c9e5558..4b5ccb5 100644 --- a/source/ircd/channel.d +++ b/source/ircd/channel.d @@ -34,7 +34,7 @@ class Channel { 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, "366", [name, "End of NAMES list"], true)); + connection.send(Message(_server.name, "353", [connection.nick, channelType, name, members.map!(m => m.nick).join(' ')], true)); + connection.send(Message(_server.name, "366", [connection.nick, name, "End of NAMES list"], true)); } } diff --git a/source/ircd/connection.d b/source/ircd/connection.d index abad70a..e9dc6bd 100644 --- a/source/ircd/connection.d +++ b/source/ircd/connection.d @@ -27,7 +27,7 @@ class Connection @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; @@ -39,6 +39,16 @@ class Connection 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) { string messageString = message.toString; @@ -46,6 +56,20 @@ class Connection _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() { writeln("client disconnected"); @@ -96,7 +120,7 @@ class Connection break; default: writeln("unknown command '", message.command, "'"); - send(Message(_server.name, "421", ["Unknown command"])); + send(Message(_server.name, "421", [nick, "Unknown command"])); break; } } @@ -109,10 +133,7 @@ class Connection auto newNick = message.parameters[0]; if(nick !is null) { - foreach(connection; channels.map!(c => c.members).fold!((a, b) => a ~ b).sort().uniq.filter!(c => c != this)) - { - connection.send(Message(nick, "NICK", [newNick])); - } + sendToPeers(Message(nick, "NICK", [newNick])); send(Message(nick, "NICK", [newNick])); } @@ -155,7 +176,7 @@ class Connection auto channel = message.parameters[0]; 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 { @@ -170,11 +191,11 @@ class Connection auto channel = message.parameters[0]; 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)) { - 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 { diff --git a/source/ircd/server.d b/source/ircd/server.d index f7a95da..bf90c07 100644 --- a/source/ircd/server.d +++ b/source/ircd/server.d @@ -83,7 +83,7 @@ class Server 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) {