]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/utils.cpp
0bf8dd9940be59abad380310f196b54e107ee094
[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         lines_to_apply = 0;
181
182         this->TreeRoot = new TreeServer(this, ServerInstance, ServerInstance->Config->ServerName, ServerInstance->Config->ServerDesc, ServerInstance->Config->GetSID());
183
184         modulelist* ml = ServerInstance->Modules->FindInterface("InspSocketHook");
185
186         /* Did we find any modules? */
187         if (ml)
188         {
189                 /* Yes, enumerate them all to find out the hook name */
190                 for (modulelist::iterator m = ml->begin(); m != ml->end(); m++)
191                 {
192                         /* Make a request to it for its name, its implementing
193                          * InspSocketHook so we know its safe to do this
194                          */
195                         std::string name = InspSocketNameRequest((Module*)Creator, *m).Send();
196                         /* Build a map of them */
197                         hooks[name.c_str()] = *m;
198                         hooknames.push_back(name);
199                 }
200         }
201
202         this->ReadConfiguration(true);
203 }
204
205 SpanningTreeUtilities::~SpanningTreeUtilities()
206 {
207         for (unsigned int i = 0; i < Bindings.size(); i++)
208         {
209                 ServerInstance->SE->DelFd(Bindings[i]);
210                 Bindings[i]->Close();
211         }
212         while (TreeRoot->ChildCount())
213         {
214                 TreeServer* child_server = TreeRoot->GetChild(0);
215                 if (child_server)
216                 {
217                         TreeSocket* sock = child_server->GetSocket();
218                         ServerInstance->SE->DelFd(sock);
219                         sock->Close();
220                 }
221         }
222         delete TreeRoot;
223         ServerInstance->InspSocketCull();
224 }
225
226 void SpanningTreeUtilities::AddThisServer(TreeServer* server, TreeServerList &list)
227 {
228         if (list.find(server) == list.end())
229                 list[server] = server;
230 }
231
232 /* returns a list of DIRECT servernames for a specific channel */
233 void SpanningTreeUtilities::GetListOfServersForChannel(chanrec* c, TreeServerList &list, char status, const CUList &exempt_list)
234 {
235         CUList *ulist;
236         switch (status)
237         {
238                 case '@':
239                         ulist = c->GetOppedUsers();
240                 break;
241                 case '%':
242                         ulist = c->GetHalfoppedUsers();
243                 break;
244                 case '+':
245                         ulist = c->GetVoicedUsers();
246                 break;
247                 default:
248                         ulist = c->GetUsers();
249                 break;
250         }
251         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
252         {
253                 if ((i->first->GetFd() < 0) && (exempt_list.find(i->first) == exempt_list.end()))
254                 {
255                         TreeServer* best = this->BestRouteTo(i->first->server);
256                         if (best)
257                                 AddThisServer(best,list);
258                 }
259         }
260         return;
261 }
262
263 bool SpanningTreeUtilities::DoOneToAllButSenderRaw(const std::string &data, const std::string &omit, const std::string &prefix, const irc::string &command, std::deque<std::string> &params)
264 {
265         char pfx = 0;
266         TreeServer* omitroute = this->BestRouteTo(omit);
267         if ((command == "NOTICE") || (command == "PRIVMSG"))
268         {
269                 if (params.size() >= 2)
270                 {
271                         /* Prefixes */
272                         if ((*(params[0].c_str()) == '@') || (*(params[0].c_str()) == '%') || (*(params[0].c_str()) == '+'))
273                         {
274                                 pfx = params[0][0];
275                                 params[0] = params[0].substr(1, params[0].length()-1);
276                         }
277                         if ((*(params[0].c_str()) != '#') && (*(params[0].c_str()) != '$'))
278                         {
279                                 // special routing for private messages/notices
280                                 userrec* d = ServerInstance->FindNick(params[0]);
281                                 if (d)
282                                 {
283                                         std::deque<std::string> par;
284                                         par.push_back(params[0]);
285                                         par.push_back(":"+params[1]);
286                                         this->DoOneToOne(prefix,command.c_str(),par,d->server);
287                                         return true;
288                                 }
289                         }
290                         else if (*(params[0].c_str()) == '$')
291                         {
292                                 std::deque<std::string> par;
293                                 par.push_back(params[0]);
294                                 par.push_back(":"+params[1]);
295                                 this->DoOneToAllButSender(prefix,command.c_str(),par,omitroute->GetName());
296                                 return true;
297                         }
298                         else
299                         {
300                                 chanrec* c = ServerInstance->FindChan(params[0]);
301                                 userrec* u = ServerInstance->FindNick(prefix);
302                                 if (c && u)
303                                 {
304                                         CUList elist;
305                                         TreeServerList list;
306                                         FOREACH_MOD(I_OnBuildExemptList, OnBuildExemptList((command == "PRIVMSG" ? MSG_PRIVMSG : MSG_NOTICE), c, u, pfx, elist));
307                                         GetListOfServersForChannel(c,list,pfx,elist);
308
309                                         for (TreeServerList::iterator i = list.begin(); i != list.end(); i++)
310                                         {
311                                                 TreeSocket* Sock = i->second->GetSocket();
312                                                 if ((Sock) && (i->second->GetName() != omit) && (omitroute != i->second))
313                                                 {
314                                                         Sock->WriteLine(data);
315                                                 }
316                                         }
317                                         return true;
318                                 }
319                         }
320                 }
321         }
322         unsigned int items =this->TreeRoot->ChildCount();
323         for (unsigned int x = 0; x < items; x++)
324         {
325                 TreeServer* Route = this->TreeRoot->GetChild(x);
326                 if ((Route) && (Route->GetSocket()) && (Route->GetName() != omit) && (omitroute != Route))
327                 {
328                         TreeSocket* Sock = Route->GetSocket();
329                         if (Sock)
330                                 Sock->WriteLine(data);
331                 }
332         }
333         return true;
334 }
335
336 bool SpanningTreeUtilities::DoOneToAllButSender(const std::string &prefix, const std::string &command, std::deque<std::string> &params, std::string omit)
337 {
338         TreeServer* omitroute = this->BestRouteTo(omit);
339         std::string FullLine = ":" + prefix + " " + command;
340         unsigned int words = params.size();
341         for (unsigned int x = 0; x < words; x++)
342         {
343                 FullLine = FullLine + " " + params[x];
344         }
345         unsigned int items = this->TreeRoot->ChildCount();
346         for (unsigned int x = 0; x < items; x++)
347         {
348                 TreeServer* Route = this->TreeRoot->GetChild(x);
349                 // Send the line IF:
350                 // The route has a socket (its a direct connection)
351                 // The route isnt the one to be omitted
352                 // The route isnt the path to the one to be omitted
353                 if ((Route) && (Route->GetSocket()) && (Route->GetName() != omit) && (omitroute != Route))
354                 {
355                         TreeSocket* Sock = Route->GetSocket();
356                         if (Sock)
357                                 Sock->WriteLine(FullLine);
358                 }
359         }
360         return true;
361 }
362
363 bool SpanningTreeUtilities::DoOneToMany(const std::string &prefix, const std::string &command, std::deque<std::string> &params)
364 {
365         std::string FullLine = ":" + prefix + " " + command;
366         unsigned int words = params.size();
367         for (unsigned int x = 0; x < words; x++)
368         {
369                 FullLine = FullLine + " " + params[x];
370         }
371         unsigned int items = this->TreeRoot->ChildCount();
372         for (unsigned int x = 0; x < items; x++)
373         {
374                 TreeServer* Route = this->TreeRoot->GetChild(x);
375                 if (Route && Route->GetSocket())
376                 {
377                         TreeSocket* Sock = Route->GetSocket();
378                         if (Sock)
379                                 Sock->WriteLine(FullLine);
380                 }
381         }
382         return true;
383 }
384
385 bool SpanningTreeUtilities::DoOneToMany(const char* prefix, const char* command, std::deque<std::string> &params)
386 {
387         std::string spfx = prefix;
388         std::string scmd = command;
389         return this->DoOneToMany(spfx, scmd, params);
390 }
391
392 bool SpanningTreeUtilities::DoOneToAllButSender(const char* prefix, const char* command, std::deque<std::string> &params, std::string omit)
393 {
394         std::string spfx = prefix;
395         std::string scmd = command;
396         return this->DoOneToAllButSender(spfx, scmd, params, omit);
397 }
398
399 bool SpanningTreeUtilities::DoOneToOne(const std::string &prefix, const std::string &command, std::deque<std::string> &params, std::string target)
400 {
401         TreeServer* Route = this->BestRouteTo(target);
402         if (Route)
403         {
404                 std::string FullLine = ":" + prefix + " " + command;
405                 unsigned int words = params.size();
406                 for (unsigned int x = 0; x < words; x++)
407                 {
408                         FullLine = FullLine + " " + params[x];
409                 }
410                 if (Route && Route->GetSocket())
411                 {
412                         TreeSocket* Sock = Route->GetSocket();
413                         if (Sock)
414                                 Sock->WriteLine(FullLine);
415                 }
416                 return true;
417         }
418         else
419         {
420                 return false;
421         }
422 }
423
424 void SpanningTreeUtilities::RefreshIPCache()
425 {
426         ValidIPs.clear();
427         for (std::vector<Link>::iterator L = LinkBlocks.begin(); L != LinkBlocks.end(); L++)
428         {
429                 if ((!L->IPAddr.empty()) && (!L->RecvPass.empty()) && (!L->SendPass.empty()) && (!L->Name.empty()) && (L->Port))
430                 {
431                         ValidIPs.push_back(L->IPAddr);
432
433                         if (L->AllowMask.length())
434                                 ValidIPs.push_back(L->AllowMask);
435
436                         /* Needs resolving */
437                         bool ipvalid = true;
438                         QueryType start_type = DNS_QUERY_A;
439 #ifdef IPV6
440                         start_type = DNS_QUERY_AAAA;
441                         if (strchr(L->IPAddr.c_str(),':'))
442                         {
443                                 in6_addr n;
444                                 if (inet_pton(AF_INET6, L->IPAddr.c_str(), &n) < 1)
445                                         ipvalid = false;
446                         }
447                         else
448 #endif
449                         {
450                                 in_addr n;
451                                 if (inet_aton(L->IPAddr.c_str(),&n) < 1)
452                                         ipvalid = false;
453                         }
454                         if (!ipvalid)
455                         {
456                                 try
457                                 {
458                                         bool cached;
459                                         SecurityIPResolver* sr = new SecurityIPResolver((Module*)this->Creator, this, ServerInstance, L->IPAddr, *L, cached, start_type);
460                                         ServerInstance->AddResolver(sr, cached);
461                                 }
462                                 catch (...)
463                                 {
464                                 }
465                         }
466                 }
467         }
468 }
469
470 void SpanningTreeUtilities::ReadConfiguration(bool rebind)
471 {
472         ConfigReader* Conf = new ConfigReader(ServerInstance);
473         if (rebind)
474         {
475                 for (int j = 0; j < Conf->Enumerate("bind"); j++)
476                 {
477                         std::string Type = Conf->ReadValue("bind","type",j);
478                         std::string IP = Conf->ReadValue("bind","address",j);
479                         std::string Port = Conf->ReadValue("bind","port",j);
480                         std::string transport = Conf->ReadValue("bind","transport",j);
481                         if (Type == "servers")
482                         {
483                                 irc::portparser portrange(Port, false);
484                                 int portno = -1;
485                                 while ((portno = portrange.GetToken()))
486                                 {
487                                         if (IP == "*")
488                                                 IP.clear();
489
490                                         if ((!transport.empty()) && (hooks.find(transport.c_str()) ==  hooks.end()))
491                                         {
492                                                 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());
493                                                 break;
494                                         }
495
496                                         TreeSocket* listener = new TreeSocket(this, ServerInstance, IP.c_str(), portno, true, 10, transport.empty() ? NULL : hooks[transport.c_str()]);
497                                         if (listener->GetState() == I_LISTENING)
498                                         {
499                                                 ServerInstance->Log(DEFAULT,"m_spanningtree: Binding server port %s:%d successful!", IP.c_str(), portno);
500                                                 Bindings.push_back(listener);
501                                         }
502                                         else
503                                         {
504                                                 ServerInstance->Log(DEFAULT,"m_spanningtree: Warning: Failed to bind server port: %s:%d: %s",IP.c_str(), portno, strerror(errno));
505                                                 listener->Close();
506                                         }
507                                 }
508                         }
509                 }
510         }
511         FlatLinks = Conf->ReadFlag("options","flatlinks",0);
512         HideULines = Conf->ReadFlag("options","hideulines",0);
513         AnnounceTSChange = Conf->ReadFlag("options","announcets",0);
514         EnableTimeSync = Conf->ReadFlag("timesync","enable",0);
515         MasterTime = Conf->ReadFlag("timesync", "master", 0);
516         ChallengeResponse = !Conf->ReadFlag("options", "disablehmac", 0);
517         quiet_bursts = Conf->ReadFlag("options", "quietbursts", 0);
518         PingWarnTime = Conf->ReadInteger("options", "pingwarning", 0, true);
519         PingFreq = Conf->ReadInteger("options", "serverpingfreq", 0, true);
520
521         if (PingFreq == 0)
522                 PingFreq = 60;
523
524         if (PingWarnTime < 0 || PingWarnTime > PingFreq - 1)
525                 PingWarnTime = 0;
526
527         LinkBlocks.clear();
528         ValidIPs.clear();
529         for (int j = 0; j < Conf->Enumerate("link"); j++)
530         {
531                 Link L;
532                 std::string Allow = Conf->ReadValue("link", "allowmask", j);
533                 L.Name = (Conf->ReadValue("link", "name", j)).c_str();
534                 L.AllowMask = Allow;
535                 L.IPAddr = Conf->ReadValue("link", "ipaddr", j);
536                 L.FailOver = Conf->ReadValue("link", "failover", j).c_str();
537                 L.Port = Conf->ReadInteger("link", "port", j, true);
538                 L.SendPass = Conf->ReadValue("link", "sendpass", j);
539                 L.RecvPass = Conf->ReadValue("link", "recvpass", j);
540                 L.AutoConnect = Conf->ReadInteger("link", "autoconnect", j, true);
541                 L.HiddenFromStats = Conf->ReadFlag("link", "statshidden", j);
542                 L.Timeout = Conf->ReadInteger("link", "timeout", j, true);
543                 L.Hook = Conf->ReadValue("link", "transport", j);
544                 L.Bind = Conf->ReadValue("link", "bind", j);
545                 L.Hidden = Conf->ReadFlag("link", "hidden", j);
546
547                 if ((!L.Hook.empty()) && (hooks.find(L.Hook.c_str()) ==  hooks.end()))
548                 {
549                         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.",
550                         L.Hook.c_str(), L.Name.c_str());
551                         continue;
552
553                 }
554
555                 L.NextConnectTime = time(NULL) + L.AutoConnect;
556                 /* Bugfix by brain, do not allow people to enter bad configurations */
557                 if (L.Name != ServerInstance->Config->ServerName)
558                 {
559                         if ((!L.IPAddr.empty()) && (!L.RecvPass.empty()) && (!L.SendPass.empty()) && (!L.Name.empty()) && (L.Port))
560                         {
561                                 ValidIPs.push_back(L.IPAddr);
562
563                                 if (Allow.length())
564                                         ValidIPs.push_back(Allow);
565
566                                 /* Needs resolving */
567                                 bool ipvalid = true;
568                                 QueryType start_type = DNS_QUERY_A;
569 #ifdef IPV6
570                                 start_type = DNS_QUERY_AAAA;
571                                 if (strchr(L.IPAddr.c_str(),':'))
572                                 {
573                                         in6_addr n;
574                                         if (inet_pton(AF_INET6, L.IPAddr.c_str(), &n) < 1)
575                                                 ipvalid = false;
576                                 }
577                                 else
578                                 {
579                                         in_addr n;
580                                         if (inet_aton(L.IPAddr.c_str(),&n) < 1)
581                                                 ipvalid = false;
582                                 }
583 #else
584                                 in_addr n;
585                                 if (inet_aton(L.IPAddr.c_str(),&n) < 1)
586                                         ipvalid = false;
587 #endif
588
589                                 if (!ipvalid)
590                                 {
591                                         try
592                                         {
593                                                 bool cached;
594                                                 SecurityIPResolver* sr = new SecurityIPResolver((Module*)this->Creator, this, ServerInstance, L.IPAddr, L, cached, start_type);
595                                                 ServerInstance->AddResolver(sr, cached);
596                                         }
597                                         catch (...)
598                                         {
599                                         }
600                                 }
601
602                                 LinkBlocks.push_back(L);
603                         }
604                         else
605                         {
606                                 if (L.IPAddr.empty())
607                                 {
608                                         ServerInstance->Log(DEFAULT,"Invalid configuration for server '%s', IP address not defined!",L.Name.c_str());
609                                 }
610                                 else if (L.RecvPass.empty())
611                                 {
612                                         ServerInstance->Log(DEFAULT,"Invalid configuration for server '%s', recvpass not defined!",L.Name.c_str());
613                                 }
614                                 else if (L.SendPass.empty())
615                                 {
616                                         ServerInstance->Log(DEFAULT,"Invalid configuration for server '%s', sendpass not defined!",L.Name.c_str());
617                                 }
618                                 else if (L.Name.empty())
619                                 {
620                                         ServerInstance->Log(DEFAULT,"Invalid configuration, link tag without a name!");
621                                 }
622                                 else if (!L.Port)
623                                 {
624                                         ServerInstance->Log(DEFAULT,"Invalid configuration for server '%s', no port specified!",L.Name.c_str());
625                                 }
626                         }
627                 }
628                 else
629                 {
630                         ServerInstance->Log(DEFAULT,"Invalid configuration for server '%s', link tag has the same server name as the local server!",L.Name.c_str());
631                 }
632         }
633         DELETE(Conf);
634 }
635
636 void SpanningTreeUtilities::DoFailOver(Link* x)
637 {
638         if (x->FailOver.length())
639         {
640                 if (x->FailOver == x->Name)
641                 {
642                         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());
643                         return;
644                 }
645                 Link* TryThisOne = this->FindLink(x->FailOver.c_str());
646                 if (TryThisOne)
647                 {
648                         Creator->RemoteMessage(NULL,"FAILOVER: Trying failover link for \002%s\002: \002%s\002...", x->Name.c_str(), TryThisOne->Name.c_str());
649                         Creator->ConnectServer(TryThisOne);
650                 }
651                 else
652                 {
653                         Creator->RemoteMessage(NULL,"FAILOVER: Invalid failover server specified for server \002%s\002, will not follow!", x->Name.c_str());
654                 }
655         }
656 }
657
658 Link* SpanningTreeUtilities::FindLink(const std::string& name)
659 {
660         for (std::vector<Link>::iterator x = LinkBlocks.begin(); x < LinkBlocks.end(); x++)
661         {
662                 if (ServerInstance->MatchText(x->Name.c_str(), name.c_str()))
663                 {
664                         return &(*x);
665                 }
666         }
667         return NULL;
668 }