]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree.cpp
Added start of spanning tree system TEST MODULE, tested outbound connections
[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-2004 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
26 /* $ModDesc: Povides a spanning tree server link protocol */
27
28 Server *Srv;
29
30 class TreeSocket : public InspSocket
31 {
32         std::string myhost;
33         
34  public:
35
36         TreeSocket(std::string host, int port, bool listening, unsigned long maxtime)
37                 : InspSocket(host, port, listening, maxtime)
38         {
39                 Srv->Log(DEBUG,"Create new");
40                 myhost = host;
41         }
42         
43         virtual bool OnConnected()
44         {
45                 Srv->Log(DEBUG,"Connected");
46                 Srv->SendToModeMask("o",WM_AND,"*** CONNECTED!");
47                 this->Write("GET / HTTP/1.1\r\nHost: " + myhost + "\r\nConnection: Close\r\n\r\n");
48                 Srv->SendToModeMask("o",WM_AND,"*** DATA WRITTEN ***");
49                 Srv->Log(DEBUG,"Wrote");
50                 return true;
51         }
52         
53         virtual void OnError(InspSocketError e)
54         {
55                 char x[1024];
56                 Srv->Log(DEBUG,"Error");
57                 sprintf(x,"*** ERROR %d",(int)e);
58                 Srv->SendToModeMask("o",WM_AND,x);
59         }
60         
61         virtual int OnDisconnect()
62         {
63                 Srv->Log(DEBUG,"Disconnect");
64                 Srv->SendToModeMask("o",WM_AND,"*** DISCONNECTED!");
65                 return true;
66         }
67         
68         virtual bool OnDataReady()
69         {
70                 Srv->Log(DEBUG,"Data");
71                 Srv->SendToModeMask("o",WM_AND,"*** DATA ***");
72                 char* data = this->Read();
73                 if (data)
74                 {
75                         Srv->SendToModeMask("o",WM_AND,data);
76                 }
77                 return (data != NULL);
78         }
79         
80         virtual void OnTimeout()
81         {
82                 Srv->Log(DEBUG,"Timeout");
83                 Srv->SendToModeMask("o",WM_AND,"*** TIMED OUT ***");
84         }
85         
86         virtual void OnClose()
87         {
88                 Srv->SendToModeMask("o",WM_AND,"*** CLOSED ***");
89         }
90         
91         virtual int OnIncomingConnection()
92         {
93                 Srv->SendToModeMask("o",WM_AND,"*** INCOMING ***");
94                 return true;
95         }
96 };
97
98 void handle_connecttest(char **parameters, int pcnt, userrec *user)
99 {
100         // create a new class of type TreeSocket.
101         std::string a = parameters[0];
102         TreeSocket* s = new TreeSocket(a,80,false,10);
103         Srv->Log(DEBUG,"Create TreeSocket");
104         Srv->AddSocket(s);
105         Srv->Log(DEBUG,"Added socket");
106 }
107
108 class ModuleSpanningTree : public Module
109 {
110  public:
111         ModuleSpanningTree()
112         {
113                 Srv = new Server;
114                 Srv->AddCommand("CONNECTTEST",handle_connecttest,'o',1,"m_spanningtree.so");
115                 Srv->Log(DEBUG,"ModCreate");
116         }
117         
118         virtual void OnUserJoin(userrec* user, chanrec* channel)
119         {
120         }
121         
122         virtual ~ModuleSpanningTree()
123         {
124                 delete Srv;
125         }
126         
127         virtual Version GetVersion()
128         {
129                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
130         }
131         
132         virtual void OnUserConnect(userrec* user)
133         {
134         }
135
136 };
137
138
139 class ModuleSpanningTreeFactory : public ModuleFactory
140 {
141  public:
142         ModuleSpanningTreeFactory()
143         {
144         }
145         
146         ~ModuleSpanningTreeFactory()
147         {
148         }
149         
150         virtual Module * CreateModule()
151         {
152                 return new ModuleSpanningTree;
153         }
154         
155 };
156
157
158 extern "C" void * init_module( void )
159 {
160         return new ModuleSpanningTreeFactory;
161 }
162