1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 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 = capabilities + ",";
37 capabilities = capabilities + modlist[i];
42 void TreeSocket::SendCapabilities(int phase)
44 if (capab_phase >= phase)
47 if (capab_phase < 1 && phase >= 1)
48 WriteLine("CAPAB START " + ConvToStr(ProtocolVersion));
54 irc::commasepstream modulelist(MyModules(VF_COMMON));
55 irc::commasepstream optmodulelist(MyModules(VF_OPTCOMMON));
56 /* Send module names, split at 509 length */
58 std::string line = "CAPAB MODULES ";
59 while (modulelist.GetToken(item))
61 if (line.length() + item.length() + 1 > 509)
63 this->WriteLine(line);
64 line = "CAPAB MODULES ";
67 if (line != "CAPAB MODULES ")
72 if (line != "CAPAB MODULES ")
73 this->WriteLine(line);
75 line = "CAPAB MODSUPPORT ";
76 while (optmodulelist.GetToken(item))
78 if (line.length() + item.length() + 1 > 509)
80 this->WriteLine(line);
81 line = "CAPAB MODSUPPORT ";
84 if (line != "CAPAB MODSUPPORT ")
89 if (line != "CAPAB MODSUPPORT ")
90 this->WriteLine(line);
98 /* Do we have sha256 available? If so, we send a challenge */
99 if (Utils->ChallengeResponse && (ServerInstance->Modules->Find("m_sha256.so")))
101 this->SetOurChallenge(RandString(20));
102 extra = " CHALLENGE=" + this->GetOurChallenge();
105 this->WriteLine("CAPAB CAPABILITIES " /* Preprocessor does this one. */
106 ":NICKMAX="+ConvToStr(ServerInstance->Config->Limits.NickMax)+
107 " CHANMAX="+ConvToStr(ServerInstance->Config->Limits.ChanMax)+
108 " MAXMODES="+ConvToStr(ServerInstance->Config->Limits.MaxModes)+
109 " IDENTMAX="+ConvToStr(ServerInstance->Config->Limits.IdentMax)+
110 " MAXQUIT="+ConvToStr(ServerInstance->Config->Limits.MaxQuit)+
111 " MAXTOPIC="+ConvToStr(ServerInstance->Config->Limits.MaxTopic)+
112 " MAXKICK="+ConvToStr(ServerInstance->Config->Limits.MaxKick)+
113 " MAXGECOS="+ConvToStr(ServerInstance->Config->Limits.MaxGecos)+
114 " MAXAWAY="+ConvToStr(ServerInstance->Config->Limits.MaxAway)+
115 " IP6NATIVE="+ConvToStr(ip6)+
117 " PROTOCOL="+ConvToStr(ProtocolVersion)+extra+
118 " PREFIX="+ServerInstance->Modes->BuildPrefixes()+
119 " CHANMODES="+ServerInstance->Modes->GiveModeList(MASK_CHANNEL)+
120 " USERMODES="+ServerInstance->Modes->GiveModeList(MASK_USER)+
123 this->WriteLine("CAPAB END");
126 /* Check a comma seperated list for an item */
127 bool TreeSocket::HasItem(const std::string &list, const std::string &item)
129 irc::commasepstream seplist(list);
132 while (seplist.GetToken(item2))
140 /* Isolate and return the elements that are different between two comma seperated lists */
141 std::string TreeSocket::ListDifference(const std::string &one, const std::string &two)
143 irc::commasepstream list_one(one);
146 while (list_one.GetToken(item))
148 if (!HasItem(two, item))
157 bool TreeSocket::Capab(const parameterlist ¶ms)
159 if (params.size() < 1)
161 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
164 if (params[0] == "START")
167 OptModuleList.clear();
169 if (params.size() > 1)
170 proto_version = atoi(params[1].c_str());
173 else if (params[0] == "END")
176 /* Compare ModuleList and check CapKeys */
177 if ((this->ModuleList != this->MyModules(VF_COMMON)) && (this->ModuleList.length()))
179 std::string diffIneed = ListDifference(this->ModuleList, this->MyModules(VF_COMMON));
180 std::string diffUneed = ListDifference(this->MyModules(VF_COMMON), this->ModuleList);
181 if (diffIneed.length() == 0 && diffUneed.length() == 0)
183 reason = "Module list in CAPAB is not alphabetically ordered, cannot compare lists.";
187 reason = "Modules incorrectly matched on these servers.";
188 if (diffIneed.length())
189 reason += " Not loaded here:" + diffIneed;
190 if (diffUneed.length())
191 reason += " Not loaded there:" + diffUneed;
193 this->SendError("CAPAB negotiation failed: "+reason);
196 if (this->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->OptModuleList.length())
198 std::string diffIneed = ListDifference(this->OptModuleList, this->MyModules(VF_OPTCOMMON));
199 std::string diffUneed = ListDifference(this->MyModules(VF_OPTCOMMON), this->OptModuleList);
200 if (diffIneed.length() == 0 && diffUneed.length() == 0)
202 reason = "Optional Module list in CAPAB is not alphabetically ordered, cannot compare lists.";
204 else if (Utils->AllowOptCommon)
206 ServerInstance->SNO->WriteToSnoMask('l',
207 "Optional module lists do not match, some commands may not work globally.%s%s%s%s",
208 diffIneed.length() ? " Not loaded here:" : "", diffIneed.c_str(),
209 diffUneed.length() ? " Not loaded there:" : "", diffUneed.c_str());
213 reason = "Optional modules incorrectly matched on these servers, and options::allowmismatch not set.";
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);
223 if (this->CapKeys.find("PROTOCOL") == this->CapKeys.end())
225 reason = "Protocol version not specified";
229 proto_version = atoi(CapKeys.find("PROTOCOL")->second.c_str());
230 if (proto_version < MinCompatProtocol)
232 reason = "Server is using protocol version " + ConvToStr(proto_version) +
233 " which is too old to link with this server (version " + ConvToStr(ProtocolVersion)
234 + (ProtocolVersion != MinCompatProtocol ? ", links with " + ConvToStr(MinCompatProtocol) + " and above)" : ")");
238 if(this->CapKeys.find("PREFIX") != this->CapKeys.end() && this->CapKeys.find("PREFIX")->second != ServerInstance->Modes->BuildPrefixes())
239 reason = "One or more of the prefixes on the remote server are invalid on this server.";
241 if(this->CapKeys.find("CHANMODES") != this->CapKeys.end() && this->CapKeys.find("CHANMODES")->second != ServerInstance->Modes->GiveModeList(MASK_CHANNEL))
242 reason = "One or more of the channel modes on the remote server are invalid on this server.";
244 if(this->CapKeys.find("USERMODES") != this->CapKeys.end() && this->CapKeys.find("USERMODES")->second != ServerInstance->Modes->GiveModeList(MASK_USER))
245 reason = "One or more of the user modes on the remote server are invalid on this server.";
248 /* Challenge response, store their challenge for our password */
249 std::map<std::string,std::string>::iterator n = this->CapKeys.find("CHALLENGE");
250 if (Utils->ChallengeResponse && (n != this->CapKeys.end()) && (ServerInstance->Modules->Find("m_sha256.so")))
252 /* Challenge-response is on now */
253 this->SetTheirChallenge(n->second);
254 if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
256 this->SendCapabilities(2);
257 this->WriteLine(std::string("SERVER ")+ServerInstance->Config->ServerName+" "+this->MakePass(OutboundPass, this->GetTheirChallenge())+" 0 "+
258 ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
263 /* They didnt specify a challenge or we don't have m_sha256.so, we use plaintext */
264 if (this->LinkState == CONNECTING)
266 this->SendCapabilities(2);
267 this->WriteLine(std::string("SERVER ")+ServerInstance->Config->ServerName+" "+OutboundPass+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
273 this->SendError("CAPAB negotiation failed: "+reason);
277 else if ((params[0] == "MODULES") && (params.size() == 2))
279 if (!this->ModuleList.length())
281 this->ModuleList.append(params[1]);
285 this->ModuleList.append(",");
286 this->ModuleList.append(params[1]);
289 else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
291 if (!this->OptModuleList.length())
293 this->OptModuleList.append(params[1]);
297 this->OptModuleList.append(",");
298 this->OptModuleList.append(params[1]);
301 else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
303 irc::tokenstream capabs(params[1]);
306 while ((more = capabs.GetToken(item)))
308 /* Process each key/value pair */
309 std::string::size_type equals = item.rfind('=');
310 if (equals != std::string::npos)
312 std::string var = item.substr(0, equals);
313 std::string value = item.substr(equals+1, item.length());
314 CapKeys[var] = value;