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