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 if (filter == VF_COMMON && proto_version != ProtocolVersion)
36 CompatAddModules(modlist);
38 std::string capabilities;
39 sort(modlist.begin(),modlist.end());
40 for (std::vector<std::string>::const_iterator i = modlist.begin(); i != modlist.end(); ++i)
42 if (i != modlist.begin())
43 capabilities.push_back(proto_version > 1201 ? ' ' : ',');
44 capabilities.append(*i);
45 Module* m = ServerInstance->Modules->Find(*i);
46 if (m && proto_version > 1201)
48 Version v = m->GetVersion();
49 if (!v.link_data.empty())
51 capabilities.push_back('=');
52 capabilities.append(v.link_data);
59 static std::string BuildModeList(ModeType type)
61 std::vector<std::string> modes;
62 for(char c='A'; c <= 'z'; c++)
64 ModeHandler* mh = ServerInstance->Modes->FindMode(c, type);
67 std::string mdesc = mh->name;
70 mdesc.push_back(mh->GetPrefix());
71 if (mh->GetModeChar())
72 mdesc.push_back(mh->GetModeChar());
73 modes.push_back(mdesc);
76 sort(modes.begin(), modes.end());
77 irc::stringjoiner line(" ", modes, 0, modes.size() - 1);
78 return line.GetJoined();
81 void TreeSocket::SendCapabilities(int phase)
83 if (capab->capab_phase >= phase)
86 if (capab->capab_phase < 1 && phase >= 1)
87 WriteLine("CAPAB START " + ConvToStr(ProtocolVersion));
89 capab->capab_phase = phase;
93 char sep = proto_version > 1201 ? ' ' : ',';
94 irc::sepstream modulelist(MyModules(VF_COMMON), sep);
95 irc::sepstream optmodulelist(MyModules(VF_OPTCOMMON), sep);
96 /* Send module names, split at 509 length */
98 std::string line = "CAPAB MODULES :";
99 while (modulelist.GetToken(item))
101 if (line.length() + item.length() + 1 > 509)
103 this->WriteLine(line);
104 line = "CAPAB MODULES :";
107 if (line != "CAPAB MODULES :")
112 if (line != "CAPAB MODULES :")
113 this->WriteLine(line);
115 line = "CAPAB MODSUPPORT :";
116 while (optmodulelist.GetToken(item))
118 if (line.length() + item.length() + 1 > 509)
120 this->WriteLine(line);
121 line = "CAPAB MODSUPPORT :";
124 if (line != "CAPAB MODSUPPORT :")
129 if (line != "CAPAB MODSUPPORT :")
130 this->WriteLine(line);
132 WriteLine("CAPAB CHANMODES :" + BuildModeList(MODETYPE_CHANNEL));
133 WriteLine("CAPAB USERMODES :" + BuildModeList(MODETYPE_USER));
136 /* Do we have sha256 available? If so, we send a challenge */
137 if (Utils->ChallengeResponse && (ServerInstance->Modules->Find("m_sha256.so")))
139 SetOurChallenge(ServerInstance->GenRandomStr(20));
140 extra = " CHALLENGE=" + this->GetOurChallenge();
142 if (proto_version < 1202)
143 extra += ServerInstance->Modes->FindMode('h', MODETYPE_CHANNEL) ? " HALFOP=1" : " HALFOP=0";
145 this->WriteLine("CAPAB CAPABILITIES " /* Preprocessor does this one. */
146 ":NICKMAX="+ConvToStr(ServerInstance->Config->Limits.NickMax)+
147 " CHANMAX="+ConvToStr(ServerInstance->Config->Limits.ChanMax)+
148 " MAXMODES="+ConvToStr(ServerInstance->Config->Limits.MaxModes)+
149 " IDENTMAX="+ConvToStr(ServerInstance->Config->Limits.IdentMax)+
150 " MAXQUIT="+ConvToStr(ServerInstance->Config->Limits.MaxQuit)+
151 " MAXTOPIC="+ConvToStr(ServerInstance->Config->Limits.MaxTopic)+
152 " MAXKICK="+ConvToStr(ServerInstance->Config->Limits.MaxKick)+
153 " MAXGECOS="+ConvToStr(ServerInstance->Config->Limits.MaxGecos)+
154 " MAXAWAY="+ConvToStr(ServerInstance->Config->Limits.MaxAway)+
156 " PROTOCOL="+ConvToStr(ProtocolVersion)+extra+
157 " PREFIX="+ServerInstance->Modes->BuildPrefixes()+
158 " CHANMODES="+ServerInstance->Modes->GiveModeList(MASK_CHANNEL)+
159 " USERMODES="+ServerInstance->Modes->GiveModeList(MASK_USER)+
162 this->WriteLine("CAPAB END");
165 /* Isolate and return the elements that are different between two comma seperated lists */
166 void TreeSocket::ListDifference(const std::string &one, const std::string &two, char sep,
167 std::string& mleft, std::string& mright)
169 std::set<std::string> values;
170 irc::sepstream sepleft(one, sep);
171 irc::sepstream sepright(two, sep);
173 while (sepleft.GetToken(item))
177 while (sepright.GetToken(item))
179 if (!values.erase(item))
181 mright.push_back(sep);
185 for(std::set<std::string>::iterator i = values.begin(); i != values.end(); ++i)
187 mleft.push_back(sep);
192 bool TreeSocket::Capab(const parameterlist ¶ms)
194 if (params.size() < 1)
196 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
199 if (params[0] == "START")
201 capab->ModuleList.clear();
202 capab->OptModuleList.clear();
203 capab->CapKeys.clear();
204 if (params.size() > 1)
205 proto_version = atoi(params[1].c_str());
208 else if (params[0] == "END")
211 /* Compare ModuleList and check CapKeys */
212 if ((this->capab->ModuleList != this->MyModules(VF_COMMON)) && (this->capab->ModuleList.length()))
214 std::string diffIneed, diffUneed;
215 ListDifference(this->capab->ModuleList, this->MyModules(VF_COMMON), proto_version > 1201 ? ' ' : ',', diffIneed, diffUneed);
216 if (diffIneed.length() || diffUneed.length())
218 reason = "Modules incorrectly matched on these servers.";
219 if (diffIneed.length())
220 reason += " Not loaded here:" + diffIneed;
221 if (diffUneed.length())
222 reason += " Not loaded there:" + diffUneed;
223 this->SendError("CAPAB negotiation failed: "+reason);
227 if (this->capab->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->capab->OptModuleList.length())
229 std::string diffIneed, diffUneed;
230 ListDifference(this->capab->OptModuleList, this->MyModules(VF_OPTCOMMON), ' ', diffIneed, diffUneed);
231 if (diffIneed.length() || diffUneed.length())
233 if (Utils->AllowOptCommon)
235 ServerInstance->SNO->WriteToSnoMask('l',
236 "Optional module lists do not match, some commands may not work globally.%s%s%s%s",
237 diffIneed.length() ? " Not loaded here:" : "", diffIneed.c_str(),
238 diffUneed.length() ? " Not loaded there:" : "", diffUneed.c_str());
242 reason = "Optional modules incorrectly matched on these servers, and options::allowmismatch not set.";
243 if (diffIneed.length())
244 reason += " Not loaded here:" + diffIneed;
245 if (diffUneed.length())
246 reason += " Not loaded there:" + diffUneed;
247 this->SendError("CAPAB negotiation failed: "+reason);
253 if (this->capab->CapKeys.find("PROTOCOL") == this->capab->CapKeys.end())
255 reason = "Protocol version not specified";
259 proto_version = atoi(capab->CapKeys.find("PROTOCOL")->second.c_str());
260 if (proto_version < MinCompatProtocol)
262 reason = "Server is using protocol version " + ConvToStr(proto_version) +
263 " which is too old to link with this server (version " + ConvToStr(ProtocolVersion)
264 + (ProtocolVersion != MinCompatProtocol ? ", links with " + ConvToStr(MinCompatProtocol) + " and above)" : ")");
268 if(this->capab->CapKeys.find("PREFIX") != this->capab->CapKeys.end() && this->capab->CapKeys.find("PREFIX")->second != ServerInstance->Modes->BuildPrefixes())
269 reason = "One or more of the prefixes on the remote server are invalid on this server.";
271 if (!capab->ChanModes.empty())
273 if (capab->ChanModes != BuildModeList(MODETYPE_CHANNEL))
275 std::string diffIneed, diffUneed;
276 ListDifference(capab->ChanModes, BuildModeList(MODETYPE_CHANNEL), ' ', diffIneed, diffUneed);
277 if (diffIneed.length() || diffUneed.length())
279 reason = "Channel modes not matched on these servers.";
280 if (diffIneed.length())
281 reason += " Not loaded here:" + diffIneed;
282 if (diffUneed.length())
283 reason += " Not loaded there:" + diffUneed;
287 else if (this->capab->CapKeys.find("CHANMODES") != this->capab->CapKeys.end())
289 if (this->capab->CapKeys.find("CHANMODES")->second != ServerInstance->Modes->GiveModeList(MASK_CHANNEL))
290 reason = "One or more of the channel modes on the remote server are invalid on this server.";
293 if (!capab->UserModes.empty())
295 if (capab->UserModes != BuildModeList(MODETYPE_USER))
297 std::string diffIneed, diffUneed;
298 ListDifference(capab->UserModes, BuildModeList(MODETYPE_USER), ' ', diffIneed, diffUneed);
299 if (diffIneed.length() || diffUneed.length())
301 reason = "User modes not matched on these servers.";
302 if (diffIneed.length())
303 reason += " Not loaded here:" + diffIneed;
304 if (diffUneed.length())
305 reason += " Not loaded there:" + diffUneed;
309 else if (this->capab->CapKeys.find("USERMODES") != this->capab->CapKeys.end())
311 if (this->capab->CapKeys.find("USERMODES")->second != ServerInstance->Modes->GiveModeList(MASK_USER))
312 reason = "One or more of the user modes on the remote server are invalid on this server.";
315 /* Challenge response, store their challenge for our password */
316 std::map<std::string,std::string>::iterator n = this->capab->CapKeys.find("CHALLENGE");
317 if (Utils->ChallengeResponse && (n != this->capab->CapKeys.end()) && (ServerInstance->Modules->Find("m_sha256.so")))
319 /* Challenge-response is on now */
320 this->SetTheirChallenge(n->second);
321 if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
323 this->SendCapabilities(2);
324 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+this->MakePass(capab->link->SendPass, capab->theirchallenge)+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
329 /* They didnt specify a challenge or we don't have m_sha256.so, we use plaintext */
330 if (this->LinkState == CONNECTING)
332 this->SendCapabilities(2);
333 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+capab->link->SendPass+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
339 this->SendError("CAPAB negotiation failed: "+reason);
343 else if ((params[0] == "MODULES") && (params.size() == 2))
345 if (!capab->ModuleList.length())
347 capab->ModuleList = params[1];
351 capab->ModuleList.push_back(proto_version > 1201 ? ' ' : ',');
352 capab->ModuleList.append(params[1]);
355 else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
357 if (!capab->OptModuleList.length())
359 capab->OptModuleList = params[1];
363 capab->OptModuleList.push_back(' ');
364 capab->OptModuleList.append(params[1]);
367 else if ((params[0] == "CHANMODES") && (params.size() == 2))
369 capab->ChanModes = params[1];
371 else if ((params[0] == "USERMODES") && (params.size() == 2))
373 capab->UserModes = params[1];
375 else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
377 irc::tokenstream capabs(params[1]);
379 while (capabs.GetToken(item))
381 /* Process each key/value pair */
382 std::string::size_type equals = item.find('=');
383 if (equals != std::string::npos)
385 std::string var = item.substr(0, equals);
386 std::string value = item.substr(equals+1, item.length());
387 capab->CapKeys[var] = value;