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::WriteLine(const std::string& original_line)
29 if (LinkState == CONNECTED)
31 if (original_line.c_str()[0] != ':')
33 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Sending line without server prefix!");
34 WriteLine(":" + ServerInstance->Config->GetSID() + " " + original_line);
37 if (proto_version != ProtocolVersion)
39 std::string line = original_line;
40 std::string::size_type a = line.find(' ');
41 std::string::size_type b = line.find(' ', a + 1);
42 std::string command = line.substr(a + 1, b-a-1);
43 // now try to find a translation entry
44 // TODO a more efficient lookup method will be needed later
45 if (proto_version < 1205)
47 if (command == "IJOIN")
50 // :<uid> IJOIN <chan> <membid> [<ts> [<flags>]]
52 // :<sid> FJOIN <chan> <ts> + [<flags>],<uuid>
53 std::string::size_type c = line.find(' ', b + 1);
54 if (c == std::string::npos)
57 std::string::size_type d = line.find(' ', c + 1);
58 // Erase membership id first
60 if (d == std::string::npos)
62 // No TS or modes in the command
63 // :22DAAAAAB IJOIN #chan
64 const std::string channame = line.substr(b+1, c-b-1);
65 Channel* chan = ServerInstance->FindChan(channame);
70 line.append(ConvToStr(chan->age));
75 d = line.find(' ', c + 1);
76 if (d == std::string::npos)
78 // TS present, no modes
79 // :22DAAAAAC IJOIN #chan 12345
84 // Both TS and modes are present
85 // :22DAAAAAC IJOIN #chan 12345 ov
86 std::string::size_type e = line.find(' ', d + 1);
87 if (e != std::string::npos)
95 // Move the uuid to the end and replace the I with an F
96 line.append(line.substr(1, 9));
100 else if (command == "RESYNC")
102 else if (command == "METADATA")
104 // Drop TS for channel METADATA, translate METADATA operquit into an OPERQUIT command
105 // :sid METADATA #target TS extname ...
107 if (b == std::string::npos)
110 std::string::size_type c = line.find(' ', b + 1);
111 if (c == std::string::npos)
114 std::string::size_type d = line.find(' ', c + 1);
115 if (d == std::string::npos)
118 if (line[b + 1] == '#')
120 // We're sending channel metadata
123 else if (!line.compare(c, d-c, " operquit", 9))
125 // ":22D METADATA 22DAAAAAX operquit :message" -> ":22DAAAAAX OPERQUIT :message"
126 line = ":" + line.substr(b+1, c-b) + "OPERQUIT" + line.substr(d);
129 else if (command == "FTOPIC")
131 // Drop channel TS for FTOPIC
132 // :sid FTOPIC #target TS TopicTS setter :newtopic
134 // :uid FTOPIC #target TS TopicTS :newtopic
136 if (b == std::string::npos)
139 std::string::size_type c = line.find(' ', b + 1);
140 if (c == std::string::npos)
143 std::string::size_type d = line.find(' ', c + 1);
144 if (d == std::string::npos)
147 std::string::size_type e = line.find(' ', d + 1);
148 if (line[e+1] == ':')
156 else if ((command == "PING") || (command == "PONG"))
159 if (line.length() < 13)
162 // Insert the source SID (and a space) between the command and the first parameter
163 line.insert(10, line.substr(1, 4));
165 else if (command == "OPERTYPE")
167 std::string::size_type colon = line.find(':', b);
168 if (colon != std::string::npos)
170 for (std::string::iterator i = line.begin()+colon; i != line.end(); ++i)
175 line.erase(colon, 1);
178 else if (command == "INVITE")
180 // :22D INVITE 22DAAAAAN #chan TS ExpirationTime
182 if (b == std::string::npos)
185 std::string::size_type c = line.find(' ', b + 1);
186 if (c == std::string::npos)
189 std::string::size_type d = line.find(' ', c + 1);
190 if (d == std::string::npos)
193 std::string::size_type e = line.find(' ', d + 1);
194 // If there is no expiration time then everything will be erased from 'd'
197 else if (command == "FJOIN")
199 // Strip membership ids
200 // :22D FJOIN #chan 1234 +f 4:3 :o,22DAAAAAB:15 o,22DAAAAAA:15
201 // :22D FJOIN #chan 1234 +f 4:3 o,22DAAAAAB:15
202 // :22D FJOIN #chan 1234 +Pf 4:3 :
204 // If the last parameter is prefixed by a colon then it's a userlist which may have 0 or more users;
205 // if it isn't, then it is a single member
206 std::string::size_type spcolon = line.find(" :");
207 if (spcolon != std::string::npos)
210 // Loop while there is a ':' in the userlist, this is never true if the channel is empty
211 std::string::size_type pos = std::string::npos;
212 while ((pos = line.rfind(':', pos-1)) > spcolon)
214 // Find the next space after the ':'
215 std::string::size_type sp = line.find(' ', pos);
216 // Erase characters between the ':' and the next space after it, including the ':' but not the space;
217 // if there is no next space, everything will be erased between pos and the end of the line
218 line.erase(pos, sp-pos);
223 // Last parameter is a single member
224 std::string::size_type sp = line.rfind(' ');
225 std::string::size_type colon = line.find(':', sp);
229 else if (command == "KICK")
231 // Strip membership id if the KICK has one
232 if (b == std::string::npos)
235 std::string::size_type c = line.find(' ', b + 1);
236 if (c == std::string::npos)
239 std::string::size_type d = line.find(' ', c + 1);
240 if ((d < line.size()-1) && (original_line[d+1] != ':'))
242 // There is a third parameter which doesn't begin with a colon, erase it
243 std::string::size_type e = line.find(' ', d + 1);
247 else if (command == "SINFO")
252 ServerInstance->Logs->Log(MODNAME, LOG_RAWIO, "S[%d] O %s", this->GetFd(), line.c_str());
253 this->WriteData(line);
254 this->WriteData(newline);
259 ServerInstance->Logs->Log(MODNAME, LOG_RAWIO, "S[%d] O %s", this->GetFd(), original_line.c_str());
260 this->WriteData(original_line);
261 this->WriteData(newline);
266 bool InsertCurrentChannelTS(std::vector<std::string>& params, unsigned int chanindex = 0, unsigned int pos = 1)
268 Channel* chan = ServerInstance->FindChan(params[chanindex]);
272 // Insert the current TS of the channel after the pos-th parameter
273 params.insert(params.begin()+pos, ConvToStr(chan->age));
278 bool TreeSocket::PreProcessOldProtocolMessage(User*& who, std::string& cmd, std::vector<std::string>& params)
280 if ((cmd == "METADATA") && (params.size() >= 3) && (params[0][0] == '#'))
282 // :20D METADATA #channel extname :extdata
283 return InsertCurrentChannelTS(params);
285 else if ((cmd == "FTOPIC") && (params.size() >= 4))
287 // :20D FTOPIC #channel 100 Attila :topic text
288 return InsertCurrentChannelTS(params);
290 else if ((cmd == "PING") || (cmd == "PONG"))
292 if (params.size() == 1)
294 // If it's a PING with 1 parameter, reply with a PONG now, if it's a PONG with 1 parameter (weird), do nothing
296 this->WriteData(":" + ServerInstance->Config->GetSID() + " PONG " + params[0] + newline);
298 // Don't process this message further
304 // Drop the first parameter
305 params.erase(params.begin());
307 // If the target is a server name, translate it to a SID
308 if (!InspIRCd::IsSID(params[0]))
310 TreeServer* server = Utils->FindServer(params[0]);
313 // We've no idea what this is, log and stop processing
314 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Received a " + cmd + " with an unknown target: \"" + params[0] + "\", command dropped");
318 params[0] = server->GetID();
321 else if ((cmd == "GLINE") || (cmd == "KLINE") || (cmd == "ELINE") || (cmd == "ZLINE") || (cmd == "QLINE"))
323 // Fix undocumented protocol usage: translate GLINE, ZLINE, etc. into ADDLINE or DELLINE
324 if ((params.size() != 1) && (params.size() != 3))
328 p.push_back(cmd.substr(0, 1));
329 p.push_back(params[0]);
331 if (params.size() == 3)
334 p.push_back(who->nick);
335 p.push_back(ConvToStr(ServerInstance->Time()));
336 p.push_back(ConvToStr(InspIRCd::Duration(params[1])));
337 p.push_back(params[2]);
344 else if (cmd == "SVSMODE")
348 else if (cmd == "OPERQUIT")
350 // Translate OPERQUIT into METADATA
355 params.insert(params.begin(), who->uuid);
356 params.insert(params.begin()+1, "operquit");
357 who = MyRoot->ServerUser;
359 else if ((cmd == "TOPIC") && (params.size() >= 2))
361 // :20DAAAAAC TOPIC #chan :new topic
363 if (!InsertCurrentChannelTS(params))
366 params.insert(params.begin()+2, ConvToStr(ServerInstance->Time()));
368 else if (cmd == "MODENOTICE")
370 // MODENOTICE is always supported by 2.0 but it's optional in 2.2.
371 params.insert(params.begin(), "*");
372 params.insert(params.begin()+1, cmd);
375 else if (cmd == "RULES")
379 else if (cmd == "INVITE")
381 // :20D INVITE 22DAAABBB #chan
382 // :20D INVITE 22DAAABBB #chan 123456789
383 // Insert channel timestamp after the channel name; the 3rd parameter, if there, is the invite expiration time
384 return InsertCurrentChannelTS(params, 1, 2);
387 return true; // Passthru