]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/capab.cpp
Remove spanningtree override of /LUSERS
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / capab.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 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 = capabilities + ",";
37                 capabilities = capabilities + modlist[i];
38         }
39         return capabilities;
40 }
41
42 void TreeSocket::SendCapabilities(int phase)
43 {
44         if (capab_phase >= phase)
45                 return;
46
47         if (capab_phase < 1 && phase >= 1)
48                 WriteLine("CAPAB START " + ConvToStr(ProtocolVersion));
49
50         capab_phase = phase;
51         if (phase < 2)
52                 return;
53
54         irc::commasepstream modulelist(MyModules(VF_COMMON));
55         irc::commasepstream optmodulelist(MyModules(VF_OPTCOMMON));
56         /* Send module names, split at 509 length */
57         std::string item;
58         std::string line = "CAPAB MODULES ";
59         while (modulelist.GetToken(item))
60         {
61                 if (line.length() + item.length() + 1 > 509)
62                 {
63                         this->WriteLine(line);
64                         line = "CAPAB MODULES ";
65                 }
66
67                 if (line != "CAPAB MODULES ")
68                         line.append(",");
69
70                 line.append(item);
71         }
72         if (line != "CAPAB MODULES ")
73                 this->WriteLine(line);
74
75         line = "CAPAB MODSUPPORT ";
76         while (optmodulelist.GetToken(item))
77         {
78                 if (line.length() + item.length() + 1 > 509)
79                 {
80                         this->WriteLine(line);
81                         line = "CAPAB MODSUPPORT ";
82                 }
83
84                 if (line != "CAPAB MODSUPPORT ")
85                         line.append(",");
86
87                 line.append(item);
88         }
89         if (line != "CAPAB MODSUPPORT ")
90                 this->WriteLine(line);
91
92
93         int ip6 = 0;
94 #ifdef IPV6
95         ip6 = 1;
96 #endif
97         std::string extra;
98         /* Do we have sha256 available? If so, we send a challenge */
99         if (Utils->ChallengeResponse && (ServerInstance->Modules->Find("m_sha256.so")))
100         {
101                 this->SetOurChallenge(RandString(20));
102                 extra = " CHALLENGE=" + this->GetOurChallenge();
103         }
104
105         this->WriteLine("CAPAB CAPABILITIES " /* Preprocessor does this one. */
106                         ":NICKMAX="+ConvToStr(ServerInstance->Config->Limits.NickMax)+
107                         " CHANMAX="+ConvToStr(ServerInstance->Config->Limits.ChanMax)+
108                         " MAXMODES="+ConvToStr(ServerInstance->Config->Limits.MaxModes)+
109                         " IDENTMAX="+ConvToStr(ServerInstance->Config->Limits.IdentMax)+
110                         " MAXQUIT="+ConvToStr(ServerInstance->Config->Limits.MaxQuit)+
111                         " MAXTOPIC="+ConvToStr(ServerInstance->Config->Limits.MaxTopic)+
112                         " MAXKICK="+ConvToStr(ServerInstance->Config->Limits.MaxKick)+
113                         " MAXGECOS="+ConvToStr(ServerInstance->Config->Limits.MaxGecos)+
114                         " MAXAWAY="+ConvToStr(ServerInstance->Config->Limits.MaxAway)+
115                         " IP6NATIVE="+ConvToStr(ip6)+
116                         " IP6SUPPORT=1"+
117                         " PROTOCOL="+ConvToStr(ProtocolVersion)+extra+
118                         " PREFIX="+ServerInstance->Modes->BuildPrefixes()+
119                         " CHANMODES="+ServerInstance->Modes->GiveModeList(MASK_CHANNEL)+
120                         " USERMODES="+ServerInstance->Modes->GiveModeList(MASK_USER)+
121                         " SVSPART=1");
122
123         this->WriteLine("CAPAB END");
124 }
125
126 /* Check a comma seperated list for an item */
127 bool TreeSocket::HasItem(const std::string &list, const std::string &item)
128 {
129         irc::commasepstream seplist(list);
130         std::string item2;
131
132         while (seplist.GetToken(item2))
133         {
134                 if (item2 == item)
135                         return true;
136         }
137         return false;
138 }
139
140 /* Isolate and return the elements that are different between two comma seperated lists */
141 std::string TreeSocket::ListDifference(const std::string &one, const std::string &two)
142 {
143         irc::commasepstream list_one(one);
144         std::string item;
145         std::string result;
146         while (list_one.GetToken(item))
147         {
148                 if (!HasItem(two, item))
149                 {
150                         result.append(" ");
151                         result.append(item);
152                 }
153         }
154         return result;
155 }
156
157 bool TreeSocket::Capab(const parameterlist &params)
158 {
159         if (params.size() < 1)
160         {
161                 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
162                 return false;
163         }
164         if (params[0] == "START")
165         {
166                 ModuleList.clear();
167                 OptModuleList.clear();
168                 CapKeys.clear();
169                 if (params.size() > 1)
170                         proto_version = atoi(params[1].c_str());
171                 SendCapabilities(2);
172         }
173         else if (params[0] == "END")
174         {
175                 std::string reason;
176                 /* Compare ModuleList and check CapKeys */
177                 if ((this->ModuleList != this->MyModules(VF_COMMON)) && (this->ModuleList.length()))
178                 {
179                         std::string diffIneed = ListDifference(this->ModuleList, this->MyModules(VF_COMMON));
180                         std::string diffUneed = ListDifference(this->MyModules(VF_COMMON), this->ModuleList);
181                         if (diffIneed.length() == 0 && diffUneed.length() == 0)
182                         {
183                                 reason = "Module list in CAPAB is not alphabetically ordered, cannot compare lists.";
184                         }
185                         else
186                         {
187                                 reason = "Modules incorrectly matched on these servers.";
188                                 if (diffIneed.length())
189                                         reason += " Not loaded here:" + diffIneed;
190                                 if (diffUneed.length())
191                                         reason += " Not loaded there:" + diffUneed;
192                         }
193                         this->SendError("CAPAB negotiation failed: "+reason);
194                         return false;
195                 }
196                 if (this->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->OptModuleList.length())
197                 {
198                         std::string diffIneed = ListDifference(this->OptModuleList, this->MyModules(VF_OPTCOMMON));
199                         std::string diffUneed = ListDifference(this->MyModules(VF_OPTCOMMON), this->OptModuleList);
200                         if (diffIneed.length() == 0 && diffUneed.length() == 0)
201                         {
202                                 reason = "Optional Module list in CAPAB is not alphabetically ordered, cannot compare lists.";
203                         }
204                         else if (Utils->AllowOptCommon)
205                         {
206                                 ServerInstance->SNO->WriteToSnoMask('l',
207                                         "Optional module lists do not match, some commands may not work globally.%s%s%s%s",
208                                         diffIneed.length() ? " Not loaded here:" : "", diffIneed.c_str(),
209                                         diffUneed.length() ? " Not loaded there:" : "", diffUneed.c_str());
210                         }
211                         else
212                         {
213                                 reason = "Optional modules incorrectly matched on these servers, and options::allowmismatch not set.";
214                                 if (diffIneed.length())
215                                         reason += " Not loaded here:" + diffIneed;
216                                 if (diffUneed.length())
217                                         reason += " Not loaded there:" + diffUneed;
218                                 this->SendError("CAPAB negotiation failed: "+reason);
219                                 return false;
220                         }
221                 }
222
223                 if (this->CapKeys.find("PROTOCOL") == this->CapKeys.end())
224                 {
225                         reason = "Protocol version not specified";
226                 }
227                 else
228                 {
229                         proto_version = atoi(CapKeys.find("PROTOCOL")->second.c_str());
230                         if (proto_version < MinCompatProtocol)
231                         {
232                                 reason = "Server is using protocol version " + ConvToStr(proto_version) +
233                                         " which is too old to link with this server (version " + ConvToStr(ProtocolVersion)
234                                         + (ProtocolVersion != MinCompatProtocol ? ", links with " + ConvToStr(MinCompatProtocol) + " and above)" : ")");
235                         }
236                 }
237
238                 if(this->CapKeys.find("PREFIX") != this->CapKeys.end() && this->CapKeys.find("PREFIX")->second != ServerInstance->Modes->BuildPrefixes())
239                         reason = "One or more of the prefixes on the remote server are invalid on this server.";
240
241                 if(this->CapKeys.find("CHANMODES") != this->CapKeys.end() && this->CapKeys.find("CHANMODES")->second != ServerInstance->Modes->GiveModeList(MASK_CHANNEL))
242                         reason = "One or more of the channel modes on the remote server are invalid on this server.";
243
244                 if(this->CapKeys.find("USERMODES") != this->CapKeys.end() && this->CapKeys.find("USERMODES")->second != ServerInstance->Modes->GiveModeList(MASK_USER))
245                         reason = "One or more of the user modes on the remote server are invalid on this server.";
246
247
248                 /* Challenge response, store their challenge for our password */
249                 std::map<std::string,std::string>::iterator n = this->CapKeys.find("CHALLENGE");
250                 if (Utils->ChallengeResponse && (n != this->CapKeys.end()) && (ServerInstance->Modules->Find("m_sha256.so")))
251                 {
252                         /* Challenge-response is on now */
253                         this->SetTheirChallenge(n->second);
254                         if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
255                         {
256                                 this->SendCapabilities(2);
257                                 this->WriteLine(std::string("SERVER ")+ServerInstance->Config->ServerName+" "+this->MakePass(OutboundPass, this->GetTheirChallenge())+" 0 "+
258                                                 ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
259                         }
260                 }
261                 else
262                 {
263                         /* They didnt specify a challenge or we don't have m_sha256.so, we use plaintext */
264                         if (this->LinkState == CONNECTING)
265                         {
266                                 this->SendCapabilities(2);
267                                 this->WriteLine(std::string("SERVER ")+ServerInstance->Config->ServerName+" "+OutboundPass+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
268                         }
269                 }
270
271                 if (reason.length())
272                 {
273                         this->SendError("CAPAB negotiation failed: "+reason);
274                         return false;
275                 }
276         }
277         else if ((params[0] == "MODULES") && (params.size() == 2))
278         {
279                 if (!this->ModuleList.length())
280                 {
281                         this->ModuleList.append(params[1]);
282                 }
283                 else
284                 {
285                         this->ModuleList.append(",");
286                         this->ModuleList.append(params[1]);
287                 }
288         }
289         else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
290         {
291                 if (!this->OptModuleList.length())
292                 {
293                         this->OptModuleList.append(params[1]);
294                 }
295                 else
296                 {
297                         this->OptModuleList.append(",");
298                         this->OptModuleList.append(params[1]);
299                 }
300         }
301         else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
302         {
303                 irc::tokenstream capabs(params[1]);
304                 std::string item;
305                 bool more = true;
306                 while ((more = capabs.GetToken(item)))
307                 {
308                         /* Process each key/value pair */
309                         std::string::size_type equals = item.rfind('=');
310                         if (equals != std::string::npos)
311                         {
312                                 std::string var = item.substr(0, equals);
313                                 std::string value = item.substr(equals+1, item.length());
314                                 CapKeys[var] = value;
315                         }
316                 }
317         }
318         return true;
319 }
320