]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/netburst.cpp
d1fcc928b34758f886c8c6dea23f222c9d24df8d
[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 "xline.h"
16
17 #include "m_spanningtree/treesocket.h"
18 #include "m_spanningtree/treeserver.h"
19 #include "m_spanningtree/utils.h"
20
21 /* $ModDep: m_spanningtree/utils.h m_spanningtree/treeserver.h m_spanningtree/treesocket.h */
22
23
24 /** This function is called when we want to send a netburst to a local
25  * server. There is a set order we must do this, because for example
26  * users require their servers to exist, and channels require their
27  * users to exist. You get the idea.
28  */
29 void TreeSocket::DoBurst(TreeServer* s)
30 {
31         std::string name = s->GetName();
32         std::string burst = ":" + this->Instance->Config->GetSID() + " BURST " +ConvToStr(Instance->Time());
33         std::string endburst = ":" + this->Instance->Config->GetSID() + " ENDBURST";
34         this->Instance->SNO->WriteToSnoMask('l',"Bursting to \2%s\2 (Authentication: %s).", name.c_str(), this->GetTheirChallenge().empty() ? "plaintext password" : "SHA256-HMAC challenge-response");
35         this->WriteLine(burst);
36         /* send our version string */
37         this->WriteLine(std::string(":")+this->Instance->Config->GetSID()+" VERSION :"+this->Instance->GetVersionString());
38         /* Send server tree */
39         this->SendServers(Utils->TreeRoot,s,1);
40         /* Send users and their oper status */
41         this->SendUsers(s);
42         /* Send everything else (channel modes, xlines etc) */
43         this->SendChannelModes(s);
44         this->SendXLines(s);
45         FOREACH_MOD_I(this->Instance,I_OnSyncOtherMetaData,OnSyncOtherMetaData((Module*)Utils->Creator,(void*)this));
46         this->WriteLine(endburst);
47         this->Instance->SNO->WriteToSnoMask('l',"Finished bursting to \2"+name+"\2.");
48 }
49
50 /** Recursively send the server tree with distances as hops.
51  * This is used during network burst to inform the other server
52  * (and any of ITS servers too) of what servers we know about.
53  * If at any point any of these servers already exist on the other
54  * end, our connection may be terminated. The hopcounts given
55  * by this function are relative, this doesn't matter so long as
56  * they are all >1, as all the remote servers re-calculate them
57  * to be relative too, with themselves as hop 0.
58  */
59 void TreeSocket::SendServers(TreeServer* Current, TreeServer* s, int hops)
60 {
61         char command[1024];
62         for (unsigned int q = 0; q < Current->ChildCount(); q++)
63         {
64                 TreeServer* recursive_server = Current->GetChild(q);
65                 if (recursive_server != s)
66                 {
67                         snprintf(command,1024,":%s SERVER %s * %d %s :%s",Current->GetName().c_str(),recursive_server->GetName().c_str(),hops,
68                                         recursive_server->GetID().c_str(),
69                                         recursive_server->GetDesc().c_str());
70                         this->WriteLine(command);
71                         this->WriteLine(":"+recursive_server->GetName()+" VERSION :"+recursive_server->GetVersion());
72                         /* down to next level */
73                         this->SendServers(recursive_server, s, hops+1);
74                 }
75         }
76 }
77
78 /** Send one or more FJOINs for a channel of users.
79  * If the length of a single line is more than 480-NICKMAX
80  * in length, it is split over multiple lines.
81  */
82 void TreeSocket::SendFJoins(TreeServer* Current, Channel* c)
83 {
84         std::string buffer;
85         char list[MAXBUF];
86         std::string individual_halfops = std::string(":")+this->Instance->Config->GetSID()+" FMODE "+c->name+" "+ConvToStr(c->age);
87
88         size_t dlen, curlen;
89         dlen = curlen = snprintf(list,MAXBUF,":%s FJOIN %s %lu",this->Instance->Config->GetSID().c_str(),c->name,(unsigned long)c->age);
90         int numusers = 1;
91         char* ptr = list + dlen;
92
93         CUList *ulist = c->GetUsers();
94         std::string modes;
95         std::string params;
96
97         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
98         {
99                 // The first parameter gets a : before it
100                 size_t ptrlen = snprintf(ptr, MAXBUF, " %s%s,%s", !numusers ? ":" : "", c->GetAllPrefixChars(i->first), i->first->uuid);
101
102                 curlen += ptrlen;
103                 ptr += ptrlen;
104
105                 numusers++;
106
107                 if (curlen > (480-NICKMAX))
108                 {
109                         buffer.append(list).append("\r\n");
110                         dlen = curlen = snprintf(list,MAXBUF,":%s FJOIN %s %lu",this->Instance->Config->GetSID().c_str(),c->name,(unsigned long)c->age);
111                         ptr = list + dlen;
112                         ptrlen = 0;
113                         numusers = 0;
114                 }
115         }
116
117         if (numusers)
118                 buffer.append(list).append("\r\n");
119
120         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");
121
122         int linesize = 1;
123         for (BanList::iterator b = c->bans.begin(); b != c->bans.end(); b++)
124         {
125                 int size = strlen(b->data) + 2;
126                 int currsize = linesize + size;
127                 if (currsize <= 350)
128                 {
129                         modes.append("b");
130                         params.append(" ").append(b->data);
131                         linesize += size; 
132                 }
133                 if ((params.length() >= MAXMODES) || (currsize > 350))
134                 {
135                         /* Wrap at MAXMODES */
136                         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");
137                         modes.clear();
138                         params.clear();
139                         linesize = 1;
140                 }
141         }
142
143         /* Only send these if there are any */
144         if (!modes.empty())
145                 buffer.append(":").append(this->Instance->Config->GetSID()).append(" FMODE ").append(c->name).append(" ").append(ConvToStr(c->age)).append(" +").append(modes).append(params);
146
147         this->WriteLine(buffer);
148 }
149
150 /** Send G, Q, Z and E lines */
151 void TreeSocket::SendXLines(TreeServer* Current)
152 {
153         char data[MAXBUF];
154         std::string n = this->Instance->Config->GetSID();
155         const char* sn = n.c_str();
156
157         std::vector<std::string> types = Instance->XLines->GetAllTypes();
158
159         for (std::vector<std::string>::iterator it = types.begin(); it != types.end(); ++it)
160         {
161                 XLineLookup* lookup = Instance->XLines->GetAll(*it);
162
163                 if (lookup)
164                 {
165                         for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
166                         {
167                                 /* Is it burstable? this is better than an explicit check for type 'K'.
168                                  * We break the loop as NONE of the items in this group are worth iterating.
169                                  */
170                                 if (!i->second->IsBurstable())
171                                         break;
172
173                                 snprintf(data,MAXBUF,":%s ADDLINE %s %s %s %lu %lu :%s",sn, it->c_str(), i->second->Displayable(),
174                                                 i->second->source,
175                                                 (unsigned long)i->second->set_time,
176                                                 (unsigned long)i->second->duration,
177                                                 i->second->reason);
178                                 this->WriteLine(data);
179                         }
180                 }
181         }
182 }
183
184 /** Send channel modes and topics */
185 void TreeSocket::SendChannelModes(TreeServer* Current)
186 {
187         char data[MAXBUF];
188         std::deque<std::string> list;
189         std::string n = this->Instance->Config->GetSID();
190         const char* sn = n.c_str();
191         for (chan_hash::iterator c = this->Instance->chanlist->begin(); c != this->Instance->chanlist->end(); c++)
192         {
193                 SendFJoins(Current, c->second);
194                 if (*c->second->topic)
195                 {
196                         snprintf(data,MAXBUF,":%s FTOPIC %s %lu %s :%s",sn,c->second->name,(unsigned long)c->second->topicset,c->second->setby,c->second->topic);
197                         this->WriteLine(data);
198                 }
199                 FOREACH_MOD_I(this->Instance,I_OnSyncChannel,OnSyncChannel(c->second,(Module*)Utils->Creator,(void*)this));
200                 list.clear();
201                 c->second->GetExtList(list);
202                 for (unsigned int j = 0; j < list.size(); j++)
203                 {
204                         FOREACH_MOD_I(this->Instance,I_OnSyncChannelMetaData,OnSyncChannelMetaData(c->second,(Module*)Utils->Creator,(void*)this,list[j]));
205                 }
206         }
207 }
208
209 /** send all users and their oper state/modes */
210 void TreeSocket::SendUsers(TreeServer* Current)
211 {
212         char data[MAXBUF];
213         std::deque<std::string> list;
214         std::string dataline;
215         for (user_hash::iterator u = this->Instance->Users->clientlist->begin(); u != this->Instance->Users->clientlist->end(); u++)
216         {
217                 if (u->second->registered == REG_ALL)
218                 {
219                         TreeServer* theirserver = Utils->FindServer(u->second->server);
220                         if (theirserver)
221                         {
222                                 snprintf(data,MAXBUF,":%s UID %s %lu %s %s %s %s +%s %s %lu :%s", theirserver->GetID().c_str(), u->second->uuid,
223                                                 (unsigned long)u->second->age, u->second->nick, u->second->host, u->second->dhost,
224                                                 u->second->ident, u->second->FormatModes(), u->second->GetIPString(),
225                                                 (unsigned long)u->second->signon, u->second->fullname);
226                                 this->WriteLine(data);
227                                 if (IS_OPER(u->second))
228                                 {
229                                         snprintf(data,MAXBUF,":%s OPERTYPE %s", u->second->uuid, u->second->oper);
230                                         this->WriteLine(data);
231                                 }
232                                 if (IS_AWAY(u->second))
233                                 {
234                                         snprintf(data,MAXBUF,":%s AWAY :%s", u->second->uuid, u->second->awaymsg);
235                                         this->WriteLine(data);
236                                 }
237                         }
238
239                         FOREACH_MOD_I(this->Instance,I_OnSyncUser,OnSyncUser(u->second,(Module*)Utils->Creator,(void*)this));
240                         list.clear();
241                         u->second->GetExtList(list);
242                         for (unsigned int j = 0; j < list.size(); j++)
243                         {
244                                 FOREACH_MOD_I(this->Instance,I_OnSyncUserMetaData,OnSyncUserMetaData(u->second,(Module*)Utils->Creator,(void*)this,list[j]));
245                         }
246                 }
247         }
248 }
249