]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_xmlsocket.cpp
First two conversions
[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                         std::string x = Conf->ReadValue("bind", "type", i);
51                         if (((x.empty()) || (x == "clients")) && (Conf->ReadFlag("bind", "xmlsocket", i)))
52                         {
53                                 // Get the port we're meant to be listening on with SSL
54                                 std::string port = Conf->ReadValue("bind", "port", i);
55                                 irc::portparser portrange(port, false);
56                                 long portno = -1;
57                                 while ((portno = portrange.GetToken()))
58                                 {
59                                         try
60                                         {
61                                                 if (ServerInstance->Config->AddIOHook(portno, this))
62                                                 {
63                                                         listenports.push_back(portno);
64                                                                 for (size_t i = 0; i < ServerInstance->Config->ports.size(); i++)
65                                                                 if (ServerInstance->Config->ports[i]->GetPort() == portno)
66                                                                         ServerInstance->Config->ports[i]->SetDescription("xml");
67                                                 }
68                                                 else
69                                                 {
70                                                         ServerInstance->Log(DEFAULT, "m_xmlsocket.so: FAILED to enable XMLSocket on port %d, maybe you have another similar module loaded?", portno);
71                                                 }
72                                         }
73                                         catch (ModuleException &e)
74                                         {
75                                                 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());
76                                         }
77                                 }
78                         }
79                 }
80
81                 DELETE(Conf);
82         }
83
84         virtual ~ModuleXMLSocket()
85         {
86         }
87
88         virtual void OnUnloadModule(Module* mod, const std::string &name)
89         {
90                 if (mod == this)
91                 {
92                         for(unsigned int i = 0; i < listenports.size(); i++)
93                         {
94                                 ServerInstance->Config->DelIOHook(listenports[i]);
95                                 for (size_t j = 0; j < ServerInstance->Config->ports.size(); j++)
96                                         if (ServerInstance->Config->ports[j]->GetPort() == listenports[i])
97                                                 ServerInstance->Config->ports[j]->SetDescription("plaintext");
98                         }
99                 }
100         }
101
102         virtual Version GetVersion()
103         {
104                 return Version(1, 1, 0, 0, VF_VENDOR, API_VERSION);
105         }
106
107         void Implements(char* List)
108         {
109                 List[I_OnUnloadModule] = List[I_OnRawSocketRead] = List[I_OnRawSocketWrite] = List[I_OnRehash] = 1;
110         }
111
112         virtual int OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult)
113         {
114                 userrec* user = dynamic_cast<userrec*>(ServerInstance->FindDescriptor(fd));
115
116                 if (user == NULL)
117                         return -1;
118
119                 int result = user->ReadData(buffer, count);
120
121                 if ((result == -1) && (errno == EAGAIN))
122                         return -1;
123                 else if (result < 1)
124                         return 0;
125
126                 /* XXX: The core is more than happy to split lines purely on an \n
127                  * rather than a \r\n. This is good for us as it means that the size
128                  * of data we are receiving is exactly the same as the size of data
129                  * we asked for, and we dont need to re-implement our own socket
130                  * buffering (See below)
131                  */
132                 for (int n = 0; n < result; n++)
133                         if (buffer[n] == 0)
134                                 buffer[n] = '\n';
135
136                 readresult = result;
137                 return result;
138         }
139
140         virtual int OnRawSocketWrite(int fd, const char* buffer, int count)
141         {
142                 userrec* user = dynamic_cast<userrec*>(ServerInstance->FindDescriptor(fd));
143
144                 if (user == NULL)
145                         return -1;
146
147                 /* We want to alter the buffer, so we have to make a copy */
148                 char * tmpbuffer = new char[count + 1];
149                 memcpy(tmpbuffer, buffer, count);
150
151                 /* XXX: This will actually generate lines "looking\0\0like\0\0this"
152                  * rather than lines "looking\0like\0this". This shouldnt be a problem
153                  * to the client, but it saves us a TON of processing and the need
154                  * to re-implement socket buffering, as the data we are sending is
155                  * exactly the same length as the data we are receiving.
156                  */
157                 for (int n = 0; n < count; n++)
158                         if ((tmpbuffer[n] == '\r') || (tmpbuffer[n] == '\n'))
159                                 tmpbuffer[n] = 0;
160
161                 user->AddWriteBuf(std::string(tmpbuffer,count));
162                 delete [] tmpbuffer;
163
164                 return 1;
165         }
166
167 };
168
169 MODULE_INIT(ModuleXMLSocket);
170