]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/compat.cpp
Update copyright headers.
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / compat.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2018 Matt Schatz <genius3000@g3k.solutions>
5  *   Copyright (C) 2013-2016 Attila Molnar <attilamolnar@hush.com>
6  *   Copyright (C) 2013, 2018-2019 Sadie Powell <sadie@witchery.services>
7  *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
8  *   Copyright (C) 2010 Craig Edwards <brain@inspircd.org>
9  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
10  *
11  * This file is part of InspIRCd.  InspIRCd is free software: you can
12  * redistribute it and/or modify it under the terms of the GNU General Public
13  * License as published by the Free Software Foundation, version 2.
14  *
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  */
23
24
25 #include "inspircd.h"
26 #include "main.h"
27 #include "treesocket.h"
28 #include "treeserver.h"
29
30 static std::string newline("\n");
31
32 void TreeSocket::WriteLineNoCompat(const std::string& line)
33 {
34         ServerInstance->Logs->Log(MODNAME, LOG_RAWIO, "S[%d] O %s", this->GetFd(), line.c_str());
35         this->WriteData(line);
36         this->WriteData(newline);
37 }
38
39 void TreeSocket::WriteLine(const std::string& original_line)
40 {
41         if (LinkState == CONNECTED)
42         {
43                 if (proto_version != PROTO_NEWEST)
44                 {
45                         std::string line = original_line;
46                         std::string::size_type a = line.find(' ');
47                         if (line[0] == '@')
48                         {
49                                 // The line contains tags which the 1202 protocol can't handle.
50                                 line.erase(0, a + 1);
51                                 a = line.find(' ');
52                         }
53                         std::string::size_type b = line.find(' ', a + 1);
54                         std::string command(line, a + 1, b-a-1);
55                         // now try to find a translation entry
56                         if (proto_version < PROTO_INSPIRCD_30)
57                         {
58                                 if (command == "IJOIN")
59                                 {
60                                         // Convert
61                                         // :<uid> IJOIN <chan> <membid> [<ts> [<flags>]]
62                                         // to
63                                         // :<sid> FJOIN <chan> <ts> + [<flags>],<uuid>
64                                         std::string::size_type c = line.find(' ', b + 1);
65                                         if (c == std::string::npos)
66                                                 return;
67
68                                         std::string::size_type d = line.find(' ', c + 1);
69                                         // Erase membership id first
70                                         line.erase(c, d-c);
71                                         if (d == std::string::npos)
72                                         {
73                                                 // No TS or modes in the command
74                                                 // :22DAAAAAB IJOIN #chan
75                                                 const std::string channame(line, b+1, c-b-1);
76                                                 Channel* chan = ServerInstance->FindChan(channame);
77                                                 if (!chan)
78                                                         return;
79
80                                                 line.push_back(' ');
81                                                 line.append(ConvToStr(chan->age));
82                                                 line.append(" + ,");
83                                         }
84                                         else
85                                         {
86                                                 d = line.find(' ', c + 1);
87                                                 if (d == std::string::npos)
88                                                 {
89                                                         // TS present, no modes
90                                                         // :22DAAAAAC IJOIN #chan 12345
91                                                         line.append(" + ,");
92                                                 }
93                                                 else
94                                                 {
95                                                         // Both TS and modes are present
96                                                         // :22DAAAAAC IJOIN #chan 12345 ov
97                                                         std::string::size_type e = line.find(' ', d + 1);
98                                                         if (e != std::string::npos)
99                                                                 line.erase(e);
100
101                                                         line.insert(d, " +");
102                                                         line.push_back(',');
103                                                 }
104                                         }
105
106                                         // Move the uuid to the end and replace the I with an F
107                                         line.append(line.substr(1, 9));
108                                         line.erase(4, 6);
109                                         line[5] = 'F';
110                                 }
111                                 else if (command == "RESYNC")
112                                         return;
113                                 else if (command == "METADATA")
114                                 {
115                                         // Drop TS for channel METADATA, translate METADATA operquit into an OPERQUIT command
116                                         // :sid METADATA #target TS extname ...
117                                         //     A        B       C  D
118                                         if (b == std::string::npos)
119                                                 return;
120
121                                         std::string::size_type c = line.find(' ', b + 1);
122                                         if (c == std::string::npos)
123                                                 return;
124
125                                         std::string::size_type d = line.find(' ', c + 1);
126                                         if (d == std::string::npos)
127                                                 return;
128
129                                         if (line[b + 1] == '#')
130                                         {
131                                                 // We're sending channel metadata
132                                                 line.erase(c, d-c);
133                                         }
134                                         else if (!line.compare(c, d-c, " operquit", 9))
135                                         {
136                                                 // ":22D METADATA 22DAAAAAX operquit :message" -> ":22DAAAAAX OPERQUIT :message"
137                                                 line = ":" + line.substr(b+1, c-b) + "OPERQUIT" + line.substr(d);
138                                         }
139                                 }
140                                 else if (command == "FTOPIC")
141                                 {
142                                         // Drop channel TS for FTOPIC
143                                         // :sid FTOPIC #target TS TopicTS setter :newtopic
144                                         //     A      B       C  D       E      F
145                                         // :uid FTOPIC #target TS TopicTS :newtopic
146                                         //     A      B       C  D       E
147                                         if (b == std::string::npos)
148                                                 return;
149
150                                         std::string::size_type c = line.find(' ', b + 1);
151                                         if (c == std::string::npos)
152                                                 return;
153
154                                         std::string::size_type d = line.find(' ', c + 1);
155                                         if (d == std::string::npos)
156                                                 return;
157
158                                         std::string::size_type e = line.find(' ', d + 1);
159                                         if (line[e+1] == ':')
160                                         {
161                                                 line.erase(c, e-c);
162                                                 line.erase(a+1, 1);
163                                         }
164                                         else
165                                                 line.erase(c, d-c);
166                                 }
167                                 else if ((command == "PING") || (command == "PONG"))
168                                 {
169                                         // :22D PING 20D
170                                         if (line.length() < 13)
171                                                 return;
172
173                                         // Insert the source SID (and a space) between the command and the first parameter
174                                         line.insert(10, line.substr(1, 4));
175                                 }
176                                 else if (command == "OPERTYPE")
177                                 {
178                                         std::string::size_type colon = line.find(':', b);
179                                         if (colon != std::string::npos)
180                                         {
181                                                 for (std::string::iterator i = line.begin()+colon; i != line.end(); ++i)
182                                                 {
183                                                         if (*i == ' ')
184                                                                 *i = '_';
185                                                 }
186                                                 line.erase(colon, 1);
187                                         }
188                                 }
189                                 else if (command == "INVITE")
190                                 {
191                                         // :22D INVITE 22DAAAAAN #chan TS ExpirationTime
192                                         //     A      B         C     D  E
193                                         if (b == std::string::npos)
194                                                 return;
195
196                                         std::string::size_type c = line.find(' ', b + 1);
197                                         if (c == std::string::npos)
198                                                 return;
199
200                                         std::string::size_type d = line.find(' ', c + 1);
201                                         if (d == std::string::npos)
202                                                 return;
203
204                                         std::string::size_type e = line.find(' ', d + 1);
205                                         // If there is no expiration time then everything will be erased from 'd'
206                                         line.erase(d, e-d);
207                                 }
208                                 else if (command == "FJOIN")
209                                 {
210                                         // Strip membership ids
211                                         // :22D FJOIN #chan 1234 +f 4:3 :o,22DAAAAAB:15 o,22DAAAAAA:15
212                                         // :22D FJOIN #chan 1234 +f 4:3 o,22DAAAAAB:15
213                                         // :22D FJOIN #chan 1234 +Pf 4:3 :
214
215                                         // If the last parameter is prefixed by a colon then it's a userlist which may have 0 or more users;
216                                         // if it isn't, then it is a single member
217                                         std::string::size_type spcolon = line.find(" :");
218                                         if (spcolon != std::string::npos)
219                                         {
220                                                 spcolon++;
221                                                 // Loop while there is a ':' in the userlist, this is never true if the channel is empty
222                                                 std::string::size_type pos = std::string::npos;
223                                                 while ((pos = line.rfind(':', pos-1)) > spcolon)
224                                                 {
225                                                         // Find the next space after the ':'
226                                                         std::string::size_type sp = line.find(' ', pos);
227                                                         // Erase characters between the ':' and the next space after it, including the ':' but not the space;
228                                                         // if there is no next space, everything will be erased between pos and the end of the line
229                                                         line.erase(pos, sp-pos);
230                                                 }
231                                         }
232                                         else
233                                         {
234                                                 // Last parameter is a single member
235                                                 std::string::size_type sp = line.rfind(' ');
236                                                 std::string::size_type colon = line.find(':', sp);
237                                                 line.erase(colon);
238                                         }
239                                 }
240                                 else if (command == "KICK")
241                                 {
242                                         // Strip membership id if the KICK has one
243                                         if (b == std::string::npos)
244                                                 return;
245
246                                         std::string::size_type c = line.find(' ', b + 1);
247                                         if (c == std::string::npos)
248                                                 return;
249
250                                         std::string::size_type d = line.find(' ', c + 1);
251                                         if ((d < line.size()-1) && (original_line[d+1] != ':'))
252                                         {
253                                                 // There is a third parameter which doesn't begin with a colon, erase it
254                                                 std::string::size_type e = line.find(' ', d + 1);
255                                                 line.erase(d, e-d);
256                                         }
257                                 }
258                                 else if (command == "SINFO")
259                                 {
260                                         // :22D SINFO version :InspIRCd-3.0
261                                         //     A     B       C
262                                         std::string::size_type c = line.find(' ', b + 1);
263                                         if (c == std::string::npos)
264                                                 return;
265
266                                         // Only translating SINFO version, discard everything else
267                                         if (line.compare(b, 9, " version ", 9))
268                                                 return;
269
270                                         line = line.substr(0, 5) + "VERSION" + line.substr(c);
271                                 }
272                                 else if (command == "SERVER")
273                                 {
274                                         // :001 SERVER inspircd.test 002 [<anything> ...] :description
275                                         //     A      B             C
276                                         std::string::size_type c = line.find(' ', b + 1);
277                                         if (c == std::string::npos)
278                                                 return;
279
280                                         std::string::size_type d = c + 4;
281                                         std::string::size_type spcolon = line.find(" :", d);
282                                         if (spcolon == std::string::npos)
283                                                 return;
284
285                                         line.erase(d, spcolon-d);
286                                         line.insert(c, " * 0");
287
288                                         if (burstsent)
289                                         {
290                                                 WriteLineNoCompat(line);
291
292                                                 // Synthesize a :<newserver> BURST <time> message
293                                                 spcolon = line.find(" :");
294
295                                                 TreeServer* const source = Utils->FindServerID(line.substr(spcolon-3, 3));
296                                                 if (!source)
297                                                         return;
298
299                                                 line = CmdBuilder(source, "BURST").push_int(ServerInstance->Time()).str();
300                                         }
301                                 }
302                                 else if (command == "NUM")
303                                 {
304                                         // :<sid> NUM <numeric source sid> <target uuid> <3 digit number> <params>
305                                         // Translate to
306                                         // :<sid> PUSH <target uuid> :<numeric source name> <3 digit number> <target nick> <params>
307
308                                         TreeServer* const numericsource = Utils->FindServerID(line.substr(9, 3));
309                                         if (!numericsource)
310                                                 return;
311
312                                         // The nick of the target is necessary for building the PUSH message
313                                         User* const target = ServerInstance->FindUUID(line.substr(13, UIDGenerator::UUID_LENGTH));
314                                         if (!target)
315                                                 return;
316
317                                         std::string push = InspIRCd::Format(":%.*s PUSH %s ::%s %.*s %s", 3, line.c_str()+1, target->uuid.c_str(), numericsource->GetName().c_str(), 3, line.c_str()+23, target->nick.c_str());
318                                         push.append(line, 26, std::string::npos);
319                                         push.swap(line);
320                                 }
321                                 else if (command == "TAGMSG")
322                                 {
323                                         // Drop IRCv3 tag messages as v2 has no message tag support.
324                                         return;
325                                 }
326                         }
327                         WriteLineNoCompat(line);
328                         return;
329                 }
330         }
331
332         WriteLineNoCompat(original_line);
333 }
334
335 namespace
336 {
337         bool InsertCurrentChannelTS(CommandBase::Params& params, unsigned int chanindex = 0, unsigned int pos = 1)
338         {
339                 Channel* chan = ServerInstance->FindChan(params[chanindex]);
340                 if (!chan)
341                         return false;
342
343                 // Insert the current TS of the channel after the pos-th parameter
344                 params.insert(params.begin()+pos, ConvToStr(chan->age));
345                 return true;
346         }
347 }
348
349 bool TreeSocket::PreProcessOldProtocolMessage(User*& who, std::string& cmd, CommandBase::Params& params)
350 {
351         if ((cmd == "METADATA") && (params.size() >= 3) && (params[0][0] == '#'))
352         {
353                 // :20D METADATA #channel extname :extdata
354                 return InsertCurrentChannelTS(params);
355         }
356         else if ((cmd == "FTOPIC") && (params.size() >= 4))
357         {
358                 // :20D FTOPIC #channel 100 Attila :topic text
359                 return InsertCurrentChannelTS(params);
360         }
361         else if ((cmd == "PING") || (cmd == "PONG"))
362         {
363                 if (params.size() == 1)
364                 {
365                         // If it's a PING with 1 parameter, reply with a PONG now, if it's a PONG with 1 parameter (weird), do nothing
366                         if (cmd[1] == 'I')
367                                 this->WriteData(":" + ServerInstance->Config->GetSID() + " PONG " + params[0] + newline);
368
369                         // Don't process this message further
370                         return false;
371                 }
372
373                 // :20D PING 20D 22D
374                 // :20D PONG 20D 22D
375                 // Drop the first parameter
376                 params.erase(params.begin());
377
378                 // If the target is a server name, translate it to a SID
379                 if (!InspIRCd::IsSID(params[0]))
380                 {
381                         TreeServer* server = Utils->FindServer(params[0]);
382                         if (!server)
383                         {
384                                 // We've no idea what this is, log and stop processing
385                                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Received a " + cmd + " with an unknown target: \"" + params[0] + "\", command dropped");
386                                 return false;
387                         }
388
389                         params[0] = server->GetId();
390                 }
391         }
392         else if ((cmd == "GLINE") || (cmd == "KLINE") || (cmd == "ELINE") || (cmd == "ZLINE") || (cmd == "QLINE"))
393         {
394                 // Fix undocumented protocol usage: translate GLINE, ZLINE, etc. into ADDLINE or DELLINE
395                 if ((params.size() != 1) && (params.size() != 3))
396                         return false;
397
398                 CommandBase::Params p;
399                 p.push_back(cmd.substr(0, 1));
400                 p.push_back(params[0]);
401
402                 if (params.size() == 3)
403                 {
404                         cmd = "ADDLINE";
405                         p.push_back(who->nick);
406                         p.push_back(ConvToStr(ServerInstance->Time()));
407                         p.push_back(ConvToStr(InspIRCd::Duration(params[1])));
408                         p.push_back(params[2]);
409                 }
410                 else
411                         cmd = "DELLINE";
412
413                 params.swap(p);
414         }
415         else if (cmd == "SVSMODE")
416         {
417                 cmd = "MODE";
418         }
419         else if (cmd == "OPERQUIT")
420         {
421                 // Translate OPERQUIT into METADATA
422                 if (params.empty())
423                         return false;
424
425                 cmd = "METADATA";
426                 params.insert(params.begin(), who->uuid);
427                 params.insert(params.begin()+1, "operquit");
428                 who = MyRoot->ServerUser;
429         }
430         else if ((cmd == "TOPIC") && (params.size() >= 2))
431         {
432                 // :20DAAAAAC TOPIC #chan :new topic
433                 cmd = "FTOPIC";
434                 if (!InsertCurrentChannelTS(params))
435                         return false;
436
437                 params.insert(params.begin()+2, ConvToStr(ServerInstance->Time()));
438         }
439         else if (cmd == "MODENOTICE")
440         {
441                 // MODENOTICE is always supported by 2.0 but it's optional in 3.0.
442                 params.insert(params.begin(), "*");
443                 params.insert(params.begin()+1, cmd);
444                 cmd = "ENCAP";
445         }
446         else if (cmd == "RULES")
447         {
448                 return false;
449         }
450         else if (cmd == "INVITE")
451         {
452                 // :20D INVITE 22DAAABBB #chan
453                 // :20D INVITE 22DAAABBB #chan 123456789
454                 // Insert channel timestamp after the channel name; the 3rd parameter, if there, is the invite expiration time
455                 return InsertCurrentChannelTS(params, 1, 2);
456         }
457         else if (cmd == "VERSION")
458         {
459                 // :20D VERSION :InspIRCd-2.0
460                 // change to
461                 // :20D SINFO version :InspIRCd-2.0
462                 cmd = "SINFO";
463                 params.insert(params.begin(), "version");
464         }
465         else if (cmd == "JOIN")
466         {
467                 // 2.0 allows and forwards legacy JOINs but we don't, so translate them to FJOINs before processing
468                 if ((params.size() != 1) || (IS_SERVER(who)))
469                         return false; // Huh?
470
471                 cmd = "FJOIN";
472                 Channel* chan = ServerInstance->FindChan(params[0]);
473                 params.push_back(ConvToStr(chan ? chan->age : ServerInstance->Time()));
474                 params.push_back("+");
475                 params.push_back(",");
476                 params.back().append(who->uuid);
477                 who = TreeServer::Get(who)->ServerUser;
478         }
479         else if ((cmd == "FMODE") && (params.size() >= 2))
480         {
481                 // Translate user mode changes with timestamp to MODE
482                 if (params[0][0] != '#')
483                 {
484                         User* user = ServerInstance->FindUUID(params[0]);
485                         if (!user)
486                                 return false;
487
488                         // Emulate the old nonsensical behavior
489                         if (user->age < ServerCommand::ExtractTS(params[1]))
490                                 return false;
491
492                         cmd = "MODE";
493                         params.erase(params.begin()+1);
494                 }
495         }
496         else if ((cmd == "SERVER") && (params.size() > 4))
497         {
498                 // This does not affect the initial SERVER line as it is sent before the link state is CONNECTED
499                 // :20D SERVER <name> * 0 <sid> <desc>
500                 // change to
501                 // :20D SERVER <name> <sid> <desc>
502
503                 params[1].swap(params[3]);
504                 params.erase(params.begin()+2, params.begin()+4);
505
506                 // If the source of this SERVER message or any of its parents are bursting, then new servers it
507                 // introduces are not bursting.
508                 bool bursting = false;
509                 for (TreeServer* server = TreeServer::Get(who); server; server = server->GetParent())
510                 {
511                         if (server->IsBursting())
512                         {
513                                 bursting = true;
514                                 break;
515                         }
516                 }
517
518                 if (!bursting)
519                         params.insert(params.begin()+2, "burst=" + ConvToStr(((uint64_t)ServerInstance->Time())*1000));
520         }
521         else if (cmd == "BURST")
522         {
523                 // A server is introducing another one, drop unnecessary BURST
524                 return false;
525         }
526         else if (cmd == "SVSWATCH")
527         {
528                 // SVSWATCH was removed because nothing was using it, but better be sure
529                 return false;
530         }
531         else if (cmd == "SVSSILENCE")
532         {
533                 // SVSSILENCE was removed because nothing was using it, but better be sure
534                 return false;
535         }
536         else if (cmd == "PUSH")
537         {
538                 if ((params.size() != 2) || (!this->MyRoot))
539                         return false; // Huh?
540
541                 irc::tokenstream ts(params.back());
542
543                 std::string srcstr;
544                 ts.GetMiddle(srcstr);
545                 srcstr.erase(0, 1);
546
547                 std::string token;
548                 ts.GetMiddle(token);
549
550                 // See if it's a numeric being sent to the target via PUSH
551                 unsigned int numeric_number = 0;
552                 if (token.length() == 3)
553                         numeric_number = ConvToNum<unsigned int>(token);
554
555                 if ((numeric_number > 0) && (numeric_number < 1000))
556                 {
557                         // It's a numeric, translate to NUM
558
559                         // srcstr must be a valid server name
560                         TreeServer* const numericsource = Utils->FindServer(srcstr);
561                         if (!numericsource)
562                         {
563                                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Unable to translate PUSH numeric %s to user %s from 1202 protocol server %s: source \"%s\" doesn't exist", token.c_str(), params[0].c_str(), this->MyRoot->GetName().c_str(), srcstr.c_str());
564                                 return false;
565                         }
566
567                         cmd = "NUM";
568
569                         // Second parameter becomes the target uuid
570                         params[0].swap(params[1]);
571                         // Replace first param (now the PUSH payload, not needed) with the source sid
572                         params[0] = numericsource->GetId();
573
574                         params.push_back(InspIRCd::Format("%03u", numeric_number));
575
576                         // Ignore the nickname in the numeric in PUSH
577                         ts.GetMiddle(token);
578
579                         // Rest of the tokens are the numeric parameters, add them to NUM
580                         while (ts.GetTrailing(token))
581                                 params.push_back(token);
582                 }
583                 else if ((token == "PRIVMSG") || (token == "NOTICE"))
584                 {
585                         // Command is a PRIVMSG/NOTICE
586                         cmd.swap(token);
587
588                         // Check if the PRIVMSG/NOTICE target is a nickname
589                         ts.GetMiddle(token);
590                         if (token.c_str()[0] == '#')
591                         {
592                                 ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Unable to translate PUSH %s to user %s from 1202 protocol server %s, target \"%s\"", cmd.c_str(), params[0].c_str(), this->MyRoot->GetName().c_str(), token.c_str());
593                                 return false;
594                         }
595
596                         // Replace second parameter with the message
597                         ts.GetTrailing(params[1]);
598                 }
599                 else
600                 {
601                         ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Unable to translate PUSH to user %s from 1202 protocol server %s", params[0].c_str(), this->MyRoot->GetName().c_str());
602                         return false;
603                 }
604
605                 return true;
606         }
607
608         return true; // Passthru
609 }