]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/netburst.cpp
978886be6a89b371863aad40423bbbb590ea5835
[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 "utils.h"
29 #include "main.h"
30
31 /** This function is called when we want to send a netburst to a local
32  * server. There is a set order we must do this, because for example
33  * users require their servers to exist, and channels require their
34  * users to exist. You get the idea.
35  */
36 void TreeSocket::DoBurst(TreeServer* s)
37 {
38         std::string servername = s->GetName();
39         ServerInstance->SNO->WriteToSnoMask('l',"Bursting to \2%s\2 (Authentication: %s%s).",
40                 servername.c_str(),
41                 capab->auth_fingerprint ? "SSL Fingerprint and " : "",
42                 capab->auth_challenge ? "challenge-response" : "plaintext password");
43         this->CleanNegotiationInfo();
44         this->WriteLine(":" + ServerInstance->Config->GetSID() + " BURST " + ConvToStr(ServerInstance->Time()));
45         /* send our version string */
46         this->WriteLine(":" + ServerInstance->Config->GetSID() + " VERSION :"+ServerInstance->GetVersionString());
47         /* Send server tree */
48         this->SendServers(Utils->TreeRoot,s,1);
49         /* Send users and their oper status */
50         this->SendUsers();
51         /* Send everything else (channel modes, xlines etc) */
52         this->SendChannelModes();
53         this->SendXLines();
54         FOREACH_MOD(I_OnSyncNetwork,OnSyncNetwork(Utils->Creator,(void*)this));
55         this->WriteLine(":" + ServerInstance->Config->GetSID() + " ENDBURST");
56         ServerInstance->SNO->WriteToSnoMask('l',"Finished bursting to \2"+ s->GetName()+"\2.");
57 }
58
59 /** Recursively send the server tree with distances as hops.
60  * This is used during network burst to inform the other server
61  * (and any of ITS servers too) of what servers we know about.
62  * If at any point any of these servers already exist on the other
63  * end, our connection may be terminated. The hopcounts given
64  * by this function are relative, this doesn't matter so long as
65  * they are all >1, as all the remote servers re-calculate them
66  * to be relative too, with themselves as hop 0.
67  */
68 void TreeSocket::SendServers(TreeServer* Current, TreeServer* s, int hops)
69 {
70         char command[MAXBUF];
71         for (unsigned int q = 0; q < Current->ChildCount(); q++)
72         {
73                 TreeServer* recursive_server = Current->GetChild(q);
74                 if (recursive_server != s)
75                 {
76                         std::string recursive_servername = recursive_server->GetName();
77                         snprintf(command, MAXBUF, ":%s SERVER %s * %d %s :%s", Current->GetID().c_str(), recursive_servername.c_str(), hops,
78                                         recursive_server->GetID().c_str(),
79                                         recursive_server->GetDesc().c_str());
80                         this->WriteLine(command);
81                         this->WriteLine(":"+recursive_server->GetID()+" VERSION :"+recursive_server->GetVersion());
82                         /* down to next level */
83                         this->SendServers(recursive_server, s, hops+1);
84                 }
85         }
86 }
87
88 /** Send one or more FJOINs for a channel of users.
89  * If the length of a single line is more than 480-NICKMAX
90  * in length, it is split over multiple lines.
91  */
92 void TreeSocket::SendFJoins(Channel* c)
93 {
94         std::string buffer;
95         char list[MAXBUF];
96
97         size_t curlen, headlen;
98         curlen = headlen = snprintf(list,MAXBUF,":%s FJOIN %s %lu +%s :",
99                 ServerInstance->Config->GetSID().c_str(), c->name.c_str(), (unsigned long)c->age, c->ChanModes(true));
100         int numusers = 0;
101         char* ptr = list + curlen;
102         bool looped_once = false;
103
104         const UserMembList *ulist = c->GetUsers();
105         std::string modes;
106         std::string params;
107
108         for (UserMembCIter i = ulist->begin(); i != ulist->end(); i++)
109         {
110                 size_t ptrlen = 0;
111                 std::string modestr = i->second->modes;
112
113                 if ((curlen + modestr.length() + i->first->uuid.length() + 4) > 480)
114                 {
115                         // remove the final space
116                         if (ptr[-1] == ' ')
117                                 ptr[-1] = '\0';
118                         buffer.append(list).append("\r\n");
119                         curlen = headlen;
120                         ptr = list + headlen;
121                         numusers = 0;
122                 }
123
124                 ptrlen = snprintf(ptr, MAXBUF-curlen, "%s,%s ", modestr.c_str(), i->first->uuid.c_str());
125
126                 looped_once = true;
127
128                 curlen += ptrlen;
129                 ptr += ptrlen;
130
131                 numusers++;
132         }
133
134         // Okay, permanent channels will (of course) need this \r\n anyway, numusers check is if there
135         // actually were people in the channel (looped_once == true)
136         if (!looped_once || numusers > 0)
137         {
138                 // remove the final space
139                 if (ptr[-1] == ' ')
140                         ptr[-1] = '\0';
141                 buffer.append(list).append("\r\n");
142         }
143
144         ModeReference ban(NULL, "ban");
145         static_cast<ListModeBase*>(*ban)->DoSyncChannel(c, Utils->Creator, this);
146 }
147
148 /** Send all XLines we know about */
149 void TreeSocket::SendXLines()
150 {
151         char data[MAXBUF];
152         std::string n = ServerInstance->Config->GetSID();
153         const char* sn = n.c_str();
154
155         std::vector<std::string> types = ServerInstance->XLines->GetAllTypes();
156         time_t current = ServerInstance->Time();
157
158         for (std::vector<std::string>::iterator it = types.begin(); it != types.end(); ++it)
159         {
160                 XLineLookup* lookup = ServerInstance->XLines->GetAll(*it);
161
162                 if (lookup)
163                 {
164                         for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
165                         {
166                                 /* Is it burstable? this is better than an explicit check for type 'K'.
167                                  * We break the loop as NONE of the items in this group are worth iterating.
168                                  */
169                                 if (!i->second->IsBurstable())
170                                         break;
171
172                                 /* If it's expired, don't bother to burst it
173                                  */
174                                 if (i->second->duration && current > i->second->expiry)
175                                         continue;
176
177                                 snprintf(data,MAXBUF,":%s ADDLINE %s %s %s %lu %lu :%s",sn, it->c_str(), i->second->Displayable(),
178                                                 i->second->source.c_str(),
179                                                 (unsigned long)i->second->set_time,
180                                                 (unsigned long)i->second->duration,
181                                                 i->second->reason.c_str());
182                                 this->WriteLine(data);
183                         }
184                 }
185         }
186 }
187
188 /** Send channel topic, modes and metadata */
189 void TreeSocket::SendChannelModes()
190 {
191         char data[MAXBUF];
192         std::string n = ServerInstance->Config->GetSID();
193         const char* sn = n.c_str();
194
195         for (chan_hash::iterator c = ServerInstance->chanlist->begin(); c != ServerInstance->chanlist->end(); c++)
196         {
197                 SendFJoins(c->second);
198                 if (!c->second->topic.empty())
199                 {
200                         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());
201                         this->WriteLine(data);
202                 }
203
204                 for(Extensible::ExtensibleStore::const_iterator i = c->second->GetExtList().begin(); i != c->second->GetExtList().end(); i++)
205                 {
206                         ExtensionItem* item = i->first;
207                         std::string value = item->serialize(FORMAT_NETWORK, c->second, i->second);
208                         if (!value.empty())
209                                 Utils->Creator->ProtoSendMetaData(this, c->second, item->name, value);
210                 }
211
212                 FOREACH_MOD(I_OnSyncChannel,OnSyncChannel(c->second,Utils->Creator,this));
213         }
214 }
215
216 /** send all users and their oper state/modes */
217 void TreeSocket::SendUsers()
218 {
219         char data[MAXBUF];
220         std::string dataline;
221         for (user_hash::iterator u = ServerInstance->Users->clientlist->begin(); u != ServerInstance->Users->clientlist->end(); u++)
222         {
223                 if (u->second->registered == REG_ALL)
224                 {
225                         TreeServer* theirserver = Utils->FindServer(u->second->server);
226                         if (theirserver)
227                         {
228                                 snprintf(data,MAXBUF,":%s UID %s %lu %s %s %s %s %s %lu +%s :%s",
229                                                 theirserver->GetID().c_str(),   /* Prefix: SID */
230                                                 u->second->uuid.c_str(),        /* 0: UUID */
231                                                 (unsigned long)u->second->age,  /* 1: TS */
232                                                 u->second->nick.c_str(),        /* 2: Nick */
233                                                 u->second->host.c_str(),        /* 3: Displayed Host */
234                                                 u->second->dhost.c_str(),       /* 4: Real host */
235                                                 u->second->ident.c_str(),       /* 5: Ident */
236                                                 u->second->GetIPString().c_str(),       /* 6: IP string */
237                                                 (unsigned long)u->second->signon, /* 7: Signon time for WHOWAS */
238                                                 u->second->FormatModes(true),   /* 8...n: Modes and params */
239                                                 u->second->fullname.c_str());   /* size-1: GECOS */
240                                 this->WriteLine(data);
241                                 if (IS_OPER(u->second))
242                                 {
243                                         snprintf(data,MAXBUF,":%s OPERTYPE %s", u->second->uuid.c_str(), u->second->oper->name.c_str());
244                                         this->WriteLine(data);
245                                 }
246                                 if (IS_AWAY(u->second))
247                                 {
248                                         snprintf(data,MAXBUF,":%s AWAY %ld :%s", u->second->uuid.c_str(), (long)u->second->awaytime, u->second->awaymsg.c_str());
249                                         this->WriteLine(data);
250                                 }
251                         }
252
253                         for(Extensible::ExtensibleStore::const_iterator i = u->second->GetExtList().begin(); i != u->second->GetExtList().end(); i++)
254                         {
255                                 ExtensionItem* item = i->first;
256                                 std::string value = item->serialize(FORMAT_NETWORK, u->second, i->second);
257                                 if (!value.empty())
258                                         Utils->Creator->ProtoSendMetaData(this, u->second, item->name, value);
259                         }
260
261                         FOREACH_MOD(I_OnSyncUser,OnSyncUser(u->second,Utils->Creator,this));
262                 }
263         }
264 }
265