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