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