]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/capab.cpp
m_spanningtree Advertise the availability of m_globops in CAPAB CAPABILITIES
[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 (std::vector<std::string>::const_iterator i = modlist.begin(); i != modlist.end(); ++i)
41         {
42                 if (i != modlist.begin())
43                         capabilities.push_back(proto_version > 1201 ? ' ' : ',');
44                 capabilities.append(*i);
45                 Module* m = ServerInstance->Modules->Find(*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                         // XXX: Advertise the presence or absence of m_globops in CAPAB CAPABILITIES.
161                         // Services want to know about it, and since m_globops was not marked as VF_(OPT)COMMON
162                         // in 2.0, we advertise it here to not break linking to previous versions.
163                         // Protocol version 1201 (1.2) does not have this issue because we advertise m_globops
164                         // to 1202 protocol servers irrespectively of its module flags.
165                         (ServerInstance->Modules->Find("m_globops.so") != NULL ? " GLOBOPS=1" : " GLOBOPS=0")+
166                         " SVSPART=1");
167
168         this->WriteLine("CAPAB END");
169 }
170
171 /* Isolate and return the elements that are different between two comma seperated lists */
172 void TreeSocket::ListDifference(const std::string &one, const std::string &two, char sep,
173                 std::string& mleft, std::string& mright)
174 {
175         std::set<std::string> values;
176         irc::sepstream sepleft(one, sep);
177         irc::sepstream sepright(two, sep);
178         std::string item;
179         while (sepleft.GetToken(item))
180         {
181                 values.insert(item);
182         }
183         while (sepright.GetToken(item))
184         {
185                 if (!values.erase(item))
186                 {
187                         mright.push_back(sep);
188                         mright.append(item);
189                 }
190         }
191         for(std::set<std::string>::iterator i = values.begin(); i != values.end(); ++i)
192         {
193                 mleft.push_back(sep);
194                 mleft.append(*i);
195         }
196 }
197
198 bool TreeSocket::Capab(const parameterlist &params)
199 {
200         if (params.size() < 1)
201         {
202                 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
203                 return false;
204         }
205         if (params[0] == "START")
206         {
207                 capab->ModuleList.clear();
208                 capab->OptModuleList.clear();
209                 capab->CapKeys.clear();
210                 if (params.size() > 1)
211                         proto_version = atoi(params[1].c_str());
212                 SendCapabilities(2);
213         }
214         else if (params[0] == "END")
215         {
216                 std::string reason;
217                 /* Compare ModuleList and check CapKeys */
218                 if ((this->capab->ModuleList != this->MyModules(VF_COMMON)) && (this->capab->ModuleList.length()))
219                 {
220                         std::string diffIneed, diffUneed;
221                         ListDifference(this->capab->ModuleList, this->MyModules(VF_COMMON), proto_version > 1201 ? ' ' : ',', diffIneed, diffUneed);
222                         if (diffIneed.length() || diffUneed.length())
223                         {
224                                 reason = "Modules incorrectly matched on these servers.";
225                                 if (diffIneed.length())
226                                         reason += " Not loaded here:" + diffIneed;
227                                 if (diffUneed.length())
228                                         reason += " Not loaded there:" + diffUneed;
229                                 this->SendError("CAPAB negotiation failed: "+reason);
230                                 return false;
231                         }
232                 }
233                 if (this->capab->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->capab->OptModuleList.length())
234                 {
235                         std::string diffIneed, diffUneed;
236                         ListDifference(this->capab->OptModuleList, this->MyModules(VF_OPTCOMMON), ' ', diffIneed, diffUneed);
237                         if (diffIneed.length() || diffUneed.length())
238                         {
239                                 if (Utils->AllowOptCommon)
240                                 {
241                                         ServerInstance->SNO->WriteToSnoMask('l',
242                                                 "Optional module lists do not match, some commands may not work globally.%s%s%s%s",
243                                                 diffIneed.length() ? " Not loaded here:" : "", diffIneed.c_str(),
244                                                 diffUneed.length() ? " Not loaded there:" : "", diffUneed.c_str());
245                                 }
246                                 else
247                                 {
248                                         reason = "Optional modules incorrectly matched on these servers, and options::allowmismatch not set.";
249                                         if (diffIneed.length())
250                                                 reason += " Not loaded here:" + diffIneed;
251                                         if (diffUneed.length())
252                                                 reason += " Not loaded there:" + diffUneed;
253                                         this->SendError("CAPAB negotiation failed: "+reason);
254                                         return false;
255                                 }
256                         }
257                 }
258
259                 if (this->capab->CapKeys.find("PROTOCOL") == this->capab->CapKeys.end())
260                 {
261                         reason = "Protocol version not specified";
262                 }
263                 else
264                 {
265                         proto_version = atoi(capab->CapKeys.find("PROTOCOL")->second.c_str());
266                         if (proto_version < MinCompatProtocol)
267                         {
268                                 reason = "Server is using protocol version " + ConvToStr(proto_version) +
269                                         " which is too old to link with this server (version " + ConvToStr(ProtocolVersion)
270                                         + (ProtocolVersion != MinCompatProtocol ? ", links with " + ConvToStr(MinCompatProtocol) + " and above)" : ")");
271                         }
272                 }
273
274                 if(this->capab->CapKeys.find("PREFIX") != this->capab->CapKeys.end() && this->capab->CapKeys.find("PREFIX")->second != ServerInstance->Modes->BuildPrefixes())
275                         reason = "One or more of the prefixes on the remote server are invalid on this server.";
276
277                 if (!capab->ChanModes.empty())
278                 {
279                         if (capab->ChanModes != BuildModeList(MODETYPE_CHANNEL))
280                         {
281                                 std::string diffIneed, diffUneed;
282                                 ListDifference(capab->ChanModes, BuildModeList(MODETYPE_CHANNEL), ' ', diffIneed, diffUneed);
283                                 if (diffIneed.length() || diffUneed.length())
284                                 {
285                                         reason = "Channel modes not matched on these servers.";
286                                         if (diffIneed.length())
287                                                 reason += " Not loaded here:" + diffIneed;
288                                         if (diffUneed.length())
289                                                 reason += " Not loaded there:" + diffUneed;
290                                 }
291                         }
292                 }
293                 else if (this->capab->CapKeys.find("CHANMODES") != this->capab->CapKeys.end())
294                 {
295                         if (this->capab->CapKeys.find("CHANMODES")->second != ServerInstance->Modes->GiveModeList(MASK_CHANNEL))
296                                 reason = "One or more of the channel modes on the remote server are invalid on this server.";
297                 }
298
299                 if (!capab->UserModes.empty())
300                 {
301                         if (capab->UserModes != BuildModeList(MODETYPE_USER))
302                         {
303                                 std::string diffIneed, diffUneed;
304                                 ListDifference(capab->UserModes, BuildModeList(MODETYPE_USER), ' ', diffIneed, diffUneed);
305                                 if (diffIneed.length() || diffUneed.length())
306                                 {
307                                         reason = "User modes not matched on these servers.";
308                                         if (diffIneed.length())
309                                                 reason += " Not loaded here:" + diffIneed;
310                                         if (diffUneed.length())
311                                                 reason += " Not loaded there:" + diffUneed;
312                                 }
313                         }
314                 }
315                 else if (this->capab->CapKeys.find("USERMODES") != this->capab->CapKeys.end())
316                 {
317                         if (this->capab->CapKeys.find("USERMODES")->second != ServerInstance->Modes->GiveModeList(MASK_USER))
318                                 reason = "One or more of the user modes on the remote server are invalid on this server.";
319                 }
320
321                 /* Challenge response, store their challenge for our password */
322                 std::map<std::string,std::string>::iterator n = this->capab->CapKeys.find("CHALLENGE");
323                 if (Utils->ChallengeResponse && (n != this->capab->CapKeys.end()) && (ServerInstance->Modules->Find("m_sha256.so")))
324                 {
325                         /* Challenge-response is on now */
326                         this->SetTheirChallenge(n->second);
327                         if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
328                         {
329                                 this->SendCapabilities(2);
330                                 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+this->MakePass(capab->link->SendPass, capab->theirchallenge)+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
331                         }
332                 }
333                 else
334                 {
335                         /* They didnt specify a challenge or we don't have m_sha256.so, we use plaintext */
336                         if (this->LinkState == CONNECTING)
337                         {
338                                 this->SendCapabilities(2);
339                                 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+capab->link->SendPass+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
340                         }
341                 }
342
343                 if (reason.length())
344                 {
345                         this->SendError("CAPAB negotiation failed: "+reason);
346                         return false;
347                 }
348         }
349         else if ((params[0] == "MODULES") && (params.size() == 2))
350         {
351                 if (!capab->ModuleList.length())
352                 {
353                         capab->ModuleList = params[1];
354                 }
355                 else
356                 {
357                         capab->ModuleList.push_back(proto_version > 1201 ? ' ' : ',');
358                         capab->ModuleList.append(params[1]);
359                 }
360         }
361         else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
362         {
363                 if (!capab->OptModuleList.length())
364                 {
365                         capab->OptModuleList = params[1];
366                 }
367                 else
368                 {
369                         capab->OptModuleList.push_back(' ');
370                         capab->OptModuleList.append(params[1]);
371                 }
372         }
373         else if ((params[0] == "CHANMODES") && (params.size() == 2))
374         {
375                 capab->ChanModes = params[1];
376         }
377         else if ((params[0] == "USERMODES") && (params.size() == 2))
378         {
379                 capab->UserModes = params[1];
380         }
381         else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
382         {
383                 irc::tokenstream capabs(params[1]);
384                 std::string item;
385                 while (capabs.GetToken(item))
386                 {
387                         /* Process each key/value pair */
388                         std::string::size_type equals = item.find('=');
389                         if (equals != std::string::npos)
390                         {
391                                 std::string var = item.substr(0, equals);
392                                 std::string value = item.substr(equals+1, item.length());
393                                 capab->CapKeys[var] = value;
394                         }
395                 }
396         }
397         return true;
398 }
399