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