]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/compat.cpp
m_spanningtree Translate the new SERVER message for 1202 protocol servers
[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> <membid> [<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                                                 return;
56
57                                         std::string::size_type d = line.find(' ', c + 1);
58                                         // Erase membership id first
59                                         line.erase(c, d-c);
60                                         if (d == std::string::npos)
61                                         {
62                                                 // No TS or modes in the command
63                                                 // :22DAAAAAB IJOIN #chan
64                                                 const std::string channame = line.substr(b+1, c-b-1);
65                                                 Channel* chan = ServerInstance->FindChan(channame);
66                                                 if (!chan)
67                                                         return;
68
69                                                 line.push_back(' ');
70                                                 line.append(ConvToStr(chan->age));
71                                                 line.append(" + ,");
72                                         }
73                                         else
74                                         {
75                                                 d = line.find(' ', c + 1);
76                                                 if (d == std::string::npos)
77                                                 {
78                                                         // TS present, no modes
79                                                         // :22DAAAAAC IJOIN #chan 12345
80                                                         line.append(" + ,");
81                                                 }
82                                                 else
83                                                 {
84                                                         // Both TS and modes are present
85                                                         // :22DAAAAAC IJOIN #chan 12345 ov
86                                                         std::string::size_type e = line.find(' ', d + 1);
87                                                         if (e != std::string::npos)
88                                                                 line.erase(e);
89
90                                                         line.insert(d, " +");
91                                                         line.push_back(',');
92                                                 }
93                                         }
94
95                                         // Move the uuid to the end and replace the I with an F
96                                         line.append(line.substr(1, 9));
97                                         line.erase(4, 6);
98                                         line[5] = 'F';
99                                 }
100                                 else if (command == "RESYNC")
101                                         return;
102                                 else if (command == "METADATA")
103                                 {
104                                         // Drop TS for channel METADATA, translate METADATA operquit into an OPERQUIT command
105                                         // :sid METADATA #target TS extname ...
106                                         //     A        B       C  D
107                                         if (b == std::string::npos)
108                                                 return;
109
110                                         std::string::size_type c = line.find(' ', b + 1);
111                                         if (c == std::string::npos)
112                                                 return;
113
114                                         std::string::size_type d = line.find(' ', c + 1);
115                                         if (d == std::string::npos)
116                                                 return;
117
118                                         if (line[b + 1] == '#')
119                                         {
120                                                 // We're sending channel metadata
121                                                 line.erase(c, d-c);
122                                         }
123                                         else if (!line.compare(c, d-c, " operquit", 9))
124                                         {
125                                                 // ":22D METADATA 22DAAAAAX operquit :message" -> ":22DAAAAAX OPERQUIT :message"
126                                                 line = ":" + line.substr(b+1, c-b) + "OPERQUIT" + line.substr(d);
127                                         }
128                                 }
129                                 else if (command == "FTOPIC")
130                                 {
131                                         // Drop channel TS for FTOPIC
132                                         // :sid FTOPIC #target TS TopicTS setter :newtopic
133                                         //     A      B       C  D       E      F
134                                         // :uid FTOPIC #target TS TopicTS :newtopic
135                                         //     A      B       C  D       E
136                                         if (b == std::string::npos)
137                                                 return;
138
139                                         std::string::size_type c = line.find(' ', b + 1);
140                                         if (c == std::string::npos)
141                                                 return;
142
143                                         std::string::size_type d = line.find(' ', c + 1);
144                                         if (d == std::string::npos)
145                                                 return;
146
147                                         std::string::size_type e = line.find(' ', d + 1);
148                                         if (line[e+1] == ':')
149                                         {
150                                                 line.erase(c, e-c);
151                                                 line.erase(a+1, 1);
152                                         }
153                                         else
154                                                 line.erase(c, d-c);
155                                 }
156                                 else if ((command == "PING") || (command == "PONG"))
157                                 {
158                                         // :22D PING 20D
159                                         if (line.length() < 13)
160                                                 return;
161
162                                         // Insert the source SID (and a space) between the command and the first parameter
163                                         line.insert(10, line.substr(1, 4));
164                                 }
165                                 else if (command == "OPERTYPE")
166                                 {
167                                         std::string::size_type colon = line.find(':', b);
168                                         if (colon != std::string::npos)
169                                         {
170                                                 for (std::string::iterator i = line.begin()+colon; i != line.end(); ++i)
171                                                 {
172                                                         if (*i == ' ')
173                                                                 *i = '_';
174                                                 }
175                                                 line.erase(colon, 1);
176                                         }
177                                 }
178                                 else if (command == "INVITE")
179                                 {
180                                         // :22D INVITE 22DAAAAAN #chan TS ExpirationTime
181                                         //     A      B         C     D  E
182                                         if (b == std::string::npos)
183                                                 return;
184
185                                         std::string::size_type c = line.find(' ', b + 1);
186                                         if (c == std::string::npos)
187                                                 return;
188
189                                         std::string::size_type d = line.find(' ', c + 1);
190                                         if (d == std::string::npos)
191                                                 return;
192
193                                         std::string::size_type e = line.find(' ', d + 1);
194                                         // If there is no expiration time then everything will be erased from 'd'
195                                         line.erase(d, e-d);
196                                 }
197                                 else if (command == "FJOIN")
198                                 {
199                                         // Strip membership ids
200                                         // :22D FJOIN #chan 1234 +f 4:3 :o,22DAAAAAB:15 o,22DAAAAAA:15
201                                         // :22D FJOIN #chan 1234 +f 4:3 o,22DAAAAAB:15
202                                         // :22D FJOIN #chan 1234 +Pf 4:3 :
203
204                                         // If the last parameter is prefixed by a colon then it's a userlist which may have 0 or more users;
205                                         // if it isn't, then it is a single member
206                                         std::string::size_type spcolon = line.find(" :");
207                                         if (spcolon != std::string::npos)
208                                         {
209                                                 spcolon++;
210                                                 // Loop while there is a ':' in the userlist, this is never true if the channel is empty
211                                                 std::string::size_type pos = std::string::npos;
212                                                 while ((pos = line.rfind(':', pos-1)) > spcolon)
213                                                 {
214                                                         // Find the next space after the ':'
215                                                         std::string::size_type sp = line.find(' ', pos);
216                                                         // Erase characters between the ':' and the next space after it, including the ':' but not the space;
217                                                         // if there is no next space, everything will be erased between pos and the end of the line
218                                                         line.erase(pos, sp-pos);
219                                                 }
220                                         }
221                                         else
222                                         {
223                                                 // Last parameter is a single member
224                                                 std::string::size_type sp = line.rfind(' ');
225                                                 std::string::size_type colon = line.find(':', sp);
226                                                 line.erase(colon);
227                                         }
228                                 }
229                                 else if (command == "KICK")
230                                 {
231                                         // Strip membership id if the KICK has one
232                                         if (b == std::string::npos)
233                                                 return;
234
235                                         std::string::size_type c = line.find(' ', b + 1);
236                                         if (c == std::string::npos)
237                                                 return;
238
239                                         std::string::size_type d = line.find(' ', c + 1);
240                                         if ((d < line.size()-1) && (original_line[d+1] != ':'))
241                                         {
242                                                 // There is a third parameter which doesn't begin with a colon, erase it
243                                                 std::string::size_type e = line.find(' ', d + 1);
244                                                 line.erase(d, e-d);
245                                         }
246                                 }
247                                 else if (command == "SINFO")
248                                 {
249                                         // :22D SINFO version :InspIRCd-2.2
250                                         //     A     B       C
251                                         std::string::size_type c = line.find(' ', b + 1);
252                                         if (c == std::string::npos)
253                                                 return;
254
255                                         // Only translating SINFO version, discard everything else
256                                         if (line.compare(b, 9, " version ", 9))
257                                                 return;
258
259                                         line = line.substr(0, 5) + "VERSION" + line.substr(c);
260                                 }
261                                 else if (command == "SERVER")
262                                 {
263                                         // :001 SERVER inspircd.test 002 [<anything> ...] :gecos
264                                         //     A      B             C
265                                         std::string::size_type c = line.find(' ', b + 1);
266                                         if (c == std::string::npos)
267                                                 return;
268
269                                         std::string::size_type d = c + 4;
270                                         std::string::size_type spcolon = line.find(" :", d);
271                                         if (spcolon == std::string::npos)
272                                                 return;
273
274                                         line.erase(d, spcolon-d);
275                                         line.insert(c, " * 0");
276                                 }
277                         }
278                         ServerInstance->Logs->Log(MODNAME, LOG_RAWIO, "S[%d] O %s", this->GetFd(), line.c_str());
279                         this->WriteData(line);
280                         this->WriteData(newline);
281                         return;
282                 }
283         }
284
285         ServerInstance->Logs->Log(MODNAME, LOG_RAWIO, "S[%d] O %s", this->GetFd(), original_line.c_str());
286         this->WriteData(original_line);
287         this->WriteData(newline);
288 }
289
290 namespace
291 {
292         bool InsertCurrentChannelTS(std::vector<std::string>& params, unsigned int chanindex = 0, unsigned int pos = 1)
293         {
294                 Channel* chan = ServerInstance->FindChan(params[chanindex]);
295                 if (!chan)
296                         return false;
297
298                 // Insert the current TS of the channel after the pos-th parameter
299                 params.insert(params.begin()+pos, ConvToStr(chan->age));
300                 return true;
301         }
302 }
303
304 bool TreeSocket::PreProcessOldProtocolMessage(User*& who, std::string& cmd, std::vector<std::string>& params)
305 {
306         if ((cmd == "METADATA") && (params.size() >= 3) && (params[0][0] == '#'))
307         {
308                 // :20D METADATA #channel extname :extdata
309                 return InsertCurrentChannelTS(params);
310         }
311         else if ((cmd == "FTOPIC") && (params.size() >= 4))
312         {
313                 // :20D FTOPIC #channel 100 Attila :topic text
314                 return InsertCurrentChannelTS(params);
315         }
316         else if ((cmd == "PING") || (cmd == "PONG"))
317         {
318                 if (params.size() == 1)
319                 {
320                         // If it's a PING with 1 parameter, reply with a PONG now, if it's a PONG with 1 parameter (weird), do nothing
321                         if (cmd[1] == 'I')
322                                 this->WriteData(":" + ServerInstance->Config->GetSID() + " PONG " + params[0] + newline);
323
324                         // Don't process this message further
325                         return false;
326                 }
327
328                 // :20D PING 20D 22D
329                 // :20D PONG 20D 22D
330                 // Drop the first parameter
331                 params.erase(params.begin());
332
333                 // If the target is a server name, translate it to a SID
334                 if (!InspIRCd::IsSID(params[0]))
335                 {
336                         TreeServer* server = Utils->FindServer(params[0]);
337                         if (!server)
338                         {
339                                 // We've no idea what this is, log and stop processing
340                                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Received a " + cmd + " with an unknown target: \"" + params[0] + "\", command dropped");
341                                 return false;
342                         }
343
344                         params[0] = server->GetID();
345                 }
346         }
347         else if ((cmd == "GLINE") || (cmd == "KLINE") || (cmd == "ELINE") || (cmd == "ZLINE") || (cmd == "QLINE"))
348         {
349                 // Fix undocumented protocol usage: translate GLINE, ZLINE, etc. into ADDLINE or DELLINE
350                 if ((params.size() != 1) && (params.size() != 3))
351                         return false;
352
353                 parameterlist p;
354                 p.push_back(cmd.substr(0, 1));
355                 p.push_back(params[0]);
356
357                 if (params.size() == 3)
358                 {
359                         cmd = "ADDLINE";
360                         p.push_back(who->nick);
361                         p.push_back(ConvToStr(ServerInstance->Time()));
362                         p.push_back(ConvToStr(InspIRCd::Duration(params[1])));
363                         p.push_back(params[2]);
364                 }
365                 else
366                         cmd = "DELLINE";
367
368                 params.swap(p);
369         }
370         else if (cmd == "SVSMODE")
371         {
372                 cmd = "MODE";
373         }
374         else if (cmd == "OPERQUIT")
375         {
376                 // Translate OPERQUIT into METADATA
377                 if (params.empty())
378                         return false;
379
380                 cmd = "METADATA";
381                 params.insert(params.begin(), who->uuid);
382                 params.insert(params.begin()+1, "operquit");
383                 who = MyRoot->ServerUser;
384         }
385         else if ((cmd == "TOPIC") && (params.size() >= 2))
386         {
387                 // :20DAAAAAC TOPIC #chan :new topic
388                 cmd = "FTOPIC";
389                 if (!InsertCurrentChannelTS(params))
390                         return false;
391
392                 params.insert(params.begin()+2, ConvToStr(ServerInstance->Time()));
393         }
394         else if (cmd == "MODENOTICE")
395         {
396                 // MODENOTICE is always supported by 2.0 but it's optional in 2.2.
397                 params.insert(params.begin(), "*");
398                 params.insert(params.begin()+1, cmd);
399                 cmd = "ENCAP";
400         }
401         else if (cmd == "RULES")
402         {
403                 return false;
404         }
405         else if (cmd == "INVITE")
406         {
407                 // :20D INVITE 22DAAABBB #chan
408                 // :20D INVITE 22DAAABBB #chan 123456789
409                 // Insert channel timestamp after the channel name; the 3rd parameter, if there, is the invite expiration time
410                 return InsertCurrentChannelTS(params, 1, 2);
411         }
412         else if (cmd == "VERSION")
413         {
414                 // :20D VERSION :InspIRCd-2.0
415                 // change to
416                 // :20D SINFO version :InspIRCd-2.0
417                 cmd = "SINFO";
418                 params.insert(params.begin(), "version");
419         }
420         else if (cmd == "JOIN")
421         {
422                 // 2.0 allows and forwards legacy JOINs but we don't, so translate them to FJOINs before processing
423                 if ((params.size() != 1) || (IS_SERVER(who)))
424                         return false; // Huh?
425
426                 cmd = "FJOIN";
427                 Channel* chan = ServerInstance->FindChan(params[0]);
428                 params.push_back(ConvToStr(chan ? chan->age : ServerInstance->Time()));
429                 params.push_back("+");
430                 params.push_back(",");
431                 params.back().append(who->uuid);
432                 who = TreeServer::Get(who)->ServerUser;
433         }
434         else if ((cmd == "FMODE") && (params.size() >= 2))
435         {
436                 // Translate user mode changes with timestamp to MODE
437                 if (params[0][0] != '#')
438                 {
439                         User* user = ServerInstance->FindUUID(params[0]);
440                         if (!user)
441                                 return false;
442
443                         // Emulate the old nonsensical behavior
444                         if (user->age < ServerCommand::ExtractTS(params[1]))
445                                 return false;
446
447                         cmd = "MODE";
448                         params.erase(params.begin()+1);
449                 }
450         }
451         else if ((cmd == "SERVER") && (params.size() > 4))
452         {
453                 // This does not affect the initial SERVER line as it is sent before the link state is CONNECTED
454                 // :20D SERVER <name> * 0 <sid> <desc>
455                 // change to
456                 // :20D SERVER <name> <sid> <desc>
457
458                 params[1].swap(params[3]);
459                 params.erase(params.begin()+2, params.begin()+4);
460         }
461
462         return true; // Passthru
463 }