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> [<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)
56 // No TS or modes in the command
57 // :22DAAAAAB IJOIN #chan
58 const std::string channame = line.substr(b+1, c-b-1);
59 Channel* chan = ServerInstance->FindChan(channame);
64 line.append(ConvToStr(chan->age));
69 std::string::size_type d = line.find(' ', c + 1);
70 if (d == std::string::npos)
72 // TS present, no modes
73 // :22DAAAAAC IJOIN #chan 12345
78 // Both TS and modes are present
79 // :22DAAAAAC IJOIN #chan 12345 ov
80 std::string::size_type e = line.find(' ', d + 1);
81 if (e != std::string::npos)
89 // Move the uuid to the end and replace the I with an F
90 line.append(line.substr(1, 9));
94 else if (command == "RESYNC")
96 else if (command == "METADATA")
98 // Drop TS for channel METADATA, translate METADATA operquit into an OPERQUIT command
99 // :sid METADATA #target TS extname ...
101 if (b == std::string::npos)
104 std::string::size_type c = line.find(' ', b + 1);
105 if (c == std::string::npos)
108 std::string::size_type d = line.find(' ', c + 1);
109 if (d == std::string::npos)
112 if (line[b + 1] == '#')
114 // We're sending channel metadata
117 else if (line.substr(c, d-c) == " operquit")
119 // ":22D METADATA 22DAAAAAX operquit :message" -> ":22DAAAAAX OPERQUIT :message"
120 line = ":" + line.substr(b+1, c-b) + "OPERQUIT" + line.substr(d);
123 else if (command == "FTOPIC")
125 // Drop channel TS for FTOPIC
126 // :sid FTOPIC #target TS TopicTS setter :newtopic
128 // :uid FTOPIC #target TS TopicTS :newtopic
130 if (b == std::string::npos)
133 std::string::size_type c = line.find(' ', b + 1);
134 if (c == std::string::npos)
137 std::string::size_type d = line.find(' ', c + 1);
138 if (d == std::string::npos)
141 std::string::size_type e = line.find(' ', d + 1);
142 if (line[e+1] == ':')
150 else if ((command == "PING") || (command == "PONG"))
153 if (line.length() < 13)
156 // Insert the source SID (and a space) between the command and the first parameter
157 line.insert(10, line.substr(1, 4));
159 else if (command == "OPERTYPE")
161 std::string::size_type colon = line.find(':', b);
162 if (colon != std::string::npos)
164 for (std::string::iterator i = line.begin()+colon; i != line.end(); ++i)
169 line.erase(colon, 1);
173 ServerInstance->Logs->Log(MODNAME, LOG_RAWIO, "S[%d] O %s", this->GetFd(), line.c_str());
174 this->WriteData(line);
175 this->WriteData(newline);
180 ServerInstance->Logs->Log(MODNAME, LOG_RAWIO, "S[%d] O %s", this->GetFd(), original_line.c_str());
181 this->WriteData(original_line);
182 this->WriteData(newline);
187 bool InsertCurrentChannelTS(std::vector<std::string>& params)
189 Channel* chan = ServerInstance->FindChan(params[0]);
193 // Insert the current TS of the channel between the first and the second parameters
194 params.insert(params.begin()+1, ConvToStr(chan->age));
199 bool TreeSocket::PreProcessOldProtocolMessage(User*& who, std::string& cmd, std::vector<std::string>& params)
201 if ((cmd == "METADATA") && (params.size() >= 3) && (params[0][0] == '#'))
203 // :20D METADATA #channel extname :extdata
204 return InsertCurrentChannelTS(params);
206 else if ((cmd == "FTOPIC") && (params.size() >= 4))
208 // :20D FTOPIC #channel 100 Attila :topic text
209 return InsertCurrentChannelTS(params);
211 else if ((cmd == "PING") || (cmd == "PONG"))
213 if (params.size() == 1)
215 // If it's a PING with 1 parameter, reply with a PONG now, if it's a PONG with 1 parameter (weird), do nothing
217 this->WriteData(":" + ServerInstance->Config->GetSID() + " PONG " + params[0] + newline);
219 // Don't process this message further
225 // Drop the first parameter
226 params.erase(params.begin());
228 // If the target is a server name, translate it to a SID
229 if (!InspIRCd::IsSID(params[0]))
231 TreeServer* server = Utils->FindServer(params[0]);
234 // We've no idea what this is, log and stop processing
235 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Received a " + cmd + " with an unknown target: \"" + params[0] + "\", command dropped");
239 params[0] = server->GetID();
242 else if ((cmd == "GLINE") || (cmd == "KLINE") || (cmd == "ELINE") || (cmd == "ZLINE") || (cmd == "QLINE"))
244 // Fix undocumented protocol usage: translate GLINE, ZLINE, etc. into ADDLINE or DELLINE
245 if ((params.size() != 1) && (params.size() != 3))
249 p.push_back(cmd.substr(0, 1));
250 p.push_back(params[0]);
252 if (params.size() == 3)
255 p.push_back(who->nick);
256 p.push_back(ConvToStr(ServerInstance->Time()));
257 p.push_back(ConvToStr(InspIRCd::Duration(params[1])));
258 p.push_back(params[2]);
265 else if (cmd == "SVSMODE")
269 else if (cmd == "OPERQUIT")
271 // Translate OPERQUIT into METADATA
276 params.insert(params.begin(), who->uuid);
277 params.insert(params.begin()+1, "operquit");
278 who = MyRoot->ServerUser;
280 else if ((cmd == "TOPIC") && (params.size() >= 2))
282 // :20DAAAAAC TOPIC #chan :new topic
284 if (!InsertCurrentChannelTS(params))
287 params.insert(params.begin()+2, ConvToStr(ServerInstance->Time()));
289 else if (cmd == "MODENOTICE")
291 // MODENOTICE is always supported by 2.0 but it's optional in 2.2.
292 params.insert(params.begin(), "*");
293 params.insert(params.begin()+1, cmd);
296 else if (cmd == "RULES")
301 return true; // Passthru