1 module server.server;
2 
3 import std.socket : Socket, Address, SocketType, ProtocolType;
4 import client.client : ButterflyClient;
5 import std.file : mkdir, exists, isDir;
6 import server.listener : ButterflyListener;
7 import std.stdio : writeln;
8 import std..string : cmp;
9 import gogga;
10 
11 public final class ButterflyServer
12 {
13     /**
14     * TODO: Later implement listeners so that we can
15     * bind to multiple sockets.
16     */
17     private ButterflyListener[] listeners;
18 
19     /**
20     * Socket to listen for incoming connections on
21     */
22     private Socket serverSocket;
23 
24     /**
25     * Whether or not the server is active
26     */
27     private bool active = true;
28 
29     this(ButterflyListener[] listeners)
30     {
31         /**
32         * Create the needed directories (if not already present)
33         */
34         directoryCheck();
35 
36         /**
37         * Set all the listeners
38         */
39         this.listeners = listeners;
40 
41         /**
42         * Set the server of all listeners to this server
43         */
44         foreach(ButterflyListener listener; listeners)
45         {
46             listener.setServer(this);
47         }
48     }
49 
50     private void directoryCheck()
51     {
52         /* TODO: Create the `mailboxes/` directory, if it does not exist */
53 
54         /* Check to make sure there is n FS node at `mailboxes` */
55         if(exists("mailboxes"))
56         {
57             /* Make sure it is a directory */
58             if(isDir("mailboxes"))
59             {
60 
61             }
62             else
63             {
64                 /* TODO: Error handling */
65             }
66         }
67         else
68         {
69             /* Create the `mailboxes` directory */
70             mkdir("mailboxes");
71         }  
72 
73         /* Check to make sure there is an FS node at `accounts/` */
74         if(exists("accounts"))
75         {
76             /* Make sure it is a directory */
77             if(isDir("accounts"))
78             {
79 
80             }
81             else
82             {
83                 /* TODO: Error handling */
84             }
85         }
86         else
87         {
88             /* Create the `accounts` directory */
89             mkdir("accounts");
90         }  
91     }
92 
93     public void run()
94     {
95         /* Start the listeners */
96         foreach(ButterflyListener listener; listeners)
97         {
98             gprintln("Starting listener \"" ~ listener.getName() ~"\" ...");
99             gprintln("Listener is using configuration: "~listener.getConfig().toPrettyString());
100             listener.start();
101             gprintln("Listener \"" ~ listener.getName() ~ "\" started");
102         }
103     }
104 
105     public bool isLocalDomain(string domain)
106     {
107         /**
108         * Loop through each listener and check if the requested domain
109         * appears in one of them.
110         */ 
111         foreach(ButterflyListener listener; listeners)
112         {
113             if(cmp(listener.getDomain(), domain) == 0)
114             {
115                 return true;
116             }
117         }
118 
119         return false;
120     }
121 
122 }