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