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