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 * ---------------------------------------------------
14 #include "inspircd_config.h"
15 #include "configreader.h"
23 /* $ModDesc: Provides XMLSocket support for clients */
26 class ModuleXMLSocket : public Module
31 std::vector<int> listenports;
36 InspIRCd* PublicInstance;
38 ModuleXMLSocket(InspIRCd* Me)
39 : Module::Module(Me), PublicInstance(Me)
44 virtual void OnRehash(userrec* user, const std::string ¶m)
47 Conf = new ConfigReader(ServerInstance);
49 for (unsigned int i = 0; i < listenports.size(); i++)
51 ServerInstance->Config->DelIOHook(listenports[i]);
57 for (int i = 0; i < Conf->Enumerate("bind"); i++)
59 // For each <bind> tag
60 if (((Conf->ReadValue("bind", "type", i) == "") || (Conf->ReadValue("bind", "type", i) == "clients")) && (Conf->ReadValue("bind", "xmlsocket", i) == "yes"))
62 // Get the port we're meant to be listening on with SSL
63 std::string port = Conf->ReadValue("bind", "port", i);
64 irc::portparser portrange(port, false);
66 while ((portno = portrange.GetToken()))
71 if (ServerInstance->Config->AddIOHook(portno, this))
73 listenports.push_back(portno);
74 for (unsigned int i = 0; i < ServerInstance->stats->BoundPortCount; i++)
75 if (ServerInstance->Config->ports[i] == portno)
76 ServerInstance->Config->openSockfd[i]->SetDescription("xml");
80 ServerInstance->Log(DEFAULT, "m_xmlsocket.so: FAILED to enable XMLSocket on port %d, maybe you have another similar module loaded?", portno);
83 catch (ModuleException &e)
85 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());
94 virtual ~ModuleXMLSocket()
98 virtual void OnUnloadModule(Module* mod, const std::string &name)
102 for(unsigned int i = 0; i < listenports.size(); i++)
104 ServerInstance->Config->DelIOHook(listenports[i]);
105 for (unsigned int j = 0; j < ServerInstance->stats->BoundPortCount; j++)
106 if (ServerInstance->Config->ports[j] == listenports[i])
107 if (ServerInstance->Config->openSockfd[j])
108 ServerInstance->Config->openSockfd[j]->SetDescription("plaintext");
113 virtual Version GetVersion()
115 return Version(1, 1, 0, 0, VF_VENDOR, API_VERSION);
118 void Implements(char* List)
120 List[I_OnUnloadModule] = List[I_OnRawSocketRead] = List[I_OnRawSocketWrite] = List[I_OnRehash] = 1;
123 virtual int OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult)
125 userrec* user = dynamic_cast<userrec*>(ServerInstance->FindDescriptor(fd));
130 int result = user->ReadData(buffer, count);
132 if ((result == -1) && (errno == EAGAIN))
137 /* XXX: The core is more than happy to split lines purely on an \n
138 * rather than a \r\n. This is good for us as it means that the size
139 * of data we are receiving is exactly the same as the size of data
140 * we asked for, and we dont need to re-implement our own socket
141 * buffering (See below)
143 for (int n = 0; n < result; n++)
151 virtual int OnRawSocketWrite(int fd, const char* buffer, int count)
153 userrec* user = dynamic_cast<userrec*>(ServerInstance->FindDescriptor(fd));
158 /* We want to alter the buffer, so we have to make a copy */
159 char tmpbuffer[count+1];
160 memcpy(tmpbuffer, buffer, count);
162 /* XXX: This will actually generate lines "looking\0\0like\0\0this"
163 * rather than lines "looking\0like\0this". This shouldnt be a problem
164 * to the client, but it saves us a TON of processing and the need
165 * to re-implement socket buffering, as the data we are sending is
166 * exactly the same length as the data we are receiving.
168 for (int n = 0; n < count; n++)
169 if ((tmpbuffer[n] == '\r') || (tmpbuffer[n] == '\n'))
172 user->AddWriteBuf(std::string(tmpbuffer,count));
179 class ModuleXMLSocketFactory : public ModuleFactory
182 ModuleXMLSocketFactory()
186 ~ModuleXMLSocketFactory()
190 virtual Module * CreateModule(InspIRCd* Me)
192 return new ModuleXMLSocket(Me);
197 extern "C" void * init_module( void )
199 return new ModuleXMLSocketFactory;