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
25 #include "conn-func.h"
29 #include "irc-write.h"
35 * Announce a channel and its users in the network.
38 Announce_Channel(CLIENT *Client, CHANNEL *Chan)
42 char str[COMMAND_LEN], *ptr;
45 /* Check features of remote server */
46 njoin = Conn_Options(Client_Conn(Client)) & CONN_RFC1459 ? false : true;
47 xop = Client_HasFlag(Client, 'X') ? true : false;
49 /* Get all the members of this channel */
50 cl2chan = Channel_FirstMember(Chan);
51 snprintf(str, sizeof(str), "NJOIN %s :", Channel_Name(Chan));
53 cl = Channel_GetClient(cl2chan);
57 /* RFC 2813: send NJOIN with nicknames and modes
58 * (if user is channel operator or has voice) */
59 if (str[strlen(str) - 1] != ':')
60 strlcat(str, ",", sizeof(str));
62 /* Prepare user prefix (ChanOp, voiced, ...) */
63 if (xop && Channel_UserHasMode(Chan, cl, 'q'))
64 strlcat(str, "~", sizeof(str));
65 if (xop && Channel_UserHasMode(Chan, cl, 'a'))
66 strlcat(str, "&", sizeof(str));
67 if (Channel_UserHasMode(Chan, cl, 'o'))
68 strlcat(str, "@", sizeof(str));
69 if (xop && Channel_UserHasMode(Chan, cl, 'h'))
70 strlcat(str, "%", sizeof(str));
71 if (Channel_UserHasMode(Chan, cl, 'v'))
72 strlcat(str, "+", sizeof(str));
74 strlcat(str, Client_ID(cl), sizeof(str));
76 /* Send the data if the buffer is "full" */
77 if (strlen(str) > (sizeof(str) - CLIENT_NICK_LEN - 8)) {
78 if (!IRC_WriteStrClient(Client, "%s", str))
80 snprintf(str, sizeof(str), "NJOIN %s :",
84 /* RFC 1459: no NJOIN, send JOIN and MODE */
85 if (!IRC_WriteStrClientPrefix(Client, cl, "JOIN %s",
88 ptr = Channel_UserModes(Chan, cl);
90 if (!IRC_WriteStrClientPrefix(Client, cl,
92 Channel_Name(Chan), ptr[0],
99 cl2chan = Channel_NextMember(Chan, cl2chan);
102 /* Data left in the buffer? */
103 if (str[strlen(str) - 1] != ':') {
104 /* Yes, send it ... */
105 if (!IRC_WriteStrClient(Client, "%s", str))
110 } /* 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 */
146 * Synchronize invite, ban, G- and K-Line lists between servers.
148 * @param Client New server.
149 * @return CONNECTED or DISCONNECTED.
152 Synchronize_Lists(CLIENT * Client)
155 struct list_head *head;
156 struct list_elem *elem;
158 assert(Client != NULL);
161 head = Class_GetList(CLASS_GLINE);
162 elem = Lists_GetFirst(head);
164 if (!IRC_WriteStrClient(Client, "GLINE %s %ld :%s",
166 Lists_GetValidity(elem) - time(NULL),
167 Lists_GetReason(elem)))
169 elem = Lists_GetNext(elem);
175 head = Channel_GetListBans(c);
176 elem = Lists_GetFirst(head);
178 if (!IRC_WriteStrClient(Client, "MODE %s +b %s",
180 Lists_GetMask(elem))) {
183 elem = Lists_GetNext(elem);
187 head = Channel_GetListInvites(c);
188 elem = Lists_GetFirst(head);
190 if (!IRC_WriteStrClient(Client, "MODE %s +I %s",
192 Lists_GetMask(elem))) {
195 elem = Lists_GetNext(elem);
204 * Send CHANINFO commands to a new server (inform it about existing channels).
205 * @param Client New server
206 * @param Chan Channel
209 Send_CHANINFO(CLIENT * Client, CHANNEL * Chan)
215 Log(LOG_DEBUG, "Sending CHANINFO commands ...");
218 modes = Channel_Modes(Chan);
219 topic = Channel_Topic(Chan);
221 if (!*modes && !*topic)
224 has_k = Channel_HasMode(Chan, 'k');
225 has_l = Channel_HasMode(Chan, 'l');
228 if (!has_k && !has_l) {
230 /* "CHANINFO <chan> +<modes>" */
231 return IRC_WriteStrClient(Client, "CHANINFO %s +%s",
232 Channel_Name(Chan), modes);
234 /* "CHANINFO <chan> +<modes> :<topic>" */
235 return IRC_WriteStrClient(Client, "CHANINFO %s +%s :%s",
236 Channel_Name(Chan), modes, topic);
238 /* "CHANINFO <chan> +<modes> <key> <limit> :<topic>" */
239 return IRC_WriteStrClient(Client, "CHANINFO %s +%s %s %lu :%s",
240 Channel_Name(Chan), modes,
241 has_k ? Channel_Key(Chan) : "*",
242 has_l ? Channel_MaxUsers(Chan) : 0, topic);
243 } /* Send_CHANINFO */
248 * Handle ENDOFMOTD (376) numeric and login remote server.
249 * The peer is either an IRC server (no IRC+ protocol), or we got the
250 * ENDOFMOTD numeric from an IRC+ server. We have to register the new server.
253 IRC_Num_ENDOFMOTD(CLIENT * Client, UNUSED REQUEST * Req)
259 Client_SetType(Client, CLIENT_SERVER);
261 Log(LOG_NOTICE | LOG_snotice,
262 "Server \"%s\" registered (connection %d, 1 hop - direct link).",
263 Client_ID(Client), Client_Conn(Client));
265 /* Get highest hop count */
269 if (Client_Hops(c) > max_hops)
270 max_hops = Client_Hops(c);
274 /* Inform the new server about all other servers, and announce the
275 * new server to all the already registered ones. Important: we have
276 * to do this "in order" and can't introduce servers of which the
277 * "toplevel server" isn't known already. */
278 for (i = 0; i < (max_hops + 1); i++) {
279 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
280 if (Client_Type(c) != CLIENT_SERVER)
281 continue; /* not a server */
282 if (Client_Hops(c) != i)
283 continue; /* not actual "nesting level" */
284 if (c == Client || c == Client_ThisServer())
285 continue; /* that's us or the peer! */
287 if (!Announce_Server(Client, c))
292 /* Announce all the users to the new server */
295 if (Client_Type(c) == CLIENT_USER ||
296 Client_Type(c) == CLIENT_SERVICE) {
297 if (!Client_Announce(Client, Client_ThisServer(), c))
303 /* Announce all channels to the new server */
304 chan = Channel_First();
306 if (Channel_IsLocal(chan)) {
307 chan = Channel_Next(chan);
311 /* Send CHANINFO if the peer supports it */
312 if (Client_HasFlag(Client, 'C')) {
313 if (!Send_CHANINFO(Client, chan))
318 if (!Announce_Channel(Client, chan))
321 /* Get next channel ... */
322 chan = Channel_Next(chan);
326 if (Client_HasFlag(Client, 'L')) {
327 LogDebug("Synchronizing INVITE- and BAN-lists ...");
328 if (!Synchronize_Lists(Client))
333 if (!IRC_WriteStrClient(Client, "PING :%s",
334 Client_ID(Client_ThisServer())))
338 } /* IRC_Num_ENDOFMOTD */
341 * Handle ISUPPORT (005) numeric.
344 IRC_Num_ISUPPORT(CLIENT * Client, REQUEST * Req)
349 for (i = 1; i < Req->argc - 1; i++) {
351 value = strchr(key, '=');
357 if (strcmp("NICKLEN", key) == 0) {
358 if ((unsigned int)atol(value) == Conf_MaxNickLength - 1)
361 /* Nickname length settings are different! */
363 "Peer uses incompatible nickname length (%d/%d)! Disconnecting ...",
364 Conf_MaxNickLength - 1, atoi(value));
365 Conn_Close(Client_Conn(Client),
366 "Incompatible nickname length",
373 } /* IRC_Num_ISUPPORT */