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