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