]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/netburst.cpp
First phase of conversion to dynamic limits on all the lengths, configured via the...
[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
87         size_t dlen, curlen;
88         dlen = curlen = snprintf(list,MAXBUF,":%s FJOIN %s %lu +%s", this->Instance->Config->GetSID().c_str(), c->name.c_str(),(unsigned long)c->age, c->ChanModes(true));
89         int numusers = 0;
90         char* ptr = list + dlen;
91         bool looped_once = false;
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                 size_t ptrlen = 0;
100                 std::string modestr = this->Instance->Modes->ModeString(i->first, c, false);
101
102                 if ((curlen + modestr.length() + i->first->uuid.length() + 4) > 480)
103                 {
104                         buffer.append(list).append("\r\n");
105                         dlen = curlen = snprintf(list, MAXBUF, ":%s FJOIN %s %lu +%s", this->Instance->Config->GetSID().c_str(), c->name.c_str(), (unsigned long)c->age, c->ChanModes(true));
106                         ptr = list + dlen;
107                         numusers = 0;
108                 }
109
110                 // The first parameter gets a : before it
111                 ptrlen = snprintf(ptr, MAXBUF, " %s%s,%s", !numusers ? ":" : "", modestr.c_str(), i->first->uuid.c_str());
112
113                 looped_once = true;
114
115                 curlen += ptrlen;
116                 ptr += ptrlen;
117
118                 numusers++;
119         }
120
121         // Okay, permanent channels will (of course) need this \r\n anyway, numusers check is if there
122         // actually were people in the channel (looped_once == true)
123         if (!looped_once || numusers > 0)
124                 buffer.append(list).append("\r\n");
125
126         int linesize = 1;
127         for (BanList::iterator b = c->bans.begin(); b != c->bans.end(); b++)
128         {
129                 int size = b->data.length() + 2;
130                 int currsize = linesize + size;
131                 if (currsize <= 350)
132                 {
133                         modes.append("b");
134                         params.append(" ").append(b->data);
135                         linesize += size; 
136                 }
137                 if ((params.length() >= Instance->Config->Limits.MaxModes) || (currsize > 350))
138                 {
139                         /* Wrap at MAXMODES */
140                         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");
141                         modes.clear();
142                         params.clear();
143                         linesize = 1;
144                 }
145         }
146
147         /* Only send these if there are any */
148         if (!modes.empty())
149                 buffer.append(":").append(this->Instance->Config->GetSID()).append(" FMODE ").append(c->name).append(" ").append(ConvToStr(c->age)).append(" +").append(modes).append(params);
150
151         this->WriteLine(buffer);
152 }
153
154 /** Send G, Q, Z and E lines */
155 void TreeSocket::SendXLines(TreeServer* Current)
156 {
157         char data[MAXBUF];
158         std::string n = this->Instance->Config->GetSID();
159         const char* sn = n.c_str();
160
161         std::vector<std::string> types = Instance->XLines->GetAllTypes();
162
163         for (std::vector<std::string>::iterator it = types.begin(); it != types.end(); ++it)
164         {
165                 XLineLookup* lookup = Instance->XLines->GetAll(*it);
166
167                 if (lookup)
168                 {
169                         for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
170                         {
171                                 /* Is it burstable? this is better than an explicit check for type 'K'.
172                                  * We break the loop as NONE of the items in this group are worth iterating.
173                                  */
174                                 if (!i->second->IsBurstable())
175                                         break;
176
177                                 snprintf(data,MAXBUF,":%s ADDLINE %s %s %s %lu %lu :%s",sn, it->c_str(), i->second->Displayable(),
178                                                 i->second->source,
179                                                 (unsigned long)i->second->set_time,
180                                                 (unsigned long)i->second->duration,
181                                                 i->second->reason);
182                                 this->WriteLine(data);
183                         }
184                 }
185         }
186 }
187
188 /** Send channel modes and topics */
189 void TreeSocket::SendChannelModes(TreeServer* Current)
190 {
191         char data[MAXBUF];
192         std::deque<std::string> list;
193         std::string n = this->Instance->Config->GetSID();
194         const char* sn = n.c_str();
195         for (chan_hash::iterator c = this->Instance->chanlist->begin(); c != this->Instance->chanlist->end(); c++)
196         {
197                 SendFJoins(Current, c->second);
198                 if (!c->second->topic.empty())
199                 {
200                         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());
201                         this->WriteLine(data);
202                 }
203                 FOREACH_MOD_I(this->Instance,I_OnSyncChannel,OnSyncChannel(c->second,(Module*)Utils->Creator,(void*)this));
204                 list.clear();
205                 c->second->GetExtList(list);
206                 for (unsigned int j = 0; j < list.size(); j++)
207                 {
208                         FOREACH_MOD_I(this->Instance,I_OnSyncChannelMetaData,OnSyncChannelMetaData(c->second,(Module*)Utils->Creator,(void*)this,list[j]));
209                 }
210         }
211 }
212
213 /** send all users and their oper state/modes */
214 void TreeSocket::SendUsers(TreeServer* Current)
215 {
216         char data[MAXBUF];
217         std::deque<std::string> list;
218         std::string dataline;
219         for (user_hash::iterator u = this->Instance->Users->clientlist->begin(); u != this->Instance->Users->clientlist->end(); u++)
220         {
221                 if (u->second->registered == REG_ALL)
222                 {
223                         TreeServer* theirserver = Utils->FindServer(u->second->server);
224                         if (theirserver)
225                         {
226                                 snprintf(data,MAXBUF,":%s UID %s %lu %s %s %s %s +%s %s %lu :%s", theirserver->GetID().c_str(), u->second->uuid.c_str(),
227                                                 (unsigned long)u->second->age, u->second->nick.c_str(), u->second->host.c_str(), u->second->dhost.c_str(),
228                                                 u->second->ident.c_str(), u->second->FormatModes(), u->second->GetIPString(),
229                                                 (unsigned long)u->second->signon, u->second->fullname.c_str());
230                                 this->WriteLine(data);
231                                 if (IS_OPER(u->second))
232                                 {
233                                         snprintf(data,MAXBUF,":%s OPERTYPE %s", u->second->uuid.c_str(), u->second->oper.c_str());
234                                         this->WriteLine(data);
235                                 }
236                                 if (IS_AWAY(u->second))
237                                 {
238                                         snprintf(data,MAXBUF,":%s AWAY :%s", u->second->uuid.c_str(), u->second->awaymsg.c_str());
239                                         this->WriteLine(data);
240                                 }
241                         }
242
243                         FOREACH_MOD_I(this->Instance,I_OnSyncUser,OnSyncUser(u->second,(Module*)Utils->Creator,(void*)this));
244                         list.clear();
245                         u->second->GetExtList(list);
246                         for (unsigned int j = 0; j < list.size(); j++)
247                         {
248                                 FOREACH_MOD_I(this->Instance,I_OnSyncUserMetaData,OnSyncUserMetaData(u->second,(Module*)Utils->Creator,(void*)this,list[j]));
249                         }
250                 }
251         }
252 }
253