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 " HALFOP="+ConvToStr(ServerInstance->Config->AllowHalfop)+
108 " CHANMAX="+ConvToStr(ServerInstance->Config->Limits.ChanMax)+
109 " MAXMODES="+ConvToStr(ServerInstance->Config->Limits.MaxModes)+
110 " IDENTMAX="+ConvToStr(ServerInstance->Config->Limits.IdentMax)+
111 " MAXQUIT="+ConvToStr(ServerInstance->Config->Limits.MaxQuit)+
112 " MAXTOPIC="+ConvToStr(ServerInstance->Config->Limits.MaxTopic)+
113 " MAXKICK="+ConvToStr(ServerInstance->Config->Limits.MaxKick)+
114 " MAXGECOS="+ConvToStr(ServerInstance->Config->Limits.MaxGecos)+
115 " MAXAWAY="+ConvToStr(ServerInstance->Config->Limits.MaxAway)+
116 " IP6NATIVE="+ConvToStr(ip6)+
118 " PROTOCOL="+ConvToStr(ProtocolVersion)+extra+
119 " PREFIX="+ServerInstance->Modes->BuildPrefixes()+
120 " CHANMODES="+ServerInstance->Modes->GiveModeList(MASK_CHANNEL)+
121 " USERMODES="+ServerInstance->Modes->GiveModeList(MASK_USER)+
124 this->WriteLine("CAPAB END");
127 /* Check a comma seperated list for an item */
128 bool TreeSocket::HasItem(const std::string &list, const std::string &item)
130 irc::commasepstream seplist(list);
133 while (seplist.GetToken(item2))
141 /* Isolate and return the elements that are different between two comma seperated lists */
142 std::string TreeSocket::ListDifference(const std::string &one, const std::string &two)
144 irc::commasepstream list_one(one);
147 while (list_one.GetToken(item))
149 if (!HasItem(two, item))
158 bool TreeSocket::Capab(const parameterlist ¶ms)
160 if (params.size() < 1)
162 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
165 if (params[0] == "START")
168 OptModuleList.clear();
170 if (params.size() > 1)
171 proto_version = atoi(params[1].c_str());
174 else if (params[0] == "END")
177 /* Compare ModuleList and check CapKeys */
178 if ((this->ModuleList != this->MyModules(VF_COMMON)) && (this->ModuleList.length()))
180 std::string diffIneed = ListDifference(this->ModuleList, this->MyModules(VF_COMMON));
181 std::string diffUneed = ListDifference(this->MyModules(VF_COMMON), this->ModuleList);
182 if (diffIneed.length() == 0 && diffUneed.length() == 0)
184 reason = "Module list in CAPAB is not alphabetically ordered, cannot compare lists.";
188 reason = "Modules incorrectly matched on these servers.";
189 if (diffIneed.length())
190 reason += " Not loaded here:" + diffIneed;
191 if (diffUneed.length())
192 reason += " Not loaded there:" + diffUneed;
194 this->SendError("CAPAB negotiation failed: "+reason);
197 if (this->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->OptModuleList.length())
199 std::string diffIneed = ListDifference(this->OptModuleList, this->MyModules(VF_OPTCOMMON));
200 std::string diffUneed = ListDifference(this->MyModules(VF_OPTCOMMON), this->OptModuleList);
201 if (diffIneed.length() == 0 && diffUneed.length() == 0)
203 reason = "Optional Module list in CAPAB is not alphabetically ordered, cannot compare lists.";
205 else if (Utils->AllowOptCommon)
207 ServerInstance->SNO->WriteToSnoMask('l',
208 "Optional module lists do not match, some commands may not work globally.%s%s%s%s",
209 diffIneed.length() ? " Not loaded here:" : "", diffIneed.c_str(),
210 diffUneed.length() ? " Not loaded there:" : "", diffUneed.c_str());
214 reason = "Optional modules incorrectly matched on these servers, and options::allowmismatch not set.";
215 if (diffIneed.length())
216 reason += " Not loaded here:" + diffIneed;
217 if (diffUneed.length())
218 reason += " Not loaded there:" + diffUneed;
219 this->SendError("CAPAB negotiation failed: "+reason);
224 if (this->CapKeys.find("PROTOCOL") == this->CapKeys.end())
226 reason = "Protocol version not specified";
230 proto_version = atoi(CapKeys.find("PROTOCOL")->second.c_str());
231 if (proto_version < MinCompatProtocol)
233 reason = "Server is using protocol version " + ConvToStr(proto_version) +
234 " which is too old to link with this server (version " + ConvToStr(ProtocolVersion)
235 + (ProtocolVersion != MinCompatProtocol ? ", links with " + ConvToStr(MinCompatProtocol) + " and above)" : ")");
239 if(this->CapKeys.find("PREFIX") != this->CapKeys.end() && this->CapKeys.find("PREFIX")->second != ServerInstance->Modes->BuildPrefixes())
240 reason = "One or more of the prefixes on the remote server are invalid on this server.";
242 if(this->CapKeys.find("CHANMODES") != this->CapKeys.end() && this->CapKeys.find("CHANMODES")->second != ServerInstance->Modes->GiveModeList(MASK_CHANNEL))
243 reason = "One or more of the channel modes on the remote server are invalid on this server.";
245 if(this->CapKeys.find("USERMODES") != this->CapKeys.end() && this->CapKeys.find("USERMODES")->second != ServerInstance->Modes->GiveModeList(MASK_USER))
246 reason = "One or more of the user modes on the remote server are invalid on this server.";
249 /* Challenge response, store their challenge for our password */
250 std::map<std::string,std::string>::iterator n = this->CapKeys.find("CHALLENGE");
251 if (Utils->ChallengeResponse && (n != this->CapKeys.end()) && (ServerInstance->Modules->Find("m_sha256.so")))
253 /* Challenge-response is on now */
254 this->SetTheirChallenge(n->second);
255 if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
257 this->SendCapabilities(2);
258 this->WriteLine(std::string("SERVER ")+ServerInstance->Config->ServerName+" "+this->MakePass(OutboundPass, this->GetTheirChallenge())+" 0 "+
259 ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
264 /* They didnt specify a challenge or we don't have m_sha256.so, we use plaintext */
265 if (this->LinkState == CONNECTING)
267 this->SendCapabilities(2);
268 this->WriteLine(std::string("SERVER ")+ServerInstance->Config->ServerName+" "+OutboundPass+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
274 this->SendError("CAPAB negotiation failed: "+reason);
278 else if ((params[0] == "MODULES") && (params.size() == 2))
280 if (!this->ModuleList.length())
282 this->ModuleList.append(params[1]);
286 this->ModuleList.append(",");
287 this->ModuleList.append(params[1]);
290 else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
292 if (!this->OptModuleList.length())
294 this->OptModuleList.append(params[1]);
298 this->OptModuleList.append(",");
299 this->OptModuleList.append(params[1]);
302 else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
304 irc::tokenstream capabs(params[1]);
307 while ((more = capabs.GetToken(item)))
309 /* Process each key/value pair */
310 std::string::size_type equals = item.rfind('=');
311 if (equals != std::string::npos)
313 std::string var = item.substr(0, equals);
314 std::string value = item.substr(equals+1, item.length());
315 CapKeys[var] = value;