]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/capab.cpp
fixed some indentation and spacing in 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-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 " /* Preprocessor does this one. */
84                         ":NICKMAX="+ConvToStr(Instance->Config->Limits.NickMax)+
85                         " HALFOP="+ConvToStr(Instance->Config->AllowHalfop)+
86                         " CHANMAX="+ConvToStr(Instance->Config->Limits.ChanMax)+
87                         " MAXMODES="+ConvToStr(Instance->Config->Limits.MaxModes)+
88                         " IDENTMAX="+ConvToStr(Instance->Config->Limits.IdentMax)+
89                         " MAXQUIT="+ConvToStr(Instance->Config->Limits.MaxQuit)+
90                         " MAXTOPIC="+ConvToStr(Instance->Config->Limits.MaxTopic)+
91                         " MAXKICK="+ConvToStr(Instance->Config->Limits.MaxKick)+
92                         " MAXGECOS="+ConvToStr(Instance->Config->Limits.MaxGecos)+
93                         " MAXAWAY="+ConvToStr(Instance->Config->Limits.MaxAway)+
94                         " IP6NATIVE="+ConvToStr(ip6)+
95                         " IP6SUPPORT="+ConvToStr(ip6support)+
96                         " PROTOCOL="+ConvToStr(ProtocolVersion)+extra+
97                         " PREFIX="+Instance->Modes->BuildPrefixes()+
98                         " CHANMODES="+Instance->Modes->ChanModes()+
99                         " SVSPART=1");
100
101         this->WriteLine("CAPAB END");
102 }
103
104 /* Check a comma seperated list for an item */
105 bool TreeSocket::HasItem(const std::string &list, const std::string &item)
106 {
107         irc::commasepstream seplist(list);
108         std::string item2;
109
110         while (seplist.GetToken(item2))
111         {
112                 if (item2 == item)
113                         return true;
114         }
115         return false;
116 }
117
118 /* Isolate and return the elements that are different between two comma seperated lists */
119 std::string TreeSocket::ListDifference(const std::string &one, const std::string &two)
120 {
121         irc::commasepstream list_one(one);
122         std::string item;
123         std::string result;
124         while (list_one.GetToken(item))
125         {
126                 if (!HasItem(two, item))
127                 {
128                         result.append(" ");
129                         result.append(item);
130                 }
131         }
132         return result;
133 }
134
135 bool TreeSocket::Capab(const std::deque<std::string> &params)
136 {
137         if (params.size() < 1)
138         {
139                 this->SendError("Invalid number of parameters for CAPAB - Mismatched version");
140                 return false;
141         }
142         if (params[0] == "START")
143         {
144                 this->ModuleList.clear();
145                 this->CapKeys.clear();
146         }
147         else if (params[0] == "END")
148         {
149                 std::string reason;
150                 int ip6support = 0;
151 #ifdef SUPPORT_IP6LINKS
152                 ip6support = 1;
153 #endif
154                 /* Compare ModuleList and check CapKeys...
155                  * Maybe this could be tidier? -- Brain
156                  */
157                 if ((this->ModuleList != this->MyCapabilities()) && (this->ModuleList.length()))
158                 {
159                         std::string diff = ListDifference(this->ModuleList, this->MyCapabilities());
160                         if (!diff.length())
161                         {
162                                 diff = "your server:" + ListDifference(this->MyCapabilities(), this->ModuleList);
163                         }
164                         else
165                         {
166                                 diff = "this server:" + diff;
167                         }
168                         if (diff.length() == 12)
169                                 reason = "Module list in CAPAB is not alphabetically ordered, cannot compare lists.";
170                         else
171                                 reason = "Modules loaded on these servers are not correctly matched, these modules are not loaded on " + diff;
172                 }
173
174                 if (((this->CapKeys.find("IP6SUPPORT") == this->CapKeys.end()) && (ip6support)) || ((this->CapKeys.find("IP6SUPPORT") != this->CapKeys.end()) && (this->CapKeys.find("IP6SUPPORT")->second != ConvToStr(ip6support))))
175                         reason = "We don't both support linking to IPV6 servers";
176                 if (((this->CapKeys.find("IP6NATIVE") != this->CapKeys.end()) && (this->CapKeys.find("IP6NATIVE")->second == "1")) && (!ip6support))
177                         reason = "The remote server is IPV6 native, and we don't support linking to IPV6 servers";
178                 if (((this->CapKeys.find("PROTOCOL") == this->CapKeys.end()) || ((this->CapKeys.find("PROTOCOL") != this->CapKeys.end()) && (this->CapKeys.find("PROTOCOL")->second != ConvToStr(ProtocolVersion)))))
179                 {
180                         if (this->CapKeys.find("PROTOCOL") != this->CapKeys.end())
181                                 reason = "Mismatched protocol versions "+this->CapKeys.find("PROTOCOL")->second+" and "+ConvToStr(ProtocolVersion);
182                         else
183                                 reason = "Protocol version not specified";
184                 }
185
186                 if(this->CapKeys.find("PREFIX") != this->CapKeys.end() && this->CapKeys.find("PREFIX")->second != this->Instance->Modes->BuildPrefixes())
187                         reason = "One or more of the prefixes on the remote server are invalid on this server.";
188
189                 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))))
190                         reason = "We don't both have halfop support enabled/disabled identically";
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()) && (Instance->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->Instance->Config->ServerName+" "+this->MakePass(OutboundPass, this->GetTheirChallenge())+" 0 "+
202                                                 Instance->Config->GetSID()+" :"+this->Instance->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->Instance->Config->ServerName+" "+OutboundPass+" 0 "+Instance->Config->GetSID()+" :"+this->Instance->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