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"
29 #include "irc-write.h"
40 * Announce a channel and its users in the network.
43 Announce_Channel(CLIENT *Client, CHANNEL *Chan)
47 char str[LINE_LEN], *ptr;
50 if (Conn_Options(Client_Conn(Client)) & CONN_RFC1459)
55 /* Get all the members of this channel */
56 cl2chan = Channel_FirstMember(Chan);
57 snprintf(str, sizeof(str), "NJOIN %s :", Channel_Name(Chan));
59 cl = Channel_GetClient(cl2chan);
63 /* RFC 2813: send NJOIN with nick names and modes
64 * (if user is channel operator or has voice) */
65 if (str[strlen(str) - 1] != ':')
66 strlcat(str, ",", sizeof(str));
67 if (strchr(Channel_UserModes(Chan, cl), 'v'))
68 strlcat(str, "+", sizeof(str));
69 if (strchr(Channel_UserModes(Chan, cl), 'o'))
70 strlcat(str, "@", sizeof(str));
71 strlcat(str, Client_ID(cl), sizeof(str));
73 /* Send the data if the buffer is "full" */
74 if (strlen(str) > (LINE_LEN - CLIENT_NICK_LEN - 8)) {
75 if (!IRC_WriteStrClient(Client, "%s", str))
77 snprintf(str, sizeof(str), "NJOIN %s :",
81 /* RFC 1459: no NJOIN, send JOIN and MODE */
82 if (!IRC_WriteStrClientPrefix(Client, cl, "JOIN %s",
85 ptr = Channel_UserModes(Chan, cl);
87 if (!IRC_WriteStrClientPrefix(Client, cl,
89 Channel_Name(Chan), ptr[0],
96 cl2chan = Channel_NextMember(Chan, cl2chan);
99 /* Data left in the buffer? */
100 if (str[strlen(str) - 1] != ':') {
101 /* Yes, send it ... */
102 if (!IRC_WriteStrClient(Client, "%s", str))
107 } /* Announce_Channel */
111 * Announce new server in the network
112 * @param Client New server
113 * @param Server Existing server in the network
116 Announce_Server(CLIENT * Client, CLIENT * Server)
120 if (Client_Conn(Server) > NONE) {
121 /* Announce the new server to the one already registered
122 * which is directly connected to the local server */
123 if (!IRC_WriteStrClient
124 (Server, "SERVER %s %d %d :%s", Client_ID(Client),
125 Client_Hops(Client) + 1, Client_MyToken(Client),
126 Client_Info(Client)))
130 if (Client_Hops(Server) == 1)
131 c = Client_ThisServer();
133 c = Client_TopServer(Server);
135 /* Inform new server about the one already registered in the network */
136 return IRC_WriteStrClientPrefix(Client, c, "SERVER %s %d %d :%s",
137 Client_ID(Server), Client_Hops(Server) + 1,
138 Client_MyToken(Server), Client_Info(Server));
139 } /* Announce_Server */
143 * Announce existing user to a new server
144 * @param Client New server
145 * @param User Existing user in the network
148 Announce_User(CLIENT * Client, CLIENT * User)
153 conn = Client_Conn(Client);
154 if (Conn_Options(conn) & CONN_RFC1459) {
155 /* RFC 1459 mode: separate NICK and USER commands */
156 if (! Conn_WriteStr(conn, "NICK %s :%d",
157 Client_ID(User), Client_Hops(User) + 1))
159 if (! Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
160 Client_ID(User), Client_User(User),
161 Client_Hostname(User),
162 Client_ID(Client_Introducer(User)),
165 modes = Client_Modes(User);
167 return Conn_WriteStr(conn, ":%s MODE %s +%s",
168 Client_ID(User), Client_ID(User),
173 /* RFC 2813 mode: one combined NICK or SERVICE command */
174 if (Client_Type(User) == CLIENT_SERVICE
175 && strchr(Client_Flags(Client), 'S'))
176 return IRC_WriteStrClient(Client,
177 "SERVICE %s %d * +%s %d :%s", Client_Mask(User),
178 Client_MyToken(Client_Introducer(User)),
179 Client_Modes(User), Client_Hops(User) + 1,
182 return IRC_WriteStrClient(Client,
183 "NICK %s %d %s %s %d +%s :%s",
184 Client_ID(User), Client_Hops(User) + 1,
185 Client_User(User), Client_Hostname(User),
186 Client_MyToken(Client_Introducer(User)),
187 Client_Modes(User), Client_Info(User));
189 } /* Announce_User */
195 * Synchronize invite and ban lists between servers
196 * @param Client New server
199 Synchronize_Lists(CLIENT * Client)
202 struct list_head *head;
203 struct list_elem *elem;
205 assert(Client != NULL);
210 head = Channel_GetListBans(c);
211 elem = Lists_GetFirst(head);
213 if (!IRC_WriteStrClient(Client, "MODE %s +b %s",
215 Lists_GetMask(elem))) {
218 elem = Lists_GetNext(elem);
222 head = Channel_GetListInvites(c);
223 elem = Lists_GetFirst(head);
225 if (!IRC_WriteStrClient(Client, "MODE %s +I %s",
227 Lists_GetMask(elem))) {
230 elem = Lists_GetNext(elem);
240 * Send CHANINFO commands to a new server (inform it about existing channels).
241 * @param Client New server
242 * @param Chan Channel
245 Send_CHANINFO(CLIENT * Client, CHANNEL * Chan)
251 Log(LOG_DEBUG, "Sending CHANINFO commands ...");
254 modes = Channel_Modes(Chan);
255 topic = Channel_Topic(Chan);
257 if (!*modes && !*topic)
260 has_k = strchr(modes, 'k') != NULL;
261 has_l = strchr(modes, 'l') != NULL;
264 if (!has_k && !has_l) {
266 /* "CHANINFO <chan> +<modes>" */
267 return IRC_WriteStrClient(Client, "CHANINFO %s +%s",
268 Channel_Name(Chan), modes);
270 /* "CHANINFO <chan> +<modes> :<topic>" */
271 return IRC_WriteStrClient(Client, "CHANINFO %s +%s :%s",
272 Channel_Name(Chan), modes, topic);
274 /* "CHANINFO <chan> +<modes> <key> <limit> :<topic>" */
275 return IRC_WriteStrClient(Client, "CHANINFO %s +%s %s %lu :%s",
276 Channel_Name(Chan), modes,
277 has_k ? Channel_Key(Chan) : "*",
278 has_l ? Channel_MaxUsers(Chan) : 0, topic);
279 } /* Send_CHANINFO */
285 * Handle ENDOFMOTD (376) numeric and login remote server.
286 * The peer is either an IRC server (no IRC+ protocol), or we got the
287 * ENDOFMOTD numeric from an IRC+ server. We have to register the new server.
290 IRC_Num_ENDOFMOTD(CLIENT * Client, UNUSED REQUEST * Req)
296 Client_SetType(Client, CLIENT_SERVER);
298 Log(LOG_NOTICE | LOG_snotice,
299 "Server \"%s\" registered (connection %d, 1 hop - direct link).",
300 Client_ID(Client), Client_Conn(Client));
302 /* Get highest hop count */
306 if (Client_Hops(c) > max_hops)
307 max_hops = Client_Hops(c);
311 /* Inform the new server about all other servers, and announce the
312 * new server to all the already registered ones. Important: we have
313 * to do this "in order" and can't introduce servers of which the
314 * "toplevel server" isn't known already. */
315 for (i = 0; i < (max_hops + 1); i++) {
316 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
317 if (Client_Type(c) != CLIENT_SERVER)
318 continue; /* not a server */
319 if (Client_Hops(c) != i)
320 continue; /* not actual "nesting level" */
321 if (c == Client || c == Client_ThisServer())
322 continue; /* that's us or the peer! */
324 if (!Announce_Server(Client, c))
329 /* Announce all the users to the new server */
332 if (Client_Type(c) == CLIENT_USER ||
333 Client_Type(c) == CLIENT_SERVICE) {
334 if (!Announce_User(Client, c))
340 /* Announce all channels to the new server */
341 chan = Channel_First();
343 if (Channel_IsLocal(chan)) {
344 chan = Channel_Next(chan);
348 /* Send CHANINFO if the peer supports it */
349 if (strchr(Client_Flags(Client), 'C')) {
350 if (!Send_CHANINFO(Client, chan))
355 if (!Announce_Channel(Client, chan))
358 /* Get next channel ... */
359 chan = Channel_Next(chan);
363 if (strchr(Client_Flags(Client), 'L')) {
364 LogDebug("Synchronizing INVITE- and BAN-lists ...");
365 if (!Synchronize_Lists(Client))
371 } /* IRC_Num_ENDOFMOTD */
375 * Handle ISUPPORT (005) numeric.
378 IRC_Num_ISUPPORT(CLIENT * Client, REQUEST * Req)
383 for (i = 1; i < Req->argc - 1; i++) {
385 value = strchr(key, '=');
391 if (strcmp("NICKLEN", key) == 0) {
392 if ((unsigned int)atol(value) == Conf_MaxNickLength - 1)
395 /* Nick name length settings are different! */
397 "Peer uses incompatible nick name length (%d/%d)! Disconnecting ...",
398 Conf_MaxNickLength - 1, atoi(value));
399 Conn_Close(Client_Conn(Client),
400 "Incompatible nick name length",
407 } /* IRC_Num_ISUPPORT */