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