1
0
mirror of https://github.com/deavminet/dnetd synced 2024-09-21 09:43:37 +02:00

Added 'membercount' command

This commit is contained in:
Tristan B. Kildaire 2020-09-29 14:29:46 +02:00
parent 978f02cff3
commit 5ef44b3d9b
2 changed files with 99 additions and 5 deletions

View File

@ -20,6 +20,7 @@ import std.string : split;
import dnetd.dchannel : DChannel;
import std.conv : to;
import std.stdio : writeln;
import std.algorithm : reverse;
public class DConnection : Thread
{
@ -39,6 +40,7 @@ public class DConnection : Thread
REGISTER,
LIST,
MSG,
MEMBER_COUNT,
UNKNOWN
}
@ -233,6 +235,10 @@ public class DConnection : Thread
{
command = Command.MSG;
}
else if(commandByte == cast(ulong)8)
{
command = Command.MEMBER_COUNT;
}
@ -440,6 +446,45 @@ public class DConnection : Thread
/* TODO: */
reply = [status];
}
/* If `membercount` command (requires: authed, client) */
else if(command == Command.MEMBER_COUNT && hasAuthed && connType == ConnectionType.CLIENT)
{
/* Status */
bool status = true;
/* Get the channel name */
string channelName = cast(string)message.data[1..message.data.length];
/* The memebr count */
long memberCount;
/* Get the member count */
status = getMemberCount(channelName, memberCount);
/* Encode the status */
reply = [status];
/* If there was no error fetching the member count */
if(status)
{
/* Data bytes */
byte[] numberBytes;
numberBytes.length = 8;
/* Encode the length (Big Endian) from Little Endian */
numberBytes[0] = *((cast(byte*)&memberCount)+7);
numberBytes[1] = *((cast(byte*)&memberCount)+6);
numberBytes[2] = *((cast(byte*)&memberCount)+5);
numberBytes[3] = *((cast(byte*)&memberCount)+4);
numberBytes[4] = *((cast(byte*)&memberCount)+3);
numberBytes[5] = *((cast(byte*)&memberCount)+2);
numberBytes[6] = *((cast(byte*)&memberCount)+1);
numberBytes[7] = *((cast(byte*)&memberCount)+0);
/* Append the length */
reply ~= numberBytes;
}
}
/* If no matching built-in command was found */
else
{
@ -474,6 +519,37 @@ public class DConnection : Thread
return true;
}
/**
* Get member count
*
* Gets the member count of a given channel
*/
private bool getMemberCount(string channelName, ref long count)
{
/* Status of operation */
bool status;
/* The channel */
DChannel channel = server.getChannelByName(channelName);
/* Check if the channel exists */
if(channel)
{
/* Get the channel count */
count = channel.getMemberCount();
status = true;
}
/* If the channel does not exist */
else
{
status = false;
}
return status;
}
/**
* Send user a message
*
@ -505,11 +581,9 @@ public class DConnection : Thread
protocolData ~= cast(byte[])message;
/* Send the messge */
user.writeSocket(0, protocolData);
/* TODO: Return value should be based off message send success */
return true;
bool sendStatus = user.writeSocket(0, protocolData);
return sendStatus;
}
/* If the user was not found */
else

View File

@ -274,6 +274,26 @@ public class DServer : Thread
return currentConnections;
}
public bool channelExists(string channelName)
{
/* Whether or not it exists */
bool exists;
/* Get all channels */
DChannel[] currentChannels = getChannels();
foreach(DChannel currentChannel; currentChannels)
{
if(cmp(currentChannel.getName(), channelName) == 0)
{
exists = true;
break;
}
}
return exists;
}
public DChannel[] getChannels()
{
/* The current channels list */