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