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