]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree.cpp
688df280a7b21ebf8f70a88d8f15791d203d2750
[user/henk/code/inspircd.git] / src / modules / m_spanningtree.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2005 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include <stdio.h>
20 #include <vector>
21 #include "users.h"
22 #include "channels.h"
23 #include "modules.h"
24 #include "socket.h"
25 #include "helperfuncs.h"
26 #include "inspircd.h"
27
28 enum ServerState { CONNECTING, WAIT_AUTH_1, WAIT_AUTH_2, CONNECTED };
29
30 class TreeServer;
31 class TreeSocket;
32
33 class TreeServer
34 {
35         TreeServer* Parent;
36         std::vector<TreeServer*> Children;
37         std::string ServerName;
38         std::string ServerDesc;
39         std::string VersionString;
40         int UserCount;
41         int OperCount;
42         ServerState State;
43         TreeSocket* Socket;     // for directly connected servers this points at the socket object
44         
45  public:
46
47         TreeServer()
48         {
49                 Parent = NULL;
50                 ServerName = "";
51                 ServerDesc = "";
52                 VersionString = "";
53                 UserCount = OperCount = 0;
54         }
55
56         TreeServer(std::string Name, std::string Desc) : ServerName(Name), ServerDesc(Desc)
57         {
58                 Parent = NULL;
59                 VersionString = "";
60                 UserCount = OperCount = 0;
61         }
62
63         TreeServer(std::string Name, std::string Desc, TreeServer* Above) : Parent(Above), ServerName(Name), ServerDesc(Desc)
64         {
65                 VersionString = "";
66                 UserCount = OperCount = 0;
67         }
68
69         void AddChild(TreeServer* Child)
70         {
71                 Children.push_back(Child);
72         }
73
74         bool DelChild(TreeServer* Child)
75         {
76                 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
77                 {
78                         if (*a == Child)
79                         {
80                                 Children.erase(a);
81                                 return true;
82                         }
83                 }
84                 return false;
85         }
86 };
87
88 class Link
89 {
90  public:
91          std::string Name;
92          std::string IPAddr;
93          int Port;
94          std::string SendPass;
95          std::string RecvPass;
96 };
97
98 /* $ModDesc: Povides a spanning tree server link protocol */
99
100 Server *Srv;
101 ConfigReader *Conf;
102 TreeServer *TreeRoot;
103 std::vector<Link> LinkBlocks;
104
105 class TreeSocket : public InspSocket
106 {
107         std::string myhost;
108         
109  public:
110
111         TreeSocket(std::string host, int port, bool listening, unsigned long maxtime)
112                 : InspSocket(host, port, listening, maxtime)
113         {
114                 Srv->Log(DEBUG,"Create new");
115                 myhost = host;
116         }
117
118         TreeSocket(int newfd)
119                 : InspSocket(newfd)
120         {
121         }
122         
123         virtual bool OnConnected()
124         {
125                 return true;
126         }
127         
128         virtual void OnError(InspSocketError e)
129         {
130         }
131
132         virtual int OnDisconnect()
133         {
134                 Srv->Log(DEBUG,"Disconnect");
135                 Srv->SendToModeMask("o",WM_AND,"*** DISCONNECTED!");
136                 return true;
137         }
138
139         virtual bool OnDataReady()
140         {
141                 Srv->SendToModeMask("o",WM_AND,"*** DATA ***");
142                 char* data = this->Read();
143                 if (data)
144                 {
145                         Srv->SendToModeMask("o",WM_AND,data);
146                 }
147                 return (data != NULL);
148         }
149
150         virtual void OnTimeout()
151         {
152                 Srv->SendToModeMask("o",WM_AND,"*** TIMED OUT ***");
153         }
154
155         virtual void OnClose()
156         {
157                 Srv->SendToModeMask("o",WM_AND,"*** CLOSED ***");
158         }
159
160         virtual int OnIncomingConnection(int newsock, char* ip)
161         {
162                 TreeSocket* s = new TreeSocket(newsock);
163                 Srv->AddSocket(s);
164                 return true;
165         }
166 };
167
168 void handle_connecttest(char **parameters, int pcnt, userrec *user)
169 {
170         std::string addr = parameters[0];
171         TreeSocket* sock = new TreeSocket(addr,80,false,10);
172         Srv->AddSocket(sock);
173 }
174
175 class ModuleSpanningTree : public Module
176 {
177         std::vector<TreeSocket*> Bindings;
178
179  public:
180
181         void ReadConfiguration(bool rebind)
182         {
183                 if (rebind)
184                 {
185                         for (int j =0; j < Conf->Enumerate("bind"); j++)
186                         {
187                                 std::string Type = Conf->ReadValue("bind","type",j);
188                                 std::string IP = Conf->ReadValue("bind","address",j);
189                                 long Port = Conf->ReadInteger("bind","port",j,true);
190                                 if (Type == "servers")
191                                 {
192                                         if (IP == "*")
193                                         {
194                                                 IP = "";
195                                         }
196                                         TreeSocket* listener = new TreeSocket(IP.c_str(),Port,true,10);
197                                         if (listener->GetState() == I_LISTENING)
198                                         {
199                                                 Srv->AddSocket(listener);
200                                                 Bindings.push_back(listener);
201                                         }
202                                         else
203                                         {
204                                                 log(DEFAULT,"m_spanningtree: Warning: Failed to bind server port %d",Port);
205                                                 listener->Close();
206                                                 delete listener;
207                                         }
208                                 }
209                         }
210                 }
211                 LinkBlocks.clear();
212                 for (int j =0; j < Conf->Enumerate("link"); j++)
213                 {
214                         Link L;
215                         L.Name = Conf->ReadValue("link","name",j);
216                         L.IPAddr = Conf->ReadValue("link","ipaddr",j);
217                         L.Port = Conf->ReadInteger("link","port",j,true);
218                         L.SendPass = Conf->ReadValue("link","sendpass",j);
219                         L.RecvPass = Conf->ReadValue("link","recvpass",j);
220                         LinkBlocks.push_back(L);
221                         log(DEBUG,"m_spanningtree: Read server %s with host %s:%d",L.Name.c_str(),L.IPAddr.c_str(),L.Port);
222                 }
223         }
224
225         ModuleSpanningTree()
226         {
227                 Srv = new Server;
228                 Conf = new ConfigReader;
229                 Bindings.clear();
230
231                 // Create the root of the tree
232                 TreeRoot = new TreeServer(Srv->GetServerName(),Srv->GetServerDescription());
233
234                 ReadConfiguration(true);
235         }
236
237         void HandleLinks(char** parameters, int pcnt, userrec* user)
238         {
239                 return;
240         }
241
242         void HandleLusers(char** parameters, int pcnt, userrec* user)
243         {
244                 return;
245         }
246
247         void HandleMap(char** parameters, int pcnt, userrec* user)
248         {
249                 return;
250         }
251
252         int HandleSquit(char** parameters, int pcnt, userrec* user)
253         {
254                 return 1;
255         }
256
257         int HandleConnect(char** parameters, int pcnt, userrec* user)
258         {
259                 return 1;
260         }
261
262         virtual int OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user)
263         {
264                 if (command == "CONNECT")
265                 {
266                         return this->HandleConnect(parameters,pcnt,user);
267                 }
268                 else if (command == "SQUIT")
269                 {
270                         return this->HandleSquit(parameters,pcnt,user);
271                 }
272                 else if (command == "MAP")
273                 {
274                         this->HandleMap(parameters,pcnt,user);
275                         return 1;
276                 }
277                 else if (command == "LUSERS")
278                 {
279                         this->HandleLusers(parameters,pcnt,user);
280                         return 1;
281                 }
282                 else if (command == "LINKS")
283                 {
284                         this->HandleLinks(parameters,pcnt,user);
285                         return 1;
286                 }
287                 return 0;
288         }
289
290         virtual ~ModuleSpanningTree()
291         {
292                 delete Srv;
293         }
294
295         virtual Version GetVersion()
296         {
297                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
298         }
299 };
300
301
302 class ModuleSpanningTreeFactory : public ModuleFactory
303 {
304  public:
305         ModuleSpanningTreeFactory()
306         {
307         }
308         
309         ~ModuleSpanningTreeFactory()
310         {
311         }
312         
313         virtual Module * CreateModule()
314         {
315                 return new ModuleSpanningTree;
316         }
317         
318 };
319
320
321 extern "C" void * init_module( void )
322 {
323         return new ModuleSpanningTreeFactory;
324 }
325