]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/compat.cpp
ca9fbfa6aad69f61d891136c1fd9cb6c396c7ba1
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / compat.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *
6  * This file is part of InspIRCd.  InspIRCd is free software: you can
7  * redistribute it and/or modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation, version 2.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19
20 #include "inspircd.h"
21 #include "main.h"
22 #include "treesocket.h"
23 #include "treeserver.h"
24
25 static std::string newline("\n");
26
27 void TreeSocket::WriteLine(const std::string& original_line)
28 {
29         if (LinkState == CONNECTED)
30         {
31                 if (original_line.c_str()[0] != ':')
32                 {
33                         ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Sending line without server prefix!");
34                         WriteLine(":" + ServerInstance->Config->GetSID() + " " + original_line);
35                         return;
36                 }
37                 if (proto_version != ProtocolVersion)
38                 {
39                         std::string line = original_line;
40                         std::string::size_type a = line.find(' ');
41                         std::string::size_type b = line.find(' ', a + 1);
42                         std::string command = line.substr(a + 1, b-a-1);
43                         // now try to find a translation entry
44                         // TODO a more efficient lookup method will be needed later
45                         if (proto_version < 1205)
46                         {
47                                 if (command == "IJOIN")
48                                 {
49                                         // Convert
50                                         // :<uid> IJOIN <chan> [<ts> [<flags>]]
51                                         // to
52                                         // :<sid> FJOIN <chan> <ts> + [<flags>],<uuid>
53                                         std::string::size_type c = line.find(' ', b + 1);
54                                         if (c == std::string::npos)
55                                         {
56                                                 // No TS or modes in the command
57                                                 // :22DAAAAAB IJOIN #chan
58                                                 const std::string channame = line.substr(b+1, c-b-1);
59                                                 Channel* chan = ServerInstance->FindChan(channame);
60                                                 if (!chan)
61                                                         return;
62
63                                                 line.push_back(' ');
64                                                 line.append(ConvToStr(chan->age));
65                                                 line.append(" + ,");
66                                         }
67                                         else
68                                         {
69                                                 std::string::size_type d = line.find(' ', c + 1);
70                                                 if (d == std::string::npos)
71                                                 {
72                                                         // TS present, no modes
73                                                         // :22DAAAAAC IJOIN #chan 12345
74                                                         line.append(" + ,");
75                                                 }
76                                                 else
77                                                 {
78                                                         // Both TS and modes are present
79                                                         // :22DAAAAAC IJOIN #chan 12345 ov
80                                                         std::string::size_type e = line.find(' ', d + 1);
81                                                         if (e != std::string::npos)
82                                                                 line.erase(e);
83
84                                                         line.insert(d, " +");
85                                                         line.push_back(',');
86                                                 }
87                                         }
88
89                                         // Move the uuid to the end and replace the I with an F
90                                         line.append(line.substr(1, 9));
91                                         line.erase(4, 6);
92                                         line[5] = 'F';
93                                 }
94                                 else if (command == "RESYNC")
95                                         return;
96                                 else if (command == "METADATA")
97                                 {
98                                         // Drop TS for channel METADATA, translate METADATA operquit into an OPERQUIT command
99                                         // :sid METADATA #target TS extname ...
100                                         //     A        B       C  D
101                                         if (b == std::string::npos)
102                                                 return;
103
104                                         std::string::size_type c = line.find(' ', b + 1);
105                                         if (c == std::string::npos)
106                                                 return;
107
108                                         std::string::size_type d = line.find(' ', c + 1);
109                                         if (d == std::string::npos)
110                                                 return;
111
112                                         if (line[b + 1] == '#')
113                                         {
114                                                 // We're sending channel metadata
115                                                 line.erase(c, d-c);
116                                         }
117                                         else if (!line.compare(c, d-c, " operquit", 9))
118                                         {
119                                                 // ":22D METADATA 22DAAAAAX operquit :message" -> ":22DAAAAAX OPERQUIT :message"
120                                                 line = ":" + line.substr(b+1, c-b) + "OPERQUIT" + line.substr(d);
121                                         }
122                                 }
123                                 else if (command == "FTOPIC")
124                                 {
125                                         // Drop channel TS for FTOPIC
126                                         // :sid FTOPIC #target TS TopicTS setter :newtopic
127                                         //     A      B       C  D       E      F
128                                         // :uid FTOPIC #target TS TopicTS :newtopic
129                                         //     A      B       C  D       E
130                                         if (b == std::string::npos)
131                                                 return;
132
133                                         std::string::size_type c = line.find(' ', b + 1);
134                                         if (c == std::string::npos)
135                                                 return;
136
137                                         std::string::size_type d = line.find(' ', c + 1);
138                                         if (d == std::string::npos)
139                                                 return;
140
141                                         std::string::size_type e = line.find(' ', d + 1);
142                                         if (line[e+1] == ':')
143                                         {
144                                                 line.erase(c, e-c);
145                                                 line.erase(a+1, 1);
146                                         }
147                                         else
148                                                 line.erase(c, d-c);
149                                 }
150                                 else if ((command == "PING") || (command == "PONG"))
151                                 {
152                                         // :22D PING 20D
153                                         if (line.length() < 13)
154                                                 return;
155
156                                         // Insert the source SID (and a space) between the command and the first parameter
157                                         line.insert(10, line.substr(1, 4));
158                                 }
159                                 else if (command == "OPERTYPE")
160                                 {
161                                         std::string::size_type colon = line.find(':', b);
162                                         if (colon != std::string::npos)
163                                         {
164                                                 for (std::string::iterator i = line.begin()+colon; i != line.end(); ++i)
165                                                 {
166                                                         if (*i == ' ')
167                                                                 *i = '_';
168                                                 }
169                                                 line.erase(colon, 1);
170                                         }
171                                 }
172                                 else if (command == "INVITE")
173                                 {
174                                         // :22D INVITE 22DAAAAAN #chan TS ExpirationTime
175                                         //     A      B         C     D  E
176                                         if (b == std::string::npos)
177                                                 return;
178
179                                         std::string::size_type c = line.find(' ', b + 1);
180                                         if (c == std::string::npos)
181                                                 return;
182
183                                         std::string::size_type d = line.find(' ', c + 1);
184                                         if (d == std::string::npos)
185                                                 return;
186
187                                         std::string::size_type e = line.find(' ', d + 1);
188                                         // If there is no expiration time then everything will be erased from 'd'
189                                         line.erase(d, e-d);
190                                 }
191                         }
192                         ServerInstance->Logs->Log(MODNAME, LOG_RAWIO, "S[%d] O %s", this->GetFd(), line.c_str());
193                         this->WriteData(line);
194                         this->WriteData(newline);
195                         return;
196                 }
197         }
198
199         ServerInstance->Logs->Log(MODNAME, LOG_RAWIO, "S[%d] O %s", this->GetFd(), original_line.c_str());
200         this->WriteData(original_line);
201         this->WriteData(newline);
202 }
203
204 namespace
205 {
206         bool InsertCurrentChannelTS(std::vector<std::string>& params, unsigned int chanindex = 0, unsigned int pos = 1)
207         {
208                 Channel* chan = ServerInstance->FindChan(params[chanindex]);
209                 if (!chan)
210                         return false;
211
212                 // Insert the current TS of the channel after the pos-th parameter
213                 params.insert(params.begin()+pos, ConvToStr(chan->age));
214                 return true;
215         }
216 }
217
218 bool TreeSocket::PreProcessOldProtocolMessage(User*& who, std::string& cmd, std::vector<std::string>& params)
219 {
220         if ((cmd == "METADATA") && (params.size() >= 3) && (params[0][0] == '#'))
221         {
222                 // :20D METADATA #channel extname :extdata
223                 return InsertCurrentChannelTS(params);
224         }
225         else if ((cmd == "FTOPIC") && (params.size() >= 4))
226         {
227                 // :20D FTOPIC #channel 100 Attila :topic text
228                 return InsertCurrentChannelTS(params);
229         }
230         else if ((cmd == "PING") || (cmd == "PONG"))
231         {
232                 if (params.size() == 1)
233                 {
234                         // If it's a PING with 1 parameter, reply with a PONG now, if it's a PONG with 1 parameter (weird), do nothing
235                         if (cmd[1] == 'I')
236                                 this->WriteData(":" + ServerInstance->Config->GetSID() + " PONG " + params[0] + newline);
237
238                         // Don't process this message further
239                         return false;
240                 }
241
242                 // :20D PING 20D 22D
243                 // :20D PONG 20D 22D
244                 // Drop the first parameter
245                 params.erase(params.begin());
246
247                 // If the target is a server name, translate it to a SID
248                 if (!InspIRCd::IsSID(params[0]))
249                 {
250                         TreeServer* server = Utils->FindServer(params[0]);
251                         if (!server)
252                         {
253                                 // We've no idea what this is, log and stop processing
254                                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Received a " + cmd + " with an unknown target: \"" + params[0] + "\", command dropped");
255                                 return false;
256                         }
257
258                         params[0] = server->GetID();
259                 }
260         }
261         else if ((cmd == "GLINE") || (cmd == "KLINE") || (cmd == "ELINE") || (cmd == "ZLINE") || (cmd == "QLINE"))
262         {
263                 // Fix undocumented protocol usage: translate GLINE, ZLINE, etc. into ADDLINE or DELLINE
264                 if ((params.size() != 1) && (params.size() != 3))
265                         return false;
266
267                 parameterlist p;
268                 p.push_back(cmd.substr(0, 1));
269                 p.push_back(params[0]);
270
271                 if (params.size() == 3)
272                 {
273                         cmd = "ADDLINE";
274                         p.push_back(who->nick);
275                         p.push_back(ConvToStr(ServerInstance->Time()));
276                         p.push_back(ConvToStr(InspIRCd::Duration(params[1])));
277                         p.push_back(params[2]);
278                 }
279                 else
280                         cmd = "DELLINE";
281
282                 params.swap(p);
283         }
284         else if (cmd == "SVSMODE")
285         {
286                 cmd = "MODE";
287         }
288         else if (cmd == "OPERQUIT")
289         {
290                 // Translate OPERQUIT into METADATA
291                 if (params.empty())
292                         return false;
293
294                 cmd = "METADATA";
295                 params.insert(params.begin(), who->uuid);
296                 params.insert(params.begin()+1, "operquit");
297                 who = MyRoot->ServerUser;
298         }
299         else if ((cmd == "TOPIC") && (params.size() >= 2))
300         {
301                 // :20DAAAAAC TOPIC #chan :new topic
302                 cmd = "FTOPIC";
303                 if (!InsertCurrentChannelTS(params))
304                         return false;
305
306                 params.insert(params.begin()+2, ConvToStr(ServerInstance->Time()));
307         }
308         else if (cmd == "MODENOTICE")
309         {
310                 // MODENOTICE is always supported by 2.0 but it's optional in 2.2.
311                 params.insert(params.begin(), "*");
312                 params.insert(params.begin()+1, cmd);
313                 cmd = "ENCAP";
314         }
315         else if (cmd == "RULES")
316         {
317                 return false;
318         }
319         else if (cmd == "INVITE")
320         {
321                 // :20D INVITE 22DAAABBB #chan
322                 // :20D INVITE 22DAAABBB #chan 123456789
323                 // Insert channel timestamp after the channel name; the 3rd parameter, if there, is the invite expiration time
324                 return InsertCurrentChannelTS(params, 1, 2);
325         }
326
327         return true; // Passthru
328 }