]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/capab.cpp
039a3c5e70b7a5db8de36497b5b046b35ce3aa36
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / capab.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
6  *   Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
7  *
8  * This file is part of InspIRCd.  InspIRCd is free software: you can
9  * redistribute it and/or modify it under the terms of the GNU General Public
10  * License as published by the Free Software Foundation, version 2.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21
22 #include "inspircd.h"
23
24 #include "treeserver.h"
25 #include "utils.h"
26 #include "link.h"
27 #include "main.h"
28
29 std::string TreeSocket::MyModules(int filter)
30 {
31         std::vector<std::string> modlist = ServerInstance->Modules->GetAllModuleNames(filter);
32
33         std::string capabilities;
34         sort(modlist.begin(),modlist.end());
35         for (std::vector<std::string>::const_iterator i = modlist.begin(); i != modlist.end(); ++i)
36         {
37                 if (i != modlist.begin())
38                         capabilities.push_back(' ');
39                 capabilities.append(*i);
40                 Module* m = ServerInstance->Modules->Find(*i);
41                 Version v = m->GetVersion();
42                 if (!v.link_data.empty())
43                 {
44                         capabilities.push_back('=');
45                         capabilities.append(v.link_data);
46                 }
47         }
48         return capabilities;
49 }
50
51 static std::string BuildModeList(ModeType type)
52 {
53         std::vector<std::string> modes;
54         for(char c='A'; c <= 'z'; c++)
55         {
56                 ModeHandler* mh = ServerInstance->Modes->FindMode(c, type);
57                 if (mh)
58                 {
59                         std::string mdesc = mh->name;
60                         mdesc.push_back('=');
61                         if (mh->GetPrefix())
62                                 mdesc.push_back(mh->GetPrefix());
63                         if (mh->GetModeChar())
64                                 mdesc.push_back(mh->GetModeChar());
65                         modes.push_back(mdesc);
66                 }
67         }
68         sort(modes.begin(), modes.end());
69         irc::stringjoiner line(" ", modes, 0, modes.size() - 1);
70         return line.GetJoined();
71 }
72
73 void TreeSocket::SendCapabilities(int phase)
74 {
75         if (capab->capab_phase >= phase)
76                 return;
77
78         if (capab->capab_phase < 1 && phase >= 1)
79                 WriteLine("CAPAB START " + ConvToStr(ProtocolVersion));
80
81         capab->capab_phase = phase;
82         if (phase < 2)
83                 return;
84
85         const char sep = ' ';
86         irc::sepstream modulelist(MyModules(VF_COMMON), sep);
87         irc::sepstream optmodulelist(MyModules(VF_OPTCOMMON), sep);
88         /* Send module names, split at 509 length */
89         std::string item;
90         std::string line = "CAPAB MODULES :";
91         while (modulelist.GetToken(item))
92         {
93                 if (line.length() + item.length() + 1 > 509)
94                 {
95                         this->WriteLine(line);
96                         line = "CAPAB MODULES :";
97                 }
98
99                 if (line != "CAPAB MODULES :")
100                         line.push_back(sep);
101
102                 line.append(item);
103         }
104         if (line != "CAPAB MODULES :")
105                 this->WriteLine(line);
106
107         line = "CAPAB MODSUPPORT :";
108         while (optmodulelist.GetToken(item))
109         {
110                 if (line.length() + item.length() + 1 > 509)
111                 {
112                         this->WriteLine(line);
113                         line = "CAPAB MODSUPPORT :";
114                 }
115
116                 if (line != "CAPAB MODSUPPORT :")
117                         line.push_back(sep);
118
119                 line.append(item);
120         }
121         if (line != "CAPAB MODSUPPORT :")
122                 this->WriteLine(line);
123
124         WriteLine("CAPAB CHANMODES :" + BuildModeList(MODETYPE_CHANNEL));
125         WriteLine("CAPAB USERMODES :" + BuildModeList(MODETYPE_USER));
126
127         std::string extra;
128         /* Do we have sha256 available? If so, we send a challenge */
129         if (Utils->ChallengeResponse && (ServerInstance->Modules->Find("m_sha256.so")))
130         {
131                 SetOurChallenge(ServerInstance->GenRandomStr(20));
132                 extra = " CHALLENGE=" + this->GetOurChallenge();
133         }
134
135         this->WriteLine("CAPAB CAPABILITIES " /* Preprocessor does this one. */
136                         ":NICKMAX="+ConvToStr(ServerInstance->Config->Limits.NickMax)+
137                         " CHANMAX="+ConvToStr(ServerInstance->Config->Limits.ChanMax)+
138                         " MAXMODES="+ConvToStr(ServerInstance->Config->Limits.MaxModes)+
139                         " IDENTMAX="+ConvToStr(ServerInstance->Config->Limits.IdentMax)+
140                         " MAXQUIT="+ConvToStr(ServerInstance->Config->Limits.MaxQuit)+
141                         " MAXTOPIC="+ConvToStr(ServerInstance->Config->Limits.MaxTopic)+
142                         " MAXKICK="+ConvToStr(ServerInstance->Config->Limits.MaxKick)+
143                         " MAXGECOS="+ConvToStr(ServerInstance->Config->Limits.MaxGecos)+
144                         " MAXAWAY="+ConvToStr(ServerInstance->Config->Limits.MaxAway)+
145                         " IP6SUPPORT=1"+
146                         " PROTOCOL="+ConvToStr(ProtocolVersion)+extra+
147                         " PREFIX="+ServerInstance->Modes->BuildPrefixes()+
148                         " CHANMODES="+ServerInstance->Modes->GiveModeList(MASK_CHANNEL)+
149                         " USERMODES="+ServerInstance->Modes->GiveModeList(MASK_USER)+
150                         " SVSPART=1");
151
152         this->WriteLine("CAPAB END");
153 }
154
155 /* Isolate and return the elements that are different between two comma seperated lists */
156 void TreeSocket::ListDifference(const std::string &one, const std::string &two, char sep,
157                 std::string& mleft, std::string& mright)
158 {
159         std::set<std::string> values;
160         irc::sepstream sepleft(one, sep);
161         irc::sepstream sepright(two, sep);
162         std::string item;
163         while (sepleft.GetToken(item))
164         {
165                 values.insert(item);
166         }
167         while (sepright.GetToken(item))
168         {
169                 if (!values.erase(item))
170                 {
171                         mright.push_back(sep);
172                         mright.append(item);
173                 }
174         }
175         for(std::set<std::string>::iterator i = values.begin(); i != values.end(); ++i)
176         {
177                 mleft.push_back(sep);
178                 mleft.append(*i);
179         }
180 }
181
182 bool TreeSocket::Capab(const parameterlist &params)
183 {
184         if (params.size() < 1)
185         {
186                 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
187                 return false;
188         }
189         if (params[0] == "START")
190         {
191                 capab->ModuleList.clear();
192                 capab->OptModuleList.clear();
193                 capab->CapKeys.clear();
194                 if (params.size() > 1)
195                         proto_version = ConvToInt(params[1]);
196                 SendCapabilities(2);
197         }
198         else if (params[0] == "END")
199         {
200                 std::string reason;
201                 /* Compare ModuleList and check CapKeys */
202                 if ((this->capab->ModuleList != this->MyModules(VF_COMMON)) && (this->capab->ModuleList.length()))
203                 {
204                         std::string diffIneed, diffUneed;
205                         ListDifference(this->capab->ModuleList, this->MyModules(VF_COMMON), ' ', diffIneed, diffUneed);
206                         if (diffIneed.length() || diffUneed.length())
207                         {
208                                 reason = "Modules incorrectly matched on these servers.";
209                                 if (diffIneed.length())
210                                         reason += " Not loaded here:" + diffIneed;
211                                 if (diffUneed.length())
212                                         reason += " Not loaded there:" + diffUneed;
213                                 this->SendError("CAPAB negotiation failed: "+reason);
214                                 return false;
215                         }
216                 }
217                 if (this->capab->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->capab->OptModuleList.length())
218                 {
219                         std::string diffIneed, diffUneed;
220                         ListDifference(this->capab->OptModuleList, this->MyModules(VF_OPTCOMMON), ' ', diffIneed, diffUneed);
221                         if (diffIneed.length() || diffUneed.length())
222                         {
223                                 if (Utils->AllowOptCommon)
224                                 {
225                                         ServerInstance->SNO->WriteToSnoMask('l',
226                                                 "Optional module lists do not match, some commands may not work globally.%s%s%s%s",
227                                                 diffIneed.length() ? " Not loaded here:" : "", diffIneed.c_str(),
228                                                 diffUneed.length() ? " Not loaded there:" : "", diffUneed.c_str());
229                                 }
230                                 else
231                                 {
232                                         reason = "Optional modules incorrectly matched on these servers, and options::allowmismatch not set.";
233                                         if (diffIneed.length())
234                                                 reason += " Not loaded here:" + diffIneed;
235                                         if (diffUneed.length())
236                                                 reason += " Not loaded there:" + diffUneed;
237                                         this->SendError("CAPAB negotiation failed: "+reason);
238                                         return false;
239                                 }
240                         }
241                 }
242
243                 if (this->capab->CapKeys.find("PROTOCOL") == this->capab->CapKeys.end())
244                 {
245                         reason = "Protocol version not specified";
246                 }
247                 else
248                 {
249                         proto_version = ConvToInt(capab->CapKeys.find("PROTOCOL")->second);
250                         if (proto_version < MinCompatProtocol)
251                         {
252                                 reason = "Server is using protocol version " + ConvToStr(proto_version) +
253                                         " which is too old to link with this server (version " + ConvToStr(ProtocolVersion)
254                                         + (ProtocolVersion != MinCompatProtocol ? ", links with " + ConvToStr(MinCompatProtocol) + " and above)" : ")");
255                         }
256                 }
257
258                 if(this->capab->CapKeys.find("PREFIX") != this->capab->CapKeys.end() && this->capab->CapKeys.find("PREFIX")->second != ServerInstance->Modes->BuildPrefixes())
259                         reason = "One or more of the prefixes on the remote server are invalid on this server.";
260
261                 if (!capab->ChanModes.empty())
262                 {
263                         if (capab->ChanModes != BuildModeList(MODETYPE_CHANNEL))
264                         {
265                                 std::string diffIneed, diffUneed;
266                                 ListDifference(capab->ChanModes, BuildModeList(MODETYPE_CHANNEL), ' ', diffIneed, diffUneed);
267                                 if (diffIneed.length() || diffUneed.length())
268                                 {
269                                         reason = "Channel modes not matched on these servers.";
270                                         if (diffIneed.length())
271                                                 reason += " Not loaded here:" + diffIneed;
272                                         if (diffUneed.length())
273                                                 reason += " Not loaded there:" + diffUneed;
274                                 }
275                         }
276                 }
277                 else if (this->capab->CapKeys.find("CHANMODES") != this->capab->CapKeys.end())
278                 {
279                         if (this->capab->CapKeys.find("CHANMODES")->second != ServerInstance->Modes->GiveModeList(MASK_CHANNEL))
280                                 reason = "One or more of the channel modes on the remote server are invalid on this server.";
281                 }
282
283                 if (!capab->UserModes.empty())
284                 {
285                         if (capab->UserModes != BuildModeList(MODETYPE_USER))
286                         {
287                                 std::string diffIneed, diffUneed;
288                                 ListDifference(capab->UserModes, BuildModeList(MODETYPE_USER), ' ', diffIneed, diffUneed);
289                                 if (diffIneed.length() || diffUneed.length())
290                                 {
291                                         reason = "User modes not matched on these servers.";
292                                         if (diffIneed.length())
293                                                 reason += " Not loaded here:" + diffIneed;
294                                         if (diffUneed.length())
295                                                 reason += " Not loaded there:" + diffUneed;
296                                 }
297                         }
298                 }
299                 else if (this->capab->CapKeys.find("USERMODES") != this->capab->CapKeys.end())
300                 {
301                         if (this->capab->CapKeys.find("USERMODES")->second != ServerInstance->Modes->GiveModeList(MASK_USER))
302                                 reason = "One or more of the user modes on the remote server are invalid on this server.";
303                 }
304
305                 /* Challenge response, store their challenge for our password */
306                 std::map<std::string,std::string>::iterator n = this->capab->CapKeys.find("CHALLENGE");
307                 if (Utils->ChallengeResponse && (n != this->capab->CapKeys.end()) && (ServerInstance->Modules->Find("m_sha256.so")))
308                 {
309                         /* Challenge-response is on now */
310                         this->SetTheirChallenge(n->second);
311                         if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
312                         {
313                                 this->SendCapabilities(2);
314                                 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+this->MakePass(capab->link->SendPass, capab->theirchallenge)+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
315                         }
316                 }
317                 else
318                 {
319                         /* They didnt specify a challenge or we don't have m_sha256.so, we use plaintext */
320                         if (this->LinkState == CONNECTING)
321                         {
322                                 this->SendCapabilities(2);
323                                 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+capab->link->SendPass+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
324                         }
325                 }
326
327                 if (reason.length())
328                 {
329                         this->SendError("CAPAB negotiation failed: "+reason);
330                         return false;
331                 }
332         }
333         else if ((params[0] == "MODULES") && (params.size() == 2))
334         {
335                 if (!capab->ModuleList.length())
336                 {
337                         capab->ModuleList = params[1];
338                 }
339                 else
340                 {
341                         capab->ModuleList.push_back(' ');
342                         capab->ModuleList.append(params[1]);
343                 }
344         }
345         else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
346         {
347                 if (!capab->OptModuleList.length())
348                 {
349                         capab->OptModuleList = params[1];
350                 }
351                 else
352                 {
353                         capab->OptModuleList.push_back(' ');
354                         capab->OptModuleList.append(params[1]);
355                 }
356         }
357         else if ((params[0] == "CHANMODES") && (params.size() == 2))
358         {
359                 capab->ChanModes = params[1];
360         }
361         else if ((params[0] == "USERMODES") && (params.size() == 2))
362         {
363                 capab->UserModes = params[1];
364         }
365         else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
366         {
367                 irc::tokenstream capabs(params[1]);
368                 std::string item;
369                 while (capabs.GetToken(item))
370                 {
371                         /* Process each key/value pair */
372                         std::string::size_type equals = item.find('=');
373                         if (equals != std::string::npos)
374                         {
375                                 std::string var = item.substr(0, equals);
376                                 std::string value = item.substr(equals+1, item.length());
377                                 capab->CapKeys[var] = value;
378                         }
379                 }
380         }
381         return true;
382 }
383