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