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/>.
25 #include "treesocket.h"
26 #include "treeserver.h"
31 std::string TreeSocket::MyModules(int filter)
33 std::vector<std::string> modlist = ServerInstance->Modules->GetAllModuleNames(filter);
35 std::string capabilities;
36 sort(modlist.begin(),modlist.end());
37 for (std::vector<std::string>::const_iterator i = modlist.begin(); i != modlist.end(); ++i)
39 if (i != modlist.begin())
40 capabilities.push_back(' ');
41 capabilities.append(*i);
42 Module* m = ServerInstance->Modules->Find(*i);
43 Version v = m->GetVersion();
44 if (!v.link_data.empty())
46 capabilities.push_back('=');
47 capabilities.append(v.link_data);
53 static std::string BuildModeList(ModeType type)
55 std::vector<std::string> modes;
56 for(char c='A'; c <= 'z'; c++)
58 ModeHandler* mh = ServerInstance->Modes->FindMode(c, type);
61 std::string mdesc = mh->name;
64 mdesc.push_back(mh->GetPrefix());
65 if (mh->GetModeChar())
66 mdesc.push_back(mh->GetModeChar());
67 modes.push_back(mdesc);
70 sort(modes.begin(), modes.end());
71 irc::stringjoiner line(" ", modes, 0, modes.size() - 1);
72 return line.GetJoined();
75 void TreeSocket::SendCapabilities(int phase)
77 if (capab->capab_phase >= phase)
80 if (capab->capab_phase < 1 && phase >= 1)
81 WriteLine("CAPAB START " + ConvToStr(ProtocolVersion));
83 capab->capab_phase = phase;
88 irc::sepstream modulelist(MyModules(VF_COMMON), sep);
89 irc::sepstream optmodulelist(MyModules(VF_OPTCOMMON), sep);
90 /* Send module names, split at 509 length */
92 std::string line = "CAPAB MODULES :";
93 while (modulelist.GetToken(item))
95 if (line.length() + item.length() + 1 > 509)
97 this->WriteLine(line);
98 line = "CAPAB MODULES :";
101 if (line != "CAPAB MODULES :")
106 if (line != "CAPAB MODULES :")
107 this->WriteLine(line);
109 line = "CAPAB MODSUPPORT :";
110 while (optmodulelist.GetToken(item))
112 if (line.length() + item.length() + 1 > 509)
114 this->WriteLine(line);
115 line = "CAPAB MODSUPPORT :";
118 if (line != "CAPAB MODSUPPORT :")
123 if (line != "CAPAB MODSUPPORT :")
124 this->WriteLine(line);
126 WriteLine("CAPAB CHANMODES :" + BuildModeList(MODETYPE_CHANNEL));
127 WriteLine("CAPAB USERMODES :" + BuildModeList(MODETYPE_USER));
130 /* Do we have sha256 available? If so, we send a challenge */
131 if (Utils->ChallengeResponse && (ServerInstance->Modules->Find("m_sha256.so")))
133 SetOurChallenge(ServerInstance->GenRandomStr(20));
134 extra = " CHALLENGE=" + this->GetOurChallenge();
137 this->WriteLine("CAPAB CAPABILITIES " /* Preprocessor does this one. */
138 ":NICKMAX="+ConvToStr(ServerInstance->Config->Limits.NickMax)+
139 " CHANMAX="+ConvToStr(ServerInstance->Config->Limits.ChanMax)+
140 " MAXMODES="+ConvToStr(ServerInstance->Config->Limits.MaxModes)+
141 " IDENTMAX="+ConvToStr(ServerInstance->Config->Limits.IdentMax)+
142 " MAXQUIT="+ConvToStr(ServerInstance->Config->Limits.MaxQuit)+
143 " MAXTOPIC="+ConvToStr(ServerInstance->Config->Limits.MaxTopic)+
144 " MAXKICK="+ConvToStr(ServerInstance->Config->Limits.MaxKick)+
145 " MAXGECOS="+ConvToStr(ServerInstance->Config->Limits.MaxGecos)+
146 " MAXAWAY="+ConvToStr(ServerInstance->Config->Limits.MaxAway)+
148 " PROTOCOL="+ConvToStr(ProtocolVersion)+extra+
149 " PREFIX="+ServerInstance->Modes->BuildPrefixes()+
150 " CHANMODES="+ServerInstance->Modes->GiveModeList(MASK_CHANNEL)+
151 " USERMODES="+ServerInstance->Modes->GiveModeList(MASK_USER)+
154 this->WriteLine("CAPAB END");
157 /* Isolate and return the elements that are different between two comma seperated lists */
158 void TreeSocket::ListDifference(const std::string &one, const std::string &two, char sep,
159 std::string& mleft, std::string& mright)
161 std::set<std::string> values;
162 irc::sepstream sepleft(one, sep);
163 irc::sepstream sepright(two, sep);
165 while (sepleft.GetToken(item))
169 while (sepright.GetToken(item))
171 if (!values.erase(item))
173 mright.push_back(sep);
177 for(std::set<std::string>::iterator i = values.begin(); i != values.end(); ++i)
179 mleft.push_back(sep);
184 bool TreeSocket::Capab(const parameterlist ¶ms)
186 if (params.size() < 1)
188 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
191 if (params[0] == "START")
193 capab->ModuleList.clear();
194 capab->OptModuleList.clear();
195 capab->CapKeys.clear();
196 if (params.size() > 1)
197 proto_version = atoi(params[1].c_str());
200 else if (params[0] == "END")
203 /* Compare ModuleList and check CapKeys */
204 if ((this->capab->ModuleList != this->MyModules(VF_COMMON)) && (this->capab->ModuleList.length()))
206 std::string diffIneed, diffUneed;
207 ListDifference(this->capab->ModuleList, this->MyModules(VF_COMMON), ' ', diffIneed, diffUneed);
208 if (diffIneed.length() || diffUneed.length())
210 reason = "Modules incorrectly matched on these servers.";
211 if (diffIneed.length())
212 reason += " Not loaded here:" + diffIneed;
213 if (diffUneed.length())
214 reason += " Not loaded there:" + diffUneed;
215 this->SendError("CAPAB negotiation failed: "+reason);
219 if (this->capab->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->capab->OptModuleList.length())
221 std::string diffIneed, diffUneed;
222 ListDifference(this->capab->OptModuleList, this->MyModules(VF_OPTCOMMON), ' ', diffIneed, diffUneed);
223 if (diffIneed.length() || diffUneed.length())
225 if (Utils->AllowOptCommon)
227 ServerInstance->SNO->WriteToSnoMask('l',
228 "Optional module lists do not match, some commands may not work globally.%s%s%s%s",
229 diffIneed.length() ? " Not loaded here:" : "", diffIneed.c_str(),
230 diffUneed.length() ? " Not loaded there:" : "", diffUneed.c_str());
234 reason = "Optional modules incorrectly matched on these servers, and options::allowmismatch not set.";
235 if (diffIneed.length())
236 reason += " Not loaded here:" + diffIneed;
237 if (diffUneed.length())
238 reason += " Not loaded there:" + diffUneed;
239 this->SendError("CAPAB negotiation failed: "+reason);
245 if (this->capab->CapKeys.find("PROTOCOL") == this->capab->CapKeys.end())
247 reason = "Protocol version not specified";
251 proto_version = atoi(capab->CapKeys.find("PROTOCOL")->second.c_str());
252 if (proto_version < MinCompatProtocol)
254 reason = "Server is using protocol version " + ConvToStr(proto_version) +
255 " which is too old to link with this server (version " + ConvToStr(ProtocolVersion)
256 + (ProtocolVersion != MinCompatProtocol ? ", links with " + ConvToStr(MinCompatProtocol) + " and above)" : ")");
260 if(this->capab->CapKeys.find("PREFIX") != this->capab->CapKeys.end() && this->capab->CapKeys.find("PREFIX")->second != ServerInstance->Modes->BuildPrefixes())
261 reason = "One or more of the prefixes on the remote server are invalid on this server.";
263 if (!capab->ChanModes.empty())
265 if (capab->ChanModes != BuildModeList(MODETYPE_CHANNEL))
267 std::string diffIneed, diffUneed;
268 ListDifference(capab->ChanModes, BuildModeList(MODETYPE_CHANNEL), ' ', diffIneed, diffUneed);
269 if (diffIneed.length() || diffUneed.length())
271 reason = "Channel modes not matched on these servers.";
272 if (diffIneed.length())
273 reason += " Not loaded here:" + diffIneed;
274 if (diffUneed.length())
275 reason += " Not loaded there:" + diffUneed;
279 else if (this->capab->CapKeys.find("CHANMODES") != this->capab->CapKeys.end())
281 if (this->capab->CapKeys.find("CHANMODES")->second != ServerInstance->Modes->GiveModeList(MASK_CHANNEL))
282 reason = "One or more of the channel modes on the remote server are invalid on this server.";
285 if (!capab->UserModes.empty())
287 if (capab->UserModes != BuildModeList(MODETYPE_USER))
289 std::string diffIneed, diffUneed;
290 ListDifference(capab->UserModes, BuildModeList(MODETYPE_USER), ' ', diffIneed, diffUneed);
291 if (diffIneed.length() || diffUneed.length())
293 reason = "User modes not matched on these servers.";
294 if (diffIneed.length())
295 reason += " Not loaded here:" + diffIneed;
296 if (diffUneed.length())
297 reason += " Not loaded there:" + diffUneed;
301 else if (this->capab->CapKeys.find("USERMODES") != this->capab->CapKeys.end())
303 if (this->capab->CapKeys.find("USERMODES")->second != ServerInstance->Modes->GiveModeList(MASK_USER))
304 reason = "One or more of the user modes on the remote server are invalid on this server.";
307 /* Challenge response, store their challenge for our password */
308 std::map<std::string,std::string>::iterator n = this->capab->CapKeys.find("CHALLENGE");
309 if (Utils->ChallengeResponse && (n != this->capab->CapKeys.end()) && (ServerInstance->Modules->Find("m_sha256.so")))
311 /* Challenge-response is on now */
312 this->SetTheirChallenge(n->second);
313 if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
315 this->SendCapabilities(2);
316 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+this->MakePass(capab->link->SendPass, capab->theirchallenge)+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
321 /* They didnt specify a challenge or we don't have m_sha256.so, we use plaintext */
322 if (this->LinkState == CONNECTING)
324 this->SendCapabilities(2);
325 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+capab->link->SendPass+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
331 this->SendError("CAPAB negotiation failed: "+reason);
335 else if ((params[0] == "MODULES") && (params.size() == 2))
337 if (!capab->ModuleList.length())
339 capab->ModuleList = params[1];
343 capab->ModuleList.push_back(' ');
344 capab->ModuleList.append(params[1]);
347 else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
349 if (!capab->OptModuleList.length())
351 capab->OptModuleList = params[1];
355 capab->OptModuleList.push_back(' ');
356 capab->OptModuleList.append(params[1]);
359 else if ((params[0] == "CHANMODES") && (params.size() == 2))
361 capab->ChanModes = params[1];
363 else if ((params[0] == "USERMODES") && (params.size() == 2))
365 capab->UserModes = params[1];
367 else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
369 irc::tokenstream capabs(params[1]);
371 while (capabs.GetToken(item))
373 /* Process each key/value pair */
374 std::string::size_type equals = item.find('=');
375 if (equals != std::string::npos)
377 std::string var = item.substr(0, equals);
378 std::string value = item.substr(equals+1, item.length());
379 capab->CapKeys[var] = value;