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