]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/netburst.cpp
m_spanningtree Specify the uuid of the remote user when sending server NOTICE/PRIVMSG...
[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
25 #include "treesocket.h"
26 #include "treeserver.h"
27 #include "utils.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         std::string name = s->GetName();
38         std::string burst = ":" + ServerInstance->Config->GetSID() + " BURST " +ConvToStr(ServerInstance->Time());
39         std::string endburst = ":" + ServerInstance->Config->GetSID() + " ENDBURST";
40         ServerInstance->SNO->WriteToSnoMask('l',"Bursting to \2%s\2 (Authentication: %s%s).",
41                 name.c_str(),
42                 capab->auth_fingerprint ? "SSL Fingerprint and " : "",
43                 capab->auth_challenge ? "challenge-response" : "plaintext password");
44         this->CleanNegotiationInfo();
45         this->WriteLine(burst);
46         /* send our version string */
47         this->WriteLine(std::string(":")+ServerInstance->Config->GetSID()+" VERSION :"+ServerInstance->GetVersionString());
48         /* Send server tree */
49         this->SendServers(Utils->TreeRoot,s,1);
50         /* Send users and their oper status */
51         this->SendUsers(s);
52         /* Send everything else (channel modes, xlines etc) */
53         this->SendChannelModes(s);
54         this->SendXLines(s);
55         FOREACH_MOD(I_OnSyncNetwork,OnSyncNetwork(Utils->Creator,(void*)this));
56         this->WriteLine(endburst);
57         ServerInstance->SNO->WriteToSnoMask('l',"Finished bursting to \2"+name+"\2.");
58 }
59
60 /** Recursively send the server tree with distances as hops.
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. The hopcounts given
65  * by this function are relative, this doesn't matter so long as
66  * they are all >1, as all the remote servers re-calculate them
67  * to be relative too, with themselves as hop 0.
68  */
69 void TreeSocket::SendServers(TreeServer* Current, TreeServer* s, int hops)
70 {
71         char command[1024];
72         for (unsigned int q = 0; q < Current->ChildCount(); q++)
73         {
74                 TreeServer* recursive_server = Current->GetChild(q);
75                 if (recursive_server != s)
76                 {
77                         snprintf(command,1024,":%s SERVER %s * %d %s :%s",Current->GetName().c_str(),recursive_server->GetName().c_str(),hops,
78                                         recursive_server->GetID().c_str(),
79                                         recursive_server->GetDesc().c_str());
80                         this->WriteLine(command);
81                         this->WriteLine(":"+recursive_server->GetName()+" 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(TreeServer* Current, 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         int linesize = 1;
145         for (BanList::iterator b = c->bans.begin(); b != c->bans.end(); b++)
146         {
147                 int size = b->data.length() + 2;
148                 int currsize = linesize + size;
149                 if (currsize <= 350)
150                 {
151                         modes.append("b");
152                         params.append(" ").append(b->data);
153                         linesize += size;
154                 }
155                 if ((modes.length() >= ServerInstance->Config->Limits.MaxModes) || (currsize > 350))
156                 {
157                         /* Wrap at MAXMODES */
158                         buffer.append(":").append(ServerInstance->Config->GetSID()).append(" FMODE ").append(c->name).append(" ").append(ConvToStr(c->age)).append(" +").append(modes).append(params).append("\r\n");
159                         modes.clear();
160                         params.clear();
161                         linesize = 1;
162                 }
163         }
164
165         /* Only send these if there are any */
166         if (!modes.empty())
167                 buffer.append(":").append(ServerInstance->Config->GetSID()).append(" FMODE ").append(c->name).append(" ").append(ConvToStr(c->age)).append(" +").append(modes).append(params);
168
169         this->WriteLine(buffer);
170 }
171
172 /** Send G, Q, Z and E lines */
173 void TreeSocket::SendXLines(TreeServer* Current)
174 {
175         char data[MAXBUF];
176         std::string n = ServerInstance->Config->GetSID();
177         const char* sn = n.c_str();
178
179         std::vector<std::string> types = ServerInstance->XLines->GetAllTypes();
180         time_t current = ServerInstance->Time();
181
182         for (std::vector<std::string>::iterator it = types.begin(); it != types.end(); ++it)
183         {
184                 XLineLookup* lookup = ServerInstance->XLines->GetAll(*it);
185
186                 if (lookup)
187                 {
188                         for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
189                         {
190                                 /* Is it burstable? this is better than an explicit check for type 'K'.
191                                  * We break the loop as NONE of the items in this group are worth iterating.
192                                  */
193                                 if (!i->second->IsBurstable())
194                                         break;
195
196                                 /* If it's expired, don't bother to burst it
197                                  */
198                                 if (i->second->duration && current > i->second->expiry)
199                                         continue;
200
201                                 snprintf(data,MAXBUF,":%s ADDLINE %s %s %s %lu %lu :%s",sn, it->c_str(), i->second->Displayable(),
202                                                 i->second->source.c_str(),
203                                                 (unsigned long)i->second->set_time,
204                                                 (unsigned long)i->second->duration,
205                                                 i->second->reason.c_str());
206                                 this->WriteLine(data);
207                         }
208                 }
209         }
210 }
211
212 /** Send channel modes and topics */
213 void TreeSocket::SendChannelModes(TreeServer* Current)
214 {
215         char data[MAXBUF];
216         std::deque<std::string> list;
217         std::string n = ServerInstance->Config->GetSID();
218         const char* sn = n.c_str();
219         for (chan_hash::iterator c = ServerInstance->chanlist->begin(); c != ServerInstance->chanlist->end(); c++)
220         {
221                 SendFJoins(Current, c->second);
222                 if (!c->second->topic.empty())
223                 {
224                         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());
225                         this->WriteLine(data);
226                 }
227
228                 for(Extensible::ExtensibleStore::const_iterator i = c->second->GetExtList().begin(); i != c->second->GetExtList().end(); i++)
229                 {
230                         ExtensionItem* item = i->first;
231                         std::string value = item->serialize(FORMAT_NETWORK, c->second, i->second);
232                         if (!value.empty())
233                                 Utils->Creator->ProtoSendMetaData(this, c->second, item->name, value);
234                 }
235
236                 FOREACH_MOD(I_OnSyncChannel,OnSyncChannel(c->second,Utils->Creator,this));
237         }
238 }
239
240 /** send all users and their oper state/modes */
241 void TreeSocket::SendUsers(TreeServer* Current)
242 {
243         char data[MAXBUF];
244         std::string dataline;
245         for (user_hash::iterator u = ServerInstance->Users->clientlist->begin(); u != ServerInstance->Users->clientlist->end(); u++)
246         {
247                 if (u->second->registered == REG_ALL)
248                 {
249                         TreeServer* theirserver = Utils->FindServer(u->second->server);
250                         if (theirserver)
251                         {
252                                 snprintf(data,MAXBUF,":%s UID %s %lu %s %s %s %s %s %lu +%s :%s",
253                                                 theirserver->GetID().c_str(),   /* Prefix: SID */
254                                                 u->second->uuid.c_str(),        /* 0: UUID */
255                                                 (unsigned long)u->second->age,  /* 1: TS */
256                                                 u->second->nick.c_str(),        /* 2: Nick */
257                                                 u->second->host.c_str(),        /* 3: Displayed Host */
258                                                 u->second->dhost.c_str(),       /* 4: Real host */
259                                                 u->second->ident.c_str(),       /* 5: Ident */
260                                                 u->second->GetIPString(),       /* 6: IP string */
261                                                 (unsigned long)u->second->signon, /* 7: Signon time for WHOWAS */
262                                                 u->second->FormatModes(true),   /* 8...n: Modes and params */
263                                                 u->second->fullname.c_str());   /* size-1: GECOS */
264                                 this->WriteLine(data);
265                                 if (IS_OPER(u->second))
266                                 {
267                                         snprintf(data,MAXBUF,":%s OPERTYPE %s", u->second->uuid.c_str(), u->second->oper->name.c_str());
268                                         this->WriteLine(data);
269                                 }
270                                 if (IS_AWAY(u->second))
271                                 {
272                                         snprintf(data,MAXBUF,":%s AWAY %ld :%s", u->second->uuid.c_str(), (long)u->second->awaytime, u->second->awaymsg.c_str());
273                                         this->WriteLine(data);
274                                 }
275                         }
276
277                         for(Extensible::ExtensibleStore::const_iterator i = u->second->GetExtList().begin(); i != u->second->GetExtList().end(); i++)
278                         {
279                                 ExtensionItem* item = i->first;
280                                 std::string value = item->serialize(FORMAT_NETWORK, u->second, i->second);
281                                 if (!value.empty())
282                                         Utils->Creator->ProtoSendMetaData(this, u->second, item->name, value);
283                         }
284
285                         FOREACH_MOD(I_OnSyncUser,OnSyncUser(u->second,Utils->Creator,this));
286                 }
287         }
288 }
289