]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/netburst.cpp
m_spanningtree FJOIN handler: Refactor, update doc
[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
52         for (chan_hash::const_iterator i = ServerInstance->chanlist->begin(); i != ServerInstance->chanlist->end(); ++i)
53                 SyncChannel(i->second);
54
55         this->SendXLines();
56         FOREACH_MOD(I_OnSyncNetwork,OnSyncNetwork(Utils->Creator,(void*)this));
57         this->WriteLine(":" + ServerInstance->Config->GetSID() + " ENDBURST");
58         ServerInstance->SNO->WriteToSnoMask('l',"Finished bursting to \2"+ s->GetName()+"\2.");
59 }
60
61 /** Recursively send the server tree with distances as hops.
62  * This is used during network burst to inform the other server
63  * (and any of ITS servers too) of what servers we know about.
64  * If at any point any of these servers already exist on the other
65  * end, our connection may be terminated. The hopcounts given
66  * by this function are relative, this doesn't matter so long as
67  * they are all >1, as all the remote servers re-calculate them
68  * to be relative too, with themselves as hop 0.
69  */
70 void TreeSocket::SendServers(TreeServer* Current, TreeServer* s, int hops)
71 {
72         char command[MAXBUF];
73         for (unsigned int q = 0; q < Current->ChildCount(); q++)
74         {
75                 TreeServer* recursive_server = Current->GetChild(q);
76                 if (recursive_server != s)
77                 {
78                         std::string recursive_servername = recursive_server->GetName();
79                         snprintf(command, MAXBUF, ":%s SERVER %s * %d %s :%s", Current->GetID().c_str(), recursive_servername.c_str(), hops,
80                                         recursive_server->GetID().c_str(),
81                                         recursive_server->GetDesc().c_str());
82                         this->WriteLine(command);
83                         this->WriteLine(":"+recursive_server->GetID()+" VERSION :"+recursive_server->GetVersion());
84                         /* down to next level */
85                         this->SendServers(recursive_server, s, hops+1);
86                 }
87         }
88 }
89
90 /** Send one or more FJOINs for a channel of users.
91  * If the length of a single line is more than 480-NICKMAX
92  * in length, it is split over multiple lines.
93  * Send one or more FMODEs for a channel with the
94  * channel bans, if there's any.
95  */
96 void TreeSocket::SendFJoins(Channel* c)
97 {
98         std::string line(":");
99         line.append(ServerInstance->Config->GetSID()).append(" FJOIN ").append(c->name).append(1, ' ').append(ConvToStr(c->age)).append(" +");
100         std::string::size_type erase_from = line.length();
101         line.append(c->ChanModes(true)).append(" :");
102
103         const UserMembList *ulist = c->GetUsers();
104
105         for (UserMembCIter i = ulist->begin(); i != ulist->end(); ++i)
106         {
107                 const std::string& modestr = i->second->modes;
108                 if ((line.length() + modestr.length() + (UUID_LENGTH-1) + 2) > 480)
109                 {
110                         this->WriteLine(line);
111                         line.erase(erase_from);
112                         line.append(" :");
113                 }
114                 line.append(modestr).append(1, ',').append(i->first->uuid).push_back(' ');
115         }
116         this->WriteLine(line);
117
118         ModeReference ban(NULL, "ban");
119         static_cast<ListModeBase*>(*ban)->DoSyncChannel(c, Utils->Creator, this);
120 }
121
122 /** Send all XLines we know about */
123 void TreeSocket::SendXLines()
124 {
125         char data[MAXBUF];
126         const char* sn = ServerInstance->Config->GetSID().c_str();
127
128         std::vector<std::string> types = ServerInstance->XLines->GetAllTypes();
129
130         for (std::vector<std::string>::const_iterator it = types.begin(); it != types.end(); ++it)
131         {
132                 /* Expired lines are removed in XLineManager::GetAll() */
133                 XLineLookup* lookup = ServerInstance->XLines->GetAll(*it);
134
135                 /* lookup cannot be NULL in this case but a check won't hurt */
136                 if (lookup)
137                 {
138                         for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
139                         {
140                                 /* Is it burstable? this is better than an explicit check for type 'K'.
141                                  * We break the loop as NONE of the items in this group are worth iterating.
142                                  */
143                                 if (!i->second->IsBurstable())
144                                         break;
145
146                                 snprintf(data,MAXBUF,":%s ADDLINE %s %s %s %lu %lu :%s",sn, it->c_str(), i->second->Displayable(),
147                                                 i->second->source.c_str(),
148                                                 (unsigned long)i->second->set_time,
149                                                 (unsigned long)i->second->duration,
150                                                 i->second->reason.c_str());
151                                 this->WriteLine(data);
152                         }
153                 }
154         }
155 }
156
157 /** Send channel topic, modes and metadata */
158 void TreeSocket::SyncChannel(Channel* chan)
159 {
160         char data[MAXBUF];
161
162         SendFJoins(chan);
163         if (!chan->topic.empty())
164         {
165                 snprintf(data,MAXBUF,":%s FTOPIC %s %lu %s :%s", ServerInstance->Config->GetSID().c_str(), chan->name.c_str(), (unsigned long)chan->topicset, chan->setby.c_str(), chan->topic.c_str());
166                 this->WriteLine(data);
167         }
168
169         for (Extensible::ExtensibleStore::const_iterator i = chan->GetExtList().begin(); i != chan->GetExtList().end(); i++)
170         {
171                 ExtensionItem* item = i->first;
172                 std::string value = item->serialize(FORMAT_NETWORK, chan, i->second);
173                 if (!value.empty())
174                         Utils->Creator->ProtoSendMetaData(this, chan, item->name, value);
175         }
176
177         FOREACH_MOD(I_OnSyncChannel,OnSyncChannel(chan, Utils->Creator, this));
178 }
179
180 /** send all users and their oper state/modes */
181 void TreeSocket::SendUsers()
182 {
183         char data[MAXBUF];
184         std::string dataline;
185         for (user_hash::iterator u = ServerInstance->Users->clientlist->begin(); u != ServerInstance->Users->clientlist->end(); u++)
186         {
187                 if (u->second->registered == REG_ALL)
188                 {
189                         TreeServer* theirserver = Utils->FindServer(u->second->server);
190                         if (theirserver)
191                         {
192                                 snprintf(data,MAXBUF,":%s UID %s %lu %s %s %s %s %s %lu +%s :%s",
193                                                 theirserver->GetID().c_str(),   /* Prefix: SID */
194                                                 u->second->uuid.c_str(),        /* 0: UUID */
195                                                 (unsigned long)u->second->age,  /* 1: TS */
196                                                 u->second->nick.c_str(),        /* 2: Nick */
197                                                 u->second->host.c_str(),        /* 3: Displayed Host */
198                                                 u->second->dhost.c_str(),       /* 4: Real host */
199                                                 u->second->ident.c_str(),       /* 5: Ident */
200                                                 u->second->GetIPString().c_str(),       /* 6: IP string */
201                                                 (unsigned long)u->second->signon, /* 7: Signon time for WHOWAS */
202                                                 u->second->FormatModes(true),   /* 8...n: Modes and params */
203                                                 u->second->fullname.c_str());   /* size-1: GECOS */
204                                 this->WriteLine(data);
205                                 if (u->second->IsOper())
206                                 {
207                                         snprintf(data,MAXBUF,":%s OPERTYPE %s", u->second->uuid.c_str(), u->second->oper->name.c_str());
208                                         this->WriteLine(data);
209                                 }
210                                 if (u->second->IsAway())
211                                 {
212                                         snprintf(data,MAXBUF,":%s AWAY %ld :%s", u->second->uuid.c_str(), (long)u->second->awaytime, u->second->awaymsg.c_str());
213                                         this->WriteLine(data);
214                                 }
215                         }
216
217                         for(Extensible::ExtensibleStore::const_iterator i = u->second->GetExtList().begin(); i != u->second->GetExtList().end(); i++)
218                         {
219                                 ExtensionItem* item = i->first;
220                                 std::string value = item->serialize(FORMAT_NETWORK, u->second, i->second);
221                                 if (!value.empty())
222                                         Utils->Creator->ProtoSendMetaData(this, u->second, item->name, value);
223                         }
224
225                         FOREACH_MOD(I_OnSyncUser,OnSyncUser(u->second,Utils->Creator,this));
226                 }
227         }
228 }
229