2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2008 Alexander Barton (alex@barton.de)
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.
11 * Handlers for IRC numerics sent to the server
27 #include "conn-func.h"
30 #include "irc-write.h"
41 * Announce new server in the network
42 * @param Client New server
43 * @param Server Existing server in the network
46 Announce_Server(CLIENT * Client, CLIENT * Server)
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),
60 if (Client_Hops(Server) == 1)
61 c = Client_ThisServer();
63 c = Client_Introducer(Server);
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 */
73 * Announce existing user to a new server
74 * @param Client New server
75 * @param User Existing user in the network
78 Announce_User(CLIENT * Client, CLIENT * User)
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))
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)),
95 modes = Client_Modes(User);
97 return Conn_WriteStr(conn, ":%s MODE %s +%s",
98 Client_ID(User), Client_ID(User),
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));
110 } /* Announce_User */
116 * Synchronize invite and ban lists between servers
117 * @param Client New server
120 Synchronize_Lists(CLIENT * Client)
123 struct list_head *head;
124 struct list_elem *elem;
126 assert(Client != NULL);
131 head = Channel_GetListBans(c);
132 elem = Lists_GetFirst(head);
134 if (!IRC_WriteStrClient(Client, "MODE %s +b %s",
136 Lists_GetMask(elem))) {
139 elem = Lists_GetNext(elem);
143 head = Channel_GetListInvites(c);
144 elem = Lists_GetFirst(head);
146 if (!IRC_WriteStrClient(Client, "MODE %s +I %s",
148 Lists_GetMask(elem))) {
151 elem = Lists_GetNext(elem);
161 * Send CHANINFO commands to a new server (inform it about existing channels).
162 * @param Client New server
163 * @param Chan Channel
166 Send_CHANINFO(CLIENT * Client, CHANNEL * Chan)
172 Log(LOG_DEBUG, "Sending CHANINFO commands ...");
175 modes = Channel_Modes(Chan);
176 topic = Channel_Topic(Chan);
178 if (!*modes && !*topic)
181 has_k = strchr(modes, 'k') != NULL;
182 has_l = strchr(modes, 'l') != NULL;
185 if (!has_k && !has_l) {
187 /* "CHANINFO <chan> +<modes>" */
188 return IRC_WriteStrClient(Client, "CHANINFO %s +%s",
189 Channel_Name(Chan), modes);
191 /* "CHANINFO <chan> +<modes> :<topic>" */
192 return IRC_WriteStrClient(Client, "CHANINFO %s +%s :%s",
193 Channel_Name(Chan), modes, topic);
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 */
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.
211 IRC_Num_ENDOFMOTD(CLIENT * Client, UNUSED REQUEST * Req)
219 Client_SetType(Client, CLIENT_SERVER);
221 Log(LOG_NOTICE | LOG_snotice,
222 "Server \"%s\" registered (connection %d, 1 hop - direct link).",
223 Client_ID(Client), Client_Conn(Client));
225 /* Get highest hop count */
229 if (Client_Hops(c) > max_hops)
230 max_hops = Client_Hops(c);
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! */
247 if (!Announce_Server(Client, c))
252 /* Announce all the users to the new server */
255 if (Client_Type(c) == CLIENT_USER) {
256 if (!Announce_User(Client, c))
262 /* Announce all channels to the new server */
263 chan = Channel_First();
266 /* Send CHANINFO if the peer supports it */
267 if (strchr(Client_Flags(Client), 'C')) {
268 if (!Send_CHANINFO(Client, chan))
273 /* Get all the members of this channel */
274 cl2chan = Channel_FirstMember(chan);
275 snprintf(str, sizeof(str), "NJOIN %s :", Channel_Name(chan));
277 cl = Channel_GetClient(cl2chan);
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));
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))
293 snprintf(str, sizeof(str), "NJOIN %s :",
297 cl2chan = Channel_NextMember(chan, cl2chan);
300 /* Data left in the buffer? */
301 if (str[strlen(str) - 1] != ':') {
302 /* Yes, send it ... */
303 if (!IRC_WriteStrClient(Client, "%s", str))
307 /* Get next channel ... */
308 chan = Channel_Next(chan);
312 if (strchr(Client_Flags(Client), 'L')) {
313 LogDebug("Synchronizing INVITE- and BAN-lists ...");
314 if (!Synchronize_Lists(Client))
320 } /* IRC_Num_ENDOFMOTD */
324 * Handle ISUPPORT (005) numeric.
327 IRC_Num_ISUPPORT(CLIENT * Client, REQUEST * Req)
332 for (i = 1; i < Req->argc - 1; i++) {
334 value = strchr(key, '=');
340 if (strcmp("NICKLEN", key) == 0) {
341 if ((unsigned int)atol(value) == Conf_MaxNickLength - 1)
344 /* Nick name length settings are different! */
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",
356 } /* IRC_Num_ISUPPORT */