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