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