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 (original_line.c_str()[0] != ':')
40 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Sending line without server prefix!");
41 WriteLine(":" + ServerInstance->Config->GetSID() + " " + original_line);
44 if (proto_version != ProtocolVersion)
46 std::string line = original_line;
47 std::string::size_type a = line.find(' ');
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)
54 if (command == "IJOIN")
57 // :<uid> IJOIN <chan> <membid> [<ts> [<flags>]]
59 // :<sid> FJOIN <chan> <ts> + [<flags>],<uuid>
60 std::string::size_type c = line.find(' ', b + 1);
61 if (c == std::string::npos)
64 std::string::size_type d = line.find(' ', c + 1);
65 // Erase membership id first
67 if (d == std::string::npos)
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);
77 line.append(ConvToStr(chan->age));
82 d = line.find(' ', c + 1);
83 if (d == std::string::npos)
85 // TS present, no modes
86 // :22DAAAAAC IJOIN #chan 12345
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)
102 // Move the uuid to the end and replace the I with an F
103 line.append(line.substr(1, 9));
107 else if (command == "RESYNC")
109 else if (command == "METADATA")
111 // Drop TS for channel METADATA, translate METADATA operquit into an OPERQUIT command
112 // :sid METADATA #target TS extname ...
114 if (b == std::string::npos)
117 std::string::size_type c = line.find(' ', b + 1);
118 if (c == std::string::npos)
121 std::string::size_type d = line.find(' ', c + 1);
122 if (d == std::string::npos)
125 if (line[b + 1] == '#')
127 // We're sending channel metadata
130 else if (!line.compare(c, d-c, " operquit", 9))
132 // ":22D METADATA 22DAAAAAX operquit :message" -> ":22DAAAAAX OPERQUIT :message"
133 line = ":" + line.substr(b+1, c-b) + "OPERQUIT" + line.substr(d);
136 else if (command == "FTOPIC")
138 // Drop channel TS for FTOPIC
139 // :sid FTOPIC #target TS TopicTS setter :newtopic
141 // :uid FTOPIC #target TS TopicTS :newtopic
143 if (b == std::string::npos)
146 std::string::size_type c = line.find(' ', b + 1);
147 if (c == std::string::npos)
150 std::string::size_type d = line.find(' ', c + 1);
151 if (d == std::string::npos)
154 std::string::size_type e = line.find(' ', d + 1);
155 if (line[e+1] == ':')
163 else if ((command == "PING") || (command == "PONG"))
166 if (line.length() < 13)
169 // Insert the source SID (and a space) between the command and the first parameter
170 line.insert(10, line.substr(1, 4));
172 else if (command == "OPERTYPE")
174 std::string::size_type colon = line.find(':', b);
175 if (colon != std::string::npos)
177 for (std::string::iterator i = line.begin()+colon; i != line.end(); ++i)
182 line.erase(colon, 1);
185 else if (command == "INVITE")
187 // :22D INVITE 22DAAAAAN #chan TS ExpirationTime
189 if (b == std::string::npos)
192 std::string::size_type c = line.find(' ', b + 1);
193 if (c == std::string::npos)
196 std::string::size_type d = line.find(' ', c + 1);
197 if (d == std::string::npos)
200 std::string::size_type e = line.find(' ', d + 1);
201 // If there is no expiration time then everything will be erased from 'd'
204 else if (command == "FJOIN")
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 :
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)
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)
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);
230 // Last parameter is a single member
231 std::string::size_type sp = line.rfind(' ');
232 std::string::size_type colon = line.find(':', sp);
236 else if (command == "KICK")
238 // Strip membership id if the KICK has one
239 if (b == std::string::npos)
242 std::string::size_type c = line.find(' ', b + 1);
243 if (c == std::string::npos)
246 std::string::size_type d = line.find(' ', c + 1);
247 if ((d < line.size()-1) && (original_line[d+1] != ':'))
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);
254 else if (command == "SINFO")
256 // :22D SINFO version :InspIRCd-2.2
258 std::string::size_type c = line.find(' ', b + 1);
259 if (c == std::string::npos)
262 // Only translating SINFO version, discard everything else
263 if (line.compare(b, 9, " version ", 9))
266 line = line.substr(0, 5) + "VERSION" + line.substr(c);
268 else if (command == "SERVER")
270 // :001 SERVER inspircd.test 002 [<anything> ...] :gecos
272 std::string::size_type c = line.find(' ', b + 1);
273 if (c == std::string::npos)
276 std::string::size_type d = c + 4;
277 std::string::size_type spcolon = line.find(" :", d);
278 if (spcolon == std::string::npos)
281 line.erase(d, spcolon-d);
282 line.insert(c, " * 0");
286 WriteLineNoCompat(line);
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();
294 WriteLineNoCompat(line);
299 WriteLineNoCompat(original_line);
304 bool InsertCurrentChannelTS(std::vector<std::string>& params, unsigned int chanindex = 0, unsigned int pos = 1)
306 Channel* chan = ServerInstance->FindChan(params[chanindex]);
310 // Insert the current TS of the channel after the pos-th parameter
311 params.insert(params.begin()+pos, ConvToStr(chan->age));
316 bool TreeSocket::PreProcessOldProtocolMessage(User*& who, std::string& cmd, std::vector<std::string>& params)
318 if ((cmd == "METADATA") && (params.size() >= 3) && (params[0][0] == '#'))
320 // :20D METADATA #channel extname :extdata
321 return InsertCurrentChannelTS(params);
323 else if ((cmd == "FTOPIC") && (params.size() >= 4))
325 // :20D FTOPIC #channel 100 Attila :topic text
326 return InsertCurrentChannelTS(params);
328 else if ((cmd == "PING") || (cmd == "PONG"))
330 if (params.size() == 1)
332 // If it's a PING with 1 parameter, reply with a PONG now, if it's a PONG with 1 parameter (weird), do nothing
334 this->WriteData(":" + ServerInstance->Config->GetSID() + " PONG " + params[0] + newline);
336 // Don't process this message further
342 // Drop the first parameter
343 params.erase(params.begin());
345 // If the target is a server name, translate it to a SID
346 if (!InspIRCd::IsSID(params[0]))
348 TreeServer* server = Utils->FindServer(params[0]);
351 // We've no idea what this is, log and stop processing
352 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Received a " + cmd + " with an unknown target: \"" + params[0] + "\", command dropped");
356 params[0] = server->GetID();
359 else if ((cmd == "GLINE") || (cmd == "KLINE") || (cmd == "ELINE") || (cmd == "ZLINE") || (cmd == "QLINE"))
361 // Fix undocumented protocol usage: translate GLINE, ZLINE, etc. into ADDLINE or DELLINE
362 if ((params.size() != 1) && (params.size() != 3))
366 p.push_back(cmd.substr(0, 1));
367 p.push_back(params[0]);
369 if (params.size() == 3)
372 p.push_back(who->nick);
373 p.push_back(ConvToStr(ServerInstance->Time()));
374 p.push_back(ConvToStr(InspIRCd::Duration(params[1])));
375 p.push_back(params[2]);
382 else if (cmd == "SVSMODE")
386 else if (cmd == "OPERQUIT")
388 // Translate OPERQUIT into METADATA
393 params.insert(params.begin(), who->uuid);
394 params.insert(params.begin()+1, "operquit");
395 who = MyRoot->ServerUser;
397 else if ((cmd == "TOPIC") && (params.size() >= 2))
399 // :20DAAAAAC TOPIC #chan :new topic
401 if (!InsertCurrentChannelTS(params))
404 params.insert(params.begin()+2, ConvToStr(ServerInstance->Time()));
406 else if (cmd == "MODENOTICE")
408 // MODENOTICE is always supported by 2.0 but it's optional in 2.2.
409 params.insert(params.begin(), "*");
410 params.insert(params.begin()+1, cmd);
413 else if (cmd == "RULES")
417 else if (cmd == "INVITE")
419 // :20D INVITE 22DAAABBB #chan
420 // :20D INVITE 22DAAABBB #chan 123456789
421 // Insert channel timestamp after the channel name; the 3rd parameter, if there, is the invite expiration time
422 return InsertCurrentChannelTS(params, 1, 2);
424 else if (cmd == "VERSION")
426 // :20D VERSION :InspIRCd-2.0
428 // :20D SINFO version :InspIRCd-2.0
430 params.insert(params.begin(), "version");
432 else if (cmd == "JOIN")
434 // 2.0 allows and forwards legacy JOINs but we don't, so translate them to FJOINs before processing
435 if ((params.size() != 1) || (IS_SERVER(who)))
436 return false; // Huh?
439 Channel* chan = ServerInstance->FindChan(params[0]);
440 params.push_back(ConvToStr(chan ? chan->age : ServerInstance->Time()));
441 params.push_back("+");
442 params.push_back(",");
443 params.back().append(who->uuid);
444 who = TreeServer::Get(who)->ServerUser;
446 else if ((cmd == "FMODE") && (params.size() >= 2))
448 // Translate user mode changes with timestamp to MODE
449 if (params[0][0] != '#')
451 User* user = ServerInstance->FindUUID(params[0]);
455 // Emulate the old nonsensical behavior
456 if (user->age < ServerCommand::ExtractTS(params[1]))
460 params.erase(params.begin()+1);
463 else if ((cmd == "SERVER") && (params.size() > 4))
465 // This does not affect the initial SERVER line as it is sent before the link state is CONNECTED
466 // :20D SERVER <name> * 0 <sid> <desc>
468 // :20D SERVER <name> <sid> <desc>
470 params[1].swap(params[3]);
471 params.erase(params.begin()+2, params.begin()+4);
473 // If the source of this SERVER message is not bursting, then new servers it introduces are bursting
474 TreeServer* server = TreeServer::Get(who);
475 if (!server->IsBursting())
476 params.insert(params.begin()+2, "burst=" + ConvToStr(ServerInstance->Time()*1000));
478 else if (cmd == "BURST")
480 // A server is introducing another one, drop unnecessary BURST
484 return true; // Passthru