]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/compat.cpp
m_spanningtree Propagate oper-only quit reason using METADATA, remove OPERQUIT
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / compat.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *
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.
9  *
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
13  * details.
14  *
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/>.
17  */
18
19
20 #include "inspircd.h"
21 #include "main.h"
22 #include "treesocket.h"
23 #include "treeserver.h"
24
25 static std::string newline("\n");
26
27 void TreeSocket::WriteLine(std::string line)
28 {
29         if (LinkState == CONNECTED)
30         {
31                 if (line[0] != ':')
32                 {
33                         ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Sending line without server prefix!");
34                         line = ":" + ServerInstance->Config->GetSID() + " " + line;
35                 }
36                 if (proto_version != ProtocolVersion)
37                 {
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)
44                         {
45                                 if (command == "IJOIN")
46                                 {
47                                         // Convert
48                                         // :<uid> IJOIN <chan> [<ts> [<flags>]]
49                                         // to
50                                         // :<sid> FJOIN <chan> <ts> + [<flags>],<uuid>
51                                         std::string::size_type c = line.find(' ', b + 1);
52                                         if (c == std::string::npos)
53                                         {
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);
58                                                 if (!chan)
59                                                         return;
60
61                                                 line.push_back(' ');
62                                                 line.append(ConvToStr(chan->age));
63                                                 line.append(" + ,");
64                                         }
65                                         else
66                                         {
67                                                 std::string::size_type d = line.find(' ', c + 1);
68                                                 if (d == std::string::npos)
69                                                 {
70                                                         // TS present, no modes
71                                                         // :22DAAAAAC IJOIN #chan 12345
72                                                         line.append(" + ,");
73                                                 }
74                                                 else
75                                                 {
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)
80                                                                 line.erase(e);
81
82                                                         line.insert(d, " +");
83                                                         line.push_back(',');
84                                                 }
85                                         }
86
87                                         // Move the uuid to the end and replace the I with an F
88                                         line.append(line.substr(1, 9));
89                                         line.erase(4, 6);
90                                         line[5] = 'F';
91                                 }
92                                 else if (command == "RESYNC")
93                                         return;
94                                 else if (command == "METADATA")
95                                 {
96                                         // Drop TS for channel METADATA, translate METADATA operquit into an OPERQUIT command
97                                         // :sid METADATA #target TS extname ...
98                                         //     A        B       C  D
99                                         if (b == std::string::npos)
100                                                 return;
101
102                                         std::string::size_type c = line.find(' ', b + 1);
103                                         if (c == std::string::npos)
104                                                 return;
105
106                                         std::string::size_type d = line.find(' ', c + 1);
107                                         if (d == std::string::npos)
108                                                 return;
109
110                                         if (line[b + 1] == '#')
111                                         {
112                                                 // We're sending channel metadata
113                                                 line.erase(c, d-c);
114                                         }
115                                         else if (line.substr(c, d-c) == " operquit")
116                                         {
117                                                 // ":22D METADATA 22DAAAAAX operquit :message" -> ":22DAAAAAX OPERQUIT :message"
118                                                 line = ":" + line.substr(b+1, c-b) + "OPERQUIT" + line.substr(d);
119                                         }
120                                 }
121                                 else if (command == "FTOPIC")
122                                 {
123                                         // Drop channel TS for FTOPIC
124                                         // :sid FTOPIC #target TS TopicTS ...
125                                         //     A      B       C  D
126                                         if (b == std::string::npos)
127                                                 return;
128
129                                         std::string::size_type c = line.find(' ', b + 1);
130                                         if (c == std::string::npos)
131                                                 return;
132
133                                         std::string::size_type d = line.find(' ', c + 1);
134                                         if (d == std::string::npos)
135                                                 return;
136
137                                         line.erase(c, d-c);
138                                 }
139                                 else if ((command == "PING") || (command == "PONG"))
140                                 {
141                                         // :22D PING 20D
142                                         if (line.length() < 13)
143                                                 return;
144
145                                         // Insert the source SID (and a space) between the command and the first parameter
146                                         line.insert(10, line.substr(1, 4));
147                                 }
148                                 else if (command == "OPERTYPE")
149                                 {
150                                         std::string::size_type colon = line.find(':', b);
151                                         if (colon != std::string::npos)
152                                         {
153                                                 for (std::string::iterator i = line.begin()+colon; i != line.end(); ++i)
154                                                 {
155                                                         if (*i == ' ')
156                                                                 *i = '_';
157                                                 }
158                                                 line.erase(colon, 1);
159                                         }
160                                 }
161                         }
162                 }
163         }
164
165         ServerInstance->Logs->Log(MODNAME, LOG_RAWIO, "S[%d] O %s", this->GetFd(), line.c_str());
166         this->WriteData(line);
167         this->WriteData(newline);
168 }
169
170 namespace
171 {
172         bool InsertCurrentChannelTS(std::vector<std::string>& params)
173         {
174                 Channel* chan = ServerInstance->FindChan(params[0]);
175                 if (!chan)
176                         return false;
177
178                 // Insert the current TS of the channel between the first and the second parameters
179                 params.insert(params.begin()+1, ConvToStr(chan->age));
180                 return true;
181         }
182 }
183
184 bool TreeSocket::PreProcessOldProtocolMessage(User*& who, std::string& cmd, std::vector<std::string>& params)
185 {
186         if ((cmd == "METADATA") && (params.size() >= 3) && (params[0][0] == '#'))
187         {
188                 // :20D METADATA #channel extname :extdata
189                 return InsertCurrentChannelTS(params);
190         }
191         else if ((cmd == "FTOPIC") && (params.size() >= 4))
192         {
193                 // :20D FTOPIC #channel 100 Attila :topic text
194                 return InsertCurrentChannelTS(params);
195         }
196         else if ((cmd == "PING") || (cmd == "PONG"))
197         {
198                 if (params.size() == 1)
199                 {
200                         // If it's a PING with 1 parameter, reply with a PONG now, if it's a PONG with 1 parameter (weird), do nothing
201                         if (cmd[1] == 'I')
202                                 this->WriteData(":" + ServerInstance->Config->GetSID() + " PONG " + params[0] + newline);
203
204                         // Don't process this message further
205                         return false;
206                 }
207
208                 // :20D PING 20D 22D
209                 // :20D PONG 20D 22D
210                 // Drop the first parameter
211                 params.erase(params.begin());
212
213                 // If the target is a server name, translate it to a SID
214                 if (!InspIRCd::IsSID(params[0]))
215                 {
216                         TreeServer* server = Utils->FindServer(params[0]);
217                         if (!server)
218                         {
219                                 // We've no idea what this is, log and stop processing
220                                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Received a " + cmd + " with an unknown target: \"" + params[0] + "\", command dropped");
221                                 return false;
222                         }
223
224                         params[0] = server->GetID();
225                 }
226         }
227         else if ((cmd == "GLINE") || (cmd == "KLINE") || (cmd == "ELINE") || (cmd == "ZLINE") || (cmd == "QLINE"))
228         {
229                 // Fix undocumented protocol usage: translate GLINE, ZLINE, etc. into ADDLINE or DELLINE
230                 if ((params.size() != 1) && (params.size() != 3))
231                         return false;
232
233                 parameterlist p;
234                 p.push_back(cmd.substr(0, 1));
235                 p.push_back(params[0]);
236
237                 if (params.size() == 3)
238                 {
239                         cmd = "ADDLINE";
240                         p.push_back(who->nick);
241                         p.push_back(ConvToStr(ServerInstance->Time()));
242                         p.push_back(ConvToStr(InspIRCd::Duration(params[1])));
243                         p.push_back(params[2]);
244                 }
245                 else
246                         cmd = "DELLINE";
247
248                 params.swap(p);
249         }
250         else if (cmd == "SVSMODE")
251         {
252                 cmd = "MODE";
253         }
254         else if (cmd == "OPERQUIT")
255         {
256                 // Translate OPERQUIT into METADATA
257                 if (params.empty())
258                         return false;
259
260                 cmd = "METADATA";
261                 params.insert(params.begin(), who->uuid);
262                 params.insert(params.begin()+1, "operquit");
263                 who = MyRoot->ServerUser;
264         }
265
266         return true; // Passthru
267 }