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);
65 line.push_back(mh->GetPrefix());
72 void TreeSocket::SendCapabilities(int phase)
74 if (capab->capab_phase >= phase)
77 if (capab->capab_phase < 1 && phase >= 1)
78 WriteLine("CAPAB START " + ConvToStr(ProtocolVersion));
80 capab->capab_phase = phase;
84 irc::commasepstream modulelist(MyModules(VF_COMMON));
85 irc::commasepstream optmodulelist(MyModules(VF_OPTCOMMON));
86 /* Send module names, split at 509 length */
88 std::string line = "CAPAB MODULES ";
89 while (modulelist.GetToken(item))
91 if (line.length() + item.length() + 1 > 509)
93 this->WriteLine(line);
94 line = "CAPAB MODULES ";
97 if (line != "CAPAB MODULES ")
102 if (line != "CAPAB MODULES ")
103 this->WriteLine(line);
105 line = "CAPAB MODSUPPORT ";
106 while (optmodulelist.GetToken(item))
108 if (line.length() + item.length() + 1 > 509)
110 this->WriteLine(line);
111 line = "CAPAB MODSUPPORT ";
114 if (line != "CAPAB MODSUPPORT ")
119 if (line != "CAPAB MODSUPPORT ")
120 this->WriteLine(line);
122 line = "CAPAB CHANMODES " + BuildModeList(MODETYPE_CHANNEL);
123 if (line != "CAPAB CHANMODES ")
124 this->WriteLine(line);
126 line = "CAPAB USERMODES " + BuildModeList(MODETYPE_USER);
127 if (line != "CAPAB USERMODES ")
128 this->WriteLine(line);
135 /* Do we have sha256 available? If so, we send a challenge */
136 if (Utils->ChallengeResponse && (ServerInstance->Modules->Find("m_sha256.so")))
138 this->SetOurChallenge(RandString(20));
139 extra = " CHALLENGE=" + this->GetOurChallenge();
142 this->WriteLine("CAPAB CAPABILITIES " /* Preprocessor does this one. */
143 ":NICKMAX="+ConvToStr(ServerInstance->Config->Limits.NickMax)+
144 " CHANMAX="+ConvToStr(ServerInstance->Config->Limits.ChanMax)+
145 " MAXMODES="+ConvToStr(ServerInstance->Config->Limits.MaxModes)+
146 " IDENTMAX="+ConvToStr(ServerInstance->Config->Limits.IdentMax)+
147 " MAXQUIT="+ConvToStr(ServerInstance->Config->Limits.MaxQuit)+
148 " MAXTOPIC="+ConvToStr(ServerInstance->Config->Limits.MaxTopic)+
149 " MAXKICK="+ConvToStr(ServerInstance->Config->Limits.MaxKick)+
150 " MAXGECOS="+ConvToStr(ServerInstance->Config->Limits.MaxGecos)+
151 " MAXAWAY="+ConvToStr(ServerInstance->Config->Limits.MaxAway)+
152 " IP6NATIVE="+ConvToStr(ip6)+
154 " PROTOCOL="+ConvToStr(ProtocolVersion)+extra+
155 " PREFIX="+ServerInstance->Modes->BuildPrefixes()+
156 " CHANMODES="+ServerInstance->Modes->GiveModeList(MASK_CHANNEL)+
157 " USERMODES="+ServerInstance->Modes->GiveModeList(MASK_USER)+
160 this->WriteLine("CAPAB END");
163 /* Check a comma seperated list for an item */
164 bool TreeSocket::HasItem(const std::string &list, const std::string &item)
166 irc::commasepstream seplist(list);
169 while (seplist.GetToken(item2))
177 /* Isolate and return the elements that are different between two comma seperated lists */
178 std::string TreeSocket::ListDifference(const std::string &one, const std::string &two)
180 irc::commasepstream list_one(one);
183 while (list_one.GetToken(item))
185 if (!HasItem(two, item))
194 bool TreeSocket::Capab(const parameterlist ¶ms)
196 if (params.size() < 1)
198 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
201 if (params[0] == "START")
203 capab->ModuleList.clear();
204 capab->OptModuleList.clear();
205 capab->CapKeys.clear();
206 if (params.size() > 1)
207 proto_version = atoi(params[1].c_str());
210 else if (params[0] == "END")
213 /* Compare ModuleList and check CapKeys */
214 if ((this->capab->ModuleList != this->MyModules(VF_COMMON)) && (this->capab->ModuleList.length()))
216 std::string diffIneed = ListDifference(this->capab->ModuleList, this->MyModules(VF_COMMON));
217 std::string diffUneed = ListDifference(this->MyModules(VF_COMMON), this->capab->ModuleList);
218 if (diffIneed.length() == 0 && diffUneed.length() == 0)
220 reason = "Module list in CAPAB is not alphabetically ordered, cannot compare lists.";
224 reason = "Modules incorrectly matched on these servers.";
225 if (diffIneed.length())
226 reason += " Not loaded here:" + diffIneed;
227 if (diffUneed.length())
228 reason += " Not loaded there:" + diffUneed;
230 this->SendError("CAPAB negotiation failed: "+reason);
233 if (this->capab->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->capab->OptModuleList.length())
235 std::string diffIneed = ListDifference(this->capab->OptModuleList, this->MyModules(VF_OPTCOMMON));
236 std::string diffUneed = ListDifference(this->MyModules(VF_OPTCOMMON), this->capab->OptModuleList);
237 if (diffIneed.length() == 0 && diffUneed.length() == 0)
239 reason = "Optional Module list in CAPAB is not alphabetically ordered, cannot compare lists.";
241 else if (Utils->AllowOptCommon)
243 ServerInstance->SNO->WriteToSnoMask('l',
244 "Optional module lists do not match, some commands may not work globally.%s%s%s%s",
245 diffIneed.length() ? " Not loaded here:" : "", diffIneed.c_str(),
246 diffUneed.length() ? " Not loaded there:" : "", diffUneed.c_str());
250 reason = "Optional modules incorrectly matched on these servers, and options::allowmismatch not set.";
251 if (diffIneed.length())
252 reason += " Not loaded here:" + diffIneed;
253 if (diffUneed.length())
254 reason += " Not loaded there:" + diffUneed;
255 this->SendError("CAPAB negotiation failed: "+reason);
260 if (this->capab->CapKeys.find("PROTOCOL") == this->capab->CapKeys.end())
262 reason = "Protocol version not specified";
266 proto_version = atoi(capab->CapKeys.find("PROTOCOL")->second.c_str());
267 if (proto_version < MinCompatProtocol)
269 reason = "Server is using protocol version " + ConvToStr(proto_version) +
270 " which is too old to link with this server (version " + ConvToStr(ProtocolVersion)
271 + (ProtocolVersion != MinCompatProtocol ? ", links with " + ConvToStr(MinCompatProtocol) + " and above)" : ")");
275 if(this->capab->CapKeys.find("PREFIX") != this->capab->CapKeys.end() && this->capab->CapKeys.find("PREFIX")->second != ServerInstance->Modes->BuildPrefixes())
276 reason = "One or more of the prefixes on the remote server are invalid on this server.";
278 if (!capab->ChanModes.empty())
280 if (capab->ChanModes != BuildModeList(MODETYPE_CHANNEL))
282 std::string diffIneed = ListDifference(capab->ChanModes, BuildModeList(MODETYPE_CHANNEL));
283 std::string diffUneed = ListDifference(BuildModeList(MODETYPE_CHANNEL), capab->ChanModes);
284 reason = "Channel modes not matched on these servers.";
285 if (diffIneed.length())
286 reason += " Not loaded here:" + diffIneed;
287 if (diffUneed.length())
288 reason += " Not loaded there:" + diffUneed;
291 else if (this->capab->CapKeys.find("CHANMODES") != this->capab->CapKeys.end())
293 if (this->capab->CapKeys.find("CHANMODES")->second != ServerInstance->Modes->GiveModeList(MASK_CHANNEL))
294 reason = "One or more of the channel modes on the remote server are invalid on this server.";
297 if (!capab->UserModes.empty())
299 if (capab->UserModes != BuildModeList(MODETYPE_USER))
301 std::string diffIneed = ListDifference(capab->UserModes, BuildModeList(MODETYPE_USER));
302 std::string diffUneed = ListDifference(BuildModeList(MODETYPE_USER), capab->UserModes);
303 reason = "User modes not matched on these servers.";
304 if (diffIneed.length())
305 reason += " Not loaded here:" + diffIneed;
306 if (diffUneed.length())
307 reason += " Not loaded there:" + diffUneed;
310 else if (this->capab->CapKeys.find("USERMODES") != this->capab->CapKeys.end())
312 if (this->capab->CapKeys.find("USERMODES")->second != ServerInstance->Modes->GiveModeList(MASK_USER))
313 reason = "One or more of the user modes on the remote server are invalid on this server.";
316 /* Challenge response, store their challenge for our password */
317 std::map<std::string,std::string>::iterator n = this->capab->CapKeys.find("CHALLENGE");
318 if (Utils->ChallengeResponse && (n != this->capab->CapKeys.end()) && (ServerInstance->Modules->Find("m_sha256.so")))
320 /* Challenge-response is on now */
321 this->SetTheirChallenge(n->second);
322 if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
324 this->SendCapabilities(2);
325 this->WriteLine(std::string("SERVER ")+ServerInstance->Config->ServerName+" "+this->MakePass(capab->OutboundPass, this->GetTheirChallenge())+" 0 "+
326 ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
331 /* They didnt specify a challenge or we don't have m_sha256.so, we use plaintext */
332 if (this->LinkState == CONNECTING)
334 this->SendCapabilities(2);
335 this->WriteLine(std::string("SERVER ")+ServerInstance->Config->ServerName+" "+capab->OutboundPass+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
341 this->SendError("CAPAB negotiation failed: "+reason);
345 else if ((params[0] == "MODULES") && (params.size() == 2))
347 if (!this->capab->ModuleList.length())
349 this->capab->ModuleList.append(params[1]);
353 this->capab->ModuleList.append(",");
354 this->capab->ModuleList.append(params[1]);
357 else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
359 if (!this->capab->OptModuleList.length())
361 this->capab->OptModuleList.append(params[1]);
365 this->capab->OptModuleList.append(",");
366 this->capab->OptModuleList.append(params[1]);
369 else if ((params[0] == "CHANMODES") && (params.size() == 2))
371 capab->ChanModes = params[1];
373 else if ((params[0] == "USERMODES") && (params.size() == 2))
375 capab->UserModes = params[1];
377 else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
379 irc::tokenstream capabs(params[1]);
382 while ((more = capabs.GetToken(item)))
384 /* Process each key/value pair */
385 std::string::size_type equals = item.rfind('=');
386 if (equals != std::string::npos)
388 std::string var = item.substr(0, equals);
389 std::string value = item.substr(equals+1, item.length());
390 capab->CapKeys[var] = value;