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 < 1205 && i->second->ModuleSourceFile == "m_kicknorejoin.so")
53 bool do_compat_include = false;
54 if (proto_version < 1205)
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 = i->second->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);
82 std::string TreeSocket::BuildModeList(ModeType mtype)
84 std::vector<std::string> modes;
85 const ModeParser::ModeHandlerMap& mhs = ServerInstance->Modes.GetModes(mtype);
86 for (ModeParser::ModeHandlerMap::const_iterator i = mhs.begin(); i != mhs.end(); ++i)
88 const ModeHandler* const mh = i->second;
89 const PrefixMode* const pm = mh->IsPrefixMode();
91 if (proto_version != 1202)
94 mdesc.append("prefix:").append(ConvToStr(pm->GetPrefixRank())).push_back(':');
95 else if (mh->IsListMode())
96 mdesc.append("list:");
97 else if (mh->NeedsParam(true))
98 mdesc.append(mh->NeedsParam(false) ? "param:" : "param-set:");
100 mdesc.append("simple:");
102 mdesc.append(mh->name);
103 mdesc.push_back('=');
107 mdesc.push_back(pm->GetPrefix());
109 mdesc.push_back(mh->GetModeChar());
110 modes.push_back(mdesc);
112 std::sort(modes.begin(), modes.end());
113 return stdalgo::string::join(modes);
116 void TreeSocket::SendCapabilities(int phase)
118 if (capab->capab_phase >= phase)
121 if (capab->capab_phase < 1 && phase >= 1)
122 WriteLine("CAPAB START " + ConvToStr(ProtocolVersion));
124 capab->capab_phase = phase;
128 const char sep = ' ';
129 irc::sepstream modulelist(MyModules(VF_COMMON), sep);
130 irc::sepstream optmodulelist(MyModules(VF_OPTCOMMON), sep);
131 /* Send module names, split at 509 length */
133 std::string line = "CAPAB MODULES :";
134 while (modulelist.GetToken(item))
136 if (line.length() + item.length() + 1 > 509)
138 this->WriteLine(line);
139 line = "CAPAB MODULES :";
142 if (line != "CAPAB MODULES :")
147 if (line != "CAPAB MODULES :")
148 this->WriteLine(line);
150 line = "CAPAB MODSUPPORT :";
151 while (optmodulelist.GetToken(item))
153 if (line.length() + item.length() + 1 > 509)
155 this->WriteLine(line);
156 line = "CAPAB MODSUPPORT :";
159 if (line != "CAPAB MODSUPPORT :")
164 if (line != "CAPAB MODSUPPORT :")
165 this->WriteLine(line);
167 WriteLine("CAPAB CHANMODES :" + BuildModeList(MODETYPE_CHANNEL));
168 WriteLine("CAPAB USERMODES :" + BuildModeList(MODETYPE_USER));
171 /* Do we have sha256 available? If so, we send a challenge */
172 if (ServerInstance->Modules->FindService(SERVICE_DATA, "hash/sha256"))
174 SetOurChallenge(ServerInstance->GenRandomStr(20));
175 extra = " CHALLENGE=" + this->GetOurChallenge();
178 // 2.0 needs these keys.
179 if (proto_version == 1202)
181 extra.append(" PROTOCOL="+ConvToStr(ProtocolVersion))
182 .append(" MAXGECOS="+ConvToStr(ServerInstance->Config->Limits.MaxReal))
183 .append(" CHANMODES="+ServerInstance->Modes->GiveModeList(MODETYPE_CHANNEL))
184 .append(" USERMODES="+ServerInstance->Modes->GiveModeList(MODETYPE_USER))
185 .append(" PREFIX="+ ServerInstance->Modes->BuildPrefixes());
188 this->WriteLine("CAPAB CAPABILITIES " /* Preprocessor does this one. */
189 ":NICKMAX="+ConvToStr(ServerInstance->Config->Limits.NickMax)+
190 " CHANMAX="+ConvToStr(ServerInstance->Config->Limits.ChanMax)+
191 " MAXMODES="+ConvToStr(ServerInstance->Config->Limits.MaxModes)+
192 " IDENTMAX="+ConvToStr(ServerInstance->Config->Limits.IdentMax)+
193 " MAXQUIT="+ConvToStr(ServerInstance->Config->Limits.MaxQuit)+
194 " MAXTOPIC="+ConvToStr(ServerInstance->Config->Limits.MaxTopic)+
195 " MAXKICK="+ConvToStr(ServerInstance->Config->Limits.MaxKick)+
196 " MAXREAL="+ConvToStr(ServerInstance->Config->Limits.MaxReal)+
197 " MAXAWAY="+ConvToStr(ServerInstance->Config->Limits.MaxAway)+
198 " MAXHOST="+ConvToStr(ServerInstance->Config->Limits.MaxHost)+
200 " CASEMAPPING="+ServerInstance->Config->CaseMapping+
201 // XXX: Advertise the presence or absence of m_globops in CAPAB CAPABILITIES.
202 // Services want to know about it, and since m_globops was not marked as VF_(OPT)COMMON
203 // in 2.0, we advertise it here to not break linking to previous versions.
204 // Protocol version 1201 (1.2) does not have this issue because we advertise m_globops
205 // to 1201 protocol servers irrespectively of its module flags.
206 (ServerInstance->Modules->Find("m_globops.so") != NULL ? " GLOBOPS=1" : " GLOBOPS=0")
209 this->WriteLine("CAPAB END");
212 /* Isolate and return the elements that are different between two comma seperated lists */
213 void TreeSocket::ListDifference(const std::string &one, const std::string &two, char sep,
214 std::string& mleft, std::string& mright)
216 std::set<std::string> values;
217 irc::sepstream sepleft(one, sep);
218 irc::sepstream sepright(two, sep);
220 while (sepleft.GetToken(item))
224 while (sepright.GetToken(item))
226 if (!values.erase(item))
228 mright.push_back(sep);
232 for(std::set<std::string>::iterator i = values.begin(); i != values.end(); ++i)
234 mleft.push_back(sep);
239 bool TreeSocket::Capab(const CommandBase::Params& params)
241 if (params.size() < 1)
243 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
246 if (params[0] == "START")
248 capab->ModuleList.clear();
249 capab->OptModuleList.clear();
250 capab->CapKeys.clear();
251 if (params.size() > 1)
252 proto_version = ConvToNum<unsigned int>(params[1]);
254 if (proto_version < MinCompatProtocol)
256 SendError("CAPAB negotiation failed: Server is using protocol version " + (proto_version ? ConvToStr(proto_version) : "1201 or older")
257 + " which is too old to link with this server (version " + ConvToStr(ProtocolVersion)
258 + (ProtocolVersion != MinCompatProtocol ? ", links with " + ConvToStr(MinCompatProtocol) + " and above)" : ")"));
262 // Special case, may be removed in the future
263 if (proto_version == 1203 || proto_version == 1204)
265 SendError("CAPAB negotiation failed: InspIRCd 2.1 beta is not supported");
271 else if (params[0] == "END")
274 /* Compare ModuleList and check CapKeys */
275 if ((this->capab->ModuleList != this->MyModules(VF_COMMON)) && (this->capab->ModuleList.length()))
277 std::string diffIneed, diffUneed;
278 ListDifference(this->capab->ModuleList, this->MyModules(VF_COMMON), ' ', diffIneed, diffUneed);
279 if (diffIneed.length() || diffUneed.length())
281 reason = "Modules incorrectly matched on these servers.";
282 if (diffIneed.length())
283 reason += " Not loaded here:" + diffIneed;
284 if (diffUneed.length())
285 reason += " Not loaded there:" + diffUneed;
286 this->SendError("CAPAB negotiation failed: "+reason);
291 if (this->capab->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->capab->OptModuleList.length())
293 std::string diffIneed, diffUneed;
294 ListDifference(this->capab->OptModuleList, this->MyModules(VF_OPTCOMMON), ' ', diffIneed, diffUneed);
295 if (diffIneed.length() || diffUneed.length())
297 if (Utils->AllowOptCommon)
299 ServerInstance->SNO->WriteToSnoMask('l',
300 "Optional module lists do not match, some commands may not work globally.%s%s%s%s",
301 diffIneed.length() ? " Not loaded here:" : "", diffIneed.c_str(),
302 diffUneed.length() ? " Not loaded there:" : "", diffUneed.c_str());
306 reason = "Optional modules incorrectly matched on these servers and <options:allowmismatch> is not enabled.";
307 if (diffIneed.length())
308 reason += " Not loaded here:" + diffIneed;
309 if (diffUneed.length())
310 reason += " Not loaded there:" + diffUneed;
311 this->SendError("CAPAB negotiation failed: "+reason);
317 if (!capab->ChanModes.empty())
319 if (capab->ChanModes != BuildModeList(MODETYPE_CHANNEL))
321 std::string diffIneed, diffUneed;
322 ListDifference(capab->ChanModes, BuildModeList(MODETYPE_CHANNEL), ' ', diffIneed, diffUneed);
323 if (diffIneed.length() || diffUneed.length())
325 reason = "Channel modes not matched on these servers.";
326 if (diffIneed.length())
327 reason += " Not loaded here:" + diffIneed;
328 if (diffUneed.length())
329 reason += " Not loaded there:" + diffUneed;
333 else if (proto_version == 1202)
335 if (this->capab->CapKeys.find("CHANMODES") != this->capab->CapKeys.end())
337 if (this->capab->CapKeys.find("CHANMODES")->second != ServerInstance->Modes->GiveModeList(MODETYPE_CHANNEL))
338 reason = "One or more of the channel modes on the remote server are invalid on this server.";
341 else if (this->capab->CapKeys.find("PREFIX") != this->capab->CapKeys.end())
343 if (this->capab->CapKeys.find("PREFIX")->second != ServerInstance->Modes->BuildPrefixes())
344 reason = "One or more of the prefixes on the remote server are invalid on this server.";
350 this->SendError("CAPAB negotiation failed: " + reason);
354 if (!capab->UserModes.empty())
356 if (capab->UserModes != BuildModeList(MODETYPE_USER))
358 std::string diffIneed, diffUneed;
359 ListDifference(capab->UserModes, BuildModeList(MODETYPE_USER), ' ', diffIneed, diffUneed);
360 if (diffIneed.length() || diffUneed.length())
362 reason = "User modes not matched on these servers.";
363 if (diffIneed.length())
364 reason += " Not loaded here:" + diffIneed;
365 if (diffUneed.length())
366 reason += " Not loaded there:" + diffUneed;
370 else if (proto_version == 1202 && this->capab->CapKeys.find("USERMODES") != this->capab->CapKeys.end())
372 if (this->capab->CapKeys.find("USERMODES")->second != ServerInstance->Modes->GiveModeList(MODETYPE_USER))
373 reason = "One or more of the user modes on the remote server are invalid on this server.";
378 this->SendError("CAPAB negotiation failed: " + reason);
382 if (this->capab->CapKeys.find("CASEMAPPING") != this->capab->CapKeys.end())
384 const std::string casemapping = this->capab->CapKeys.find("CASEMAPPING")->second;
385 if (casemapping != ServerInstance->Config->CaseMapping)
387 reason = "The casemapping of the remote server differs to that of the local server."
388 " Local casemapping: " + ServerInstance->Config->CaseMapping +
389 " Remote casemapping: " + casemapping;
390 this->SendError("CAPAB negotiation failed: " + reason);
395 /* Challenge response, store their challenge for our password */
396 std::map<std::string,std::string>::iterator n = this->capab->CapKeys.find("CHALLENGE");
397 if ((n != this->capab->CapKeys.end()) && (ServerInstance->Modules->FindService(SERVICE_DATA, "hash/sha256")))
399 /* Challenge-response is on now */
400 this->SetTheirChallenge(n->second);
401 if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
403 this->SendCapabilities(2);
404 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+this->MakePass(capab->link->SendPass, capab->theirchallenge)+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
409 // They didn't specify a challenge or we don't have sha256, we use plaintext
410 if (this->LinkState == CONNECTING)
412 this->SendCapabilities(2);
413 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+capab->link->SendPass+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
417 else if ((params[0] == "MODULES") && (params.size() == 2))
419 if (!capab->ModuleList.length())
421 capab->ModuleList = params[1];
425 capab->ModuleList.push_back(' ');
426 capab->ModuleList.append(params[1]);
429 else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
431 if (!capab->OptModuleList.length())
433 capab->OptModuleList = params[1];
437 capab->OptModuleList.push_back(' ');
438 capab->OptModuleList.append(params[1]);
441 else if ((params[0] == "CHANMODES") && (params.size() == 2))
443 capab->ChanModes = params[1];
445 else if ((params[0] == "USERMODES") && (params.size() == 2))
447 capab->UserModes = params[1];
449 else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
451 irc::spacesepstream capabs(params[1]);
453 while (capabs.GetToken(item))
455 /* Process each key/value pair */
456 std::string::size_type equals = item.find('=');
457 if (equals != std::string::npos)
459 std::string var(item, 0, equals);
460 std::string value(item, equals+1);
461 capab->CapKeys[var] = value;