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 std::string x = Conf->ReadValue("bind", "type", i);
51 if (((x.empty()) || (x == "clients")) && (Conf->ReadFlag("bind", "xmlsocket", i)))
53 // Get the port we're meant to be listening on with SSL
54 std::string port = Conf->ReadValue("bind", "port", i);
55 irc::portparser portrange(port, false);
57 while ((portno = portrange.GetToken()))
61 if (ServerInstance->Config->AddIOHook(portno, this))
63 listenports.push_back(portno);
64 for (size_t i = 0; i < ServerInstance->Config->ports.size(); i++)
65 if (ServerInstance->Config->ports[i]->GetPort() == portno)
66 ServerInstance->Config->ports[i]->SetDescription("xml");
70 ServerInstance->Log(DEFAULT, "m_xmlsocket.so: FAILED to enable XMLSocket on port %d, maybe you have another similar module loaded?", portno);
73 catch (ModuleException &e)
75 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());
84 virtual ~ModuleXMLSocket()
88 virtual void OnUnloadModule(Module* mod, const std::string &name)
92 for(unsigned int i = 0; i < listenports.size(); i++)
94 ServerInstance->Config->DelIOHook(listenports[i]);
95 for (size_t j = 0; j < ServerInstance->Config->ports.size(); j++)
96 if (ServerInstance->Config->ports[j]->GetPort() == listenports[i])
97 ServerInstance->Config->ports[j]->SetDescription("plaintext");
102 virtual Version GetVersion()
104 return Version(1, 1, 0, 0, VF_VENDOR, API_VERSION);
107 void Implements(char* List)
109 List[I_OnUnloadModule] = List[I_OnRawSocketRead] = List[I_OnRawSocketWrite] = List[I_OnRehash] = 1;
112 virtual int OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult)
114 userrec* user = dynamic_cast<userrec*>(ServerInstance->FindDescriptor(fd));
119 int result = user->ReadData(buffer, count);
121 if ((result == -1) && (errno == EAGAIN))
126 /* XXX: The core is more than happy to split lines purely on an \n
127 * rather than a \r\n. This is good for us as it means that the size
128 * of data we are receiving is exactly the same as the size of data
129 * we asked for, and we dont need to re-implement our own socket
130 * buffering (See below)
132 for (int n = 0; n < result; n++)
140 virtual int OnRawSocketWrite(int fd, const char* buffer, int count)
142 userrec* user = dynamic_cast<userrec*>(ServerInstance->FindDescriptor(fd));
147 /* We want to alter the buffer, so we have to make a copy */
148 char * tmpbuffer = new char[count + 1];
149 memcpy(tmpbuffer, buffer, count);
151 /* XXX: This will actually generate lines "looking\0\0like\0\0this"
152 * rather than lines "looking\0like\0this". This shouldnt be a problem
153 * to the client, but it saves us a TON of processing and the need
154 * to re-implement socket buffering, as the data we are sending is
155 * exactly the same length as the data we are receiving.
157 for (int n = 0; n < count; n++)
158 if ((tmpbuffer[n] == '\r') || (tmpbuffer[n] == '\n'))
161 user->AddWriteBuf(std::string(tmpbuffer,count));
169 MODULE_INIT(ModuleXMLSocket)