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 import gogga;
12 
13 public final class DConfig
14 {
15     /* General configuration */
16     private DGeneralConfig generalConfig;
17 
18     /* Link configuration */
19     private DLinkConfig linksConfig;
20 
21     private this()
22     {
23         /* TODO: */
24     }
25 
26     public DGeneralConfig getGeneral()
27     {
28         return generalConfig;
29     }
30 
31     public DLinkConfig getLinks()
32     {
33         return linksConfig;
34     }
35 
36     public static DConfig getConfig(JSONValue json)
37     {
38         /* The newly created configuration */
39         DConfig config = new DConfig();
40 
41         try
42         {
43             /* TODO: Parse */
44 
45             /* Get the `general` block */
46             JSONValue generalBlock = json["general"];
47             config.generalConfig = DGeneralConfig.getConfig(generalBlock);
48 
49             /* Get the `links` block */
50             //JSONValue linksBlock = json["links"];
51             //config.linksConfig = DLinkConfig.getConfig(linksBlock);
52         }
53         catch(JSONException e)
54         {
55             /* Set config to null (signals an error) */
56             config = null;
57         }
58 
59         return config;
60     }
61 
62     public JSONValue saveConfig()
63     {
64         JSONValue config;
65 
66 
67         return config;
68     }
69 }
70 
71 public final class DGeneralConfig
72 {
73     /* Addresses to bind sockets to */
74     private Address[] addresses;
75     private ushort port;
76 
77     /* Server information */
78     private string network;
79     private string name;
80     private string motd;
81 
82     public static DGeneralConfig getConfig(JSONValue generalBlock)
83     {
84         /* The generated general config */
85         DGeneralConfig config = new DGeneralConfig();
86         gprintln("Reading config:\n"~generalBlock.toPrettyString());
87 
88         try
89         {
90             /* Set the addresses to bind to */
91             foreach(JSONValue bindBlock; generalBlock["binds"].array())
92             {
93                 /* Get the address */
94                 string address = bindBlock["address"].str();
95 
96                 /* Get the port */
97                 ushort port = to!(ushort)(bindBlock["port"].str());
98 
99                 /* Add the address and port tuple to the list of addresses to bind to */
100                 config.addresses ~= parseAddress(address, port);
101             }
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         return config;
120     }
121 
122     public string getMotd()
123     {
124         return motd;
125     }
126 
127     public Address[] getAddresses()
128     {
129         return addresses;
130     }
131 }
132 
133 public final class DLinkConfig
134 {
135 
136 }