]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/netburst.cpp
Send SVSNICKs during nick collision to prevent servers that do not fully implement...
[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://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->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).", 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->ServerInstance->Config->GetSID()+" VERSION :"+this->ServerInstance->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->ServerInstance,I_OnSyncOtherMetaData,OnSyncOtherMetaData((Module*)Utils->Creator,(void*)this));
46         this->WriteLine(endburst);
47         this->ServerInstance->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 curlen, headlen;
88         curlen = headlen = snprintf(list,MAXBUF,":%s FJOIN %s %lu +%s :",
89                 this->ServerInstance->Config->GetSID().c_str(), c->name.c_str(), (unsigned long)c->age, c->ChanModes(true));
90         int numusers = 0;
91         char* ptr = list + curlen;
92         bool looped_once = false;
93
94         CUList *ulist = c->GetUsers();
95         std::string modes;
96         std::string params;
97
98         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
99         {
100                 size_t ptrlen = 0;
101                 std::string modestr = this->ServerInstance->Modes->ModeString(i->first, c, false);
102
103                 if ((curlen + modestr.length() + i->first->uuid.length() + 4) > 480)
104                 {
105                         // remove the final space
106                         if (ptr[-1] == ' ')
107                                 ptr[-1] = '\0';
108                         buffer.append(list).append("\r\n");
109                         curlen = headlen;
110                         ptr = list + headlen;
111                         numusers = 0;
112                 }
113
114                 ptrlen = snprintf(ptr, MAXBUF-curlen, "%s,%s ", modestr.c_str(), i->first->uuid.c_str());
115
116                 looped_once = true;
117
118                 curlen += ptrlen;
119                 ptr += ptrlen;
120
121                 numusers++;
122         }
123
124         // Okay, permanent channels will (of course) need this \r\n anyway, numusers check is if there
125         // actually were people in the channel (looped_once == true)
126         if (!looped_once || numusers > 0)
127         {
128                 // remove the final space
129                 if (ptr[-1] == ' ')
130                         ptr[-1] = '\0';
131                 buffer.append(list).append("\r\n");
132         }
133
134         int linesize = 1;
135         for (BanList::iterator b = c->bans.begin(); b != c->bans.end(); b++)
136         {
137                 int size = b->data.length() + 2;
138                 int currsize = linesize + size;
139                 if (currsize <= 350)
140                 {
141                         modes.append("b");
142                         params.append(" ").append(b->data);
143                         linesize += size;
144                 }
145                 if ((params.length() >= ServerInstance->Config->Limits.MaxModes) || (currsize > 350))
146                 {
147                         /* Wrap at MAXMODES */
148                         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");
149                         modes.clear();
150                         params.clear();
151                         linesize = 1;
152                 }
153         }
154
155         /* Only send these if there are any */
156         if (!modes.empty())
157                 buffer.append(":").append(this->ServerInstance->Config->GetSID()).append(" FMODE ").append(c->name).append(" ").append(ConvToStr(c->age)).append(" +").append(modes).append(params);
158
159         this->WriteLine(buffer);
160 }
161
162 /** Send G, Q, Z and E lines */
163 void TreeSocket::SendXLines(TreeServer* Current)
164 {
165         char data[MAXBUF];
166         std::string n = this->ServerInstance->Config->GetSID();
167         const char* sn = n.c_str();
168
169         std::vector<std::string> types = ServerInstance->XLines->GetAllTypes();
170         time_t current = ServerInstance->Time();
171
172         for (std::vector<std::string>::iterator it = types.begin(); it != types.end(); ++it)
173         {
174                 XLineLookup* lookup = ServerInstance->XLines->GetAll(*it);
175
176                 if (lookup)
177                 {
178                         for (LookupIter i = lookup->begin(); i != lookup->end(); ++i)
179                         {
180                                 /* Is it burstable? this is better than an explicit check for type 'K'.
181                                  * We break the loop as NONE of the items in this group are worth iterating.
182                                  */
183                                 if (!i->second->IsBurstable())
184                                         break;
185
186                                 /* If it's expired, don't bother to burst it
187                                  */
188                                 if (i->second->duration && current > i->second->expiry)
189                                         continue;
190
191                                 snprintf(data,MAXBUF,":%s ADDLINE %s %s %s %lu %lu :%s",sn, it->c_str(), i->second->Displayable(),
192                                                 i->second->source,
193                                                 (unsigned long)i->second->set_time,
194                                                 (unsigned long)i->second->duration,
195                                                 i->second->reason);
196                                 this->WriteLine(data);
197                         }
198                 }
199         }
200 }
201
202 /** Send channel modes and topics */
203 void TreeSocket::SendChannelModes(TreeServer* Current)
204 {
205         char data[MAXBUF];
206         std::deque<std::string> list;
207         std::string n = this->ServerInstance->Config->GetSID();
208         const char* sn = n.c_str();
209         for (chan_hash::iterator c = this->ServerInstance->chanlist->begin(); c != this->ServerInstance->chanlist->end(); c++)
210         {
211                 SendFJoins(Current, c->second);
212                 if (!c->second->topic.empty())
213                 {
214                         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());
215                         this->WriteLine(data);
216                 }
217                 FOREACH_MOD_I(this->ServerInstance,I_OnSyncChannel,OnSyncChannel(c->second,(Module*)Utils->Creator,(void*)this));
218                 list.clear();
219                 c->second->GetExtList(list);
220                 for (unsigned int j = 0; j < list.size(); j++)
221                 {
222                         FOREACH_MOD_I(this->ServerInstance,I_OnSyncChannelMetaData,OnSyncChannelMetaData(c->second,(Module*)Utils->Creator,(void*)this,list[j]));
223                 }
224         }
225 }
226
227 /** send all users and their oper state/modes */
228 void TreeSocket::SendUsers(TreeServer* Current)
229 {
230         char data[MAXBUF];
231         std::deque<std::string> list;
232         std::string dataline;
233         for (user_hash::iterator u = this->ServerInstance->Users->clientlist->begin(); u != this->ServerInstance->Users->clientlist->end(); u++)
234         {
235                 if (u->second->registered == REG_ALL)
236                 {
237                         TreeServer* theirserver = Utils->FindServer(u->second->server);
238                         if (theirserver)
239                         {
240                                 snprintf(data,MAXBUF,":%s UID %s %lu %s %s %s %s %s %lu +%s :%s",
241                                                 theirserver->GetID().c_str(),   /* Prefix: SID */
242                                                 u->second->uuid.c_str(),        /* 0: UUID */
243                                                 (unsigned long)u->second->age,  /* 1: TS */
244                                                 u->second->nick.c_str(),        /* 2: Nick */
245                                                 u->second->host.c_str(),        /* 3: Displayed Host */
246                                                 u->second->dhost.c_str(),       /* 4: Real host */
247                                                 u->second->ident.c_str(),       /* 5: Ident */
248                                                 u->second->GetIPString(),       /* 6: IP string */
249                                                 (unsigned long)u->second->signon, /* 7: Signon time for WHOWAS */
250                                                 u->second->FormatModes(true),   /* 8...n: Modes and params */
251                                                 u->second->fullname.c_str());   /* size-1: GECOS */
252                                 this->WriteLine(data);
253                                 if (IS_OPER(u->second))
254                                 {
255                                         snprintf(data,MAXBUF,":%s OPERTYPE %s", u->second->uuid.c_str(), u->second->oper.c_str());
256                                         this->WriteLine(data);
257                                 }
258                                 if (IS_AWAY(u->second))
259                                 {
260                                         snprintf(data,MAXBUF,":%s AWAY :%s", u->second->uuid.c_str(), u->second->awaymsg.c_str());
261                                         this->WriteLine(data);
262                                 }
263                         }
264
265                         FOREACH_MOD_I(this->ServerInstance,I_OnSyncUser,OnSyncUser(u->second,(Module*)Utils->Creator,(void*)this));
266                         list.clear();
267                         u->second->GetExtList(list);
268                         for (unsigned int j = 0; j < list.size(); j++)
269                         {
270                                 FOREACH_MOD_I(this->ServerInstance,I_OnSyncUserMetaData,OnSyncUserMetaData(u->second,(Module*)Utils->Creator,(void*)this,list[j]));
271                         }
272                 }
273         }
274 }
275