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.
16 * Handlers for IRC numerics sent to the server
29 #include "conn-func.h"
31 #include "irc-write.h"
42 * Announce a channel and its users in the network.
45 Announce_Channel(CLIENT *Client, CHANNEL *Chan)
49 char str[LINE_LEN], *ptr;
52 if (Conn_Options(Client_Conn(Client)) & CONN_RFC1459)
57 /* Get all the members of this channel */
58 cl2chan = Channel_FirstMember(Chan);
59 snprintf(str, sizeof(str), "NJOIN %s :", Channel_Name(Chan));
61 cl = Channel_GetClient(cl2chan);
65 /* RFC 2813: send NJOIN with nick names and modes
66 * (if user is channel operator or has voice) */
67 if (str[strlen(str) - 1] != ':')
68 strlcat(str, ",", sizeof(str));
69 if (strchr(Channel_UserModes(Chan, cl), 'v'))
70 strlcat(str, "+", sizeof(str));
71 if (strchr(Channel_UserModes(Chan, cl), 'o'))
72 strlcat(str, "@", sizeof(str));
73 strlcat(str, Client_ID(cl), sizeof(str));
75 /* Send the data if the buffer is "full" */
76 if (strlen(str) > (LINE_LEN - CLIENT_NICK_LEN - 8)) {
77 if (!IRC_WriteStrClient(Client, "%s", str))
79 snprintf(str, sizeof(str), "NJOIN %s :",
83 /* RFC 1459: no NJOIN, send JOIN and MODE */
84 if (!IRC_WriteStrClientPrefix(Client, cl, "JOIN %s",
87 ptr = Channel_UserModes(Chan, cl);
89 if (!IRC_WriteStrClientPrefix(Client, cl,
91 Channel_Name(Chan), ptr[0],
98 cl2chan = Channel_NextMember(Chan, cl2chan);
101 /* Data left in the buffer? */
102 if (str[strlen(str) - 1] != ':') {
103 /* Yes, send it ... */
104 if (!IRC_WriteStrClient(Client, "%s", str))
109 } /* Announce_Channel */
113 * Announce new server in the network
114 * @param Client New server
115 * @param Server Existing server in the network
118 Announce_Server(CLIENT * Client, CLIENT * Server)
122 if (Client_Conn(Server) > NONE) {
123 /* Announce the new server to the one already registered
124 * which is directly connected to the local server */
125 if (!IRC_WriteStrClient
126 (Server, "SERVER %s %d %d :%s", Client_ID(Client),
127 Client_Hops(Client) + 1, Client_MyToken(Client),
128 Client_Info(Client)))
132 if (Client_Hops(Server) == 1)
133 c = Client_ThisServer();
135 c = Client_TopServer(Server);
137 /* Inform new server about the one already registered in the network */
138 return IRC_WriteStrClientPrefix(Client, c, "SERVER %s %d %d :%s",
139 Client_ID(Server), Client_Hops(Server) + 1,
140 Client_MyToken(Server), Client_Info(Server));
141 } /* Announce_Server */
145 * Announce existing user to a new server
146 * @param Client New server
147 * @param User Existing user in the network
150 Announce_User(CLIENT * Client, CLIENT * User)
155 conn = Client_Conn(Client);
156 if (Conn_Options(conn) & CONN_RFC1459) {
157 /* RFC 1459 mode: separate NICK and USER commands */
158 if (! Conn_WriteStr(conn, "NICK %s :%d",
159 Client_ID(User), Client_Hops(User) + 1))
161 if (! Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
162 Client_ID(User), Client_User(User),
163 Client_Hostname(User),
164 Client_ID(Client_Introducer(User)),
167 modes = Client_Modes(User);
169 return Conn_WriteStr(conn, ":%s MODE %s +%s",
170 Client_ID(User), Client_ID(User),
175 /* RFC 2813 mode: one combined NICK or SERVICE command */
176 if (Client_Type(User) == CLIENT_SERVICE
177 && strchr(Client_Flags(Client), 'S'))
178 return IRC_WriteStrClient(Client,
179 "SERVICE %s %d * +%s %d :%s", Client_Mask(User),
180 Client_MyToken(Client_Introducer(User)),
181 Client_Modes(User), Client_Hops(User) + 1,
184 return IRC_WriteStrClient(Client,
185 "NICK %s %d %s %s %d +%s :%s",
186 Client_ID(User), Client_Hops(User) + 1,
187 Client_User(User), Client_Hostname(User),
188 Client_MyToken(Client_Introducer(User)),
189 Client_Modes(User), Client_Info(User));
191 } /* Announce_User */
197 * Synchronize invite and ban lists between servers
198 * @param Client New server
201 Synchronize_Lists(CLIENT * Client)
204 struct list_head *head;
205 struct list_elem *elem;
207 assert(Client != NULL);
212 head = Channel_GetListBans(c);
213 elem = Lists_GetFirst(head);
215 if (!IRC_WriteStrClient(Client, "MODE %s +b %s",
217 Lists_GetMask(elem))) {
220 elem = Lists_GetNext(elem);
224 head = Channel_GetListInvites(c);
225 elem = Lists_GetFirst(head);
227 if (!IRC_WriteStrClient(Client, "MODE %s +I %s",
229 Lists_GetMask(elem))) {
232 elem = Lists_GetNext(elem);
242 * Send CHANINFO commands to a new server (inform it about existing channels).
243 * @param Client New server
244 * @param Chan Channel
247 Send_CHANINFO(CLIENT * Client, CHANNEL * Chan)
253 Log(LOG_DEBUG, "Sending CHANINFO commands ...");
256 modes = Channel_Modes(Chan);
257 topic = Channel_Topic(Chan);
259 if (!*modes && !*topic)
262 has_k = strchr(modes, 'k') != NULL;
263 has_l = strchr(modes, 'l') != NULL;
266 if (!has_k && !has_l) {
268 /* "CHANINFO <chan> +<modes>" */
269 return IRC_WriteStrClient(Client, "CHANINFO %s +%s",
270 Channel_Name(Chan), modes);
272 /* "CHANINFO <chan> +<modes> :<topic>" */
273 return IRC_WriteStrClient(Client, "CHANINFO %s +%s :%s",
274 Channel_Name(Chan), modes, topic);
276 /* "CHANINFO <chan> +<modes> <key> <limit> :<topic>" */
277 return IRC_WriteStrClient(Client, "CHANINFO %s +%s %s %lu :%s",
278 Channel_Name(Chan), modes,
279 has_k ? Channel_Key(Chan) : "*",
280 has_l ? Channel_MaxUsers(Chan) : 0, topic);
281 } /* Send_CHANINFO */
287 * Handle ENDOFMOTD (376) numeric and login remote server.
288 * The peer is either an IRC server (no IRC+ protocol), or we got the
289 * ENDOFMOTD numeric from an IRC+ server. We have to register the new server.
292 IRC_Num_ENDOFMOTD(CLIENT * Client, UNUSED REQUEST * Req)
298 Client_SetType(Client, CLIENT_SERVER);
300 Log(LOG_NOTICE | LOG_snotice,
301 "Server \"%s\" registered (connection %d, 1 hop - direct link).",
302 Client_ID(Client), Client_Conn(Client));
304 /* Get highest hop count */
308 if (Client_Hops(c) > max_hops)
309 max_hops = Client_Hops(c);
313 /* Inform the new server about all other servers, and announce the
314 * new server to all the already registered ones. Important: we have
315 * to do this "in order" and can't introduce servers of which the
316 * "toplevel server" isn't known already. */
317 for (i = 0; i < (max_hops + 1); i++) {
318 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
319 if (Client_Type(c) != CLIENT_SERVER)
320 continue; /* not a server */
321 if (Client_Hops(c) != i)
322 continue; /* not actual "nesting level" */
323 if (c == Client || c == Client_ThisServer())
324 continue; /* that's us or the peer! */
326 if (!Announce_Server(Client, c))
331 /* Announce all the users to the new server */
334 if (Client_Type(c) == CLIENT_USER ||
335 Client_Type(c) == CLIENT_SERVICE) {
336 if (!Announce_User(Client, c))
342 /* Announce all channels to the new server */
343 chan = Channel_First();
345 if (Channel_IsLocal(chan)) {
346 chan = Channel_Next(chan);
350 /* Send CHANINFO if the peer supports it */
351 if (strchr(Client_Flags(Client), 'C')) {
352 if (!Send_CHANINFO(Client, chan))
357 if (!Announce_Channel(Client, chan))
360 /* Get next channel ... */
361 chan = Channel_Next(chan);
365 if (strchr(Client_Flags(Client), 'L')) {
366 LogDebug("Synchronizing INVITE- and BAN-lists ...");
367 if (!Synchronize_Lists(Client))
373 } /* IRC_Num_ENDOFMOTD */
377 * Handle ISUPPORT (005) numeric.
380 IRC_Num_ISUPPORT(CLIENT * Client, REQUEST * Req)
385 for (i = 1; i < Req->argc - 1; i++) {
387 value = strchr(key, '=');
393 if (strcmp("NICKLEN", key) == 0) {
394 if ((unsigned int)atol(value) == Conf_MaxNickLength - 1)
397 /* Nick name length settings are different! */
399 "Peer uses incompatible nick name length (%d/%d)! Disconnecting ...",
400 Conf_MaxNickLength - 1, atoi(value));
401 Conn_Close(Client_Conn(Client),
402 "Incompatible nick name length",
409 } /* IRC_Num_ISUPPORT */