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