]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/compat.cpp
m_spanningtree Translate SINFO version to VERSION and vice versa for 1202 protocol...
[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(const std::string& original_line)
28 {
29         if (LinkState == CONNECTED)
30         {
31                 if (original_line.c_str()[0] != ':')
32                 {
33                         ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Sending line without server prefix!");
34                         WriteLine(":" + ServerInstance->Config->GetSID() + " " + original_line);
35                         return;
36                 }
37                 if (proto_version != ProtocolVersion)
38                 {
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)
46                         {
47                                 if (command == "IJOIN")
48                                 {
49                                         // Convert
50                                         // :<uid> IJOIN <chan> <membid> [<ts> [<flags>]]
51                                         // to
52                                         // :<sid> FJOIN <chan> <ts> + [<flags>],<uuid>
53                                         std::string::size_type c = line.find(' ', b + 1);
54                                         if (c == std::string::npos)
55                                                 return;
56
57                                         std::string::size_type d = line.find(' ', c + 1);
58                                         // Erase membership id first
59                                         line.erase(c, d-c);
60                                         if (d == std::string::npos)
61                                         {
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);
66                                                 if (!chan)
67                                                         return;
68
69                                                 line.push_back(' ');
70                                                 line.append(ConvToStr(chan->age));
71                                                 line.append(" + ,");
72                                         }
73                                         else
74                                         {
75                                                 d = line.find(' ', c + 1);
76                                                 if (d == std::string::npos)
77                                                 {
78                                                         // TS present, no modes
79                                                         // :22DAAAAAC IJOIN #chan 12345
80                                                         line.append(" + ,");
81                                                 }
82                                                 else
83                                                 {
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)
88                                                                 line.erase(e);
89
90                                                         line.insert(d, " +");
91                                                         line.push_back(',');
92                                                 }
93                                         }
94
95                                         // Move the uuid to the end and replace the I with an F
96                                         line.append(line.substr(1, 9));
97                                         line.erase(4, 6);
98                                         line[5] = 'F';
99                                 }
100                                 else if (command == "RESYNC")
101                                         return;
102                                 else if (command == "METADATA")
103                                 {
104                                         // Drop TS for channel METADATA, translate METADATA operquit into an OPERQUIT command
105                                         // :sid METADATA #target TS extname ...
106                                         //     A        B       C  D
107                                         if (b == std::string::npos)
108                                                 return;
109
110                                         std::string::size_type c = line.find(' ', b + 1);
111                                         if (c == std::string::npos)
112                                                 return;
113
114                                         std::string::size_type d = line.find(' ', c + 1);
115                                         if (d == std::string::npos)
116                                                 return;
117
118                                         if (line[b + 1] == '#')
119                                         {
120                                                 // We're sending channel metadata
121                                                 line.erase(c, d-c);
122                                         }
123                                         else if (!line.compare(c, d-c, " operquit", 9))
124                                         {
125                                                 // ":22D METADATA 22DAAAAAX operquit :message" -> ":22DAAAAAX OPERQUIT :message"
126                                                 line = ":" + line.substr(b+1, c-b) + "OPERQUIT" + line.substr(d);
127                                         }
128                                 }
129                                 else if (command == "FTOPIC")
130                                 {
131                                         // Drop channel TS for FTOPIC
132                                         // :sid FTOPIC #target TS TopicTS setter :newtopic
133                                         //     A      B       C  D       E      F
134                                         // :uid FTOPIC #target TS TopicTS :newtopic
135                                         //     A      B       C  D       E
136                                         if (b == std::string::npos)
137                                                 return;
138
139                                         std::string::size_type c = line.find(' ', b + 1);
140                                         if (c == std::string::npos)
141                                                 return;
142
143                                         std::string::size_type d = line.find(' ', c + 1);
144                                         if (d == std::string::npos)
145                                                 return;
146
147                                         std::string::size_type e = line.find(' ', d + 1);
148                                         if (line[e+1] == ':')
149                                         {
150                                                 line.erase(c, e-c);
151                                                 line.erase(a+1, 1);
152                                         }
153                                         else
154                                                 line.erase(c, d-c);
155                                 }
156                                 else if ((command == "PING") || (command == "PONG"))
157                                 {
158                                         // :22D PING 20D
159                                         if (line.length() < 13)
160                                                 return;
161
162                                         // Insert the source SID (and a space) between the command and the first parameter
163                                         line.insert(10, line.substr(1, 4));
164                                 }
165                                 else if (command == "OPERTYPE")
166                                 {
167                                         std::string::size_type colon = line.find(':', b);
168                                         if (colon != std::string::npos)
169                                         {
170                                                 for (std::string::iterator i = line.begin()+colon; i != line.end(); ++i)
171                                                 {
172                                                         if (*i == ' ')
173                                                                 *i = '_';
174                                                 }
175                                                 line.erase(colon, 1);
176                                         }
177                                 }
178                                 else if (command == "INVITE")
179                                 {
180                                         // :22D INVITE 22DAAAAAN #chan TS ExpirationTime
181                                         //     A      B         C     D  E
182                                         if (b == std::string::npos)
183                                                 return;
184
185                                         std::string::size_type c = line.find(' ', b + 1);
186                                         if (c == std::string::npos)
187                                                 return;
188
189                                         std::string::size_type d = line.find(' ', c + 1);
190                                         if (d == std::string::npos)
191                                                 return;
192
193                                         std::string::size_type e = line.find(' ', d + 1);
194                                         // If there is no expiration time then everything will be erased from 'd'
195                                         line.erase(d, e-d);
196                                 }
197                                 else if (command == "FJOIN")
198                                 {
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 :
203
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)
208                                         {
209                                                 spcolon++;
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)
213                                                 {
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);
219                                                 }
220                                         }
221                                         else
222                                         {
223                                                 // Last parameter is a single member
224                                                 std::string::size_type sp = line.rfind(' ');
225                                                 std::string::size_type colon = line.find(':', sp);
226                                                 line.erase(colon);
227                                         }
228                                 }
229                                 else if (command == "KICK")
230                                 {
231                                         // Strip membership id if the KICK has one
232                                         if (b == std::string::npos)
233                                                 return;
234
235                                         std::string::size_type c = line.find(' ', b + 1);
236                                         if (c == std::string::npos)
237                                                 return;
238
239                                         std::string::size_type d = line.find(' ', c + 1);
240                                         if ((d < line.size()-1) && (original_line[d+1] != ':'))
241                                         {
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);
244                                                 line.erase(d, e-d);
245                                         }
246                                 }
247                                 else if (command == "SINFO")
248                                 {
249                                         // :22D SINFO version :InspIRCd-2.2
250                                         //     A     B       C
251                                         std::string::size_type c = line.find(' ', b + 1);
252                                         if (c == std::string::npos)
253                                                 return;
254
255                                         // Only translating SINFO version, discard everything else
256                                         if (line.compare(b, 9, " version ", 9))
257                                                 return;
258
259                                         line = line.substr(0, 5) + "VERSION" + line.substr(c);
260                                 }
261                         }
262                         ServerInstance->Logs->Log(MODNAME, LOG_RAWIO, "S[%d] O %s", this->GetFd(), line.c_str());
263                         this->WriteData(line);
264                         this->WriteData(newline);
265                         return;
266                 }
267         }
268
269         ServerInstance->Logs->Log(MODNAME, LOG_RAWIO, "S[%d] O %s", this->GetFd(), original_line.c_str());
270         this->WriteData(original_line);
271         this->WriteData(newline);
272 }
273
274 namespace
275 {
276         bool InsertCurrentChannelTS(std::vector<std::string>& params, unsigned int chanindex = 0, unsigned int pos = 1)
277         {
278                 Channel* chan = ServerInstance->FindChan(params[chanindex]);
279                 if (!chan)
280                         return false;
281
282                 // Insert the current TS of the channel after the pos-th parameter
283                 params.insert(params.begin()+pos, ConvToStr(chan->age));
284                 return true;
285         }
286 }
287
288 bool TreeSocket::PreProcessOldProtocolMessage(User*& who, std::string& cmd, std::vector<std::string>& params)
289 {
290         if ((cmd == "METADATA") && (params.size() >= 3) && (params[0][0] == '#'))
291         {
292                 // :20D METADATA #channel extname :extdata
293                 return InsertCurrentChannelTS(params);
294         }
295         else if ((cmd == "FTOPIC") && (params.size() >= 4))
296         {
297                 // :20D FTOPIC #channel 100 Attila :topic text
298                 return InsertCurrentChannelTS(params);
299         }
300         else if ((cmd == "PING") || (cmd == "PONG"))
301         {
302                 if (params.size() == 1)
303                 {
304                         // If it's a PING with 1 parameter, reply with a PONG now, if it's a PONG with 1 parameter (weird), do nothing
305                         if (cmd[1] == 'I')
306                                 this->WriteData(":" + ServerInstance->Config->GetSID() + " PONG " + params[0] + newline);
307
308                         // Don't process this message further
309                         return false;
310                 }
311
312                 // :20D PING 20D 22D
313                 // :20D PONG 20D 22D
314                 // Drop the first parameter
315                 params.erase(params.begin());
316
317                 // If the target is a server name, translate it to a SID
318                 if (!InspIRCd::IsSID(params[0]))
319                 {
320                         TreeServer* server = Utils->FindServer(params[0]);
321                         if (!server)
322                         {
323                                 // We've no idea what this is, log and stop processing
324                                 ServerInstance->Logs->Log(MODNAME, LOG_DEFAULT, "Received a " + cmd + " with an unknown target: \"" + params[0] + "\", command dropped");
325                                 return false;
326                         }
327
328                         params[0] = server->GetID();
329                 }
330         }
331         else if ((cmd == "GLINE") || (cmd == "KLINE") || (cmd == "ELINE") || (cmd == "ZLINE") || (cmd == "QLINE"))
332         {
333                 // Fix undocumented protocol usage: translate GLINE, ZLINE, etc. into ADDLINE or DELLINE
334                 if ((params.size() != 1) && (params.size() != 3))
335                         return false;
336
337                 parameterlist p;
338                 p.push_back(cmd.substr(0, 1));
339                 p.push_back(params[0]);
340
341                 if (params.size() == 3)
342                 {
343                         cmd = "ADDLINE";
344                         p.push_back(who->nick);
345                         p.push_back(ConvToStr(ServerInstance->Time()));
346                         p.push_back(ConvToStr(InspIRCd::Duration(params[1])));
347                         p.push_back(params[2]);
348                 }
349                 else
350                         cmd = "DELLINE";
351
352                 params.swap(p);
353         }
354         else if (cmd == "SVSMODE")
355         {
356                 cmd = "MODE";
357         }
358         else if (cmd == "OPERQUIT")
359         {
360                 // Translate OPERQUIT into METADATA
361                 if (params.empty())
362                         return false;
363
364                 cmd = "METADATA";
365                 params.insert(params.begin(), who->uuid);
366                 params.insert(params.begin()+1, "operquit");
367                 who = MyRoot->ServerUser;
368         }
369         else if ((cmd == "TOPIC") && (params.size() >= 2))
370         {
371                 // :20DAAAAAC TOPIC #chan :new topic
372                 cmd = "FTOPIC";
373                 if (!InsertCurrentChannelTS(params))
374                         return false;
375
376                 params.insert(params.begin()+2, ConvToStr(ServerInstance->Time()));
377         }
378         else if (cmd == "MODENOTICE")
379         {
380                 // MODENOTICE is always supported by 2.0 but it's optional in 2.2.
381                 params.insert(params.begin(), "*");
382                 params.insert(params.begin()+1, cmd);
383                 cmd = "ENCAP";
384         }
385         else if (cmd == "RULES")
386         {
387                 return false;
388         }
389         else if (cmd == "INVITE")
390         {
391                 // :20D INVITE 22DAAABBB #chan
392                 // :20D INVITE 22DAAABBB #chan 123456789
393                 // Insert channel timestamp after the channel name; the 3rd parameter, if there, is the invite expiration time
394                 return InsertCurrentChannelTS(params, 1, 2);
395         }
396         else if (cmd == "VERSION")
397         {
398                 // :20D VERSION :InspIRCd-2.0
399                 // change to
400                 // :20D SINFO version :InspIRCd-2.0
401                 cmd = "SINFO";
402                 params.insert(params.begin(), "version");
403         }
404
405         return true; // Passthru
406 }