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