From 87eb1f02e71ce845712c8aeacc7dbb447e70d49c Mon Sep 17 00:00:00 2001 From: "Tristan B. Kildaire" Date: Tue, 29 Sep 2020 23:28:58 +0200 Subject: [PATCH] Implemented `memberlist` command --- source/dnetd/dchannel.d | 20 ++++++++++++++ source/dnetd/dconnection.d | 54 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/source/dnetd/dchannel.d b/source/dnetd/dchannel.d index 044e3a5..9e6c7cc 100644 --- a/source/dnetd/dchannel.d +++ b/source/dnetd/dchannel.d @@ -291,6 +291,26 @@ public class DChannel return true; } + /** + * Returns a list of all the members + */ + public DConnection[] getMembers() + { + /* Members list */ + DConnection[] memberList; + + /* Lock the members list */ + memberLock.lock(); + + memberList = members; + + /* Unlock the members list */ + memberLock.unlock(); + + return memberList; + } + + public override string toString() { string toStr; diff --git a/source/dnetd/dconnection.d b/source/dnetd/dconnection.d index 6017c68..6b9a5eb 100644 --- a/source/dnetd/dconnection.d +++ b/source/dnetd/dconnection.d @@ -41,6 +41,7 @@ public class DConnection : Thread LIST, MSG, MEMBER_COUNT, + MEMBER_LIST, UNKNOWN } @@ -484,6 +485,59 @@ public class DConnection : Thread /* Append the length */ reply ~= numberBytes; } + } + /* If `memberlist` command (requires: authed, client) */ + else if(command == Command.MEMBER_LIST && hasAuthed && connType == ConnectionType.CLIENT) + { + /* Status */ + bool status = true; + + /* Get the channel name */ + string channelName = cast(string)message.data[1..message.data.length]; + + /* Get the channel */ + DChannel channel = server.getChannelByName(channelName); + + /* Encode the status */ + reply ~= [channel !is null]; + + /* If the channel exists */ + if(channel) + { + /* Get the list of members in the channel */ + DConnection[] members = channel.getMembers(); + + /* Construct a CSV string of the members */ + string memberString; + + for(ulong i = 0; i < members.length; i++) + { + if(i == members.length-1) + { + memberString ~= members[i].getUsername(); + } + else + { + memberString ~= members[i].getUsername()~","; + } + } + + /* Encode the string into the reply */ + reply ~= cast(byte[])memberString; + } + /* If the channel does not exist */ + else + { + status = false; + } + + + + + + + + } /* If no matching built-in command was found */ else