]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/compat.cpp
Get rid of CommandBuilder::push_back.
[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
290                                                 TreeServer* const source = Utils->FindServerID(line.substr(spcolon-3, 3));
291                                                 if (!source)
292                                                         return;
293
294                                                 line = CmdBuilder(source, "BURST").push_int(ServerInstance->Time()).str();
295                                         }
296                                 }
297                                 else if (command == "NUM")
298                                 {
299                                         // :<sid> NUM <numeric source sid> <target uuid> <3 digit number> <params>
300                                         // Translate to
301                                         // :<sid> PUSH <target uuid> :<numeric source name> <3 digit number> <target nick> <params>
302
303                                         TreeServer* const numericsource = Utils->FindServerID(line.substr(9, 3));
304                                         if (!numericsource)
305                                                 return;
306
307                                         // The nick of the target is necessary for building the PUSH message
308                                         User* const target = ServerInstance->FindUUID(line.substr(13, UIDGenerator::UUID_LENGTH));
309                                         if (!target)
310                                                 return;
311
312                                         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());
313                                         push.append(line, 26, std::string::npos);
314                                         push.swap(line);
315                                 }
316                                 else if (command == "TAGMSG")
317                                 {
318                                         // Drop IRCv3 tag messages as v2 has no message tag support.
319                                         return;
320                                 }
321                         }
322                         WriteLineNoCompat(line);
323                         return;
324                 }
325         }
326
327         WriteLineNoCompat(original_line);
328 }
329
330 namespace
331 {
332         bool InsertCurrentChannelTS(CommandBase::Params& params, unsigned int chanindex = 0, unsigned int pos = 1)
333         {
334                 Channel* chan = ServerInstance->FindChan(params[chanindex]);
335                 if (!chan)
336                         return false;
337
338                 // Insert the current TS of the channel after the pos-th parameter
339                 params.insert(params.begin()+pos, ConvToStr(chan->age));
340                 return true;
341         }
342 }
343
344 bool TreeSocket::PreProcessOldProtocolMessage(User*& who, std::string& cmd, CommandBase::Params& params)
345 {
346         if ((cmd == "METADATA") && (params.size() >= 3) && (params[0][0] == '#'))
347         {
348                 // :20D METADATA #channel extname :extdata
349                 return InsertCurrentChannelTS(params);
350         }
351         else if ((cmd == "FTOPIC") && (params.size() >= 4))
352         {
353                 // :20D FTOPIC #channel 100 Attila :topic text
354                 return InsertCurrentChannelTS(params);
355         }
356         else if ((cmd == "PING") || (cmd == "PONG"))
357         {
358                 if (params.size() == 1)
359                 {
360                         // If it's a PING with 1 parameter, reply with a PONG now, if it's a PONG with 1 parameter (weird), do nothing
361                         if (cmd[1] == 'I')
362                                 this->WriteData(":" + ServerInstance->Config->GetSID() + " PONG " + params[0] + newline);
363
364                         // Don't process this message further
365                         return false;
366                 }
367
368                 // :20D PING 20D 22D
369                 // :20D PONG 20D 22D
370                 // Drop the first parameter
371                 params.erase(params.begin());
372
373                 // If the target is a server name, translate it to a SID
374                 if (!InspIRCd::IsSID(params[0]))
375                 {
376                         TreeServer* server = Utils->FindServer(params[0]);
377                         if (!server)
378                         {
379                                 // We've no idea what this is, log and stop processing
380                                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Received a " + cmd + " with an unknown target: \"" + params[0] + "\", command dropped");
381                                 return false;
382                         }
383
384                         params[0] = server->GetID();
385                 }
386         }
387         else if ((cmd == "GLINE") || (cmd == "KLINE") || (cmd == "ELINE") || (cmd == "ZLINE") || (cmd == "QLINE"))
388         {
389                 // Fix undocumented protocol usage: translate GLINE, ZLINE, etc. into ADDLINE or DELLINE
390                 if ((params.size() != 1) && (params.size() != 3))
391                         return false;
392
393                 CommandBase::Params p;
394                 p.push_back(cmd.substr(0, 1));
395                 p.push_back(params[0]);
396
397                 if (params.size() == 3)
398                 {
399                         cmd = "ADDLINE";
400                         p.push_back(who->nick);
401                         p.push_back(ConvToStr(ServerInstance->Time()));
402                         p.push_back(ConvToStr(InspIRCd::Duration(params[1])));
403                         p.push_back(params[2]);
404                 }
405                 else
406                         cmd = "DELLINE";
407
408                 params.swap(p);
409         }
410         else if (cmd == "SVSMODE")
411         {
412                 cmd = "MODE";
413         }
414         else if (cmd == "OPERQUIT")
415         {
416                 // Translate OPERQUIT into METADATA
417                 if (params.empty())
418                         return false;
419
420                 cmd = "METADATA";
421                 params.insert(params.begin(), who->uuid);
422                 params.insert(params.begin()+1, "operquit");
423                 who = MyRoot->ServerUser;
424         }
425         else if ((cmd == "TOPIC") && (params.size() >= 2))
426         {
427                 // :20DAAAAAC TOPIC #chan :new topic
428                 cmd = "FTOPIC";
429                 if (!InsertCurrentChannelTS(params))
430                         return false;
431
432                 params.insert(params.begin()+2, ConvToStr(ServerInstance->Time()));
433         }
434         else if (cmd == "MODENOTICE")
435         {
436                 // MODENOTICE is always supported by 2.0 but it's optional in 3.0.
437                 params.insert(params.begin(), "*");
438                 params.insert(params.begin()+1, cmd);
439                 cmd = "ENCAP";
440         }
441         else if (cmd == "RULES")
442         {
443                 return false;
444         }
445         else if (cmd == "INVITE")
446         {
447                 // :20D INVITE 22DAAABBB #chan
448                 // :20D INVITE 22DAAABBB #chan 123456789
449                 // Insert channel timestamp after the channel name; the 3rd parameter, if there, is the invite expiration time
450                 return InsertCurrentChannelTS(params, 1, 2);
451         }
452         else if (cmd == "VERSION")
453         {
454                 // :20D VERSION :InspIRCd-2.0
455                 // change to
456                 // :20D SINFO version :InspIRCd-2.0
457                 cmd = "SINFO";
458                 params.insert(params.begin(), "version");
459         }
460         else if (cmd == "JOIN")
461         {
462                 // 2.0 allows and forwards legacy JOINs but we don't, so translate them to FJOINs before processing
463                 if ((params.size() != 1) || (IS_SERVER(who)))
464                         return false; // Huh?
465
466                 cmd = "FJOIN";
467                 Channel* chan = ServerInstance->FindChan(params[0]);
468                 params.push_back(ConvToStr(chan ? chan->age : ServerInstance->Time()));
469                 params.push_back("+");
470                 params.push_back(",");
471                 params.back().append(who->uuid);
472                 who = TreeServer::Get(who)->ServerUser;
473         }
474         else if ((cmd == "FMODE") && (params.size() >= 2))
475         {
476                 // Translate user mode changes with timestamp to MODE
477                 if (params[0][0] != '#')
478                 {
479                         User* user = ServerInstance->FindUUID(params[0]);
480                         if (!user)
481                                 return false;
482
483                         // Emulate the old nonsensical behavior
484                         if (user->age < ServerCommand::ExtractTS(params[1]))
485                                 return false;
486
487                         cmd = "MODE";
488                         params.erase(params.begin()+1);
489                 }
490         }
491         else if ((cmd == "SERVER") && (params.size() > 4))
492         {
493                 // This does not affect the initial SERVER line as it is sent before the link state is CONNECTED
494                 // :20D SERVER <name> * 0 <sid> <desc>
495                 // change to
496                 // :20D SERVER <name> <sid> <desc>
497
498                 params[1].swap(params[3]);
499                 params.erase(params.begin()+2, params.begin()+4);
500
501                 // If the source of this SERVER message or any of its parents are bursting, then new servers it
502                 // introduces are not bursting.
503                 bool bursting = false;
504                 for (TreeServer* server = TreeServer::Get(who); server; server = server->GetParent())
505                 {
506                         if (server->IsBursting())
507                         {
508                                 bursting = true;
509                                 break;
510                         }
511                 }
512
513                 if (!bursting)
514                         params.insert(params.begin()+2, "burst=" + ConvToStr(((uint64_t)ServerInstance->Time())*1000));
515         }
516         else if (cmd == "BURST")
517         {
518                 // A server is introducing another one, drop unnecessary BURST
519                 return false;
520         }
521         else if (cmd == "SVSWATCH")
522         {
523                 // SVSWATCH was removed because nothing was using it, but better be sure
524                 return false;
525         }
526         else if (cmd == "SVSSILENCE")
527         {
528                 // SVSSILENCE was removed because nothing was using it, but better be sure
529                 return false;
530         }
531         else if (cmd == "PUSH")
532         {
533                 if ((params.size() != 2) || (!this->MyRoot))
534                         return false; // Huh?
535
536                 irc::tokenstream ts(params.back());
537
538                 std::string srcstr;
539                 ts.GetMiddle(srcstr);
540                 srcstr.erase(0, 1);
541
542                 std::string token;
543                 ts.GetMiddle(token);
544
545                 // See if it's a numeric being sent to the target via PUSH
546                 unsigned int numeric_number = 0;
547                 if (token.length() == 3)
548                         numeric_number = ConvToNum<unsigned int>(token);
549
550                 if ((numeric_number > 0) && (numeric_number < 1000))
551                 {
552                         // It's a numeric, translate to NUM
553
554                         // srcstr must be a valid server name
555                         TreeServer* const numericsource = Utils->FindServer(srcstr);
556                         if (!numericsource)
557                         {
558                                 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());
559                                 return false;
560                         }
561
562                         cmd = "NUM";
563
564                         // Second parameter becomes the target uuid
565                         params[0].swap(params[1]);
566                         // Replace first param (now the PUSH payload, not needed) with the source sid
567                         params[0] = numericsource->GetID();
568
569                         params.push_back(InspIRCd::Format("%03u", numeric_number));
570
571                         // Ignore the nickname in the numeric in PUSH
572                         ts.GetMiddle(token);
573
574                         // Rest of the tokens are the numeric parameters, add them to NUM
575                         while (ts.GetTrailing(token))
576                                 params.push_back(token);
577                 }
578                 else if ((token == "PRIVMSG") || (token == "NOTICE"))
579                 {
580                         // Command is a PRIVMSG/NOTICE
581                         cmd.swap(token);
582
583                         // Check if the PRIVMSG/NOTICE target is a nickname
584                         ts.GetMiddle(token);
585                         if (token.c_str()[0] == '#')
586                         {
587                                 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());
588                                 return false;
589                         }
590
591                         // Replace second parameter with the message
592                         ts.GetTrailing(params[1]);
593                 }
594                 else
595                 {
596                         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());
597                         return false;
598                 }
599
600                 return true;
601         }
602
603         return true; // Passthru
604 }