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 char sep = proto_version > 1201 ? ' ' : ',';
83 irc::sepstream modulelist(MyModules(VF_COMMON), sep);
84 irc::sepstream optmodulelist(MyModules(VF_OPTCOMMON), sep);
85 /* Send module names, split at 509 length */
87 std::string line = "CAPAB MODULES :";
88 while (modulelist.GetToken(item))
90 if (line.length() + item.length() + 1 > 509)
92 this->WriteLine(line);
93 line = "CAPAB MODULES :";
96 if (line != "CAPAB MODULES :")
101 if (line != "CAPAB MODULES :")
102 this->WriteLine(line);
104 line = "CAPAB MODSUPPORT :";
105 while (optmodulelist.GetToken(item))
107 if (line.length() + item.length() + 1 > 509)
109 this->WriteLine(line);
110 line = "CAPAB MODSUPPORT :";
113 if (line != "CAPAB MODSUPPORT :")
118 if (line != "CAPAB MODSUPPORT :")
119 this->WriteLine(line);
121 WriteLine("CAPAB CHANMODES :" + BuildModeList(MODETYPE_CHANNEL));
122 WriteLine("CAPAB USERMODES :" + BuildModeList(MODETYPE_USER));
129 /* Do we have sha256 available? If so, we send a challenge */
130 if (Utils->ChallengeResponse && (ServerInstance->Modules->Find("m_sha256.so")))
132 this->SetOurChallenge(RandString(20));
133 extra = " CHALLENGE=" + this->GetOurChallenge();
136 this->WriteLine("CAPAB CAPABILITIES " /* Preprocessor does this one. */
137 ":NICKMAX="+ConvToStr(ServerInstance->Config->Limits.NickMax)+
138 " CHANMAX="+ConvToStr(ServerInstance->Config->Limits.ChanMax)+
139 " MAXMODES="+ConvToStr(ServerInstance->Config->Limits.MaxModes)+
140 " IDENTMAX="+ConvToStr(ServerInstance->Config->Limits.IdentMax)+
141 " MAXQUIT="+ConvToStr(ServerInstance->Config->Limits.MaxQuit)+
142 " MAXTOPIC="+ConvToStr(ServerInstance->Config->Limits.MaxTopic)+
143 " MAXKICK="+ConvToStr(ServerInstance->Config->Limits.MaxKick)+
144 " MAXGECOS="+ConvToStr(ServerInstance->Config->Limits.MaxGecos)+
145 " MAXAWAY="+ConvToStr(ServerInstance->Config->Limits.MaxAway)+
146 " IP6NATIVE="+ConvToStr(ip6)+
148 " PROTOCOL="+ConvToStr(ProtocolVersion)+extra+
149 " PREFIX="+ServerInstance->Modes->BuildPrefixes()+
150 " CHANMODES="+ServerInstance->Modes->GiveModeList(MASK_CHANNEL)+
151 " USERMODES="+ServerInstance->Modes->GiveModeList(MASK_USER)+
154 this->WriteLine("CAPAB END");
157 /* Isolate and return the elements that are different between two comma seperated lists */
158 void TreeSocket::ListDifference(const std::string &one, const std::string &two, char sep,
159 std::string& mleft, std::string& mright)
161 std::set<std::string> values;
162 irc::sepstream sepleft(one, sep);
163 irc::sepstream sepright(two, sep);
165 while (sepleft.GetToken(item))
169 while (sepright.GetToken(item))
171 if (!values.erase(item))
173 mright.push_back(sep);
177 for(std::set<std::string>::iterator i = values.begin(); i != values.end(); ++i)
179 mleft.push_back(sep);
184 bool TreeSocket::Capab(const parameterlist ¶ms)
186 if (params.size() < 1)
188 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
191 if (params[0] == "START")
193 capab->ModuleList.clear();
194 capab->OptModuleList.clear();
195 capab->CapKeys.clear();
196 if (params.size() > 1)
197 proto_version = atoi(params[1].c_str());
200 else if (params[0] == "END")
203 /* Compare ModuleList and check CapKeys */
204 if ((this->capab->ModuleList != this->MyModules(VF_COMMON)) && (this->capab->ModuleList.length()))
206 std::string diffIneed, diffUneed;
207 ListDifference(this->capab->ModuleList, this->MyModules(VF_COMMON), proto_version > 1201 ? ' ' : ',', diffIneed, diffUneed);
208 if (diffIneed.length() || diffUneed.length())
210 reason = "Modules incorrectly matched on these servers.";
211 if (diffIneed.length())
212 reason += " Not loaded here:" + diffIneed;
213 if (diffUneed.length())
214 reason += " Not loaded there:" + diffUneed;
215 this->SendError("CAPAB negotiation failed: "+reason);
219 if (this->capab->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->capab->OptModuleList.length())
221 std::string diffIneed, diffUneed;
222 ListDifference(this->capab->OptModuleList, this->MyModules(VF_OPTCOMMON), ' ', diffIneed, diffUneed);
223 if (diffIneed.length() || diffUneed.length())
225 if (Utils->AllowOptCommon)
227 ServerInstance->SNO->WriteToSnoMask('l',
228 "Optional module lists do not match, some commands may not work globally.%s%s%s%s",
229 diffIneed.length() ? " Not loaded here:" : "", diffIneed.c_str(),
230 diffUneed.length() ? " Not loaded there:" : "", diffUneed.c_str());
234 reason = "Optional modules incorrectly matched on these servers, and options::allowmismatch not set.";
235 if (diffIneed.length())
236 reason += " Not loaded here:" + diffIneed;
237 if (diffUneed.length())
238 reason += " Not loaded there:" + diffUneed;
239 this->SendError("CAPAB negotiation failed: "+reason);
245 if (this->capab->CapKeys.find("PROTOCOL") == this->capab->CapKeys.end())
247 reason = "Protocol version not specified";
251 proto_version = atoi(capab->CapKeys.find("PROTOCOL")->second.c_str());
252 if (proto_version < MinCompatProtocol)
254 reason = "Server is using protocol version " + ConvToStr(proto_version) +
255 " which is too old to link with this server (version " + ConvToStr(ProtocolVersion)
256 + (ProtocolVersion != MinCompatProtocol ? ", links with " + ConvToStr(MinCompatProtocol) + " and above)" : ")");
260 if(this->capab->CapKeys.find("PREFIX") != this->capab->CapKeys.end() && this->capab->CapKeys.find("PREFIX")->second != ServerInstance->Modes->BuildPrefixes())
261 reason = "One or more of the prefixes on the remote server are invalid on this server.";
263 if (!capab->ChanModes.empty())
265 if (capab->ChanModes != BuildModeList(MODETYPE_CHANNEL))
267 std::string diffIneed, diffUneed;
268 ListDifference(capab->ChanModes, BuildModeList(MODETYPE_CHANNEL), ' ', diffIneed, diffUneed);
269 if (diffIneed.length() || diffUneed.length())
271 reason = "Channel modes not matched on these servers.";
272 if (diffIneed.length())
273 reason += " Not loaded here:" + diffIneed;
274 if (diffUneed.length())
275 reason += " Not loaded there:" + diffUneed;
279 else if (this->capab->CapKeys.find("CHANMODES") != this->capab->CapKeys.end())
281 if (this->capab->CapKeys.find("CHANMODES")->second != ServerInstance->Modes->GiveModeList(MASK_CHANNEL))
282 reason = "One or more of the channel modes on the remote server are invalid on this server.";
285 if (!capab->UserModes.empty())
287 if (capab->UserModes != BuildModeList(MODETYPE_USER))
289 std::string diffIneed, diffUneed;
290 ListDifference(capab->UserModes, BuildModeList(MODETYPE_USER), ' ', diffIneed, diffUneed);
291 if (diffIneed.length() || diffUneed.length())
293 reason = "User modes not matched on these servers.";
294 if (diffIneed.length())
295 reason += " Not loaded here:" + diffIneed;
296 if (diffUneed.length())
297 reason += " Not loaded there:" + diffUneed;
301 else if (this->capab->CapKeys.find("USERMODES") != this->capab->CapKeys.end())
303 if (this->capab->CapKeys.find("USERMODES")->second != ServerInstance->Modes->GiveModeList(MASK_USER))
304 reason = "One or more of the user modes on the remote server are invalid on this server.";
307 /* Challenge response, store their challenge for our password */
308 std::map<std::string,std::string>::iterator n = this->capab->CapKeys.find("CHALLENGE");
309 if (Utils->ChallengeResponse && (n != this->capab->CapKeys.end()) && (ServerInstance->Modules->Find("m_sha256.so")))
311 /* Challenge-response is on now */
312 this->SetTheirChallenge(n->second);
313 if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
315 this->SendCapabilities(2);
316 this->WriteLine(std::string("SERVER ")+ServerInstance->Config->ServerName+" "+this->MakePass(capab->OutboundPass, this->GetTheirChallenge())+" 0 "+
317 ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
322 /* They didnt specify a challenge or we don't have m_sha256.so, we use plaintext */
323 if (this->LinkState == CONNECTING)
325 this->SendCapabilities(2);
326 this->WriteLine(std::string("SERVER ")+ServerInstance->Config->ServerName+" "+capab->OutboundPass+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
332 this->SendError("CAPAB negotiation failed: "+reason);
336 else if ((params[0] == "MODULES") && (params.size() == 2))
338 if (!capab->ModuleList.length())
340 capab->ModuleList = params[1];
344 capab->ModuleList.push_back(proto_version > 1201 ? ' ' : ',');
345 capab->ModuleList.append(params[1]);
348 else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
350 if (!capab->OptModuleList.length())
352 capab->OptModuleList = params[1];
356 capab->OptModuleList.push_back(' ');
357 capab->OptModuleList.append(params[1]);
360 else if ((params[0] == "CHANMODES") && (params.size() == 2))
362 capab->ChanModes = params[1];
364 else if ((params[0] == "USERMODES") && (params.size() == 2))
366 capab->UserModes = params[1];
368 else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
370 irc::tokenstream capabs(params[1]);
372 while (capabs.GetToken(item))
374 /* Process each key/value pair */
375 std::string::size_type equals = item.find('=');
376 if (equals != std::string::npos)
378 std::string var = item.substr(0, equals);
379 std::string value = item.substr(equals+1, item.length());
380 capab->CapKeys[var] = value;