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 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 " CASEMAPPING="+ServerInstance->Config->CaseMapping+
186 // XXX: Advertise the presence or absence of m_globops in CAPAB CAPABILITIES.
187 // Services want to know about it, and since m_globops was not marked as VF_(OPT)COMMON
188 // in 2.0, we advertise it here to not break linking to previous versions.
189 // Protocol version 1201 (1.2) does not have this issue because we advertise m_globops
190 // to 1201 protocol servers irrespectively of its module flags.
191 (ServerInstance->Modules->Find("m_globops.so") != NULL ? " GLOBOPS=1" : " GLOBOPS=0")
194 this->WriteLine("CAPAB END");
197 /* Isolate and return the elements that are different between two comma seperated lists */
198 void TreeSocket::ListDifference(const std::string &one, const std::string &two, char sep,
199 std::string& mleft, std::string& mright)
201 std::set<std::string> values;
202 irc::sepstream sepleft(one, sep);
203 irc::sepstream sepright(two, sep);
205 while (sepleft.GetToken(item))
209 while (sepright.GetToken(item))
211 if (!values.erase(item))
213 mright.push_back(sep);
217 for(std::set<std::string>::iterator i = values.begin(); i != values.end(); ++i)
219 mleft.push_back(sep);
224 bool TreeSocket::Capab(const CommandBase::Params& params)
226 if (params.size() < 1)
228 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
231 if (params[0] == "START")
233 capab->ModuleList.clear();
234 capab->OptModuleList.clear();
235 capab->CapKeys.clear();
236 if (params.size() > 1)
237 proto_version = ConvToInt(params[1]);
239 if (proto_version < MinCompatProtocol)
241 SendError("CAPAB negotiation failed: Server is using protocol version " + (proto_version ? ConvToStr(proto_version) : "1201 or older")
242 + " which is too old to link with this server (version " + ConvToStr(ProtocolVersion)
243 + (ProtocolVersion != MinCompatProtocol ? ", links with " + ConvToStr(MinCompatProtocol) + " and above)" : ")"));
247 // Special case, may be removed in the future
248 if (proto_version == 1203 || proto_version == 1204)
250 SendError("CAPAB negotiation failed: InspIRCd 2.1 beta is not supported");
256 else if (params[0] == "END")
259 /* Compare ModuleList and check CapKeys */
260 if ((this->capab->ModuleList != this->MyModules(VF_COMMON)) && (this->capab->ModuleList.length()))
262 std::string diffIneed, diffUneed;
263 ListDifference(this->capab->ModuleList, this->MyModules(VF_COMMON), ' ', diffIneed, diffUneed);
264 if (diffIneed.length() || diffUneed.length())
266 reason = "Modules incorrectly matched on these servers.";
267 if (diffIneed.length())
268 reason += " Not loaded here:" + diffIneed;
269 if (diffUneed.length())
270 reason += " Not loaded there:" + diffUneed;
271 this->SendError("CAPAB negotiation failed: "+reason);
275 if (this->capab->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->capab->OptModuleList.length())
277 std::string diffIneed, diffUneed;
278 ListDifference(this->capab->OptModuleList, this->MyModules(VF_OPTCOMMON), ' ', diffIneed, diffUneed);
279 if (diffIneed.length() || diffUneed.length())
281 if (Utils->AllowOptCommon)
283 ServerInstance->SNO->WriteToSnoMask('l',
284 "Optional module lists do not match, some commands may not work globally.%s%s%s%s",
285 diffIneed.length() ? " Not loaded here:" : "", diffIneed.c_str(),
286 diffUneed.length() ? " Not loaded there:" : "", diffUneed.c_str());
290 reason = "Optional modules incorrectly matched on these servers, and options::allowmismatch not set.";
291 if (diffIneed.length())
292 reason += " Not loaded here:" + diffIneed;
293 if (diffUneed.length())
294 reason += " Not loaded there:" + diffUneed;
295 this->SendError("CAPAB negotiation failed: "+reason);
301 if(this->capab->CapKeys.find("PREFIX") != this->capab->CapKeys.end() && this->capab->CapKeys.find("PREFIX")->second != ServerInstance->Modes->BuildPrefixes())
302 reason = "One or more of the prefixes on the remote server are invalid on this server.";
304 if (!capab->ChanModes.empty())
306 if (capab->ChanModes != BuildModeList(MODETYPE_CHANNEL))
308 std::string diffIneed, diffUneed;
309 ListDifference(capab->ChanModes, BuildModeList(MODETYPE_CHANNEL), ' ', diffIneed, diffUneed);
310 if (diffIneed.length() || diffUneed.length())
312 reason = "Channel modes not matched on these servers.";
313 if (diffIneed.length())
314 reason += " Not loaded here:" + diffIneed;
315 if (diffUneed.length())
316 reason += " Not loaded there:" + diffUneed;
320 else if (this->capab->CapKeys.find("CHANMODES") != this->capab->CapKeys.end())
322 if (this->capab->CapKeys.find("CHANMODES")->second != ServerInstance->Modes->GiveModeList(MODETYPE_CHANNEL))
323 reason = "One or more of the channel modes on the remote server are invalid on this server.";
326 if (!capab->UserModes.empty())
328 if (capab->UserModes != BuildModeList(MODETYPE_USER))
330 std::string diffIneed, diffUneed;
331 ListDifference(capab->UserModes, BuildModeList(MODETYPE_USER), ' ', diffIneed, diffUneed);
332 if (diffIneed.length() || diffUneed.length())
334 reason = "User modes not matched on these servers.";
335 if (diffIneed.length())
336 reason += " Not loaded here:" + diffIneed;
337 if (diffUneed.length())
338 reason += " Not loaded there:" + diffUneed;
342 else if (this->capab->CapKeys.find("USERMODES") != this->capab->CapKeys.end())
344 if (this->capab->CapKeys.find("USERMODES")->second != ServerInstance->Modes->GiveModeList(MODETYPE_USER))
345 reason = "One or more of the user modes on the remote server are invalid on this server.";
349 // We default to rfc1459 here because if this key is not sent then
350 // the remote server is running the 2.0 protocol which uses rfc1459
352 std::string casemapping = "rfc1459";
353 std::map<std::string, std::string>::iterator citer = this->capab->CapKeys.find("CASEMAPPING");
354 if (citer != this->capab->CapKeys.end())
355 casemapping = citer->second;
357 if (casemapping != ServerInstance->Config->CaseMapping)
359 reason = "The casemapping of the remote server differs to that of the local server."
360 " Local casemapping: " + ServerInstance->Config->CaseMapping +
361 " Remote casemapping: " + casemapping;
366 /* Challenge response, store their challenge for our password */
367 std::map<std::string,std::string>::iterator n = this->capab->CapKeys.find("CHALLENGE");
368 if ((n != this->capab->CapKeys.end()) && (ServerInstance->Modules->FindService(SERVICE_DATA, "hash/sha256")))
370 /* Challenge-response is on now */
371 this->SetTheirChallenge(n->second);
372 if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
374 this->SendCapabilities(2);
375 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+this->MakePass(capab->link->SendPass, capab->theirchallenge)+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
380 // They didn't specify a challenge or we don't have sha256, we use plaintext
381 if (this->LinkState == CONNECTING)
383 this->SendCapabilities(2);
384 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+capab->link->SendPass+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
390 this->SendError("CAPAB negotiation failed: "+reason);
394 else if ((params[0] == "MODULES") && (params.size() == 2))
396 if (!capab->ModuleList.length())
398 capab->ModuleList = params[1];
402 capab->ModuleList.push_back(' ');
403 capab->ModuleList.append(params[1]);
406 else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
408 if (!capab->OptModuleList.length())
410 capab->OptModuleList = params[1];
414 capab->OptModuleList.push_back(' ');
415 capab->OptModuleList.append(params[1]);
418 else if ((params[0] == "CHANMODES") && (params.size() == 2))
420 capab->ChanModes = params[1];
422 else if ((params[0] == "USERMODES") && (params.size() == 2))
424 capab->UserModes = params[1];
426 else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
428 irc::tokenstream capabs(params[1]);
430 while (capabs.GetToken(item))
432 /* Process each key/value pair */
433 std::string::size_type equals = item.find('=');
434 if (equals != std::string::npos)
436 std::string var(item, 0, equals);
437 std::string value(item, equals+1);
438 capab->CapKeys[var] = value;