1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 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)
29 Implementation eventlist[] = { I_OnUnloadModule, I_OnRawSocketRead, I_OnRawSocketWrite, I_OnRehash };
30 ServerInstance->Modules->Attach(eventlist, this, 4);
33 virtual void OnRehash(User* user, const std::string ¶m)
36 Conf = new ConfigReader(ServerInstance);
38 for (unsigned int i = 0; i < listenports.size(); i++)
40 ServerInstance->Config->DelIOHook(listenports[i]);
45 for (int i = 0; i < Conf->Enumerate("bind"); i++)
47 // For each <bind> tag
48 std::string x = Conf->ReadValue("bind", "type", i);
49 if (((x.empty()) || (x == "clients")) && (Conf->ReadFlag("bind", "xmlsocket", i)))
51 // Get the port we're meant to be listening on with SSL
52 std::string port = Conf->ReadValue("bind", "port", i);
53 irc::portparser portrange(port, false);
55 while ((portno = portrange.GetToken()))
59 if (ServerInstance->Config->AddIOHook(portno, this))
61 listenports.push_back(portno);
62 for (size_t j = 0; j < ServerInstance->Config->ports.size(); j++)
63 if (ServerInstance->Config->ports[j]->GetPort() == portno)
64 ServerInstance->Config->ports[j]->SetDescription("xml");
68 ServerInstance->Log(DEFAULT, "m_xmlsocket.so: FAILED to enable XMLSocket on port %d, maybe you have another similar module loaded?", portno);
71 catch (ModuleException &e)
73 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());
82 virtual ~ModuleXMLSocket()
86 virtual void OnUnloadModule(Module* mod, const std::string &name)
90 for(unsigned int i = 0; i < listenports.size(); i++)
92 ServerInstance->Config->DelIOHook(listenports[i]);
93 for (size_t j = 0; j < ServerInstance->Config->ports.size(); j++)
94 if (ServerInstance->Config->ports[j]->GetPort() == listenports[i])
95 ServerInstance->Config->ports[j]->SetDescription("plaintext");
100 virtual Version GetVersion()
102 return Version(1, 1, 0, 0, VF_VENDOR, API_VERSION);
106 virtual int OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult)
108 User* user = dynamic_cast<User*>(ServerInstance->FindDescriptor(fd));
113 int result = user->ReadData(buffer, count);
115 if ((result == -1) && (errno == EAGAIN))
120 /* XXX: The core is more than happy to split lines purely on an \n
121 * rather than a \r\n. This is good for us as it means that the size
122 * of data we are receiving is exactly the same as the size of data
123 * we asked for, and we dont need to re-implement our own socket
124 * buffering (See below)
126 for (int n = 0; n < result; n++)
134 virtual int OnRawSocketWrite(int fd, const char* buffer, int count)
136 User* user = dynamic_cast<User*>(ServerInstance->FindDescriptor(fd));
141 /* We want to alter the buffer, so we have to make a copy */
142 char * tmpbuffer = new char[count + 1];
143 memcpy(tmpbuffer, buffer, count);
145 /* XXX: This will actually generate lines "looking\0\0like\0\0this"
146 * rather than lines "looking\0like\0this". This shouldnt be a problem
147 * to the client, but it saves us a TON of processing and the need
148 * to re-implement socket buffering, as the data we are sending is
149 * exactly the same length as the data we are receiving.
151 for (int n = 0; n < count; n++)
152 if ((tmpbuffer[n] == '\r') || (tmpbuffer[n] == '\n'))
155 user->AddWriteBuf(std::string(tmpbuffer,count));
163 MODULE_INIT(ModuleXMLSocket)