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