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