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 * ---------------------------------------------------
16 /* $ModDesc: Provides XMLSocket support for clients */
18 class ModuleXMLSocket : public Module
21 std::vector<int> listenports;
25 ModuleXMLSocket(InspIRCd* Me)
31 virtual void OnRehash(userrec* user, const std::string ¶m)
34 Conf = new ConfigReader(ServerInstance);
36 for (unsigned int i = 0; i < listenports.size(); i++)
38 ServerInstance->Config->DelIOHook(listenports[i]);
43 for (int i = 0; i < Conf->Enumerate("bind"); i++)
45 // For each <bind> tag
46 std::string x = Conf->ReadValue("bind", "type", i);
47 if (((x.empty()) || (x == "clients")) && (Conf->ReadFlag("bind", "xmlsocket", i)))
49 // Get the port we're meant to be listening on with SSL
50 std::string port = Conf->ReadValue("bind", "port", i);
51 irc::portparser portrange(port, false);
53 while ((portno = portrange.GetToken()))
57 if (ServerInstance->Config->AddIOHook(portno, this))
59 listenports.push_back(portno);
60 for (size_t i = 0; i < ServerInstance->Config->ports.size(); i++)
61 if (ServerInstance->Config->ports[i]->GetPort() == portno)
62 ServerInstance->Config->ports[i]->SetDescription("xml");
66 ServerInstance->Log(DEFAULT, "m_xmlsocket.so: FAILED to enable XMLSocket on port %d, maybe you have another similar module loaded?", portno);
69 catch (ModuleException &e)
71 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());
80 virtual ~ModuleXMLSocket()
84 virtual void OnUnloadModule(Module* mod, const std::string &name)
88 for(unsigned int i = 0; i < listenports.size(); i++)
90 ServerInstance->Config->DelIOHook(listenports[i]);
91 for (size_t j = 0; j < ServerInstance->Config->ports.size(); j++)
92 if (ServerInstance->Config->ports[j]->GetPort() == listenports[i])
93 ServerInstance->Config->ports[j]->SetDescription("plaintext");
98 virtual Version GetVersion()
100 return Version(1, 1, 0, 0, VF_VENDOR, API_VERSION);
103 void Implements(char* List)
105 List[I_OnUnloadModule] = List[I_OnRawSocketRead] = List[I_OnRawSocketWrite] = List[I_OnRehash] = 1;
108 virtual int OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult)
110 userrec* user = dynamic_cast<userrec*>(ServerInstance->FindDescriptor(fd));
115 int result = user->ReadData(buffer, count);
117 if ((result == -1) && (errno == EAGAIN))
122 /* XXX: The core is more than happy to split lines purely on an \n
123 * rather than a \r\n. This is good for us as it means that the size
124 * of data we are receiving is exactly the same as the size of data
125 * we asked for, and we dont need to re-implement our own socket
126 * buffering (See below)
128 for (int n = 0; n < result; n++)
136 virtual int OnRawSocketWrite(int fd, const char* buffer, int count)
138 userrec* user = dynamic_cast<userrec*>(ServerInstance->FindDescriptor(fd));
143 /* We want to alter the buffer, so we have to make a copy */
144 char * tmpbuffer = new char[count + 1];
145 memcpy(tmpbuffer, buffer, count);
147 /* XXX: This will actually generate lines "looking\0\0like\0\0this"
148 * rather than lines "looking\0like\0this". This shouldnt be a problem
149 * to the client, but it saves us a TON of processing and the need
150 * to re-implement socket buffering, as the data we are sending is
151 * exactly the same length as the data we are receiving.
153 for (int n = 0; n < count; n++)
154 if ((tmpbuffer[n] == '\r') || (tmpbuffer[n] == '\n'))
157 user->AddWriteBuf(std::string(tmpbuffer,count));
165 MODULE_INIT(ModuleXMLSocket)