1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
17 #include <openssl/ssl.h>
18 #include <openssl/err.h>
20 #include "inspircd_config.h"
21 #include "configreader.h"
30 /* $ModDesc: Provides XMLSocket support for clients */
33 class ModuleXMLSocket : public Module
38 std::vector<int> listenports;
43 InspIRCd* PublicInstance;
45 ModuleXMLSocket(InspIRCd* Me)
46 : Module::Module(Me), PublicInstance(Me)
51 virtual void OnRehash(userrec* user, const std::string ¶m)
54 Conf = new ConfigReader(ServerInstance);
56 for (unsigned int i = 0; i < listenports.size(); i++)
58 ServerInstance->Config->DelIOHook(listenports[i]);
64 for (int i = 0; i < Conf->Enumerate("bind"); i++)
66 // For each <bind> tag
67 if (((Conf->ReadValue("bind", "type", i) == "") || (Conf->ReadValue("bind", "type", i) == "clients")) && (Conf->ReadValue("bind", "xmlsocket", i) == "yes"))
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);
73 while ((portno = portrange.GetToken()))
78 if (ServerInstance->Config->AddIOHook(portno, this))
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");
87 ServerInstance->Log(DEFAULT, "m_xmlsocket.so: FAILED to enable XMLSocket on port %d, maybe you have another similar module loaded?", portno);
90 catch (ModuleException &e)
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());
101 virtual ~ModuleXMLSocket()
105 virtual void OnUnloadModule(Module* mod, const std::string &name)
109 for(unsigned int i = 0; i < listenports.size(); i++)
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");
120 virtual Version GetVersion()
122 return Version(1, 1, 0, 0, VF_VENDOR, API_VERSION);
125 void Implements(char* List)
127 List[I_OnUnloadModule] = List[I_OnRawSocketRead] = List[I_OnRawSocketWrite] = List[I_OnRehash] = 1;
130 virtual int OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult)
132 userrec* user = dynamic_cast<userrec*>(ServerInstance->FindDescriptor(fd));
137 int result = user->ReadData(buffer, count);
139 if ((result == -1) && (errno == EAGAIN))
144 /* XXX: The core is more than happy to split lines purely on an \n
145 * rather than a \r\n. This is good for us as it means that the size
146 * of data we are receiving is exactly the same as the size of data
147 * we asked for, and we dont need to re-implement our own socket
148 * buffering (See below)
150 for (int n = 0; n < result; n++)
158 virtual int OnRawSocketWrite(int fd, const char* buffer, int count)
160 userrec* user = dynamic_cast<userrec*>(ServerInstance->FindDescriptor(fd));
165 /* We want to alter the buffer, so we have to make a copy */
166 char tmpbuffer[count+1];
167 memcpy(&tmpbuffer, &buffer, count);
169 /* XXX: This will actually generate lines "looking\0\0like\0\0this"
170 * rather than lines "looking\0like\0this". This shouldnt be a problem
171 * to the client, but it saves us a TON of processing and the need
172 * to re-implement socket buffering, as the data we are sending is
173 * exactly the same length as the data we are receiving.
175 for (int n = 0; n < count; n++)
176 if ((tmpbuffer[n] == '\r') || (tmpbuffer[n] == '\n'))
179 user->AddWriteBuf(std::string(tmpbuffer,count));
186 class ModuleXMLSocketFactory : public ModuleFactory
189 ModuleXMLSocketFactory()
193 ~ModuleXMLSocketFactory()
197 virtual Module * CreateModule(InspIRCd* Me)
199 return new ModuleXMLSocket(Me);
204 extern "C" void * init_module( void )
206 return new ModuleXMLSocketFactory;