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);
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 // 2.0 needs this key
136 if (proto_version == 1202)
137 extra.append(" PROTOCOL="+ConvToStr(ProtocolVersion));
139 this->WriteLine("CAPAB CAPABILITIES " /* Preprocessor does this one. */
140 ":NICKMAX="+ConvToStr(ServerInstance->Config->Limits.NickMax)+
141 " CHANMAX="+ConvToStr(ServerInstance->Config->Limits.ChanMax)+
142 " MAXMODES="+ConvToStr(ServerInstance->Config->Limits.MaxModes)+
143 " IDENTMAX="+ConvToStr(ServerInstance->Config->Limits.IdentMax)+
144 " MAXQUIT="+ConvToStr(ServerInstance->Config->Limits.MaxQuit)+
145 " MAXTOPIC="+ConvToStr(ServerInstance->Config->Limits.MaxTopic)+
146 " MAXKICK="+ConvToStr(ServerInstance->Config->Limits.MaxKick)+
147 " MAXGECOS="+ConvToStr(ServerInstance->Config->Limits.MaxGecos)+
148 " MAXAWAY="+ConvToStr(ServerInstance->Config->Limits.MaxAway)+
150 " PREFIX="+ServerInstance->Modes->BuildPrefixes()+
151 " CHANMODES="+ServerInstance->Modes->GiveModeList(MASK_CHANNEL)+
152 " USERMODES="+ServerInstance->Modes->GiveModeList(MASK_USER)
155 this->WriteLine("CAPAB END");
158 /* Isolate and return the elements that are different between two comma seperated lists */
159 void TreeSocket::ListDifference(const std::string &one, const std::string &two, char sep,
160 std::string& mleft, std::string& mright)
162 std::set<std::string> values;
163 irc::sepstream sepleft(one, sep);
164 irc::sepstream sepright(two, sep);
166 while (sepleft.GetToken(item))
170 while (sepright.GetToken(item))
172 if (!values.erase(item))
174 mright.push_back(sep);
178 for(std::set<std::string>::iterator i = values.begin(); i != values.end(); ++i)
180 mleft.push_back(sep);
185 bool TreeSocket::Capab(const parameterlist ¶ms)
187 if (params.size() < 1)
189 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
192 if (params[0] == "START")
194 capab->ModuleList.clear();
195 capab->OptModuleList.clear();
196 capab->CapKeys.clear();
197 if (params.size() > 1)
198 proto_version = ConvToInt(params[1]);
200 if (proto_version < MinCompatProtocol)
202 SendError("CAPAB negotiation failed: Server is using protocol version " + (proto_version ? ConvToStr(proto_version) : "1201 or older")
203 + " which is too old to link with this server (version " + ConvToStr(ProtocolVersion)
204 + (ProtocolVersion != MinCompatProtocol ? ", links with " + ConvToStr(MinCompatProtocol) + " and above)" : ")"));
208 // Special case, may be removed in the future
209 if (proto_version == 1203 || proto_version == 1204)
211 SendError("CAPAB negotiation failed: InspIRCd 2.1 beta is not supported");
217 else if (params[0] == "END")
220 /* Compare ModuleList and check CapKeys */
221 if ((this->capab->ModuleList != this->MyModules(VF_COMMON)) && (this->capab->ModuleList.length()))
223 std::string diffIneed, diffUneed;
224 ListDifference(this->capab->ModuleList, this->MyModules(VF_COMMON), ' ', diffIneed, diffUneed);
225 if (diffIneed.length() || diffUneed.length())
227 reason = "Modules incorrectly matched on these servers.";
228 if (diffIneed.length())
229 reason += " Not loaded here:" + diffIneed;
230 if (diffUneed.length())
231 reason += " Not loaded there:" + diffUneed;
232 this->SendError("CAPAB negotiation failed: "+reason);
236 if (this->capab->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->capab->OptModuleList.length())
238 std::string diffIneed, diffUneed;
239 ListDifference(this->capab->OptModuleList, this->MyModules(VF_OPTCOMMON), ' ', diffIneed, diffUneed);
240 if (diffIneed.length() || diffUneed.length())
242 if (Utils->AllowOptCommon)
244 ServerInstance->SNO->WriteToSnoMask('l',
245 "Optional module lists do not match, some commands may not work globally.%s%s%s%s",
246 diffIneed.length() ? " Not loaded here:" : "", diffIneed.c_str(),
247 diffUneed.length() ? " Not loaded there:" : "", diffUneed.c_str());
251 reason = "Optional modules incorrectly matched on these servers, and options::allowmismatch not set.";
252 if (diffIneed.length())
253 reason += " Not loaded here:" + diffIneed;
254 if (diffUneed.length())
255 reason += " Not loaded there:" + diffUneed;
256 this->SendError("CAPAB negotiation failed: "+reason);
262 if(this->capab->CapKeys.find("PREFIX") != this->capab->CapKeys.end() && this->capab->CapKeys.find("PREFIX")->second != ServerInstance->Modes->BuildPrefixes())
263 reason = "One or more of the prefixes on the remote server are invalid on this server.";
265 if (!capab->ChanModes.empty())
267 if (capab->ChanModes != BuildModeList(MODETYPE_CHANNEL))
269 std::string diffIneed, diffUneed;
270 ListDifference(capab->ChanModes, BuildModeList(MODETYPE_CHANNEL), ' ', diffIneed, diffUneed);
271 if (diffIneed.length() || diffUneed.length())
273 reason = "Channel modes not matched on these servers.";
274 if (diffIneed.length())
275 reason += " Not loaded here:" + diffIneed;
276 if (diffUneed.length())
277 reason += " Not loaded there:" + diffUneed;
281 else if (this->capab->CapKeys.find("CHANMODES") != this->capab->CapKeys.end())
283 if (this->capab->CapKeys.find("CHANMODES")->second != ServerInstance->Modes->GiveModeList(MASK_CHANNEL))
284 reason = "One or more of the channel modes on the remote server are invalid on this server.";
287 if (!capab->UserModes.empty())
289 if (capab->UserModes != BuildModeList(MODETYPE_USER))
291 std::string diffIneed, diffUneed;
292 ListDifference(capab->UserModes, BuildModeList(MODETYPE_USER), ' ', diffIneed, diffUneed);
293 if (diffIneed.length() || diffUneed.length())
295 reason = "User modes not matched on these servers.";
296 if (diffIneed.length())
297 reason += " Not loaded here:" + diffIneed;
298 if (diffUneed.length())
299 reason += " Not loaded there:" + diffUneed;
303 else if (this->capab->CapKeys.find("USERMODES") != this->capab->CapKeys.end())
305 if (this->capab->CapKeys.find("USERMODES")->second != ServerInstance->Modes->GiveModeList(MASK_USER))
306 reason = "One or more of the user modes on the remote server are invalid on this server.";
309 /* Challenge response, store their challenge for our password */
310 std::map<std::string,std::string>::iterator n = this->capab->CapKeys.find("CHALLENGE");
311 if (Utils->ChallengeResponse && (n != this->capab->CapKeys.end()) && (ServerInstance->Modules->Find("m_sha256.so")))
313 /* Challenge-response is on now */
314 this->SetTheirChallenge(n->second);
315 if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
317 this->SendCapabilities(2);
318 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+this->MakePass(capab->link->SendPass, capab->theirchallenge)+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
323 /* They didnt specify a challenge or we don't have m_sha256.so, we use plaintext */
324 if (this->LinkState == CONNECTING)
326 this->SendCapabilities(2);
327 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+capab->link->SendPass+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
333 this->SendError("CAPAB negotiation failed: "+reason);
337 else if ((params[0] == "MODULES") && (params.size() == 2))
339 if (!capab->ModuleList.length())
341 capab->ModuleList = params[1];
345 capab->ModuleList.push_back(' ');
346 capab->ModuleList.append(params[1]);
349 else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
351 if (!capab->OptModuleList.length())
353 capab->OptModuleList = params[1];
357 capab->OptModuleList.push_back(' ');
358 capab->OptModuleList.append(params[1]);
361 else if ((params[0] == "CHANMODES") && (params.size() == 2))
363 capab->ChanModes = params[1];
365 else if ((params[0] == "USERMODES") && (params.size() == 2))
367 capab->UserModes = params[1];
369 else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
371 irc::tokenstream capabs(params[1]);
373 while (capabs.GetToken(item))
375 /* Process each key/value pair */
376 std::string::size_type equals = item.find('=');
377 if (equals != std::string::npos)
379 std::string var = item.substr(0, equals);
380 std::string value = item.substr(equals+1, item.length());
381 capab->CapKeys[var] = value;