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)+
160 // XXX: Advertise the presence or absence of m_globops in CAPAB CAPABILITIES.
161 // Services want to know about it, and since m_globops was not marked as VF_(OPT)COMMON
162 // in 2.0, we advertise it here to not break linking to previous versions.
163 // Protocol version 1201 (1.2) does not have this issue because we advertise m_globops
164 // to 1201 protocol servers irrespectively of its module flags.
165 (ServerInstance->Modules->Find("m_globops.so") != NULL ? " GLOBOPS=1" : " GLOBOPS=0")+
168 this->WriteLine("CAPAB END");
171 /* Isolate and return the elements that are different between two comma seperated lists */
172 void TreeSocket::ListDifference(const std::string &one, const std::string &two, char sep,
173 std::string& mleft, std::string& mright)
175 std::set<std::string> values;
176 irc::sepstream sepleft(one, sep);
177 irc::sepstream sepright(two, sep);
179 while (sepleft.GetToken(item))
183 while (sepright.GetToken(item))
185 if (!values.erase(item))
187 mright.push_back(sep);
191 for(std::set<std::string>::iterator i = values.begin(); i != values.end(); ++i)
193 mleft.push_back(sep);
198 bool TreeSocket::Capab(const parameterlist ¶ms)
200 if (params.size() < 1)
202 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
205 if (params[0] == "START")
207 capab->ModuleList.clear();
208 capab->OptModuleList.clear();
209 capab->CapKeys.clear();
210 if (params.size() > 1)
211 proto_version = atoi(params[1].c_str());
214 else if (params[0] == "END")
217 /* Compare ModuleList and check CapKeys */
218 if ((this->capab->ModuleList != this->MyModules(VF_COMMON)) && (this->capab->ModuleList.length()))
220 std::string diffIneed, diffUneed;
221 ListDifference(this->capab->ModuleList, this->MyModules(VF_COMMON), proto_version > 1201 ? ' ' : ',', diffIneed, diffUneed);
222 if (diffIneed.length() || diffUneed.length())
224 reason = "Modules incorrectly matched on these servers.";
225 if (diffIneed.length())
226 reason += " Not loaded here:" + diffIneed;
227 if (diffUneed.length())
228 reason += " Not loaded there:" + diffUneed;
229 this->SendError("CAPAB negotiation failed: "+reason);
233 if (this->capab->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->capab->OptModuleList.length())
235 std::string diffIneed, diffUneed;
236 ListDifference(this->capab->OptModuleList, this->MyModules(VF_OPTCOMMON), ' ', diffIneed, diffUneed);
237 if (diffIneed.length() || diffUneed.length())
239 if (Utils->AllowOptCommon)
241 ServerInstance->SNO->WriteToSnoMask('l',
242 "Optional module lists do not match, some commands may not work globally.%s%s%s%s",
243 diffIneed.length() ? " Not loaded here:" : "", diffIneed.c_str(),
244 diffUneed.length() ? " Not loaded there:" : "", diffUneed.c_str());
248 reason = "Optional modules incorrectly matched on these servers, and options::allowmismatch not set.";
249 if (diffIneed.length())
250 reason += " Not loaded here:" + diffIneed;
251 if (diffUneed.length())
252 reason += " Not loaded there:" + diffUneed;
253 this->SendError("CAPAB negotiation failed: "+reason);
259 if (this->capab->CapKeys.find("PROTOCOL") == this->capab->CapKeys.end())
261 reason = "Protocol version not specified";
265 proto_version = atoi(capab->CapKeys.find("PROTOCOL")->second.c_str());
266 if (proto_version < MinCompatProtocol)
268 reason = "Server is using protocol version " + ConvToStr(proto_version) +
269 " which is too old to link with this server (version " + ConvToStr(ProtocolVersion)
270 + (ProtocolVersion != MinCompatProtocol ? ", links with " + ConvToStr(MinCompatProtocol) + " and above)" : ")");
274 if(this->capab->CapKeys.find("PREFIX") != this->capab->CapKeys.end() && this->capab->CapKeys.find("PREFIX")->second != ServerInstance->Modes->BuildPrefixes())
275 reason = "One or more of the prefixes on the remote server are invalid on this server.";
277 if (!capab->ChanModes.empty())
279 if (capab->ChanModes != BuildModeList(MODETYPE_CHANNEL))
281 std::string diffIneed, diffUneed;
282 ListDifference(capab->ChanModes, BuildModeList(MODETYPE_CHANNEL), ' ', diffIneed, diffUneed);
283 if (diffIneed.length() || diffUneed.length())
285 reason = "Channel modes not matched on these servers.";
286 if (diffIneed.length())
287 reason += " Not loaded here:" + diffIneed;
288 if (diffUneed.length())
289 reason += " Not loaded there:" + diffUneed;
293 else if (this->capab->CapKeys.find("CHANMODES") != this->capab->CapKeys.end())
295 if (this->capab->CapKeys.find("CHANMODES")->second != ServerInstance->Modes->GiveModeList(MASK_CHANNEL))
296 reason = "One or more of the channel modes on the remote server are invalid on this server.";
299 if (!capab->UserModes.empty())
301 if (capab->UserModes != BuildModeList(MODETYPE_USER))
303 std::string diffIneed, diffUneed;
304 ListDifference(capab->UserModes, BuildModeList(MODETYPE_USER), ' ', diffIneed, diffUneed);
305 if (diffIneed.length() || diffUneed.length())
307 reason = "User modes not matched on these servers.";
308 if (diffIneed.length())
309 reason += " Not loaded here:" + diffIneed;
310 if (diffUneed.length())
311 reason += " Not loaded there:" + diffUneed;
315 else if (this->capab->CapKeys.find("USERMODES") != this->capab->CapKeys.end())
317 if (this->capab->CapKeys.find("USERMODES")->second != ServerInstance->Modes->GiveModeList(MASK_USER))
318 reason = "One or more of the user modes on the remote server are invalid on this server.";
321 /* Challenge response, store their challenge for our password */
322 std::map<std::string,std::string>::iterator n = this->capab->CapKeys.find("CHALLENGE");
323 if (Utils->ChallengeResponse && (n != this->capab->CapKeys.end()) && (ServerInstance->Modules->Find("m_sha256.so")))
325 /* Challenge-response is on now */
326 this->SetTheirChallenge(n->second);
327 if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
329 this->SendCapabilities(2);
330 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+this->MakePass(capab->link->SendPass, capab->theirchallenge)+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
335 /* They didnt specify a challenge or we don't have m_sha256.so, we use plaintext */
336 if (this->LinkState == CONNECTING)
338 this->SendCapabilities(2);
339 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+capab->link->SendPass+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
345 this->SendError("CAPAB negotiation failed: "+reason);
349 else if ((params[0] == "MODULES") && (params.size() == 2))
351 if (!capab->ModuleList.length())
353 capab->ModuleList = params[1];
357 capab->ModuleList.push_back(proto_version > 1201 ? ' ' : ',');
358 capab->ModuleList.append(params[1]);
361 else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
363 if (!capab->OptModuleList.length())
365 capab->OptModuleList = params[1];
369 capab->OptModuleList.push_back(' ');
370 capab->OptModuleList.append(params[1]);
373 else if ((params[0] == "CHANMODES") && (params.size() == 2))
375 capab->ChanModes = params[1];
377 else if ((params[0] == "USERMODES") && (params.size() == 2))
379 capab->UserModes = params[1];
381 else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
383 irc::tokenstream capabs(params[1]);
385 while (capabs.GetToken(item))
387 /* Process each key/value pair */
388 std::string::size_type equals = item.find('=');
389 if (equals != std::string::npos)
391 std::string var = item.substr(0, equals);
392 std::string value = item.substr(equals+1, item.length());
393 capab->CapKeys[var] = value;