]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/netburst.cpp
m_spanningtree Propagate topic changes via FTOPIC in order to prevent desync when...
[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 #include "commands.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         ServerInstance->SNO->WriteToSnoMask('l',"Bursting to \2%s\2 (Authentication: %s%s).",
39                 s->GetName().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(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         const TreeServer::ChildServers& children = Current->GetChildren();
70         for (TreeServer::ChildServers::const_iterator i = children.begin(); i != children.end(); ++i)
71         {
72                 TreeServer* recursive_server = *i;
73                 if (recursive_server != s)
74                 {
75                         this->WriteLine(CommandServer::Builder(recursive_server));
76                         this->WriteLine(":" + recursive_server->GetID() + " VERSION :" + recursive_server->GetVersion());
77                         /* down to next level */
78                         this->SendServers(recursive_server, s);
79                 }
80         }
81 }
82
83 /** Send one or more FJOINs for a channel of users.
84  * If the length of a single line is more than 480-NICKMAX
85  * in length, it is split over multiple lines.
86  * Send one or more FMODEs for a channel with the
87  * channel bans, if there's any.
88  */
89 void TreeSocket::SendFJoins(Channel* c)
90 {
91         std::string line(":");
92         line.append(ServerInstance->Config->GetSID()).append(" FJOIN ").append(c->name).append(1, ' ').append(ConvToStr(c->age)).append(" +");
93         std::string::size_type erase_from = line.length();
94         line.append(c->ChanModes(true)).append(" :");
95
96         const UserMembList *ulist = c->GetUsers();
97
98         for (UserMembCIter i = ulist->begin(); i != ulist->end(); ++i)
99         {
100                 const std::string& modestr = i->second->modes;
101                 if ((line.length() + modestr.length() + UIDGenerator::UUID_LENGTH + 2) > 480)
102                 {
103                         this->WriteLine(line);
104                         line.erase(erase_from);
105                         line.append(" :");
106                 }
107                 line.append(modestr).append(1, ',').append(i->first->uuid).push_back(' ');
108         }
109         this->WriteLine(line);
110
111         ChanModeReference ban(NULL, "ban");
112         static_cast<ListModeBase*>(*ban)->DoSyncChannel(c, Utils->Creator, this);
113 }
114
115 /** Send all XLines we know about */
116 void TreeSocket::SendXLines()
117 {
118         std::vector<std::string> types = ServerInstance->XLines->GetAllTypes();
119
120         for (std::vector<std::string>::const_iterator it = types.begin(); it != types.end(); ++it)
121         {
122                 /* Expired lines are removed in XLineManager::GetAll() */
123                 XLineLookup* lookup = ServerInstance->XLines->GetAll(*it);
124
125                 /* lookup cannot be NULL in this case but a check won't hurt */
126                 if (lookup)
127                 {
128                         for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
129                         {
130                                 /* Is it burstable? this is better than an explicit check for type 'K'.
131                                  * We break the loop as NONE of the items in this group are worth iterating.
132                                  */
133                                 if (!i->second->IsBurstable())
134                                         break;
135
136                                 this->WriteLine(CommandAddLine::Builder(i->second));
137                         }
138                 }
139         }
140 }
141
142 /** Send channel topic, modes and metadata */
143 void TreeSocket::SyncChannel(Channel* chan)
144 {
145         SendFJoins(chan);
146
147         // If the topic was ever set, send it, even if it's empty now
148         // because a new empty topic should override an old non-empty topic
149         if (chan->topicset != 0)
150                 this->WriteLine(CommandFTopic::Builder(chan));
151
152         for (Extensible::ExtensibleStore::const_iterator i = chan->GetExtList().begin(); i != chan->GetExtList().end(); i++)
153         {
154                 ExtensionItem* item = i->first;
155                 std::string value = item->serialize(FORMAT_NETWORK, chan, i->second);
156                 if (!value.empty())
157                         Utils->Creator->ProtoSendMetaData(this, chan, item->name, value);
158         }
159
160         FOREACH_MOD(OnSyncChannel, (chan, Utils->Creator, this));
161 }
162
163 /** send all users and their oper state/modes */
164 void TreeSocket::SendUsers()
165 {
166         for (user_hash::iterator u = ServerInstance->Users->clientlist->begin(); u != ServerInstance->Users->clientlist->end(); u++)
167         {
168                 User* user = u->second;
169                 if (user->registered != REG_ALL)
170                         continue;
171
172                 this->WriteLine(CommandUID::Builder(user));
173
174                 if (user->IsOper())
175                         this->WriteLine(CommandOpertype::Builder(user));
176
177                 if (user->IsAway())
178                         this->WriteLine(CommandAway::Builder(user));
179
180                 const Extensible::ExtensibleStore& exts = user->GetExtList();
181                 for (Extensible::ExtensibleStore::const_iterator i = exts.begin(); i != exts.end(); ++i)
182                 {
183                         ExtensionItem* item = i->first;
184                         std::string value = item->serialize(FORMAT_NETWORK, u->second, i->second);
185                         if (!value.empty())
186                                 Utils->Creator->ProtoSendMetaData(this, u->second, item->name, value);
187                 }
188
189                 FOREACH_MOD(OnSyncUser, (user, Utils->Creator, this));
190         }
191 }
192