]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_xmlsocket.cpp
Don't use this yet. we're waiting on w00t for a test client
[user/henk/code/inspircd.git] / src / modules / m_xmlsocket.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include <string>
15 #include <vector>
16
17 #include <openssl/ssl.h>
18 #include <openssl/err.h>
19
20 #include "inspircd_config.h"
21 #include "configreader.h"
22 #include "users.h"
23 #include "channels.h"
24 #include "modules.h"
25
26 #include "socket.h"
27 #include "hashcomp.h"
28 #include "inspircd.h"
29
30 /* $ModDesc: Provides XMLSocket support for clients */
31
32
33 class ModuleXMLSocket : public Module
34 {
35
36         ConfigReader* Conf;
37
38         std::vector<int> listenports;
39         int clientactive;
40
41  public:
42
43         InspIRCd* PublicInstance;
44
45         ModuleXMLSocket(InspIRCd* Me)
46                 : Module::Module(Me), PublicInstance(Me)
47         {
48                 OnRehash(NULL,"");
49         }
50
51         virtual void OnRehash(userrec* user, const std::string &param)
52         {
53
54                 Conf = new ConfigReader(ServerInstance);
55
56                 for (unsigned int i = 0; i < listenports.size(); i++)
57                 {
58                         ServerInstance->Config->DelIOHook(listenports[i]);
59                 }
60
61                 listenports.clear();
62                 clientactive = 0;
63
64                 for (int i = 0; i < Conf->Enumerate("bind"); i++)
65                 {
66                         // For each <bind> tag
67                         if (((Conf->ReadValue("bind", "type", i) == "") || (Conf->ReadValue("bind", "type", i) == "clients")) && (Conf->ReadValue("bind", "xmlsocket", i) == "yes"))
68                         {
69                                 // Get the port we're meant to be listening on with SSL
70                                 std::string port = Conf->ReadValue("bind", "port", i);
71                                 irc::portparser portrange(port, false);
72                                 long portno = -1;
73                                 while ((portno = portrange.GetToken()))
74                                 {
75                                         clientactive++;
76                                         try
77                                         {
78                                                 if (ServerInstance->Config->AddIOHook(portno, this))
79                                                 {
80                                                         listenports.push_back(portno);
81                                                                 for (unsigned int i = 0; i < ServerInstance->stats->BoundPortCount; i++)
82                                                                 if (ServerInstance->Config->ports[i] == portno)
83                                                                         ServerInstance->Config->openSockfd[i]->SetDescription("xml");
84                                                 }
85                                                 else
86                                                 {
87                                                         ServerInstance->Log(DEFAULT, "m_xmlsocket.so: FAILED to enable XMLSocket on port %d, maybe you have another similar module loaded?", portno);
88                                                 }
89                                         }
90                                         catch (ModuleException &e)
91                                         {
92                                                 ServerInstance->Log(DEFAULT, "m_xmlsocket.so: FAILED to enable XMLSocket on port %d: %s. Maybe it's already hooked by the same port on a different IP, or you have another similar module loaded?", portno, e.GetReason());
93                                         }
94                                 }
95                         }
96                 }
97
98                 DELETE(Conf);
99         }
100
101         virtual ~ModuleXMLSocket()
102         {
103         }
104
105         virtual void OnUnloadModule(Module* mod, const std::string &name)
106         {
107                 if (mod == this)
108                 {
109                         for(unsigned int i = 0; i < listenports.size(); i++)
110                         {
111                                 ServerInstance->Config->DelIOHook(listenports[i]);
112                                 for (unsigned int j = 0; j < ServerInstance->stats->BoundPortCount; j++)
113                                         if (ServerInstance->Config->ports[j] == listenports[i])
114                                                 if (ServerInstance->Config->openSockfd[j])
115                                                         ServerInstance->Config->openSockfd[j]->SetDescription("plaintext");
116                         }
117                 }
118         }
119
120         virtual Version GetVersion()
121         {
122                 return Version(1, 1, 0, 0, VF_VENDOR, API_VERSION);
123         }
124
125         void Implements(char* List)
126         {
127                 List[I_OnUnloadModule] = List[I_OnRawSocketRead] = List[I_OnRawSocketWrite] = List[I_OnRehash] = 1;
128         }
129
130         virtual int OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult)
131         {
132                 userrec* user = dynamic_cast<userrec*>(ServerInstance->FindDescriptor(fd));
133
134                 if (user == NULL)
135                         return -1;
136
137                 int result = user->ReadData(buffer, count);
138
139                 if ((result == -1) && (errno == EAGAIN))
140                         return -1;
141                 else if (result < 1)
142                         return 0;
143
144                 for (int n = 0; n < result; n++)
145                         if (buffer[n] == 0)
146                                 buffer[n] = '\n';
147
148                 readresult = result;
149                 return result;
150         }
151
152         virtual int OnRawSocketWrite(int fd, const char* buffer, int count)
153         {
154                 userrec* user = dynamic_cast<userrec*>(ServerInstance->FindDescriptor(fd));
155
156                 if (user == NULL)
157                         return -1;
158
159                 char tmpbuffer[count+1];
160                 memcpy(&tmpbuffer, &buffer, count);
161
162                 for (int n = 0; n < count; n++)
163                         if ((tmpbuffer[n] == '\r') || (tmpbuffer[n] == '\n'))
164                                 tmpbuffer[n] = 0;
165
166                 user->AddWriteBuf(std::string(tmpbuffer,count));
167
168                 return 1;
169         }
170
171 };
172
173 class ModuleXMLSocketFactory : public ModuleFactory
174 {
175  public:
176         ModuleXMLSocketFactory()
177         {
178         }
179
180         ~ModuleXMLSocketFactory()
181         {
182         }
183
184         virtual Module * CreateModule(InspIRCd* Me)
185         {
186                 return new ModuleXMLSocket(Me);
187         }
188 };
189
190
191 extern "C" void * init_module( void )
192 {
193         return new ModuleXMLSocketFactory;
194 }