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.
11 * Handlers for IRC numerics sent to the server
27 #include "conn-func.h"
30 #include "irc-write.h"
41 * Announce a channel and its users in the network.
44 Announce_Channel(CLIENT *Client, CHANNEL *Chan)
48 char str[LINE_LEN], *ptr;
51 if (Conn_Options(Client_Conn(Client)) & CONN_RFC1459)
56 /* Get all the members of this channel */
57 cl2chan = Channel_FirstMember(Chan);
58 snprintf(str, sizeof(str), "NJOIN %s :", Channel_Name(Chan));
60 cl = Channel_GetClient(cl2chan);
64 /* RFC 2813: send NJOIN with nick names and modes
65 * (if user is channel operator or has voice) */
66 if (str[strlen(str) - 1] != ':')
67 strlcat(str, ",", sizeof(str));
68 if (strchr(Channel_UserModes(Chan, cl), 'v'))
69 strlcat(str, "+", sizeof(str));
70 if (strchr(Channel_UserModes(Chan, cl), 'o'))
71 strlcat(str, "@", sizeof(str));
72 strlcat(str, Client_ID(cl), sizeof(str));
74 /* Send the data if the buffer is "full" */
75 if (strlen(str) > (LINE_LEN - CLIENT_NICK_LEN - 8)) {
76 if (!IRC_WriteStrClient(Client, "%s", str))
78 snprintf(str, sizeof(str), "NJOIN %s :",
82 /* RFC 1459: no NJOIN, send JOIN and MODE */
83 if (!IRC_WriteStrClientPrefix(Client, cl, "JOIN %s",
86 ptr = Channel_UserModes(Chan, cl);
88 if (!IRC_WriteStrClientPrefix(Client, cl,
90 Channel_Name(Chan), ptr[0],
97 cl2chan = Channel_NextMember(Chan, cl2chan);
100 /* Data left in the buffer? */
101 if (str[strlen(str) - 1] != ':') {
102 /* Yes, send it ... */
103 if (!IRC_WriteStrClient(Client, "%s", str))
108 } /* Announce_Channel */
112 * Announce new server in the network
113 * @param Client New server
114 * @param Server Existing server in the network
117 Announce_Server(CLIENT * Client, CLIENT * Server)
121 if (Client_Conn(Server) > NONE) {
122 /* Announce the new server to the one already registered
123 * which is directly connected to the local server */
124 if (!IRC_WriteStrClient
125 (Server, "SERVER %s %d %d :%s", Client_ID(Client),
126 Client_Hops(Client) + 1, Client_MyToken(Client),
127 Client_Info(Client)))
131 if (Client_Hops(Server) == 1)
132 c = Client_ThisServer();
134 c = Client_Introducer(Server);
136 /* Inform new server about the one already registered in the network */
137 return IRC_WriteStrClientPrefix(Client, c, "SERVER %s %d %d :%s",
138 Client_ID(Server), Client_Hops(Server) + 1,
139 Client_MyToken(Server), Client_Info(Server));
140 } /* Announce_Server */
144 * Announce existing user to a new server
145 * @param Client New server
146 * @param User Existing user in the network
149 Announce_User(CLIENT * Client, CLIENT * User)
154 conn = Client_Conn(Client);
155 if (Conn_Options(conn) & CONN_RFC1459) {
156 /* RFC 1459 mode: separate NICK and USER commands */
157 if (! Conn_WriteStr(conn, "NICK %s :%d",
158 Client_ID(User), Client_Hops(User) + 1))
160 if (! Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
161 Client_ID(User), Client_User(User),
162 Client_Hostname(User),
163 Client_ID(Client_Introducer(User)),
166 modes = Client_Modes(User);
168 return Conn_WriteStr(conn, ":%s MODE %s +%s",
169 Client_ID(User), Client_ID(User),
174 /* RFC 2813 mode: one combined NICK or SERVICE command */
175 if (Client_Type(User) == CLIENT_SERVICE
176 && strchr(Client_Flags(Client), 'S'))
177 return IRC_WriteStrClient(Client,
178 "SERVICE %s %d * +%s %d :%s", Client_Mask(User),
179 Client_MyToken(Client_Introducer(User)),
180 Client_Modes(User), Client_Hops(User) + 1,
183 return IRC_WriteStrClient(Client,
184 "NICK %s %d %s %s %d +%s :%s",
185 Client_ID(User), Client_Hops(User) + 1,
186 Client_User(User), Client_Hostname(User),
187 Client_MyToken(Client_Introducer(User)),
188 Client_Modes(User), Client_Info(User));
190 } /* Announce_User */
196 * Synchronize invite and ban lists between servers
197 * @param Client New server
200 Synchronize_Lists(CLIENT * Client)
203 struct list_head *head;
204 struct list_elem *elem;
206 assert(Client != NULL);
211 head = Channel_GetListBans(c);
212 elem = Lists_GetFirst(head);
214 if (!IRC_WriteStrClient(Client, "MODE %s +b %s",
216 Lists_GetMask(elem))) {
219 elem = Lists_GetNext(elem);
223 head = Channel_GetListInvites(c);
224 elem = Lists_GetFirst(head);
226 if (!IRC_WriteStrClient(Client, "MODE %s +I %s",
228 Lists_GetMask(elem))) {
231 elem = Lists_GetNext(elem);
241 * Send CHANINFO commands to a new server (inform it about existing channels).
242 * @param Client New server
243 * @param Chan Channel
246 Send_CHANINFO(CLIENT * Client, CHANNEL * Chan)
252 Log(LOG_DEBUG, "Sending CHANINFO commands ...");
255 modes = Channel_Modes(Chan);
256 topic = Channel_Topic(Chan);
258 if (!*modes && !*topic)
261 has_k = strchr(modes, 'k') != NULL;
262 has_l = strchr(modes, 'l') != NULL;
265 if (!has_k && !has_l) {
267 /* "CHANINFO <chan> +<modes>" */
268 return IRC_WriteStrClient(Client, "CHANINFO %s +%s",
269 Channel_Name(Chan), modes);
271 /* "CHANINFO <chan> +<modes> :<topic>" */
272 return IRC_WriteStrClient(Client, "CHANINFO %s +%s :%s",
273 Channel_Name(Chan), modes, topic);
275 /* "CHANINFO <chan> +<modes> <key> <limit> :<topic>" */
276 return IRC_WriteStrClient(Client, "CHANINFO %s +%s %s %lu :%s",
277 Channel_Name(Chan), modes,
278 has_k ? Channel_Key(Chan) : "*",
279 has_l ? Channel_MaxUsers(Chan) : 0, topic);
280 } /* Send_CHANINFO */
286 * Handle ENDOFMOTD (376) numeric and login remote server.
287 * The peer is either an IRC server (no IRC+ protocol), or we got the
288 * ENDOFMOTD numeric from an IRC+ server. We have to register the new server.
291 IRC_Num_ENDOFMOTD(CLIENT * Client, UNUSED REQUEST * Req)
297 Client_SetType(Client, CLIENT_SERVER);
299 Log(LOG_NOTICE | LOG_snotice,
300 "Server \"%s\" registered (connection %d, 1 hop - direct link).",
301 Client_ID(Client), Client_Conn(Client));
303 /* Get highest hop count */
307 if (Client_Hops(c) > max_hops)
308 max_hops = Client_Hops(c);
312 /* Inform the new server about all other servers, and announce the
313 * new server to all the already registered ones. Important: we have
314 * to do this "in order" and can't introduce servers of which the
315 * "toplevel server" isn't known already. */
316 for (i = 0; i < (max_hops + 1); i++) {
317 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
318 if (Client_Type(c) != CLIENT_SERVER)
319 continue; /* not a server */
320 if (Client_Hops(c) != i)
321 continue; /* not actual "nesting level" */
322 if (c == Client || c == Client_ThisServer())
323 continue; /* that's us or the peer! */
325 if (!Announce_Server(Client, c))
330 /* Announce all the users to the new server */
333 if (Client_Type(c) == CLIENT_USER ||
334 Client_Type(c) == CLIENT_SERVICE) {
335 if (!Announce_User(Client, c))
341 /* Announce all channels to the new server */
342 chan = Channel_First();
345 /* Send CHANINFO if the peer supports it */
346 if (strchr(Client_Flags(Client), 'C')) {
347 if (!Send_CHANINFO(Client, chan))
352 if (!Announce_Channel(Client, chan))
355 /* Get next channel ... */
356 chan = Channel_Next(chan);
360 if (strchr(Client_Flags(Client), 'L')) {
361 LogDebug("Synchronizing INVITE- and BAN-lists ...");
362 if (!Synchronize_Lists(Client))
368 } /* IRC_Num_ENDOFMOTD */
372 * Handle ISUPPORT (005) numeric.
375 IRC_Num_ISUPPORT(CLIENT * Client, REQUEST * Req)
380 for (i = 1; i < Req->argc - 1; i++) {
382 value = strchr(key, '=');
388 if (strcmp("NICKLEN", key) == 0) {
389 if ((unsigned int)atol(value) == Conf_MaxNickLength - 1)
392 /* Nick name length settings are different! */
394 "Peer uses incompatible nick name length (%d/%d)! Disconnecting ...",
395 Conf_MaxNickLength - 1, atoi(value));
396 Conn_Close(Client_Conn(Client),
397 "Incompatible nick name length",
404 } /* IRC_Num_ISUPPORT */