]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/numeric.c
Server links: detect RFC 1459 mode direct after SERVER command
[ngircd-alex.git] / src / ngircd / numeric.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2008 Alexander Barton (alex@barton.de)
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * Please read the file COPYING, README and AUTHORS for more information.
10  *
11  * Handlers for IRC numerics sent to the server
12  */
13
14 #include "portab.h"
15
16 #include "imp.h"
17 #include <assert.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21
22 #include "defines.h"
23 #include "resolve.h"
24 #include "conn.h"
25 #include "conf.h"
26 #include "conn.h"
27 #include "conn-func.h"
28 #include "client.h"
29 #include "channel.h"
30 #include "irc-write.h"
31 #include "lists.h"
32 #include "log.h"
33 #include "messages.h"
34 #include "parse.h"
35
36 #include "exp.h"
37 #include "numeric.h"
38
39
40 /**
41  * Announce new server in the network
42  * @param Client New server
43  * @param Server Existing server in the network
44  */
45 static bool
46 Announce_Server(CLIENT * Client, CLIENT * Server)
47 {
48         CLIENT *c;
49
50         if (Client_Conn(Server) > NONE) {
51                 /* Announce the new server to the one already registered
52                  * which is directly connected to the local server */
53                 if (!IRC_WriteStrClient
54                     (Server, "SERVER %s %d %d :%s", Client_ID(Client),
55                      Client_Hops(Client) + 1, Client_MyToken(Client),
56                      Client_Info(Client)))
57                         return DISCONNECTED;
58         }
59
60         if (Client_Hops(Server) == 1)
61                 c = Client_ThisServer();
62         else
63                 c = Client_Introducer(Server);
64
65         /* Inform new server about the one already registered in the network */
66         return IRC_WriteStrClientPrefix(Client, c, "SERVER %s %d %d :%s",
67                 Client_ID(Server), Client_Hops(Server) + 1,
68                 Client_MyToken(Server), Client_Info(Server));
69 } /* Announce_Server */
70
71
72 /**
73  * Announce existing user to a new server
74  * @param Client New server
75  * @param User Existing user in the network
76  */
77 static bool
78 Announce_User(CLIENT * Client, CLIENT * User)
79 {
80         CONN_ID conn;
81         conn = Client_Conn(Client);
82         if (Conn_Options(conn) & CONN_RFC1459) {
83                 /* RFC 1459 mode: separate NICK and USER commands */
84                 if (! Conn_WriteStr(conn, "NICK %s :%d",
85                                     Client_ID(User), Client_Hops(User) + 1))
86                         return DISCONNECTED;
87                 return Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
88                                      Client_ID(User), Client_User(User),
89                                      Client_Hostname(User),
90                                      Client_ID(Client_Introducer(User)),
91                                      Client_Info(User));
92                 
93         } else {
94                 /* RFC 2813 mode: one combined NICK command */
95                 return IRC_WriteStrClient(Client, "NICK %s %d %s %s %d +%s :%s",
96                                 Client_ID(User), Client_Hops(User) + 1,
97                                 Client_User(User), Client_Hostname(User),
98                                 Client_MyToken(Client_Introducer(User)),
99                                 Client_Modes(User), Client_Info(User));
100         }
101 } /* Announce_User */
102
103
104 #ifdef IRCPLUS
105
106 /**
107  * Synchronize invite and ban lists between servers
108  * @param Client New server
109  */
110 static bool
111 Synchronize_Lists(CLIENT * Client)
112 {
113         CHANNEL *c;
114         struct list_head *head;
115         struct list_elem *elem;
116
117         assert(Client != NULL);
118
119         c = Channel_First();
120         while (c) {
121                 /* ban list */
122                 head = Channel_GetListBans(c);
123                 elem = Lists_GetFirst(head);
124                 while (elem) {
125                         if (!IRC_WriteStrClient(Client, "MODE %s +b %s",
126                                                 Channel_Name(c),
127                                                 Lists_GetMask(elem))) {
128                                 return DISCONNECTED;
129                         }
130                         elem = Lists_GetNext(elem);
131                 }
132
133                 /* invite list */
134                 head = Channel_GetListInvites(c);
135                 elem = Lists_GetFirst(head);
136                 while (elem) {
137                         if (!IRC_WriteStrClient(Client, "MODE %s +I %s",
138                                                 Channel_Name(c),
139                                                 Lists_GetMask(elem))) {
140                                 return DISCONNECTED;
141                         }
142                         elem = Lists_GetNext(elem);
143                 }
144
145                 c = Channel_Next(c);
146         }
147         return CONNECTED;
148 }
149
150
151 /**
152  * Send CHANINFO commands to a new server (inform it about existing channels).
153  * @param Client New server
154  * @param Chan Channel
155  */
156 static bool
157 Send_CHANINFO(CLIENT * Client, CHANNEL * Chan)
158 {
159         char *modes, *topic;
160         bool has_k, has_l;
161         
162 #ifdef DEBUG
163         Log(LOG_DEBUG, "Sending CHANINFO commands ...");
164 #endif
165         
166         modes = Channel_Modes(Chan);
167         topic = Channel_Topic(Chan);
168         
169         if (!*modes && !*topic)
170                 return CONNECTED;
171         
172         has_k = strchr(modes, 'k') != NULL;
173         has_l = strchr(modes, 'l') != NULL;
174         
175         /* send CHANINFO */
176         if (!has_k && !has_l) {
177                 if (!*topic) {
178                         /* "CHANINFO <chan> +<modes>" */
179                         return IRC_WriteStrClient(Client, "CHANINFO %s +%s",
180                                                   Channel_Name(Chan), modes);
181                 }
182                 /* "CHANINFO <chan> +<modes> :<topic>" */
183                 return IRC_WriteStrClient(Client, "CHANINFO %s +%s :%s",
184                                           Channel_Name(Chan), modes, topic);
185         }
186         /* "CHANINFO <chan> +<modes> <key> <limit> :<topic>" */
187         return IRC_WriteStrClient(Client, "CHANINFO %s +%s %s %lu :%s",
188                                   Channel_Name(Chan), modes,
189                                   has_k ? Channel_Key(Chan) : "*",
190                                   has_l ? Channel_MaxUsers(Chan) : 0, topic);
191 } /* Send_CHANINFO */
192
193 #endif /* IRCPLUS */
194
195
196 /**
197  * Handle ENDOFMOTD (376) numeric and login remote server.
198  * The peer is either an IRC server (no IRC+ protocol), or we got the
199  * ENDOFMOTD numeric from an IRC+ server. We have to register the new server.
200  */
201 GLOBAL bool
202 IRC_Num_ENDOFMOTD(CLIENT * Client, UNUSED REQUEST * Req)
203 {
204         char str[LINE_LEN];
205         int max_hops, i;
206         CLIENT *c, *cl;
207         CHANNEL *chan;
208         CL2CHAN *cl2chan;
209
210         Client_SetType(Client, CLIENT_SERVER);
211
212         Log(LOG_NOTICE | LOG_snotice,
213             "Server \"%s\" registered (connection %d, 1 hop - direct link).",
214             Client_ID(Client), Client_Conn(Client));
215
216         /* Get highest hop count */
217         max_hops = 0;
218         c = Client_First();
219         while (c) {
220                 if (Client_Hops(c) > max_hops)
221                         max_hops = Client_Hops(c);
222                 c = Client_Next(c);
223         }
224
225         /* Inform the new server about all other servers, and announce the
226          * new server to all the already registered ones. Important: we have
227          * to do this "in order" and can't introduce servers of which the
228          * "toplevel server" isn't known already. */
229         for (i = 0; i < (max_hops + 1); i++) {
230                 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
231                         if (Client_Type(c) != CLIENT_SERVER)
232                                 continue;       /* not a server */
233                         if (Client_Hops(c) != i)
234                                 continue;       /* not actual "nesting level" */
235                         if (c == Client || c == Client_ThisServer())
236                                 continue;       /* that's us or the peer! */
237
238                         if (!Announce_Server(Client, c))
239                                 return DISCONNECTED;
240                 }
241         }
242
243         /* Announce all the users to the new server */
244         c = Client_First();
245         while (c) {
246                 if (Client_Type(c) == CLIENT_USER) {
247                         if (!Announce_User(Client, c))
248                                 return DISCONNECTED;
249                 }
250                 c = Client_Next(c);
251         }
252
253         /* Announce all channels to the new server */
254         chan = Channel_First();
255         while (chan) {
256 #ifdef IRCPLUS
257                 /* Send CHANINFO if the peer supports it */
258                 if (strchr(Client_Flags(Client), 'C')) {
259                         if (!Send_CHANINFO(Client, chan))
260                                 return DISCONNECTED;
261                 }
262 #endif
263
264                 /* Get all the members of this channel */
265                 cl2chan = Channel_FirstMember(chan);
266                 snprintf(str, sizeof(str), "NJOIN %s :", Channel_Name(chan));
267                 while (cl2chan) {
268                         cl = Channel_GetClient(cl2chan);
269                         assert(cl != NULL);
270
271                         /* Nick name, with modes (if applicable) */
272                         if (str[strlen(str) - 1] != ':')
273                                 strlcat(str, ",", sizeof(str));
274                         if (strchr(Channel_UserModes(chan, cl), 'v'))
275                                 strlcat(str, "+", sizeof(str));
276                         if (strchr(Channel_UserModes(chan, cl), 'o'))
277                                 strlcat(str, "@", sizeof(str));
278                         strlcat(str, Client_ID(cl), sizeof(str));
279
280                         /* Send the data if the buffer is "full" */
281                         if (strlen(str) > (LINE_LEN - CLIENT_NICK_LEN - 8)) {
282                                 if (!IRC_WriteStrClient(Client, "%s", str))
283                                         return DISCONNECTED;
284                                 snprintf(str, sizeof(str), "NJOIN %s :",
285                                          Channel_Name(chan));
286                         }
287
288                         cl2chan = Channel_NextMember(chan, cl2chan);
289                 }
290
291                 /* Data left in the buffer? */
292                 if (str[strlen(str) - 1] != ':') {
293                         /* Yes, send it ... */
294                         if (!IRC_WriteStrClient(Client, "%s", str))
295                                 return DISCONNECTED;
296                 }
297
298                 /* Get next channel ... */
299                 chan = Channel_Next(chan);
300         }
301
302 #ifdef IRCPLUS
303         if (strchr(Client_Flags(Client), 'L')) {
304                 LogDebug("Synchronizing INVITE- and BAN-lists ...");
305                 if (!Synchronize_Lists(Client))
306                         return DISCONNECTED;
307         }
308 #endif
309
310         return CONNECTED;
311 } /* IRC_Num_ENDOFMOTD */
312
313
314 /**
315  * Handle ISUPPORT (005) numeric.
316  */
317 GLOBAL bool
318 IRC_Num_ISUPPORT(CLIENT * Client, REQUEST * Req)
319 {
320         int i;
321         char *key, *value;
322
323         for (i = 1; i < Req->argc - 1; i++) {
324                 key = Req->argv[i];
325                 value = strchr(key, '=');
326                 if (value)
327                         *value++ = '\0';
328                 else
329                         value = "";
330
331                 if (strcmp("NICKLEN", key) == 0) {
332                         if ((unsigned int)atol(value) == Conf_MaxNickLength - 1)
333                                 continue;
334
335                         /* Nick name length settings are different! */
336                         Log(LOG_ERR,
337                             "Peer uses incompatible nick name length (%d/%d)! Disconnecting ...",
338                             Conf_MaxNickLength - 1, atoi(value));
339                         Conn_Close(Client_Conn(Client),
340                                    "Incompatible nick name length",
341                                    NULL, false);
342                         return DISCONNECTED;
343                 }
344         }
345
346         return CONNECTED;
347 } /* IRC_Num_ISUPPORT */
348
349
350 /* -eof- */