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