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
28 #include "conn-func.h"
31 #include "irc-write.h"
40 * Announce a channel and its users in the network.
43 Announce_Channel(CLIENT *Client, CHANNEL *Chan)
47 char str[COMMAND_LEN], *ptr;
50 /* Check features of remote server */
51 njoin = Conn_Options(Client_Conn(Client)) & CONN_RFC1459 ? false : true;
52 xop = Client_HasFlag(Client, 'X') ? true : false;
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 nicknames and modes
63 * (if user is channel operator or has voice) */
64 if (str[strlen(str) - 1] != ':')
65 strlcat(str, ",", sizeof(str));
67 /* Prepare user prefix (ChanOp, voiced, ...) */
68 if (xop && Channel_UserHasMode(Chan, cl, 'q'))
69 strlcat(str, "~", sizeof(str));
70 if (xop && Channel_UserHasMode(Chan, cl, 'a'))
71 strlcat(str, "&", sizeof(str));
72 if (Channel_UserHasMode(Chan, cl, 'o'))
73 strlcat(str, "@", sizeof(str));
74 if (xop && Channel_UserHasMode(Chan, cl, 'h'))
75 strlcat(str, "%", sizeof(str));
76 if (Channel_UserHasMode(Chan, cl, 'v'))
77 strlcat(str, "+", sizeof(str));
79 strlcat(str, Client_ID(cl), sizeof(str));
81 /* Send the data if the buffer is "full" */
82 if (strlen(str) > (sizeof(str) - CLIENT_NICK_LEN - 8)) {
83 if (!IRC_WriteStrClient(Client, "%s", str))
85 snprintf(str, sizeof(str), "NJOIN %s :",
89 /* RFC 1459: no NJOIN, send JOIN and MODE */
90 if (!IRC_WriteStrClientPrefix(Client, cl, "JOIN %s",
93 ptr = Channel_UserModes(Chan, cl);
95 if (!IRC_WriteStrClientPrefix(Client, cl,
97 Channel_Name(Chan), ptr[0],
104 cl2chan = Channel_NextMember(Chan, cl2chan);
107 /* Data left in the buffer? */
108 if (str[strlen(str) - 1] != ':') {
109 /* Yes, send it ... */
110 if (!IRC_WriteStrClient(Client, "%s", str))
115 } /* Announce_Channel */
118 * Announce new server in the network
119 * @param Client New server
120 * @param Server Existing server in the network
123 Announce_Server(CLIENT * Client, CLIENT * Server)
127 if (Client_Conn(Server) > NONE) {
128 /* Announce the new server to the one already registered
129 * which is directly connected to the local server */
130 if (!IRC_WriteStrClient
131 (Server, "SERVER %s %d %d :%s", Client_ID(Client),
132 Client_Hops(Client) + 1, Client_MyToken(Client),
133 Client_Info(Client)))
137 if (Client_Hops(Server) == 1)
138 c = Client_ThisServer();
140 c = Client_TopServer(Server);
142 /* Inform new server about the one already registered in the network */
143 return IRC_WriteStrClientPrefix(Client, c, "SERVER %s %d %d :%s",
144 Client_ID(Server), Client_Hops(Server) + 1,
145 Client_MyToken(Server), Client_Info(Server));
146 } /* Announce_Server */
151 * Synchronize invite, ban, G- and K-Line lists between servers.
153 * @param Client New server.
154 * @return CONNECTED or DISCONNECTED.
157 Synchronize_Lists(CLIENT * Client)
160 struct list_head *head;
161 struct list_elem *elem;
163 assert(Client != NULL);
166 head = Class_GetList(CLASS_GLINE);
167 elem = Lists_GetFirst(head);
169 if (!IRC_WriteStrClient(Client, "GLINE %s %ld :%s",
171 Lists_GetValidity(elem) - time(NULL),
172 Lists_GetReason(elem)))
174 elem = Lists_GetNext(elem);
180 head = Channel_GetListBans(c);
181 elem = Lists_GetFirst(head);
183 if (!IRC_WriteStrClient(Client, "MODE %s +b %s",
185 Lists_GetMask(elem))) {
188 elem = Lists_GetNext(elem);
192 head = Channel_GetListInvites(c);
193 elem = Lists_GetFirst(head);
195 if (!IRC_WriteStrClient(Client, "MODE %s +I %s",
197 Lists_GetMask(elem))) {
200 elem = Lists_GetNext(elem);
209 * Send CHANINFO commands to a new server (inform it about existing channels).
210 * @param Client New server
211 * @param Chan Channel
214 Send_CHANINFO(CLIENT * Client, CHANNEL * Chan)
220 Log(LOG_DEBUG, "Sending CHANINFO commands ...");
223 modes = Channel_Modes(Chan);
224 topic = Channel_Topic(Chan);
226 if (!*modes && !*topic)
229 has_k = Channel_HasMode(Chan, 'k');
230 has_l = Channel_HasMode(Chan, 'l');
233 if (!has_k && !has_l) {
235 /* "CHANINFO <chan> +<modes>" */
236 return IRC_WriteStrClient(Client, "CHANINFO %s +%s",
237 Channel_Name(Chan), modes);
239 /* "CHANINFO <chan> +<modes> :<topic>" */
240 return IRC_WriteStrClient(Client, "CHANINFO %s +%s :%s",
241 Channel_Name(Chan), modes, topic);
243 /* "CHANINFO <chan> +<modes> <key> <limit> :<topic>" */
244 return IRC_WriteStrClient(Client, "CHANINFO %s +%s %s %lu :%s",
245 Channel_Name(Chan), modes,
246 has_k ? Channel_Key(Chan) : "*",
247 has_l ? Channel_MaxUsers(Chan) : 0, topic);
248 } /* Send_CHANINFO */
253 * Handle ENDOFMOTD (376) numeric and login remote server.
254 * The peer is either an IRC server (no IRC+ protocol), or we got the
255 * ENDOFMOTD numeric from an IRC+ server. We have to register the new server.
258 IRC_Num_ENDOFMOTD(CLIENT * Client, UNUSED REQUEST * Req)
264 Client_SetType(Client, CLIENT_SERVER);
266 Log(LOG_NOTICE | LOG_snotice,
267 "Server \"%s\" registered (connection %d, 1 hop - direct link).",
268 Client_ID(Client), Client_Conn(Client));
270 /* Get highest hop count */
274 if (Client_Hops(c) > max_hops)
275 max_hops = Client_Hops(c);
279 /* Inform the new server about all other servers, and announce the
280 * new server to all the already registered ones. Important: we have
281 * to do this "in order" and can't introduce servers of which the
282 * "toplevel server" isn't known already. */
283 for (i = 0; i < (max_hops + 1); i++) {
284 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
285 if (Client_Type(c) != CLIENT_SERVER)
286 continue; /* not a server */
287 if (Client_Hops(c) != i)
288 continue; /* not actual "nesting level" */
289 if (c == Client || c == Client_ThisServer())
290 continue; /* that's us or the peer! */
292 if (!Announce_Server(Client, c))
297 /* Announce all the users to the new server */
300 if (Client_Type(c) == CLIENT_USER ||
301 Client_Type(c) == CLIENT_SERVICE) {
302 if (!Client_Announce(Client, Client_ThisServer(), c))
308 /* Announce all channels to the new server */
309 chan = Channel_First();
311 if (Channel_IsLocal(chan)) {
312 chan = Channel_Next(chan);
316 /* Send CHANINFO if the peer supports it */
317 if (Client_HasFlag(Client, 'C')) {
318 if (!Send_CHANINFO(Client, chan))
323 if (!Announce_Channel(Client, chan))
326 /* Get next channel ... */
327 chan = Channel_Next(chan);
331 if (Client_HasFlag(Client, 'L')) {
332 LogDebug("Synchronizing INVITE- and BAN-lists ...");
333 if (!Synchronize_Lists(Client))
338 if (!IRC_WriteStrClient(Client, "PING :%s",
339 Client_ID(Client_ThisServer())))
343 } /* IRC_Num_ENDOFMOTD */
346 * Handle ISUPPORT (005) numeric.
349 IRC_Num_ISUPPORT(CLIENT * Client, REQUEST * Req)
354 for (i = 1; i < Req->argc - 1; i++) {
356 value = strchr(key, '=');
362 if (strcmp("NICKLEN", key) == 0) {
363 if ((unsigned int)atol(value) == Conf_MaxNickLength - 1)
366 /* Nickname length settings are different! */
368 "Peer uses incompatible nickname length (%d/%d)! Disconnecting ...",
369 Conf_MaxNickLength - 1, atoi(value));
370 Conn_Close(Client_Conn(Client),
371 "Incompatible nickname length",
378 } /* IRC_Num_ISUPPORT */