]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/capab.cpp
Remove trailing whitespace from various source files.
[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
24 #include "treeserver.h"
25 #include "utils.h"
26 #include "link.h"
27 #include "main.h"
28
29 struct CompatMod
30 {
31         const char* name;
32         ModuleFlags listflag;
33 };
34
35 static CompatMod compatmods[] =
36 {
37         { "m_watch.so", VF_OPTCOMMON }
38 };
39
40 std::string TreeSocket::MyModules(int filter)
41 {
42         const ModuleManager::ModuleMap& modlist = ServerInstance->Modules->GetModules();
43
44         std::string capabilities;
45         for (ModuleManager::ModuleMap::const_iterator i = modlist.begin(); i != modlist.end(); ++i)
46         {
47                 Module* const mod = i->second;
48                 // 3.0 advertises its settings for the benefit of services
49                 // 2.0 would bork on this
50                 if (proto_version < 1205 && i->second->ModuleSourceFile == "m_kicknorejoin.so")
51                         continue;
52
53                 bool do_compat_include = false;
54                 if (proto_version < 1205)
55                 {
56                         for (size_t j = 0; j < sizeof(compatmods)/sizeof(compatmods[0]); j++)
57                         {
58                                 if ((compatmods[j].listflag & filter) && (mod->ModuleSourceFile == compatmods[j].name))
59                                 {
60                                         do_compat_include = true;
61                                         break;
62                                 }
63                         }
64                 }
65
66                 Version v = i->second->GetVersion();
67                 if ((!do_compat_include) && (!(v.Flags & filter)))
68                         continue;
69
70                 if (i != modlist.begin())
71                         capabilities.push_back(' ');
72                 capabilities.append(i->first);
73                 if (!v.link_data.empty())
74                 {
75                         capabilities.push_back('=');
76                         capabilities.append(v.link_data);
77                 }
78         }
79
80         // If we are linked in a 2.0 server and have an ascii casemapping
81         // advertise it as m_ascii.so from inspircd-extras
82         if ((filter & VF_COMMON) && ServerInstance->Config->CaseMapping == "ascii" && proto_version == 1202)
83         {
84                 if (!capabilities.empty())
85                         capabilities += "m_ascii.so";
86         }
87
88         return capabilities;
89 }
90
91 std::string TreeSocket::BuildModeList(ModeType mtype)
92 {
93         std::vector<std::string> modes;
94         const ModeParser::ModeHandlerMap& mhs = ServerInstance->Modes.GetModes(mtype);
95         for (ModeParser::ModeHandlerMap::const_iterator i = mhs.begin(); i != mhs.end(); ++i)
96         {
97                 const ModeHandler* const mh = i->second;
98                 const PrefixMode* const pm = mh->IsPrefixMode();
99                 std::string mdesc;
100                 if (proto_version != 1202)
101                 {
102                         if (pm)
103                                 mdesc.append("prefix:").append(ConvToStr(pm->GetPrefixRank())).push_back(':');
104                         else if (mh->IsListMode())
105                                 mdesc.append("list:");
106                         else if (mh->NeedsParam(true))
107                                 mdesc.append(mh->NeedsParam(false) ? "param:" : "param-set:");
108                         else
109                                 mdesc.append("simple:");
110                 }
111                 mdesc.append(mh->name);
112                 mdesc.push_back('=');
113                 if (pm)
114                 {
115                         if (pm->GetPrefix())
116                                 mdesc.push_back(pm->GetPrefix());
117                 }
118                 mdesc.push_back(mh->GetModeChar());
119                 modes.push_back(mdesc);
120         }
121         std::sort(modes.begin(), modes.end());
122         return stdalgo::string::join(modes);
123 }
124
125 void TreeSocket::SendCapabilities(int phase)
126 {
127         if (capab->capab_phase >= phase)
128                 return;
129
130         if (capab->capab_phase < 1 && phase >= 1)
131                 WriteLine("CAPAB START " + ConvToStr(ProtocolVersion));
132
133         capab->capab_phase = phase;
134         if (phase < 2)
135                 return;
136
137         const char sep = ' ';
138         irc::sepstream modulelist(MyModules(VF_COMMON), sep);
139         irc::sepstream optmodulelist(MyModules(VF_OPTCOMMON), sep);
140         /* Send module names, split at 509 length */
141         std::string item;
142         std::string line = "CAPAB MODULES :";
143         while (modulelist.GetToken(item))
144         {
145                 if (line.length() + item.length() + 1 > 509)
146                 {
147                         this->WriteLine(line);
148                         line = "CAPAB MODULES :";
149                 }
150
151                 if (line != "CAPAB MODULES :")
152                         line.push_back(sep);
153
154                 line.append(item);
155         }
156         if (line != "CAPAB MODULES :")
157                 this->WriteLine(line);
158
159         line = "CAPAB MODSUPPORT :";
160         while (optmodulelist.GetToken(item))
161         {
162                 if (line.length() + item.length() + 1 > 509)
163                 {
164                         this->WriteLine(line);
165                         line = "CAPAB MODSUPPORT :";
166                 }
167
168                 if (line != "CAPAB MODSUPPORT :")
169                         line.push_back(sep);
170
171                 line.append(item);
172         }
173         if (line != "CAPAB MODSUPPORT :")
174                 this->WriteLine(line);
175
176         WriteLine("CAPAB CHANMODES :" + BuildModeList(MODETYPE_CHANNEL));
177         WriteLine("CAPAB USERMODES :" + BuildModeList(MODETYPE_USER));
178
179         std::string extra;
180         /* Do we have sha256 available? If so, we send a challenge */
181         if (ServerInstance->Modules->FindService(SERVICE_DATA, "hash/sha256"))
182         {
183                 SetOurChallenge(ServerInstance->GenRandomStr(20));
184                 extra = " CHALLENGE=" + this->GetOurChallenge();
185         }
186
187         // 2.0 needs these keys.
188         if (proto_version == 1202)
189         {
190                 extra.append(" PROTOCOL="+ConvToStr(ProtocolVersion))
191                         .append(" MAXGECOS="+ConvToStr(ServerInstance->Config->Limits.MaxReal))
192                         .append(" CHANMODES="+ServerInstance->Modes->GiveModeList(MODETYPE_CHANNEL))
193                         .append(" USERMODES="+ServerInstance->Modes->GiveModeList(MODETYPE_USER))
194                         .append(" PREFIX="+ ServerInstance->Modes->BuildPrefixes());
195         }
196
197         this->WriteLine("CAPAB CAPABILITIES " /* Preprocessor does this one. */
198                         ":NICKMAX="+ConvToStr(ServerInstance->Config->Limits.NickMax)+
199                         " CHANMAX="+ConvToStr(ServerInstance->Config->Limits.ChanMax)+
200                         " MAXMODES="+ConvToStr(ServerInstance->Config->Limits.MaxModes)+
201                         " IDENTMAX="+ConvToStr(ServerInstance->Config->Limits.IdentMax)+
202                         " MAXQUIT="+ConvToStr(ServerInstance->Config->Limits.MaxQuit)+
203                         " MAXTOPIC="+ConvToStr(ServerInstance->Config->Limits.MaxTopic)+
204                         " MAXKICK="+ConvToStr(ServerInstance->Config->Limits.MaxKick)+
205                         " MAXREAL="+ConvToStr(ServerInstance->Config->Limits.MaxReal)+
206                         " MAXAWAY="+ConvToStr(ServerInstance->Config->Limits.MaxAway)+
207                         " MAXHOST="+ConvToStr(ServerInstance->Config->Limits.MaxHost)+
208                         extra+
209                         " CASEMAPPING="+ServerInstance->Config->CaseMapping+
210                         // XXX: Advertise the presence or absence of m_globops in CAPAB CAPABILITIES.
211                         // Services want to know about it, and since m_globops was not marked as VF_(OPT)COMMON
212                         // in 2.0, we advertise it here to not break linking to previous versions.
213                         // Protocol version 1201 (1.2) does not have this issue because we advertise m_globops
214                         // to 1201 protocol servers irrespectively of its module flags.
215                         (ServerInstance->Modules->Find("m_globops.so") != NULL ? " GLOBOPS=1" : " GLOBOPS=0")
216                         );
217
218         this->WriteLine("CAPAB END");
219 }
220
221 /* Isolate and return the elements that are different between two comma seperated lists */
222 void TreeSocket::ListDifference(const std::string &one, const std::string &two, char sep,
223                 std::string& mleft, std::string& mright)
224 {
225         std::set<std::string> values;
226         irc::sepstream sepleft(one, sep);
227         irc::sepstream sepright(two, sep);
228         std::string item;
229         while (sepleft.GetToken(item))
230         {
231                 values.insert(item);
232         }
233         while (sepright.GetToken(item))
234         {
235                 if (!values.erase(item))
236                 {
237                         mright.push_back(sep);
238                         mright.append(item);
239                 }
240         }
241         for(std::set<std::string>::iterator i = values.begin(); i != values.end(); ++i)
242         {
243                 mleft.push_back(sep);
244                 mleft.append(*i);
245         }
246 }
247
248 bool TreeSocket::Capab(const CommandBase::Params& params)
249 {
250         if (params.size() < 1)
251         {
252                 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
253                 return false;
254         }
255         if (params[0] == "START")
256         {
257                 capab->ModuleList.clear();
258                 capab->OptModuleList.clear();
259                 capab->CapKeys.clear();
260                 if (params.size() > 1)
261                         proto_version = ConvToNum<unsigned int>(params[1]);
262
263                 if (proto_version < MinCompatProtocol)
264                 {
265                         SendError("CAPAB negotiation failed: Server is using protocol version " + (proto_version ? ConvToStr(proto_version) : "1201 or older")
266                                 + " which is too old to link with this server (version " + ConvToStr(ProtocolVersion)
267                                 + (ProtocolVersion != MinCompatProtocol ? ", links with " + ConvToStr(MinCompatProtocol) + " and above)" : ")"));
268                         return false;
269                 }
270
271                 // Special case, may be removed in the future
272                 if (proto_version == 1203 || proto_version == 1204)
273                 {
274                         SendError("CAPAB negotiation failed: InspIRCd 2.1 beta is not supported");
275                         return false;
276                 }
277
278                 SendCapabilities(2);
279         }
280         else if (params[0] == "END")
281         {
282                 std::string reason;
283                 /* Compare ModuleList and check CapKeys */
284                 if ((this->capab->ModuleList != this->MyModules(VF_COMMON)) && (this->capab->ModuleList.length()))
285                 {
286                         std::string diffIneed, diffUneed;
287                         ListDifference(this->capab->ModuleList, this->MyModules(VF_COMMON), ' ', diffIneed, diffUneed);
288                         if (diffIneed.length() || diffUneed.length())
289                         {
290                                 reason = "Modules incorrectly matched on these servers.";
291                                 if (diffIneed.length())
292                                         reason += " Not loaded here:" + diffIneed;
293                                 if (diffUneed.length())
294                                         reason += " Not loaded there:" + diffUneed;
295                                 this->SendError("CAPAB negotiation failed: "+reason);
296                                 return false;
297                         }
298                 }
299
300                 if (this->capab->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->capab->OptModuleList.length())
301                 {
302                         std::string diffIneed, diffUneed;
303                         ListDifference(this->capab->OptModuleList, this->MyModules(VF_OPTCOMMON), ' ', diffIneed, diffUneed);
304                         if (diffIneed.length() || diffUneed.length())
305                         {
306                                 if (Utils->AllowOptCommon)
307                                 {
308                                         ServerInstance->SNO->WriteToSnoMask('l',
309                                                 "Optional module lists do not match, some commands may not work globally.%s%s%s%s",
310                                                 diffIneed.length() ? " Not loaded here:" : "", diffIneed.c_str(),
311                                                 diffUneed.length() ? " Not loaded there:" : "", diffUneed.c_str());
312                                 }
313                                 else
314                                 {
315                                         reason = "Optional modules incorrectly matched on these servers and <options:allowmismatch> is not enabled.";
316                                         if (diffIneed.length())
317                                                 reason += " Not loaded here:" + diffIneed;
318                                         if (diffUneed.length())
319                                                 reason += " Not loaded there:" + diffUneed;
320                                         this->SendError("CAPAB negotiation failed: "+reason);
321                                         return false;
322                                 }
323                         }
324                 }
325
326                 if (!capab->ChanModes.empty())
327                 {
328                         if (capab->ChanModes != BuildModeList(MODETYPE_CHANNEL))
329                         {
330                                 std::string diffIneed, diffUneed;
331                                 ListDifference(capab->ChanModes, BuildModeList(MODETYPE_CHANNEL), ' ', diffIneed, diffUneed);
332                                 if (diffIneed.length() || diffUneed.length())
333                                 {
334                                         reason = "Channel modes not matched on these servers.";
335                                         if (diffIneed.length())
336                                                 reason += " Not loaded here:" + diffIneed;
337                                         if (diffUneed.length())
338                                                 reason += " Not loaded there:" + diffUneed;
339                                 }
340                         }
341                 }
342                 else if (proto_version == 1202)
343                 {
344                         if (this->capab->CapKeys.find("CHANMODES") != this->capab->CapKeys.end())
345                         {
346                                 if (this->capab->CapKeys.find("CHANMODES")->second != ServerInstance->Modes->GiveModeList(MODETYPE_CHANNEL))
347                                         reason = "One or more of the channel modes on the remote server are invalid on this server.";
348                         }
349
350                         else if (this->capab->CapKeys.find("PREFIX") != this->capab->CapKeys.end())
351                         {
352                                 if (this->capab->CapKeys.find("PREFIX")->second != ServerInstance->Modes->BuildPrefixes())
353                                         reason = "One or more of the prefixes on the remote server are invalid on this server.";
354                         }
355                 }
356
357                 if (!reason.empty())
358                 {
359                         this->SendError("CAPAB negotiation failed: " + reason);
360                         return false;
361                 }
362
363                 if (!capab->UserModes.empty())
364                 {
365                         if (capab->UserModes != BuildModeList(MODETYPE_USER))
366                         {
367                                 std::string diffIneed, diffUneed;
368                                 ListDifference(capab->UserModes, BuildModeList(MODETYPE_USER), ' ', diffIneed, diffUneed);
369                                 if (diffIneed.length() || diffUneed.length())
370                                 {
371                                         reason = "User modes not matched on these servers.";
372                                         if (diffIneed.length())
373                                                 reason += " Not loaded here:" + diffIneed;
374                                         if (diffUneed.length())
375                                                 reason += " Not loaded there:" + diffUneed;
376                                 }
377                         }
378                 }
379                 else if (proto_version == 1202 && this->capab->CapKeys.find("USERMODES") != this->capab->CapKeys.end())
380                 {
381                         if (this->capab->CapKeys.find("USERMODES")->second != ServerInstance->Modes->GiveModeList(MODETYPE_USER))
382                                 reason = "One or more of the user modes on the remote server are invalid on this server.";
383                 }
384
385                 if (!reason.empty())
386                 {
387                         this->SendError("CAPAB negotiation failed: " + reason);
388                         return false;
389                 }
390
391                 if (this->capab->CapKeys.find("CASEMAPPING") != this->capab->CapKeys.end())
392                 {
393                         const std::string casemapping = this->capab->CapKeys.find("CASEMAPPING")->second;
394                         if (casemapping != ServerInstance->Config->CaseMapping)
395                         {
396                                 reason = "The casemapping of the remote server differs to that of the local server."
397                                         " Local casemapping: " + ServerInstance->Config->CaseMapping +
398                                         " Remote casemapping: " + casemapping;
399                                 this->SendError("CAPAB negotiation failed: " + reason);
400                                 return false;
401                         }
402                 }
403
404                 /* Challenge response, store their challenge for our password */
405                 std::map<std::string,std::string>::iterator n = this->capab->CapKeys.find("CHALLENGE");
406                 if ((n != this->capab->CapKeys.end()) && (ServerInstance->Modules->FindService(SERVICE_DATA, "hash/sha256")))
407                 {
408                         /* Challenge-response is on now */
409                         this->SetTheirChallenge(n->second);
410                         if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
411                         {
412                                 this->SendCapabilities(2);
413                                 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+this->MakePass(capab->link->SendPass, capab->theirchallenge)+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
414                         }
415                 }
416                 else
417                 {
418                         // They didn't specify a challenge or we don't have sha256, we use plaintext
419                         if (this->LinkState == CONNECTING)
420                         {
421                                 this->SendCapabilities(2);
422                                 this->WriteLine("SERVER "+ServerInstance->Config->ServerName+" "+capab->link->SendPass+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
423                         }
424                 }
425         }
426         else if ((params[0] == "MODULES") && (params.size() == 2))
427         {
428                 if (!capab->ModuleList.length())
429                 {
430                         capab->ModuleList = params[1];
431                 }
432                 else
433                 {
434                         capab->ModuleList.push_back(' ');
435                         capab->ModuleList.append(params[1]);
436                 }
437         }
438         else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
439         {
440                 if (!capab->OptModuleList.length())
441                 {
442                         capab->OptModuleList = params[1];
443                 }
444                 else
445                 {
446                         capab->OptModuleList.push_back(' ');
447                         capab->OptModuleList.append(params[1]);
448                 }
449         }
450         else if ((params[0] == "CHANMODES") && (params.size() == 2))
451         {
452                 capab->ChanModes = params[1];
453         }
454         else if ((params[0] == "USERMODES") && (params.size() == 2))
455         {
456                 capab->UserModes = params[1];
457         }
458         else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
459         {
460                 irc::spacesepstream capabs(params[1]);
461                 std::string item;
462                 while (capabs.GetToken(item))
463                 {
464                         /* Process each key/value pair */
465                         std::string::size_type equals = item.find('=');
466                         if (equals != std::string::npos)
467                         {
468                                 std::string var(item, 0, equals);
469                                 std::string value(item, equals+1);
470                                 capab->CapKeys[var] = value;
471                         }
472                 }
473         }
474         return true;
475 }