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 &host, int port, const std::vector<std::string> &portlist)
35 if (std::find(portlist.begin(), portlist.end(), "*:" + ConvToStr(port)) != portlist.end())
38 if (std::find(portlist.begin(), portlist.end(), ":" + ConvToStr(port)) != portlist.end())
41 return std::find(portlist.begin(), portlist.end(), host + ":" + ConvToStr(port)) != portlist.end();
44 virtual void OnRehash(User* user, const std::string ¶m)
47 Conf = new ConfigReader(ServerInstance);
51 for (int i = 0; i < Conf->Enumerate("bind"); i++)
53 // For each <bind> tag
54 std::string x = Conf->ReadValue("bind", "type", i);
55 if (((x.empty()) || (x == "clients")) && (Conf->ReadFlag("bind", "xmlsocket", i)))
57 // Get the port we're meant to be listening on with SSL
58 std::string port = Conf->ReadValue("bind", "port", i);
59 std::string addr = Conf->ReadValue("bind", "address", i);
61 irc::portparser portrange(port, false);
63 while ((portno = portrange.GetToken()))
67 listenports.push_back(addr + ":" + ConvToStr(portno));
68 for (size_t j = 0; j < ServerInstance->Config->ports.size(); j++)
69 if ((ServerInstance->Config->ports[j]->GetPort() == portno) && (ServerInstance->Config->ports[j]->GetIP() == addr))
70 ServerInstance->Config->ports[j]->SetDescription("xml");
72 catch (ModuleException &e)
74 ServerInstance->Logs->Log("m_xmlsocket",DEFAULT, "m_xmlsocket.so: FAILED to enable XMLSocket on port %ld: %s. Maybe 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 for (size_t j = 0; j < ServerInstance->Config->ports.size(); j++)
94 if (listenports[i] == (ServerInstance->Config->ports[j]->GetIP()+":"+ConvToStr(ServerInstance->Config->ports[j]->GetPort())))
95 ServerInstance->Config->ports[j]->SetDescription("plaintext");
100 virtual void OnCleanup(int target_type, void* item)
102 if(target_type == TYPE_USER)
104 User* user = (User*)item;
110 virtual Version GetVersion()
112 return Version("$Id$", VF_VENDOR, API_VERSION);
115 virtual void OnHookUserIO(User* user, const std::string &targetip)
117 if (!user->io && isin(targetip,user->GetPort(),listenports))
119 /* Hook the user with our module */
124 virtual int OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult)
126 User* user = dynamic_cast<User*>(ServerInstance->FindDescriptor(fd));
131 int result = user->ReadData(buffer, count);
133 if ((result == -1) && (errno == EAGAIN))
138 /* XXX: The core is more than happy to split lines purely on an \n
139 * rather than a \r\n. This is good for us as it means that the size
140 * of data we are receiving is exactly the same as the size of data
141 * we asked for, and we dont need to re-implement our own socket
142 * buffering (See below)
144 for (int n = 0; n < result; n++)
152 virtual int OnRawSocketWrite(int fd, const char* buffer, int count)
154 User* user = dynamic_cast<User*>(ServerInstance->FindDescriptor(fd));
159 /* We want to alter the buffer, so we have to make a copy */
160 char * tmpbuffer = new char[count + 1];
161 memcpy(tmpbuffer, buffer, count);
163 /* XXX: This will actually generate lines "looking\0\0like\0\0this"
164 * rather than lines "looking\0like\0this". This shouldnt be a problem
165 * to the client, but it saves us a TON of processing and the need
166 * to re-implement socket buffering, as the data we are sending is
167 * exactly the same length as the data we are receiving.
169 for (int n = 0; n < count; n++)
170 if ((tmpbuffer[n] == '\r') || (tmpbuffer[n] == '\n'))
173 user->AddWriteBuf(std::string(tmpbuffer,count));
181 MODULE_INIT(ModuleXMLSocket)