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)
81 conn = Client_Conn(Client);
82 if (Conn_Options(conn) & CONN_RFC1459) {
83 /* RFC 1459 mode: separate NICK and USER commands */
84 if (! Conn_WriteStr(conn, "NICK %s :%d",
85 Client_ID(User), Client_Hops(User) + 1))
87 return Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
88 Client_ID(User), Client_User(User),
89 Client_Hostname(User),
90 Client_ID(Client_Introducer(User)),
93 /* RFC 2813 mode: one combined NICK command */
94 return IRC_WriteStrClient(Client, "NICK %s %d %s %s %d +%s :%s",
95 Client_ID(User), Client_Hops(User) + 1,
96 Client_User(User), Client_Hostname(User),
97 Client_MyToken(Client_Introducer(User)),
98 Client_Modes(User), Client_Info(User));
100 } /* Announce_User */
106 * Synchronize invite and ban lists between servers
107 * @param Client New server
110 Synchronize_Lists(CLIENT * Client)
113 struct list_head *head;
114 struct list_elem *elem;
116 assert(Client != NULL);
121 head = Channel_GetListBans(c);
122 elem = Lists_GetFirst(head);
124 if (!IRC_WriteStrClient(Client, "MODE %s +b %s",
126 Lists_GetMask(elem))) {
129 elem = Lists_GetNext(elem);
133 head = Channel_GetListInvites(c);
134 elem = Lists_GetFirst(head);
136 if (!IRC_WriteStrClient(Client, "MODE %s +I %s",
138 Lists_GetMask(elem))) {
141 elem = Lists_GetNext(elem);
151 * Send CHANINFO commands to a new server (inform it about existing channels).
152 * @param Client New server
153 * @param Chan Channel
156 Send_CHANINFO(CLIENT * Client, CHANNEL * Chan)
162 Log(LOG_DEBUG, "Sending CHANINFO commands ...");
165 modes = Channel_Modes(Chan);
166 topic = Channel_Topic(Chan);
168 if (!*modes && !*topic)
171 has_k = strchr(modes, 'k') != NULL;
172 has_l = strchr(modes, 'l') != NULL;
175 if (!has_k && !has_l) {
177 /* "CHANINFO <chan> +<modes>" */
178 return IRC_WriteStrClient(Client, "CHANINFO %s +%s",
179 Channel_Name(Chan), modes);
181 /* "CHANINFO <chan> +<modes> :<topic>" */
182 return IRC_WriteStrClient(Client, "CHANINFO %s +%s :%s",
183 Channel_Name(Chan), modes, topic);
185 /* "CHANINFO <chan> +<modes> <key> <limit> :<topic>" */
186 return IRC_WriteStrClient(Client, "CHANINFO %s +%s %s %lu :%s",
187 Channel_Name(Chan), modes,
188 has_k ? Channel_Key(Chan) : "*",
189 has_l ? Channel_MaxUsers(Chan) : 0, topic);
190 } /* Send_CHANINFO */
196 * Handle ENDOFMOTD (376) numeric and login remote server.
197 * The peer is either an IRC server (no IRC+ protocol), or we got the
198 * ENDOFMOTD numeric from an IRC+ server. We have to register the new server.
201 IRC_Num_ENDOFMOTD(CLIENT * Client, UNUSED REQUEST * Req)
209 Client_SetType(Client, CLIENT_SERVER);
211 Log(LOG_NOTICE | LOG_snotice,
212 "Server \"%s\" registered (connection %d, 1 hop - direct link).",
213 Client_ID(Client), Client_Conn(Client));
215 /* Get highest hop count */
219 if (Client_Hops(c) > max_hops)
220 max_hops = Client_Hops(c);
224 /* Inform the new server about all other servers, and announce the
225 * new server to all the already registered ones. Important: we have
226 * to do this "in order" and can't introduce servers of which the
227 * "toplevel server" isn't known already. */
228 for (i = 0; i < (max_hops + 1); i++) {
229 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
230 if (Client_Type(c) != CLIENT_SERVER)
231 continue; /* not a server */
232 if (Client_Hops(c) != i)
233 continue; /* not actual "nesting level" */
234 if (c == Client || c == Client_ThisServer())
235 continue; /* that's us or the peer! */
237 if (!Announce_Server(Client, c))
242 /* Announce all the users to the new server */
245 if (Client_Type(c) == CLIENT_USER) {
246 if (!Announce_User(Client, c))
252 /* Announce all channels to the new server */
253 chan = Channel_First();
256 /* Send CHANINFO if the peer supports it */
257 if (strchr(Client_Flags(Client), 'C')) {
258 if (!Send_CHANINFO(Client, chan))
263 /* Get all the members of this channel */
264 cl2chan = Channel_FirstMember(chan);
265 snprintf(str, sizeof(str), "NJOIN %s :", Channel_Name(chan));
267 cl = Channel_GetClient(cl2chan);
270 /* Nick name, with modes (if applicable) */
271 if (str[strlen(str) - 1] != ':')
272 strlcat(str, ",", sizeof(str));
273 if (strchr(Channel_UserModes(chan, cl), 'v'))
274 strlcat(str, "+", sizeof(str));
275 if (strchr(Channel_UserModes(chan, cl), 'o'))
276 strlcat(str, "@", sizeof(str));
277 strlcat(str, Client_ID(cl), sizeof(str));
279 /* Send the data if the buffer is "full" */
280 if (strlen(str) > (LINE_LEN - CLIENT_NICK_LEN - 8)) {
281 if (!IRC_WriteStrClient(Client, "%s", str))
283 snprintf(str, sizeof(str), "NJOIN %s :",
287 cl2chan = Channel_NextMember(chan, cl2chan);
290 /* Data left in the buffer? */
291 if (str[strlen(str) - 1] != ':') {
292 /* Yes, send it ... */
293 if (!IRC_WriteStrClient(Client, "%s", str))
297 /* Get next channel ... */
298 chan = Channel_Next(chan);
302 if (strchr(Client_Flags(Client), 'L')) {
303 LogDebug("Synchronizing INVITE- and BAN-lists ...");
304 if (!Synchronize_Lists(Client))
310 } /* IRC_Num_ENDOFMOTD */
314 * Handle ISUPPORT (005) numeric.
317 IRC_Num_ISUPPORT(CLIENT * Client, REQUEST * Req)
322 for (i = 1; i < Req->argc - 1; i++) {
324 value = strchr(key, '=');
330 if (strcmp("NICKLEN", key) == 0) {
331 if ((unsigned int)atol(value) == Conf_MaxNickLength - 1)
334 /* Nick name length settings are different! */
336 "Peer uses incompatible nick name length (%d/%d)! Disconnecting ...",
337 Conf_MaxNickLength - 1, atoi(value));
338 Conn_Close(Client_Conn(Client),
339 "Incompatible nick name length",
346 } /* IRC_Num_ISUPPORT */