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