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 * ---------------------------------------------------
20 /* $ModDesc: Provides XMLSocket support for clients */
22 class ModuleXMLSocket : public Module
25 std::vector<int> listenports;
29 ModuleXMLSocket(InspIRCd* Me)
35 virtual void OnRehash(userrec* user, const std::string ¶m)
38 Conf = new ConfigReader(ServerInstance);
40 for (unsigned int i = 0; i < listenports.size(); i++)
42 ServerInstance->Config->DelIOHook(listenports[i]);
47 for (int i = 0; i < Conf->Enumerate("bind"); i++)
49 // For each <bind> tag
50 if (((Conf->ReadValue("bind", "type", i) == "") || (Conf->ReadValue("bind", "type", i) == "clients")) && (Conf->ReadFlag("bind", "xmlsocket", i)))
52 // Get the port we're meant to be listening on with SSL
53 std::string port = Conf->ReadValue("bind", "port", i);
54 irc::portparser portrange(port, false);
56 while ((portno = portrange.GetToken()))
60 if (ServerInstance->Config->AddIOHook(portno, this))
62 listenports.push_back(portno);
63 for (size_t i = 0; i < ServerInstance->Config->ports.size(); i++)
64 if (ServerInstance->Config->ports[i]->GetPort() == portno)
65 ServerInstance->Config->ports[i]->SetDescription("xml");
69 ServerInstance->Log(DEFAULT, "m_xmlsocket.so: FAILED to enable XMLSocket on port %d, maybe you have another similar module loaded?", portno);
72 catch (ModuleException &e)
74 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());
83 virtual ~ModuleXMLSocket()
87 virtual void OnUnloadModule(Module* mod, const std::string &name)
91 for(unsigned int i = 0; i < listenports.size(); i++)
93 ServerInstance->Config->DelIOHook(listenports[i]);
94 for (size_t j = 0; j < ServerInstance->Config->ports.size(); j++)
95 if (ServerInstance->Config->ports[j]->GetPort() == listenports[i])
96 ServerInstance->Config->ports[j]->SetDescription("plaintext");
101 virtual Version GetVersion()
103 return Version(1, 1, 0, 0, VF_VENDOR, API_VERSION);
106 void Implements(char* List)
108 List[I_OnUnloadModule] = List[I_OnRawSocketRead] = List[I_OnRawSocketWrite] = List[I_OnRehash] = 1;
111 virtual int OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult)
113 userrec* user = dynamic_cast<userrec*>(ServerInstance->FindDescriptor(fd));
118 int result = user->ReadData(buffer, count);
120 if ((result == -1) && (errno == EAGAIN))
125 /* XXX: The core is more than happy to split lines purely on an \n
126 * rather than a \r\n. This is good for us as it means that the size
127 * of data we are receiving is exactly the same as the size of data
128 * we asked for, and we dont need to re-implement our own socket
129 * buffering (See below)
131 for (int n = 0; n < result; n++)
139 virtual int OnRawSocketWrite(int fd, const char* buffer, int count)
141 userrec* user = dynamic_cast<userrec*>(ServerInstance->FindDescriptor(fd));
146 /* We want to alter the buffer, so we have to make a copy */
147 char * tmpbuffer = new char[count + 1];
148 memcpy(tmpbuffer, buffer, count);
150 /* XXX: This will actually generate lines "looking\0\0like\0\0this"
151 * rather than lines "looking\0like\0this". This shouldnt be a problem
152 * to the client, but it saves us a TON of processing and the need
153 * to re-implement socket buffering, as the data we are sending is
154 * exactly the same length as the data we are receiving.
156 for (int n = 0; n < count; n++)
157 if ((tmpbuffer[n] == '\r') || (tmpbuffer[n] == '\n'))
160 user->AddWriteBuf(std::string(tmpbuffer,count));
168 class ModuleXMLSocketFactory : public ModuleFactory
171 ModuleXMLSocketFactory()
175 ~ModuleXMLSocketFactory()
179 virtual Module * CreateModule(InspIRCd* Me)
181 return new ModuleXMLSocket(Me);
186 extern "C" DllExport void * init_module( void )
188 return new ModuleXMLSocketFactory;