]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/capab.cpp
Clean up treesocket naming confusion by adding a link block reference during negotiation
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / capab.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "xline.h"
16
17 #include "treesocket.h"
18 #include "treeserver.h"
19 #include "utils.h"
20 #include "link.h"
21 #include "main.h"
22
23 std::string TreeSocket::MyModules(int filter)
24 {
25         std::vector<std::string> modlist = ServerInstance->Modules->GetAllModuleNames(filter);
26
27         if (filter == VF_COMMON && proto_version != ProtocolVersion)
28                 CompatAddModules(modlist);
29
30         std::string capabilities;
31         sort(modlist.begin(),modlist.end());
32         for (unsigned int i = 0; i < modlist.size(); i++)
33         {
34                 if (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)
39                 {
40                         Version v = m->GetVersion();
41                         if (!v.link_data.empty())
42                         {
43                                 capabilities.push_back('=');
44                                 capabilities.append(v.link_data);
45                         }
46                 }
47         }
48         return capabilities;
49 }
50
51 static std::string BuildModeList(ModeType type)
52 {
53         std::vector<std::string> modes;
54         for(char c='A'; c <= 'z'; c++)
55         {
56                 ModeHandler* mh = ServerInstance->Modes->FindMode(c, type);
57                 if (mh)
58                 {
59                         std::string mdesc = mh->name;
60                         mdesc.push_back('=');
61                         if (mh->GetPrefix())
62                                 mdesc.push_back(mh->GetPrefix());
63                         if (mh->GetModeChar())
64                                 mdesc.push_back(mh->GetModeChar());
65                         modes.push_back(mdesc);
66                 }
67         }
68         sort(modes.begin(), modes.end());
69         irc::stringjoiner line(" ", modes, 0, modes.size() - 1);
70         return line.GetJoined();
71 }
72
73 void TreeSocket::SendCapabilities(int phase)
74 {
75         if (capab->capab_phase >= phase)
76                 return;
77
78         if (capab->capab_phase < 1 && phase >= 1)
79                 WriteLine("CAPAB START " + ConvToStr(ProtocolVersion));
80
81         capab->capab_phase = phase;
82         if (phase < 2)
83                 return;
84
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 */
89         std::string item;
90         std::string line = "CAPAB MODULES :";
91         while (modulelist.GetToken(item))
92         {
93                 if (line.length() + item.length() + 1 > 509)
94                 {
95                         this->WriteLine(line);
96                         line = "CAPAB MODULES :";
97                 }
98
99                 if (line != "CAPAB MODULES :")
100                         line.push_back(sep);
101
102                 line.append(item);
103         }
104         if (line != "CAPAB MODULES :")
105                 this->WriteLine(line);
106
107         line = "CAPAB MODSUPPORT :";
108         while (optmodulelist.GetToken(item))
109         {
110                 if (line.length() + item.length() + 1 > 509)
111                 {
112                         this->WriteLine(line);
113                         line = "CAPAB MODSUPPORT :";
114                 }
115
116                 if (line != "CAPAB MODSUPPORT :")
117                         line.push_back(sep);
118
119                 line.append(item);
120         }
121         if (line != "CAPAB MODSUPPORT :")
122                 this->WriteLine(line);
123
124         WriteLine("CAPAB CHANMODES :" + BuildModeList(MODETYPE_CHANNEL));
125         WriteLine("CAPAB USERMODES :" + BuildModeList(MODETYPE_USER));
126
127         int ip6 = 0;
128 #ifdef IPV6
129         ip6 = 1;
130 #endif
131         std::string extra;
132         /* Do we have sha256 available? If so, we send a challenge */
133         if (Utils->ChallengeResponse && (ServerInstance->Modules->Find("m_sha256.so")))
134         {
135                 SetOurChallenge(ServerInstance->GenRandomStr(20));
136                 extra = " CHALLENGE=" + this->GetOurChallenge();
137         }
138
139         this->WriteLine("CAPAB CAPABILITIES " /* Preprocessor does this one. */
140                         ":NICKMAX="+ConvToStr(ServerInstance->Config->Limits.NickMax)+
141                         " CHANMAX="+ConvToStr(ServerInstance->Config->Limits.ChanMax)+
142                         " MAXMODES="+ConvToStr(ServerInstance->Config->Limits.MaxModes)+
143                         " IDENTMAX="+ConvToStr(ServerInstance->Config->Limits.IdentMax)+
144                         " MAXQUIT="+ConvToStr(ServerInstance->Config->Limits.MaxQuit)+
145                         " MAXTOPIC="+ConvToStr(ServerInstance->Config->Limits.MaxTopic)+
146                         " MAXKICK="+ConvToStr(ServerInstance->Config->Limits.MaxKick)+
147                         " MAXGECOS="+ConvToStr(ServerInstance->Config->Limits.MaxGecos)+
148                         " MAXAWAY="+ConvToStr(ServerInstance->Config->Limits.MaxAway)+
149                         " IP6NATIVE="+ConvToStr(ip6)+
150                         " IP6SUPPORT=1"+
151                         " PROTOCOL="+ConvToStr(ProtocolVersion)+extra+
152                         " PREFIX="+ServerInstance->Modes->BuildPrefixes()+
153                         " CHANMODES="+ServerInstance->Modes->GiveModeList(MASK_CHANNEL)+
154                         " USERMODES="+ServerInstance->Modes->GiveModeList(MASK_USER)+
155                         " SVSPART=1");
156
157         this->WriteLine("CAPAB END");
158 }
159
160 /* Isolate and return the elements that are different between two comma seperated lists */
161 void TreeSocket::ListDifference(const std::string &one, const std::string &two, char sep,
162                 std::string& mleft, std::string& mright)
163 {
164         std::set<std::string> values;
165         irc::sepstream sepleft(one, sep);
166         irc::sepstream sepright(two, sep);
167         std::string item;
168         while (sepleft.GetToken(item))
169         {
170                 values.insert(item);
171         }
172         while (sepright.GetToken(item))
173         {
174                 if (!values.erase(item))
175                 {
176                         mright.push_back(sep);
177                         mright.append(item);
178                 }
179         }
180         for(std::set<std::string>::iterator i = values.begin(); i != values.end(); ++i)
181         {
182                 mleft.push_back(sep);
183                 mleft.append(*i);
184         }
185 }
186
187 bool TreeSocket::Capab(const parameterlist &params)
188 {
189         if (params.size() < 1)
190         {
191                 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
192                 return false;
193         }
194         if (params[0] == "START")
195         {
196                 capab->ModuleList.clear();
197                 capab->OptModuleList.clear();
198                 capab->CapKeys.clear();
199                 if (params.size() > 1)
200                         proto_version = atoi(params[1].c_str());
201                 SendCapabilities(2);
202         }
203         else if (params[0] == "END")
204         {
205                 std::string reason;
206                 /* Compare ModuleList and check CapKeys */
207                 if ((this->capab->ModuleList != this->MyModules(VF_COMMON)) && (this->capab->ModuleList.length()))
208                 {
209                         std::string diffIneed, diffUneed;
210                         ListDifference(this->capab->ModuleList, this->MyModules(VF_COMMON), proto_version > 1201 ? ' ' : ',', diffIneed, diffUneed);
211                         if (diffIneed.length() || diffUneed.length())
212                         {
213                                 reason = "Modules incorrectly matched on these servers.";
214                                 if (diffIneed.length())
215                                         reason += " Not loaded here:" + diffIneed;
216                                 if (diffUneed.length())
217                                         reason += " Not loaded there:" + diffUneed;
218                                 this->SendError("CAPAB negotiation failed: "+reason);
219                                 return false;
220                         }
221                 }
222                 if (this->capab->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->capab->OptModuleList.length())
223                 {
224                         std::string diffIneed, diffUneed;
225                         ListDifference(this->capab->OptModuleList, this->MyModules(VF_OPTCOMMON), ' ', diffIneed, diffUneed);
226                         if (diffIneed.length() || diffUneed.length())
227                         {
228                                 if (Utils->AllowOptCommon)
229                                 {
230                                         ServerInstance->SNO->WriteToSnoMask('l',
231                                                 "Optional module lists do not match, some commands may not work globally.%s%s%s%s",
232                                                 diffIneed.length() ? " Not loaded here:" : "", diffIneed.c_str(),
233                                                 diffUneed.length() ? " Not loaded there:" : "", diffUneed.c_str());
234                                 }
235                                 else
236                                 {
237                                         reason = "Optional modules incorrectly matched on these servers, and options::allowmismatch not set.";
238                                         if (diffIneed.length())
239                                                 reason += " Not loaded here:" + diffIneed;
240                                         if (diffUneed.length())
241                                                 reason += " Not loaded there:" + diffUneed;
242                                         this->SendError("CAPAB negotiation failed: "+reason);
243                                         return false;
244                                 }
245                         }
246                 }
247
248                 if (this->capab->CapKeys.find("PROTOCOL") == this->capab->CapKeys.end())
249                 {
250                         reason = "Protocol version not specified";
251                 }
252                 else
253                 {
254                         proto_version = atoi(capab->CapKeys.find("PROTOCOL")->second.c_str());
255                         if (proto_version < MinCompatProtocol)
256                         {
257                                 reason = "Server is using protocol version " + ConvToStr(proto_version) +
258                                         " which is too old to link with this server (version " + ConvToStr(ProtocolVersion)
259                                         + (ProtocolVersion != MinCompatProtocol ? ", links with " + ConvToStr(MinCompatProtocol) + " and above)" : ")");
260                         }
261                 }
262
263                 if(this->capab->CapKeys.find("PREFIX") != this->capab->CapKeys.end() && this->capab->CapKeys.find("PREFIX")->second != ServerInstance->Modes->BuildPrefixes())
264                         reason = "One or more of the prefixes on the remote server are invalid on this server.";
265
266                 if (!capab->ChanModes.empty())
267                 {
268                         if (capab->ChanModes != BuildModeList(MODETYPE_CHANNEL))
269                         {
270                                 std::string diffIneed, diffUneed;
271                                 ListDifference(capab->ChanModes, BuildModeList(MODETYPE_CHANNEL), ' ', diffIneed, diffUneed);
272                                 if (diffIneed.length() || diffUneed.length())
273                                 {
274                                         reason = "Channel modes not matched on these servers.";
275                                         if (diffIneed.length())
276                                                 reason += " Not loaded here:" + diffIneed;
277                                         if (diffUneed.length())
278                                                 reason += " Not loaded there:" + diffUneed;
279                                 }
280                         }
281                 }
282                 else if (this->capab->CapKeys.find("CHANMODES") != this->capab->CapKeys.end())
283                 {
284                         if (this->capab->CapKeys.find("CHANMODES")->second != ServerInstance->Modes->GiveModeList(MASK_CHANNEL))
285                                 reason = "One or more of the channel modes on the remote server are invalid on this server.";
286                 }
287
288                 if (!capab->UserModes.empty())
289                 {
290                         if (capab->UserModes != BuildModeList(MODETYPE_USER))
291                         {
292                                 std::string diffIneed, diffUneed;
293                                 ListDifference(capab->UserModes, BuildModeList(MODETYPE_USER), ' ', diffIneed, diffUneed);
294                                 if (diffIneed.length() || diffUneed.length())
295                                 {
296                                         reason = "User modes not matched on these servers.";
297                                         if (diffIneed.length())
298                                                 reason += " Not loaded here:" + diffIneed;
299                                         if (diffUneed.length())
300                                                 reason += " Not loaded there:" + diffUneed;
301                                 }
302                         }
303                 }
304                 else if (this->capab->CapKeys.find("USERMODES") != this->capab->CapKeys.end())
305                 {
306                         if (this->capab->CapKeys.find("USERMODES")->second != ServerInstance->Modes->GiveModeList(MASK_USER))
307                                 reason = "One or more of the user modes on the remote server are invalid on this server.";
308                 }
309
310                 /* Challenge response, store their challenge for our password */
311                 std::map<std::string,std::string>::iterator n = this->capab->CapKeys.find("CHALLENGE");
312                 if (Utils->ChallengeResponse && (n != this->capab->CapKeys.end()) && (ServerInstance->Modules->Find("m_sha256.so")))
313                 {
314                         /* Challenge-response is on now */
315                         this->SetTheirChallenge(n->second);
316                         if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
317                         {
318                                 this->SendCapabilities(2);
319                                 this->WriteLine(std::string("SERVER ")+ServerInstance->Config->ServerName+" "+this->MakePass(capab->link->SendPass, capab->theirchallenge)+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
320                         }
321                 }
322                 else
323                 {
324                         /* They didnt specify a challenge or we don't have m_sha256.so, we use plaintext */
325                         if (this->LinkState == CONNECTING)
326                         {
327                                 this->SendCapabilities(2);
328                                 this->WriteLine(std::string("SERVER ")+ServerInstance->Config->ServerName+" "+capab->link->SendPass+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
329                         }
330                 }
331
332                 if (reason.length())
333                 {
334                         this->SendError("CAPAB negotiation failed: "+reason);
335                         return false;
336                 }
337         }
338         else if ((params[0] == "MODULES") && (params.size() == 2))
339         {
340                 if (!capab->ModuleList.length())
341                 {
342                         capab->ModuleList = params[1];
343                 }
344                 else
345                 {
346                         capab->ModuleList.push_back(proto_version > 1201 ? ' ' : ',');
347                         capab->ModuleList.append(params[1]);
348                 }
349         }
350         else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
351         {
352                 if (!capab->OptModuleList.length())
353                 {
354                         capab->OptModuleList = params[1];
355                 }
356                 else
357                 {
358                         capab->OptModuleList.push_back(' ');
359                         capab->OptModuleList.append(params[1]);
360                 }
361         }
362         else if ((params[0] == "CHANMODES") && (params.size() == 2))
363         {
364                 capab->ChanModes = params[1];
365         }
366         else if ((params[0] == "USERMODES") && (params.size() == 2))
367         {
368                 capab->UserModes = params[1];
369         }
370         else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
371         {
372                 irc::tokenstream capabs(params[1]);
373                 std::string item;
374                 while (capabs.GetToken(item))
375                 {
376                         /* Process each key/value pair */
377                         std::string::size_type equals = item.find('=');
378                         if (equals != std::string::npos)
379                         {
380                                 std::string var = item.substr(0, equals);
381                                 std::string value = item.substr(equals+1, item.length());
382                                 capab->CapKeys[var] = value;
383                         }
384                 }
385         }
386         return true;
387 }
388