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"
23 std::string TreeSocket::MyModules(int filter)
25 std::vector<std::string> modlist = ServerInstance->Modules->GetAllModuleNames(filter);
27 if (filter == VF_COMMON && proto_version != ProtocolVersion)
28 CompatAddModules(modlist);
30 std::string capabilities;
31 sort(modlist.begin(),modlist.end());
32 for (unsigned int i = 0; i < modlist.size(); i++)
35 capabilities.push_back(proto_version > 1201 ? ' ' : ',');
36 capabilities.append(modlist[i]);
37 Module* m = ServerInstance->Modules->Find(modlist[i]);
38 if (m && proto_version > 1201)
40 Version v = m->GetVersion();
41 if (!v.link_data.empty())
43 capabilities.push_back('=');
44 capabilities.append(v.link_data);
51 static std::string BuildModeList(ModeType type)
53 std::vector<std::string> modes;
54 for(char c='A'; c <= 'z'; c++)
56 ModeHandler* mh = ServerInstance->Modes->FindMode(c, type);
59 std::string mdesc = mh->name;
62 mdesc.push_back(mh->GetPrefix());
63 if (mh->GetModeChar())
64 mdesc.push_back(mh->GetModeChar());
65 modes.push_back(mdesc);
68 sort(modes.begin(), modes.end());
69 irc::stringjoiner line(" ", modes, 0, modes.size() - 1);
70 return line.GetJoined();
73 void TreeSocket::SendCapabilities(int phase)
75 if (capab->capab_phase >= phase)
78 if (capab->capab_phase < 1 && phase >= 1)
79 WriteLine("CAPAB START " + ConvToStr(ProtocolVersion));
81 capab->capab_phase = phase;
85 char sep = proto_version > 1201 ? ' ' : ',';
86 irc::sepstream modulelist(MyModules(VF_COMMON), sep);
87 irc::sepstream optmodulelist(MyModules(VF_OPTCOMMON), sep);
88 /* Send module names, split at 509 length */
90 std::string line = "CAPAB MODULES :";
91 while (modulelist.GetToken(item))
93 if (line.length() + item.length() + 1 > 509)
95 this->WriteLine(line);
96 line = "CAPAB MODULES :";
99 if (line != "CAPAB MODULES :")
104 if (line != "CAPAB MODULES :")
105 this->WriteLine(line);
107 line = "CAPAB MODSUPPORT :";
108 while (optmodulelist.GetToken(item))
110 if (line.length() + item.length() + 1 > 509)
112 this->WriteLine(line);
113 line = "CAPAB MODSUPPORT :";
116 if (line != "CAPAB MODSUPPORT :")
121 if (line != "CAPAB MODSUPPORT :")
122 this->WriteLine(line);
124 WriteLine("CAPAB CHANMODES :" + BuildModeList(MODETYPE_CHANNEL));
125 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 SetOurChallenge(ServerInstance->GenRandomStr(20));
132 extra = " CHALLENGE=" + this->GetOurChallenge();
134 if (proto_version < 1202)
135 extra += ServerInstance->Modes->FindMode('h', MODETYPE_CHANNEL) ? " HALFOP=1" : " HALFOP=0";
137 this->WriteLine("CAPAB CAPABILITIES " /* Preprocessor does this one. */
138 ":NICKMAX="+ConvToStr(ServerInstance->Config->Limits.NickMax)+
139 " CHANMAX="+ConvToStr(ServerInstance->Config->Limits.ChanMax)+
140 " MAXMODES="+ConvToStr(ServerInstance->Config->Limits.MaxModes)+
141 " IDENTMAX="+ConvToStr(ServerInstance->Config->Limits.IdentMax)+
142 " MAXQUIT="+ConvToStr(ServerInstance->Config->Limits.MaxQuit)+
143 " MAXTOPIC="+ConvToStr(ServerInstance->Config->Limits.MaxTopic)+
144 " MAXKICK="+ConvToStr(ServerInstance->Config->Limits.MaxKick)+
145 " MAXGECOS="+ConvToStr(ServerInstance->Config->Limits.MaxGecos)+
146 " MAXAWAY="+ConvToStr(ServerInstance->Config->Limits.MaxAway)+
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->link->SendPass, capab->theirchallenge)+" 0 "+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->link->SendPass+" 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 (!capab->ModuleList.length())
339 capab->ModuleList = params[1];
343 capab->ModuleList.push_back(proto_version > 1201 ? ' ' : ',');
344 capab->ModuleList.append(params[1]);
347 else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
349 if (!capab->OptModuleList.length())
351 capab->OptModuleList = params[1];
355 capab->OptModuleList.push_back(' ');
356 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;