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/>.
24 #include "treeserver.h"
29 std::string TreeSocket::MyModules(int filter)
31 const ModuleManager::ModuleMap& modlist = ServerInstance->Modules->GetModules();
33 std::string capabilities;
34 for (ModuleManager::ModuleMap::const_iterator i = modlist.begin(); i != modlist.end(); ++i)
36 // 2.2 advertises its settings for the benefit of services
37 // 2.0 would bork on this
38 if (proto_version < 1205 && i->second->ModuleSourceFile == "m_kicknorejoin.so")
41 Version v = i->second->GetVersion();
42 if (!(v.Flags & filter))
45 if (i != modlist.begin())
46 capabilities.push_back(' ');
47 capabilities.append(i->first);
48 if (!v.link_data.empty())
50 capabilities.push_back('=');
51 capabilities.append(v.link_data);
57 static std::string BuildModeList(ModeType type)
59 std::vector<std::string> modes;
60 for(char c='A'; c <= 'z'; c++)
62 ModeHandler* mh = ServerInstance->Modes->FindMode(c, type);
65 std::string mdesc = mh->name;
67 PrefixMode* pm = mh->IsPrefixMode();
71 mdesc.push_back(pm->GetPrefix());
73 mdesc.push_back(mh->GetModeChar());
74 modes.push_back(mdesc);
77 std::sort(modes.begin(), modes.end());
78 return irc::stringjoiner(modes);
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;
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 (ServerInstance->Modules->FindService(SERVICE_DATA, "hash/sha256"))
139 SetOurChallenge(ServerInstance->GenRandomStr(20));
140 extra = " CHALLENGE=" + this->GetOurChallenge();
143 // 2.0 needs this key
144 if (proto_version == 1202)
145 extra.append(" PROTOCOL="+ConvToStr(ProtocolVersion));
147 this->WriteLine("CAPAB CAPABILITIES " /* Preprocessor does this one. */
148 ":NICKMAX="+ConvToStr(ServerInstance->Config->Limits.NickMax)+
149 " CHANMAX="+ConvToStr(ServerInstance->Config->Limits.ChanMax)+
150 " MAXMODES="+ConvToStr(ServerInstance->Config->Limits.MaxModes)+
151 " IDENTMAX="+ConvToStr(ServerInstance->Config->Limits.IdentMax)+
152 " MAXQUIT="+ConvToStr(ServerInstance->Config->Limits.MaxQuit)+
153 " MAXTOPIC="+ConvToStr(ServerInstance->Config->Limits.MaxTopic)+
154 " MAXKICK="+ConvToStr(ServerInstance->Config->Limits.MaxKick)+
155 " MAXGECOS="+ConvToStr(ServerInstance->Config->Limits.MaxGecos)+
156 " MAXAWAY="+ConvToStr(ServerInstance->Config->Limits.MaxAway)+
157 " MAXHOST="+ConvToStr(ServerInstance->Config->Limits.MaxHost)+
159 " PREFIX="+ServerInstance->Modes->BuildPrefixes()+
160 " CHANMODES="+ServerInstance->Modes->GiveModeList(MODETYPE_CHANNEL)+
161 " USERMODES="+ServerInstance->Modes->GiveModeList(MODETYPE_USER)+
162 // XXX: Advertise the presence or absence of m_globops in CAPAB CAPABILITIES.
163 // Services want to know about it, and since m_globops was not marked as VF_(OPT)COMMON
164 // in 2.0, we advertise it here to not break linking to previous versions.
165 // Protocol version 1201 (1.2) does not have this issue because we advertise m_globops
166 // to 1201 protocol servers irrespectively of its module flags.
167 (ServerInstance->Modules->Find("m_globops.so") != NULL ? " GLOBOPS=1" : " GLOBOPS=0")
170 this->WriteLine("CAPAB END");
173 /* Isolate and return the elements that are different between two comma seperated lists */
174 void TreeSocket::ListDifference(const std::string &one, const std::string &two, char sep,
175 std::string& mleft, std::string& mright)
177 std::set<std::string> values;
178 irc::sepstream sepleft(one, sep);
179 irc::sepstream sepright(two, sep);
181 while (sepleft.GetToken(item))
185 while (sepright.GetToken(item))
187 if (!values.erase(item))
189 mright.push_back(sep);
193 for(std::set<std::string>::iterator i = values.begin(); i != values.end(); ++i)
195 mleft.push_back(sep);
200 bool TreeSocket::Capab(const parameterlist ¶ms)
202 if (params.size() < 1)
204 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
207 if (params[0] == "START")
209 capab->ModuleList.clear();
210 capab->OptModuleList.clear();
211 capab->CapKeys.clear();
212 if (params.size() > 1)
213 proto_version = ConvToInt(params[1]);
215 if (proto_version < MinCompatProtocol)
217 SendError("CAPAB negotiation failed: Server is using protocol version " + (proto_version ? ConvToStr(proto_version) : "1201 or older")
218 + " which is too old to link with this server (version " + ConvToStr(ProtocolVersion)
219 + (ProtocolVersion != MinCompatProtocol ? ", links with " + ConvToStr(MinCompatProtocol) + " and above)" : ")"));
223 // Special case, may be removed in the future
224 if (proto_version == 1203 || proto_version == 1204)
226 SendError("CAPAB negotiation failed: InspIRCd 2.1 beta is not supported");
232 else if (params[0] == "END")
235 /* Compare ModuleList and check CapKeys */
236 if ((this->capab->ModuleList != this->MyModules(VF_COMMON)) && (this->capab->ModuleList.length()))
238 std::string diffIneed, diffUneed;
239 ListDifference(this->capab->ModuleList, this->MyModules(VF_COMMON), ' ', diffIneed, diffUneed);
240 if (diffIneed.length() || diffUneed.length())
242 reason = "Modules incorrectly matched on these servers.";
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);
251 if (this->capab->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->capab->OptModuleList.length())
253 std::string diffIneed, diffUneed;
254 ListDifference(this->capab->OptModuleList, this->MyModules(VF_OPTCOMMON), ' ', diffIneed, diffUneed);
255 if (diffIneed.length() || diffUneed.length())
257 if (Utils->AllowOptCommon)
259 ServerInstance->SNO->WriteToSnoMask('l',
260 "Optional module lists do not match, some commands may not work globally.%s%s%s%s",
261 diffIneed.length() ? " Not loaded here:" : "", diffIneed.c_str(),
262 diffUneed.length() ? " Not loaded there:" : "", diffUneed.c_str());
266 reason = "Optional modules incorrectly matched on these servers, and options::allowmismatch not set.";
267 if (diffIneed.length())
268 reason += " Not loaded here:" + diffIneed;
269 if (diffUneed.length())
270 reason += " Not loaded there:" + diffUneed;
271 this->SendError("CAPAB negotiation failed: "+reason);
277 if(this->capab->CapKeys.find("PREFIX") != this->capab->CapKeys.end() && this->capab->CapKeys.find("PREFIX")->second != ServerInstance->Modes->BuildPrefixes())
278 reason = "One or more of the prefixes on the remote server are invalid on this server.";
280 if (!capab->ChanModes.empty())
282 if (capab->ChanModes != BuildModeList(MODETYPE_CHANNEL))
284 std::string diffIneed, diffUneed;
285 ListDifference(capab->ChanModes, BuildModeList(MODETYPE_CHANNEL), ' ', diffIneed, diffUneed);
286 if (diffIneed.length() || diffUneed.length())
288 reason = "Channel modes not matched on these servers.";
289 if (diffIneed.length())
290 reason += " Not loaded here:" + diffIneed;
291 if (diffUneed.length())
292 reason += " Not loaded there:" + diffUneed;
296 else if (this->capab->CapKeys.find("CHANMODES") != this->capab->CapKeys.end())
298 if (this->capab->CapKeys.find("CHANMODES")->second != ServerInstance->Modes->GiveModeList(MODETYPE_CHANNEL))
299 reason = "One or more of the channel modes on the remote server are invalid on this server.";
302 if (!capab->UserModes.empty())
304 if (capab->UserModes != BuildModeList(MODETYPE_USER))
306 std::string diffIneed, diffUneed;
307 ListDifference(capab->UserModes, BuildModeList(MODETYPE_USER), ' ', diffIneed, diffUneed);
308 if (diffIneed.length() || diffUneed.length())
310 reason = "User modes not matched on these servers.";
311 if (diffIneed.length())
312 reason += " Not loaded here:" + diffIneed;
313 if (diffUneed.length())
314 reason += " Not loaded there:" + diffUneed;
318 else if (this->capab->CapKeys.find("USERMODES") != this->capab->CapKeys.end())
320 if (this->capab->CapKeys.find("USERMODES")->second != ServerInstance->Modes->GiveModeList(MODETYPE_USER))
321 reason = "One or more of the user modes on the remote server are invalid on this server.";
324 /* Challenge response, store their challenge for our password */
325 std::map<std::string,std::string>::iterator n = this->capab->CapKeys.find("CHALLENGE");
326 if ((n != this->capab->CapKeys.end()) && (ServerInstance->Modules->FindService(SERVICE_DATA, "hash/sha256")))
328 /* Challenge-response is on now */
329 this->SetTheirChallenge(n->second);
330 if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
332 this->SendCapabilities(2);
333 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+this->MakePass(capab->link->SendPass, capab->theirchallenge)+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
338 // They didn't specify a challenge or we don't have sha256, we use plaintext
339 if (this->LinkState == CONNECTING)
341 this->SendCapabilities(2);
342 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+capab->link->SendPass+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
348 this->SendError("CAPAB negotiation failed: "+reason);
352 else if ((params[0] == "MODULES") && (params.size() == 2))
354 if (!capab->ModuleList.length())
356 capab->ModuleList = params[1];
360 capab->ModuleList.push_back(' ');
361 capab->ModuleList.append(params[1]);
364 else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
366 if (!capab->OptModuleList.length())
368 capab->OptModuleList = params[1];
372 capab->OptModuleList.push_back(' ');
373 capab->OptModuleList.append(params[1]);
376 else if ((params[0] == "CHANMODES") && (params.size() == 2))
378 capab->ChanModes = params[1];
380 else if ((params[0] == "USERMODES") && (params.size() == 2))
382 capab->UserModes = params[1];
384 else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
386 irc::tokenstream capabs(params[1]);
388 while (capabs.GetToken(item))
390 /* Process each key/value pair */
391 std::string::size_type equals = item.find('=');
392 if (equals != std::string::npos)
394 std::string var(item, 0, equals);
395 std::string value(item, equals+1);
396 capab->CapKeys[var] = value;