]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/capab.cpp
Remove VF_COMMON from mode-provider modules (no longer needed due to better CAPAB...
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / capab.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "xline.h"
16
17 #include "treesocket.h"
18 #include "treeserver.h"
19 #include "utils.h"
20 #include "main.h"
21
22 /* $ModDep: m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/treesocket.h */
23
24 std::string TreeSocket::MyModules(int filter)
25 {
26         std::vector<std::string> modlist = ServerInstance->Modules->GetAllModuleNames(filter);
27
28         if (filter == VF_COMMON && proto_version != ProtocolVersion)
29                 CompatAddModules(modlist);
30
31         std::string capabilities;
32         sort(modlist.begin(),modlist.end());
33         for (unsigned int i = 0; i < modlist.size(); i++)
34         {
35                 if (i)
36                         capabilities.push_back(',');
37                 capabilities.append(modlist[i]);
38                 Module* m = ServerInstance->Modules->Find(modlist[i]);
39                 if (m && proto_version >= 1202)
40                 {
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         }
49         return capabilities;
50 }
51
52 static std::string BuildModeList(ModeType type)
53 {
54         std::string line;
55         for(char c='A'; c <= 'z'; c++)
56         {
57                 ModeHandler* mh = ServerInstance->Modes->FindMode(c, type);
58                 if (mh)
59                 {
60                         if (!line.empty())
61                                 line.push_back(',');
62                         line.append(mh->name);
63                         line.push_back('=');
64                         line.push_back(c);
65                 }
66         }
67         return line;
68 }
69
70 void TreeSocket::SendCapabilities(int phase)
71 {
72         if (capab->capab_phase >= phase)
73                 return;
74
75         if (capab->capab_phase < 1 && phase >= 1)
76                 WriteLine("CAPAB START " + ConvToStr(ProtocolVersion));
77
78         capab->capab_phase = phase;
79         if (phase < 2)
80                 return;
81
82         irc::commasepstream modulelist(MyModules(VF_COMMON));
83         irc::commasepstream optmodulelist(MyModules(VF_OPTCOMMON));
84         /* Send module names, split at 509 length */
85         std::string item;
86         std::string line = "CAPAB MODULES ";
87         while (modulelist.GetToken(item))
88         {
89                 if (line.length() + item.length() + 1 > 509)
90                 {
91                         this->WriteLine(line);
92                         line = "CAPAB MODULES ";
93                 }
94
95                 if (line != "CAPAB MODULES ")
96                         line.append(",");
97
98                 line.append(item);
99         }
100         if (line != "CAPAB MODULES ")
101                 this->WriteLine(line);
102
103         line = "CAPAB MODSUPPORT ";
104         while (optmodulelist.GetToken(item))
105         {
106                 if (line.length() + item.length() + 1 > 509)
107                 {
108                         this->WriteLine(line);
109                         line = "CAPAB MODSUPPORT ";
110                 }
111
112                 if (line != "CAPAB MODSUPPORT ")
113                         line.append(",");
114
115                 line.append(item);
116         }
117         if (line != "CAPAB MODSUPPORT ")
118                 this->WriteLine(line);
119
120         line = "CAPAB CHANMODES " + BuildModeList(MODETYPE_CHANNEL);
121         if (line != "CAPAB CHANMODES ")
122                 this->WriteLine(line);
123
124         line = "CAPAB USERMODES " + BuildModeList(MODETYPE_USER);
125         if (line != "CAPAB USERMODES ")
126                 this->WriteLine(line);
127
128         int ip6 = 0;
129 #ifdef IPV6
130         ip6 = 1;
131 #endif
132         std::string extra;
133         /* Do we have sha256 available? If so, we send a challenge */
134         if (Utils->ChallengeResponse && (ServerInstance->Modules->Find("m_sha256.so")))
135         {
136                 this->SetOurChallenge(RandString(20));
137                 extra = " CHALLENGE=" + this->GetOurChallenge();
138         }
139
140         this->WriteLine("CAPAB CAPABILITIES " /* Preprocessor does this one. */
141                         ":NICKMAX="+ConvToStr(ServerInstance->Config->Limits.NickMax)+
142                         " CHANMAX="+ConvToStr(ServerInstance->Config->Limits.ChanMax)+
143                         " MAXMODES="+ConvToStr(ServerInstance->Config->Limits.MaxModes)+
144                         " IDENTMAX="+ConvToStr(ServerInstance->Config->Limits.IdentMax)+
145                         " MAXQUIT="+ConvToStr(ServerInstance->Config->Limits.MaxQuit)+
146                         " MAXTOPIC="+ConvToStr(ServerInstance->Config->Limits.MaxTopic)+
147                         " MAXKICK="+ConvToStr(ServerInstance->Config->Limits.MaxKick)+
148                         " MAXGECOS="+ConvToStr(ServerInstance->Config->Limits.MaxGecos)+
149                         " MAXAWAY="+ConvToStr(ServerInstance->Config->Limits.MaxAway)+
150                         " IP6NATIVE="+ConvToStr(ip6)+
151                         " IP6SUPPORT=1"+
152                         " PROTOCOL="+ConvToStr(ProtocolVersion)+extra+
153                         " PREFIX="+ServerInstance->Modes->BuildPrefixes()+
154                         " CHANMODES="+ServerInstance->Modes->GiveModeList(MASK_CHANNEL)+
155                         " USERMODES="+ServerInstance->Modes->GiveModeList(MASK_USER)+
156                         " SVSPART=1");
157
158         this->WriteLine("CAPAB END");
159 }
160
161 /* Check a comma seperated list for an item */
162 bool TreeSocket::HasItem(const std::string &list, const std::string &item)
163 {
164         irc::commasepstream seplist(list);
165         std::string item2;
166
167         while (seplist.GetToken(item2))
168         {
169                 if (item2 == item)
170                         return true;
171         }
172         return false;
173 }
174
175 /* Isolate and return the elements that are different between two comma seperated lists */
176 std::string TreeSocket::ListDifference(const std::string &one, const std::string &two)
177 {
178         irc::commasepstream list_one(one);
179         std::string item;
180         std::string result;
181         while (list_one.GetToken(item))
182         {
183                 if (!HasItem(two, item))
184                 {
185                         result.append(" ");
186                         result.append(item);
187                 }
188         }
189         return result;
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 = ListDifference(this->capab->ModuleList, this->MyModules(VF_COMMON));
215                         std::string diffUneed = ListDifference(this->MyModules(VF_COMMON), this->capab->ModuleList);
216                         if (diffIneed.length() == 0 && diffUneed.length() == 0)
217                         {
218                                 reason = "Module list in CAPAB is not alphabetically ordered, cannot compare lists.";
219                         }
220                         else
221                         {
222                                 reason = "Modules incorrectly matched on these servers.";
223                                 if (diffIneed.length())
224                                         reason += " Not loaded here:" + diffIneed;
225                                 if (diffUneed.length())
226                                         reason += " Not loaded there:" + diffUneed;
227                         }
228                         this->SendError("CAPAB negotiation failed: "+reason);
229                         return false;
230                 }
231                 if (this->capab->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->capab->OptModuleList.length())
232                 {
233                         std::string diffIneed = ListDifference(this->capab->OptModuleList, this->MyModules(VF_OPTCOMMON));
234                         std::string diffUneed = ListDifference(this->MyModules(VF_OPTCOMMON), this->capab->OptModuleList);
235                         if (diffIneed.length() == 0 && diffUneed.length() == 0)
236                         {
237                                 reason = "Optional Module list in CAPAB is not alphabetically ordered, cannot compare lists.";
238                         }
239                         else 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                 if (this->capab->CapKeys.find("PROTOCOL") == this->capab->CapKeys.end())
259                 {
260                         reason = "Protocol version not specified";
261                 }
262                 else
263                 {
264                         proto_version = atoi(capab->CapKeys.find("PROTOCOL")->second.c_str());
265                         if (proto_version < MinCompatProtocol)
266                         {
267                                 reason = "Server is using protocol version " + ConvToStr(proto_version) +
268                                         " which is too old to link with this server (version " + ConvToStr(ProtocolVersion)
269                                         + (ProtocolVersion != MinCompatProtocol ? ", links with " + ConvToStr(MinCompatProtocol) + " and above)" : ")");
270                         }
271                 }
272
273                 if(this->capab->CapKeys.find("PREFIX") != this->capab->CapKeys.end() && this->capab->CapKeys.find("PREFIX")->second != ServerInstance->Modes->BuildPrefixes())
274                         reason = "One or more of the prefixes on the remote server are invalid on this server.";
275
276                 if (!capab->ChanModes.empty())
277                 {
278                         if (capab->ChanModes != BuildModeList(MODETYPE_CHANNEL))
279                         {
280                                 std::string diffIneed = ListDifference(capab->ChanModes, BuildModeList(MODETYPE_CHANNEL));
281                                 std::string diffUneed = ListDifference(BuildModeList(MODETYPE_CHANNEL), capab->ChanModes);
282                                 reason = "Channel modes not matched on these servers.";
283                                 if (diffIneed.length())
284                                         reason += " Not loaded here:" + diffIneed;
285                                 if (diffUneed.length())
286                                         reason += " Not loaded there:" + diffUneed;
287                         }
288                 }
289                 else if (this->capab->CapKeys.find("CHANMODES") != this->capab->CapKeys.end())
290                 {
291                         if (this->capab->CapKeys.find("CHANMODES")->second != ServerInstance->Modes->GiveModeList(MASK_CHANNEL))
292                                 reason = "One or more of the channel modes on the remote server are invalid on this server.";
293                 }
294
295                 if (!capab->UserModes.empty())
296                 {
297                         if (capab->UserModes != BuildModeList(MODETYPE_USER))
298                         {
299                                 std::string diffIneed = ListDifference(capab->UserModes, BuildModeList(MODETYPE_USER));
300                                 std::string diffUneed = ListDifference(BuildModeList(MODETYPE_USER), capab->UserModes);
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                 else if (this->capab->CapKeys.find("USERMODES") != this->capab->CapKeys.end())
309                 {
310                         if (this->capab->CapKeys.find("USERMODES")->second != ServerInstance->Modes->GiveModeList(MASK_USER))
311                                 reason = "One or more of the user modes on the remote server are invalid on this server.";
312                 }
313
314                 /* Challenge response, store their challenge for our password */
315                 std::map<std::string,std::string>::iterator n = this->capab->CapKeys.find("CHALLENGE");
316                 if (Utils->ChallengeResponse && (n != this->capab->CapKeys.end()) && (ServerInstance->Modules->Find("m_sha256.so")))
317                 {
318                         /* Challenge-response is on now */
319                         this->SetTheirChallenge(n->second);
320                         if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
321                         {
322                                 this->SendCapabilities(2);
323                                 this->WriteLine(std::string("SERVER ")+ServerInstance->Config->ServerName+" "+this->MakePass(capab->OutboundPass, this->GetTheirChallenge())+" 0 "+
324                                                 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->OutboundPass+" 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 (!this->capab->ModuleList.length())
346                 {
347                         this->capab->ModuleList.append(params[1]);
348                 }
349                 else
350                 {
351                         this->capab->ModuleList.append(",");
352                         this->capab->ModuleList.append(params[1]);
353                 }
354         }
355         else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
356         {
357                 if (!this->capab->OptModuleList.length())
358                 {
359                         this->capab->OptModuleList.append(params[1]);
360                 }
361                 else
362                 {
363                         this->capab->OptModuleList.append(",");
364                         this->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                 bool more = true;
380                 while ((more = capabs.GetToken(item)))
381                 {
382                         /* Process each key/value pair */
383                         std::string::size_type equals = item.rfind('=');
384                         if (equals != std::string::npos)
385                         {
386                                 std::string var = item.substr(0, equals);
387                                 std::string value = item.substr(equals+1, item.length());
388                                 capab->CapKeys[var] = value;
389                         }
390                 }
391         }
392         return true;
393 }
394