2 * InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
24 #include "treeserver.h"
35 static CompatMod compatmods[] =
37 { "m_watch.so", VF_OPTCOMMON }
40 std::string TreeSocket::MyModules(int filter)
42 const ModuleManager::ModuleMap& modlist = ServerInstance->Modules->GetModules();
44 std::string capabilities;
45 for (ModuleManager::ModuleMap::const_iterator i = modlist.begin(); i != modlist.end(); ++i)
47 Module* const mod = i->second;
48 // 3.0 advertises its settings for the benefit of services
49 // 2.0 would bork on this
50 if (proto_version < PROTO_INSPIRCD_30 && mod->ModuleSourceFile == "m_kicknorejoin.so")
53 bool do_compat_include = false;
54 if (proto_version < PROTO_INSPIRCD_30)
56 for (size_t j = 0; j < sizeof(compatmods)/sizeof(compatmods[0]); j++)
58 if ((compatmods[j].listflag & filter) && (mod->ModuleSourceFile == compatmods[j].name))
60 do_compat_include = true;
66 Version v = mod->GetVersion();
67 if ((!do_compat_include) && (!(v.Flags & filter)))
70 if (i != modlist.begin())
71 capabilities.push_back(' ');
72 capabilities.append(i->first);
73 if (!v.link_data.empty())
75 capabilities.push_back('=');
76 capabilities.append(v.link_data);
80 // If we are linked in a 2.0 server and have an ascii casemapping
81 // advertise it as m_ascii.so from inspircd-extras
82 if ((filter & VF_COMMON) && ServerInstance->Config->CaseMapping == "ascii" && proto_version == PROTO_INSPIRCD_20)
84 if (!capabilities.empty())
85 capabilities += "m_ascii.so";
91 std::string TreeSocket::BuildModeList(ModeType mtype)
93 std::vector<std::string> modes;
94 const ModeParser::ModeHandlerMap& mhs = ServerInstance->Modes.GetModes(mtype);
95 for (ModeParser::ModeHandlerMap::const_iterator i = mhs.begin(); i != mhs.end(); ++i)
97 const ModeHandler* const mh = i->second;
98 const PrefixMode* const pm = mh->IsPrefixMode();
100 if (proto_version >= PROTO_INSPIRCD_30)
103 mdesc.append("prefix:").append(ConvToStr(pm->GetPrefixRank())).push_back(':');
104 else if (mh->IsListMode())
105 mdesc.append("list:");
106 else if (mh->NeedsParam(true))
107 mdesc.append(mh->NeedsParam(false) ? "param:" : "param-set:");
109 mdesc.append("simple:");
111 mdesc.append(mh->name);
112 mdesc.push_back('=');
116 mdesc.push_back(pm->GetPrefix());
118 mdesc.push_back(mh->GetModeChar());
119 modes.push_back(mdesc);
121 std::sort(modes.begin(), modes.end());
122 return stdalgo::string::join(modes);
125 void TreeSocket::SendCapabilities(int phase)
127 if (capab->capab_phase >= phase)
130 if (capab->capab_phase < 1 && phase >= 1)
131 WriteLine("CAPAB START " + ConvToStr(PROTO_NEWEST));
133 capab->capab_phase = phase;
137 const char sep = ' ';
138 irc::sepstream modulelist(MyModules(VF_COMMON), sep);
139 irc::sepstream optmodulelist(MyModules(VF_OPTCOMMON), sep);
140 /* Send module names, split at 509 length */
142 std::string line = "CAPAB MODULES :";
143 while (modulelist.GetToken(item))
145 if (line.length() + item.length() + 1 > 509)
147 this->WriteLine(line);
148 line = "CAPAB MODULES :";
151 if (line != "CAPAB MODULES :")
156 if (line != "CAPAB MODULES :")
157 this->WriteLine(line);
159 line = "CAPAB MODSUPPORT :";
160 while (optmodulelist.GetToken(item))
162 if (line.length() + item.length() + 1 > 509)
164 this->WriteLine(line);
165 line = "CAPAB MODSUPPORT :";
168 if (line != "CAPAB MODSUPPORT :")
173 if (line != "CAPAB MODSUPPORT :")
174 this->WriteLine(line);
176 WriteLine("CAPAB CHANMODES :" + BuildModeList(MODETYPE_CHANNEL));
177 WriteLine("CAPAB USERMODES :" + BuildModeList(MODETYPE_USER));
180 /* Do we have sha256 available? If so, we send a challenge */
181 if (ServerInstance->Modules->FindService(SERVICE_DATA, "hash/sha256"))
183 SetOurChallenge(ServerInstance->GenRandomStr(20));
184 extra = " CHALLENGE=" + this->GetOurChallenge();
187 // 2.0 needs these keys.
188 if (proto_version == PROTO_INSPIRCD_20)
190 extra.append(" PROTOCOL="+ConvToStr(proto_version))
191 .append(" MAXGECOS="+ConvToStr(ServerInstance->Config->Limits.MaxReal))
192 .append(" CHANMODES="+ServerInstance->Modes->GiveModeList(MODETYPE_CHANNEL))
193 .append(" USERMODES="+ServerInstance->Modes->GiveModeList(MODETYPE_USER))
194 .append(" PREFIX="+ ServerInstance->Modes->BuildPrefixes());
197 this->WriteLine("CAPAB CAPABILITIES " /* Preprocessor does this one. */
198 ":NICKMAX="+ConvToStr(ServerInstance->Config->Limits.NickMax)+
199 " CHANMAX="+ConvToStr(ServerInstance->Config->Limits.ChanMax)+
200 " MAXMODES="+ConvToStr(ServerInstance->Config->Limits.MaxModes)+
201 " IDENTMAX="+ConvToStr(ServerInstance->Config->Limits.IdentMax)+
202 " MAXQUIT="+ConvToStr(ServerInstance->Config->Limits.MaxQuit)+
203 " MAXTOPIC="+ConvToStr(ServerInstance->Config->Limits.MaxTopic)+
204 " MAXKICK="+ConvToStr(ServerInstance->Config->Limits.MaxKick)+
205 " MAXREAL="+ConvToStr(ServerInstance->Config->Limits.MaxReal)+
206 " MAXAWAY="+ConvToStr(ServerInstance->Config->Limits.MaxAway)+
207 " MAXHOST="+ConvToStr(ServerInstance->Config->Limits.MaxHost)+
209 " CASEMAPPING="+ServerInstance->Config->CaseMapping+
210 // XXX: Advertise the presence or absence of m_globops in CAPAB CAPABILITIES.
211 // Services want to know about it, and since m_globops was not marked as VF_(OPT)COMMON
212 // in 2.0, we advertise it here to not break linking to previous versions.
213 // Protocol version 1201 (1.2) does not have this issue because we advertise m_globops
214 // to 1201 protocol servers irrespectively of its module flags.
215 (ServerInstance->Modules->Find("m_globops.so") != NULL ? " GLOBOPS=1" : " GLOBOPS=0")
218 this->WriteLine("CAPAB END");
221 /* Isolate and return the elements that are different between two comma seperated lists */
222 void TreeSocket::ListDifference(const std::string &one, const std::string &two, char sep,
223 std::string& mleft, std::string& mright)
225 std::set<std::string> values;
226 irc::sepstream sepleft(one, sep);
227 irc::sepstream sepright(two, sep);
229 while (sepleft.GetToken(item))
233 while (sepright.GetToken(item))
235 if (!values.erase(item))
237 mright.push_back(sep);
241 for(std::set<std::string>::iterator i = values.begin(); i != values.end(); ++i)
243 mleft.push_back(sep);
248 bool TreeSocket::Capab(const CommandBase::Params& params)
250 if (params.size() < 1)
252 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
255 if (params[0] == "START")
257 capab->ModuleList.clear();
258 capab->OptModuleList.clear();
259 capab->CapKeys.clear();
260 if (params.size() > 1)
261 proto_version = ConvToNum<unsigned int>(params[1]);
263 if (proto_version < PROTO_OLDEST)
265 SendError("CAPAB negotiation failed: Server is using protocol version "
266 + (proto_version ? ConvToStr(proto_version) : "1201 or older")
267 + " which is too old to link with this server (protocol versions "
268 + ConvToStr(PROTO_OLDEST) + " to " + ConvToStr(PROTO_NEWEST) + " are supported)");
272 // We don't support the 2.1 protocol.
273 if (proto_version == PROTO_INSPIRCD_21_A0 || proto_version == PROTO_INSPIRCD_21_B2)
275 SendError("CAPAB negotiation failed: InspIRCd 2.1 beta is not supported");
281 else if (params[0] == "END")
284 /* Compare ModuleList and check CapKeys */
285 if ((this->capab->ModuleList != this->MyModules(VF_COMMON)) && (this->capab->ModuleList.length()))
287 std::string diffIneed, diffUneed;
288 ListDifference(this->capab->ModuleList, this->MyModules(VF_COMMON), ' ', diffIneed, diffUneed);
289 if (diffIneed.length() || diffUneed.length())
291 reason = "Modules incorrectly 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 this->SendError("CAPAB negotiation failed: "+reason);
301 if (this->capab->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->capab->OptModuleList.length())
303 std::string diffIneed, diffUneed;
304 ListDifference(this->capab->OptModuleList, this->MyModules(VF_OPTCOMMON), ' ', diffIneed, diffUneed);
305 if (diffIneed.length() || diffUneed.length())
307 if (Utils->AllowOptCommon)
309 ServerInstance->SNO->WriteToSnoMask('l',
310 "Optional module lists do not match, some commands may not work globally.%s%s%s%s",
311 diffIneed.length() ? " Not loaded here:" : "", diffIneed.c_str(),
312 diffUneed.length() ? " Not loaded there:" : "", diffUneed.c_str());
316 reason = "Optional modules incorrectly matched on these servers and <options:allowmismatch> is not enabled.";
317 if (diffIneed.length())
318 reason += " Not loaded here:" + diffIneed;
319 if (diffUneed.length())
320 reason += " Not loaded there:" + diffUneed;
321 this->SendError("CAPAB negotiation failed: "+reason);
327 if (!capab->ChanModes.empty())
329 if (capab->ChanModes != BuildModeList(MODETYPE_CHANNEL))
331 std::string diffIneed, diffUneed;
332 ListDifference(capab->ChanModes, BuildModeList(MODETYPE_CHANNEL), ' ', diffIneed, diffUneed);
333 if (diffIneed.length() || diffUneed.length())
335 reason = "Channel modes not matched on these servers.";
336 if (diffIneed.length())
337 reason += " Not loaded here:" + diffIneed;
338 if (diffUneed.length())
339 reason += " Not loaded there:" + diffUneed;
343 else if (proto_version == PROTO_INSPIRCD_20)
345 if (this->capab->CapKeys.find("CHANMODES") != this->capab->CapKeys.end())
347 if (this->capab->CapKeys.find("CHANMODES")->second != ServerInstance->Modes->GiveModeList(MODETYPE_CHANNEL))
348 reason = "One or more of the channel modes on the remote server are invalid on this server.";
351 else if (this->capab->CapKeys.find("PREFIX") != this->capab->CapKeys.end())
353 if (this->capab->CapKeys.find("PREFIX")->second != ServerInstance->Modes->BuildPrefixes())
354 reason = "One or more of the prefixes on the remote server are invalid on this server.";
360 this->SendError("CAPAB negotiation failed: " + reason);
364 if (!capab->UserModes.empty())
366 if (capab->UserModes != BuildModeList(MODETYPE_USER))
368 std::string diffIneed, diffUneed;
369 ListDifference(capab->UserModes, BuildModeList(MODETYPE_USER), ' ', diffIneed, diffUneed);
370 if (diffIneed.length() || diffUneed.length())
372 reason = "User modes not matched on these servers.";
373 if (diffIneed.length())
374 reason += " Not loaded here:" + diffIneed;
375 if (diffUneed.length())
376 reason += " Not loaded there:" + diffUneed;
380 else if (proto_version == PROTO_INSPIRCD_20 && this->capab->CapKeys.find("USERMODES") != this->capab->CapKeys.end())
382 if (this->capab->CapKeys.find("USERMODES")->second != ServerInstance->Modes->GiveModeList(MODETYPE_USER))
383 reason = "One or more of the user modes on the remote server are invalid on this server.";
388 this->SendError("CAPAB negotiation failed: " + reason);
392 if (this->capab->CapKeys.find("CASEMAPPING") != this->capab->CapKeys.end())
394 const std::string casemapping = this->capab->CapKeys.find("CASEMAPPING")->second;
395 if (casemapping != ServerInstance->Config->CaseMapping)
397 reason = "The casemapping of the remote server differs to that of the local server."
398 " Local casemapping: " + ServerInstance->Config->CaseMapping +
399 " Remote casemapping: " + casemapping;
400 this->SendError("CAPAB negotiation failed: " + reason);
405 /* Challenge response, store their challenge for our password */
406 std::map<std::string,std::string>::iterator n = this->capab->CapKeys.find("CHALLENGE");
407 if ((n != this->capab->CapKeys.end()) && (ServerInstance->Modules->FindService(SERVICE_DATA, "hash/sha256")))
409 /* Challenge-response is on now */
410 this->SetTheirChallenge(n->second);
411 if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
413 this->SendCapabilities(2);
414 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+this->MakePass(capab->link->SendPass, capab->theirchallenge)+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
419 // They didn't specify a challenge or we don't have sha256, we use plaintext
420 if (this->LinkState == CONNECTING)
422 this->SendCapabilities(2);
423 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+capab->link->SendPass+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
427 else if ((params[0] == "MODULES") && (params.size() == 2))
429 if (!capab->ModuleList.length())
431 capab->ModuleList = params[1];
435 capab->ModuleList.push_back(' ');
436 capab->ModuleList.append(params[1]);
439 else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
441 if (!capab->OptModuleList.length())
443 capab->OptModuleList = params[1];
447 capab->OptModuleList.push_back(' ');
448 capab->OptModuleList.append(params[1]);
451 else if ((params[0] == "CHANMODES") && (params.size() == 2))
453 capab->ChanModes = params[1];
455 else if ((params[0] == "USERMODES") && (params.size() == 2))
457 capab->UserModes = params[1];
459 else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
461 irc::spacesepstream capabs(params[1]);
463 while (capabs.GetToken(item))
465 /* Process each key/value pair */
466 std::string::size_type equals = item.find('=');
467 if (equals != std::string::npos)
469 std::string var(item, 0, equals);
470 std::string value(item, equals+1);
471 capab->CapKeys[var] = value;