- char command[1024];
- for (unsigned int q = 0; q < Current->ChildCount(); q++)
- {
- TreeServer* recursive_server = Current->GetChild(q);
- if (recursive_server != s)
- {
- snprintf(command,1024,":%s SERVER %s * %d :%s",Current->GetName().c_str(),recursive_server->GetName().c_str(),hops,recursive_server->GetDesc().c_str());
- this->WriteLine(command);
- this->WriteLine(":"+recursive_server->GetName()+" VERSION :"+recursive_server->GetVersion());
- /* down to next level */
- this->SendServers(recursive_server, s, hops+1);
- }
- }
-}
-
-std::string TreeSocket::MyCapabilities()
-{
- std::vector<std::string> modlist;
- std::string capabilities = "";
- for (int i = 0; i <= this->Instance->GetModuleCount(); i++)
- {
- if (this->Instance->modules[i]->GetVersion().Flags & VF_COMMON)
- modlist.push_back(this->Instance->Config->module_names[i]);
- }
- sort(modlist.begin(),modlist.end());
- for (unsigned int i = 0; i < modlist.size(); i++)
- {
- if (i)
- capabilities = capabilities + ",";
- capabilities = capabilities + modlist[i];
- }
- return capabilities;
-}
-
-std::string TreeSocket::RandString(unsigned int length)
-{
- char* randombuf = new char[length+1];
- std::string out;
- int fd = open("/dev/urandom", O_RDONLY, 0);
-
- if (fd >= 0)
- {
- read(fd, randombuf, length);
- close(fd);
- }
- else
- {
- for (unsigned int i = 0; i < length; i++)
- randombuf[i] = rand();
- }
-
- for (unsigned int i = 0; i < length; i++)
- {
- char randchar = static_cast<char>((randombuf[i] & 0x7F) | 0x21);
- out += (randchar == '=' ? '_' : randchar);
- }
-
- delete[] randombuf;
- return out;
-}
-
-void TreeSocket::SendCapabilities()
-{
- irc::commasepstream modulelist(MyCapabilities());
- this->WriteLine("CAPAB START");
-
- /* Send module names, split at 509 length */
- std::string item = "*";
- std::string line = "CAPAB MODULES ";
- while ((item = modulelist.GetToken()) != "")
- {
- if (line.length() + item.length() + 1 > 509)
- {
- this->WriteLine(line);
- line = "CAPAB MODULES ";
- }
-
- if (line != "CAPAB MODULES ")
- line.append(",");
-
- line.append(item);
- }
- if (line != "CAPAB MODULES ")
- this->WriteLine(line);
-
- int ip6 = 0;
- int ip6support = 0;
-#ifdef IPV6
- ip6 = 1;
-#endif
-#ifdef SUPPORT_IP6LINKS
- ip6support = 1;
-#endif
- std::string extra;
- /* Do we have sha256 available? If so, we send a challenge */
- if (Utils->ChallengeResponse && (Instance->FindModule("m_sha256.so")))
- {
- this->SetOurChallenge(RandString(20));
- extra = " CHALLENGE=" + this->GetOurChallenge();
- }
-
- this->WriteLine("CAPAB CAPABILITIES :NICKMAX="+ConvToStr(NICKMAX)+" HALFOP="+ConvToStr(this->Instance->Config->AllowHalfop)+" CHANMAX="+ConvToStr(CHANMAX)+" MAXMODES="+ConvToStr(MAXMODES)+" IDENTMAX="+ConvToStr(IDENTMAX)+" MAXQUIT="+ConvToStr(MAXQUIT)+" MAXTOPIC="+ConvToStr(MAXTOPIC)+" MAXKICK="+ConvToStr(MAXKICK)+" MAXGECOS="+ConvToStr(MAXGECOS)+" MAXAWAY="+ConvToStr(MAXAWAY)+" IP6NATIVE="+ConvToStr(ip6)+" IP6SUPPORT="+ConvToStr(ip6support)+" PROTOCOL="+ConvToStr(ProtocolVersion)+extra);
-
- this->WriteLine("CAPAB END");
-}
-
-/* Check a comma seperated list for an item */
-bool TreeSocket::HasItem(const std::string &list, const std::string &item)
-{
- irc::commasepstream seplist(list);
- std::string item2 = "*";
- while ((item2 = seplist.GetToken()) != "")
- {
- if (item2 == item)
- return true;
- }
- return false;
-}
-
-/* Isolate and return the elements that are different between two comma seperated lists */
-std::string TreeSocket::ListDifference(const std::string &one, const std::string &two)
-{
- irc::commasepstream list_one(one);
- std::string item = "*";
- std::string result = "";
- while ((item = list_one.GetToken()) != "")
- {
- if (!HasItem(two, item))
- {
- result.append(" ");
- result.append(item);
- }
- }
- return result;
-}
-
-bool TreeSocket::Capab(const std::deque<std::string> ¶ms)
-{
- if (params.size() < 1)
- {
- this->WriteLine("ERROR :Invalid number of parameters for CAPAB - Mismatched version");
- return false;
- }
- if (params[0] == "START")
- {
- this->ModuleList = "";
- this->CapKeys.clear();
- }
- else if (params[0] == "END")
- {
- std::string reason = "";
- int ip6support = 0;
-#ifdef SUPPORT_IP6LINKS
- ip6support = 1;
-#endif
- /* Compare ModuleList and check CapKeys...
- * Maybe this could be tidier? -- Brain
- */
- if ((this->ModuleList != this->MyCapabilities()) && (this->ModuleList.length()))
- {
- std::string diff = ListDifference(this->ModuleList, this->MyCapabilities());
- if (!diff.length())
- {
- diff = "your server:" + ListDifference(this->MyCapabilities(), this->ModuleList);
- }
- else
- {
- diff = "this server:" + diff;
- }
- if (diff.length() == 12)
- reason = "Module list in CAPAB is not alphabetically ordered, cannot compare lists.";
- else
- reason = "Modules loaded on these servers are not correctly matched, these modules are not loaded on " + diff;
- }
- if (((this->CapKeys.find("IP6SUPPORT") == this->CapKeys.end()) && (ip6support)) || ((this->CapKeys.find("IP6SUPPORT") != this->CapKeys.end()) && (this->CapKeys.find("IP6SUPPORT")->second != ConvToStr(ip6support))))
- reason = "We don't both support linking to IPV6 servers";
- if (((this->CapKeys.find("IP6NATIVE") != this->CapKeys.end()) && (this->CapKeys.find("IP6NATIVE")->second == "1")) && (!ip6support))
- reason = "The remote server is IPV6 native, and we don't support linking to IPV6 servers";
- if (((this->CapKeys.find("NICKMAX") == this->CapKeys.end()) || ((this->CapKeys.find("NICKMAX") != this->CapKeys.end()) && (this->CapKeys.find("NICKMAX")->second != ConvToStr(NICKMAX)))))
- reason = "Maximum nickname lengths differ or remote nickname length not specified";
- if (((this->CapKeys.find("PROTOCOL") == this->CapKeys.end()) || ((this->CapKeys.find("PROTOCOL") != this->CapKeys.end()) && (this->CapKeys.find("PROTOCOL")->second != ConvToStr(ProtocolVersion)))))
- {
- if (this->CapKeys.find("PROTOCOL") != this->CapKeys.end())
- {
- reason = "Mismatched protocol versions "+this->CapKeys.find("PROTOCOL")->second+" and "+ConvToStr(ProtocolVersion);
- }
- else
- {
- reason = "Protocol version not specified";
- }
- }
- if (((this->CapKeys.find("HALFOP") == this->CapKeys.end()) && (Instance->Config->AllowHalfop)) || ((this->CapKeys.find("HALFOP") != this->CapKeys.end()) && (this->CapKeys.find("HALFOP")->second != ConvToStr(Instance->Config->AllowHalfop))))
- reason = "We don't both have halfop support enabled/disabled identically";
- if (((this->CapKeys.find("IDENTMAX") == this->CapKeys.end()) || ((this->CapKeys.find("IDENTMAX") != this->CapKeys.end()) && (this->CapKeys.find("IDENTMAX")->second != ConvToStr(IDENTMAX)))))
- reason = "Maximum ident lengths differ or remote ident length not specified";
- if (((this->CapKeys.find("CHANMAX") == this->CapKeys.end()) || ((this->CapKeys.find("CHANMAX") != this->CapKeys.end()) && (this->CapKeys.find("CHANMAX")->second != ConvToStr(CHANMAX)))))
- reason = "Maximum channel lengths differ or remote channel length not specified";
- if (((this->CapKeys.find("MAXMODES") == this->CapKeys.end()) || ((this->CapKeys.find("MAXMODES") != this->CapKeys.end()) && (this->CapKeys.find("MAXMODES")->second != ConvToStr(MAXMODES)))))
- reason = "Maximum modes per line differ or remote modes per line not specified";
- if (((this->CapKeys.find("MAXQUIT") == this->CapKeys.end()) || ((this->CapKeys.find("MAXQUIT") != this->CapKeys.end()) && (this->CapKeys.find("MAXQUIT")->second != ConvToStr(MAXQUIT)))))
- reason = "Maximum quit lengths differ or remote quit length not specified";
- if (((this->CapKeys.find("MAXTOPIC") == this->CapKeys.end()) || ((this->CapKeys.find("MAXTOPIC") != this->CapKeys.end()) && (this->CapKeys.find("MAXTOPIC")->second != ConvToStr(MAXTOPIC)))))
- reason = "Maximum topic lengths differ or remote topic length not specified";
- if (((this->CapKeys.find("MAXKICK") == this->CapKeys.end()) || ((this->CapKeys.find("MAXKICK") != this->CapKeys.end()) && (this->CapKeys.find("MAXKICK")->second != ConvToStr(MAXKICK)))))
- reason = "Maximum kick lengths differ or remote kick length not specified";
- if (((this->CapKeys.find("MAXGECOS") == this->CapKeys.end()) || ((this->CapKeys.find("MAXGECOS") != this->CapKeys.end()) && (this->CapKeys.find("MAXGECOS")->second != ConvToStr(MAXGECOS)))))
- reason = "Maximum GECOS (fullname) lengths differ or remote GECOS length not specified";
- if (((this->CapKeys.find("MAXAWAY") == this->CapKeys.end()) || ((this->CapKeys.find("MAXAWAY") != this->CapKeys.end()) && (this->CapKeys.find("MAXAWAY")->second != ConvToStr(MAXAWAY)))))
- reason = "Maximum awaymessage lengths differ or remote awaymessage length not specified";
-
- /* Challenge response, store their challenge for our password */
- std::map<std::string,std::string>::iterator n = this->CapKeys.find("CHALLENGE");
- if (Utils->ChallengeResponse && (n != this->CapKeys.end()) && (Instance->FindModule("m_sha256.so")))
- {
- /* Challenge-response is on now */
- this->SetTheirChallenge(n->second);
- if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
- {
- this->WriteLine(std::string("SERVER ")+this->Instance->Config->ServerName+" "+this->MakePass(OutboundPass, this->GetTheirChallenge())+" 0 :"+this->Instance->Config->ServerDesc);
- }
- }
- else
- {
- /* They didnt specify a challenge or we don't have m_sha256.so, we use plaintext */
- if (this->LinkState == CONNECTING)
- this->WriteLine(std::string("SERVER ")+this->Instance->Config->ServerName+" "+OutboundPass+" 0 :"+this->Instance->Config->ServerDesc);
- }
-
- if (reason.length())
- {
- this->WriteLine("ERROR :CAPAB negotiation failed: "+reason);
- return false;
- }
- }
- else if ((params[0] == "MODULES") && (params.size() == 2))
- {
- if (!this->ModuleList.length())
- {
- this->ModuleList.append(params[1]);
- }
- else
- {
- this->ModuleList.append(",");
- this->ModuleList.append(params[1]);
- }
- }
-
- else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
- {
- irc::tokenstream capabs(params[1]);
- std::string item;
- bool more = true;
- while ((more = capabs.GetToken(item)))
- {
- /* Process each key/value pair */
- std::string::size_type equals = item.rfind('=');
- if (equals != std::string::npos)
- {
- std::string var = item.substr(0, equals);
- std::string value = item.substr(equals+1, item.length());
- CapKeys[var] = value;
- }
- }
- }
- return true;