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