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(' ');
44 capabilities.append(*i);
45 Module* m = ServerInstance->Modules->Find(*i);
46 Version v = m->GetVersion();
47 if (!v.link_data.empty())
49 capabilities.push_back('=');
50 capabilities.append(v.link_data);
56 static std::string BuildModeList(ModeType type)
58 std::vector<std::string> modes;
59 for(char c='A'; c <= 'z'; c++)
61 ModeHandler* mh = ServerInstance->Modes->FindMode(c, type);
64 std::string mdesc = mh->name;
67 mdesc.push_back(mh->GetPrefix());
68 if (mh->GetModeChar())
69 mdesc.push_back(mh->GetModeChar());
70 modes.push_back(mdesc);
73 sort(modes.begin(), modes.end());
74 irc::stringjoiner line(" ", modes, 0, modes.size() - 1);
75 return line.GetJoined();
78 void TreeSocket::SendCapabilities(int phase)
80 if (capab->capab_phase >= phase)
83 if (capab->capab_phase < 1 && phase >= 1)
84 WriteLine("CAPAB START " + ConvToStr(ProtocolVersion));
86 capab->capab_phase = phase;
91 irc::sepstream modulelist(MyModules(VF_COMMON), sep);
92 irc::sepstream optmodulelist(MyModules(VF_OPTCOMMON), sep);
93 /* Send module names, split at 509 length */
95 std::string line = "CAPAB MODULES :";
96 while (modulelist.GetToken(item))
98 if (line.length() + item.length() + 1 > 509)
100 this->WriteLine(line);
101 line = "CAPAB MODULES :";
104 if (line != "CAPAB MODULES :")
109 if (line != "CAPAB MODULES :")
110 this->WriteLine(line);
112 line = "CAPAB MODSUPPORT :";
113 while (optmodulelist.GetToken(item))
115 if (line.length() + item.length() + 1 > 509)
117 this->WriteLine(line);
118 line = "CAPAB MODSUPPORT :";
121 if (line != "CAPAB MODSUPPORT :")
126 if (line != "CAPAB MODSUPPORT :")
127 this->WriteLine(line);
129 WriteLine("CAPAB CHANMODES :" + BuildModeList(MODETYPE_CHANNEL));
130 WriteLine("CAPAB USERMODES :" + BuildModeList(MODETYPE_USER));
133 /* Do we have sha256 available? If so, we send a challenge */
134 if (Utils->ChallengeResponse && (ServerInstance->Modules->Find("m_sha256.so")))
136 SetOurChallenge(ServerInstance->GenRandomStr(20));
137 extra = " CHALLENGE=" + this->GetOurChallenge();
140 this->WriteLine("CAPAB CAPABILITIES " /* Preprocessor does this one. */
141 ":NICKMAX="+ConvToStr(ServerInstance->Config->Limits.NickMax)+
142 " CHANMAX="+ConvToStr(ServerInstance->Config->Limits.ChanMax)+
143 " MAXMODES="+ConvToStr(ServerInstance->Config->Limits.MaxModes)+
144 " IDENTMAX="+ConvToStr(ServerInstance->Config->Limits.IdentMax)+
145 " MAXQUIT="+ConvToStr(ServerInstance->Config->Limits.MaxQuit)+
146 " MAXTOPIC="+ConvToStr(ServerInstance->Config->Limits.MaxTopic)+
147 " MAXKICK="+ConvToStr(ServerInstance->Config->Limits.MaxKick)+
148 " MAXGECOS="+ConvToStr(ServerInstance->Config->Limits.MaxGecos)+
149 " MAXAWAY="+ConvToStr(ServerInstance->Config->Limits.MaxAway)+
151 " PROTOCOL="+ConvToStr(ProtocolVersion)+extra+
152 " PREFIX="+ServerInstance->Modes->BuildPrefixes()+
153 " CHANMODES="+ServerInstance->Modes->GiveModeList(MASK_CHANNEL)+
154 " USERMODES="+ServerInstance->Modes->GiveModeList(MASK_USER)+
157 this->WriteLine("CAPAB END");
160 /* Isolate and return the elements that are different between two comma seperated lists */
161 void TreeSocket::ListDifference(const std::string &one, const std::string &two, char sep,
162 std::string& mleft, std::string& mright)
164 std::set<std::string> values;
165 irc::sepstream sepleft(one, sep);
166 irc::sepstream sepright(two, sep);
168 while (sepleft.GetToken(item))
172 while (sepright.GetToken(item))
174 if (!values.erase(item))
176 mright.push_back(sep);
180 for(std::set<std::string>::iterator i = values.begin(); i != values.end(); ++i)
182 mleft.push_back(sep);
187 bool TreeSocket::Capab(const parameterlist ¶ms)
189 if (params.size() < 1)
191 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
194 if (params[0] == "START")
196 capab->ModuleList.clear();
197 capab->OptModuleList.clear();
198 capab->CapKeys.clear();
199 if (params.size() > 1)
200 proto_version = atoi(params[1].c_str());
203 else if (params[0] == "END")
206 /* Compare ModuleList and check CapKeys */
207 if ((this->capab->ModuleList != this->MyModules(VF_COMMON)) && (this->capab->ModuleList.length()))
209 std::string diffIneed, diffUneed;
210 ListDifference(this->capab->ModuleList, this->MyModules(VF_COMMON), ' ', diffIneed, diffUneed);
211 if (diffIneed.length() || diffUneed.length())
213 reason = "Modules incorrectly matched on these servers.";
214 if (diffIneed.length())
215 reason += " Not loaded here:" + diffIneed;
216 if (diffUneed.length())
217 reason += " Not loaded there:" + diffUneed;
218 this->SendError("CAPAB negotiation failed: "+reason);
222 if (this->capab->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->capab->OptModuleList.length())
224 std::string diffIneed, diffUneed;
225 ListDifference(this->capab->OptModuleList, this->MyModules(VF_OPTCOMMON), ' ', diffIneed, diffUneed);
226 if (diffIneed.length() || diffUneed.length())
228 if (Utils->AllowOptCommon)
230 ServerInstance->SNO->WriteToSnoMask('l',
231 "Optional module lists do not match, some commands may not work globally.%s%s%s%s",
232 diffIneed.length() ? " Not loaded here:" : "", diffIneed.c_str(),
233 diffUneed.length() ? " Not loaded there:" : "", diffUneed.c_str());
237 reason = "Optional modules incorrectly matched on these servers, and options::allowmismatch not set.";
238 if (diffIneed.length())
239 reason += " Not loaded here:" + diffIneed;
240 if (diffUneed.length())
241 reason += " Not loaded there:" + diffUneed;
242 this->SendError("CAPAB negotiation failed: "+reason);
248 if (this->capab->CapKeys.find("PROTOCOL") == this->capab->CapKeys.end())
250 reason = "Protocol version not specified";
254 proto_version = atoi(capab->CapKeys.find("PROTOCOL")->second.c_str());
255 if (proto_version < MinCompatProtocol)
257 reason = "Server is using protocol version " + ConvToStr(proto_version) +
258 " which is too old to link with this server (version " + ConvToStr(ProtocolVersion)
259 + (ProtocolVersion != MinCompatProtocol ? ", links with " + ConvToStr(MinCompatProtocol) + " and above)" : ")");
263 if(this->capab->CapKeys.find("PREFIX") != this->capab->CapKeys.end() && this->capab->CapKeys.find("PREFIX")->second != ServerInstance->Modes->BuildPrefixes())
264 reason = "One or more of the prefixes on the remote server are invalid on this server.";
266 if (!capab->ChanModes.empty())
268 if (capab->ChanModes != BuildModeList(MODETYPE_CHANNEL))
270 std::string diffIneed, diffUneed;
271 ListDifference(capab->ChanModes, BuildModeList(MODETYPE_CHANNEL), ' ', diffIneed, diffUneed);
272 if (diffIneed.length() || diffUneed.length())
274 reason = "Channel modes not matched on these servers.";
275 if (diffIneed.length())
276 reason += " Not loaded here:" + diffIneed;
277 if (diffUneed.length())
278 reason += " Not loaded there:" + diffUneed;
282 else if (this->capab->CapKeys.find("CHANMODES") != this->capab->CapKeys.end())
284 if (this->capab->CapKeys.find("CHANMODES")->second != ServerInstance->Modes->GiveModeList(MASK_CHANNEL))
285 reason = "One or more of the channel modes on the remote server are invalid on this server.";
288 if (!capab->UserModes.empty())
290 if (capab->UserModes != BuildModeList(MODETYPE_USER))
292 std::string diffIneed, diffUneed;
293 ListDifference(capab->UserModes, BuildModeList(MODETYPE_USER), ' ', diffIneed, diffUneed);
294 if (diffIneed.length() || diffUneed.length())
296 reason = "User modes not matched on these servers.";
297 if (diffIneed.length())
298 reason += " Not loaded here:" + diffIneed;
299 if (diffUneed.length())
300 reason += " Not loaded there:" + diffUneed;
304 else if (this->capab->CapKeys.find("USERMODES") != this->capab->CapKeys.end())
306 if (this->capab->CapKeys.find("USERMODES")->second != ServerInstance->Modes->GiveModeList(MASK_USER))
307 reason = "One or more of the user modes on the remote server are invalid on this server.";
310 /* Challenge response, store their challenge for our password */
311 std::map<std::string,std::string>::iterator n = this->capab->CapKeys.find("CHALLENGE");
312 if (Utils->ChallengeResponse && (n != this->capab->CapKeys.end()) && (ServerInstance->Modules->Find("m_sha256.so")))
314 /* Challenge-response is on now */
315 this->SetTheirChallenge(n->second);
316 if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
318 this->SendCapabilities(2);
319 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+this->MakePass(capab->link->SendPass, capab->theirchallenge)+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
324 /* They didnt specify a challenge or we don't have m_sha256.so, we use plaintext */
325 if (this->LinkState == CONNECTING)
327 this->SendCapabilities(2);
328 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+capab->link->SendPass+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
334 this->SendError("CAPAB negotiation failed: "+reason);
338 else if ((params[0] == "MODULES") && (params.size() == 2))
340 if (!capab->ModuleList.length())
342 capab->ModuleList = params[1];
346 capab->ModuleList.push_back(' ');
347 capab->ModuleList.append(params[1]);
350 else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
352 if (!capab->OptModuleList.length())
354 capab->OptModuleList = params[1];
358 capab->OptModuleList.push_back(' ');
359 capab->OptModuleList.append(params[1]);
362 else if ((params[0] == "CHANMODES") && (params.size() == 2))
364 capab->ChanModes = params[1];
366 else if ((params[0] == "USERMODES") && (params.size() == 2))
368 capab->UserModes = params[1];
370 else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
372 irc::tokenstream capabs(params[1]);
374 while (capabs.GetToken(item))
376 /* Process each key/value pair */
377 std::string::size_type equals = item.find('=');
378 if (equals != std::string::npos)
380 std::string var = item.substr(0, equals);
381 std::string value = item.substr(equals+1, item.length());
382 capab->CapKeys[var] = value;