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 */
121 * Announce new server in the network
122 * @param Client New server
123 * @param Server Existing server in the network
126 Announce_Server(CLIENT * Client, CLIENT * Server)
130 if (Client_Conn(Server) > NONE) {
131 /* Announce the new server to the one already registered
132 * which is directly connected to the local server */
133 if (!IRC_WriteStrClient
134 (Server, "SERVER %s %d %d :%s", Client_ID(Client),
135 Client_Hops(Client) + 1, Client_MyToken(Client),
136 Client_Info(Client)))
140 if (Client_Hops(Server) == 1)
141 c = Client_ThisServer();
143 c = Client_TopServer(Server);
145 /* Inform new server about the one already registered in the network */
146 return IRC_WriteStrClientPrefix(Client, c, "SERVER %s %d %d :%s",
147 Client_ID(Server), Client_Hops(Server) + 1,
148 Client_MyToken(Server), Client_Info(Server));
149 } /* Announce_Server */
154 * Synchronize invite, ban, G- and K-Line lists between servers.
156 * @param Client New server.
157 * @return CONNECTED or DISCONNECTED.
160 Synchronize_Lists(CLIENT * Client)
163 struct list_head *head;
164 struct list_elem *elem;
166 assert(Client != NULL);
169 head = Class_GetList(CLASS_GLINE);
170 elem = Lists_GetFirst(head);
172 if (!IRC_WriteStrClient(Client, "GLINE %s %ld :%s",
174 Lists_GetValidity(elem) - time(NULL),
175 Lists_GetReason(elem)))
177 elem = Lists_GetNext(elem);
183 head = Channel_GetListBans(c);
184 elem = Lists_GetFirst(head);
186 if (!IRC_WriteStrClient(Client, "MODE %s +b %s",
188 Lists_GetMask(elem))) {
191 elem = Lists_GetNext(elem);
195 head = Channel_GetListInvites(c);
196 elem = Lists_GetFirst(head);
198 if (!IRC_WriteStrClient(Client, "MODE %s +I %s",
200 Lists_GetMask(elem))) {
203 elem = Lists_GetNext(elem);
212 * Send CHANINFO commands to a new server (inform it about existing channels).
213 * @param Client New server
214 * @param Chan Channel
217 Send_CHANINFO(CLIENT * Client, CHANNEL * Chan)
223 Log(LOG_DEBUG, "Sending CHANINFO commands ...");
226 modes = Channel_Modes(Chan);
227 topic = Channel_Topic(Chan);
229 if (!*modes && !*topic)
232 has_k = Channel_HasMode(Chan, 'k');
233 has_l = Channel_HasMode(Chan, 'l');
236 if (!has_k && !has_l) {
238 /* "CHANINFO <chan> +<modes>" */
239 return IRC_WriteStrClient(Client, "CHANINFO %s +%s",
240 Channel_Name(Chan), modes);
242 /* "CHANINFO <chan> +<modes> :<topic>" */
243 return IRC_WriteStrClient(Client, "CHANINFO %s +%s :%s",
244 Channel_Name(Chan), modes, topic);
246 /* "CHANINFO <chan> +<modes> <key> <limit> :<topic>" */
247 return IRC_WriteStrClient(Client, "CHANINFO %s +%s %s %lu :%s",
248 Channel_Name(Chan), modes,
249 has_k ? Channel_Key(Chan) : "*",
250 has_l ? Channel_MaxUsers(Chan) : 0, topic);
251 } /* Send_CHANINFO */
256 * Handle ENDOFMOTD (376) numeric and login remote server.
257 * The peer is either an IRC server (no IRC+ protocol), or we got the
258 * ENDOFMOTD numeric from an IRC+ server. We have to register the new server.
261 IRC_Num_ENDOFMOTD(CLIENT * Client, UNUSED REQUEST * Req)
267 Client_SetType(Client, CLIENT_SERVER);
269 Log(LOG_NOTICE | LOG_snotice,
270 "Server \"%s\" registered (connection %d, 1 hop - direct link).",
271 Client_ID(Client), Client_Conn(Client));
273 /* Get highest hop count */
277 if (Client_Hops(c) > max_hops)
278 max_hops = Client_Hops(c);
282 /* Inform the new server about all other servers, and announce the
283 * new server to all the already registered ones. Important: we have
284 * to do this "in order" and can't introduce servers of which the
285 * "toplevel server" isn't known already. */
286 for (i = 0; i < (max_hops + 1); i++) {
287 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
288 if (Client_Type(c) != CLIENT_SERVER)
289 continue; /* not a server */
290 if (Client_Hops(c) != i)
291 continue; /* not actual "nesting level" */
292 if (c == Client || c == Client_ThisServer())
293 continue; /* that's us or the peer! */
295 if (!Announce_Server(Client, c))
300 /* Announce all the users to the new server */
303 if (Client_Type(c) == CLIENT_USER ||
304 Client_Type(c) == CLIENT_SERVICE) {
305 if (!Client_Announce(Client, Client_ThisServer(), c))
311 /* Announce all channels to the new server */
312 chan = Channel_First();
314 if (Channel_IsLocal(chan)) {
315 chan = Channel_Next(chan);
319 /* Send CHANINFO if the peer supports it */
320 if (Client_HasFlag(Client, 'C')) {
321 if (!Send_CHANINFO(Client, chan))
326 if (!Announce_Channel(Client, chan))
329 /* Get next channel ... */
330 chan = Channel_Next(chan);
334 if (Client_HasFlag(Client, 'L')) {
335 LogDebug("Synchronizing INVITE- and BAN-lists ...");
336 if (!Synchronize_Lists(Client))
341 if (!IRC_WriteStrClient(Client, "PING :%s",
342 Client_ID(Client_ThisServer())))
346 } /* IRC_Num_ENDOFMOTD */
349 * Handle ISUPPORT (005) numeric.
352 IRC_Num_ISUPPORT(CLIENT * Client, REQUEST * Req)
357 for (i = 1; i < Req->argc - 1; i++) {
359 value = strchr(key, '=');
365 if (strcmp("NICKLEN", key) == 0) {
366 if ((unsigned int)atol(value) == Conf_MaxNickLength - 1)
369 /* Nickname length settings are different! */
371 "Peer uses incompatible nickname length (%d/%d)! Disconnecting ...",
372 Conf_MaxNickLength - 1, atoi(value));
373 Conn_Close(Client_Conn(Client),
374 "Incompatible nickname length",
381 } /* IRC_Num_ISUPPORT */