2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2015 Alexander Barton (alex@barton.de) and Contributors.
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"
37 * Announce a channel and its users in the network.
40 Announce_Channel(CLIENT *Client, CHANNEL *Chan)
44 char str[COMMAND_LEN], *ptr;
47 /* Check features of remote server */
48 njoin = Conn_Options(Client_Conn(Client)) & CONN_RFC1459 ? false : true;
49 xop = Client_HasFlag(Client, 'X') ? true : false;
51 /* Get all the members of this channel */
52 cl2chan = Channel_FirstMember(Chan);
53 snprintf(str, sizeof(str), "NJOIN %s :", Channel_Name(Chan));
55 cl = Channel_GetClient(cl2chan);
59 /* RFC 2813: send NJOIN with nicknames and modes
60 * (if user is channel operator or has voice) */
61 if (str[strlen(str) - 1] != ':')
62 strlcat(str, ",", sizeof(str));
64 /* Prepare user prefix (ChanOp, voiced, ...) */
65 if (xop && Channel_UserHasMode(Chan, cl, 'q'))
66 strlcat(str, "~", sizeof(str));
67 if (xop && Channel_UserHasMode(Chan, cl, 'a'))
68 strlcat(str, "&", sizeof(str));
69 if (Channel_UserHasMode(Chan, cl, 'o'))
70 strlcat(str, "@", sizeof(str));
71 if (xop && Channel_UserHasMode(Chan, cl, 'h'))
72 strlcat(str, "%", sizeof(str));
73 if (Channel_UserHasMode(Chan, cl, 'v'))
74 strlcat(str, "+", sizeof(str));
76 strlcat(str, Client_ID(cl), sizeof(str));
78 /* Send the data if the buffer is "full" */
79 if (strlen(str) > (sizeof(str) - CLIENT_NICK_LEN - 8)) {
80 if (!IRC_WriteStrClient(Client, "%s", str))
82 snprintf(str, sizeof(str), "NJOIN %s :",
86 /* RFC 1459: no NJOIN, send JOIN and MODE */
87 if (!IRC_WriteStrClientPrefix(Client, cl, "JOIN %s",
90 ptr = Channel_UserModes(Chan, cl);
92 if (!IRC_WriteStrClientPrefix(Client, cl,
94 Channel_Name(Chan), ptr[0],
101 cl2chan = Channel_NextMember(Chan, cl2chan);
104 /* Data left in the buffer? */
105 if (str[strlen(str) - 1] != ':') {
106 /* Yes, send it ... */
107 if (!IRC_WriteStrClient(Client, "%s", str))
112 } /* Announce_Channel */
115 * Announce new server in the network
116 * @param Client New server
117 * @param Server Existing server in the network
120 Announce_Server(CLIENT * Client, CLIENT * Server)
124 if (Client_Conn(Server) > NONE) {
125 /* Announce the new server to the one already registered
126 * which is directly connected to the local server */
127 if (!IRC_WriteStrClient
128 (Server, "SERVER %s %d %d :%s", Client_ID(Client),
129 Client_Hops(Client) + 1, Client_MyToken(Client),
130 Client_Info(Client)))
134 if (Client_Hops(Server) == 1)
135 c = Client_ThisServer();
137 c = Client_TopServer(Server);
139 /* Inform new server about the one already registered in the network */
140 return IRC_WriteStrClientPrefix(Client, c, "SERVER %s %d %d :%s",
141 Client_ID(Server), Client_Hops(Server) + 1,
142 Client_MyToken(Server), Client_Info(Server));
143 } /* Announce_Server */
148 * Send a specific list to a remote server.
151 Send_List(CLIENT *Client, CHANNEL *Chan, struct list_head *Head, char Type)
153 struct list_elem *elem;
155 elem = Lists_GetFirst(Head);
157 if (!IRC_WriteStrClient(Client, "MODE %s +%c %s",
158 Channel_Name(Chan), Type,
159 Lists_GetMask(elem))) {
162 elem = Lists_GetNext(elem);
168 * Synchronize invite, ban, except, and G-Line lists between servers.
170 * @param Client New server.
171 * @return CONNECTED or DISCONNECTED.
174 Synchronize_Lists(CLIENT * Client)
177 struct list_head *head;
178 struct list_elem *elem;
181 assert(Client != NULL);
184 head = Class_GetList(CLASS_GLINE);
185 elem = Lists_GetFirst(head);
187 t = Lists_GetValidity(elem) - time(NULL);
188 if (!IRC_WriteStrClient(Client, "GLINE %s %ld :%s",
191 Lists_GetReason(elem)))
193 elem = Lists_GetNext(elem);
198 if (!Send_List(Client, c, Channel_GetListExcepts(c), 'e'))
200 if (!Send_List(Client, c, Channel_GetListBans(c), 'b'))
202 if (!Send_List(Client, c, Channel_GetListInvites(c), 'I'))
210 * Send CHANINFO commands to a new server (inform it about existing channels).
211 * @param Client New server
212 * @param Chan Channel
215 Send_CHANINFO(CLIENT * Client, CHANNEL * Chan)
217 char *modes, *topic, *key;
220 Log(LOG_DEBUG, "Sending CHANINFO commands for \"%s\" ...",
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 key = Channel_Key(Chan);
245 return IRC_WriteStrClient(Client, "CHANINFO %s +%s %s %lu :%s",
246 Channel_Name(Chan), modes,
247 has_k ? (key && *key ? key : "*") : "*",
248 has_l ? Channel_MaxUsers(Chan) : 0, topic);
249 } /* Send_CHANINFO */
254 * Handle ENDOFMOTD (376) numeric and login remote server.
255 * The peer is either an IRC server (no IRC+ protocol), or we got the
256 * ENDOFMOTD numeric from an IRC+ server. We have to register the new server.
259 IRC_Num_ENDOFMOTD(CLIENT * Client, UNUSED REQUEST * Req)
265 Client_SetType(Client, CLIENT_SERVER);
267 Log(LOG_NOTICE | LOG_snotice,
268 "Server \"%s\" registered (connection %d, 1 hop - direct link).",
269 Client_ID(Client), Client_Conn(Client));
271 /* Get highest hop count */
275 if (Client_Hops(c) > max_hops)
276 max_hops = Client_Hops(c);
280 /* Inform the new server about all other servers, and announce the
281 * new server to all the already registered ones. Important: we have
282 * to do this "in order" and can't introduce servers of which the
283 * "toplevel server" isn't known already. */
284 for (i = 0; i < (max_hops + 1); i++) {
285 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
286 if (Client_Type(c) != CLIENT_SERVER)
287 continue; /* not a server */
288 if (Client_Hops(c) != i)
289 continue; /* not actual "nesting level" */
290 if (c == Client || c == Client_ThisServer())
291 continue; /* that's us or the peer! */
293 if (!Announce_Server(Client, c))
298 /* Announce all the users to the new server */
301 if (Client_Type(c) == CLIENT_USER ||
302 Client_Type(c) == CLIENT_SERVICE) {
303 if (!Client_Announce(Client, Client_ThisServer(), c))
309 /* Announce all channels to the new server */
310 chan = Channel_First();
312 if (Channel_IsLocal(chan)) {
313 chan = Channel_Next(chan);
317 /* Send CHANINFO if the peer supports it */
318 if (Client_HasFlag(Client, 'C')) {
319 if (!Send_CHANINFO(Client, chan))
324 if (!Announce_Channel(Client, chan))
327 /* Get next channel ... */
328 chan = Channel_Next(chan);
332 if (Client_HasFlag(Client, 'L')) {
333 LogDebug("Synchronizing INVITE- and BAN-lists ...");
334 if (!Synchronize_Lists(Client))
339 if (!IRC_WriteStrClient(Client, "PING :%s",
340 Client_ID(Client_ThisServer())))
344 } /* IRC_Num_ENDOFMOTD */
347 * Handle ISUPPORT (005) numeric.
350 IRC_Num_ISUPPORT(CLIENT * Client, REQUEST * Req)
355 for (i = 1; i < Req->argc - 1; i++) {
357 value = strchr(key, '=');
363 if (strcmp("NICKLEN", key) == 0) {
364 if ((unsigned int)atol(value) == Conf_MaxNickLength - 1)
367 /* Nickname length settings are different! */
369 "Peer uses incompatible nickname length (%d/%d)! Disconnecting ...",
370 Conf_MaxNickLength - 1, atoi(value));
371 Conn_Close(Client_Conn(Client),
372 "Incompatible nickname length",
379 } /* IRC_Num_ISUPPORT */