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