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