1 module skoenlapper.configurator; 2 3 import std.json; 4 import gogga; 5 import std.stdio; 6 import std.socket : parseAddress, Address; 7 import std.string : split; 8 import std.conv : to; 9 10 public final class Configuration 11 { 12 13 private Account[] accounts; 14 15 this(JSONValue json) 16 { 17 /* Get each account */ 18 //JSONValue[] accountJSON = ["active"].array(); 19 20 accounts = Account.getAccounts(json["accounts"]); 21 22 // foreach(JSONValue account; accountJSON) 23 // { 24 // /* Get the active account's name (TODO: Key not found) */ 25 // string accountName = account.str(); 26 27 // /* Get ther account block */ 28 // JSONValue accountBlock = json["accounts"][accountName]; 29 30 // /* Generate the account info */ 31 // string username = accountBlock["auth"]["username"].str(); 32 // string password = accountBlock["auth"]["password"].str(); 33 // string server = accountBlock["server"].str(); 34 // accounts ~= new Account([username, password], server); 35 // } 36 } 37 38 public Account getAccount(ulong index) 39 { 40 return accounts[index]; 41 } 42 } 43 44 public JSONValue getConfiguration(string filename) 45 { 46 /* The JSON configuration file */ 47 JSONValue config; 48 49 /* Read the configuration file */ 50 File configFile; 51 /* TODO: Change this later */ 52 configFile.open(filename, "rb"); 53 byte[] configuration; 54 configuration.length = configFile.size(); 55 configuration = configFile.rawRead(configuration); 56 configFile.close(); 57 58 /* Parse the JSON */ 59 config = parseJSON(cast(string) configuration); 60 61 return config; 62 } 63 64 public final class Account 65 { 66 private string[] authenticationCredentials; 67 private string server; 68 private string mailbox; 69 70 this(string[] authenticationCredentials, string server) 71 { 72 this.authenticationCredentials = authenticationCredentials; 73 this.server = server; 74 } 75 76 public string getUsername() 77 { 78 return authenticationCredentials[0]; 79 } 80 81 public string getPassword() 82 { 83 return authenticationCredentials[1]; 84 } 85 86 public Address getServer() 87 { 88 return parseAddress(split(server, ":")[0], to!(ushort)(split(server, ":")[1])); 89 } 90 91 public string getMailbox() 92 { 93 return mailbox; 94 } 95 96 public static Account[] getAccounts(JSONValue accountBlock) 97 { 98 /* All the accounts generated */ 99 Account[] accounts; 100 101 /* Find all accounts */ 102 foreach (JSONValue account; accountBlock["active"].array()) 103 { 104 /* The account to activate */ 105 string accountName = account.str(); 106 107 /* Generate the account (TODO: Error handling for key not found) */ 108 accounts ~= getAccount(accountBlock[accountName]); 109 110 gprintln("Activated account '" ~ accountName ~ "'"); 111 } 112 113 return accounts; 114 } 115 116 private static Account getAccount(JSONValue accountBlock) 117 { 118 /* The generated account */ 119 Account account; 120 121 /* Get the authentication credentials (TODO: Key not found exception handling) */ 122 string[] authenticationCredentials; 123 authenticationCredentials ~= accountBlock["auth"]["username"].str(); 124 authenticationCredentials ~= accountBlock["auth"]["password"].str(); 125 126 /* Get the server (TODO: Key not found exception handling) */ 127 string server = accountBlock["server"].str(); 128 129 /* Generate the account */ 130 account = new Account(authenticationCredentials, server); 131 132 /* Set the mailbox */ 133 account.mailbox = accountBlock["mailDirectory"].str(); 134 135 return account; 136 } 137 }