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