1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2005 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
25 #include "helperfuncs.h"
28 enum ServerState { CONNECTING, WAIT_AUTH_1, WAIT_AUTH_2, CONNECTED };
36 std::vector<TreeServer*> Children;
37 std::string ServerName;
38 std::string ServerDesc;
39 std::string VersionString;
43 TreeSocket* Socket; // for directly connected servers this points at the socket object
53 UserCount = OperCount = 0;
56 TreeServer(std::string Name, std::string Desc) : ServerName(Name), ServerDesc(Desc)
60 UserCount = OperCount = 0;
63 TreeServer(std::string Name, std::string Desc, TreeServer* Above) : Parent(Above), ServerName(Name), ServerDesc(Desc)
66 UserCount = OperCount = 0;
69 void AddChild(TreeServer* Child)
71 Children.push_back(Child);
74 bool DelChild(TreeServer* Child)
76 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
98 /* $ModDesc: Povides a spanning tree server link protocol */
102 TreeServer *TreeRoot;
103 std::vector<Link> LinkBlocks;
105 class TreeSocket : public InspSocket
111 TreeSocket(std::string host, int port, bool listening, unsigned long maxtime)
112 : InspSocket(host, port, listening, maxtime)
114 Srv->Log(DEBUG,"Create new");
118 TreeSocket(int newfd)
123 virtual bool OnConnected()
128 virtual void OnError(InspSocketError e)
132 virtual int OnDisconnect()
134 Srv->Log(DEBUG,"Disconnect");
135 Srv->SendToModeMask("o",WM_AND,"*** DISCONNECTED!");
139 virtual bool OnDataReady()
141 Srv->SendToModeMask("o",WM_AND,"*** DATA ***");
142 char* data = this->Read();
145 Srv->SendToModeMask("o",WM_AND,data);
147 return (data != NULL);
150 virtual void OnTimeout()
152 Srv->SendToModeMask("o",WM_AND,"*** TIMED OUT ***");
155 virtual void OnClose()
157 Srv->SendToModeMask("o",WM_AND,"*** CLOSED ***");
160 virtual int OnIncomingConnection(int newsock, char* ip)
162 TreeSocket* s = new TreeSocket(newsock);
168 void handle_connecttest(char **parameters, int pcnt, userrec *user)
170 std::string addr = parameters[0];
171 TreeSocket* sock = new TreeSocket(addr,80,false,10);
172 Srv->AddSocket(sock);
175 class ModuleSpanningTree : public Module
177 std::vector<TreeSocket*> Bindings;
181 void ReadConfiguration(bool rebind)
185 for (int j =0; j < Conf->Enumerate("bind"); j++)
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")
196 TreeSocket* listener = new TreeSocket(IP.c_str(),Port,true,10);
197 if (listener->GetState() == I_LISTENING)
199 Srv->AddSocket(listener);
200 Bindings.push_back(listener);
204 log(DEFAULT,"m_spanningtree: Warning: Failed to bind server port %d",Port);
212 for (int j =0; j < Conf->Enumerate("link"); j++)
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);
228 Conf = new ConfigReader;
231 // Create the root of the tree
232 TreeRoot = new TreeServer(Srv->GetServerName(),Srv->GetServerDescription());
234 ReadConfiguration(true);
237 void HandleLinks(char** parameters, int pcnt, userrec* user)
242 void HandleLusers(char** parameters, int pcnt, userrec* user)
247 void HandleMap(char** parameters, int pcnt, userrec* user)
252 int HandleSquit(char** parameters, int pcnt, userrec* user)
257 int HandleConnect(char** parameters, int pcnt, userrec* user)
262 virtual int OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user)
264 if (command == "CONNECT")
266 return this->HandleConnect(parameters,pcnt,user);
268 else if (command == "SQUIT")
270 return this->HandleSquit(parameters,pcnt,user);
272 else if (command == "MAP")
274 this->HandleMap(parameters,pcnt,user);
277 else if (command == "LUSERS")
279 this->HandleLusers(parameters,pcnt,user);
282 else if (command == "LINKS")
284 this->HandleLinks(parameters,pcnt,user);
290 virtual ~ModuleSpanningTree()
295 virtual Version GetVersion()
297 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
302 class ModuleSpanningTreeFactory : public ModuleFactory
305 ModuleSpanningTreeFactory()
309 ~ModuleSpanningTreeFactory()
313 virtual Module * CreateModule()
315 return new ModuleSpanningTree;
321 extern "C" void * init_module( void )
323 return new ModuleSpanningTreeFactory;