summaryrefslogtreecommitdiff
path: root/src/modules/m_spanningtree.cpp
blob: 4cc4b50d656c5de01def36b6a43a7a17a9e948f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*       +------------------------------------+
 *       | Inspire Internet Relay Chat Daemon |
 *       +------------------------------------+
 *
 *  Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
 *                       E-mail:
 *                <brain@chatspike.net>
 *           	  <Craig@chatspike.net>
 *     
 * Written by Craig Edwards, Craig McLure, and others.
 * This program is free but copyrighted software; see
 *            the file COPYING for details.
 *
 * ---------------------------------------------------
 */

using namespace std;

#include <stdio.h>
#include <vector>
#include "users.h"
#include "channels.h"
#include "modules.h"
#include "socket.h"

/* $ModDesc: Povides a spanning tree server link protocol */

Server *Srv;

class TreeSocket : public InspSocket
{
	std::string myhost;
	
 public:

	TreeSocket(std::string host, int port, bool listening, unsigned long maxtime)
		: InspSocket(host, port, listening, maxtime)
	{
		Srv->Log(DEBUG,"Create new");
		myhost = host;
	}

	TreeSocket(int newfd)
		: InspSocket(newfd)
	{
		// Associate with an existing file descriptor (accepted from incoming connection)
	}
	
        virtual bool OnConnected()
	{
		Srv->Log(DEBUG,"Connected");
		Srv->SendToModeMask("o",WM_AND,"*** CONNECTED!");
		this->Write("GET / HTTP/1.1\r\nHost: " + myhost + "\r\nConnection: Close\r\n\r\n");
		Srv->SendToModeMask("o",WM_AND,"*** DATA WRITTEN ***");
		Srv->Log(DEBUG,"Wrote");
		return true;
	}
	
        virtual void OnError(InspSocketError e)
	{
		char x[1024];
		Srv->Log(DEBUG,"Error");
		sprintf(x,"*** ERROR %d",(int)e);
		Srv->SendToModeMask("o",WM_AND,x);
	}
	
        virtual int OnDisconnect()
	{
		Srv->Log(DEBUG,"Disconnect");
		Srv->SendToModeMask("o",WM_AND,"*** DISCONNECTED!");
		return true;
	}
	
        virtual bool OnDataReady()
	{
		Srv->Log(DEBUG,"Data");
		Srv->SendToModeMask("o",WM_AND,"*** DATA ***");
		char* data = this->Read();
		if (data)
		{
			Srv->SendToModeMask("o",WM_AND,data);
		}
		return (data != NULL);
	}
	
        virtual void OnTimeout()
	{
		Srv->Log(DEBUG,"Timeout");
		Srv->SendToModeMask("o",WM_AND,"*** TIMED OUT ***");
	}
	
        virtual void OnClose()
	{
		Srv->SendToModeMask("o",WM_AND,"*** CLOSED ***");
	}
	
	virtual int OnIncomingConnection(int newsock, char* ip)
	{
		Srv->SendToModeMask("o",WM_AND,"*** INCOMING ***");
		// use the (int) constructor to associate an incoming
		// connection with a class, without actually creating
		// a connection or binding from scratch.
		TreeSocket* s = new TreeSocket(newsock);
		Srv->AddSocket(s);
		char message[1024];
		sprintf(message,"Added new socket to list with fd %d from %s",newsock,ip);
		Srv->Log(DEBUG,message);
		return true;
	}
};

void handle_connecttest(char **parameters, int pcnt, userrec *user)
{
	// create a new class of type TreeSocket.
	std::string a = parameters[0];
	TreeSocket* s = new TreeSocket(a,80,false,10);
	Srv->Log(DEBUG,"Create TreeSocket");
	Srv->AddSocket(s);
	Srv->Log(DEBUG,"Added socket");
}

class ModuleSpanningTree : public Module
{
 public:
	ModuleSpanningTree()
	{
		Srv = new Server;
		Srv->AddCommand("CONNECTTEST",handle_connecttest,'o',1,"m_spanningtree.so");
		Srv->Log(DEBUG,"ModCreate");
		TreeSocket* listeningsock = new TreeSocket("127.0.0.1",11111,true,10);
		Srv->AddSocket(listeningsock);
	}
	
	virtual void OnUserJoin(userrec* user, chanrec* channel)
	{
	}
	
	virtual ~ModuleSpanningTree()
	{
		delete Srv;
	}
	
	virtual Version GetVersion()
	{
		return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
	}
	
	virtual void OnUserConnect(userrec* user)
	{
	}

};


class ModuleSpanningTreeFactory : public ModuleFactory
{
 public:
	ModuleSpanningTreeFactory()
	{
	}
	
	~ModuleSpanningTreeFactory()
	{
	}
	
	virtual Module * CreateModule()
	{
		return new ModuleSpanningTree;
	}
	
};


extern "C" void * init_module( void )
{
	return new ModuleSpanningTreeFactory;
}