skippy/source/app.d

236 lines
4.5 KiB
D
Raw Normal View History

2020-09-24 17:20:11 +00:00
import std.stdio;
import tristanable.manager;
import std.socket;
2020-09-24 23:08:51 +00:00
import client;
import std.string : cmp, split, strip;
import std.conv : to;
2020-09-25 17:51:24 +00:00
import notifications;
2020-09-26 16:40:31 +00:00
import std.file;
import std.json;
JSONValue config;
/* Current conneciton */
DClient dclient;
2020-09-24 17:20:11 +00:00
NotificationWatcher dnotifications;
2020-09-24 17:20:11 +00:00
void main()
{
/* If the configuration file exists */
if(exists("config.example")) /* TODO: Change */
2020-09-26 16:40:31 +00:00
{
/* Load the config */
loadConfig("config.example");
2020-09-26 16:40:31 +00:00
}
/* If the configuration file doesn't exist */
2020-09-26 16:40:31 +00:00
else
{
/* Set default config */
defaultConfig();
}
/* Start the REPL */
2020-09-24 23:08:51 +00:00
commandLine();
}
void clientAuth(string username, string password)
2020-09-24 23:08:51 +00:00
{
if(dclient.auth(username, password))
{
writeln("Auth good");
}
else
{
writeln("Auth bad");
}
}
2020-09-24 23:08:51 +00:00
void commandLine()
{
2020-09-25 17:51:24 +00:00
2020-09-24 23:08:51 +00:00
/* The current command */
string commandLine;
2020-09-25 17:51:24 +00:00
string currentChannel;
2020-09-24 23:08:51 +00:00
while(true)
{
/* Read in a command line */
write("> ");
commandLine = readln();
2020-09-24 17:20:11 +00:00
2020-09-24 23:08:51 +00:00
if(cmp(strip(commandLine), "") == 0)
{
continue;
}
2020-09-24 17:20:11 +00:00
2020-09-24 23:08:51 +00:00
string[] elements = split(commandLine);
string command = elements[0];
/* If the command is `exit` */
if(cmp(command, "exit") == 0)
{
break;
}
/* If the command is `connect` */
else if(cmp(command, "connect") == 0)
{
2020-09-26 17:18:09 +00:00
string address;
string port;
Address addr;
bool isConfigConnect;
2020-09-26 17:18:09 +00:00
2020-09-26 16:40:31 +00:00
/* If there is only one argument then it is a server name */
if(elements.length == 2)
{
string serverName = elements[1];
try
{
/* Get the address and port */
JSONValue serverInfo = config["servers"][serverName];
2020-09-26 17:18:09 +00:00
address = serverInfo["address"].str();
port = serverInfo["port"].str();
isConfigConnect = true;
2020-09-26 16:40:31 +00:00
}
catch(JSONException e)
{
writeln("Could not find server: "~to!(string)(e));
2020-09-26 17:18:09 +00:00
continue;
2020-09-26 16:40:31 +00:00
}
}
/* Then it must be `<address> <port>` */
else if(elements.length == 3)
{
2020-09-26 17:18:09 +00:00
address = elements[1];
port = elements[2];
2020-09-26 16:40:31 +00:00
}
/* Syntax error */
else
{
writeln("Syntax error");
2020-09-26 17:18:09 +00:00
continue;
2020-09-26 16:40:31 +00:00
}
2020-09-26 17:18:09 +00:00
/* TODO: How many are rtuend and which to use ? */
addr = getAddress(address, to!(ushort)(port))[0];
2020-09-26 17:18:09 +00:00
writeln("Connecting to "~to!(string)(addr)~"...");
dclient = new DClient(addr);
dnotifications= new NotificationWatcher(dclient.getManager());
2020-09-26 17:18:09 +00:00
writeln("Connected!");
if(isConfigConnect)
{
string server = elements[1];
string username = config["servers"][server]["auth"]["username"].str();
string password = config["servers"][server]["auth"]["password"].str();
/* Authenticate the user */
clientAuth(username, password);
/* Auto join config */
configAutoJoin(server);
}
2020-09-24 23:08:51 +00:00
}
/* If the command is `auth` */
else if(cmp(command, "auth") == 0)
{
string username = elements[1];
string password = elements[2];
2020-09-24 17:20:11 +00:00
/* Authenticate the user */
clientAuth(username, password);
2020-09-25 17:51:24 +00:00
}
2020-09-25 20:16:23 +00:00
/* If the command is `list` */
else if(cmp(command, "list") == 0 || cmp(command, "l") == 0)
2020-09-25 20:16:23 +00:00
{
string[] channels = dclient.list();
2020-09-26 16:40:31 +00:00
writeln("Channels ("~to!(string)(channels.length)~" total)\n");
foreach(string channel; channels)
{
writeln("\t"~channel);
}
2020-09-25 20:16:23 +00:00
}
2020-09-25 17:51:24 +00:00
/* If the command is `join` */
else if(cmp(command, "join") == 0)
{
string[] channels = elements[1..elements.length];
foreach(string channel; channels)
{
if(dclient.join(channel))
2020-09-25 17:51:24 +00:00
{
writeln("Already present in channel "~channel);
}
}
currentChannel = elements[elements.length-1];
}
/* If the command is `msg` */
else if(cmp(command, "open") == 0)
{
}
else
{
dclient.sendMessage(false, currentChannel, strip(commandLine));
2020-09-24 23:08:51 +00:00
}
}
2020-09-24 17:20:11 +00:00
if(dclient)
2020-09-24 23:08:51 +00:00
{
dclient.disconnect();
2020-09-24 23:08:51 +00:00
}
2020-09-24 17:20:11 +00:00
}
2020-09-26 16:40:31 +00:00
void configAutoJoin(string server)
{
foreach(JSONValue value; config["servers"][server]["channels"].array())
{
string channel = value.str();
if(dclient.join(channel))
{
writeln("Already present in channel "~channel);
}
}
}
2020-09-26 16:40:31 +00:00
void defaultConfig()
{
/* Server block */
JSONValue serverBlock;
/* TODO: Remove test servers? */
2020-09-26 17:18:09 +00:00
JSONValue dserv;
dserv["address"] = "127.0.0.1";
dserv["port"] = "7777";
2020-09-26 20:53:29 +00:00
// JSONValue[] joins = []
// dserv["joins"] =
2020-09-26 17:18:09 +00:00
serverBlock["dserv"] = dserv;
2020-09-26 16:40:31 +00:00
config["servers"] = serverBlock;
}
void loadConfig(string configPath)
{
2020-09-27 11:41:09 +00:00
/* Open the provided configuration file */
2020-09-26 20:53:29 +00:00
File file;
file.open(configPath);
2020-09-27 11:41:09 +00:00
/* Read the configuration file */
2020-09-26 20:53:29 +00:00
byte[] buffer;
buffer.length = file.size();
buffer = file.rawRead(buffer);
2020-09-27 11:41:09 +00:00
/* Close the file */
file.close();
2020-09-27 11:41:09 +00:00
/* Parse the JSON of the configuration file */
config = parseJSON(cast(string)buffer);
2020-09-26 16:40:31 +00:00
}