2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
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.
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
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/>.
22 #include "treesocket.h"
23 #include "treeserver.h"
25 static std::string newline("\n");
27 void TreeSocket::WriteLineNoCompat(const std::string& line)
29 ServerInstance->Logs->Log(MODNAME, LOG_RAWIO, "S[%d] O %s", this->GetFd(), line.c_str());
30 this->WriteData(line);
31 this->WriteData(newline);
34 void TreeSocket::WriteLine(const std::string& original_line)
36 if (LinkState == CONNECTED)
38 if (proto_version != PROTO_NEWEST)
40 std::string line = original_line;
41 std::string::size_type a = line.find(' ');
44 // The line contains tags which the 1202 protocol can't handle.
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)
53 if (command == "IJOIN")
56 // :<uid> IJOIN <chan> <membid> [<ts> [<flags>]]
58 // :<sid> FJOIN <chan> <ts> + [<flags>],<uuid>
59 std::string::size_type c = line.find(' ', b + 1);
60 if (c == std::string::npos)
63 std::string::size_type d = line.find(' ', c + 1);
64 // Erase membership id first
66 if (d == std::string::npos)
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);
76 line.append(ConvToStr(chan->age));
81 d = line.find(' ', c + 1);
82 if (d == std::string::npos)
84 // TS present, no modes
85 // :22DAAAAAC IJOIN #chan 12345
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)
101 // Move the uuid to the end and replace the I with an F
102 line.append(line.substr(1, 9));
106 else if (command == "RESYNC")
108 else if (command == "METADATA")
110 // Drop TS for channel METADATA, translate METADATA operquit into an OPERQUIT command
111 // :sid METADATA #target TS extname ...
113 if (b == std::string::npos)
116 std::string::size_type c = line.find(' ', b + 1);
117 if (c == std::string::npos)
120 std::string::size_type d = line.find(' ', c + 1);
121 if (d == std::string::npos)
124 if (line[b + 1] == '#')
126 // We're sending channel metadata
129 else if (!line.compare(c, d-c, " operquit", 9))
131 // ":22D METADATA 22DAAAAAX operquit :message" -> ":22DAAAAAX OPERQUIT :message"
132 line = ":" + line.substr(b+1, c-b) + "OPERQUIT" + line.substr(d);
135 else if (command == "FTOPIC")
137 // Drop channel TS for FTOPIC
138 // :sid FTOPIC #target TS TopicTS setter :newtopic
140 // :uid FTOPIC #target TS TopicTS :newtopic
142 if (b == std::string::npos)
145 std::string::size_type c = line.find(' ', b + 1);
146 if (c == std::string::npos)
149 std::string::size_type d = line.find(' ', c + 1);
150 if (d == std::string::npos)
153 std::string::size_type e = line.find(' ', d + 1);
154 if (line[e+1] == ':')
162 else if ((command == "PING") || (command == "PONG"))
165 if (line.length() < 13)
168 // Insert the source SID (and a space) between the command and the first parameter
169 line.insert(10, line.substr(1, 4));
171 else if (command == "OPERTYPE")
173 std::string::size_type colon = line.find(':', b);
174 if (colon != std::string::npos)
176 for (std::string::iterator i = line.begin()+colon; i != line.end(); ++i)
181 line.erase(colon, 1);
184 else if (command == "INVITE")
186 // :22D INVITE 22DAAAAAN #chan TS ExpirationTime
188 if (b == std::string::npos)
191 std::string::size_type c = line.find(' ', b + 1);
192 if (c == std::string::npos)
195 std::string::size_type d = line.find(' ', c + 1);
196 if (d == std::string::npos)
199 std::string::size_type e = line.find(' ', d + 1);
200 // If there is no expiration time then everything will be erased from 'd'
203 else if (command == "FJOIN")
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 :
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)
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)
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);
229 // Last parameter is a single member
230 std::string::size_type sp = line.rfind(' ');
231 std::string::size_type colon = line.find(':', sp);
235 else if (command == "KICK")
237 // Strip membership id if the KICK has one
238 if (b == std::string::npos)
241 std::string::size_type c = line.find(' ', b + 1);
242 if (c == std::string::npos)
245 std::string::size_type d = line.find(' ', c + 1);
246 if ((d < line.size()-1) && (original_line[d+1] != ':'))
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);
253 else if (command == "SINFO")
255 // :22D SINFO version :InspIRCd-3.0
257 std::string::size_type c = line.find(' ', b + 1);
258 if (c == std::string::npos)
261 // Only translating SINFO version, discard everything else
262 if (line.compare(b, 9, " version ", 9))
265 line = line.substr(0, 5) + "VERSION" + line.substr(c);
267 else if (command == "SERVER")
269 // :001 SERVER inspircd.test 002 [<anything> ...] :description
271 std::string::size_type c = line.find(' ', b + 1);
272 if (c == std::string::npos)
275 std::string::size_type d = c + 4;
276 std::string::size_type spcolon = line.find(" :", d);
277 if (spcolon == std::string::npos)
280 line.erase(d, spcolon-d);
281 line.insert(c, " * 0");
285 WriteLineNoCompat(line);
287 // Synthesize a :<newserver> BURST <time> message
288 spcolon = line.find(" :");
290 TreeServer* const source = Utils->FindServerID(line.substr(spcolon-3, 3));
294 line = CmdBuilder(source, "BURST").push_int(ServerInstance->Time()).str();
297 else if (command == "NUM")
299 // :<sid> NUM <numeric source sid> <target uuid> <3 digit number> <params>
301 // :<sid> PUSH <target uuid> :<numeric source name> <3 digit number> <target nick> <params>
303 TreeServer* const numericsource = Utils->FindServerID(line.substr(9, 3));
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));
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);
316 else if (command == "TAGMSG")
318 // Drop IRCv3 tag messages as v2 has no message tag support.
322 WriteLineNoCompat(line);
327 WriteLineNoCompat(original_line);
332 bool InsertCurrentChannelTS(CommandBase::Params& params, unsigned int chanindex = 0, unsigned int pos = 1)
334 Channel* chan = ServerInstance->FindChan(params[chanindex]);
338 // Insert the current TS of the channel after the pos-th parameter
339 params.insert(params.begin()+pos, ConvToStr(chan->age));
344 bool TreeSocket::PreProcessOldProtocolMessage(User*& who, std::string& cmd, CommandBase::Params& params)
346 if ((cmd == "METADATA") && (params.size() >= 3) && (params[0][0] == '#'))
348 // :20D METADATA #channel extname :extdata
349 return InsertCurrentChannelTS(params);
351 else if ((cmd == "FTOPIC") && (params.size() >= 4))
353 // :20D FTOPIC #channel 100 Attila :topic text
354 return InsertCurrentChannelTS(params);
356 else if ((cmd == "PING") || (cmd == "PONG"))
358 if (params.size() == 1)
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
362 this->WriteData(":" + ServerInstance->Config->GetSID() + " PONG " + params[0] + newline);
364 // Don't process this message further
370 // Drop the first parameter
371 params.erase(params.begin());
373 // If the target is a server name, translate it to a SID
374 if (!InspIRCd::IsSID(params[0]))
376 TreeServer* server = Utils->FindServer(params[0]);
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");
384 params[0] = server->GetID();
387 else if ((cmd == "GLINE") || (cmd == "KLINE") || (cmd == "ELINE") || (cmd == "ZLINE") || (cmd == "QLINE"))
389 // Fix undocumented protocol usage: translate GLINE, ZLINE, etc. into ADDLINE or DELLINE
390 if ((params.size() != 1) && (params.size() != 3))
393 CommandBase::Params p;
394 p.push_back(cmd.substr(0, 1));
395 p.push_back(params[0]);
397 if (params.size() == 3)
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]);
410 else if (cmd == "SVSMODE")
414 else if (cmd == "OPERQUIT")
416 // Translate OPERQUIT into METADATA
421 params.insert(params.begin(), who->uuid);
422 params.insert(params.begin()+1, "operquit");
423 who = MyRoot->ServerUser;
425 else if ((cmd == "TOPIC") && (params.size() >= 2))
427 // :20DAAAAAC TOPIC #chan :new topic
429 if (!InsertCurrentChannelTS(params))
432 params.insert(params.begin()+2, ConvToStr(ServerInstance->Time()));
434 else if (cmd == "MODENOTICE")
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);
441 else if (cmd == "RULES")
445 else if (cmd == "INVITE")
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);
452 else if (cmd == "VERSION")
454 // :20D VERSION :InspIRCd-2.0
456 // :20D SINFO version :InspIRCd-2.0
458 params.insert(params.begin(), "version");
460 else if (cmd == "JOIN")
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?
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;
474 else if ((cmd == "FMODE") && (params.size() >= 2))
476 // Translate user mode changes with timestamp to MODE
477 if (params[0][0] != '#')
479 User* user = ServerInstance->FindUUID(params[0]);
483 // Emulate the old nonsensical behavior
484 if (user->age < ServerCommand::ExtractTS(params[1]))
488 params.erase(params.begin()+1);
491 else if ((cmd == "SERVER") && (params.size() > 4))
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>
496 // :20D SERVER <name> <sid> <desc>
498 params[1].swap(params[3]);
499 params.erase(params.begin()+2, params.begin()+4);
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())
506 if (server->IsBursting())
514 params.insert(params.begin()+2, "burst=" + ConvToStr(((uint64_t)ServerInstance->Time())*1000));
516 else if (cmd == "BURST")
518 // A server is introducing another one, drop unnecessary BURST
521 else if (cmd == "SVSWATCH")
523 // SVSWATCH was removed because nothing was using it, but better be sure
526 else if (cmd == "SVSSILENCE")
528 // SVSSILENCE was removed because nothing was using it, but better be sure
531 else if (cmd == "PUSH")
533 if ((params.size() != 2) || (!this->MyRoot))
534 return false; // Huh?
536 irc::tokenstream ts(params.back());
539 ts.GetMiddle(srcstr);
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);
550 if ((numeric_number > 0) && (numeric_number < 1000))
552 // It's a numeric, translate to NUM
554 // srcstr must be a valid server name
555 TreeServer* const numericsource = Utils->FindServer(srcstr);
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());
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();
569 params.push_back(InspIRCd::Format("%03u", numeric_number));
571 // Ignore the nickname in the numeric in PUSH
574 // Rest of the tokens are the numeric parameters, add them to NUM
575 while (ts.GetTrailing(token))
576 params.push_back(token);
578 else if ((token == "PRIVMSG") || (token == "NOTICE"))
580 // Command is a PRIVMSG/NOTICE
583 // Check if the PRIVMSG/NOTICE target is a nickname
585 if (token.c_str()[0] == '#')
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());
591 // Replace second parameter with the message
592 ts.GetTrailing(params[1]);
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());
603 return true; // Passthru