1 /**
2 * DConfig
3 *
4 * Represents all configuration parameters
5 */
6 module dnetd.dconfig;
7 
8 import std.json;
9 import std.conv;
10 import std.socket : Address, parseAddress;
11 
12 public final class DConfig
13 {
14     /* General configuration */
15     private DGeneralConfig generalConfig;
16 
17     /* Link configuration */
18     private DLinkConfig linksConfig;
19 
20     private this()
21     {
22         /* TODO: */
23     }
24 
25     public DGeneralConfig getGeneral()
26     {
27         return generalConfig;
28     }
29 
30     public DLinkConfig getLinks()
31     {
32         return linksConfig;
33     }
34 
35     public static DConfig getConfig(JSONValue json)
36     {
37         /* The newly created configuration */
38         DConfig config = new DConfig();
39 
40         try
41         {
42             /* TODO: Parse */
43 
44             /* Get the `general` block */
45             JSONValue generalBlock = json["general"];
46             config.generalConfig = DGeneralConfig.getConfig(generalBlock);
47 
48             /* Get the `links` block */
49             //JSONValue linksBlock = json["links"];
50             //config.linksConfig = DLinkConfig.getConfig(linksBlock);
51         }
52         catch(JSONException e)
53         {
54             /* Set config to null (signals an error) */
55             config = null;
56         }
57 
58         return config;
59     }
60 
61     public JSONValue saveConfig()
62     {
63         JSONValue config;
64 
65 
66         return config;
67     }
68 }
69 
70 public final class DGeneralConfig
71 {
72 
73     /* Addresses to bind sockets to */
74     private string[] addresses;
75     private ushort port;
76 
77     /* Server information */
78     private string network;
79     private string name;
80     private string motd;
81 
82     private this()
83     {
84 
85     }
86 
87     public static DGeneralConfig getConfig(JSONValue generalBlock)
88     {
89         /* The generated general config */
90         DGeneralConfig config = new DGeneralConfig();
91 
92         try
93         {
94             /* Set the addresses */
95             foreach(JSONValue address; generalBlock["addresses"].array())
96             {
97                 config.addresses ~= [address.str()];
98             }
99             
100             /* Set the ports */
101             config.port = to!(ushort)(generalBlock["port"].str());
102 
103             /* Set the network name */
104             config.network = generalBlock["network"].str();
105 
106             /* Set the server name */
107             config.name = generalBlock["name"].str();
108 
109             /* Set the message of the day */
110             config.motd = generalBlock["motd"].str();
111 
112         }
113         catch(JSONException e)
114         {
115             /* Set the config to null (signals an error) */
116             config = null;
117         }
118 
119 
120         return config;
121     }
122 
123     public string getMotd()
124     {
125         return motd;
126     }
127 
128     public Address getAddress()
129     {
130         /* TODO: Add multi address support later */
131         return parseAddress(addresses[0], port);
132     }
133 }
134 
135 public final class DLinkConfig
136 {
137 
138 }