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 static std::string BuildModeList(ModeType type)
84 std::vector<std::string> modes;
85 const ModeParser::ModeHandlerMap& mhs = ServerInstance->Modes.GetModes(type);
86 for (ModeParser::ModeHandlerMap::const_iterator i = mhs.begin(); i != mhs.end(); ++i)
88 const ModeHandler* const mh = i->second;
89 std::string mdesc = mh->name;
91 const PrefixMode* const pm = mh->IsPrefixMode();
95 mdesc.push_back(pm->GetPrefix());
97 mdesc.push_back(mh->GetModeChar());
98 modes.push_back(mdesc);
100 std::sort(modes.begin(), modes.end());
101 return stdalgo::string::join(modes);
104 void TreeSocket::SendCapabilities(int phase)
106 if (capab->capab_phase >= phase)
109 if (capab->capab_phase < 1 && phase >= 1)
110 WriteLine("CAPAB START " + ConvToStr(ProtocolVersion));
112 capab->capab_phase = phase;
116 const char sep = ' ';
117 irc::sepstream modulelist(MyModules(VF_COMMON), sep);
118 irc::sepstream optmodulelist(MyModules(VF_OPTCOMMON), sep);
119 /* Send module names, split at 509 length */
121 std::string line = "CAPAB MODULES :";
122 while (modulelist.GetToken(item))
124 if (line.length() + item.length() + 1 > 509)
126 this->WriteLine(line);
127 line = "CAPAB MODULES :";
130 if (line != "CAPAB MODULES :")
135 if (line != "CAPAB MODULES :")
136 this->WriteLine(line);
138 line = "CAPAB MODSUPPORT :";
139 while (optmodulelist.GetToken(item))
141 if (line.length() + item.length() + 1 > 509)
143 this->WriteLine(line);
144 line = "CAPAB MODSUPPORT :";
147 if (line != "CAPAB MODSUPPORT :")
152 if (line != "CAPAB MODSUPPORT :")
153 this->WriteLine(line);
155 WriteLine("CAPAB CHANMODES :" + BuildModeList(MODETYPE_CHANNEL));
156 WriteLine("CAPAB USERMODES :" + BuildModeList(MODETYPE_USER));
159 /* Do we have sha256 available? If so, we send a challenge */
160 if (ServerInstance->Modules->FindService(SERVICE_DATA, "hash/sha256"))
162 SetOurChallenge(ServerInstance->GenRandomStr(20));
163 extra = " CHALLENGE=" + this->GetOurChallenge();
166 // 2.0 needs these keys.
167 if (proto_version == 1202)
169 extra.append(" PROTOCOL="+ConvToStr(ProtocolVersion));
170 extra.append(" MAXGECOS="+ConvToStr(ServerInstance->Config->Limits.MaxReal));
173 this->WriteLine("CAPAB CAPABILITIES " /* Preprocessor does this one. */
174 ":NICKMAX="+ConvToStr(ServerInstance->Config->Limits.NickMax)+
175 " CHANMAX="+ConvToStr(ServerInstance->Config->Limits.ChanMax)+
176 " MAXMODES="+ConvToStr(ServerInstance->Config->Limits.MaxModes)+
177 " IDENTMAX="+ConvToStr(ServerInstance->Config->Limits.IdentMax)+
178 " MAXQUIT="+ConvToStr(ServerInstance->Config->Limits.MaxQuit)+
179 " MAXTOPIC="+ConvToStr(ServerInstance->Config->Limits.MaxTopic)+
180 " MAXKICK="+ConvToStr(ServerInstance->Config->Limits.MaxKick)+
181 " MAXREAL="+ConvToStr(ServerInstance->Config->Limits.MaxReal)+
182 " MAXAWAY="+ConvToStr(ServerInstance->Config->Limits.MaxAway)+
183 " MAXHOST="+ConvToStr(ServerInstance->Config->Limits.MaxHost)+
185 " PREFIX="+ServerInstance->Modes->BuildPrefixes()+
186 " CHANMODES="+ServerInstance->Modes->GiveModeList(MODETYPE_CHANNEL)+
187 " USERMODES="+ServerInstance->Modes->GiveModeList(MODETYPE_USER)+
188 " CASEMAPPING="+ServerInstance->Config->CaseMapping+
189 // XXX: Advertise the presence or absence of m_globops in CAPAB CAPABILITIES.
190 // Services want to know about it, and since m_globops was not marked as VF_(OPT)COMMON
191 // in 2.0, we advertise it here to not break linking to previous versions.
192 // Protocol version 1201 (1.2) does not have this issue because we advertise m_globops
193 // to 1201 protocol servers irrespectively of its module flags.
194 (ServerInstance->Modules->Find("m_globops.so") != NULL ? " GLOBOPS=1" : " GLOBOPS=0")
197 this->WriteLine("CAPAB END");
200 /* Isolate and return the elements that are different between two comma seperated lists */
201 void TreeSocket::ListDifference(const std::string &one, const std::string &two, char sep,
202 std::string& mleft, std::string& mright)
204 std::set<std::string> values;
205 irc::sepstream sepleft(one, sep);
206 irc::sepstream sepright(two, sep);
208 while (sepleft.GetToken(item))
212 while (sepright.GetToken(item))
214 if (!values.erase(item))
216 mright.push_back(sep);
220 for(std::set<std::string>::iterator i = values.begin(); i != values.end(); ++i)
222 mleft.push_back(sep);
227 bool TreeSocket::Capab(const CommandBase::Params& params)
229 if (params.size() < 1)
231 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
234 if (params[0] == "START")
236 capab->ModuleList.clear();
237 capab->OptModuleList.clear();
238 capab->CapKeys.clear();
239 if (params.size() > 1)
240 proto_version = ConvToInt(params[1]);
242 if (proto_version < MinCompatProtocol)
244 SendError("CAPAB negotiation failed: Server is using protocol version " + (proto_version ? ConvToStr(proto_version) : "1201 or older")
245 + " which is too old to link with this server (version " + ConvToStr(ProtocolVersion)
246 + (ProtocolVersion != MinCompatProtocol ? ", links with " + ConvToStr(MinCompatProtocol) + " and above)" : ")"));
250 // Special case, may be removed in the future
251 if (proto_version == 1203 || proto_version == 1204)
253 SendError("CAPAB negotiation failed: InspIRCd 2.1 beta is not supported");
259 else if (params[0] == "END")
262 /* Compare ModuleList and check CapKeys */
263 if ((this->capab->ModuleList != this->MyModules(VF_COMMON)) && (this->capab->ModuleList.length()))
265 std::string diffIneed, diffUneed;
266 ListDifference(this->capab->ModuleList, this->MyModules(VF_COMMON), ' ', diffIneed, diffUneed);
267 if (diffIneed.length() || diffUneed.length())
269 reason = "Modules incorrectly 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 this->SendError("CAPAB negotiation failed: "+reason);
278 if (this->capab->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->capab->OptModuleList.length())
280 std::string diffIneed, diffUneed;
281 ListDifference(this->capab->OptModuleList, this->MyModules(VF_OPTCOMMON), ' ', diffIneed, diffUneed);
282 if (diffIneed.length() || diffUneed.length())
284 if (Utils->AllowOptCommon)
286 ServerInstance->SNO->WriteToSnoMask('l',
287 "Optional module lists do not match, some commands may not work globally.%s%s%s%s",
288 diffIneed.length() ? " Not loaded here:" : "", diffIneed.c_str(),
289 diffUneed.length() ? " Not loaded there:" : "", diffUneed.c_str());
293 reason = "Optional modules incorrectly matched on these servers, and options::allowmismatch not set.";
294 if (diffIneed.length())
295 reason += " Not loaded here:" + diffIneed;
296 if (diffUneed.length())
297 reason += " Not loaded there:" + diffUneed;
298 this->SendError("CAPAB negotiation failed: "+reason);
304 if(this->capab->CapKeys.find("PREFIX") != this->capab->CapKeys.end() && this->capab->CapKeys.find("PREFIX")->second != ServerInstance->Modes->BuildPrefixes())
305 reason = "One or more of the prefixes on the remote server are invalid on this server.";
307 if (!capab->ChanModes.empty())
309 if (capab->ChanModes != BuildModeList(MODETYPE_CHANNEL))
311 std::string diffIneed, diffUneed;
312 ListDifference(capab->ChanModes, BuildModeList(MODETYPE_CHANNEL), ' ', diffIneed, diffUneed);
313 if (diffIneed.length() || diffUneed.length())
315 reason = "Channel modes not matched on these servers.";
316 if (diffIneed.length())
317 reason += " Not loaded here:" + diffIneed;
318 if (diffUneed.length())
319 reason += " Not loaded there:" + diffUneed;
323 else if (this->capab->CapKeys.find("CHANMODES") != this->capab->CapKeys.end())
325 if (this->capab->CapKeys.find("CHANMODES")->second != ServerInstance->Modes->GiveModeList(MODETYPE_CHANNEL))
326 reason = "One or more of the channel modes on the remote server are invalid on this server.";
329 if (!capab->UserModes.empty())
331 if (capab->UserModes != BuildModeList(MODETYPE_USER))
333 std::string diffIneed, diffUneed;
334 ListDifference(capab->UserModes, BuildModeList(MODETYPE_USER), ' ', diffIneed, diffUneed);
335 if (diffIneed.length() || diffUneed.length())
337 reason = "User modes not matched on these servers.";
338 if (diffIneed.length())
339 reason += " Not loaded here:" + diffIneed;
340 if (diffUneed.length())
341 reason += " Not loaded there:" + diffUneed;
345 else if (this->capab->CapKeys.find("USERMODES") != this->capab->CapKeys.end())
347 if (this->capab->CapKeys.find("USERMODES")->second != ServerInstance->Modes->GiveModeList(MODETYPE_USER))
348 reason = "One or more of the user modes on the remote server are invalid on this server.";
352 // We default to rfc1459 here because if this key is not sent then
353 // the remote server is running the 2.0 protocol which uses rfc1459
355 std::string casemapping = "rfc1459";
356 std::map<std::string, std::string>::iterator citer = this->capab->CapKeys.find("CASEMAPPING");
357 if (citer != this->capab->CapKeys.end())
358 casemapping = citer->second;
360 if (casemapping != ServerInstance->Config->CaseMapping)
362 reason = "The casemapping of the remote server differs to that of the local server."
363 " Local casemapping: " + ServerInstance->Config->CaseMapping +
364 " Remote casemapping: " + casemapping;
369 /* Challenge response, store their challenge for our password */
370 std::map<std::string,std::string>::iterator n = this->capab->CapKeys.find("CHALLENGE");
371 if ((n != this->capab->CapKeys.end()) && (ServerInstance->Modules->FindService(SERVICE_DATA, "hash/sha256")))
373 /* Challenge-response is on now */
374 this->SetTheirChallenge(n->second);
375 if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
377 this->SendCapabilities(2);
378 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+this->MakePass(capab->link->SendPass, capab->theirchallenge)+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
383 // They didn't specify a challenge or we don't have sha256, we use plaintext
384 if (this->LinkState == CONNECTING)
386 this->SendCapabilities(2);
387 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+capab->link->SendPass+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
393 this->SendError("CAPAB negotiation failed: "+reason);
397 else if ((params[0] == "MODULES") && (params.size() == 2))
399 if (!capab->ModuleList.length())
401 capab->ModuleList = params[1];
405 capab->ModuleList.push_back(' ');
406 capab->ModuleList.append(params[1]);
409 else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
411 if (!capab->OptModuleList.length())
413 capab->OptModuleList = params[1];
417 capab->OptModuleList.push_back(' ');
418 capab->OptModuleList.append(params[1]);
421 else if ((params[0] == "CHANMODES") && (params.size() == 2))
423 capab->ChanModes = params[1];
425 else if ((params[0] == "USERMODES") && (params.size() == 2))
427 capab->UserModes = params[1];
429 else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
431 irc::tokenstream capabs(params[1]);
433 while (capabs.GetToken(item))
435 /* Process each key/value pair */
436 std::string::size_type equals = item.find('=');
437 if (equals != std::string::npos)
439 std::string var(item, 0, equals);
440 std::string value(item, equals+1);
441 capab->CapKeys[var] = value;