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