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[LINE_LEN], *ptr;
53 /* Check features of remote server */
54 njoin = Conn_Options(Client_Conn(Client)) & CONN_RFC1459 ? false : true;
55 xop = strchr(Client_Flags(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 && strchr(Channel_UserModes(Chan, cl), 'q'))
72 strlcat(str, "~", sizeof(str));
73 if (xop && strchr(Channel_UserModes(Chan, cl), 'a'))
74 strlcat(str, "&", sizeof(str));
75 if (strchr(Channel_UserModes(Chan, cl), 'o'))
76 strlcat(str, "@", sizeof(str));
77 if (xop && strchr(Channel_UserModes(Chan, cl), 'h'))
78 strlcat(str, "%", sizeof(str));
79 if (strchr(Channel_UserModes(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) > (LINE_LEN - 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 */
154 * Announce existing user to a new server
155 * @param Client New server
156 * @param User Existing user in the network
159 Announce_User(CLIENT * Client, CLIENT * User)
164 conn = Client_Conn(Client);
165 if (Conn_Options(conn) & CONN_RFC1459) {
166 /* RFC 1459 mode: separate NICK and USER commands */
167 if (! Conn_WriteStr(conn, "NICK %s :%d",
168 Client_ID(User), Client_Hops(User) + 1))
170 if (! Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
171 Client_ID(User), Client_User(User),
172 Client_Hostname(User),
173 Client_ID(Client_Introducer(User)),
176 modes = Client_Modes(User);
178 return Conn_WriteStr(conn, ":%s MODE %s +%s",
179 Client_ID(User), Client_ID(User),
183 /* RFC 2813 mode: one combined NICK or SERVICE command */
184 if (Client_Type(User) == CLIENT_SERVICE
185 && strchr(Client_Flags(Client), 'S')) {
186 if (!IRC_WriteStrClient(Client,
187 "SERVICE %s %d * +%s %d :%s",
189 Client_MyToken(Client_Introducer(User)),
190 Client_Modes(User), Client_Hops(User) + 1,
194 if (!IRC_WriteStrClient(Client,
195 "NICK %s %d %s %s %d +%s :%s",
196 Client_ID(User), Client_Hops(User) + 1,
197 Client_User(User), Client_Hostname(User),
198 Client_MyToken(Client_Introducer(User)),
199 Client_Modes(User), Client_Info(User)))
204 if (strchr(Client_Flags(Client), 'M')) {
205 /* Synchronize metadata */
206 if (Client_HostnameCloaked(User)) {
207 if (!IRC_WriteStrClient(Client,
208 "METADATA %s cloakhost :%s",
210 Client_HostnameCloaked(User)))
216 } /* Announce_User */
222 * Synchronize invite, ban, G- and K-Line lists between servers.
224 * @param Client New server.
225 * @return CONNECTED or DISCONNECTED.
228 Synchronize_Lists(CLIENT * Client)
231 struct list_head *head;
232 struct list_elem *elem;
234 assert(Client != NULL);
237 head = Class_GetList(CLASS_GLINE);
238 elem = Lists_GetFirst(head);
240 if (!IRC_WriteStrClient(Client, "GLINE %s %ld :%s",
242 Lists_GetValidity(elem) - time(NULL),
243 Lists_GetReason(elem)))
245 elem = Lists_GetNext(elem);
251 head = Channel_GetListBans(c);
252 elem = Lists_GetFirst(head);
254 if (!IRC_WriteStrClient(Client, "MODE %s +b %s",
256 Lists_GetMask(elem))) {
259 elem = Lists_GetNext(elem);
263 head = Channel_GetListInvites(c);
264 elem = Lists_GetFirst(head);
266 if (!IRC_WriteStrClient(Client, "MODE %s +I %s",
268 Lists_GetMask(elem))) {
271 elem = Lists_GetNext(elem);
281 * Send CHANINFO commands to a new server (inform it about existing channels).
282 * @param Client New server
283 * @param Chan Channel
286 Send_CHANINFO(CLIENT * Client, CHANNEL * Chan)
292 Log(LOG_DEBUG, "Sending CHANINFO commands ...");
295 modes = Channel_Modes(Chan);
296 topic = Channel_Topic(Chan);
298 if (!*modes && !*topic)
301 has_k = strchr(modes, 'k') != NULL;
302 has_l = strchr(modes, 'l') != NULL;
305 if (!has_k && !has_l) {
307 /* "CHANINFO <chan> +<modes>" */
308 return IRC_WriteStrClient(Client, "CHANINFO %s +%s",
309 Channel_Name(Chan), modes);
311 /* "CHANINFO <chan> +<modes> :<topic>" */
312 return IRC_WriteStrClient(Client, "CHANINFO %s +%s :%s",
313 Channel_Name(Chan), modes, topic);
315 /* "CHANINFO <chan> +<modes> <key> <limit> :<topic>" */
316 return IRC_WriteStrClient(Client, "CHANINFO %s +%s %s %lu :%s",
317 Channel_Name(Chan), modes,
318 has_k ? Channel_Key(Chan) : "*",
319 has_l ? Channel_MaxUsers(Chan) : 0, topic);
320 } /* Send_CHANINFO */
326 * Handle ENDOFMOTD (376) numeric and login remote server.
327 * The peer is either an IRC server (no IRC+ protocol), or we got the
328 * ENDOFMOTD numeric from an IRC+ server. We have to register the new server.
331 IRC_Num_ENDOFMOTD(CLIENT * Client, UNUSED REQUEST * Req)
337 Client_SetType(Client, CLIENT_SERVER);
339 Log(LOG_NOTICE | LOG_snotice,
340 "Server \"%s\" registered (connection %d, 1 hop - direct link).",
341 Client_ID(Client), Client_Conn(Client));
343 /* Get highest hop count */
347 if (Client_Hops(c) > max_hops)
348 max_hops = Client_Hops(c);
352 /* Inform the new server about all other servers, and announce the
353 * new server to all the already registered ones. Important: we have
354 * to do this "in order" and can't introduce servers of which the
355 * "toplevel server" isn't known already. */
356 for (i = 0; i < (max_hops + 1); i++) {
357 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
358 if (Client_Type(c) != CLIENT_SERVER)
359 continue; /* not a server */
360 if (Client_Hops(c) != i)
361 continue; /* not actual "nesting level" */
362 if (c == Client || c == Client_ThisServer())
363 continue; /* that's us or the peer! */
365 if (!Announce_Server(Client, c))
370 /* Announce all the users to the new server */
373 if (Client_Type(c) == CLIENT_USER ||
374 Client_Type(c) == CLIENT_SERVICE) {
375 if (!Announce_User(Client, c))
381 /* Announce all channels to the new server */
382 chan = Channel_First();
384 if (Channel_IsLocal(chan)) {
385 chan = Channel_Next(chan);
389 /* Send CHANINFO if the peer supports it */
390 if (strchr(Client_Flags(Client), 'C')) {
391 if (!Send_CHANINFO(Client, chan))
396 if (!Announce_Channel(Client, chan))
399 /* Get next channel ... */
400 chan = Channel_Next(chan);
404 if (strchr(Client_Flags(Client), 'L')) {
405 LogDebug("Synchronizing INVITE- and BAN-lists ...");
406 if (!Synchronize_Lists(Client))
411 if (!IRC_WriteStrClient(Client, "PING :%s",
412 Client_ID(Client_ThisServer())))
416 } /* IRC_Num_ENDOFMOTD */
420 * Handle ISUPPORT (005) numeric.
423 IRC_Num_ISUPPORT(CLIENT * Client, REQUEST * Req)
428 for (i = 1; i < Req->argc - 1; i++) {
430 value = strchr(key, '=');
436 if (strcmp("NICKLEN", key) == 0) {
437 if ((unsigned int)atol(value) == Conf_MaxNickLength - 1)
440 /* Nickname length settings are different! */
442 "Peer uses incompatible nickname length (%d/%d)! Disconnecting ...",
443 Conf_MaxNickLength - 1, atoi(value));
444 Conn_Close(Client_Conn(Client),
445 "Incompatible nickname length",
452 } /* IRC_Num_ISUPPORT */