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<std::string> listenports;
25 ModuleXMLSocket(InspIRCd* Me)
29 Implementation eventlist[] = { I_OnUnloadModule, I_OnRawSocketRead, I_OnRawSocketWrite, I_OnRehash, I_OnHookUserIO, I_OnCleanup };
30 ServerInstance->Modules->Attach(eventlist, this, 6);
33 bool isin(const std::string &hostandport, const std::vector<std::string> &portlist)
35 return std::find(portlist.begin(), portlist.end(), hostandport) != portlist.end();
38 virtual void OnRehash(User* user, const std::string ¶m)
41 Conf = new ConfigReader(ServerInstance);
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 std::string addr = Conf->ReadValue("bind", "address", i);
54 irc::portparser portrange(port, false);
56 while ((portno = portrange.GetToken()))
60 listenports.push_back(addr + ":" + ConvToStr(portno));
61 for (size_t j = 0; j < ServerInstance->Config->ports.size(); j++)
62 if ((ServerInstance->Config->ports[i]->GetPort() == portno) && (ServerInstance->Config->ports[i]->GetIP() == addr))
63 ServerInstance->Config->ports[j]->SetDescription("xml");
65 catch (ModuleException &e)
67 ServerInstance->Logs->Log("m_xmlsocket",DEFAULT, "m_xmlsocket.so: FAILED to enable XMLSocket on port %d: %s. Maybe you have another similar module loaded?", portno, e.GetReason());
76 virtual ~ModuleXMLSocket()
80 virtual void OnUnloadModule(Module* mod, const std::string &name)
84 for(unsigned int i = 0; i < listenports.size(); i++)
86 for (size_t j = 0; j < ServerInstance->Config->ports.size(); j++)
87 if (listenports[i] == (ServerInstance->Config->ports[j]->GetIP()+":"+ConvToStr(ServerInstance->Config->ports[j]->GetPort())))
88 ServerInstance->Config->ports[j]->SetDescription("plaintext");
93 virtual void OnCleanup(int target_type, void* item)
95 if(target_type == TYPE_USER)
97 User* user = (User*)item;
103 virtual Version GetVersion()
105 return Version(1, 2, 0, 0, VF_VENDOR, API_VERSION);
108 virtual void OnHookUserIO(User* user, const std::string &targetip)
110 if (!user->io && isin(targetip+":"+ConvToStr(user->GetPort()),listenports))
112 /* Hook the user with our module */
117 virtual int OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult)
119 User* user = dynamic_cast<User*>(ServerInstance->FindDescriptor(fd));
124 int result = user->ReadData(buffer, count);
126 if ((result == -1) && (errno == EAGAIN))
131 /* XXX: The core is more than happy to split lines purely on an \n
132 * rather than a \r\n. This is good for us as it means that the size
133 * of data we are receiving is exactly the same as the size of data
134 * we asked for, and we dont need to re-implement our own socket
135 * buffering (See below)
137 for (int n = 0; n < result; n++)
145 virtual int OnRawSocketWrite(int fd, const char* buffer, int count)
147 User* user = dynamic_cast<User*>(ServerInstance->FindDescriptor(fd));
152 /* We want to alter the buffer, so we have to make a copy */
153 char * tmpbuffer = new char[count + 1];
154 memcpy(tmpbuffer, buffer, count);
156 /* XXX: This will actually generate lines "looking\0\0like\0\0this"
157 * rather than lines "looking\0like\0this". This shouldnt be a problem
158 * to the client, but it saves us a TON of processing and the need
159 * to re-implement socket buffering, as the data we are sending is
160 * exactly the same length as the data we are receiving.
162 for (int n = 0; n < count; n++)
163 if ((tmpbuffer[n] == '\r') || (tmpbuffer[n] == '\n'))
166 user->AddWriteBuf(std::string(tmpbuffer,count));
174 MODULE_INIT(ModuleXMLSocket)