]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/netburst.cpp
5248ea897e3a33dcc4ba5f4be58ba062cd2d2d68
[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
25 #include "treesocket.h"
26 #include "treeserver.h"
27 #include "utils.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,1);
48         /* Send users and their oper status */
49         this->SendUsers();
50         /* Send everything else (channel modes, xlines etc) */
51         this->SendChannelModes();
52         this->SendXLines();
53         FOREACH_MOD(I_OnSyncNetwork,OnSyncNetwork(Utils->Creator,(void*)this));
54         this->WriteLine(":" + ServerInstance->Config->GetSID() + " ENDBURST");
55         ServerInstance->SNO->WriteToSnoMask('l',"Finished bursting to \2"+ s->GetName()+"\2.");
56 }
57
58 /** Recursively send the server tree with distances as hops.
59  * This is used during network burst to inform the other server
60  * (and any of ITS servers too) of what servers we know about.
61  * If at any point any of these servers already exist on the other
62  * end, our connection may be terminated. The hopcounts given
63  * by this function are relative, this doesn't matter so long as
64  * they are all >1, as all the remote servers re-calculate them
65  * to be relative too, with themselves as hop 0.
66  */
67 void TreeSocket::SendServers(TreeServer* Current, TreeServer* s, int hops)
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 * %d %s :%s", Current->GetID().c_str(), recursive_servername.c_str(), hops,
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, hops+1);
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  */
91 void TreeSocket::SendFJoins(Channel* c)
92 {
93         std::string buffer;
94         char list[MAXBUF];
95
96         size_t curlen, headlen;
97         curlen = headlen = snprintf(list,MAXBUF,":%s FJOIN %s %lu +%s :",
98                 ServerInstance->Config->GetSID().c_str(), c->name.c_str(), (unsigned long)c->age, c->ChanModes(true));
99         int numusers = 0;
100         char* ptr = list + curlen;
101         bool looped_once = false;
102
103         const UserMembList *ulist = c->GetUsers();
104         std::string modes;
105         std::string params;
106
107         for (UserMembCIter i = ulist->begin(); i != ulist->end(); i++)
108         {
109                 size_t ptrlen = 0;
110                 std::string modestr = i->second->modes;
111
112                 if ((curlen + modestr.length() + i->first->uuid.length() + 4) > 480)
113                 {
114                         // remove the final space
115                         if (ptr[-1] == ' ')
116                                 ptr[-1] = '\0';
117                         buffer.append(list).append("\r\n");
118                         curlen = headlen;
119                         ptr = list + headlen;
120                         numusers = 0;
121                 }
122
123                 ptrlen = snprintf(ptr, MAXBUF-curlen, "%s,%s ", modestr.c_str(), i->first->uuid.c_str());
124
125                 looped_once = true;
126
127                 curlen += ptrlen;
128                 ptr += ptrlen;
129
130                 numusers++;
131         }
132
133         // Okay, permanent channels will (of course) need this \r\n anyway, numusers check is if there
134         // actually were people in the channel (looped_once == true)
135         if (!looped_once || numusers > 0)
136         {
137                 // remove the final space
138                 if (ptr[-1] == ' ')
139                         ptr[-1] = '\0';
140                 buffer.append(list).append("\r\n");
141         }
142
143         int linesize = 1;
144         for (BanList::iterator b = c->bans.begin(); b != c->bans.end(); b++)
145         {
146                 int size = b->data.length() + 2;
147                 int currsize = linesize + size;
148                 if (currsize <= 350)
149                 {
150                         modes.append("b");
151                         params.append(" ").append(b->data);
152                         linesize += size;
153                 }
154                 if ((modes.length() >= ServerInstance->Config->Limits.MaxModes) || (currsize > 350))
155                 {
156                         /* Wrap at MAXMODES */
157                         buffer.append(":").append(ServerInstance->Config->GetSID()).append(" FMODE ").append(c->name).append(" ").append(ConvToStr(c->age)).append(" +").append(modes).append(params).append("\r\n");
158                         modes.clear();
159                         params.clear();
160                         linesize = 1;
161                 }
162         }
163
164         /* Only send these if there are any */
165         if (!modes.empty())
166                 buffer.append(":").append(ServerInstance->Config->GetSID()).append(" FMODE ").append(c->name).append(" ").append(ConvToStr(c->age)).append(" +").append(modes).append(params);
167
168         this->WriteLine(buffer);
169 }
170
171 /** Send all XLines we know about */
172 void TreeSocket::SendXLines()
173 {
174         char data[MAXBUF];
175         std::string n = ServerInstance->Config->GetSID();
176         const char* sn = n.c_str();
177
178         std::vector<std::string> types = ServerInstance->XLines->GetAllTypes();
179         time_t current = ServerInstance->Time();
180
181         for (std::vector<std::string>::iterator it = types.begin(); it != types.end(); ++it)
182         {
183                 XLineLookup* lookup = ServerInstance->XLines->GetAll(*it);
184
185                 if (lookup)
186                 {
187                         for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
188                         {
189                                 /* Is it burstable? this is better than an explicit check for type 'K'.
190                                  * We break the loop as NONE of the items in this group are worth iterating.
191                                  */
192                                 if (!i->second->IsBurstable())
193                                         break;
194
195                                 /* If it's expired, don't bother to burst it
196                                  */
197                                 if (i->second->duration && current > i->second->expiry)
198                                         continue;
199
200                                 snprintf(data,MAXBUF,":%s ADDLINE %s %s %s %lu %lu :%s",sn, it->c_str(), i->second->Displayable(),
201                                                 i->second->source.c_str(),
202                                                 (unsigned long)i->second->set_time,
203                                                 (unsigned long)i->second->duration,
204                                                 i->second->reason.c_str());
205                                 this->WriteLine(data);
206                         }
207                 }
208         }
209 }
210
211 /** Send channel topic, modes and metadata */
212 void TreeSocket::SendChannelModes()
213 {
214         char data[MAXBUF];
215         std::string n = ServerInstance->Config->GetSID();
216         const char* sn = n.c_str();
217
218         for (chan_hash::iterator c = ServerInstance->chanlist->begin(); c != ServerInstance->chanlist->end(); c++)
219         {
220                 SendFJoins(c->second);
221                 if (!c->second->topic.empty())
222                 {
223                         snprintf(data,MAXBUF,":%s FTOPIC %s %lu %s :%s", sn, c->second->name.c_str(), (unsigned long)c->second->topicset, c->second->setby.c_str(), c->second->topic.c_str());
224                         this->WriteLine(data);
225                 }
226
227                 for(Extensible::ExtensibleStore::const_iterator i = c->second->GetExtList().begin(); i != c->second->GetExtList().end(); i++)
228                 {
229                         ExtensionItem* item = i->first;
230                         std::string value = item->serialize(FORMAT_NETWORK, c->second, i->second);
231                         if (!value.empty())
232                                 Utils->Creator->ProtoSendMetaData(this, c->second, item->name, value);
233                 }
234
235                 FOREACH_MOD(I_OnSyncChannel,OnSyncChannel(c->second,Utils->Creator,this));
236         }
237 }
238
239 /** send all users and their oper state/modes */
240 void TreeSocket::SendUsers()
241 {
242         char data[MAXBUF];
243         std::string dataline;
244         for (user_hash::iterator u = ServerInstance->Users->clientlist->begin(); u != ServerInstance->Users->clientlist->end(); u++)
245         {
246                 if (u->second->registered == REG_ALL)
247                 {
248                         TreeServer* theirserver = Utils->FindServer(u->second->server);
249                         if (theirserver)
250                         {
251                                 snprintf(data,MAXBUF,":%s UID %s %lu %s %s %s %s %s %lu +%s :%s",
252                                                 theirserver->GetID().c_str(),   /* Prefix: SID */
253                                                 u->second->uuid.c_str(),        /* 0: UUID */
254                                                 (unsigned long)u->second->age,  /* 1: TS */
255                                                 u->second->nick.c_str(),        /* 2: Nick */
256                                                 u->second->host.c_str(),        /* 3: Displayed Host */
257                                                 u->second->dhost.c_str(),       /* 4: Real host */
258                                                 u->second->ident.c_str(),       /* 5: Ident */
259                                                 u->second->GetIPString(),       /* 6: IP string */
260                                                 (unsigned long)u->second->signon, /* 7: Signon time for WHOWAS */
261                                                 u->second->FormatModes(true),   /* 8...n: Modes and params */
262                                                 u->second->fullname.c_str());   /* size-1: GECOS */
263                                 this->WriteLine(data);
264                                 if (IS_OPER(u->second))
265                                 {
266                                         snprintf(data,MAXBUF,":%s OPERTYPE %s", u->second->uuid.c_str(), u->second->oper->name.c_str());
267                                         this->WriteLine(data);
268                                 }
269                                 if (IS_AWAY(u->second))
270                                 {
271                                         snprintf(data,MAXBUF,":%s AWAY %ld :%s", u->second->uuid.c_str(), (long)u->second->awaytime, u->second->awaymsg.c_str());
272                                         this->WriteLine(data);
273                                 }
274                         }
275
276                         for(Extensible::ExtensibleStore::const_iterator i = u->second->GetExtList().begin(); i != u->second->GetExtList().end(); i++)
277                         {
278                                 ExtensionItem* item = i->first;
279                                 std::string value = item->serialize(FORMAT_NETWORK, u->second, i->second);
280                                 if (!value.empty())
281                                         Utils->Creator->ProtoSendMetaData(this, u->second, item->name, value);
282                         }
283
284                         FOREACH_MOD(I_OnSyncUser,OnSyncUser(u->second,Utils->Creator,this));
285                 }
286         }
287 }
288