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 if (Conn_Options(Client_Conn(Client)) & CONN_RFC1459)
58 /* Get all the members of this channel */
59 cl2chan = Channel_FirstMember(Chan);
60 snprintf(str, sizeof(str), "NJOIN %s :", Channel_Name(Chan));
62 cl = Channel_GetClient(cl2chan);
66 /* RFC 2813: send NJOIN with nicknames and modes
67 * (if user is channel operator or has voice) */
68 if (str[strlen(str) - 1] != ':')
69 strlcat(str, ",", sizeof(str));
70 if (strchr(Channel_UserModes(Chan, cl), 'q'))
71 strlcat(str, "~", sizeof(str));
72 if (strchr(Channel_UserModes(Chan, cl), 'a'))
73 strlcat(str, "&", sizeof(str));
74 if (strchr(Channel_UserModes(Chan, cl), 'o'))
75 strlcat(str, "@", sizeof(str));
76 if (strchr(Channel_UserModes(Chan, cl), 'h'))
77 strlcat(str, "%", sizeof(str));
78 if (strchr(Channel_UserModes(Chan, cl), 'v'))
79 strlcat(str, "+", sizeof(str));
81 strlcat(str, Client_ID(cl), sizeof(str));
83 /* Send the data if the buffer is "full" */
84 if (strlen(str) > (LINE_LEN - CLIENT_NICK_LEN - 8)) {
85 if (!IRC_WriteStrClient(Client, "%s", str))
87 snprintf(str, sizeof(str), "NJOIN %s :",
91 /* RFC 1459: no NJOIN, send JOIN and MODE */
92 if (!IRC_WriteStrClientPrefix(Client, cl, "JOIN %s",
95 ptr = Channel_UserModes(Chan, cl);
97 if (!IRC_WriteStrClientPrefix(Client, cl,
99 Channel_Name(Chan), ptr[0],
106 cl2chan = Channel_NextMember(Chan, cl2chan);
109 /* Data left in the buffer? */
110 if (str[strlen(str) - 1] != ':') {
111 /* Yes, send it ... */
112 if (!IRC_WriteStrClient(Client, "%s", str))
117 } /* 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 */
153 * Announce existing user to a new server
154 * @param Client New server
155 * @param User Existing user in the network
158 Announce_User(CLIENT * Client, CLIENT * User)
163 conn = Client_Conn(Client);
164 if (Conn_Options(conn) & CONN_RFC1459) {
165 /* RFC 1459 mode: separate NICK and USER commands */
166 if (! Conn_WriteStr(conn, "NICK %s :%d",
167 Client_ID(User), Client_Hops(User) + 1))
169 if (! Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
170 Client_ID(User), Client_User(User),
171 Client_Hostname(User),
172 Client_ID(Client_Introducer(User)),
175 modes = Client_Modes(User);
177 return Conn_WriteStr(conn, ":%s MODE %s +%s",
178 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 return IRC_WriteStrClient(Client,
187 "SERVICE %s %d * +%s %d :%s", Client_Mask(User),
188 Client_MyToken(Client_Introducer(User)),
189 Client_Modes(User), Client_Hops(User) + 1,
192 return IRC_WriteStrClient(Client,
193 "NICK %s %d %s %s %d +%s :%s",
194 Client_ID(User), Client_Hops(User) + 1,
195 Client_User(User), Client_Hostname(User),
196 Client_MyToken(Client_Introducer(User)),
197 Client_Modes(User), Client_Info(User));
199 } /* Announce_User */
205 * Synchronize invite, ban, G- and K-Line lists between servers.
207 * @param Client New server.
208 * @return CONNECTED or DISCONNECTED.
211 Synchronize_Lists(CLIENT * Client)
214 struct list_head *head;
215 struct list_elem *elem;
217 assert(Client != NULL);
220 head = Class_GetList(CLASS_GLINE);
221 elem = Lists_GetFirst(head);
223 if (!IRC_WriteStrClient(Client, "GLINE %s %ld :%s",
225 Lists_GetValidity(elem) - time(NULL),
226 Lists_GetReason(elem)))
228 elem = Lists_GetNext(elem);
234 head = Channel_GetListBans(c);
235 elem = Lists_GetFirst(head);
237 if (!IRC_WriteStrClient(Client, "MODE %s +b %s",
239 Lists_GetMask(elem))) {
242 elem = Lists_GetNext(elem);
246 head = Channel_GetListInvites(c);
247 elem = Lists_GetFirst(head);
249 if (!IRC_WriteStrClient(Client, "MODE %s +I %s",
251 Lists_GetMask(elem))) {
254 elem = Lists_GetNext(elem);
264 * Send CHANINFO commands to a new server (inform it about existing channels).
265 * @param Client New server
266 * @param Chan Channel
269 Send_CHANINFO(CLIENT * Client, CHANNEL * Chan)
275 Log(LOG_DEBUG, "Sending CHANINFO commands ...");
278 modes = Channel_Modes(Chan);
279 topic = Channel_Topic(Chan);
281 if (!*modes && !*topic)
284 has_k = strchr(modes, 'k') != NULL;
285 has_l = strchr(modes, 'l') != NULL;
288 if (!has_k && !has_l) {
290 /* "CHANINFO <chan> +<modes>" */
291 return IRC_WriteStrClient(Client, "CHANINFO %s +%s",
292 Channel_Name(Chan), modes);
294 /* "CHANINFO <chan> +<modes> :<topic>" */
295 return IRC_WriteStrClient(Client, "CHANINFO %s +%s :%s",
296 Channel_Name(Chan), modes, topic);
298 /* "CHANINFO <chan> +<modes> <key> <limit> :<topic>" */
299 return IRC_WriteStrClient(Client, "CHANINFO %s +%s %s %lu :%s",
300 Channel_Name(Chan), modes,
301 has_k ? Channel_Key(Chan) : "*",
302 has_l ? Channel_MaxUsers(Chan) : 0, topic);
303 } /* Send_CHANINFO */
309 * Handle ENDOFMOTD (376) numeric and login remote server.
310 * The peer is either an IRC server (no IRC+ protocol), or we got the
311 * ENDOFMOTD numeric from an IRC+ server. We have to register the new server.
314 IRC_Num_ENDOFMOTD(CLIENT * Client, UNUSED REQUEST * Req)
320 Client_SetType(Client, CLIENT_SERVER);
322 Log(LOG_NOTICE | LOG_snotice,
323 "Server \"%s\" registered (connection %d, 1 hop - direct link).",
324 Client_ID(Client), Client_Conn(Client));
326 /* Get highest hop count */
330 if (Client_Hops(c) > max_hops)
331 max_hops = Client_Hops(c);
335 /* Inform the new server about all other servers, and announce the
336 * new server to all the already registered ones. Important: we have
337 * to do this "in order" and can't introduce servers of which the
338 * "toplevel server" isn't known already. */
339 for (i = 0; i < (max_hops + 1); i++) {
340 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
341 if (Client_Type(c) != CLIENT_SERVER)
342 continue; /* not a server */
343 if (Client_Hops(c) != i)
344 continue; /* not actual "nesting level" */
345 if (c == Client || c == Client_ThisServer())
346 continue; /* that's us or the peer! */
348 if (!Announce_Server(Client, c))
353 /* Announce all the users to the new server */
356 if (Client_Type(c) == CLIENT_USER ||
357 Client_Type(c) == CLIENT_SERVICE) {
358 if (!Announce_User(Client, c))
364 /* Announce all channels to the new server */
365 chan = Channel_First();
367 if (Channel_IsLocal(chan)) {
368 chan = Channel_Next(chan);
372 /* Send CHANINFO if the peer supports it */
373 if (strchr(Client_Flags(Client), 'C')) {
374 if (!Send_CHANINFO(Client, chan))
379 if (!Announce_Channel(Client, chan))
382 /* Get next channel ... */
383 chan = Channel_Next(chan);
387 if (strchr(Client_Flags(Client), 'L')) {
388 LogDebug("Synchronizing INVITE- and BAN-lists ...");
389 if (!Synchronize_Lists(Client))
394 if (!IRC_WriteStrClient(Client, "PING :%s",
395 Client_ID(Client_ThisServer())))
399 } /* IRC_Num_ENDOFMOTD */
403 * Handle ISUPPORT (005) numeric.
406 IRC_Num_ISUPPORT(CLIENT * Client, REQUEST * Req)
411 for (i = 1; i < Req->argc - 1; i++) {
413 value = strchr(key, '=');
419 if (strcmp("NICKLEN", key) == 0) {
420 if ((unsigned int)atol(value) == Conf_MaxNickLength - 1)
423 /* Nickname length settings are different! */
425 "Peer uses incompatible nickname length (%d/%d)! Disconnecting ...",
426 Conf_MaxNickLength - 1, atoi(value));
427 Conn_Close(Client_Conn(Client),
428 "Incompatible nickname length",
435 } /* IRC_Num_ISUPPORT */