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