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 std::string TreeSocket::MyModules(int filter)
24 std::vector<std::string> modlist = ServerInstance->Modules->GetAllModuleNames(filter);
26 if (filter == VF_COMMON && proto_version != ProtocolVersion)
27 CompatAddModules(modlist);
29 std::string capabilities;
30 sort(modlist.begin(),modlist.end());
31 for (unsigned int i = 0; i < modlist.size(); i++)
34 capabilities.push_back(proto_version > 1201 ? ' ' : ',');
35 capabilities.append(modlist[i]);
36 Module* m = ServerInstance->Modules->Find(modlist[i]);
37 if (m && proto_version > 1201)
39 Version v = m->GetVersion();
40 if (!v.link_data.empty())
42 capabilities.push_back('=');
43 capabilities.append(v.link_data);
50 static std::string BuildModeList(ModeType type)
53 for(char c='A'; c <= 'z'; c++)
55 ModeHandler* mh = ServerInstance->Modes->FindMode(c, type);
60 line.append(mh->name);
63 line.push_back(mh->GetPrefix());
70 void TreeSocket::SendCapabilities(int phase)
72 if (capab->capab_phase >= phase)
75 if (capab->capab_phase < 1 && phase >= 1)
76 WriteLine("CAPAB START " + ConvToStr(ProtocolVersion));
78 capab->capab_phase = phase;
82 irc::commasepstream modulelist(MyModules(VF_COMMON));
83 irc::commasepstream optmodulelist(MyModules(VF_OPTCOMMON));
84 /* Send module names, split at 509 length */
86 std::string line = "CAPAB MODULES ";
87 while (modulelist.GetToken(item))
89 if (line.length() + item.length() + 1 > 509)
91 this->WriteLine(line);
92 line = "CAPAB MODULES ";
95 if (line != "CAPAB MODULES ")
100 if (line != "CAPAB MODULES ")
101 this->WriteLine(line);
103 line = "CAPAB MODSUPPORT ";
104 while (optmodulelist.GetToken(item))
106 if (line.length() + item.length() + 1 > 509)
108 this->WriteLine(line);
109 line = "CAPAB MODSUPPORT ";
112 if (line != "CAPAB MODSUPPORT ")
117 if (line != "CAPAB MODSUPPORT ")
118 this->WriteLine(line);
120 WriteLine("CAPAB CHANMODES :" + BuildModeList(MODETYPE_CHANNEL));
121 WriteLine("CAPAB USERMODES :" + BuildModeList(MODETYPE_USER));
128 /* Do we have sha256 available? If so, we send a challenge */
129 if (Utils->ChallengeResponse && (ServerInstance->Modules->Find("m_sha256.so")))
131 this->SetOurChallenge(RandString(20));
132 extra = " CHALLENGE=" + this->GetOurChallenge();
135 this->WriteLine("CAPAB CAPABILITIES " /* Preprocessor does this one. */
136 ":NICKMAX="+ConvToStr(ServerInstance->Config->Limits.NickMax)+
137 " CHANMAX="+ConvToStr(ServerInstance->Config->Limits.ChanMax)+
138 " MAXMODES="+ConvToStr(ServerInstance->Config->Limits.MaxModes)+
139 " IDENTMAX="+ConvToStr(ServerInstance->Config->Limits.IdentMax)+
140 " MAXQUIT="+ConvToStr(ServerInstance->Config->Limits.MaxQuit)+
141 " MAXTOPIC="+ConvToStr(ServerInstance->Config->Limits.MaxTopic)+
142 " MAXKICK="+ConvToStr(ServerInstance->Config->Limits.MaxKick)+
143 " MAXGECOS="+ConvToStr(ServerInstance->Config->Limits.MaxGecos)+
144 " MAXAWAY="+ConvToStr(ServerInstance->Config->Limits.MaxAway)+
145 " IP6NATIVE="+ConvToStr(ip6)+
147 " PROTOCOL="+ConvToStr(ProtocolVersion)+extra+
148 " PREFIX="+ServerInstance->Modes->BuildPrefixes()+
149 " CHANMODES="+ServerInstance->Modes->GiveModeList(MASK_CHANNEL)+
150 " USERMODES="+ServerInstance->Modes->GiveModeList(MASK_USER)+
153 this->WriteLine("CAPAB END");
156 /* Isolate and return the elements that are different between two comma seperated lists */
157 void TreeSocket::ListDifference(const std::string &one, const std::string &two, char sep,
158 std::string& mleft, std::string& mright)
160 std::set<std::string> values;
161 irc::sepstream sepleft(one, sep);
162 irc::sepstream sepright(two, sep);
164 while (sepleft.GetToken(item))
168 while (sepright.GetToken(item))
170 if (!values.erase(item))
172 mleft.push_back(sep);
176 for(std::set<std::string>::iterator i = values.begin(); i != values.end(); ++i)
178 mright.push_back(sep);
183 bool TreeSocket::Capab(const parameterlist ¶ms)
185 if (params.size() < 1)
187 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
190 if (params[0] == "START")
192 capab->ModuleList.clear();
193 capab->OptModuleList.clear();
194 capab->CapKeys.clear();
195 if (params.size() > 1)
196 proto_version = atoi(params[1].c_str());
199 else if (params[0] == "END")
202 /* Compare ModuleList and check CapKeys */
203 if ((this->capab->ModuleList != this->MyModules(VF_COMMON)) && (this->capab->ModuleList.length()))
205 std::string diffIneed, diffUneed;
206 ListDifference(this->capab->ModuleList, this->MyModules(VF_COMMON), proto_version > 1201 ? ' ' : ',', diffIneed, diffUneed);
207 if (diffIneed.length() || diffUneed.length())
209 reason = "Modules incorrectly matched on these servers.";
210 if (diffIneed.length())
211 reason += " Not loaded here:" + diffIneed;
212 if (diffUneed.length())
213 reason += " Not loaded there:" + diffUneed;
214 this->SendError("CAPAB negotiation failed: "+reason);
218 if (this->capab->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->capab->OptModuleList.length())
220 std::string diffIneed, diffUneed;
221 ListDifference(this->capab->ModuleList, this->MyModules(VF_OPTCOMMON), ' ', diffIneed, diffUneed);
222 if (diffIneed.length() || diffUneed.length())
224 if (Utils->AllowOptCommon)
226 ServerInstance->SNO->WriteToSnoMask('l',
227 "Optional module lists do not match, some commands may not work globally.%s%s%s%s",
228 diffIneed.length() ? " Not loaded here:" : "", diffIneed.c_str(),
229 diffUneed.length() ? " Not loaded there:" : "", diffUneed.c_str());
233 reason = "Optional modules incorrectly matched on these servers, and options::allowmismatch not set.";
234 if (diffIneed.length())
235 reason += " Not loaded here:" + diffIneed;
236 if (diffUneed.length())
237 reason += " Not loaded there:" + diffUneed;
238 this->SendError("CAPAB negotiation failed: "+reason);
244 if (this->capab->CapKeys.find("PROTOCOL") == this->capab->CapKeys.end())
246 reason = "Protocol version not specified";
250 proto_version = atoi(capab->CapKeys.find("PROTOCOL")->second.c_str());
251 if (proto_version < MinCompatProtocol)
253 reason = "Server is using protocol version " + ConvToStr(proto_version) +
254 " which is too old to link with this server (version " + ConvToStr(ProtocolVersion)
255 + (ProtocolVersion != MinCompatProtocol ? ", links with " + ConvToStr(MinCompatProtocol) + " and above)" : ")");
259 if(this->capab->CapKeys.find("PREFIX") != this->capab->CapKeys.end() && this->capab->CapKeys.find("PREFIX")->second != ServerInstance->Modes->BuildPrefixes())
260 reason = "One or more of the prefixes on the remote server are invalid on this server.";
262 if (!capab->ChanModes.empty())
264 if (capab->ChanModes != BuildModeList(MODETYPE_CHANNEL))
266 std::string diffIneed, diffUneed;
267 ListDifference(capab->ChanModes, BuildModeList(MODETYPE_CHANNEL), ' ', diffIneed, diffUneed);
268 if (diffIneed.length() || diffUneed.length())
270 reason = "Channel modes not matched on these servers.";
271 if (diffIneed.length())
272 reason += " Not loaded here:" + diffIneed;
273 if (diffUneed.length())
274 reason += " Not loaded there:" + diffUneed;
278 else if (this->capab->CapKeys.find("CHANMODES") != this->capab->CapKeys.end())
280 if (this->capab->CapKeys.find("CHANMODES")->second != ServerInstance->Modes->GiveModeList(MASK_CHANNEL))
281 reason = "One or more of the channel modes on the remote server are invalid on this server.";
284 if (!capab->UserModes.empty())
286 if (capab->UserModes != BuildModeList(MODETYPE_USER))
288 std::string diffIneed, diffUneed;
289 ListDifference(capab->UserModes, BuildModeList(MODETYPE_USER), ' ', diffIneed, diffUneed);
290 if (diffIneed.length() || diffUneed.length())
292 reason = "User modes not matched on these servers.";
293 if (diffIneed.length())
294 reason += " Not loaded here:" + diffIneed;
295 if (diffUneed.length())
296 reason += " Not loaded there:" + diffUneed;
300 else if (this->capab->CapKeys.find("USERMODES") != this->capab->CapKeys.end())
302 if (this->capab->CapKeys.find("USERMODES")->second != ServerInstance->Modes->GiveModeList(MASK_USER))
303 reason = "One or more of the user modes on the remote server are invalid on this server.";
306 /* Challenge response, store their challenge for our password */
307 std::map<std::string,std::string>::iterator n = this->capab->CapKeys.find("CHALLENGE");
308 if (Utils->ChallengeResponse && (n != this->capab->CapKeys.end()) && (ServerInstance->Modules->Find("m_sha256.so")))
310 /* Challenge-response is on now */
311 this->SetTheirChallenge(n->second);
312 if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
314 this->SendCapabilities(2);
315 this->WriteLine(std::string("SERVER ")+ServerInstance->Config->ServerName+" "+this->MakePass(capab->OutboundPass, this->GetTheirChallenge())+" 0 "+
316 ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
321 /* They didnt specify a challenge or we don't have m_sha256.so, we use plaintext */
322 if (this->LinkState == CONNECTING)
324 this->SendCapabilities(2);
325 this->WriteLine(std::string("SERVER ")+ServerInstance->Config->ServerName+" "+capab->OutboundPass+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
331 this->SendError("CAPAB negotiation failed: "+reason);
335 else if ((params[0] == "MODULES") && (params.size() == 2))
337 if (!this->capab->ModuleList.length())
339 this->capab->ModuleList.append(params[1]);
343 this->capab->ModuleList.append(",");
344 this->capab->ModuleList.append(params[1]);
347 else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
349 if (!this->capab->OptModuleList.length())
351 this->capab->OptModuleList.append(params[1]);
355 this->capab->OptModuleList.append(",");
356 this->capab->OptModuleList.append(params[1]);
359 else if ((params[0] == "CHANMODES") && (params.size() == 2))
361 capab->ChanModes = params[1];
363 else if ((params[0] == "USERMODES") && (params.size() == 2))
365 capab->UserModes = params[1];
367 else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
369 irc::tokenstream capabs(params[1]);
371 while (capabs.GetToken(item))
373 /* Process each key/value pair */
374 std::string::size_type equals = item.find('=');
375 if (equals != std::string::npos)
377 std::string var = item.substr(0, equals);
378 std::string value = item.substr(equals+1, item.length());
379 capab->CapKeys[var] = value;