]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/netburst.cpp
e32f9f3ff8123c95180fdc34f440327d673b9a97
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / netburst.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/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 "commands/cmd_whois.h"
16 #include "commands/cmd_stats.h"
17 #include "socket.h"
18 #include "wildcard.h"
19 #include "xline.h"
20 #include "transport.h"
21 #include "m_hash.h"
22 #include "socketengine.h"
23
24 #include "m_spanningtree/main.h"
25 #include "m_spanningtree/utils.h"
26 #include "m_spanningtree/treeserver.h"
27 #include "m_spanningtree/link.h"
28 #include "m_spanningtree/treesocket.h"
29 #include "m_spanningtree/resolvers.h"
30 #include "m_spanningtree/handshaketimer.h"
31
32 /* $ModDep: m_spanningtree/timesynctimer.h m_spanningtree/resolvers.h m_spanningtree/main.h m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/link.h m_spanningtree/treesocket.h m_hash.h */
33
34 /** This function is called when we want to send a netburst to a local
35  * server. There is a set order we must do this, because for example
36  * users require their servers to exist, and channels require their
37  * users to exist. You get the idea.
38  */
39 void TreeSocket::DoBurst(TreeServer* s)
40 {
41         std::string name = s->GetName();
42         std::string burst = ":" + this->Instance->Config->GetSID() + " BURST " +ConvToStr(Instance->Time(true));
43         std::string endburst = ":" + this->Instance->Config->GetSID() + " ENDBURST";
44         this->Instance->SNO->WriteToSnoMask('l',"Bursting to \2%s\2 (Authentication: %s).", name.c_str(), this->GetTheirChallenge().empty() ? "plaintext password" : "SHA256-HMAC challenge-response");
45         this->WriteLine(burst);
46         /* send our version string */
47         this->WriteLine(std::string(":")+this->Instance->Config->GetSID()+" VERSION :"+this->Instance->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(this->Instance,I_OnSyncOtherMetaData,OnSyncOtherMetaData((Module*)Utils->Creator,(void*)this));
56         this->WriteLine(endburst);
57         this->Instance->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         std::string individual_halfops = std::string(":")+this->Instance->Config->GetSID()+" FMODE "+c->name+" "+ConvToStr(c->age);
97
98         size_t dlen, curlen;
99         dlen = curlen = snprintf(list,MAXBUF,":%s FJOIN %s %lu",this->Instance->Config->GetSID().c_str(),c->name,(unsigned long)c->age);
100         int numusers = 1;
101         char* ptr = list + dlen;
102
103         CUList *ulist = c->GetUsers();
104         std::string modes;
105         std::string params;
106
107         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
108         {
109                 // The first parameter gets a : before it
110                 size_t ptrlen = snprintf(ptr, MAXBUF, " %s%s,%s", !numusers ? ":" : "", c->GetAllPrefixChars(i->first), i->first->uuid);
111
112                 curlen += ptrlen;
113                 ptr += ptrlen;
114
115                 numusers++;
116
117                 if (curlen > (480-NICKMAX))
118                 {
119                         buffer.append(list).append("\r\n");
120                         dlen = curlen = snprintf(list,MAXBUF,":%s FJOIN %s %lu",this->Instance->Config->GetSID().c_str(),c->name,(unsigned long)c->age);
121                         ptr = list + dlen;
122                         ptrlen = 0;
123                         numusers = 0;
124                 }
125         }
126
127         if (numusers)
128                 buffer.append(list).append("\r\n");
129
130         buffer.append(":").append(this->Instance->Config->GetSID()).append(" FMODE ").append(c->name).append(" ").append(ConvToStr(c->age)).append(" +").append(c->ChanModes(true)).append("\r\n");
131
132         int linesize = 1;
133         for (BanList::iterator b = c->bans.begin(); b != c->bans.end(); b++)
134         {
135                 int size = strlen(b->data) + 2;
136                 int currsize = linesize + size;
137                 if (currsize <= 350)
138                 {
139                         modes.append("b");
140                         params.append(" ").append(b->data);
141                         linesize += size; 
142                 }
143                 if ((params.length() >= MAXMODES) || (currsize > 350))
144                 {
145                         /* Wrap at MAXMODES */
146                         buffer.append(":").append(this->Instance->Config->GetSID()).append(" FMODE ").append(c->name).append(" ").append(ConvToStr(c->age)).append(" +").append(modes).append(params).append("\r\n");
147                         modes.clear();
148                         params.clear();
149                         linesize = 1;
150                 }
151         }
152
153         /* Only send these if there are any */
154         if (!modes.empty())
155                 buffer.append(":").append(this->Instance->Config->GetSID()).append(" FMODE ").append(c->name).append(" ").append(ConvToStr(c->age)).append(" +").append(modes).append(params);
156
157         this->WriteLine(buffer);
158 }
159
160 /** Send G, Q, Z and E lines */
161 void TreeSocket::SendXLines(TreeServer* Current)
162 {
163         char data[MAXBUF];
164         std::string buffer;
165         std::string n = this->Instance->Config->GetSID();
166         const char* sn = n.c_str();
167
168         std::vector<std::string> types = Instance->XLines->GetAllTypes();
169
170         for (std::vector<std::string>::iterator it = types.begin(); it != types.end(); ++it)
171         {
172                 XLineLookup* lookup = Instance->XLines->GetAll(*it);
173
174                 if (lookup)
175                 {
176                         for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
177                         {
178                                 snprintf(data,MAXBUF,":%s ADDLINE %s %s %s %lu %lu :%s\r\n",sn, it->c_str(), i->second->Displayable(),
179                                                 i->second->source,
180                                                 (unsigned long)i->second->set_time,
181                                                 (unsigned long)i->second->duration,
182                                                 i->second->reason);
183                                 buffer.append(data);
184                         }
185                 }
186         }
187
188         if (!buffer.empty())
189                 this->WriteLine(buffer);
190 }
191
192 /** Send channel modes and topics */
193 void TreeSocket::SendChannelModes(TreeServer* Current)
194 {
195         char data[MAXBUF];
196         std::deque<std::string> list;
197         std::string n = this->Instance->Config->GetSID();
198         const char* sn = n.c_str();
199         Instance->Log(DEBUG,"Sending channels and modes, %d to send", this->Instance->chanlist->size());
200         for (chan_hash::iterator c = this->Instance->chanlist->begin(); c != this->Instance->chanlist->end(); c++)
201         {
202                 SendFJoins(Current, c->second);
203                 if (*c->second->topic)
204                 {
205                         snprintf(data,MAXBUF,":%s FTOPIC %s %lu %s :%s",sn,c->second->name,(unsigned long)c->second->topicset,c->second->setby,c->second->topic);
206                         this->WriteLine(data);
207                 }
208                 FOREACH_MOD_I(this->Instance,I_OnSyncChannel,OnSyncChannel(c->second,(Module*)Utils->Creator,(void*)this));
209                 list.clear();
210                 c->second->GetExtList(list);
211                 for (unsigned int j = 0; j < list.size(); j++)
212                 {
213                         FOREACH_MOD_I(this->Instance,I_OnSyncChannelMetaData,OnSyncChannelMetaData(c->second,(Module*)Utils->Creator,(void*)this,list[j]));
214                 }
215         }
216 }
217
218 /** send all users and their oper state/modes */
219 void TreeSocket::SendUsers(TreeServer* Current)
220 {
221         char data[MAXBUF];
222         std::deque<std::string> list;
223         std::string dataline;
224         for (user_hash::iterator u = this->Instance->Users->clientlist->begin(); u != this->Instance->Users->clientlist->end(); u++)
225         {
226                 if (u->second->registered == REG_ALL)
227                 {
228                         TreeServer* theirserver = Utils->FindServer(u->second->server);
229                         if (theirserver)
230                         {
231                                 snprintf(data,MAXBUF,":%s UID %s %lu %s %s %s %s +%s %s %lu :%s", theirserver->GetID().c_str(), u->second->uuid,
232                                                 (unsigned long)u->second->age, u->second->nick, u->second->host, u->second->dhost,
233                                                 u->second->ident, u->second->FormatModes(), u->second->GetIPString(),
234                                                 (unsigned long)u->second->signon, u->second->fullname);
235                                 this->WriteLine(data);
236                                 if (*u->second->oper)
237                                 {
238                                         snprintf(data,MAXBUF,":%s OPERTYPE %s", u->second->uuid, u->second->oper);
239                                         this->WriteLine(data);
240                                 }
241                                 if (*u->second->awaymsg)
242                                 {
243                                         snprintf(data,MAXBUF,":%s AWAY :%s", u->second->uuid, u->second->awaymsg);
244                                         this->WriteLine(data);
245                                 }
246                         }
247
248                         FOREACH_MOD_I(this->Instance,I_OnSyncUser,OnSyncUser(u->second,(Module*)Utils->Creator,(void*)this));
249                         list.clear();
250                         u->second->GetExtList(list);
251                         for (unsigned int j = 0; j < list.size(); j++)
252                         {
253                                 FOREACH_MOD_I(this->Instance,I_OnSyncUserMetaData,OnSyncUserMetaData(u->second,(Module*)Utils->Creator,(void*)this,list[j]));
254                         }
255                 }
256         }
257 }
258