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