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)
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 i = 0; i < ServerInstance->Config->ports.size(); i++)
63 if (ServerInstance->Config->ports[i]->GetPort() == portno)
64 ServerInstance->Config->ports[i]->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);
105 void Implements(char* List)
107 List[I_OnUnloadModule] = List[I_OnRawSocketRead] = List[I_OnRawSocketWrite] = List[I_OnRehash] = 1;
110 virtual int OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult)
112 User* user = dynamic_cast<User*>(ServerInstance->FindDescriptor(fd));
117 int result = user->ReadData(buffer, count);
119 if ((result == -1) && (errno == EAGAIN))
124 /* XXX: The core is more than happy to split lines purely on an \n
125 * rather than a \r\n. This is good for us as it means that the size
126 * of data we are receiving is exactly the same as the size of data
127 * we asked for, and we dont need to re-implement our own socket
128 * buffering (See below)
130 for (int n = 0; n < result; n++)
138 virtual int OnRawSocketWrite(int fd, const char* buffer, int count)
140 User* user = dynamic_cast<User*>(ServerInstance->FindDescriptor(fd));
145 /* We want to alter the buffer, so we have to make a copy */
146 char * tmpbuffer = new char[count + 1];
147 memcpy(tmpbuffer, buffer, count);
149 /* XXX: This will actually generate lines "looking\0\0like\0\0this"
150 * rather than lines "looking\0like\0this". This shouldnt be a problem
151 * to the client, but it saves us a TON of processing and the need
152 * to re-implement socket buffering, as the data we are sending is
153 * exactly the same length as the data we are receiving.
155 for (int n = 0; n < count; n++)
156 if ((tmpbuffer[n] == '\r') || (tmpbuffer[n] == '\n'))
159 user->AddWriteBuf(std::string(tmpbuffer,count));
167 MODULE_INIT(ModuleXMLSocket)