mirror of
https://github.com/deavminet/dnetd
synced 2024-09-21 09:43:37 +02:00
Added 'membercount' command
This commit is contained in:
parent
978f02cff3
commit
5ef44b3d9b
@ -20,6 +20,7 @@ import std.string : split;
|
|||||||
import dnetd.dchannel : DChannel;
|
import dnetd.dchannel : DChannel;
|
||||||
import std.conv : to;
|
import std.conv : to;
|
||||||
import std.stdio : writeln;
|
import std.stdio : writeln;
|
||||||
|
import std.algorithm : reverse;
|
||||||
|
|
||||||
public class DConnection : Thread
|
public class DConnection : Thread
|
||||||
{
|
{
|
||||||
@ -39,6 +40,7 @@ public class DConnection : Thread
|
|||||||
REGISTER,
|
REGISTER,
|
||||||
LIST,
|
LIST,
|
||||||
MSG,
|
MSG,
|
||||||
|
MEMBER_COUNT,
|
||||||
UNKNOWN
|
UNKNOWN
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -233,6 +235,10 @@ public class DConnection : Thread
|
|||||||
{
|
{
|
||||||
command = Command.MSG;
|
command = Command.MSG;
|
||||||
}
|
}
|
||||||
|
else if(commandByte == cast(ulong)8)
|
||||||
|
{
|
||||||
|
command = Command.MEMBER_COUNT;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -440,6 +446,45 @@ public class DConnection : Thread
|
|||||||
/* TODO: */
|
/* TODO: */
|
||||||
reply = [status];
|
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 */
|
/* If no matching built-in command was found */
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -474,6 +519,37 @@ public class DConnection : Thread
|
|||||||
return true;
|
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
|
* Send user a message
|
||||||
*
|
*
|
||||||
@ -505,11 +581,9 @@ public class DConnection : Thread
|
|||||||
protocolData ~= cast(byte[])message;
|
protocolData ~= cast(byte[])message;
|
||||||
|
|
||||||
/* Send the messge */
|
/* Send the messge */
|
||||||
user.writeSocket(0, protocolData);
|
bool sendStatus = user.writeSocket(0, protocolData);
|
||||||
|
|
||||||
|
return sendStatus;
|
||||||
/* TODO: Return value should be based off message send success */
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
/* If the user was not found */
|
/* If the user was not found */
|
||||||
else
|
else
|
||||||
|
@ -274,6 +274,26 @@ public class DServer : Thread
|
|||||||
return currentConnections;
|
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()
|
public DChannel[] getChannels()
|
||||||
{
|
{
|
||||||
/* The current channels list */
|
/* The current channels list */
|
||||||
|
Loading…
Reference in New Issue
Block a user