]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/compat.cpp
5893a09fd64eeda13a581c3dcb4063f332576012
[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                                         return;
250                                 }
251                         }
252                         ServerInstance->Logs->Log(MODNAME, LOG_RAWIO, "S[%d] O %s", this->GetFd(), line.c_str());
253                         this->WriteData(line);
254                         this->WriteData(newline);
255                         return;
256                 }
257         }
258
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);
262 }
263
264 namespace
265 {
266         bool InsertCurrentChannelTS(std::vector<std::string>& params, unsigned int chanindex = 0, unsigned int pos = 1)
267         {
268                 Channel* chan = ServerInstance->FindChan(params[chanindex]);
269                 if (!chan)
270                         return false;
271
272                 // Insert the current TS of the channel after the pos-th parameter
273                 params.insert(params.begin()+pos, ConvToStr(chan->age));
274                 return true;
275         }
276 }
277
278 bool TreeSocket::PreProcessOldProtocolMessage(User*& who, std::string& cmd, std::vector<std::string>& params)
279 {
280         if ((cmd == "METADATA") && (params.size() >= 3) && (params[0][0] == '#'))
281         {
282                 // :20D METADATA #channel extname :extdata
283                 return InsertCurrentChannelTS(params);
284         }
285         else if ((cmd == "FTOPIC") && (params.size() >= 4))
286         {
287                 // :20D FTOPIC #channel 100 Attila :topic text
288                 return InsertCurrentChannelTS(params);
289         }
290         else if ((cmd == "PING") || (cmd == "PONG"))
291         {
292                 if (params.size() == 1)
293                 {
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
295                         if (cmd[1] == 'I')
296                                 this->WriteData(":" + ServerInstance->Config->GetSID() + " PONG " + params[0] + newline);
297
298                         // Don't process this message further
299                         return false;
300                 }
301
302                 // :20D PING 20D 22D
303                 // :20D PONG 20D 22D
304                 // Drop the first parameter
305                 params.erase(params.begin());
306
307                 // If the target is a server name, translate it to a SID
308                 if (!InspIRCd::IsSID(params[0]))
309                 {
310                         TreeServer* server = Utils->FindServer(params[0]);
311                         if (!server)
312                         {
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");
315                                 return false;
316                         }
317
318                         params[0] = server->GetID();
319                 }
320         }
321         else if ((cmd == "GLINE") || (cmd == "KLINE") || (cmd == "ELINE") || (cmd == "ZLINE") || (cmd == "QLINE"))
322         {
323                 // Fix undocumented protocol usage: translate GLINE, ZLINE, etc. into ADDLINE or DELLINE
324                 if ((params.size() != 1) && (params.size() != 3))
325                         return false;
326
327                 parameterlist p;
328                 p.push_back(cmd.substr(0, 1));
329                 p.push_back(params[0]);
330
331                 if (params.size() == 3)
332                 {
333                         cmd = "ADDLINE";
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]);
338                 }
339                 else
340                         cmd = "DELLINE";
341
342                 params.swap(p);
343         }
344         else if (cmd == "SVSMODE")
345         {
346                 cmd = "MODE";
347         }
348         else if (cmd == "OPERQUIT")
349         {
350                 // Translate OPERQUIT into METADATA
351                 if (params.empty())
352                         return false;
353
354                 cmd = "METADATA";
355                 params.insert(params.begin(), who->uuid);
356                 params.insert(params.begin()+1, "operquit");
357                 who = MyRoot->ServerUser;
358         }
359         else if ((cmd == "TOPIC") && (params.size() >= 2))
360         {
361                 // :20DAAAAAC TOPIC #chan :new topic
362                 cmd = "FTOPIC";
363                 if (!InsertCurrentChannelTS(params))
364                         return false;
365
366                 params.insert(params.begin()+2, ConvToStr(ServerInstance->Time()));
367         }
368         else if (cmd == "MODENOTICE")
369         {
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);
373                 cmd = "ENCAP";
374         }
375         else if (cmd == "RULES")
376         {
377                 return false;
378         }
379         else if (cmd == "INVITE")
380         {
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);
385         }
386
387         return true; // Passthru
388 }