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
26 #include "conn-func.h"
28 #include "irc-write.h"
39 * Announce a channel and its users in the network.
42 Announce_Channel(CLIENT *Client, CHANNEL *Chan)
46 char str[LINE_LEN], *ptr;
49 if (Conn_Options(Client_Conn(Client)) & CONN_RFC1459)
54 /* Get all the members of this channel */
55 cl2chan = Channel_FirstMember(Chan);
56 snprintf(str, sizeof(str), "NJOIN %s :", Channel_Name(Chan));
58 cl = Channel_GetClient(cl2chan);
62 /* RFC 2813: send NJOIN with nick names and modes
63 * (if user is channel operator or has voice) */
64 if (str[strlen(str) - 1] != ':')
65 strlcat(str, ",", sizeof(str));
66 if (strchr(Channel_UserModes(Chan, cl), 'v'))
67 strlcat(str, "+", sizeof(str));
68 if (strchr(Channel_UserModes(Chan, cl), 'o'))
69 strlcat(str, "@", sizeof(str));
70 strlcat(str, Client_ID(cl), sizeof(str));
72 /* Send the data if the buffer is "full" */
73 if (strlen(str) > (LINE_LEN - CLIENT_NICK_LEN - 8)) {
74 if (!IRC_WriteStrClient(Client, "%s", str))
76 snprintf(str, sizeof(str), "NJOIN %s :",
80 /* RFC 1459: no NJOIN, send JOIN and MODE */
81 if (!IRC_WriteStrClientPrefix(Client, cl, "JOIN %s",
84 ptr = Channel_UserModes(Chan, cl);
86 if (!IRC_WriteStrClientPrefix(Client, cl,
88 Channel_Name(Chan), ptr[0],
95 cl2chan = Channel_NextMember(Chan, cl2chan);
98 /* Data left in the buffer? */
99 if (str[strlen(str) - 1] != ':') {
100 /* Yes, send it ... */
101 if (!IRC_WriteStrClient(Client, "%s", str))
106 } /* Announce_Channel */
110 * Announce new server in the network
111 * @param Client New server
112 * @param Server Existing server in the network
115 Announce_Server(CLIENT * Client, CLIENT * Server)
119 if (Client_Conn(Server) > NONE) {
120 /* Announce the new server to the one already registered
121 * which is directly connected to the local server */
122 if (!IRC_WriteStrClient
123 (Server, "SERVER %s %d %d :%s", Client_ID(Client),
124 Client_Hops(Client) + 1, Client_MyToken(Client),
125 Client_Info(Client)))
129 if (Client_Hops(Server) == 1)
130 c = Client_ThisServer();
132 c = Client_TopServer(Server);
134 /* Inform new server about the one already registered in the network */
135 return IRC_WriteStrClientPrefix(Client, c, "SERVER %s %d %d :%s",
136 Client_ID(Server), Client_Hops(Server) + 1,
137 Client_MyToken(Server), Client_Info(Server));
138 } /* Announce_Server */
142 * Announce existing user to a new server
143 * @param Client New server
144 * @param User Existing user in the network
147 Announce_User(CLIENT * Client, CLIENT * User)
152 conn = Client_Conn(Client);
153 if (Conn_Options(conn) & CONN_RFC1459) {
154 /* RFC 1459 mode: separate NICK and USER commands */
155 if (! Conn_WriteStr(conn, "NICK %s :%d",
156 Client_ID(User), Client_Hops(User) + 1))
158 if (! Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
159 Client_ID(User), Client_User(User),
160 Client_Hostname(User),
161 Client_ID(Client_Introducer(User)),
164 modes = Client_Modes(User);
166 return Conn_WriteStr(conn, ":%s MODE %s +%s",
167 Client_ID(User), Client_ID(User),
172 /* RFC 2813 mode: one combined NICK or SERVICE command */
173 if (Client_Type(User) == CLIENT_SERVICE
174 && strchr(Client_Flags(Client), 'S'))
175 return IRC_WriteStrClient(Client,
176 "SERVICE %s %d * +%s %d :%s", Client_Mask(User),
177 Client_MyToken(Client_Introducer(User)),
178 Client_Modes(User), Client_Hops(User) + 1,
181 return IRC_WriteStrClient(Client,
182 "NICK %s %d %s %s %d +%s :%s",
183 Client_ID(User), Client_Hops(User) + 1,
184 Client_User(User), Client_Hostname(User),
185 Client_MyToken(Client_Introducer(User)),
186 Client_Modes(User), Client_Info(User));
188 } /* Announce_User */
194 * Synchronize invite and ban lists between servers
195 * @param Client New server
198 Synchronize_Lists(CLIENT * Client)
201 struct list_head *head;
202 struct list_elem *elem;
204 assert(Client != NULL);
209 head = Channel_GetListBans(c);
210 elem = Lists_GetFirst(head);
212 if (!IRC_WriteStrClient(Client, "MODE %s +b %s",
214 Lists_GetMask(elem))) {
217 elem = Lists_GetNext(elem);
221 head = Channel_GetListInvites(c);
222 elem = Lists_GetFirst(head);
224 if (!IRC_WriteStrClient(Client, "MODE %s +I %s",
226 Lists_GetMask(elem))) {
229 elem = Lists_GetNext(elem);
239 * Send CHANINFO commands to a new server (inform it about existing channels).
240 * @param Client New server
241 * @param Chan Channel
244 Send_CHANINFO(CLIENT * Client, CHANNEL * Chan)
250 Log(LOG_DEBUG, "Sending CHANINFO commands ...");
253 modes = Channel_Modes(Chan);
254 topic = Channel_Topic(Chan);
256 if (!*modes && !*topic)
259 has_k = strchr(modes, 'k') != NULL;
260 has_l = strchr(modes, 'l') != NULL;
263 if (!has_k && !has_l) {
265 /* "CHANINFO <chan> +<modes>" */
266 return IRC_WriteStrClient(Client, "CHANINFO %s +%s",
267 Channel_Name(Chan), modes);
269 /* "CHANINFO <chan> +<modes> :<topic>" */
270 return IRC_WriteStrClient(Client, "CHANINFO %s +%s :%s",
271 Channel_Name(Chan), modes, topic);
273 /* "CHANINFO <chan> +<modes> <key> <limit> :<topic>" */
274 return IRC_WriteStrClient(Client, "CHANINFO %s +%s %s %lu :%s",
275 Channel_Name(Chan), modes,
276 has_k ? Channel_Key(Chan) : "*",
277 has_l ? Channel_MaxUsers(Chan) : 0, topic);
278 } /* Send_CHANINFO */
284 * Handle ENDOFMOTD (376) numeric and login remote server.
285 * The peer is either an IRC server (no IRC+ protocol), or we got the
286 * ENDOFMOTD numeric from an IRC+ server. We have to register the new server.
289 IRC_Num_ENDOFMOTD(CLIENT * Client, UNUSED REQUEST * Req)
295 Client_SetType(Client, CLIENT_SERVER);
297 Log(LOG_NOTICE | LOG_snotice,
298 "Server \"%s\" registered (connection %d, 1 hop - direct link).",
299 Client_ID(Client), Client_Conn(Client));
301 /* Get highest hop count */
305 if (Client_Hops(c) > max_hops)
306 max_hops = Client_Hops(c);
310 /* Inform the new server about all other servers, and announce the
311 * new server to all the already registered ones. Important: we have
312 * to do this "in order" and can't introduce servers of which the
313 * "toplevel server" isn't known already. */
314 for (i = 0; i < (max_hops + 1); i++) {
315 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
316 if (Client_Type(c) != CLIENT_SERVER)
317 continue; /* not a server */
318 if (Client_Hops(c) != i)
319 continue; /* not actual "nesting level" */
320 if (c == Client || c == Client_ThisServer())
321 continue; /* that's us or the peer! */
323 if (!Announce_Server(Client, c))
328 /* Announce all the users to the new server */
331 if (Client_Type(c) == CLIENT_USER ||
332 Client_Type(c) == CLIENT_SERVICE) {
333 if (!Announce_User(Client, c))
339 /* Announce all channels to the new server */
340 chan = Channel_First();
342 if (Channel_IsLocal(chan)) {
343 chan = Channel_Next(chan);
347 /* Send CHANINFO if the peer supports it */
348 if (strchr(Client_Flags(Client), 'C')) {
349 if (!Send_CHANINFO(Client, chan))
354 if (!Announce_Channel(Client, chan))
357 /* Get next channel ... */
358 chan = Channel_Next(chan);
362 if (strchr(Client_Flags(Client), 'L')) {
363 LogDebug("Synchronizing INVITE- and BAN-lists ...");
364 if (!Synchronize_Lists(Client))
370 } /* IRC_Num_ENDOFMOTD */
374 * Handle ISUPPORT (005) numeric.
377 IRC_Num_ISUPPORT(CLIENT * Client, REQUEST * Req)
382 for (i = 1; i < Req->argc - 1; i++) {
384 value = strchr(key, '=');
390 if (strcmp("NICKLEN", key) == 0) {
391 if ((unsigned int)atol(value) == Conf_MaxNickLength - 1)
394 /* Nick name length settings are different! */
396 "Peer uses incompatible nick name length (%d/%d)! Disconnecting ...",
397 Conf_MaxNickLength - 1, atoi(value));
398 Conn_Close(Client_Conn(Client),
399 "Incompatible nick name length",
406 } /* IRC_Num_ISUPPORT */