]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/capab.cpp
aa69e2ee6ab4956a41ad8a109ba0c1e6ec11c649
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / capab.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/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 "m_spanningtree/treesocket.h"
18 #include "m_spanningtree/treeserver.h"
19 #include "m_spanningtree/utils.h"
20 #include "m_spanningtree/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->Instance->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 && (Instance->Modules->Find("m_sha256.so")))
78         {
79                 this->SetOurChallenge(RandString(20));
80                 extra = " CHALLENGE=" + this->GetOurChallenge();
81         }
82
83         this->WriteLine("CAPAB CAPABILITIES :NICKMAX="+ConvToStr(NICKMAX)+" HALFOP="+ConvToStr(this->Instance->Config->AllowHalfop)+" CHANMAX="+ConvToStr(CHANMAX)+" MAXMODES="+ConvToStr(MAXMODES)+" IDENTMAX="+ConvToStr(IDENTMAX)+" MAXQUIT="+ConvToStr(MAXQUIT)+" MAXTOPIC="+ConvToStr(MAXTOPIC)+" MAXKICK="+ConvToStr(MAXKICK)+" MAXGECOS="+ConvToStr(MAXGECOS)+" MAXAWAY="+ConvToStr(MAXAWAY)+" IP6NATIVE="+ConvToStr(ip6)+" IP6SUPPORT="+ConvToStr(ip6support)+" PROTOCOL="+ConvToStr(ProtocolVersion)+extra+" PREFIX="+Instance->Modes->BuildPrefixes()+" CHANMODES="+Instance->Modes->ChanModes()+" SVSPART=1");
84
85         this->WriteLine("CAPAB END");
86 }
87
88 /* Check a comma seperated list for an item */
89 bool TreeSocket::HasItem(const std::string &list, const std::string &item)
90 {
91         irc::commasepstream seplist(list);
92         std::string item2;
93
94         while (seplist.GetToken(item2))
95         {
96                 if (item2 == item)
97                         return true;
98         }
99         return false;
100 }
101
102 /* Isolate and return the elements that are different between two comma seperated lists */
103 std::string TreeSocket::ListDifference(const std::string &one, const std::string &two)
104 {
105         irc::commasepstream list_one(one);
106         std::string item;
107         std::string result;
108         while (list_one.GetToken(item))
109         {
110                 if (!HasItem(two, item))
111                 {
112                         result.append(" ");
113                         result.append(item);
114                 }
115         }
116         return result;
117 }
118
119 bool TreeSocket::Capab(const std::deque<std::string> &params)
120 {
121         if (params.size() < 1)
122         {
123                 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
124                 return false;
125         }
126         if (params[0] == "START")
127         {
128                 this->ModuleList.clear();
129                 this->CapKeys.clear();
130         }
131         else if (params[0] == "END")
132         {
133                 std::string reason;
134                 int ip6support = 0;
135 #ifdef SUPPORT_IP6LINKS
136                 ip6support = 1;
137 #endif
138                 /* Compare ModuleList and check CapKeys...
139                  * Maybe this could be tidier? -- Brain
140                  */
141                 if ((this->ModuleList != this->MyCapabilities()) && (this->ModuleList.length()))
142                 {
143                         std::string diff = ListDifference(this->ModuleList, this->MyCapabilities());
144                         if (!diff.length())
145                         {
146                                 diff = "your server:" + ListDifference(this->MyCapabilities(), this->ModuleList);
147                         }
148                         else
149                         {
150                                 diff = "this server:" + diff;
151                         }
152                         if (diff.length() == 12)
153                                 reason = "Module list in CAPAB is not alphabetically ordered, cannot compare lists.";
154                         else
155                                 reason = "Modules loaded on these servers are not correctly matched, these modules are not loaded on " + diff;
156                 }
157
158                 cap_validation valid_capab[] = { 
159                         {"Maximum nickname lengths differ or remote nickname length not specified", "NICKMAX", NICKMAX},
160                         {"Maximum ident lengths differ or remote ident length not specified", "IDENTMAX", IDENTMAX},
161                         {"Maximum channel lengths differ or remote channel length not specified", "CHANMAX", CHANMAX},
162                         {"Maximum modes per line differ or remote modes per line not specified", "MAXMODES", MAXMODES},
163                         {"Maximum quit lengths differ or remote quit length not specified", "MAXQUIT", MAXQUIT},
164                         {"Maximum topic lengths differ or remote topic length not specified", "MAXTOPIC", MAXTOPIC},
165                         {"Maximum kick lengths differ or remote kick length not specified", "MAXKICK", MAXKICK},
166                         {"Maximum GECOS (fullname) lengths differ or remote GECOS length not specified", "MAXGECOS", MAXGECOS},
167                         {"Maximum awaymessage lengths differ or remote awaymessage length not specified", "MAXAWAY", MAXAWAY},
168                         {"", "", 0}
169                 };
170
171                 if (((this->CapKeys.find("IP6SUPPORT") == this->CapKeys.end()) && (ip6support)) || ((this->CapKeys.find("IP6SUPPORT") != this->CapKeys.end()) && (this->CapKeys.find("IP6SUPPORT")->second != ConvToStr(ip6support))))
172                         reason = "We don't both support linking to IPV6 servers";
173                 if (((this->CapKeys.find("IP6NATIVE") != this->CapKeys.end()) && (this->CapKeys.find("IP6NATIVE")->second == "1")) && (!ip6support))
174                         reason = "The remote server is IPV6 native, and we don't support linking to IPV6 servers";
175                 if (((this->CapKeys.find("PROTOCOL") == this->CapKeys.end()) || ((this->CapKeys.find("PROTOCOL") != this->CapKeys.end()) && (this->CapKeys.find("PROTOCOL")->second != ConvToStr(ProtocolVersion)))))
176                 {
177                         if (this->CapKeys.find("PROTOCOL") != this->CapKeys.end())
178                                 reason = "Mismatched protocol versions "+this->CapKeys.find("PROTOCOL")->second+" and "+ConvToStr(ProtocolVersion);
179                         else
180                                 reason = "Protocol version not specified";
181                 }
182
183                 if(this->CapKeys.find("PREFIX") != this->CapKeys.end() && this->CapKeys.find("PREFIX")->second != this->Instance->Modes->BuildPrefixes())
184                         reason = "One or more of the prefixes on the remote server are invalid on this server.";
185
186                 if (((this->CapKeys.find("HALFOP") == this->CapKeys.end()) && (Instance->Config->AllowHalfop)) || ((this->CapKeys.find("HALFOP") != this->CapKeys.end()) && (this->CapKeys.find("HALFOP")->second != ConvToStr(Instance->Config->AllowHalfop))))
187                         reason = "We don't both have halfop support enabled/disabled identically";
188
189                 for (int x = 0; valid_capab[x].size; ++x)
190                 {
191                         if (((this->CapKeys.find(valid_capab[x].key) == this->CapKeys.end()) || ((this->CapKeys.find(valid_capab[x].key) != this->CapKeys.end()) &&
192                                                  (this->CapKeys.find(valid_capab[x].key)->second != ConvToStr(valid_capab[x].size)))))
193                                 reason = valid_capab[x].reason;
194                 }
195         
196                 /* Challenge response, store their challenge for our password */
197                 std::map<std::string,std::string>::iterator n = this->CapKeys.find("CHALLENGE");
198                 if (Utils->ChallengeResponse && (n != this->CapKeys.end()) && (Instance->Modules->Find("m_sha256.so")))
199                 {
200                         /* Challenge-response is on now */
201                         this->SetTheirChallenge(n->second);
202                         if (!this->GetTheirChallenge().empty() && (this->LinkState == CONNECTING))
203                         {
204                                 this->SendCapabilities();
205                                 this->WriteLine(std::string("SERVER ")+this->Instance->Config->ServerName+" "+this->MakePass(OutboundPass, this->GetTheirChallenge())+" 0 "+
206                                                 Instance->Config->GetSID()+" :"+this->Instance->Config->ServerDesc);
207                         }
208                 }
209                 else
210                 {
211                         /* They didnt specify a challenge or we don't have m_sha256.so, we use plaintext */
212                         if (this->LinkState == CONNECTING)
213                         {
214                                 this->SendCapabilities();
215                                 this->WriteLine(std::string("SERVER ")+this->Instance->Config->ServerName+" "+OutboundPass+" 0 "+Instance->Config->GetSID()+" :"+this->Instance->Config->ServerDesc);
216                         }
217                 }
218
219                 if (reason.length())
220                 {
221                         this->SendError("CAPAB negotiation failed: "+reason);
222                         return false;
223                 }
224         }
225         else if ((params[0] == "MODULES") && (params.size() == 2))
226         {
227                 if (!this->ModuleList.length())
228                 {
229                         this->ModuleList.append(params[1]);
230                 }
231                 else
232                 {
233                         this->ModuleList.append(",");
234                         this->ModuleList.append(params[1]);
235                 }
236         }
237
238         else if ((params[0] == "CAPABILITIES") && (params.size() == 2))
239         {
240                 irc::tokenstream capabs(params[1]);
241                 std::string item;
242                 bool more = true;
243                 while ((more = capabs.GetToken(item)))
244                 {
245                         /* Process each key/value pair */
246                         std::string::size_type equals = item.rfind('=');
247                         if (equals != std::string::npos)
248                         {
249                                 std::string var = item.substr(0, equals);
250                                 std::string value = item.substr(equals+1, item.length());
251                                 CapKeys[var] = value;
252                         }
253                 }
254         }
255         return true;
256 }
257