Added hostname resolution to configuration file, also set address family passed on passed in Address

This commit is contained in:
Tristan B. Kildaire 2020-09-27 13:02:37 +02:00
parent ce9da7484c
commit ecb6676734
3 changed files with 63 additions and 25 deletions

View File

@ -1,7 +1,7 @@
{ {
"servers" : { "servers" : {
"dserv" : { "dserv" : {
"address" : "127.0.0.1", "address" : "memyselfandi",
"port" : "7777", "port" : "7777",
"auth" : { "auth" : {
"username" : "deavmi", "username" : "deavmi",

View File

@ -11,15 +11,20 @@ import std.json;
JSONValue config; JSONValue config;
/* Current conneciton */
DClient dclient;
NotificationWatcher dnotifications;
void main() void main()
{ {
/* Check if the default config exists */ /* If the configuration file exists */
if(exists("/home/deavmi/.config/skippy/config")) /* TODO: Change */ if(exists("config.example")) /* TODO: Change */
{ {
/* Load the config */ /* Load the config */
loadConfig("/home/deavmi/.config/skippy/config"); loadConfig("config.example");
} }
/* If the configuration file doesn't exist */
else else
{ {
/* Set default config */ /* Set default config */
@ -30,12 +35,21 @@ void main()
commandLine(); commandLine();
} }
void clientAuth(string username, string password)
{
if(dclient.auth(username, password))
{
writeln("Auth good");
}
else
{
writeln("Auth bad");
}
}
void commandLine() void commandLine()
{ {
/* Current conneciton */
DClient client;
NotificationWatcher d;
/* The current command */ /* The current command */
string commandLine; string commandLine;
@ -67,6 +81,7 @@ void commandLine()
string address; string address;
string port; string port;
Address addr; Address addr;
bool isConfigConnect;
/* If there is only one argument then it is a server name */ /* If there is only one argument then it is a server name */
if(elements.length == 2) if(elements.length == 2)
@ -79,6 +94,8 @@ void commandLine()
JSONValue serverInfo = config["servers"][serverName]; JSONValue serverInfo = config["servers"][serverName];
address = serverInfo["address"].str(); address = serverInfo["address"].str();
port = serverInfo["port"].str(); port = serverInfo["port"].str();
isConfigConnect = true;
} }
catch(JSONException e) catch(JSONException e)
{ {
@ -99,11 +116,26 @@ void commandLine()
continue; continue;
} }
addr = parseAddress(address, to!(ushort)(port)); /* TODO: How many are rtuend and which to use ? */
addr = getAddress(address, to!(ushort)(port))[0];
writeln("Connecting to "~to!(string)(addr)~"..."); writeln("Connecting to "~to!(string)(addr)~"...");
client = new DClient(addr); dclient = new DClient(addr);
d = new NotificationWatcher(client.getManager()); dnotifications= new NotificationWatcher(dclient.getManager());
writeln("Connected!"); 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);
}
} }
/* If the command is `auth` */ /* If the command is `auth` */
else if(cmp(command, "auth") == 0) else if(cmp(command, "auth") == 0)
@ -111,19 +143,13 @@ void commandLine()
string username = elements[1]; string username = elements[1];
string password = elements[2]; string password = elements[2];
if(client.auth(username, password)) /* Authenticate the user */
{ clientAuth(username, password);
writeln("Auth good");
}
else
{
writeln("Auth bad");
}
} }
/* If the command is `list` */ /* If the command is `list` */
else if(cmp(command, "list") == 0) else if(cmp(command, "list") == 0 || cmp(command, "l") == 0)
{ {
string[] channels = client.list(); string[] channels = dclient.list();
writeln("Channels ("~to!(string)(channels.length)~" total)\n"); writeln("Channels ("~to!(string)(channels.length)~" total)\n");
foreach(string channel; channels) foreach(string channel; channels)
{ {
@ -137,7 +163,7 @@ void commandLine()
foreach(string channel; channels) foreach(string channel; channels)
{ {
if(client.join(channel)) if(dclient.join(channel))
{ {
writeln("Already present in channel "~channel); writeln("Already present in channel "~channel);
} }
@ -156,13 +182,25 @@ void commandLine()
} }
} }
if(client) if(dclient)
{ {
client.disconnect(); dclient.disconnect();
} }
} }
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);
}
}
}
void defaultConfig() void defaultConfig()
{ {
/* Server block */ /* Server block */

View File

@ -14,7 +14,7 @@ public class DClient
this(Address address) this(Address address)
{ {
/* Initialize the socket */ /* Initialize the socket */
Socket socket = new Socket(AddressFamily.INET, SocketType.STREAM, ProtocolType.TCP); Socket socket = new Socket(address.addressFamily, SocketType.STREAM, ProtocolType.TCP);
socket.connect(address); socket.connect(address);
/* Initialize the manager */ /* Initialize the manager */