]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/netburst.cpp
Change the syntax of FOREACH macros to be less dumb.
[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         ServerInstance->SNO->WriteToSnoMask('l',"Bursting to \2%s\2 (Authentication: %s%s).",
38                 s->GetName().c_str(),
39                 capab->auth_fingerprint ? "SSL Fingerprint and " : "",
40                 capab->auth_challenge ? "challenge-response" : "plaintext password");
41         this->CleanNegotiationInfo();
42         this->WriteLine(":" + ServerInstance->Config->GetSID() + " BURST " + ConvToStr(ServerInstance->Time()));
43         /* send our version string */
44         this->WriteLine(":" + ServerInstance->Config->GetSID() + " VERSION :"+ServerInstance->GetVersionString());
45         /* Send server tree */
46         this->SendServers(Utils->TreeRoot, s);
47         /* Send users and their oper status */
48         this->SendUsers();
49
50         for (chan_hash::const_iterator i = ServerInstance->chanlist->begin(); i != ServerInstance->chanlist->end(); ++i)
51                 SyncChannel(i->second);
52
53         this->SendXLines();
54         FOREACH_MOD(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.
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.
64  * The hopcount parameter (3rd) is deprecated, and is always 0.
65  */
66 void TreeSocket::SendServers(TreeServer* Current, TreeServer* s)
67 {
68         for (unsigned int q = 0; q < Current->ChildCount(); q++)
69         {
70                 TreeServer* recursive_server = Current->GetChild(q);
71                 if (recursive_server != s)
72                 {
73                         this->WriteLine(InspIRCd::Format(":%s SERVER %s * 0 %s :%s", Current->GetID().c_str(),
74                                 recursive_server->GetName().c_str(), recursive_server->GetID().c_str(), recursive_server->GetDesc().c_str()));
75                         this->WriteLine(":" + recursive_server->GetID() + " VERSION :" + recursive_server->GetVersion());
76                         /* down to next level */
77                         this->SendServers(recursive_server, s);
78                 }
79         }
80 }
81
82 /** Send one or more FJOINs for a channel of users.
83  * If the length of a single line is more than 480-NICKMAX
84  * in length, it is split over multiple lines.
85  * Send one or more FMODEs for a channel with the
86  * channel bans, if there's any.
87  */
88 void TreeSocket::SendFJoins(Channel* c)
89 {
90         std::string line(":");
91         line.append(ServerInstance->Config->GetSID()).append(" FJOIN ").append(c->name).append(1, ' ').append(ConvToStr(c->age)).append(" +");
92         std::string::size_type erase_from = line.length();
93         line.append(c->ChanModes(true)).append(" :");
94
95         const UserMembList *ulist = c->GetUsers();
96
97         for (UserMembCIter i = ulist->begin(); i != ulist->end(); ++i)
98         {
99                 const std::string& modestr = i->second->modes;
100                 if ((line.length() + modestr.length() + UIDGenerator::UUID_LENGTH + 2) > 480)
101                 {
102                         this->WriteLine(line);
103                         line.erase(erase_from);
104                         line.append(" :");
105                 }
106                 line.append(modestr).append(1, ',').append(i->first->uuid).push_back(' ');
107         }
108         this->WriteLine(line);
109
110         ChanModeReference ban(NULL, "ban");
111         static_cast<ListModeBase*>(*ban)->DoSyncChannel(c, Utils->Creator, this);
112 }
113
114 /** Send all XLines we know about */
115 void TreeSocket::SendXLines()
116 {
117         std::vector<std::string> types = ServerInstance->XLines->GetAllTypes();
118
119         for (std::vector<std::string>::const_iterator it = types.begin(); it != types.end(); ++it)
120         {
121                 /* Expired lines are removed in XLineManager::GetAll() */
122                 XLineLookup* lookup = ServerInstance->XLines->GetAll(*it);
123
124                 /* lookup cannot be NULL in this case but a check won't hurt */
125                 if (lookup)
126                 {
127                         for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
128                         {
129                                 /* Is it burstable? this is better than an explicit check for type 'K'.
130                                  * We break the loop as NONE of the items in this group are worth iterating.
131                                  */
132                                 if (!i->second->IsBurstable())
133                                         break;
134
135                                 this->WriteLine(InspIRCd::Format(":%s ADDLINE %s %s %s %lu %lu :%s",
136                                         ServerInstance->Config->GetSID().c_str(),
137                                         it->c_str(),
138                                         i->second->Displayable().c_str(),
139                                         i->second->source.c_str(),
140                                         (unsigned long)i->second->set_time,
141                                         (unsigned long)i->second->duration,
142                                         i->second->reason.c_str()));
143                         }
144                 }
145         }
146 }
147
148 /** Send channel topic, modes and metadata */
149 void TreeSocket::SyncChannel(Channel* chan)
150 {
151         SendFJoins(chan);
152
153         // If the topic was ever set, send it, even if it's empty now
154         // because a new empty topic should override an old non-empty topic
155         if (chan->topicset != 0)
156         {
157                 this->WriteLine(InspIRCd::Format(":%s FTOPIC %s %lu %lu %s :%s", ServerInstance->Config->GetSID().c_str(),
158                         chan->name.c_str(), (unsigned long)chan->age, (unsigned long)chan->topicset,
159                         chan->setby.c_str(), chan->topic.c_str()));
160         }
161
162         for (Extensible::ExtensibleStore::const_iterator i = chan->GetExtList().begin(); i != chan->GetExtList().end(); i++)
163         {
164                 ExtensionItem* item = i->first;
165                 std::string value = item->serialize(FORMAT_NETWORK, chan, i->second);
166                 if (!value.empty())
167                         Utils->Creator->ProtoSendMetaData(this, chan, item->name, value);
168         }
169
170         FOREACH_MOD(OnSyncChannel, (chan, Utils->Creator, this));
171 }
172
173 /** send all users and their oper state/modes */
174 void TreeSocket::SendUsers()
175 {
176         for (user_hash::iterator u = ServerInstance->Users->clientlist->begin(); u != ServerInstance->Users->clientlist->end(); u++)
177         {
178                 if (u->second->registered == REG_ALL)
179                 {
180                         TreeServer* theirserver = Utils->FindServer(u->second->server);
181                         if (theirserver)
182                         {
183                                 this->WriteLine(InspIRCd::Format(":%s UID %s %lu %s %s %s %s %s %lu +%s :%s",
184                                         theirserver->GetID().c_str(),     // Prefix: SID
185                                         u->second->uuid.c_str(),          // 0: UUID
186                                         (unsigned long)u->second->age,    // 1: TS
187                                         u->second->nick.c_str(),          // 2: Nick
188                                         u->second->host.c_str(),          // 3: Real host
189                                         u->second->dhost.c_str(),         // 4: Display host
190                                         u->second->ident.c_str(),         // 5: Ident
191                                         u->second->GetIPString().c_str(), // 6: IP address
192                                         (unsigned long)u->second->signon, // 7: Signon time
193                                         u->second->FormatModes(true),     // 8...n: User modes and params
194                                         u->second->fullname.c_str()));    // size-1: GECOS
195
196                                 if (u->second->IsOper())
197                                 {
198                                         this->WriteLine(InspIRCd::Format(":%s OPERTYPE :%s", u->second->uuid.c_str(), u->second->oper->name.c_str()));
199                                 }
200                                 if (u->second->IsAway())
201                                 {
202                                         this->WriteLine(InspIRCd::Format(":%s AWAY %ld :%s", u->second->uuid.c_str(), (long)u->second->awaytime,
203                                                 u->second->awaymsg.c_str()));
204                                 }
205                         }
206
207                         for(Extensible::ExtensibleStore::const_iterator i = u->second->GetExtList().begin(); i != u->second->GetExtList().end(); i++)
208                         {
209                                 ExtensionItem* item = i->first;
210                                 std::string value = item->serialize(FORMAT_NETWORK, u->second, i->second);
211                                 if (!value.empty())
212                                         Utils->Creator->ProtoSendMetaData(this, u->second, item->name, value);
213                         }
214
215                         FOREACH_MOD(OnSyncUser, (u->second,Utils->Creator,this));
216                 }
217         }
218 }
219