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