1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2010 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
17 #include "treesocket.h"
18 #include "treeserver.h"
22 /* $ModDep: m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/treesocket.h */
24 std::string TreeSocket::MyModules(int filter)
26 std::vector<std::string> modlist = ServerInstance->Modules->GetAllModuleNames(filter);
28 if (filter == VF_COMMON && proto_version != ProtocolVersion)
29 CompatAddModules(modlist);
31 std::string capabilities;
32 sort(modlist.begin(),modlist.end());
33 for (unsigned int i = 0; i < modlist.size(); i++)
36 capabilities.push_back(',');
37 capabilities.append(modlist[i]);
38 Module* m = ServerInstance->Modules->Find(modlist[i]);
39 if (m && proto_version >= 1202)
41 Version v = m->GetVersion();
42 if (!v.link_data.empty())
44 capabilities.push_back('=');
45 capabilities.append(v.link_data);
52 static std::string BuildModeList(ModeType type)
55 for(char c='A'; c <= 'z'; c++)
57 ModeHandler* mh = ServerInstance->Modes->FindMode(c, type);
62 line.append(mh->name);
70 void TreeSocket::SendCapabilities(int phase)
72 if (capab->capab_phase >= phase)
75 if (capab->capab_phase < 1 && phase >= 1)
76 WriteLine("CAPAB START " + ConvToStr(ProtocolVersion));
78 capab->capab_phase = phase;
82 irc::commasepstream modulelist(MyModules(VF_COMMON));
83 irc::commasepstream optmodulelist(MyModules(VF_OPTCOMMON));
84 /* Send module names, split at 509 length */
86 std::string line = "CAPAB MODULES ";
87 while (modulelist.GetToken(item))
89 if (line.length() + item.length() + 1 > 509)
91 this->WriteLine(line);
92 line = "CAPAB MODULES ";
95 if (line != "CAPAB MODULES ")
100 if (line != "CAPAB MODULES ")
101 this->WriteLine(line);
103 line = "CAPAB MODSUPPORT ";
104 while (optmodulelist.GetToken(item))
106 if (line.length() + item.length() + 1 > 509)
108 this->WriteLine(line);
109 line = "CAPAB MODSUPPORT ";
112 if (line != "CAPAB MODSUPPORT ")
117 if (line != "CAPAB MODSUPPORT ")
118 this->WriteLine(line);
120 line = "CAPAB CHANMODES " + BuildModeList(MODETYPE_CHANNEL);
121 if (line != "CAPAB CHANMODES ")
122 this->WriteLine(line);
124 line = "CAPAB USERMODES " + BuildModeList(MODETYPE_USER);
125 if (line != "CAPAB USERMODES ")
126 this->WriteLine(line);
133 /* Do we have sha256 available? If so, we send a challenge */
134 if (Utils->ChallengeResponse && (ServerInstance->Modules->Find("m_sha256.so")))
136 this->SetOurChallenge(RandString(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)+
150 " IP6NATIVE="+ConvToStr(ip6)+
152 " PROTOCOL="+ConvToStr(ProtocolVersion)+extra+
153 " PREFIX="+ServerInstance->Modes->BuildPrefixes()+
154 " CHANMODES="+ServerInstance->Modes->GiveModeList(MASK_CHANNEL)+
155 " USERMODES="+ServerInstance->Modes->GiveModeList(MASK_USER)+
158 this->WriteLine("CAPAB END");
161 /* Check a comma seperated list for an item */
162 bool TreeSocket::HasItem(const std::string &list, const std::string &item)
164 irc::commasepstream seplist(list);
167 while (seplist.GetToken(item2))
175 /* Isolate and return the elements that are different between two comma seperated lists */
176 std::string TreeSocket::ListDifference(const std::string &one, const std::string &two)
178 irc::commasepstream list_one(one);
181 while (list_one.GetToken(item))
183 if (!HasItem(two, item))
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 = ListDifference(this->capab->ModuleList, this->MyModules(VF_COMMON));
215 std::string diffUneed = ListDifference(this->MyModules(VF_COMMON), this->capab->ModuleList);
216 if (diffIneed.length() == 0 && diffUneed.length() == 0)
218 reason = "Module list in CAPAB is not alphabetically ordered, cannot compare lists.";
222 reason = "Modules incorrectly matched on these servers.";
223 if (diffIneed.length())
224 reason += " Not loaded here:" + diffIneed;
225 if (diffUneed.length())
226 reason += " Not loaded there:" + diffUneed;
228 this->SendError("CAPAB negotiation failed: "+reason);
231 if (this->capab->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->capab->OptModuleList.length())
233 std::string diffIneed = ListDifference(this->capab->OptModuleList, this->MyModules(VF_OPTCOMMON));
234 std::string diffUneed = ListDifference(this->MyModules(VF_OPTCOMMON), this->capab->OptModuleList);
235 if (diffIneed.length() == 0 && diffUneed.length() == 0)
237 reason = "Optional Module list in CAPAB is not alphabetically ordered, cannot compare lists.";
239 else 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);
258 if (this->capab->CapKeys.find("PROTOCOL") == this->capab->CapKeys.end())
260 reason = "Protocol version not specified";
264 proto_version = atoi(capab->CapKeys.find("PROTOCOL")->second.c_str());
265 if (proto_version < MinCompatProtocol)
267 reason = "Server is using protocol version " + ConvToStr(proto_version) +
268 " which is too old to link with this server (version " + ConvToStr(ProtocolVersion)
269 + (ProtocolVersion != MinCompatProtocol ? ", links with " + ConvToStr(MinCompatProtocol) + " and above)" : ")");
273 if(this->capab->CapKeys.find("PREFIX") != this->capab->CapKeys.end() && this->capab->CapKeys.find("PREFIX")->second != ServerInstance->Modes->BuildPrefixes())
274 reason = "One or more of the prefixes on the remote server are invalid on this server.";
276 if (!capab->ChanModes.empty())
278 if (capab->ChanModes != BuildModeList(MODETYPE_CHANNEL))
280 std::string diffIneed = ListDifference(capab->ChanModes, BuildModeList(MODETYPE_CHANNEL));
281 std::string diffUneed = ListDifference(BuildModeList(MODETYPE_CHANNEL), capab->ChanModes);
282 reason = "Channel modes not matched on these servers.";
283 if (diffIneed.length())
284 reason += " Not loaded here:" + diffIneed;
285 if (diffUneed.length())
286 reason += " Not loaded there:" + diffUneed;
289 else if (this->capab->CapKeys.find("CHANMODES") != this->capab->CapKeys.end())
291 if (this->capab->CapKeys.find("CHANMODES")->second != ServerInstance->Modes->GiveModeList(MASK_CHANNEL))
292 reason = "One or more of the channel modes on the remote server are invalid on this server.";
295 if (!capab->UserModes.empty())
297 if (capab->UserModes != BuildModeList(MODETYPE_USER))
299 std::string diffIneed = ListDifference(capab->UserModes, BuildModeList(MODETYPE_USER));
300 std::string diffUneed = ListDifference(BuildModeList(MODETYPE_USER), capab->UserModes);
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;
308 else if (this->capab->CapKeys.find("USERMODES") != this->capab->CapKeys.end())
310 if (this->capab->CapKeys.find("USERMODES")->second != ServerInstance->Modes->GiveModeList(MASK_USER))
311 reason = "One or more of the user modes on the remote server are invalid on this server.";
314 /* Challenge response, store their challenge for our password */
315 std::map<std::string,std::string>::iterator n = this->capab->CapKeys.find("CHALLENGE");
316 if (Utils->ChallengeResponse && (n != this->capab->CapKeys.end()) && (ServerInstance->Modules->Find("m_sha256.so")))
318 /* Challenge-response is on now */
319 this->SetTheirChallenge(n->second);
320 if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
322 this->SendCapabilities(2);
323 this->WriteLine(std::string("SERVER ")+ServerInstance->Config->ServerName+" "+this->MakePass(capab->OutboundPass, this->GetTheirChallenge())+" 0 "+
324 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(std::string("SERVER ")+ServerInstance->Config->ServerName+" "+capab->OutboundPass+" 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 (!this->capab->ModuleList.length())
347 this->capab->ModuleList.append(params[1]);
351 this->capab->ModuleList.append(",");
352 this->capab->ModuleList.append(params[1]);
355 else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
357 if (!this->capab->OptModuleList.length())
359 this->capab->OptModuleList.append(params[1]);
363 this->capab->OptModuleList.append(",");
364 this->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]);
380 while ((more = capabs.GetToken(item)))
382 /* Process each key/value pair */
383 std::string::size_type equals = item.rfind('=');
384 if (equals != std::string::npos)
386 std::string var = item.substr(0, equals);
387 std::string value = item.substr(equals+1, item.length());
388 capab->CapKeys[var] = value;