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(std::string line)
29 if (LinkState == CONNECTED)
33 ServerInstance->Logs->Log("m_spanningtree", LOG_DEFAULT, "Sending line without server prefix!");
34 line = ":" + ServerInstance->Config->GetSID() + " " + line;
36 if (proto_version != ProtocolVersion)
38 std::string::size_type a = line.find(' ');
39 std::string::size_type b = line.find(' ', a + 1);
40 std::string command = line.substr(a + 1, b-a-1);
41 // now try to find a translation entry
42 // TODO a more efficient lookup method will be needed later
43 if (proto_version < 1205)
45 if (command == "IJOIN")
48 // :<uid> IJOIN <chan> [<ts> [<flags>]]
50 // :<sid> FJOIN <chan> <ts> + [<flags>],<uuid>
51 std::string::size_type c = line.find(' ', b + 1);
52 if (c == std::string::npos)
54 // No TS or modes in the command
55 // :22DAAAAAB IJOIN #chan
56 const std::string channame = line.substr(b+1, c-b-1);
57 Channel* chan = ServerInstance->FindChan(channame);
62 line.append(ConvToStr(chan->age));
67 std::string::size_type d = line.find(' ', c + 1);
68 if (d == std::string::npos)
70 // TS present, no modes
71 // :22DAAAAAC IJOIN #chan 12345
76 // Both TS and modes are present
77 // :22DAAAAAC IJOIN #chan 12345 ov
78 std::string::size_type e = line.find(' ', d + 1);
79 if (e != std::string::npos)
87 // Move the uuid to the end and replace the I with an F
88 line.append(line.substr(1, 9));
92 else if (command == "RESYNC")
94 else if (command == "METADATA")
96 // Drop TS for channel METADATA
97 // :sid METADATA #target TS extname ...
99 if (b == std::string::npos)
102 std::string::size_type c = line.find(' ', b + 1);
103 if (c == std::string::npos)
106 if (line[b + 1] == '#')
108 // We're sending channel metadata
109 std::string::size_type d = line.find(' ', c + 1);
110 if (d == std::string::npos)
116 else if (command == "FTOPIC")
118 // Drop channel TS for FTOPIC
119 // :sid FTOPIC #target TS TopicTS ...
121 if (b == std::string::npos)
124 std::string::size_type c = line.find(' ', b + 1);
125 if (c == std::string::npos)
128 std::string::size_type d = line.find(' ', c + 1);
129 if (d == std::string::npos)
134 else if ((command == "PING") || (command == "PONG"))
137 if (line.length() < 13)
140 // Insert the source SID (and a space) between the command and the first parameter
141 line.insert(10, line.substr(1, 4));
143 else if (command == "OPERTYPE")
145 std::string::size_type colon = line.find(':', b);
146 if (colon != std::string::npos)
148 for (std::string::iterator i = line.begin()+colon; i != line.end(); ++i)
153 line.erase(colon, 1);
160 ServerInstance->Logs->Log("m_spanningtree", LOG_RAWIO, "S[%d] O %s", this->GetFd(), line.c_str());
161 this->WriteData(line);
162 this->WriteData(newline);
167 bool InsertCurrentChannelTS(std::vector<std::string>& params)
169 Channel* chan = ServerInstance->FindChan(params[0]);
173 // Insert the current TS of the channel between the first and the second parameters
174 params.insert(params.begin()+1, ConvToStr(chan->age));
179 bool TreeSocket::PreProcessOldProtocolMessage(User*& who, std::string& cmd, std::vector<std::string>& params)
181 if ((cmd == "METADATA") && (params.size() >= 3))
183 // :20D METADATA #channel extname :extdata
184 return InsertCurrentChannelTS(params);
186 else if ((cmd == "FTOPIC") && (params.size() >= 4))
188 // :20D FTOPIC #channel 100 Attila :topic text
189 return InsertCurrentChannelTS(params);
191 else if ((cmd == "PING") || (cmd == "PONG"))
193 if (params.size() == 1)
195 // If it's a PING with 1 parameter, reply with a PONG now, if it's a PONG with 1 parameter (weird), do nothing
197 this->WriteData(":" + ServerInstance->Config->GetSID() + " PONG " + params[0] + newline);
199 // Don't process this message further
205 // Drop the first parameter
206 params.erase(params.begin());
208 // If the target is a server name, translate it to a SID
209 if (!InspIRCd::IsSID(params[0]))
211 TreeServer* server = Utils->FindServer(params[0]);
214 // We've no idea what this is, log and stop processing
215 ServerInstance->Logs->Log("m_spanningtree", LOG_DEFAULT, "Received a " + cmd + " with an unknown target: \"" + params[0] + "\", command dropped");
219 params[0] = server->GetID();
223 return true; // Passthru