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"
32 std::string TreeSocket::MyModules(int filter)
34 std::vector<std::string> modlist = ServerInstance->Modules->GetAllModuleNames(filter);
36 if (filter == VF_COMMON && proto_version != ProtocolVersion)
37 CompatAddModules(modlist);
39 std::string capabilities;
40 sort(modlist.begin(),modlist.end());
41 for (std::vector<std::string>::const_iterator i = modlist.begin(); i != modlist.end(); ++i)
43 if (i != modlist.begin())
44 capabilities.push_back(proto_version > 1201 ? ' ' : ',');
45 capabilities.append(*i);
46 Module* m = ServerInstance->Modules->Find(*i);
47 if (m && proto_version > 1201)
49 Version v = m->GetVersion();
50 if (!v.link_data.empty())
52 capabilities.push_back('=');
53 capabilities.append(v.link_data);
60 static std::string BuildModeList(ModeType type)
62 std::vector<std::string> modes;
63 for(char c='A'; c <= 'z'; c++)
65 ModeHandler* mh = ServerInstance->Modes->FindMode(c, type);
68 std::string mdesc = mh->name;
71 mdesc.push_back(mh->GetPrefix());
72 if (mh->GetModeChar())
73 mdesc.push_back(mh->GetModeChar());
74 modes.push_back(mdesc);
77 sort(modes.begin(), modes.end());
78 irc::stringjoiner line(" ", modes, 0, modes.size() - 1);
79 return line.GetJoined();
82 void TreeSocket::SendCapabilities(int phase)
84 if (capab->capab_phase >= phase)
87 if (capab->capab_phase < 1 && phase >= 1)
88 WriteLine("CAPAB START " + ConvToStr(ProtocolVersion));
90 capab->capab_phase = phase;
94 char sep = proto_version > 1201 ? ' ' : ',';
95 irc::sepstream modulelist(MyModules(VF_COMMON), sep);
96 irc::sepstream optmodulelist(MyModules(VF_OPTCOMMON), sep);
97 /* Send module names, split at 509 length */
99 std::string line = "CAPAB MODULES :";
100 while (modulelist.GetToken(item))
102 if (line.length() + item.length() + 1 > 509)
104 this->WriteLine(line);
105 line = "CAPAB MODULES :";
108 if (line != "CAPAB MODULES :")
113 if (line != "CAPAB MODULES :")
114 this->WriteLine(line);
116 line = "CAPAB MODSUPPORT :";
117 while (optmodulelist.GetToken(item))
119 if (line.length() + item.length() + 1 > 509)
121 this->WriteLine(line);
122 line = "CAPAB MODSUPPORT :";
125 if (line != "CAPAB MODSUPPORT :")
130 if (line != "CAPAB MODSUPPORT :")
131 this->WriteLine(line);
133 WriteLine("CAPAB CHANMODES :" + BuildModeList(MODETYPE_CHANNEL));
134 WriteLine("CAPAB USERMODES :" + BuildModeList(MODETYPE_USER));
137 /* Do we have sha256 available? If so, we send a challenge */
138 if (Utils->ChallengeResponse && (ServerInstance->Modules->FindDataService<HashProvider>("hash/sha256")))
140 SetOurChallenge(ServerInstance->GenRandomStr(20));
141 extra = " CHALLENGE=" + this->GetOurChallenge();
143 if (proto_version < 1202)
144 extra += ServerInstance->Modes->FindMode('h', MODETYPE_CHANNEL) ? " HALFOP=1" : " HALFOP=0";
146 this->WriteLine("CAPAB CAPABILITIES " /* Preprocessor does this one. */
147 ":NICKMAX="+ConvToStr(ServerInstance->Config->Limits.NickMax)+
148 " CHANMAX="+ConvToStr(ServerInstance->Config->Limits.ChanMax)+
149 " MAXMODES="+ConvToStr(ServerInstance->Config->Limits.MaxModes)+
150 " IDENTMAX="+ConvToStr(ServerInstance->Config->Limits.IdentMax)+
151 " MAXQUIT="+ConvToStr(ServerInstance->Config->Limits.MaxQuit)+
152 " MAXTOPIC="+ConvToStr(ServerInstance->Config->Limits.MaxTopic)+
153 " MAXKICK="+ConvToStr(ServerInstance->Config->Limits.MaxKick)+
154 " MAXGECOS="+ConvToStr(ServerInstance->Config->Limits.MaxGecos)+
155 " MAXAWAY="+ConvToStr(ServerInstance->Config->Limits.MaxAway)+
157 " PROTOCOL="+ConvToStr(ProtocolVersion)+extra+
158 " PREFIX="+ServerInstance->Modes->BuildPrefixes()+
159 " CHANMODES="+ServerInstance->Modes->GiveModeList(MASK_CHANNEL)+
160 " USERMODES="+ServerInstance->Modes->GiveModeList(MASK_USER)+
161 // XXX: Advertise the presence or absence of m_globops in CAPAB CAPABILITIES.
162 // Services want to know about it, and since m_globops was not marked as VF_(OPT)COMMON
163 // in 2.0, we advertise it here to not break linking to previous versions.
164 // Protocol version 1201 (1.2) does not have this issue because we advertise m_globops
165 // to 1201 protocol servers irrespectively of its module flags.
166 (ServerInstance->Modules->Find("m_globops.so") != NULL ? " GLOBOPS=1" : " GLOBOPS=0")+
169 this->WriteLine("CAPAB END");
172 /* Isolate and return the elements that are different between two comma seperated lists */
173 void TreeSocket::ListDifference(const std::string &one, const std::string &two, char sep,
174 std::string& mleft, std::string& mright)
176 std::set<std::string> values;
177 irc::sepstream sepleft(one, sep);
178 irc::sepstream sepright(two, sep);
180 while (sepleft.GetToken(item))
184 while (sepright.GetToken(item))
186 if (!values.erase(item))
188 mright.push_back(sep);
192 for(std::set<std::string>::iterator i = values.begin(); i != values.end(); ++i)
194 mleft.push_back(sep);
199 bool TreeSocket::Capab(const parameterlist ¶ms)
201 if (params.size() < 1)
203 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
206 if (params[0] == "START")
208 capab->ModuleList.clear();
209 capab->OptModuleList.clear();
210 capab->CapKeys.clear();
211 if (params.size() > 1)
212 proto_version = atoi(params[1].c_str());
215 else if (params[0] == "END")
218 /* Compare ModuleList and check CapKeys */
219 if ((this->capab->ModuleList != this->MyModules(VF_COMMON)) && (this->capab->ModuleList.length()))
221 std::string diffIneed, diffUneed;
222 ListDifference(this->capab->ModuleList, this->MyModules(VF_COMMON), proto_version > 1201 ? ' ' : ',', diffIneed, diffUneed);
223 if (diffIneed.length() || diffUneed.length())
225 reason = "Modules incorrectly matched on these servers.";
226 if (diffIneed.length())
227 reason += " Not loaded here:" + diffIneed;
228 if (diffUneed.length())
229 reason += " Not loaded there:" + diffUneed;
230 this->SendError("CAPAB negotiation failed: "+reason);
234 if (this->capab->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->capab->OptModuleList.length())
236 std::string diffIneed, diffUneed;
237 ListDifference(this->capab->OptModuleList, this->MyModules(VF_OPTCOMMON), ' ', diffIneed, diffUneed);
238 if (diffIneed.length() || diffUneed.length())
240 if (Utils->AllowOptCommon)
242 ServerInstance->SNO->WriteToSnoMask('l',
243 "Optional module lists do not match, some commands may not work globally.%s%s%s%s",
244 diffIneed.length() ? " Not loaded here:" : "", diffIneed.c_str(),
245 diffUneed.length() ? " Not loaded there:" : "", diffUneed.c_str());
249 reason = "Optional modules incorrectly matched on these servers, and options::allowmismatch not set.";
250 if (diffIneed.length())
251 reason += " Not loaded here:" + diffIneed;
252 if (diffUneed.length())
253 reason += " Not loaded there:" + diffUneed;
254 this->SendError("CAPAB negotiation failed: "+reason);
260 if (this->capab->CapKeys.find("PROTOCOL") == this->capab->CapKeys.end())
262 reason = "Protocol version not specified";
266 proto_version = atoi(capab->CapKeys.find("PROTOCOL")->second.c_str());
267 if (proto_version < MinCompatProtocol)
269 reason = "Server is using protocol version " + ConvToStr(proto_version) +
270 " which is too old to link with this server (version " + ConvToStr(ProtocolVersion)
271 + (ProtocolVersion != MinCompatProtocol ? ", links with " + ConvToStr(MinCompatProtocol) + " and above)" : ")");
275 if(this->capab->CapKeys.find("PREFIX") != this->capab->CapKeys.end() && this->capab->CapKeys.find("PREFIX")->second != ServerInstance->Modes->BuildPrefixes())
276 reason = "One or more of the prefixes on the remote server are invalid on this server.";
278 if (!capab->ChanModes.empty())
280 if (capab->ChanModes != BuildModeList(MODETYPE_CHANNEL))
282 std::string diffIneed, diffUneed;
283 ListDifference(capab->ChanModes, BuildModeList(MODETYPE_CHANNEL), ' ', diffIneed, diffUneed);
284 if (diffIneed.length() || diffUneed.length())
286 reason = "Channel modes not matched on these servers.";
287 if (diffIneed.length())
288 reason += " Not loaded here:" + diffIneed;
289 if (diffUneed.length())
290 reason += " Not loaded there:" + diffUneed;
294 else if (this->capab->CapKeys.find("CHANMODES") != this->capab->CapKeys.end())
296 if (this->capab->CapKeys.find("CHANMODES")->second != ServerInstance->Modes->GiveModeList(MASK_CHANNEL))
297 reason = "One or more of the channel modes on the remote server are invalid on this server.";
300 if (!capab->UserModes.empty())
302 if (capab->UserModes != BuildModeList(MODETYPE_USER))
304 std::string diffIneed, diffUneed;
305 ListDifference(capab->UserModes, BuildModeList(MODETYPE_USER), ' ', diffIneed, diffUneed);
306 if (diffIneed.length() || diffUneed.length())
308 reason = "User modes not matched on these servers.";
309 if (diffIneed.length())
310 reason += " Not loaded here:" + diffIneed;
311 if (diffUneed.length())
312 reason += " Not loaded there:" + diffUneed;
316 else if (this->capab->CapKeys.find("USERMODES") != this->capab->CapKeys.end())
318 if (this->capab->CapKeys.find("USERMODES")->second != ServerInstance->Modes->GiveModeList(MASK_USER))
319 reason = "One or more of the user modes on the remote server are invalid on this server.";
322 /* Challenge response, store their challenge for our password */
323 std::map<std::string,std::string>::iterator n = this->capab->CapKeys.find("CHALLENGE");
324 if (Utils->ChallengeResponse && (n != this->capab->CapKeys.end()) && (ServerInstance->Modules->FindDataService<HashProvider>("hash/sha256")))
326 /* Challenge-response is on now */
327 this->SetTheirChallenge(n->second);
328 if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
330 this->SendCapabilities(2);
331 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+this->MakePass(capab->link->SendPass, capab->theirchallenge)+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
336 /* They didnt specify a challenge or we don't have m_sha256.so, we use plaintext */
337 if (this->LinkState == CONNECTING)
339 this->SendCapabilities(2);
340 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+capab->link->SendPass+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
346 this->SendError("CAPAB negotiation failed: "+reason);
350 else if ((params[0] == "MODULES") && (params.size() == 2))
352 if (!capab->ModuleList.length())
354 capab->ModuleList = params[1];
358 capab->ModuleList.push_back(proto_version > 1201 ? ' ' : ',');
359 capab->ModuleList.append(params[1]);
362 else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
364 if (!capab->OptModuleList.length())
366 capab->OptModuleList = params[1];
370 capab->OptModuleList.push_back(' ');
371 capab->OptModuleList.append(params[1]);
374 else if ((params[0] == "CHANMODES") && (params.size() == 2))
376 capab->ChanModes = params[1];
378 else if ((params[0] == "USERMODES") && (params.size() == 2))
380 capab->UserModes = params[1];
382 else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
384 irc::tokenstream capabs(params[1]);
386 while (capabs.GetToken(item))
388 /* Process each key/value pair */
389 std::string::size_type equals = item.find('=');
390 if (equals != std::string::npos)
392 std::string var = item.substr(0, equals);
393 std::string value = item.substr(equals+1, item.length());
394 capab->CapKeys[var] = value;