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"
32 #include "irc-write.h"
43 * Announce a channel and its users in the network.
46 Announce_Channel(CLIENT *Client, CHANNEL *Chan)
50 char str[COMMAND_LEN], *ptr;
53 /* Check features of remote server */
54 njoin = Conn_Options(Client_Conn(Client)) & CONN_RFC1459 ? false : true;
55 xop = Client_HasFlag(Client, 'X') ? true : false;
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 nicknames and modes
66 * (if user is channel operator or has voice) */
67 if (str[strlen(str) - 1] != ':')
68 strlcat(str, ",", sizeof(str));
70 /* Prepare user prefix (ChanOp, voiced, ...) */
71 if (xop && Channel_UserHasMode(Chan, cl, 'q'))
72 strlcat(str, "~", sizeof(str));
73 if (xop && Channel_UserHasMode(Chan, cl, 'a'))
74 strlcat(str, "&", sizeof(str));
75 if (Channel_UserHasMode(Chan, cl, 'o'))
76 strlcat(str, "@", sizeof(str));
77 if (xop && Channel_UserHasMode(Chan, cl, 'h'))
78 strlcat(str, "%", sizeof(str));
79 if (Channel_UserHasMode(Chan, cl, 'v'))
80 strlcat(str, "+", sizeof(str));
82 strlcat(str, Client_ID(cl), sizeof(str));
84 /* Send the data if the buffer is "full" */
85 if (strlen(str) > (sizeof(str) - CLIENT_NICK_LEN - 8)) {
86 if (!IRC_WriteStrClient(Client, "%s", str))
88 snprintf(str, sizeof(str), "NJOIN %s :",
92 /* RFC 1459: no NJOIN, send JOIN and MODE */
93 if (!IRC_WriteStrClientPrefix(Client, cl, "JOIN %s",
96 ptr = Channel_UserModes(Chan, cl);
98 if (!IRC_WriteStrClientPrefix(Client, cl,
100 Channel_Name(Chan), ptr[0],
107 cl2chan = Channel_NextMember(Chan, cl2chan);
110 /* Data left in the buffer? */
111 if (str[strlen(str) - 1] != ':') {
112 /* Yes, send it ... */
113 if (!IRC_WriteStrClient(Client, "%s", str))
118 } /* Announce_Channel */
122 * Announce new server in the network
123 * @param Client New server
124 * @param Server Existing server in the network
127 Announce_Server(CLIENT * Client, CLIENT * Server)
131 if (Client_Conn(Server) > NONE) {
132 /* Announce the new server to the one already registered
133 * which is directly connected to the local server */
134 if (!IRC_WriteStrClient
135 (Server, "SERVER %s %d %d :%s", Client_ID(Client),
136 Client_Hops(Client) + 1, Client_MyToken(Client),
137 Client_Info(Client)))
141 if (Client_Hops(Server) == 1)
142 c = Client_ThisServer();
144 c = Client_TopServer(Server);
146 /* Inform new server about the one already registered in the network */
147 return IRC_WriteStrClientPrefix(Client, c, "SERVER %s %d %d :%s",
148 Client_ID(Server), Client_Hops(Server) + 1,
149 Client_MyToken(Server), Client_Info(Server));
150 } /* Announce_Server */
156 * Synchronize invite, ban, G- and K-Line lists between servers.
158 * @param Client New server.
159 * @return CONNECTED or DISCONNECTED.
162 Synchronize_Lists(CLIENT * Client)
165 struct list_head *head;
166 struct list_elem *elem;
168 assert(Client != NULL);
171 head = Class_GetList(CLASS_GLINE);
172 elem = Lists_GetFirst(head);
174 if (!IRC_WriteStrClient(Client, "GLINE %s %ld :%s",
176 Lists_GetValidity(elem) - time(NULL),
177 Lists_GetReason(elem)))
179 elem = Lists_GetNext(elem);
185 head = Channel_GetListBans(c);
186 elem = Lists_GetFirst(head);
188 if (!IRC_WriteStrClient(Client, "MODE %s +b %s",
190 Lists_GetMask(elem))) {
193 elem = Lists_GetNext(elem);
197 head = Channel_GetListInvites(c);
198 elem = Lists_GetFirst(head);
200 if (!IRC_WriteStrClient(Client, "MODE %s +I %s",
202 Lists_GetMask(elem))) {
205 elem = Lists_GetNext(elem);
215 * Send CHANINFO commands to a new server (inform it about existing channels).
216 * @param Client New server
217 * @param Chan Channel
220 Send_CHANINFO(CLIENT * Client, CHANNEL * Chan)
226 Log(LOG_DEBUG, "Sending CHANINFO commands ...");
229 modes = Channel_Modes(Chan);
230 topic = Channel_Topic(Chan);
232 if (!*modes && !*topic)
235 has_k = Channel_HasMode(Chan, 'k');
236 has_l = Channel_HasMode(Chan, 'l');
239 if (!has_k && !has_l) {
241 /* "CHANINFO <chan> +<modes>" */
242 return IRC_WriteStrClient(Client, "CHANINFO %s +%s",
243 Channel_Name(Chan), modes);
245 /* "CHANINFO <chan> +<modes> :<topic>" */
246 return IRC_WriteStrClient(Client, "CHANINFO %s +%s :%s",
247 Channel_Name(Chan), modes, topic);
249 /* "CHANINFO <chan> +<modes> <key> <limit> :<topic>" */
250 return IRC_WriteStrClient(Client, "CHANINFO %s +%s %s %lu :%s",
251 Channel_Name(Chan), modes,
252 has_k ? Channel_Key(Chan) : "*",
253 has_l ? Channel_MaxUsers(Chan) : 0, topic);
254 } /* Send_CHANINFO */
260 * Handle ENDOFMOTD (376) numeric and login remote server.
261 * The peer is either an IRC server (no IRC+ protocol), or we got the
262 * ENDOFMOTD numeric from an IRC+ server. We have to register the new server.
265 IRC_Num_ENDOFMOTD(CLIENT * Client, UNUSED REQUEST * Req)
271 Client_SetType(Client, CLIENT_SERVER);
273 Log(LOG_NOTICE | LOG_snotice,
274 "Server \"%s\" registered (connection %d, 1 hop - direct link).",
275 Client_ID(Client), Client_Conn(Client));
277 /* Get highest hop count */
281 if (Client_Hops(c) > max_hops)
282 max_hops = Client_Hops(c);
286 /* Inform the new server about all other servers, and announce the
287 * new server to all the already registered ones. Important: we have
288 * to do this "in order" and can't introduce servers of which the
289 * "toplevel server" isn't known already. */
290 for (i = 0; i < (max_hops + 1); i++) {
291 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
292 if (Client_Type(c) != CLIENT_SERVER)
293 continue; /* not a server */
294 if (Client_Hops(c) != i)
295 continue; /* not actual "nesting level" */
296 if (c == Client || c == Client_ThisServer())
297 continue; /* that's us or the peer! */
299 if (!Announce_Server(Client, c))
304 /* Announce all the users to the new server */
307 if (Client_Type(c) == CLIENT_USER ||
308 Client_Type(c) == CLIENT_SERVICE) {
309 if (!Client_Announce(Client, Client_ThisServer(), c))
315 /* Announce all channels to the new server */
316 chan = Channel_First();
318 if (Channel_IsLocal(chan)) {
319 chan = Channel_Next(chan);
323 /* Send CHANINFO if the peer supports it */
324 if (Client_HasFlag(Client, 'C')) {
325 if (!Send_CHANINFO(Client, chan))
330 if (!Announce_Channel(Client, chan))
333 /* Get next channel ... */
334 chan = Channel_Next(chan);
338 if (Client_HasFlag(Client, 'L')) {
339 LogDebug("Synchronizing INVITE- and BAN-lists ...");
340 if (!Synchronize_Lists(Client))
345 if (!IRC_WriteStrClient(Client, "PING :%s",
346 Client_ID(Client_ThisServer())))
350 } /* IRC_Num_ENDOFMOTD */
354 * Handle ISUPPORT (005) numeric.
357 IRC_Num_ISUPPORT(CLIENT * Client, REQUEST * Req)
362 for (i = 1; i < Req->argc - 1; i++) {
364 value = strchr(key, '=');
370 if (strcmp("NICKLEN", key) == 0) {
371 if ((unsigned int)atol(value) == Conf_MaxNickLength - 1)
374 /* Nickname length settings are different! */
376 "Peer uses incompatible nickname length (%d/%d)! Disconnecting ...",
377 Conf_MaxNickLength - 1, atoi(value));
378 Conn_Close(Client_Conn(Client),
379 "Incompatible nickname length",
386 } /* IRC_Num_ISUPPORT */