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 irc::stringjoiner(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 this key
167 if (proto_version == 1202)
168 extra.append(" PROTOCOL="+ConvToStr(ProtocolVersion));
170 this->WriteLine("CAPAB CAPABILITIES " /* Preprocessor does this one. */
171 ":NICKMAX="+ConvToStr(ServerInstance->Config->Limits.NickMax)+
172 " CHANMAX="+ConvToStr(ServerInstance->Config->Limits.ChanMax)+
173 " MAXMODES="+ConvToStr(ServerInstance->Config->Limits.MaxModes)+
174 " IDENTMAX="+ConvToStr(ServerInstance->Config->Limits.IdentMax)+
175 " MAXQUIT="+ConvToStr(ServerInstance->Config->Limits.MaxQuit)+
176 " MAXTOPIC="+ConvToStr(ServerInstance->Config->Limits.MaxTopic)+
177 " MAXKICK="+ConvToStr(ServerInstance->Config->Limits.MaxKick)+
178 " MAXGECOS="+ConvToStr(ServerInstance->Config->Limits.MaxGecos)+
179 " MAXAWAY="+ConvToStr(ServerInstance->Config->Limits.MaxAway)+
180 " MAXHOST="+ConvToStr(ServerInstance->Config->Limits.MaxHost)+
182 " PREFIX="+ServerInstance->Modes->BuildPrefixes()+
183 " CHANMODES="+ServerInstance->Modes->GiveModeList(MODETYPE_CHANNEL)+
184 " USERMODES="+ServerInstance->Modes->GiveModeList(MODETYPE_USER)+
185 // XXX: Advertise the presence or absence of m_globops in CAPAB CAPABILITIES.
186 // Services want to know about it, and since m_globops was not marked as VF_(OPT)COMMON
187 // in 2.0, we advertise it here to not break linking to previous versions.
188 // Protocol version 1201 (1.2) does not have this issue because we advertise m_globops
189 // to 1201 protocol servers irrespectively of its module flags.
190 (ServerInstance->Modules->Find("m_globops.so") != NULL ? " GLOBOPS=1" : " GLOBOPS=0")
193 this->WriteLine("CAPAB END");
196 /* Isolate and return the elements that are different between two comma seperated lists */
197 void TreeSocket::ListDifference(const std::string &one, const std::string &two, char sep,
198 std::string& mleft, std::string& mright)
200 std::set<std::string> values;
201 irc::sepstream sepleft(one, sep);
202 irc::sepstream sepright(two, sep);
204 while (sepleft.GetToken(item))
208 while (sepright.GetToken(item))
210 if (!values.erase(item))
212 mright.push_back(sep);
216 for(std::set<std::string>::iterator i = values.begin(); i != values.end(); ++i)
218 mleft.push_back(sep);
223 bool TreeSocket::Capab(const parameterlist ¶ms)
225 if (params.size() < 1)
227 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
230 if (params[0] == "START")
232 capab->ModuleList.clear();
233 capab->OptModuleList.clear();
234 capab->CapKeys.clear();
235 if (params.size() > 1)
236 proto_version = ConvToInt(params[1]);
238 if (proto_version < MinCompatProtocol)
240 SendError("CAPAB negotiation failed: Server is using protocol version " + (proto_version ? ConvToStr(proto_version) : "1201 or older")
241 + " which is too old to link with this server (version " + ConvToStr(ProtocolVersion)
242 + (ProtocolVersion != MinCompatProtocol ? ", links with " + ConvToStr(MinCompatProtocol) + " and above)" : ")"));
246 // Special case, may be removed in the future
247 if (proto_version == 1203 || proto_version == 1204)
249 SendError("CAPAB negotiation failed: InspIRCd 2.1 beta is not supported");
255 else if (params[0] == "END")
258 /* Compare ModuleList and check CapKeys */
259 if ((this->capab->ModuleList != this->MyModules(VF_COMMON)) && (this->capab->ModuleList.length()))
261 std::string diffIneed, diffUneed;
262 ListDifference(this->capab->ModuleList, this->MyModules(VF_COMMON), ' ', diffIneed, diffUneed);
263 if (diffIneed.length() || diffUneed.length())
265 reason = "Modules incorrectly matched on these servers.";
266 if (diffIneed.length())
267 reason += " Not loaded here:" + diffIneed;
268 if (diffUneed.length())
269 reason += " Not loaded there:" + diffUneed;
270 this->SendError("CAPAB negotiation failed: "+reason);
274 if (this->capab->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->capab->OptModuleList.length())
276 std::string diffIneed, diffUneed;
277 ListDifference(this->capab->OptModuleList, this->MyModules(VF_OPTCOMMON), ' ', diffIneed, diffUneed);
278 if (diffIneed.length() || diffUneed.length())
280 if (Utils->AllowOptCommon)
282 ServerInstance->SNO->WriteToSnoMask('l',
283 "Optional module lists do not match, some commands may not work globally.%s%s%s%s",
284 diffIneed.length() ? " Not loaded here:" : "", diffIneed.c_str(),
285 diffUneed.length() ? " Not loaded there:" : "", diffUneed.c_str());
289 reason = "Optional modules incorrectly matched on these servers, and options::allowmismatch not set.";
290 if (diffIneed.length())
291 reason += " Not loaded here:" + diffIneed;
292 if (diffUneed.length())
293 reason += " Not loaded there:" + diffUneed;
294 this->SendError("CAPAB negotiation failed: "+reason);
300 if(this->capab->CapKeys.find("PREFIX") != this->capab->CapKeys.end() && this->capab->CapKeys.find("PREFIX")->second != ServerInstance->Modes->BuildPrefixes())
301 reason = "One or more of the prefixes on the remote server are invalid on this server.";
303 if (!capab->ChanModes.empty())
305 if (capab->ChanModes != BuildModeList(MODETYPE_CHANNEL))
307 std::string diffIneed, diffUneed;
308 ListDifference(capab->ChanModes, BuildModeList(MODETYPE_CHANNEL), ' ', diffIneed, diffUneed);
309 if (diffIneed.length() || diffUneed.length())
311 reason = "Channel modes not matched on these servers.";
312 if (diffIneed.length())
313 reason += " Not loaded here:" + diffIneed;
314 if (diffUneed.length())
315 reason += " Not loaded there:" + diffUneed;
319 else if (this->capab->CapKeys.find("CHANMODES") != this->capab->CapKeys.end())
321 if (this->capab->CapKeys.find("CHANMODES")->second != ServerInstance->Modes->GiveModeList(MODETYPE_CHANNEL))
322 reason = "One or more of the channel modes on the remote server are invalid on this server.";
325 if (!capab->UserModes.empty())
327 if (capab->UserModes != BuildModeList(MODETYPE_USER))
329 std::string diffIneed, diffUneed;
330 ListDifference(capab->UserModes, BuildModeList(MODETYPE_USER), ' ', diffIneed, diffUneed);
331 if (diffIneed.length() || diffUneed.length())
333 reason = "User modes not matched on these servers.";
334 if (diffIneed.length())
335 reason += " Not loaded here:" + diffIneed;
336 if (diffUneed.length())
337 reason += " Not loaded there:" + diffUneed;
341 else if (this->capab->CapKeys.find("USERMODES") != this->capab->CapKeys.end())
343 if (this->capab->CapKeys.find("USERMODES")->second != ServerInstance->Modes->GiveModeList(MODETYPE_USER))
344 reason = "One or more of the user modes on the remote server are invalid on this server.";
347 /* Challenge response, store their challenge for our password */
348 std::map<std::string,std::string>::iterator n = this->capab->CapKeys.find("CHALLENGE");
349 if ((n != this->capab->CapKeys.end()) && (ServerInstance->Modules->FindService(SERVICE_DATA, "hash/sha256")))
351 /* Challenge-response is on now */
352 this->SetTheirChallenge(n->second);
353 if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
355 this->SendCapabilities(2);
356 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+this->MakePass(capab->link->SendPass, capab->theirchallenge)+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
361 // They didn't specify a challenge or we don't have sha256, we use plaintext
362 if (this->LinkState == CONNECTING)
364 this->SendCapabilities(2);
365 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+capab->link->SendPass+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
371 this->SendError("CAPAB negotiation failed: "+reason);
375 else if ((params[0] == "MODULES") && (params.size() == 2))
377 if (!capab->ModuleList.length())
379 capab->ModuleList = params[1];
383 capab->ModuleList.push_back(' ');
384 capab->ModuleList.append(params[1]);
387 else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
389 if (!capab->OptModuleList.length())
391 capab->OptModuleList = params[1];
395 capab->OptModuleList.push_back(' ');
396 capab->OptModuleList.append(params[1]);
399 else if ((params[0] == "CHANMODES") && (params.size() == 2))
401 capab->ChanModes = params[1];
403 else if ((params[0] == "USERMODES") && (params.size() == 2))
405 capab->UserModes = params[1];
407 else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
409 irc::tokenstream capabs(params[1]);
411 while (capabs.GetToken(item))
413 /* Process each key/value pair */
414 std::string::size_type equals = item.find('=');
415 if (equals != std::string::npos)
417 std::string var(item, 0, equals);
418 std::string value(item, equals+1);
419 capab->CapKeys[var] = value;