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 std::vector<std::string> modlist = ServerInstance->Modules->GetAllModuleNames(filter);
33 std::string capabilities;
34 sort(modlist.begin(),modlist.end());
35 for (std::vector<std::string>::const_iterator i = modlist.begin(); i != modlist.end(); ++i)
37 if (i != modlist.begin())
38 capabilities.push_back(' ');
39 capabilities.append(*i);
40 Module* m = ServerInstance->Modules->Find(*i);
41 Version v = m->GetVersion();
42 if (!v.link_data.empty())
44 capabilities.push_back('=');
45 capabilities.append(v.link_data);
51 static std::string BuildModeList(ModeType type)
53 std::vector<std::string> modes;
54 for(char c='A'; c <= 'z'; c++)
56 ModeHandler* mh = ServerInstance->Modes->FindMode(c, type);
59 std::string mdesc = mh->name;
62 mdesc.push_back(mh->GetPrefix());
63 if (mh->GetModeChar())
64 mdesc.push_back(mh->GetModeChar());
65 modes.push_back(mdesc);
68 sort(modes.begin(), modes.end());
69 irc::stringjoiner line(" ", modes, 0, modes.size() - 1);
70 return line.GetJoined();
73 void TreeSocket::SendCapabilities(int phase)
75 if (capab->capab_phase >= phase)
78 if (capab->capab_phase < 1 && phase >= 1)
79 WriteLine("CAPAB START " + ConvToStr(ProtocolVersion));
81 capab->capab_phase = phase;
86 irc::sepstream modulelist(MyModules(VF_COMMON), sep);
87 irc::sepstream optmodulelist(MyModules(VF_OPTCOMMON), sep);
88 /* Send module names, split at 509 length */
90 std::string line = "CAPAB MODULES :";
91 while (modulelist.GetToken(item))
93 if (line.length() + item.length() + 1 > 509)
95 this->WriteLine(line);
96 line = "CAPAB MODULES :";
99 if (line != "CAPAB MODULES :")
104 if (line != "CAPAB MODULES :")
105 this->WriteLine(line);
107 line = "CAPAB MODSUPPORT :";
108 while (optmodulelist.GetToken(item))
110 if (line.length() + item.length() + 1 > 509)
112 this->WriteLine(line);
113 line = "CAPAB MODSUPPORT :";
116 if (line != "CAPAB MODSUPPORT :")
121 if (line != "CAPAB MODSUPPORT :")
122 this->WriteLine(line);
124 WriteLine("CAPAB CHANMODES :" + BuildModeList(MODETYPE_CHANNEL));
125 WriteLine("CAPAB USERMODES :" + BuildModeList(MODETYPE_USER));
128 /* Do we have sha256 available? If so, we send a challenge */
129 if (Utils->ChallengeResponse && (ServerInstance->Modules->Find("m_sha256.so")))
131 SetOurChallenge(ServerInstance->GenRandomStr(20));
132 extra = " CHALLENGE=" + this->GetOurChallenge();
135 this->WriteLine("CAPAB CAPABILITIES " /* Preprocessor does this one. */
136 ":NICKMAX="+ConvToStr(ServerInstance->Config->Limits.NickMax)+
137 " CHANMAX="+ConvToStr(ServerInstance->Config->Limits.ChanMax)+
138 " MAXMODES="+ConvToStr(ServerInstance->Config->Limits.MaxModes)+
139 " IDENTMAX="+ConvToStr(ServerInstance->Config->Limits.IdentMax)+
140 " MAXQUIT="+ConvToStr(ServerInstance->Config->Limits.MaxQuit)+
141 " MAXTOPIC="+ConvToStr(ServerInstance->Config->Limits.MaxTopic)+
142 " MAXKICK="+ConvToStr(ServerInstance->Config->Limits.MaxKick)+
143 " MAXGECOS="+ConvToStr(ServerInstance->Config->Limits.MaxGecos)+
144 " MAXAWAY="+ConvToStr(ServerInstance->Config->Limits.MaxAway)+
146 " PROTOCOL="+ConvToStr(ProtocolVersion)+extra+
147 " PREFIX="+ServerInstance->Modes->BuildPrefixes()+
148 " CHANMODES="+ServerInstance->Modes->GiveModeList(MASK_CHANNEL)+
149 " USERMODES="+ServerInstance->Modes->GiveModeList(MASK_USER)+
152 this->WriteLine("CAPAB END");
155 /* Isolate and return the elements that are different between two comma seperated lists */
156 void TreeSocket::ListDifference(const std::string &one, const std::string &two, char sep,
157 std::string& mleft, std::string& mright)
159 std::set<std::string> values;
160 irc::sepstream sepleft(one, sep);
161 irc::sepstream sepright(two, sep);
163 while (sepleft.GetToken(item))
167 while (sepright.GetToken(item))
169 if (!values.erase(item))
171 mright.push_back(sep);
175 for(std::set<std::string>::iterator i = values.begin(); i != values.end(); ++i)
177 mleft.push_back(sep);
182 bool TreeSocket::Capab(const parameterlist ¶ms)
184 if (params.size() < 1)
186 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
189 if (params[0] == "START")
191 capab->ModuleList.clear();
192 capab->OptModuleList.clear();
193 capab->CapKeys.clear();
194 if (params.size() > 1)
195 proto_version = ConvToInt(params[1]);
198 else if (params[0] == "END")
201 /* Compare ModuleList and check CapKeys */
202 if ((this->capab->ModuleList != this->MyModules(VF_COMMON)) && (this->capab->ModuleList.length()))
204 std::string diffIneed, diffUneed;
205 ListDifference(this->capab->ModuleList, this->MyModules(VF_COMMON), ' ', diffIneed, diffUneed);
206 if (diffIneed.length() || diffUneed.length())
208 reason = "Modules incorrectly matched on these servers.";
209 if (diffIneed.length())
210 reason += " Not loaded here:" + diffIneed;
211 if (diffUneed.length())
212 reason += " Not loaded there:" + diffUneed;
213 this->SendError("CAPAB negotiation failed: "+reason);
217 if (this->capab->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->capab->OptModuleList.length())
219 std::string diffIneed, diffUneed;
220 ListDifference(this->capab->OptModuleList, this->MyModules(VF_OPTCOMMON), ' ', diffIneed, diffUneed);
221 if (diffIneed.length() || diffUneed.length())
223 if (Utils->AllowOptCommon)
225 ServerInstance->SNO->WriteToSnoMask('l',
226 "Optional module lists do not match, some commands may not work globally.%s%s%s%s",
227 diffIneed.length() ? " Not loaded here:" : "", diffIneed.c_str(),
228 diffUneed.length() ? " Not loaded there:" : "", diffUneed.c_str());
232 reason = "Optional modules incorrectly matched on these servers, and options::allowmismatch not set.";
233 if (diffIneed.length())
234 reason += " Not loaded here:" + diffIneed;
235 if (diffUneed.length())
236 reason += " Not loaded there:" + diffUneed;
237 this->SendError("CAPAB negotiation failed: "+reason);
243 if (this->capab->CapKeys.find("PROTOCOL") == this->capab->CapKeys.end())
245 reason = "Protocol version not specified";
249 proto_version = ConvToInt(capab->CapKeys.find("PROTOCOL")->second);
250 if (proto_version < MinCompatProtocol)
252 reason = "Server is using protocol version " + ConvToStr(proto_version) +
253 " which is too old to link with this server (version " + ConvToStr(ProtocolVersion)
254 + (ProtocolVersion != MinCompatProtocol ? ", links with " + ConvToStr(MinCompatProtocol) + " and above)" : ")");
258 if(this->capab->CapKeys.find("PREFIX") != this->capab->CapKeys.end() && this->capab->CapKeys.find("PREFIX")->second != ServerInstance->Modes->BuildPrefixes())
259 reason = "One or more of the prefixes on the remote server are invalid on this server.";
261 if (!capab->ChanModes.empty())
263 if (capab->ChanModes != BuildModeList(MODETYPE_CHANNEL))
265 std::string diffIneed, diffUneed;
266 ListDifference(capab->ChanModes, BuildModeList(MODETYPE_CHANNEL), ' ', diffIneed, diffUneed);
267 if (diffIneed.length() || diffUneed.length())
269 reason = "Channel modes not matched on these servers.";
270 if (diffIneed.length())
271 reason += " Not loaded here:" + diffIneed;
272 if (diffUneed.length())
273 reason += " Not loaded there:" + diffUneed;
277 else if (this->capab->CapKeys.find("CHANMODES") != this->capab->CapKeys.end())
279 if (this->capab->CapKeys.find("CHANMODES")->second != ServerInstance->Modes->GiveModeList(MASK_CHANNEL))
280 reason = "One or more of the channel modes on the remote server are invalid on this server.";
283 if (!capab->UserModes.empty())
285 if (capab->UserModes != BuildModeList(MODETYPE_USER))
287 std::string diffIneed, diffUneed;
288 ListDifference(capab->UserModes, BuildModeList(MODETYPE_USER), ' ', diffIneed, diffUneed);
289 if (diffIneed.length() || diffUneed.length())
291 reason = "User modes not matched on these servers.";
292 if (diffIneed.length())
293 reason += " Not loaded here:" + diffIneed;
294 if (diffUneed.length())
295 reason += " Not loaded there:" + diffUneed;
299 else if (this->capab->CapKeys.find("USERMODES") != this->capab->CapKeys.end())
301 if (this->capab->CapKeys.find("USERMODES")->second != ServerInstance->Modes->GiveModeList(MASK_USER))
302 reason = "One or more of the user modes on the remote server are invalid on this server.";
305 /* Challenge response, store their challenge for our password */
306 std::map<std::string,std::string>::iterator n = this->capab->CapKeys.find("CHALLENGE");
307 if (Utils->ChallengeResponse && (n != this->capab->CapKeys.end()) && (ServerInstance->Modules->Find("m_sha256.so")))
309 /* Challenge-response is on now */
310 this->SetTheirChallenge(n->second);
311 if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
313 this->SendCapabilities(2);
314 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+this->MakePass(capab->link->SendPass, capab->theirchallenge)+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
319 /* They didnt specify a challenge or we don't have m_sha256.so, we use plaintext */
320 if (this->LinkState == CONNECTING)
322 this->SendCapabilities(2);
323 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+capab->link->SendPass+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
329 this->SendError("CAPAB negotiation failed: "+reason);
333 else if ((params[0] == "MODULES") && (params.size() == 2))
335 if (!capab->ModuleList.length())
337 capab->ModuleList = params[1];
341 capab->ModuleList.push_back(' ');
342 capab->ModuleList.append(params[1]);
345 else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
347 if (!capab->OptModuleList.length())
349 capab->OptModuleList = params[1];
353 capab->OptModuleList.push_back(' ');
354 capab->OptModuleList.append(params[1]);
357 else if ((params[0] == "CHANMODES") && (params.size() == 2))
359 capab->ChanModes = params[1];
361 else if ((params[0] == "USERMODES") && (params.size() == 2))
363 capab->UserModes = params[1];
365 else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
367 irc::tokenstream capabs(params[1]);
369 while (capabs.GetToken(item))
371 /* Process each key/value pair */
372 std::string::size_type equals = item.find('=');
373 if (equals != std::string::npos)
375 std::string var = item.substr(0, equals);
376 std::string value = item.substr(equals+1, item.length());
377 capab->CapKeys[var] = value;