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)),
94 /* RFC 2813 mode: one combined NICK command */
95 return IRC_WriteStrClient(Client, "NICK %s %d %s %s %d +%s :%s",
96 Client_ID(User), Client_Hops(User) + 1,
97 Client_User(User), Client_Hostname(User),
98 Client_MyToken(Client_Introducer(User)),
99 Client_Modes(User), Client_Info(User));
101 } /* Announce_User */
107 * Synchronize invite and ban lists between servers
108 * @param Client New server
111 Synchronize_Lists(CLIENT * Client)
114 struct list_head *head;
115 struct list_elem *elem;
117 assert(Client != NULL);
122 head = Channel_GetListBans(c);
123 elem = Lists_GetFirst(head);
125 if (!IRC_WriteStrClient(Client, "MODE %s +b %s",
127 Lists_GetMask(elem))) {
130 elem = Lists_GetNext(elem);
134 head = Channel_GetListInvites(c);
135 elem = Lists_GetFirst(head);
137 if (!IRC_WriteStrClient(Client, "MODE %s +I %s",
139 Lists_GetMask(elem))) {
142 elem = Lists_GetNext(elem);
152 * Send CHANINFO commands to a new server (inform it about existing channels).
153 * @param Client New server
154 * @param Chan Channel
157 Send_CHANINFO(CLIENT * Client, CHANNEL * Chan)
163 Log(LOG_DEBUG, "Sending CHANINFO commands ...");
166 modes = Channel_Modes(Chan);
167 topic = Channel_Topic(Chan);
169 if (!*modes && !*topic)
172 has_k = strchr(modes, 'k') != NULL;
173 has_l = strchr(modes, 'l') != NULL;
176 if (!has_k && !has_l) {
178 /* "CHANINFO <chan> +<modes>" */
179 return IRC_WriteStrClient(Client, "CHANINFO %s +%s",
180 Channel_Name(Chan), modes);
182 /* "CHANINFO <chan> +<modes> :<topic>" */
183 return IRC_WriteStrClient(Client, "CHANINFO %s +%s :%s",
184 Channel_Name(Chan), modes, topic);
186 /* "CHANINFO <chan> +<modes> <key> <limit> :<topic>" */
187 return IRC_WriteStrClient(Client, "CHANINFO %s +%s %s %lu :%s",
188 Channel_Name(Chan), modes,
189 has_k ? Channel_Key(Chan) : "*",
190 has_l ? Channel_MaxUsers(Chan) : 0, topic);
191 } /* Send_CHANINFO */
197 * Handle ENDOFMOTD (376) numeric and login remote server.
198 * The peer is either an IRC server (no IRC+ protocol), or we got the
199 * ENDOFMOTD numeric from an IRC+ server. We have to register the new server.
202 IRC_Num_ENDOFMOTD(CLIENT * Client, UNUSED REQUEST * Req)
210 Client_SetType(Client, CLIENT_SERVER);
212 Log(LOG_NOTICE | LOG_snotice,
213 "Server \"%s\" registered (connection %d, 1 hop - direct link).",
214 Client_ID(Client), Client_Conn(Client));
216 /* Get highest hop count */
220 if (Client_Hops(c) > max_hops)
221 max_hops = Client_Hops(c);
225 /* Inform the new server about all other servers, and announce the
226 * new server to all the already registered ones. Important: we have
227 * to do this "in order" and can't introduce servers of which the
228 * "toplevel server" isn't known already. */
229 for (i = 0; i < (max_hops + 1); i++) {
230 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
231 if (Client_Type(c) != CLIENT_SERVER)
232 continue; /* not a server */
233 if (Client_Hops(c) != i)
234 continue; /* not actual "nesting level" */
235 if (c == Client || c == Client_ThisServer())
236 continue; /* that's us or the peer! */
238 if (!Announce_Server(Client, c))
243 /* Announce all the users to the new server */
246 if (Client_Type(c) == CLIENT_USER) {
247 if (!Announce_User(Client, c))
253 /* Announce all channels to the new server */
254 chan = Channel_First();
257 /* Send CHANINFO if the peer supports it */
258 if (strchr(Client_Flags(Client), 'C')) {
259 if (!Send_CHANINFO(Client, chan))
264 /* Get all the members of this channel */
265 cl2chan = Channel_FirstMember(chan);
266 snprintf(str, sizeof(str), "NJOIN %s :", Channel_Name(chan));
268 cl = Channel_GetClient(cl2chan);
271 /* Nick name, with modes (if applicable) */
272 if (str[strlen(str) - 1] != ':')
273 strlcat(str, ",", sizeof(str));
274 if (strchr(Channel_UserModes(chan, cl), 'v'))
275 strlcat(str, "+", sizeof(str));
276 if (strchr(Channel_UserModes(chan, cl), 'o'))
277 strlcat(str, "@", sizeof(str));
278 strlcat(str, Client_ID(cl), sizeof(str));
280 /* Send the data if the buffer is "full" */
281 if (strlen(str) > (LINE_LEN - CLIENT_NICK_LEN - 8)) {
282 if (!IRC_WriteStrClient(Client, "%s", str))
284 snprintf(str, sizeof(str), "NJOIN %s :",
288 cl2chan = Channel_NextMember(chan, cl2chan);
291 /* Data left in the buffer? */
292 if (str[strlen(str) - 1] != ':') {
293 /* Yes, send it ... */
294 if (!IRC_WriteStrClient(Client, "%s", str))
298 /* Get next channel ... */
299 chan = Channel_Next(chan);
303 if (strchr(Client_Flags(Client), 'L')) {
304 LogDebug("Synchronizing INVITE- and BAN-lists ...");
305 if (!Synchronize_Lists(Client))
311 } /* IRC_Num_ENDOFMOTD */
315 * Handle ISUPPORT (005) numeric.
318 IRC_Num_ISUPPORT(CLIENT * Client, REQUEST * Req)
323 for (i = 1; i < Req->argc - 1; i++) {
325 value = strchr(key, '=');
331 if (strcmp("NICKLEN", key) == 0) {
332 if ((unsigned int)atol(value) == Conf_MaxNickLength - 1)
335 /* Nick name length settings are different! */
337 "Peer uses incompatible nick name length (%d/%d)! Disconnecting ...",
338 Conf_MaxNickLength - 1, atoi(value));
339 Conn_Close(Client_Conn(Client),
340 "Incompatible nick name length",
347 } /* IRC_Num_ISUPPORT */