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"
29 std::string TreeSocket::MyModules(int filter)
31 const ModuleManager::ModuleMap& modlist = ServerInstance->Modules->GetModules();
33 std::string capabilities;
34 for (ModuleManager::ModuleMap::const_iterator i = modlist.begin(); i != modlist.end(); ++i)
36 Version v = i->second->GetVersion();
37 if (!(v.Flags & filter))
40 if (i != modlist.begin())
41 capabilities.push_back(' ');
42 capabilities.append(i->first);
43 if (!v.link_data.empty())
45 capabilities.push_back('=');
46 capabilities.append(v.link_data);
52 static std::string BuildModeList(ModeType type)
54 std::vector<std::string> modes;
55 for(char c='A'; c <= 'z'; c++)
57 ModeHandler* mh = ServerInstance->Modes->FindMode(c, type);
60 std::string mdesc = mh->name;
62 PrefixMode* pm = mh->IsPrefixMode();
66 mdesc.push_back(pm->GetPrefix());
68 mdesc.push_back(mh->GetModeChar());
69 modes.push_back(mdesc);
72 sort(modes.begin(), modes.end());
73 return irc::stringjoiner(modes);
76 void TreeSocket::SendCapabilities(int phase)
78 if (capab->capab_phase >= phase)
81 if (capab->capab_phase < 1 && phase >= 1)
82 WriteLine("CAPAB START " + ConvToStr(ProtocolVersion));
84 capab->capab_phase = phase;
89 irc::sepstream modulelist(MyModules(VF_COMMON), sep);
90 irc::sepstream optmodulelist(MyModules(VF_OPTCOMMON), sep);
91 /* Send module names, split at 509 length */
93 std::string line = "CAPAB MODULES :";
94 while (modulelist.GetToken(item))
96 if (line.length() + item.length() + 1 > 509)
98 this->WriteLine(line);
99 line = "CAPAB MODULES :";
102 if (line != "CAPAB MODULES :")
107 if (line != "CAPAB MODULES :")
108 this->WriteLine(line);
110 line = "CAPAB MODSUPPORT :";
111 while (optmodulelist.GetToken(item))
113 if (line.length() + item.length() + 1 > 509)
115 this->WriteLine(line);
116 line = "CAPAB MODSUPPORT :";
119 if (line != "CAPAB MODSUPPORT :")
124 if (line != "CAPAB MODSUPPORT :")
125 this->WriteLine(line);
127 WriteLine("CAPAB CHANMODES :" + BuildModeList(MODETYPE_CHANNEL));
128 WriteLine("CAPAB USERMODES :" + BuildModeList(MODETYPE_USER));
131 /* Do we have sha256 available? If so, we send a challenge */
132 if (ServerInstance->Modules->FindService(SERVICE_DATA, "hash/sha256"))
134 SetOurChallenge(ServerInstance->GenRandomStr(20));
135 extra = " CHALLENGE=" + this->GetOurChallenge();
138 // 2.0 needs this key
139 if (proto_version == 1202)
140 extra.append(" PROTOCOL="+ConvToStr(ProtocolVersion));
142 this->WriteLine("CAPAB CAPABILITIES " /* Preprocessor does this one. */
143 ":NICKMAX="+ConvToStr(ServerInstance->Config->Limits.NickMax)+
144 " CHANMAX="+ConvToStr(ServerInstance->Config->Limits.ChanMax)+
145 " MAXMODES="+ConvToStr(ServerInstance->Config->Limits.MaxModes)+
146 " IDENTMAX="+ConvToStr(ServerInstance->Config->Limits.IdentMax)+
147 " MAXQUIT="+ConvToStr(ServerInstance->Config->Limits.MaxQuit)+
148 " MAXTOPIC="+ConvToStr(ServerInstance->Config->Limits.MaxTopic)+
149 " MAXKICK="+ConvToStr(ServerInstance->Config->Limits.MaxKick)+
150 " MAXGECOS="+ConvToStr(ServerInstance->Config->Limits.MaxGecos)+
151 " MAXAWAY="+ConvToStr(ServerInstance->Config->Limits.MaxAway)+
152 " MAXHOST="+ConvToStr(ServerInstance->Config->Limits.MaxHost)+
154 " PREFIX="+ServerInstance->Modes->BuildPrefixes()+
155 " CHANMODES="+ServerInstance->Modes->GiveModeList(MODETYPE_CHANNEL)+
156 " USERMODES="+ServerInstance->Modes->GiveModeList(MODETYPE_USER)
159 this->WriteLine("CAPAB END");
162 /* Isolate and return the elements that are different between two comma seperated lists */
163 void TreeSocket::ListDifference(const std::string &one, const std::string &two, char sep,
164 std::string& mleft, std::string& mright)
166 std::set<std::string> values;
167 irc::sepstream sepleft(one, sep);
168 irc::sepstream sepright(two, sep);
170 while (sepleft.GetToken(item))
174 while (sepright.GetToken(item))
176 if (!values.erase(item))
178 mright.push_back(sep);
182 for(std::set<std::string>::iterator i = values.begin(); i != values.end(); ++i)
184 mleft.push_back(sep);
189 bool TreeSocket::Capab(const parameterlist ¶ms)
191 if (params.size() < 1)
193 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
196 if (params[0] == "START")
198 capab->ModuleList.clear();
199 capab->OptModuleList.clear();
200 capab->CapKeys.clear();
201 if (params.size() > 1)
202 proto_version = ConvToInt(params[1]);
204 if (proto_version < MinCompatProtocol)
206 SendError("CAPAB negotiation failed: Server is using protocol version " + (proto_version ? ConvToStr(proto_version) : "1201 or older")
207 + " which is too old to link with this server (version " + ConvToStr(ProtocolVersion)
208 + (ProtocolVersion != MinCompatProtocol ? ", links with " + ConvToStr(MinCompatProtocol) + " and above)" : ")"));
212 // Special case, may be removed in the future
213 if (proto_version == 1203 || proto_version == 1204)
215 SendError("CAPAB negotiation failed: InspIRCd 2.1 beta is not supported");
221 else if (params[0] == "END")
224 /* Compare ModuleList and check CapKeys */
225 if ((this->capab->ModuleList != this->MyModules(VF_COMMON)) && (this->capab->ModuleList.length()))
227 std::string diffIneed, diffUneed;
228 ListDifference(this->capab->ModuleList, this->MyModules(VF_COMMON), ' ', diffIneed, diffUneed);
229 if (diffIneed.length() || diffUneed.length())
231 reason = "Modules incorrectly matched on these servers.";
232 if (diffIneed.length())
233 reason += " Not loaded here:" + diffIneed;
234 if (diffUneed.length())
235 reason += " Not loaded there:" + diffUneed;
236 this->SendError("CAPAB negotiation failed: "+reason);
240 if (this->capab->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->capab->OptModuleList.length())
242 std::string diffIneed, diffUneed;
243 ListDifference(this->capab->OptModuleList, this->MyModules(VF_OPTCOMMON), ' ', diffIneed, diffUneed);
244 if (diffIneed.length() || diffUneed.length())
246 if (Utils->AllowOptCommon)
248 ServerInstance->SNO->WriteToSnoMask('l',
249 "Optional module lists do not match, some commands may not work globally.%s%s%s%s",
250 diffIneed.length() ? " Not loaded here:" : "", diffIneed.c_str(),
251 diffUneed.length() ? " Not loaded there:" : "", diffUneed.c_str());
255 reason = "Optional modules incorrectly matched on these servers, and options::allowmismatch not set.";
256 if (diffIneed.length())
257 reason += " Not loaded here:" + diffIneed;
258 if (diffUneed.length())
259 reason += " Not loaded there:" + diffUneed;
260 this->SendError("CAPAB negotiation failed: "+reason);
266 if(this->capab->CapKeys.find("PREFIX") != this->capab->CapKeys.end() && this->capab->CapKeys.find("PREFIX")->second != ServerInstance->Modes->BuildPrefixes())
267 reason = "One or more of the prefixes on the remote server are invalid on this server.";
269 if (!capab->ChanModes.empty())
271 if (capab->ChanModes != BuildModeList(MODETYPE_CHANNEL))
273 std::string diffIneed, diffUneed;
274 ListDifference(capab->ChanModes, BuildModeList(MODETYPE_CHANNEL), ' ', diffIneed, diffUneed);
275 if (diffIneed.length() || diffUneed.length())
277 reason = "Channel modes not matched on these servers.";
278 if (diffIneed.length())
279 reason += " Not loaded here:" + diffIneed;
280 if (diffUneed.length())
281 reason += " Not loaded there:" + diffUneed;
285 else if (this->capab->CapKeys.find("CHANMODES") != this->capab->CapKeys.end())
287 if (this->capab->CapKeys.find("CHANMODES")->second != ServerInstance->Modes->GiveModeList(MODETYPE_CHANNEL))
288 reason = "One or more of the channel modes on the remote server are invalid on this server.";
291 if (!capab->UserModes.empty())
293 if (capab->UserModes != BuildModeList(MODETYPE_USER))
295 std::string diffIneed, diffUneed;
296 ListDifference(capab->UserModes, BuildModeList(MODETYPE_USER), ' ', diffIneed, diffUneed);
297 if (diffIneed.length() || diffUneed.length())
299 reason = "User modes not matched on these servers.";
300 if (diffIneed.length())
301 reason += " Not loaded here:" + diffIneed;
302 if (diffUneed.length())
303 reason += " Not loaded there:" + diffUneed;
307 else if (this->capab->CapKeys.find("USERMODES") != this->capab->CapKeys.end())
309 if (this->capab->CapKeys.find("USERMODES")->second != ServerInstance->Modes->GiveModeList(MODETYPE_USER))
310 reason = "One or more of the user modes on the remote server are invalid on this server.";
313 /* Challenge response, store their challenge for our password */
314 std::map<std::string,std::string>::iterator n = this->capab->CapKeys.find("CHALLENGE");
315 if ((n != this->capab->CapKeys.end()) && (ServerInstance->Modules->FindService(SERVICE_DATA, "hash/sha256")))
317 /* Challenge-response is on now */
318 this->SetTheirChallenge(n->second);
319 if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
321 this->SendCapabilities(2);
322 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+this->MakePass(capab->link->SendPass, capab->theirchallenge)+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
327 // They didn't specify a challenge or we don't have sha256, we use plaintext
328 if (this->LinkState == CONNECTING)
330 this->SendCapabilities(2);
331 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+capab->link->SendPass+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
337 this->SendError("CAPAB negotiation failed: "+reason);
341 else if ((params[0] == "MODULES") && (params.size() == 2))
343 if (!capab->ModuleList.length())
345 capab->ModuleList = params[1];
349 capab->ModuleList.push_back(' ');
350 capab->ModuleList.append(params[1]);
353 else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
355 if (!capab->OptModuleList.length())
357 capab->OptModuleList = params[1];
361 capab->OptModuleList.push_back(' ');
362 capab->OptModuleList.append(params[1]);
365 else if ((params[0] == "CHANMODES") && (params.size() == 2))
367 capab->ChanModes = params[1];
369 else if ((params[0] == "USERMODES") && (params.size() == 2))
371 capab->UserModes = params[1];
373 else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
375 irc::tokenstream capabs(params[1]);
377 while (capabs.GetToken(item))
379 /* Process each key/value pair */
380 std::string::size_type equals = item.find('=');
381 if (equals != std::string::npos)
383 std::string var = item.substr(0, equals);
384 std::string value = item.substr(equals+1, item.length());
385 capab->CapKeys[var] = value;