]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/utils.cpp
Honking huge commit. Removal of DELETE() template that never worked right anyway
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / utils.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 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 "commands/cmd_whois.h"
16 #include "commands/cmd_stats.h"
17 #include "socket.h"
18 #include "wildcard.h"
19 #include "xline.h"
20 #include "transport.h"
21 #include "socketengine.h"
22
23 #include "m_spanningtree/main.h"
24 #include "m_spanningtree/utils.h"
25 #include "m_spanningtree/treeserver.h"
26 #include "m_spanningtree/link.h"
27 #include "m_spanningtree/treesocket.h"
28 #include "m_spanningtree/resolvers.h"
29
30 /* $ModDep: m_spanningtree/timesynctimer.h m_spanningtree/resolvers.h m_spanningtree/main.h m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/link.h m_spanningtree/treesocket.h */
31
32 bool SpanningTreeUtilities::IsSID(const std::string &str)
33 {
34         /* Returns true if the string given is exactly 3 characters long,
35          * starts with a digit, and the other two characters are A-Z or digits
36          */
37         return ((str.length() == 3) && isdigit(str[0]) &&
38                         ((str[1] >= 'A' && str[1] <= 'Z') || isdigit(str[1])) &&
39                          ((str[2] >= 'A' && str[2] <= 'Z') || isdigit(str[2])));
40 }
41
42 /** Yay for fast searches!
43  * This is hundreds of times faster than recursion
44  * or even scanning a linked list, especially when
45  * there are more than a few servers to deal with.
46  * (read as: lots).
47  */
48 TreeServer* SpanningTreeUtilities::FindServer(const std::string &ServerName)
49 {
50         if (IsSID(ServerName))
51                 return this->FindServerID(ServerName);
52
53         server_hash::iterator iter = serverlist.find(ServerName.c_str());
54         if (iter != serverlist.end())
55         {
56                 return iter->second;
57         }
58         else
59         {
60                 return NULL;
61         }
62 }
63
64 TreeServer* SpanningTreeUtilities::FindRemoteBurstServer(TreeServer* Server)
65 {
66         server_hash::iterator iter = RemoteServersBursting.find(Server->GetName().c_str());
67         if (iter != RemoteServersBursting.end())
68                 return iter->second;
69         else
70                 return NULL;
71 }
72
73 TreeSocket* SpanningTreeUtilities::FindBurstingServer(const std::string &ServerName)
74 {
75         std::map<irc::string,TreeSocket*>::iterator iter;
76         iter = burstingserverlist.find(ServerName.c_str());
77         if (iter != burstingserverlist.end())
78         {
79                 return iter->second;
80         }
81         else
82         {
83                 return NULL;
84         }
85 }
86
87 void SpanningTreeUtilities::SetRemoteBursting(TreeServer* Server, bool bursting)
88 {
89         server_hash::iterator iter = RemoteServersBursting.find(Server->GetName().c_str());
90         if (bursting)
91         {
92                 if (iter == RemoteServersBursting.end())
93                         RemoteServersBursting.insert(make_pair(Server->GetName(), Server));
94                 else return;
95         }
96         else
97         {
98                 if (iter != RemoteServersBursting.end())
99                         RemoteServersBursting.erase(iter);
100                 else return;
101         }
102         ServerInstance->Log(DEBUG,"Server %s is %sbursting nicknames", Server->GetName().c_str(), bursting ? "" : "no longer ");
103 }
104
105 void SpanningTreeUtilities::AddBurstingServer(const std::string &ServerName, TreeSocket* s)
106 {
107         std::map<irc::string,TreeSocket*>::iterator iter = burstingserverlist.find(ServerName.c_str());
108         if (iter == burstingserverlist.end())
109                 burstingserverlist[ServerName.c_str()] = s;
110 }
111
112 void SpanningTreeUtilities::DelBurstingServer(TreeSocket* s)
113 {
114          for (std::map<irc::string,TreeSocket*>::iterator iter = burstingserverlist.begin(); iter != burstingserverlist.end(); iter++)
115          {
116                  if (iter->second == s)
117                  {
118                          burstingserverlist.erase(iter);
119                          return;
120                  }
121          }
122 }
123
124 /** Returns the locally connected server we must route a
125  * message through to reach server 'ServerName'. This
126  * only applies to one-to-one and not one-to-many routing.
127  * See the comments for the constructor of TreeServer
128  * for more details.
129  */
130 TreeServer* SpanningTreeUtilities::BestRouteTo(const std::string &ServerName)
131 {
132         if (ServerName.c_str() == TreeRoot->GetName() || ServerName == ServerInstance->Config->GetSID())
133                 return NULL;
134         TreeServer* Found = FindServer(ServerName);
135         if (Found)
136         {
137                 return Found->GetRoute();
138         }
139         else
140         {
141                 return NULL;
142         }
143 }
144
145 /** Find the first server matching a given glob mask.
146  * Theres no find-using-glob method of hash_map [awwww :-(]
147  * so instead, we iterate over the list using an iterator
148  * and match each one until we get a hit. Yes its slow,
149  * deal with it.
150  */
151 TreeServer* SpanningTreeUtilities::FindServerMask(const std::string &ServerName)
152 {
153         for (server_hash::iterator i = serverlist.begin(); i != serverlist.end(); i++)
154         {
155                 if (match(i->first.c_str(),ServerName.c_str()))
156                         return i->second;
157         }
158         return NULL;
159 }
160
161 TreeServer* SpanningTreeUtilities::FindServerID(const std::string &id)
162 {
163         server_hash::iterator iter = sidlist.find(id);
164         if (iter != sidlist.end())
165                 return iter->second;
166         else
167                 return NULL;
168 }
169
170 /* A convenient wrapper that returns true if a server exists */
171 bool SpanningTreeUtilities::IsServer(const std::string &ServerName)
172 {
173         return (FindServer(ServerName) != NULL);
174 }
175
176 SpanningTreeUtilities::SpanningTreeUtilities(InspIRCd* Instance, ModuleSpanningTree* C) : ServerInstance(Instance), Creator(C)
177 {
178         Bindings.clear();
179
180         this->TreeRoot = new TreeServer(this, ServerInstance, ServerInstance->Config->ServerName, ServerInstance->Config->ServerDesc, ServerInstance->Config->GetSID());
181
182         modulelist* ml = ServerInstance->Modules->FindInterface("BufferedSocketHook");
183
184         /* Did we find any modules? */
185         if (ml)
186         {
187                 /* Yes, enumerate them all to find out the hook name */
188                 for (modulelist::iterator m = ml->begin(); m != ml->end(); m++)
189                 {
190                         /* Make a request to it for its name, its implementing
191                          * BufferedSocketHook so we know its safe to do this
192                          */
193                         std::string name = BufferedSocketNameRequest((Module*)Creator, *m).Send();
194                         /* Build a map of them */
195                         hooks[name.c_str()] = *m;
196                         hooknames.push_back(name);
197                 }
198         }
199
200         this->ReadConfiguration(true);
201 }
202
203 SpanningTreeUtilities::~SpanningTreeUtilities()
204 {
205         for (unsigned int i = 0; i < Bindings.size(); i++)
206         {
207                 ServerInstance->SE->DelFd(Bindings[i]);
208                 Bindings[i]->Close();
209         }
210         while (TreeRoot->ChildCount())
211         {
212                 TreeServer* child_server = TreeRoot->GetChild(0);
213                 if (child_server)
214                 {
215                         TreeSocket* sock = child_server->GetSocket();
216                         ServerInstance->SE->DelFd(sock);
217                         sock->Close();
218                 }
219         }
220         delete TreeRoot;
221         ServerInstance->BufferedSocketCull();
222 }
223
224 void SpanningTreeUtilities::AddThisServer(TreeServer* server, TreeServerList &list)
225 {
226         if (list.find(server) == list.end())
227                 list[server] = server;
228 }
229
230 /* returns a list of DIRECT servernames for a specific channel */
231 void SpanningTreeUtilities::GetListOfServersForChannel(Channel* c, TreeServerList &list, char status, const CUList &exempt_list)
232 {
233         CUList *ulist;
234         switch (status)
235         {
236                 case '@':
237                         ulist = c->GetOppedUsers();
238                 break;
239                 case '%':
240                         ulist = c->GetHalfoppedUsers();
241                 break;
242                 case '+':
243                         ulist = c->GetVoicedUsers();
244                 break;
245                 default:
246                         ulist = c->GetUsers();
247                 break;
248         }
249         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
250         {
251                 if ((i->first->GetFd() < 0) && (exempt_list.find(i->first) == exempt_list.end()))
252                 {
253                         TreeServer* best = this->BestRouteTo(i->first->server);
254                         if (best)
255                                 AddThisServer(best,list);
256                 }
257         }
258         return;
259 }
260
261 bool SpanningTreeUtilities::DoOneToAllButSenderRaw(const std::string &data, const std::string &omit, const std::string &prefix, const irc::string &command, std::deque<std::string> &params)
262 {
263         char pfx = 0;
264         TreeServer* omitroute = this->BestRouteTo(omit);
265         if ((command == "NOTICE") || (command == "PRIVMSG"))
266         {
267                 if (params.size() >= 2)
268                 {
269                         /* Prefixes */
270                         if ((*(params[0].c_str()) == '@') || (*(params[0].c_str()) == '%') || (*(params[0].c_str()) == '+'))
271                         {
272                                 pfx = params[0][0];
273                                 params[0] = params[0].substr(1, params[0].length()-1);
274                         }
275                         if ((*(params[0].c_str()) != '#') && (*(params[0].c_str()) != '$'))
276                         {
277                                 // special routing for private messages/notices
278                                 User* d = ServerInstance->FindNick(params[0]);
279                                 if (d)
280                                 {
281                                         std::deque<std::string> par;
282                                         par.push_back(params[0]);
283                                         par.push_back(":"+params[1]);
284                                         this->DoOneToOne(prefix,command.c_str(),par,d->server);
285                                         return true;
286                                 }
287                         }
288                         else if (*(params[0].c_str()) == '$')
289                         {
290                                 std::deque<std::string> par;
291                                 par.push_back(params[0]);
292                                 par.push_back(":"+params[1]);
293                                 this->DoOneToAllButSender(prefix,command.c_str(),par,omitroute->GetName());
294                                 return true;
295                         }
296                         else
297                         {
298                                 Channel* c = ServerInstance->FindChan(params[0]);
299                                 User* u = ServerInstance->FindNick(prefix);
300                                 if (c && u)
301                                 {
302                                         CUList elist;
303                                         TreeServerList list;
304                                         FOREACH_MOD(I_OnBuildExemptList, OnBuildExemptList((command == "PRIVMSG" ? MSG_PRIVMSG : MSG_NOTICE), c, u, pfx, elist, params[1]));
305                                         GetListOfServersForChannel(c,list,pfx,elist);
306
307                                         for (TreeServerList::iterator i = list.begin(); i != list.end(); i++)
308                                         {
309                                                 TreeSocket* Sock = i->second->GetSocket();
310                                                 if ((Sock) && (i->second->GetName() != omit) && (omitroute != i->second))
311                                                 {
312                                                         Sock->WriteLine(data);
313                                                 }
314                                         }
315                                         return true;
316                                 }
317                         }
318                 }
319         }
320         unsigned int items =this->TreeRoot->ChildCount();
321         for (unsigned int x = 0; x < items; x++)
322         {
323                 TreeServer* Route = this->TreeRoot->GetChild(x);
324                 if ((Route) && (Route->GetSocket()) && (Route->GetName() != omit) && (omitroute != Route))
325                 {
326                         TreeSocket* Sock = Route->GetSocket();
327                         if (Sock)
328                                 Sock->WriteLine(data);
329                 }
330         }
331         return true;
332 }
333
334 bool SpanningTreeUtilities::DoOneToAllButSender(const std::string &prefix, const std::string &command, std::deque<std::string> &params, std::string omit)
335 {
336         TreeServer* omitroute = this->BestRouteTo(omit);
337         std::string FullLine = ":" + prefix + " " + command;
338         unsigned int words = params.size();
339         for (unsigned int x = 0; x < words; x++)
340         {
341                 FullLine = FullLine + " " + params[x];
342         }
343         unsigned int items = this->TreeRoot->ChildCount();
344         for (unsigned int x = 0; x < items; x++)
345         {
346                 TreeServer* Route = this->TreeRoot->GetChild(x);
347                 // Send the line IF:
348                 // The route has a socket (its a direct connection)
349                 // The route isnt the one to be omitted
350                 // The route isnt the path to the one to be omitted
351                 if ((Route) && (Route->GetSocket()) && (Route->GetName() != omit) && (omitroute != Route))
352                 {
353                         TreeSocket* Sock = Route->GetSocket();
354                         if (Sock)
355                                 Sock->WriteLine(FullLine);
356                 }
357         }
358         return true;
359 }
360
361 bool SpanningTreeUtilities::DoOneToMany(const std::string &prefix, const std::string &command, std::deque<std::string> &params)
362 {
363         std::string FullLine = ":" + prefix + " " + command;
364         unsigned int words = params.size();
365         for (unsigned int x = 0; x < words; x++)
366         {
367                 FullLine = FullLine + " " + params[x];
368         }
369         unsigned int items = this->TreeRoot->ChildCount();
370         for (unsigned int x = 0; x < items; x++)
371         {
372                 TreeServer* Route = this->TreeRoot->GetChild(x);
373                 if (Route && Route->GetSocket())
374                 {
375                         TreeSocket* Sock = Route->GetSocket();
376                         if (Sock)
377                                 Sock->WriteLine(FullLine);
378                 }
379         }
380         return true;
381 }
382
383 bool SpanningTreeUtilities::DoOneToMany(const char* prefix, const char* command, std::deque<std::string> &params)
384 {
385         std::string spfx = prefix;
386         std::string scmd = command;
387         return this->DoOneToMany(spfx, scmd, params);
388 }
389
390 bool SpanningTreeUtilities::DoOneToAllButSender(const char* prefix, const char* command, std::deque<std::string> &params, std::string omit)
391 {
392         std::string spfx = prefix;
393         std::string scmd = command;
394         return this->DoOneToAllButSender(spfx, scmd, params, omit);
395 }
396
397 bool SpanningTreeUtilities::DoOneToOne(const std::string &prefix, const std::string &command, std::deque<std::string> &params, std::string target)
398 {
399         TreeServer* Route = this->BestRouteTo(target);
400         if (Route)
401         {
402                 std::string FullLine = ":" + prefix + " " + command;
403                 unsigned int words = params.size();
404                 for (unsigned int x = 0; x < words; x++)
405                 {
406                         FullLine = FullLine + " " + params[x];
407                 }
408                 if (Route && Route->GetSocket())
409                 {
410                         TreeSocket* Sock = Route->GetSocket();
411                         if (Sock)
412                                 Sock->WriteLine(FullLine);
413                 }
414                 return true;
415         }
416         else
417         {
418                 return false;
419         }
420 }
421
422 void SpanningTreeUtilities::RefreshIPCache()
423 {
424         ValidIPs.clear();
425         for (std::vector<Link>::iterator L = LinkBlocks.begin(); L != LinkBlocks.end(); L++)
426         {
427                 if ((!L->IPAddr.empty()) && (!L->RecvPass.empty()) && (!L->SendPass.empty()) && (!L->Name.empty()) && (L->Port))
428                 {
429                         ValidIPs.push_back(L->IPAddr);
430
431                         if (L->AllowMask.length())
432                                 ValidIPs.push_back(L->AllowMask);
433
434                         /* Needs resolving */
435                         bool ipvalid = true;
436                         QueryType start_type = DNS_QUERY_A;
437 #ifdef IPV6
438                         start_type = DNS_QUERY_AAAA;
439                         if (strchr(L->IPAddr.c_str(),':'))
440                         {
441                                 in6_addr n;
442                                 if (inet_pton(AF_INET6, L->IPAddr.c_str(), &n) < 1)
443                                         ipvalid = false;
444                         }
445                         else
446 #endif
447                         {
448                                 in_addr n;
449                                 if (inet_aton(L->IPAddr.c_str(),&n) < 1)
450                                         ipvalid = false;
451                         }
452                         if (!ipvalid)
453                         {
454                                 try
455                                 {
456                                         bool cached;
457                                         SecurityIPResolver* sr = new SecurityIPResolver((Module*)this->Creator, this, ServerInstance, L->IPAddr, *L, cached, start_type);
458                                         ServerInstance->AddResolver(sr, cached);
459                                 }
460                                 catch (...)
461                                 {
462                                 }
463                         }
464                 }
465         }
466 }
467
468 void SpanningTreeUtilities::ReadConfiguration(bool rebind)
469 {
470         ConfigReader* Conf = new ConfigReader(ServerInstance);
471         if (rebind)
472         {
473                 for (int j = 0; j < Conf->Enumerate("bind"); j++)
474                 {
475                         std::string Type = Conf->ReadValue("bind","type",j);
476                         std::string IP = Conf->ReadValue("bind","address",j);
477                         std::string Port = Conf->ReadValue("bind","port",j);
478                         std::string transport = Conf->ReadValue("bind","transport",j);
479                         if (Type == "servers")
480                         {
481                                 irc::portparser portrange(Port, false);
482                                 int portno = -1;
483                                 while ((portno = portrange.GetToken()))
484                                 {
485                                         if (IP == "*")
486                                                 IP.clear();
487
488                                         if ((!transport.empty()) && (hooks.find(transport.c_str()) ==  hooks.end()))
489                                         {
490                                                 throw CoreException("Can't find transport type '"+transport+"' for port "+IP+":"+Port+" - maybe you forgot to load it BEFORE m_spanningtree in your config file?");
491                                                 break;
492                                         }
493
494                                         TreeSocket* listener = new TreeSocket(this, ServerInstance, IP.c_str(), portno, true, 10, transport.empty() ? NULL : hooks[transport.c_str()]);
495                                         if (listener->GetState() == I_LISTENING)
496                                         {
497                                                 ServerInstance->Log(DEFAULT,"m_spanningtree: Binding server port %s:%d successful!", IP.c_str(), portno);
498                                                 Bindings.push_back(listener);
499                                         }
500                                         else
501                                         {
502                                                 ServerInstance->Log(DEFAULT,"m_spanningtree: Warning: Failed to bind server port: %s:%d: %s",IP.c_str(), portno, strerror(errno));
503                                                 listener->Close();
504                                         }
505                                 }
506                         }
507                 }
508         }
509         FlatLinks = Conf->ReadFlag("options","flatlinks",0);
510         HideULines = Conf->ReadFlag("options","hideulines",0);
511         AnnounceTSChange = Conf->ReadFlag("options","announcets",0);
512         EnableTimeSync = Conf->ReadFlag("timesync","enable",0);
513         MasterTime = Conf->ReadFlag("timesync", "master", 0);
514         ChallengeResponse = !Conf->ReadFlag("options", "disablehmac", 0);
515         quiet_bursts = Conf->ReadFlag("options", "quietbursts", 0);
516         PingWarnTime = Conf->ReadInteger("options", "pingwarning", 0, true);
517         PingFreq = Conf->ReadInteger("options", "serverpingfreq", 0, true);
518
519         if (PingFreq == 0)
520                 PingFreq = 60;
521
522         if (PingWarnTime < 0 || PingWarnTime > PingFreq - 1)
523                 PingWarnTime = 0;
524
525         LinkBlocks.clear();
526         ValidIPs.clear();
527         for (int j = 0; j < Conf->Enumerate("link"); j++)
528         {
529                 Link L;
530                 std::string Allow = Conf->ReadValue("link", "allowmask", j);
531                 L.Name = (Conf->ReadValue("link", "name", j)).c_str();
532                 L.AllowMask = Allow;
533                 L.IPAddr = Conf->ReadValue("link", "ipaddr", j);
534                 L.FailOver = Conf->ReadValue("link", "failover", j).c_str();
535                 L.Port = Conf->ReadInteger("link", "port", j, true);
536                 L.SendPass = Conf->ReadValue("link", "sendpass", j);
537                 L.RecvPass = Conf->ReadValue("link", "recvpass", j);
538                 L.AutoConnect = Conf->ReadInteger("link", "autoconnect", j, true);
539                 L.HiddenFromStats = Conf->ReadFlag("link", "statshidden", j);
540                 L.Timeout = Conf->ReadInteger("link", "timeout", j, true);
541                 L.Hook = Conf->ReadValue("link", "transport", j);
542                 L.Bind = Conf->ReadValue("link", "bind", j);
543                 L.Hidden = Conf->ReadFlag("link", "hidden", j);
544
545                 if ((!L.Hook.empty()) && (hooks.find(L.Hook.c_str()) ==  hooks.end()))
546                 {
547                         throw CoreException("Can't find transport type '"+L.Hook+"' for link '"+assign(L.Name)+"' - maybe you forgot to load it BEFORE m_spanningtree in your config file? Skipping <link> tag completely.");
548                         continue;
549
550                 }
551
552                 L.NextConnectTime = time(NULL) + L.AutoConnect;
553                 /* Bugfix by brain, do not allow people to enter bad configurations */
554                 if (L.Name != ServerInstance->Config->ServerName)
555                 {
556                         if ((!L.IPAddr.empty()) && (!L.RecvPass.empty()) && (!L.SendPass.empty()) && (!L.Name.empty()) && (L.Port))
557                         {
558                                 if (L.Name.find('.') == std::string::npos)
559                                         throw CoreException("The link name '"+assign(L.Name)+"' is invalid and must contain at least one '.' character");
560
561                                 if (L.Name.length() > 64)
562                                         throw CoreException("The link name '"+assign(L.Name)+"' is longer than 64 characters!");
563
564                                 ValidIPs.push_back(L.IPAddr);
565
566                                 if (Allow.length())
567                                         ValidIPs.push_back(Allow);
568
569                                 /* Needs resolving */
570                                 bool ipvalid = true;
571                                 QueryType start_type = DNS_QUERY_A;
572 #ifdef IPV6
573                                 start_type = DNS_QUERY_AAAA;
574                                 if (strchr(L.IPAddr.c_str(),':'))
575                                 {
576                                         in6_addr n;
577                                         if (inet_pton(AF_INET6, L.IPAddr.c_str(), &n) < 1)
578                                                 ipvalid = false;
579                                 }
580                                 else
581                                 {
582                                         in_addr n;
583                                         if (inet_aton(L.IPAddr.c_str(),&n) < 1)
584                                                 ipvalid = false;
585                                 }
586 #else
587                                 in_addr n;
588                                 if (inet_aton(L.IPAddr.c_str(),&n) < 1)
589                                         ipvalid = false;
590 #endif
591
592                                 if (!ipvalid)
593                                 {
594                                         try
595                                         {
596                                                 bool cached;
597                                                 SecurityIPResolver* sr = new SecurityIPResolver((Module*)this->Creator, this, ServerInstance, L.IPAddr, L, cached, start_type);
598                                                 ServerInstance->AddResolver(sr, cached);
599                                         }
600                                         catch (...)
601                                         {
602                                         }
603                                 }
604
605                                 LinkBlocks.push_back(L);
606                         }
607                         else
608                         {
609                                 if (L.IPAddr.empty())
610                                 {
611                                         throw CoreException("Invalid configuration for server '"+assign(L.Name)+"', IP address not defined!");
612                                 }
613                                 else if (L.RecvPass.empty())
614                                 {
615                                         throw CoreException("Invalid configuration for server '"+assign(L.Name)+"', recvpass not defined!");
616                                 }
617                                 else if (L.SendPass.empty())
618                                 {
619                                         throw CoreException("Invalid configuration for server '"+assign(L.Name)+"', sendpass not defined!");
620                                 }
621                                 else if (L.Name.empty())
622                                 {
623                                         throw CoreException("Invalid configuration, link tag without a name! IP address: "+L.IPAddr);
624                                 }
625                                 else if (!L.Port)
626                                 {
627                                         throw CoreException("Invalid configuration for server '"+assign(L.Name)+"', no port specified!");
628                                 }
629                         }
630                 }
631                 else
632                 {
633                         throw CoreException("Invalid configuration for server '"+assign(L.Name)+"', link tag has the same server name as the local server!");
634                 }
635         }
636         delete Conf;
637 }
638
639 void SpanningTreeUtilities::DoFailOver(Link* x)
640 {
641         if (x->FailOver.length())
642         {
643                 if (x->FailOver == x->Name)
644                 {
645                         Creator->RemoteMessage(NULL,"FAILOVER: Some muppet configured the failover for server \002%s\002 to point at itself. Not following it!", x->Name.c_str());
646                         return;
647                 }
648                 Link* TryThisOne = this->FindLink(x->FailOver.c_str());
649                 if (TryThisOne)
650                 {
651                         TreeServer* CheckDupe = this->FindServer(x->FailOver.c_str());
652                         if (CheckDupe)
653                         {
654                                 ServerInstance->Log(DEBUG,"Skipping existing failover: %s", x->FailOver.c_str());
655                         }
656                         else
657                         {
658                                 Creator->RemoteMessage(NULL,"FAILOVER: Trying failover link for \002%s\002: \002%s\002...", x->Name.c_str(), TryThisOne->Name.c_str());
659                                 Creator->ConnectServer(TryThisOne);
660                         }
661                 }
662                 else
663                 {
664                         Creator->RemoteMessage(NULL,"FAILOVER: Invalid failover server specified for server \002%s\002, will not follow!", x->Name.c_str());
665                 }
666         }
667 }
668
669 Link* SpanningTreeUtilities::FindLink(const std::string& name)
670 {
671         for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
672         {
673                 if (ServerInstance->MatchText(x->Name.c_str(), name.c_str()))
674                 {
675                         return &(*x);
676                 }
677         }
678         return NULL;
679 }