Added more commands

This commit is contained in:
Tristan B. Kildaire 2020-09-25 19:51:24 +02:00
parent 2684a68e6f
commit c1c3cf1cea
3 changed files with 108 additions and 5 deletions

View File

@ -5,6 +5,7 @@ import std.socket;
import client; import client;
import std.string : cmp, split, strip; import std.string : cmp, split, strip;
import std.conv : to; import std.conv : to;
import notifications;
void main() void main()
{ {
@ -16,8 +17,12 @@ void commandLine()
/* Current conneciton */ /* Current conneciton */
DClient client; DClient client;
NotificationWatcher d;
/* The current command */ /* The current command */
string commandLine; string commandLine;
string currentChannel;
while(true) while(true)
{ {
@ -47,6 +52,7 @@ void commandLine()
writeln("Connecting to "~to!(string)(addr)~"..."); writeln("Connecting to "~to!(string)(addr)~"...");
client = new DClient(addr); client = new DClient(addr);
d = new NotificationWatcher(client.getManager());
writeln("Connected!"); writeln("Connected!");
} }
/* If the command is `auth` */ /* If the command is `auth` */
@ -55,13 +61,44 @@ void commandLine()
string username = elements[1]; string username = elements[1];
string password = elements[2]; string password = elements[2];
client.auth(username, password); if(client.auth(username, password))
{
writeln("Auth good");
}
else
{
writeln("Auth bad");
}
}
/* If the command is `join` */
else if(cmp(command, "join") == 0)
{
string[] channels = elements[1..elements.length];
foreach(string channel; channels)
{
if(client.join(channel))
{
writeln("Already present in channel "~channel);
}
}
currentChannel = elements[elements.length-1];
}
/* If the command is `msg` */
else if(cmp(command, "open") == 0)
{
}
else
{
//client.sendMessage()
} }
} }
if(client) if(client)
{ {
client.disconnect();
} }
} }

View File

@ -28,16 +28,40 @@ public class DClient
writeln(manager.receiveMessage(1)); writeln(manager.receiveMessage(1));
} }
public void auth(string username, string password) public bool auth(string username, string password)
{ {
byte[] data = [0]; byte[] data = [0];
data ~= cast(byte)username.length; data ~= cast(byte)username.length;
data ~= username; data ~= username;
data ~= password; data ~= password;
writeln(data);
manager.sendMessage(1, data); manager.sendMessage(1, data);
byte[] resp = manager.receiveMessage(1); byte[] resp = manager.receiveMessage(1);
writeln("auth resp: "~to!(string)(resp));
return cast(bool)resp[0];
}
public bool join(string channel)
{
/* TODO: DO oneshot as protocol supports csv channels */
byte[] data = [3];
data ~= channel;
manager.sendMessage(1, data);
byte[] resp = manager.receiveMessage(1);
return cast(bool)resp[0];
}
public Manager getManager()
{
return manager;
}
public void sendMessage(string director, string message)
{
//
} }
public void disconnect() public void disconnect()

42
source/notifications.d Normal file
View File

@ -0,0 +1,42 @@
import core.thread : Thread;
import tristanable.manager;
import tristanable.notifications;
import std.stdio;
import core.time : dur;
public class NotificationWatcher : Thread
{
private Manager manager;
this(Manager manager)
{
super(&worker);
this.manager = manager;
manager.reserveTag(0);
start();
}
private void worker()
{
while(true)
{
/* Check for notifications every 2 seconds */
NotificationReply[] notifications =manager.popNotifications();
if(notifications.length)
{
writeln(notifications);
foreach(NotificationReply notificationReply; notifications)
{
writeln(notificationReply.getData());
string msg = cast(string)notificationReply.getData();
writeln("!> "~msg);
}
}
Thread.getThis().sleep(dur!("seconds")(2));
}
}
}