1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
17 #include "m_spanningtree/treesocket.h"
18 #include "m_spanningtree/treeserver.h"
19 #include "m_spanningtree/utils.h"
20 #include "m_spanningtree/main.h"
22 /* $ModDep: m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/treesocket.h */
25 std::string TreeSocket::MyCapabilities()
27 std::vector<std::string> modlist = this->ServerInstance->Modules->GetAllModuleNames(VF_COMMON);
28 std::string capabilities;
29 sort(modlist.begin(),modlist.end());
30 for (unsigned int i = 0; i < modlist.size(); i++)
33 capabilities = capabilities + ",";
34 capabilities = capabilities + modlist[i];
39 void TreeSocket::SendCapabilities()
45 irc::commasepstream modulelist(MyCapabilities());
46 this->WriteLine("CAPAB START");
48 /* Send module names, split at 509 length */
50 std::string line = "CAPAB MODULES ";
51 while (modulelist.GetToken(item))
53 if (line.length() + item.length() + 1 > 509)
55 this->WriteLine(line);
56 line = "CAPAB MODULES ";
59 if (line != "CAPAB MODULES ")
64 if (line != "CAPAB MODULES ")
65 this->WriteLine(line);
72 #ifdef SUPPORT_IP6LINKS
76 /* Do we have sha256 available? If so, we send a challenge */
77 if (Utils->ChallengeResponse && (ServerInstance->Modules->Find("m_sha256.so")))
79 this->SetOurChallenge(RandString(20));
80 extra = " CHALLENGE=" + this->GetOurChallenge();
83 this->WriteLine("CAPAB CAPABILITIES " /* Preprocessor does this one. */
84 ":NICKMAX="+ConvToStr(ServerInstance->Config->Limits.NickMax)+
85 " HALFOP="+ConvToStr(ServerInstance->Config->AllowHalfop)+
86 " CHANMAX="+ConvToStr(ServerInstance->Config->Limits.ChanMax)+
87 " MAXMODES="+ConvToStr(ServerInstance->Config->Limits.MaxModes)+
88 " IDENTMAX="+ConvToStr(ServerInstance->Config->Limits.IdentMax)+
89 " MAXQUIT="+ConvToStr(ServerInstance->Config->Limits.MaxQuit)+
90 " MAXTOPIC="+ConvToStr(ServerInstance->Config->Limits.MaxTopic)+
91 " MAXKICK="+ConvToStr(ServerInstance->Config->Limits.MaxKick)+
92 " MAXGECOS="+ConvToStr(ServerInstance->Config->Limits.MaxGecos)+
93 " MAXAWAY="+ConvToStr(ServerInstance->Config->Limits.MaxAway)+
94 " IP6NATIVE="+ConvToStr(ip6)+
95 " IP6SUPPORT="+ConvToStr(ip6support)+
96 " PROTOCOL="+ConvToStr(ProtocolVersion)+extra+
97 " PREFIX="+ServerInstance->Modes->BuildPrefixes()+
98 " CHANMODES="+ServerInstance->Modes->GiveModeList(MASK_CHANNEL)+
99 " USERMODES="+ServerInstance->Modes->GiveModeList(MASK_USER)+
102 this->WriteLine("CAPAB END");
105 /* Check a comma seperated list for an item */
106 bool TreeSocket::HasItem(const std::string &list, const std::string &item)
108 irc::commasepstream seplist(list);
111 while (seplist.GetToken(item2))
119 /* Isolate and return the elements that are different between two comma seperated lists */
120 std::string TreeSocket::ListDifference(const std::string &one, const std::string &two)
122 irc::commasepstream list_one(one);
125 while (list_one.GetToken(item))
127 if (!HasItem(two, item))
136 bool TreeSocket::Capab(const std::deque<std::string> ¶ms)
138 if (params.size() < 1)
140 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
143 if (params[0] == "START")
145 this->ModuleList.clear();
146 this->CapKeys.clear();
148 else if (params[0] == "END")
152 #ifdef SUPPORT_IP6LINKS
155 /* Compare ModuleList and check CapKeys...
156 * Maybe this could be tidier? -- Brain
158 if ((this->ModuleList != this->MyCapabilities()) && (this->ModuleList.length()))
160 std::string diff = ListDifference(this->ModuleList, this->MyCapabilities());
163 diff = "your server:" + ListDifference(this->MyCapabilities(), this->ModuleList);
167 diff = "the remote server:" + diff;
169 if (diff.length() == 12)
170 reason = "Module list in CAPAB is not alphabetically ordered, cannot compare lists.";
172 reason = "Modules loaded on these servers are not correctly matched, these modules are not loaded on " + diff;
175 if (((this->CapKeys.find("IP6SUPPORT") == this->CapKeys.end()) && (ip6support)) || ((this->CapKeys.find("IP6SUPPORT") != this->CapKeys.end()) && (this->CapKeys.find("IP6SUPPORT")->second != ConvToStr(ip6support))))
176 reason = "We don't both support linking to IPV6 servers";
177 if (((this->CapKeys.find("IP6NATIVE") != this->CapKeys.end()) && (this->CapKeys.find("IP6NATIVE")->second == "1")) && (!ip6support))
178 reason = "The remote server is IPV6 native, and we don't support linking to IPV6 servers";
179 if (((this->CapKeys.find("PROTOCOL") == this->CapKeys.end()) || ((this->CapKeys.find("PROTOCOL") != this->CapKeys.end()) && (this->CapKeys.find("PROTOCOL")->second != ConvToStr(ProtocolVersion)))))
181 if (this->CapKeys.find("PROTOCOL") != this->CapKeys.end())
182 reason = "Mismatched protocol versions "+this->CapKeys.find("PROTOCOL")->second+" and "+ConvToStr(ProtocolVersion);
184 reason = "Protocol version not specified";
187 if(this->CapKeys.find("PREFIX") != this->CapKeys.end() && this->CapKeys.find("PREFIX")->second != this->ServerInstance->Modes->BuildPrefixes())
188 reason = "One or more of the prefixes on the remote server are invalid on this server.";
190 if(this->CapKeys.find("CHANMODES") != this->CapKeys.end() && this->CapKeys.find("CHANMODES")->second != this->ServerInstance->Modes->GiveModeList(MASK_CHANNEL))
191 reason = "One or more of the channel modes on the remote server are invalid on this server.";
193 if(this->CapKeys.find("USERMODES") != this->CapKeys.end() && this->CapKeys.find("USERMODES")->second != this->ServerInstance->Modes->GiveModeList(MASK_USER))
194 reason = "One or more of the user modes on the remote server are invalid on this server.";
197 if (((this->CapKeys.find("HALFOP") == this->CapKeys.end()) && (ServerInstance->Config->AllowHalfop)) || ((this->CapKeys.find("HALFOP") != this->CapKeys.end()) && (this->CapKeys.find("HALFOP")->second != ConvToStr(ServerInstance->Config->AllowHalfop))))
198 reason = "We don't both have halfop support enabled/disabled identically";
202 /* Challenge response, store their challenge for our password */
203 std::map<std::string,std::string>::iterator n = this->CapKeys.find("CHALLENGE");
204 if (Utils->ChallengeResponse && (n != this->CapKeys.end()) && (ServerInstance->Modules->Find("m_sha256.so")))
206 /* Challenge-response is on now */
207 this->SetTheirChallenge(n->second);
208 if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
210 this->SendCapabilities();
211 this->WriteLine(std::string("SERVER ")+this->ServerInstance->Config->ServerName+" "+this->MakePass(OutboundPass, this->GetTheirChallenge())+" 0 "+
212 ServerInstance->Config->GetSID()+" :"+this->ServerInstance->Config->ServerDesc);
217 /* They didnt specify a challenge or we don't have m_sha256.so, we use plaintext */
218 if (this->LinkState == CONNECTING)
220 this->SendCapabilities();
221 this->WriteLine(std::string("SERVER ")+this->ServerInstance->Config->ServerName+" "+OutboundPass+" 0 "+ServerInstance->Config->GetSID()+" :"+this->ServerInstance->Config->ServerDesc);
227 this->SendError("CAPAB negotiation failed: "+reason);
231 else if ((params[0] == "MODULES") && (params.size() == 2))
233 if (!this->ModuleList.length())
235 this->ModuleList.append(params[1]);
239 this->ModuleList.append(",");
240 this->ModuleList.append(params[1]);
244 else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
246 irc::tokenstream capabs(params[1]);
249 while ((more = capabs.GetToken(item)))
251 /* Process each key/value pair */
252 std::string::size_type equals = item.rfind('=');
253 if (equals != std::string::npos)
255 std::string var = item.substr(0, equals);
256 std::string value = item.substr(equals+1, item.length());
257 CapKeys[var] = value;