]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_xmlsocket.cpp
face79635ae2d99b80b51724b62d77675c9b146e
[user/henk/code/inspircd.git] / src / modules / m_xmlsocket.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "users.h"
16 #include "channels.h"
17 #include "modules.h"
18 #include "hashcomp.h"
19
20 /* $ModDesc: Provides XMLSocket support for clients */
21
22 class ModuleXMLSocket : public Module
23 {
24         ConfigReader* Conf;
25         std::vector<int> listenports;
26
27  public:
28
29         ModuleXMLSocket(InspIRCd* Me)
30                 : Module(Me)
31         {
32                 OnRehash(NULL,"");
33         }
34
35         virtual void OnRehash(userrec* user, const std::string &param)
36         {
37
38                 Conf = new ConfigReader(ServerInstance);
39
40                 for (unsigned int i = 0; i < listenports.size(); i++)
41                 {
42                         ServerInstance->Config->DelIOHook(listenports[i]);
43                 }
44
45                 listenports.clear();
46
47                 for (int i = 0; i < Conf->Enumerate("bind"); i++)
48                 {
49                         // For each <bind> tag
50                         if (((Conf->ReadValue("bind", "type", i) == "") || (Conf->ReadValue("bind", "type", i) == "clients")) && (Conf->ReadFlag("bind", "xmlsocket", i)))
51                         {
52                                 // Get the port we're meant to be listening on with SSL
53                                 std::string port = Conf->ReadValue("bind", "port", i);
54                                 irc::portparser portrange(port, false);
55                                 long portno = -1;
56                                 while ((portno = portrange.GetToken()))
57                                 {
58                                         try
59                                         {
60                                                 if (ServerInstance->Config->AddIOHook(portno, this))
61                                                 {
62                                                         listenports.push_back(portno);
63                                                                 for (size_t i = 0; i < ServerInstance->Config->ports.size(); i++)
64                                                                 if (ServerInstance->Config->ports[i]->GetPort() == portno)
65                                                                         ServerInstance->Config->ports[i]->SetDescription("xml");
66                                                 }
67                                                 else
68                                                 {
69                                                         ServerInstance->Log(DEFAULT, "m_xmlsocket.so: FAILED to enable XMLSocket on port %d, maybe you have another similar module loaded?", portno);
70                                                 }
71                                         }
72                                         catch (ModuleException &e)
73                                         {
74                                                 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());
75                                         }
76                                 }
77                         }
78                 }
79
80                 DELETE(Conf);
81         }
82
83         virtual ~ModuleXMLSocket()
84         {
85         }
86
87         virtual void OnUnloadModule(Module* mod, const std::string &name)
88         {
89                 if (mod == this)
90                 {
91                         for(unsigned int i = 0; i < listenports.size(); i++)
92                         {
93                                 ServerInstance->Config->DelIOHook(listenports[i]);
94                                 for (size_t j = 0; j < ServerInstance->Config->ports.size(); j++)
95                                         if (ServerInstance->Config->ports[j]->GetPort() == listenports[i])
96                                                 ServerInstance->Config->ports[j]->SetDescription("plaintext");
97                         }
98                 }
99         }
100
101         virtual Version GetVersion()
102         {
103                 return Version(1, 1, 0, 0, VF_VENDOR, API_VERSION);
104         }
105
106         void Implements(char* List)
107         {
108                 List[I_OnUnloadModule] = List[I_OnRawSocketRead] = List[I_OnRawSocketWrite] = List[I_OnRehash] = 1;
109         }
110
111         virtual int OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult)
112         {
113                 userrec* user = dynamic_cast<userrec*>(ServerInstance->FindDescriptor(fd));
114
115                 if (user == NULL)
116                         return -1;
117
118                 int result = user->ReadData(buffer, count);
119
120                 if ((result == -1) && (errno == EAGAIN))
121                         return -1;
122                 else if (result < 1)
123                         return 0;
124
125                 /* XXX: The core is more than happy to split lines purely on an \n
126                  * rather than a \r\n. This is good for us as it means that the size
127                  * of data we are receiving is exactly the same as the size of data
128                  * we asked for, and we dont need to re-implement our own socket
129                  * buffering (See below)
130                  */
131                 for (int n = 0; n < result; n++)
132                         if (buffer[n] == 0)
133                                 buffer[n] = '\n';
134
135                 readresult = result;
136                 return result;
137         }
138
139         virtual int OnRawSocketWrite(int fd, const char* buffer, int count)
140         {
141                 userrec* user = dynamic_cast<userrec*>(ServerInstance->FindDescriptor(fd));
142
143                 if (user == NULL)
144                         return -1;
145
146                 /* We want to alter the buffer, so we have to make a copy */
147                 char * tmpbuffer = new char[count + 1];
148                 memcpy(tmpbuffer, buffer, count);
149
150                 /* XXX: This will actually generate lines "looking\0\0like\0\0this"
151                  * rather than lines "looking\0like\0this". This shouldnt be a problem
152                  * to the client, but it saves us a TON of processing and the need
153                  * to re-implement socket buffering, as the data we are sending is
154                  * exactly the same length as the data we are receiving.
155                  */
156                 for (int n = 0; n < count; n++)
157                         if ((tmpbuffer[n] == '\r') || (tmpbuffer[n] == '\n'))
158                                 tmpbuffer[n] = 0;
159
160                 user->AddWriteBuf(std::string(tmpbuffer,count));
161                 delete [] tmpbuffer;
162
163                 return 1;
164         }
165
166 };
167
168 class ModuleXMLSocketFactory : public ModuleFactory
169 {
170  public:
171         ModuleXMLSocketFactory()
172         {
173         }
174
175         ~ModuleXMLSocketFactory()
176         {
177         }
178
179         virtual Module * CreateModule(InspIRCd* Me)
180         {
181                 return new ModuleXMLSocket(Me);
182         }
183 };
184
185
186 extern "C" DllExport void * init_module( void )
187 {
188         return new ModuleXMLSocketFactory;
189 }