]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/capab.cpp
Sort CAPAB CHANMODES and USERMODES
[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::vector<std::string> modes;
53         for(char c='A'; c <= 'z'; c++)
54         {
55                 ModeHandler* mh = ServerInstance->Modes->FindMode(c, type)
56                 if (mh)
57                 {
58                         std::string mdesc = mh->name;
59                         mdesc.push_back('=');
60                         if (mh->GetPrefix())
61                                 mdesc.push_back(mh->GetPrefix());
62                         if (mh->GetModeChar())
63                                 mdesc.push_back(mh->GetModeChar());
64                         modes.push_back(mdesc);
65                 }
66         }
67         sort(modes.begin(), modes.end());
68         irc::stringjoiner line(" ", modes, 0, modes.size() - 1);
69         return line.GetJoined();
70 }
71
72 void TreeSocket::SendCapabilities(int phase)
73 {
74         if (capab->capab_phase >= phase)
75                 return;
76
77         if (capab->capab_phase < 1 && phase >= 1)
78                 WriteLine("CAPAB START " + ConvToStr(ProtocolVersion));
79
80         capab->capab_phase = phase;
81         if (phase < 2)
82                 return;
83
84         char sep = proto_version > 1201 ? ' ' : ',';
85         irc::sepstream modulelist(MyModules(VF_COMMON), sep);
86         irc::sepstream optmodulelist(MyModules(VF_OPTCOMMON), sep);
87         /* Send module names, split at 509 length */
88         std::string item;
89         std::string line = "CAPAB MODULES :";
90         while (modulelist.GetToken(item))
91         {
92                 if (line.length() + item.length() + 1 > 509)
93                 {
94                         this->WriteLine(line);
95                         line = "CAPAB MODULES :";
96                 }
97
98                 if (line != "CAPAB MODULES :")
99                         line.push_back(sep);
100
101                 line.append(item);
102         }
103         if (line != "CAPAB MODULES :")
104                 this->WriteLine(line);
105
106         line = "CAPAB MODSUPPORT :";
107         while (optmodulelist.GetToken(item))
108         {
109                 if (line.length() + item.length() + 1 > 509)
110                 {
111                         this->WriteLine(line);
112                         line = "CAPAB MODSUPPORT :";
113                 }
114
115                 if (line != "CAPAB MODSUPPORT :")
116                         line.push_back(sep);
117
118                 line.append(item);
119         }
120         if (line != "CAPAB MODSUPPORT :")
121                 this->WriteLine(line);
122
123         WriteLine("CAPAB CHANMODES :" + BuildModeList(MODETYPE_CHANNEL));
124         WriteLine("CAPAB USERMODES :" + BuildModeList(MODETYPE_USER));
125
126         int ip6 = 0;
127 #ifdef IPV6
128         ip6 = 1;
129 #endif
130         std::string extra;
131         /* Do we have sha256 available? If so, we send a challenge */
132         if (Utils->ChallengeResponse && (ServerInstance->Modules->Find("m_sha256.so")))
133         {
134                 SetOurChallenge(ServerInstance->GenRandomStr(20));
135                 extra = " CHALLENGE=" + this->GetOurChallenge();
136         }
137
138         this->WriteLine("CAPAB CAPABILITIES " /* Preprocessor does this one. */
139                         ":NICKMAX="+ConvToStr(ServerInstance->Config->Limits.NickMax)+
140                         " CHANMAX="+ConvToStr(ServerInstance->Config->Limits.ChanMax)+
141                         " MAXMODES="+ConvToStr(ServerInstance->Config->Limits.MaxModes)+
142                         " IDENTMAX="+ConvToStr(ServerInstance->Config->Limits.IdentMax)+
143                         " MAXQUIT="+ConvToStr(ServerInstance->Config->Limits.MaxQuit)+
144                         " MAXTOPIC="+ConvToStr(ServerInstance->Config->Limits.MaxTopic)+
145                         " MAXKICK="+ConvToStr(ServerInstance->Config->Limits.MaxKick)+
146                         " MAXGECOS="+ConvToStr(ServerInstance->Config->Limits.MaxGecos)+
147                         " MAXAWAY="+ConvToStr(ServerInstance->Config->Limits.MaxAway)+
148                         " IP6NATIVE="+ConvToStr(ip6)+
149                         " IP6SUPPORT=1"+
150                         " PROTOCOL="+ConvToStr(ProtocolVersion)+extra+
151                         " PREFIX="+ServerInstance->Modes->BuildPrefixes()+
152                         " CHANMODES="+ServerInstance->Modes->GiveModeList(MASK_CHANNEL)+
153                         " USERMODES="+ServerInstance->Modes->GiveModeList(MASK_USER)+
154                         " SVSPART=1");
155
156         this->WriteLine("CAPAB END");
157 }
158
159 /* Isolate and return the elements that are different between two comma seperated lists */
160 void TreeSocket::ListDifference(const std::string &one, const std::string &two, char sep,
161                 std::string& mleft, std::string& mright)
162 {
163         std::set<std::string> values;
164         irc::sepstream sepleft(one, sep);
165         irc::sepstream sepright(two, sep);
166         std::string item;
167         while (sepleft.GetToken(item))
168         {
169                 values.insert(item);
170         }
171         while (sepright.GetToken(item))
172         {
173                 if (!values.erase(item))
174                 {
175                         mright.push_back(sep);
176                         mright.append(item);
177                 }
178         }
179         for(std::set<std::string>::iterator i = values.begin(); i != values.end(); ++i)
180         {
181                 mleft.push_back(sep);
182                 mleft.append(*i);
183         }
184 }
185
186 bool TreeSocket::Capab(const parameterlist &params)
187 {
188         if (params.size() < 1)
189         {
190                 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
191                 return false;
192         }
193         if (params[0] == "START")
194         {
195                 capab->ModuleList.clear();
196                 capab->OptModuleList.clear();
197                 capab->CapKeys.clear();
198                 if (params.size() > 1)
199                         proto_version = atoi(params[1].c_str());
200                 SendCapabilities(2);
201         }
202         else if (params[0] == "END")
203         {
204                 std::string reason;
205                 /* Compare ModuleList and check CapKeys */
206                 if ((this->capab->ModuleList != this->MyModules(VF_COMMON)) && (this->capab->ModuleList.length()))
207                 {
208                         std::string diffIneed, diffUneed;
209                         ListDifference(this->capab->ModuleList, this->MyModules(VF_COMMON), proto_version > 1201 ? ' ' : ',', diffIneed, diffUneed);
210                         if (diffIneed.length() || diffUneed.length())
211                         {
212                                 reason = "Modules incorrectly matched on these servers.";
213                                 if (diffIneed.length())
214                                         reason += " Not loaded here:" + diffIneed;
215                                 if (diffUneed.length())
216                                         reason += " Not loaded there:" + diffUneed;
217                                 this->SendError("CAPAB negotiation failed: "+reason);
218                                 return false;
219                         }
220                 }
221                 if (this->capab->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->capab->OptModuleList.length())
222                 {
223                         std::string diffIneed, diffUneed;
224                         ListDifference(this->capab->OptModuleList, this->MyModules(VF_OPTCOMMON), ' ', diffIneed, diffUneed);
225                         if (diffIneed.length() || diffUneed.length())
226                         {
227                                 if (Utils->AllowOptCommon)
228                                 {
229                                         ServerInstance->SNO->WriteToSnoMask('l',
230                                                 "Optional module lists do not match, some commands may not work globally.%s%s%s%s",
231                                                 diffIneed.length() ? " Not loaded here:" : "", diffIneed.c_str(),
232                                                 diffUneed.length() ? " Not loaded there:" : "", diffUneed.c_str());
233                                 }
234                                 else
235                                 {
236                                         reason = "Optional modules incorrectly matched on these servers, and options::allowmismatch not set.";
237                                         if (diffIneed.length())
238                                                 reason += " Not loaded here:" + diffIneed;
239                                         if (diffUneed.length())
240                                                 reason += " Not loaded there:" + diffUneed;
241                                         this->SendError("CAPAB negotiation failed: "+reason);
242                                         return false;
243                                 }
244                         }
245                 }
246
247                 if (this->capab->CapKeys.find("PROTOCOL") == this->capab->CapKeys.end())
248                 {
249                         reason = "Protocol version not specified";
250                 }
251                 else
252                 {
253                         proto_version = atoi(capab->CapKeys.find("PROTOCOL")->second.c_str());
254                         if (proto_version < MinCompatProtocol)
255                         {
256                                 reason = "Server is using protocol version " + ConvToStr(proto_version) +
257                                         " which is too old to link with this server (version " + ConvToStr(ProtocolVersion)
258                                         + (ProtocolVersion != MinCompatProtocol ? ", links with " + ConvToStr(MinCompatProtocol) + " and above)" : ")");
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(std::string("SERVER ")+ServerInstance->Config->ServerName+" "+this->MakePass(capab->OutboundPass, this->GetTheirChallenge())+" 0 "+
319                                                 ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
320                         }
321                 }
322                 else
323                 {
324                         /* They didnt specify a challenge or we don't have m_sha256.so, we use plaintext */
325                         if (this->LinkState == CONNECTING)
326                         {
327                                 this->SendCapabilities(2);
328                                 this->WriteLine(std::string("SERVER ")+ServerInstance->Config->ServerName+" "+capab->OutboundPass+" 0 "+ServerInstance->Config->GetSID()+" :"+ServerInstance->Config->ServerDesc);
329                         }
330                 }
331
332                 if (reason.length())
333                 {
334                         this->SendError("CAPAB negotiation failed: "+reason);
335                         return false;
336                 }
337         }
338         else if ((params[0] == "MODULES") && (params.size() == 2))
339         {
340                 if (!capab->ModuleList.length())
341                 {
342                         capab->ModuleList = params[1];
343                 }
344                 else
345                 {
346                         capab->ModuleList.push_back(proto_version > 1201 ? ' ' : ',');
347                         capab->ModuleList.append(params[1]);
348                 }
349         }
350         else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
351         {
352                 if (!capab->OptModuleList.length())
353                 {
354                         capab->OptModuleList = params[1];
355                 }
356                 else
357                 {
358                         capab->OptModuleList.push_back(' ');
359                         capab->OptModuleList.append(params[1]);
360                 }
361         }
362         else if ((params[0] == "CHANMODES") && (params.size() == 2))
363         {
364                 capab->ChanModes = params[1];
365         }
366         else if ((params[0] == "USERMODES") && (params.size() == 2))
367         {
368                 capab->UserModes = params[1];
369         }
370         else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
371         {
372                 irc::tokenstream capabs(params[1]);
373                 std::string item;
374                 while (capabs.GetToken(item))
375                 {
376                         /* Process each key/value pair */
377                         std::string::size_type equals = item.find('=');
378                         if (equals != std::string::npos)
379                         {
380                                 std::string var = item.substr(0, equals);
381                                 std::string value = item.substr(equals+1, item.length());
382                                 capab->CapKeys[var] = value;
383                         }
384                 }
385         }
386         return true;
387 }
388