]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/netburst.cpp
Change CacheRefreshTimer tick time back to 5 minutes
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / netburst.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
6  *   Copyright (C) 2008 Craig Edwards <craigedwards@brainbox.cc>
7  *
8  * This file is part of InspIRCd.  InspIRCd is free software: you can
9  * redistribute it and/or modify it under the terms of the GNU General Public
10  * License as published by the Free Software Foundation, version 2.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21
22 #include "inspircd.h"
23 #include "xline.h"
24 #include "listmode.h"
25
26 #include "treesocket.h"
27 #include "treeserver.h"
28 #include "main.h"
29
30 /** This function is called when we want to send a netburst to a local
31  * server. There is a set order we must do this, because for example
32  * users require their servers to exist, and channels require their
33  * users to exist. You get the idea.
34  */
35 void TreeSocket::DoBurst(TreeServer* s)
36 {
37         std::string servername = s->GetName();
38         ServerInstance->SNO->WriteToSnoMask('l',"Bursting to \2%s\2 (Authentication: %s%s).",
39                 servername.c_str(),
40                 capab->auth_fingerprint ? "SSL Fingerprint and " : "",
41                 capab->auth_challenge ? "challenge-response" : "plaintext password");
42         this->CleanNegotiationInfo();
43         this->WriteLine(":" + ServerInstance->Config->GetSID() + " BURST " + ConvToStr(ServerInstance->Time()));
44         /* send our version string */
45         this->WriteLine(":" + ServerInstance->Config->GetSID() + " VERSION :"+ServerInstance->GetVersionString());
46         /* Send server tree */
47         this->SendServers(Utils->TreeRoot, s);
48         /* Send users and their oper status */
49         this->SendUsers();
50
51         for (chan_hash::const_iterator i = ServerInstance->chanlist->begin(); i != ServerInstance->chanlist->end(); ++i)
52                 SyncChannel(i->second);
53
54         this->SendXLines();
55         FOREACH_MOD(I_OnSyncNetwork,OnSyncNetwork(Utils->Creator,(void*)this));
56         this->WriteLine(":" + ServerInstance->Config->GetSID() + " ENDBURST");
57         ServerInstance->SNO->WriteToSnoMask('l',"Finished bursting to \2"+ s->GetName()+"\2.");
58 }
59
60 /** Recursively send the server tree.
61  * This is used during network burst to inform the other server
62  * (and any of ITS servers too) of what servers we know about.
63  * If at any point any of these servers already exist on the other
64  * end, our connection may be terminated.
65  * The hopcount parameter (3rd) is deprecated, and is always 0.
66  */
67 void TreeSocket::SendServers(TreeServer* Current, TreeServer* s)
68 {
69         char command[MAXBUF];
70         for (unsigned int q = 0; q < Current->ChildCount(); q++)
71         {
72                 TreeServer* recursive_server = Current->GetChild(q);
73                 if (recursive_server != s)
74                 {
75                         std::string recursive_servername = recursive_server->GetName();
76                         snprintf(command, MAXBUF, ":%s SERVER %s * 0 %s :%s", Current->GetID().c_str(), recursive_servername.c_str(),
77                                         recursive_server->GetID().c_str(),
78                                         recursive_server->GetDesc().c_str());
79                         this->WriteLine(command);
80                         this->WriteLine(":"+recursive_server->GetID()+" VERSION :"+recursive_server->GetVersion());
81                         /* down to next level */
82                         this->SendServers(recursive_server, s);
83                 }
84         }
85 }
86
87 /** Send one or more FJOINs for a channel of users.
88  * If the length of a single line is more than 480-NICKMAX
89  * in length, it is split over multiple lines.
90  * Send one or more FMODEs for a channel with the
91  * channel bans, if there's any.
92  */
93 void TreeSocket::SendFJoins(Channel* c)
94 {
95         std::string line(":");
96         line.append(ServerInstance->Config->GetSID()).append(" FJOIN ").append(c->name).append(1, ' ').append(ConvToStr(c->age)).append(" +");
97         std::string::size_type erase_from = line.length();
98         line.append(c->ChanModes(true)).append(" :");
99
100         const UserMembList *ulist = c->GetUsers();
101
102         for (UserMembCIter i = ulist->begin(); i != ulist->end(); ++i)
103         {
104                 const std::string& modestr = i->second->modes;
105                 if ((line.length() + modestr.length() + UIDGenerator::UUID_LENGTH + 2) > 480)
106                 {
107                         this->WriteLine(line);
108                         line.erase(erase_from);
109                         line.append(" :");
110                 }
111                 line.append(modestr).append(1, ',').append(i->first->uuid).push_back(' ');
112         }
113         this->WriteLine(line);
114
115         ModeReference ban(NULL, "ban");
116         static_cast<ListModeBase*>(*ban)->DoSyncChannel(c, Utils->Creator, this);
117 }
118
119 /** Send all XLines we know about */
120 void TreeSocket::SendXLines()
121 {
122         char data[MAXBUF];
123
124         std::vector<std::string> types = ServerInstance->XLines->GetAllTypes();
125
126         for (std::vector<std::string>::const_iterator it = types.begin(); it != types.end(); ++it)
127         {
128                 /* Expired lines are removed in XLineManager::GetAll() */
129                 XLineLookup* lookup = ServerInstance->XLines->GetAll(*it);
130
131                 /* lookup cannot be NULL in this case but a check won't hurt */
132                 if (lookup)
133                 {
134                         for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
135                         {
136                                 /* Is it burstable? this is better than an explicit check for type 'K'.
137                                  * We break the loop as NONE of the items in this group are worth iterating.
138                                  */
139                                 if (!i->second->IsBurstable())
140                                         break;
141
142                                 snprintf(data, MAXBUF, ":%s ADDLINE %s %s %s %lu %lu :%s",
143                                                 ServerInstance->Config->GetSID().c_str(),
144                                                 it->c_str(),
145                                                 i->second->Displayable().c_str(),
146                                                 i->second->source.c_str(),
147                                                 (unsigned long)i->second->set_time,
148                                                 (unsigned long)i->second->duration,
149                                                 i->second->reason.c_str());
150                                 this->WriteLine(data);
151                         }
152                 }
153         }
154 }
155
156 /** Send channel topic, modes and metadata */
157 void TreeSocket::SyncChannel(Channel* chan)
158 {
159         char data[MAXBUF];
160
161         SendFJoins(chan);
162
163         // If the topic was ever set, send it, even if it's empty now
164         // because a new empty topic should override an old non-empty topic
165         if (chan->topicset != 0)
166         {
167                 snprintf(data,MAXBUF,":%s FTOPIC %s %lu %lu %s :%s", ServerInstance->Config->GetSID().c_str(), chan->name.c_str(), (unsigned long) chan->age, (unsigned long)chan->topicset, chan->setby.c_str(), chan->topic.c_str());
168                 this->WriteLine(data);
169         }
170
171         for (Extensible::ExtensibleStore::const_iterator i = chan->GetExtList().begin(); i != chan->GetExtList().end(); i++)
172         {
173                 ExtensionItem* item = i->first;
174                 std::string value = item->serialize(FORMAT_NETWORK, chan, i->second);
175                 if (!value.empty())
176                         Utils->Creator->ProtoSendMetaData(this, chan, item->name, value);
177         }
178
179         FOREACH_MOD(I_OnSyncChannel,OnSyncChannel(chan, Utils->Creator, this));
180 }
181
182 /** send all users and their oper state/modes */
183 void TreeSocket::SendUsers()
184 {
185         char data[MAXBUF];
186         std::string dataline;
187         for (user_hash::iterator u = ServerInstance->Users->clientlist->begin(); u != ServerInstance->Users->clientlist->end(); u++)
188         {
189                 if (u->second->registered == REG_ALL)
190                 {
191                         TreeServer* theirserver = Utils->FindServer(u->second->server);
192                         if (theirserver)
193                         {
194                                 snprintf(data,MAXBUF,":%s UID %s %lu %s %s %s %s %s %lu +%s :%s",
195                                                 theirserver->GetID().c_str(),   /* Prefix: SID */
196                                                 u->second->uuid.c_str(),        /* 0: UUID */
197                                                 (unsigned long)u->second->age,  /* 1: TS */
198                                                 u->second->nick.c_str(),        /* 2: Nick */
199                                                 u->second->host.c_str(),        /* 3: Displayed Host */
200                                                 u->second->dhost.c_str(),       /* 4: Real host */
201                                                 u->second->ident.c_str(),       /* 5: Ident */
202                                                 u->second->GetIPString().c_str(),       /* 6: IP string */
203                                                 (unsigned long)u->second->signon, /* 7: Signon time for WHOWAS */
204                                                 u->second->FormatModes(true),   /* 8...n: Modes and params */
205                                                 u->second->fullname.c_str());   /* size-1: GECOS */
206                                 this->WriteLine(data);
207                                 if (u->second->IsOper())
208                                 {
209                                         snprintf(data,MAXBUF,":%s OPERTYPE :%s", u->second->uuid.c_str(), u->second->oper->name.c_str());
210                                         this->WriteLine(data);
211                                 }
212                                 if (u->second->IsAway())
213                                 {
214                                         snprintf(data,MAXBUF,":%s AWAY %ld :%s", u->second->uuid.c_str(), (long)u->second->awaytime, u->second->awaymsg.c_str());
215                                         this->WriteLine(data);
216                                 }
217                         }
218
219                         for(Extensible::ExtensibleStore::const_iterator i = u->second->GetExtList().begin(); i != u->second->GetExtList().end(); i++)
220                         {
221                                 ExtensionItem* item = i->first;
222                                 std::string value = item->serialize(FORMAT_NETWORK, u->second, i->second);
223                                 if (!value.empty())
224                                         Utils->Creator->ProtoSendMetaData(this, u->second, item->name, value);
225                         }
226
227                         FOREACH_MOD(I_OnSyncUser,OnSyncUser(u->second,Utils->Creator,this));
228                 }
229         }
230 }
231