]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/capab.cpp
Send HALFOP= line in CAPAB CAPABILITIES for 1201 compat (anope relies on this)
[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 "link.h"
21 #include "main.h"
22
23 std::string TreeSocket::MyModules(int filter)
24 {
25         std::vector<std::string> modlist = ServerInstance->Modules->GetAllModuleNames(filter);
26
27         if (filter == VF_COMMON && proto_version != ProtocolVersion)
28                 CompatAddModules(modlist);
29
30         std::string capabilities;
31         sort(modlist.begin(),modlist.end());
32         for (unsigned int i = 0; i < modlist.size(); i++)
33         {
34                 if (i)
35                         capabilities.push_back(proto_version > 1201 ? ' ' : ',');
36                 capabilities.append(modlist[i]);
37                 Module* m = ServerInstance->Modules->Find(modlist[i]);
38                 if (m && proto_version > 1201)
39                 {
40                         Version v = m->GetVersion();
41                         if (!v.link_data.empty())
42                         {
43                                 capabilities.push_back('=');
44                                 capabilities.append(v.link_data);
45                         }
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, 0, modes.size() - 1);
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         char sep = proto_version > 1201 ? ' ' : ',';
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         if (proto_version < 1202)
135                 extra += ServerInstance->Modes->FindMode('h', MODETYPE_CHANNEL) ? " HALFOP=1" : " HALFOP=0";
136
137         this->WriteLine("CAPAB CAPABILITIES " /* Preprocessor does this one. */
138                         ":NICKMAX="+ConvToStr(ServerInstance->Config->Limits.NickMax)+
139                         " CHANMAX="+ConvToStr(ServerInstance->Config->Limits.ChanMax)+
140                         " MAXMODES="+ConvToStr(ServerInstance->Config->Limits.MaxModes)+
141                         " IDENTMAX="+ConvToStr(ServerInstance->Config->Limits.IdentMax)+
142                         " MAXQUIT="+ConvToStr(ServerInstance->Config->Limits.MaxQuit)+
143                         " MAXTOPIC="+ConvToStr(ServerInstance->Config->Limits.MaxTopic)+
144                         " MAXKICK="+ConvToStr(ServerInstance->Config->Limits.MaxKick)+
145                         " MAXGECOS="+ConvToStr(ServerInstance->Config->Limits.MaxGecos)+
146                         " MAXAWAY="+ConvToStr(ServerInstance->Config->Limits.MaxAway)+
147                         " IP6SUPPORT=1"+
148                         " PROTOCOL="+ConvToStr(ProtocolVersion)+extra+
149                         " PREFIX="+ServerInstance->Modes->BuildPrefixes()+
150                         " CHANMODES="+ServerInstance->Modes->GiveModeList(MASK_CHANNEL)+
151                         " USERMODES="+ServerInstance->Modes->GiveModeList(MASK_USER)+
152                         " SVSPART=1");
153
154         this->WriteLine("CAPAB END");
155 }
156
157 /* Isolate and return the elements that are different between two comma seperated lists */
158 void TreeSocket::ListDifference(const std::string &one, const std::string &two, char sep,
159                 std::string& mleft, std::string& mright)
160 {
161         std::set<std::string> values;
162         irc::sepstream sepleft(one, sep);
163         irc::sepstream sepright(two, sep);
164         std::string item;
165         while (sepleft.GetToken(item))
166         {
167                 values.insert(item);
168         }
169         while (sepright.GetToken(item))
170         {
171                 if (!values.erase(item))
172                 {
173                         mright.push_back(sep);
174                         mright.append(item);
175                 }
176         }
177         for(std::set<std::string>::iterator i = values.begin(); i != values.end(); ++i)
178         {
179                 mleft.push_back(sep);
180                 mleft.append(*i);
181         }
182 }
183
184 bool TreeSocket::Capab(const parameterlist &params)
185 {
186         if (params.size() < 1)
187         {
188                 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
189                 return false;
190         }
191         if (params[0] == "START")
192         {
193                 capab->ModuleList.clear();
194                 capab->OptModuleList.clear();
195                 capab->CapKeys.clear();
196                 if (params.size() > 1)
197                         proto_version = atoi(params[1].c_str());
198                 SendCapabilities(2);
199         }
200         else if (params[0] == "END")
201         {
202                 std::string reason;
203                 /* Compare ModuleList and check CapKeys */
204                 if ((this->capab->ModuleList != this->MyModules(VF_COMMON)) && (this->capab->ModuleList.length()))
205                 {
206                         std::string diffIneed, diffUneed;
207                         ListDifference(this->capab->ModuleList, this->MyModules(VF_COMMON), proto_version > 1201 ? ' ' : ',', diffIneed, diffUneed);
208                         if (diffIneed.length() || diffUneed.length())
209                         {
210                                 reason = "Modules incorrectly matched on these servers.";
211                                 if (diffIneed.length())
212                                         reason += " Not loaded here:" + diffIneed;
213                                 if (diffUneed.length())
214                                         reason += " Not loaded there:" + diffUneed;
215                                 this->SendError("CAPAB negotiation failed: "+reason);
216                                 return false;
217                         }
218                 }
219                 if (this->capab->OptModuleList != this->MyModules(VF_OPTCOMMON) && this->capab->OptModuleList.length())
220                 {
221                         std::string diffIneed, diffUneed;
222                         ListDifference(this->capab->OptModuleList, this->MyModules(VF_OPTCOMMON), ' ', diffIneed, diffUneed);
223                         if (diffIneed.length() || diffUneed.length())
224                         {
225                                 if (Utils->AllowOptCommon)
226                                 {
227                                         ServerInstance->SNO->WriteToSnoMask('l',
228                                                 "Optional module lists do not match, some commands may not work globally.%s%s%s%s",
229                                                 diffIneed.length() ? " Not loaded here:" : "", diffIneed.c_str(),
230                                                 diffUneed.length() ? " Not loaded there:" : "", diffUneed.c_str());
231                                 }
232                                 else
233                                 {
234                                         reason = "Optional modules incorrectly matched on these servers, and options::allowmismatch not set.";
235                                         if (diffIneed.length())
236                                                 reason += " Not loaded here:" + diffIneed;
237                                         if (diffUneed.length())
238                                                 reason += " Not loaded there:" + diffUneed;
239                                         this->SendError("CAPAB negotiation failed: "+reason);
240                                         return false;
241                                 }
242                         }
243                 }
244
245                 if (this->capab->CapKeys.find("PROTOCOL") == this->capab->CapKeys.end())
246                 {
247                         reason = "Protocol version not specified";
248                 }
249                 else
250                 {
251                         proto_version = atoi(capab->CapKeys.find("PROTOCOL")->second.c_str());
252                         if (proto_version < MinCompatProtocol)
253                         {
254                                 reason = "Server is using protocol version " + ConvToStr(proto_version) +
255                                         " which is too old to link with this server (version " + ConvToStr(ProtocolVersion)
256                                         + (ProtocolVersion != MinCompatProtocol ? ", links with " + ConvToStr(MinCompatProtocol) + " and above)" : ")");
257                         }
258                 }
259
260                 if(this->capab->CapKeys.find("PREFIX") != this->capab->CapKeys.end() && this->capab->CapKeys.find("PREFIX")->second != ServerInstance->Modes->BuildPrefixes())
261                         reason = "One or more of the prefixes on the remote server are invalid on this server.";
262
263                 if (!capab->ChanModes.empty())
264                 {
265                         if (capab->ChanModes != BuildModeList(MODETYPE_CHANNEL))
266                         {
267                                 std::string diffIneed, diffUneed;
268                                 ListDifference(capab->ChanModes, BuildModeList(MODETYPE_CHANNEL), ' ', diffIneed, diffUneed);
269                                 if (diffIneed.length() || diffUneed.length())
270                                 {
271                                         reason = "Channel modes not matched on these servers.";
272                                         if (diffIneed.length())
273                                                 reason += " Not loaded here:" + diffIneed;
274                                         if (diffUneed.length())
275                                                 reason += " Not loaded there:" + diffUneed;
276                                 }
277                         }
278                 }
279                 else if (this->capab->CapKeys.find("CHANMODES") != this->capab->CapKeys.end())
280                 {
281                         if (this->capab->CapKeys.find("CHANMODES")->second != ServerInstance->Modes->GiveModeList(MASK_CHANNEL))
282                                 reason = "One or more of the channel modes on the remote server are invalid on this server.";
283                 }
284
285                 if (!capab->UserModes.empty())
286                 {
287                         if (capab->UserModes != BuildModeList(MODETYPE_USER))
288                         {
289                                 std::string diffIneed, diffUneed;
290                                 ListDifference(capab->UserModes, BuildModeList(MODETYPE_USER), ' ', diffIneed, diffUneed);
291                                 if (diffIneed.length() || diffUneed.length())
292                                 {
293                                         reason = "User modes not matched on these servers.";
294                                         if (diffIneed.length())
295                                                 reason += " Not loaded here:" + diffIneed;
296                                         if (diffUneed.length())
297                                                 reason += " Not loaded there:" + diffUneed;
298                                 }
299                         }
300                 }
301                 else if (this->capab->CapKeys.find("USERMODES") != this->capab->CapKeys.end())
302                 {
303                         if (this->capab->CapKeys.find("USERMODES")->second != ServerInstance->Modes->GiveModeList(MASK_USER))
304                                 reason = "One or more of the user modes on the remote server are invalid on this server.";
305                 }
306
307                 /* Challenge response, store their challenge for our password */
308                 std::map<std::string,std::string>::iterator n = this->capab->CapKeys.find("CHALLENGE");
309                 if (Utils->ChallengeResponse && (n != this->capab->CapKeys.end()) && (ServerInstance->Modules->Find("m_sha256.so")))
310                 {
311                         /* Challenge-response is on now */
312                         this->SetTheirChallenge(n->second);
313                         if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
314                         {
315                                 this->SendCapabilities(2);
316                                 this->WriteLine(std::string("SERVER ")+ServerInstance->Config->ServerName+" "+this->MakePass(capab->link->SendPass, capab->theirchallenge)+" 0 "+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->link->SendPass+" 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 (!capab->ModuleList.length())
338                 {
339                         capab->ModuleList = params[1];
340                 }
341                 else
342                 {
343                         capab->ModuleList.push_back(proto_version > 1201 ? ' ' : ',');
344                         capab->ModuleList.append(params[1]);
345                 }
346         }
347         else if ((params[0] == "MODSUPPORT") && (params.size() == 2))
348         {
349                 if (!capab->OptModuleList.length())
350                 {
351                         capab->OptModuleList = params[1];
352                 }
353                 else
354                 {
355                         capab->OptModuleList.push_back(' ');
356                         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