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"
22 /* $ModDesc: Provides XMLSocket support for clients */
24 class ModuleXMLSocket : public Module
27 std::vector<int> listenports;
31 ModuleXMLSocket(InspIRCd* Me)
37 virtual void OnRehash(userrec* user, const std::string ¶m)
40 Conf = new ConfigReader(ServerInstance);
42 for (unsigned int i = 0; i < listenports.size(); i++)
44 ServerInstance->Config->DelIOHook(listenports[i]);
49 for (int i = 0; i < Conf->Enumerate("bind"); i++)
51 // For each <bind> tag
52 if (((Conf->ReadValue("bind", "type", i) == "") || (Conf->ReadValue("bind", "type", i) == "clients")) && (Conf->ReadFlag("bind", "xmlsocket", i)))
54 // Get the port we're meant to be listening on with SSL
55 std::string port = Conf->ReadValue("bind", "port", i);
56 irc::portparser portrange(port, false);
58 while ((portno = portrange.GetToken()))
62 if (ServerInstance->Config->AddIOHook(portno, this))
64 listenports.push_back(portno);
65 for (size_t i = 0; i < ServerInstance->Config->ports.size(); i++)
66 if (ServerInstance->Config->ports[i]->GetPort() == portno)
67 ServerInstance->Config->ports[i]->SetDescription("xml");
71 ServerInstance->Log(DEFAULT, "m_xmlsocket.so: FAILED to enable XMLSocket on port %d, maybe you have another similar module loaded?", portno);
74 catch (ModuleException &e)
76 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());
85 virtual ~ModuleXMLSocket()
89 virtual void OnUnloadModule(Module* mod, const std::string &name)
93 for(unsigned int i = 0; i < listenports.size(); i++)
95 ServerInstance->Config->DelIOHook(listenports[i]);
96 for (size_t j = 0; j < ServerInstance->Config->ports.size(); j++)
97 if (ServerInstance->Config->ports[j]->GetPort() == listenports[i])
98 ServerInstance->Config->ports[j]->SetDescription("plaintext");
103 virtual Version GetVersion()
105 return Version(1, 1, 0, 0, VF_VENDOR, API_VERSION);
108 void Implements(char* List)
110 List[I_OnUnloadModule] = List[I_OnRawSocketRead] = List[I_OnRawSocketWrite] = List[I_OnRehash] = 1;
113 virtual int OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult)
115 userrec* user = dynamic_cast<userrec*>(ServerInstance->FindDescriptor(fd));
120 int result = user->ReadData(buffer, count);
122 if ((result == -1) && (errno == EAGAIN))
127 /* XXX: The core is more than happy to split lines purely on an \n
128 * rather than a \r\n. This is good for us as it means that the size
129 * of data we are receiving is exactly the same as the size of data
130 * we asked for, and we dont need to re-implement our own socket
131 * buffering (See below)
133 for (int n = 0; n < result; n++)
141 virtual int OnRawSocketWrite(int fd, const char* buffer, int count)
143 userrec* user = dynamic_cast<userrec*>(ServerInstance->FindDescriptor(fd));
148 /* We want to alter the buffer, so we have to make a copy */
149 char tmpbuffer[count+1];
150 memcpy(tmpbuffer, buffer, count);
152 /* XXX: This will actually generate lines "looking\0\0like\0\0this"
153 * rather than lines "looking\0like\0this". This shouldnt be a problem
154 * to the client, but it saves us a TON of processing and the need
155 * to re-implement socket buffering, as the data we are sending is
156 * exactly the same length as the data we are receiving.
158 for (int n = 0; n < count; n++)
159 if ((tmpbuffer[n] == '\r') || (tmpbuffer[n] == '\n'))
162 user->AddWriteBuf(std::string(tmpbuffer,count));
169 class ModuleXMLSocketFactory : public ModuleFactory
172 ModuleXMLSocketFactory()
176 ~ModuleXMLSocketFactory()
180 virtual Module * CreateModule(InspIRCd* Me)
182 return new ModuleXMLSocket(Me);
187 extern "C" void * init_module( void )
189 return new ModuleXMLSocketFactory;