Blob


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