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