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

Added functionality to send message to everyone in a channel

This commit is contained in:
Tristan B. Kildaire 2020-09-24 13:28:09 +02:00
parent 7caf0aa22f
commit 59d4dd1f25
2 changed files with 83 additions and 1 deletions

View File

@ -37,6 +37,9 @@ public class DChannel
return name; return name;
} }
/**
* Joins the given client to this channel
*/
public void join(DConnection client) public void join(DConnection client)
{ {
/* Lock the members list */ /* Lock the members list */
@ -54,6 +57,9 @@ public class DChannel
memberLock.unlock(); memberLock.unlock();
} }
/**
* Removes the given client from this channel
*/
public void leave(DConnection client) public void leave(DConnection client)
{ {
/* Lock the members list */ /* Lock the members list */
@ -78,6 +84,25 @@ public class DChannel
memberLock.unlock(); memberLock.unlock();
} }
public void sendMessage(DConnection sender, string message)
{
/* TODO: Generate message */
/* TODO: Spec out in protocol */
/* TODO: Reserved tag 0 for notifications */
byte[] msg;
/* Send the message to everyone else in the channel */
foreach(DConnection member; members)
{
/* Skip sending to self */
if(!(member is sender))
{
/* Send the message */
member.writeSocket(0, msg);
}
}
}
public override string toString() public override string toString()
{ {
string toStr; string toStr;

View File

@ -110,7 +110,7 @@ public class DConnection : Thread
* socket to the client/server, and unlocks the * socket to the client/server, and unlocks the
* mutex * mutex
*/ */
private bool writeSocket(long tag, byte[] data) public bool writeSocket(long tag, byte[] data)
{ {
/* Send status */ /* Send status */
bool status; bool status;
@ -265,6 +265,63 @@ public class DConnection : Thread
/* TODO: Implement me, use return value */ /* TODO: Implement me, use return value */
writeSocket(tag, reply); writeSocket(tag, reply);
} }
/* If `msg` command (requires: authed) */
else if(commandByte == 7 && hasAuthed)
{
/* Status */
bool status = true;
/* Get the type of message */
byte messageType = message.data[0];
/* Get the channel/person name */
string destination;
ulong i = 0;
while(message.data[1+i] != cast(byte)0)
{
destination ~= message.data[1+i];
i++;
}
/* Get the message (offset from null-terminator, hence +1 at the end) */
string msg = cast(string)message.data[1+i+1..message.data.length];
/* If we are sending to a user */
if(messageType == cast(byte)0)
{
/* TODO Implemet me */
}
/* If we are sending to a channel */
else if(messageType == cast(ubyte)1)
{
/* If the channel exists */
if(server.getChannelByName(destination))
{
/* TODO Implemet me */
}
/* If the channel does not exist */
else
{
status = false;
}
}
/* Unknown destination type */
else
{
status = false;
}
/* TODO: Handling here, should we make the user wait? */
/* Encode the reply */
// byte[] reply = [status];
// reply ~= channelList;
/* TODO: Implement me, use return value */
// writeSocket(tag, reply);
}
/* TODO: Handle this case */ /* TODO: Handle this case */
else else
{ {