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 // 2.2 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 for(char c='A'; c <= 'z'; c++)
87 ModeHandler* mh = ServerInstance->Modes->FindMode(c, type);
90 std::string mdesc = mh->name;
92 PrefixMode* pm = mh->IsPrefixMode();
96 mdesc.push_back(pm->GetPrefix());
98 mdesc.push_back(mh->GetModeChar());
99 modes.push_back(mdesc);
102 std::sort(modes.begin(), modes.end());
103 return irc::stringjoiner(modes);
106 void TreeSocket::SendCapabilities(int phase)
108 if (capab->capab_phase >= phase)
111 if (capab->capab_phase < 1 && phase >= 1)
112 WriteLine("CAPAB START " + ConvToStr(ProtocolVersion));
114 capab->capab_phase = phase;
118 const char sep = ' ';
119 irc::sepstream modulelist(MyModules(VF_COMMON), sep);
120 irc::sepstream optmodulelist(MyModules(VF_OPTCOMMON), sep);
121 /* Send module names, split at 509 length */
123 std::string line = "CAPAB MODULES :";
124 while (modulelist.GetToken(item))
126 if (line.length() + item.length() + 1 > 509)
128 this->WriteLine(line);
129 line = "CAPAB MODULES :";
132 if (line != "CAPAB MODULES :")
137 if (line != "CAPAB MODULES :")
138 this->WriteLine(line);
140 line = "CAPAB MODSUPPORT :";
141 while (optmodulelist.GetToken(item))
143 if (line.length() + item.length() + 1 > 509)
145 this->WriteLine(line);
146 line = "CAPAB MODSUPPORT :";
149 if (line != "CAPAB MODSUPPORT :")
154 if (line != "CAPAB MODSUPPORT :")
155 this->WriteLine(line);
157 WriteLine("CAPAB CHANMODES :" + BuildModeList(MODETYPE_CHANNEL));
158 WriteLine("CAPAB USERMODES :" + BuildModeList(MODETYPE_USER));
161 /* Do we have sha256 available? If so, we send a challenge */
162 if (ServerInstance->Modules->FindService(SERVICE_DATA, "hash/sha256"))
164 SetOurChallenge(ServerInstance->GenRandomStr(20));
165 extra = " CHALLENGE=" + this->GetOurChallenge();
168 // 2.0 needs this key
169 if (proto_version == 1202)
170 extra.append(" PROTOCOL="+ConvToStr(ProtocolVersion));
172 this->WriteLine("CAPAB CAPABILITIES " /* Preprocessor does this one. */
173 ":NICKMAX="+ConvToStr(ServerInstance->Config->Limits.NickMax)+
174 " CHANMAX="+ConvToStr(ServerInstance->Config->Limits.ChanMax)+
175 " MAXMODES="+ConvToStr(ServerInstance->Config->Limits.MaxModes)+
176 " IDENTMAX="+ConvToStr(ServerInstance->Config->Limits.IdentMax)+
177 " MAXQUIT="+ConvToStr(ServerInstance->Config->Limits.MaxQuit)+
178 " MAXTOPIC="+ConvToStr(ServerInstance->Config->Limits.MaxTopic)+
179 " MAXKICK="+ConvToStr(ServerInstance->Config->Limits.MaxKick)+
180 " MAXGECOS="+ConvToStr(ServerInstance->Config->Limits.MaxGecos)+
181 " MAXAWAY="+ConvToStr(ServerInstance->Config->Limits.MaxAway)+
182 " MAXHOST="+ConvToStr(ServerInstance->Config->Limits.MaxHost)+
184 " PREFIX="+ServerInstance->Modes->BuildPrefixes()+
185 " CHANMODES="+ServerInstance->Modes->GiveModeList(MODETYPE_CHANNEL)+
186 " USERMODES="+ServerInstance->Modes->GiveModeList(MODETYPE_USER)+
187 // XXX: Advertise the presence or absence of m_globops in CAPAB CAPABILITIES.
188 // Services want to know about it, and since m_globops was not marked as VF_(OPT)COMMON
189 // in 2.0, we advertise it here to not break linking to previous versions.
190 // Protocol version 1201 (1.2) does not have this issue because we advertise m_globops
191 // to 1201 protocol servers irrespectively of its module flags.
192 (ServerInstance->Modules->Find("m_globops.so") != NULL ? " GLOBOPS=1" : " GLOBOPS=0")
195 this->WriteLine("CAPAB END");
198 /* Isolate and return the elements that are different between two comma seperated lists */
199 void TreeSocket::ListDifference(const std::string &one, const std::string &two, char sep,
200 std::string& mleft, std::string& mright)
202 std::set<std::string> values;
203 irc::sepstream sepleft(one, sep);
204 irc::sepstream sepright(two, sep);
206 while (sepleft.GetToken(item))
210 while (sepright.GetToken(item))
212 if (!values.erase(item))
214 mright.push_back(sep);
218 for(std::set<std::string>::iterator i = values.begin(); i != values.end(); ++i)
220 mleft.push_back(sep);
225 bool TreeSocket::Capab(const parameterlist ¶ms)
227 if (params.size() < 1)
229 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
232 if (params[0] == "START")
234 capab->ModuleList.clear();
235 capab->OptModuleList.clear();
236 capab->CapKeys.clear();
237 if (params.size() > 1)
238 proto_version = ConvToInt(params[1]);
240 if (proto_version < MinCompatProtocol)
242 SendError("CAPAB negotiation failed: Server is using protocol version " + (proto_version ? ConvToStr(proto_version) : "1201 or older")
243 + " which is too old to link with this server (version " + ConvToStr(ProtocolVersion)
244 + (ProtocolVersion != MinCompatProtocol ? ", links with " + ConvToStr(MinCompatProtocol) + " and above)" : ")"));
248 // Special case, may be removed in the future
249 if (proto_version == 1203 || proto_version == 1204)
251 SendError("CAPAB negotiation failed: InspIRCd 2.1 beta is not supported");
257 else if (params[0] == "END")
260 /* Compare ModuleList and check CapKeys */
261 if ((this->capab->ModuleList != this->MyModules(VF_COMMON)) && (this->capab->ModuleList.length()))
263 std::string diffIneed, diffUneed;
264 ListDifference(this->capab->ModuleList, this->MyModules(VF_COMMON), ' ', diffIneed, diffUneed);
265 if (diffIneed.length() || diffUneed.length())
267 reason = "Modules incorrectly matched on these servers.";
268 if (diffIneed.length())
269 reason += " Not loaded here:" + diffIneed;
270 if (diffUneed.length())
271 reason += " Not loaded there:" + diffUneed;
272 this->SendError("CAPAB negotiation failed: "+reason);
276 if (this->capab->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->capab->OptModuleList.length())
278 std::string diffIneed, diffUneed;
279 ListDifference(this->capab->OptModuleList, this->MyModules(VF_OPTCOMMON), ' ', diffIneed, diffUneed);
280 if (diffIneed.length() || diffUneed.length())
282 if (Utils->AllowOptCommon)
284 ServerInstance->SNO->WriteToSnoMask('l',
285 "Optional module lists do not match, some commands may not work globally.%s%s%s%s",
286 diffIneed.length() ? " Not loaded here:" : "", diffIneed.c_str(),
287 diffUneed.length() ? " Not loaded there:" : "", diffUneed.c_str());
291 reason = "Optional modules incorrectly matched on these servers, and options::allowmismatch not set.";
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);
302 if(this->capab->CapKeys.find("PREFIX") != this->capab->CapKeys.end() && this->capab->CapKeys.find("PREFIX")->second != ServerInstance->Modes->BuildPrefixes())
303 reason = "One or more of the prefixes on the remote server are invalid on this server.";
305 if (!capab->ChanModes.empty())
307 if (capab->ChanModes != BuildModeList(MODETYPE_CHANNEL))
309 std::string diffIneed, diffUneed;
310 ListDifference(capab->ChanModes, BuildModeList(MODETYPE_CHANNEL), ' ', diffIneed, diffUneed);
311 if (diffIneed.length() || diffUneed.length())
313 reason = "Channel modes not matched on these servers.";
314 if (diffIneed.length())
315 reason += " Not loaded here:" + diffIneed;
316 if (diffUneed.length())
317 reason += " Not loaded there:" + diffUneed;
321 else if (this->capab->CapKeys.find("CHANMODES") != this->capab->CapKeys.end())
323 if (this->capab->CapKeys.find("CHANMODES")->second != ServerInstance->Modes->GiveModeList(MODETYPE_CHANNEL))
324 reason = "One or more of the channel modes on the remote server are invalid on this server.";
327 if (!capab->UserModes.empty())
329 if (capab->UserModes != BuildModeList(MODETYPE_USER))
331 std::string diffIneed, diffUneed;
332 ListDifference(capab->UserModes, BuildModeList(MODETYPE_USER), ' ', diffIneed, diffUneed);
333 if (diffIneed.length() || diffUneed.length())
335 reason = "User 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 (this->capab->CapKeys.find("USERMODES") != this->capab->CapKeys.end())
345 if (this->capab->CapKeys.find("USERMODES")->second != ServerInstance->Modes->GiveModeList(MODETYPE_USER))
346 reason = "One or more of the user modes on the remote server are invalid on this server.";
349 /* Challenge response, store their challenge for our password */
350 std::map<std::string,std::string>::iterator n = this->capab->CapKeys.find("CHALLENGE");
351 if ((n != this->capab->CapKeys.end()) && (ServerInstance->Modules->FindService(SERVICE_DATA, "hash/sha256")))
353 /* Challenge-response is on now */
354 this->SetTheirChallenge(n->second);
355 if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
357 this->SendCapabilities(2);
358 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+this->MakePass(capab->link->SendPass, capab->theirchallenge)+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
363 // They didn't specify a challenge or we don't have sha256, we use plaintext
364 if (this->LinkState == CONNECTING)
366 this->SendCapabilities(2);
367 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+capab->link->SendPass+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
373 this->SendError("CAPAB negotiation failed: "+reason);
377 else if ((params[0] == "MODULES") && (params.size() == 2))
379 if (!capab->ModuleList.length())
381 capab->ModuleList = params[1];
385 capab->ModuleList.push_back(' ');
386 capab->ModuleList.append(params[1]);
389 else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
391 if (!capab->OptModuleList.length())
393 capab->OptModuleList = params[1];
397 capab->OptModuleList.push_back(' ');
398 capab->OptModuleList.append(params[1]);
401 else if ((params[0] == "CHANMODES") && (params.size() == 2))
403 capab->ChanModes = params[1];
405 else if ((params[0] == "USERMODES") && (params.size() == 2))
407 capab->UserModes = params[1];
409 else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
411 irc::tokenstream capabs(params[1]);
413 while (capabs.GetToken(item))
415 /* Process each key/value pair */
416 std::string::size_type equals = item.find('=');
417 if (equals != std::string::npos)
419 std::string var(item, 0, equals);
420 std::string value(item, equals+1);
421 capab->CapKeys[var] = value;