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 nick names 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), 'v'))
71 strlcat(str, "+", sizeof(str));
72 if (strchr(Channel_UserModes(Chan, cl), 'o'))
73 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) > (LINE_LEN - 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 */
114 * Announce new server in the network
115 * @param Client New server
116 * @param Server Existing server in the network
119 Announce_Server(CLIENT * Client, CLIENT * Server)
123 if (Client_Conn(Server) > NONE) {
124 /* Announce the new server to the one already registered
125 * which is directly connected to the local server */
126 if (!IRC_WriteStrClient
127 (Server, "SERVER %s %d %d :%s", Client_ID(Client),
128 Client_Hops(Client) + 1, Client_MyToken(Client),
129 Client_Info(Client)))
133 if (Client_Hops(Server) == 1)
134 c = Client_ThisServer();
136 c = Client_TopServer(Server);
138 /* Inform new server about the one already registered in the network */
139 return IRC_WriteStrClientPrefix(Client, c, "SERVER %s %d %d :%s",
140 Client_ID(Server), Client_Hops(Server) + 1,
141 Client_MyToken(Server), Client_Info(Server));
142 } /* Announce_Server */
146 * Announce existing user to a new server
147 * @param Client New server
148 * @param User Existing user in the network
151 Announce_User(CLIENT * Client, CLIENT * User)
156 conn = Client_Conn(Client);
157 if (Conn_Options(conn) & CONN_RFC1459) {
158 /* RFC 1459 mode: separate NICK and USER commands */
159 if (! Conn_WriteStr(conn, "NICK %s :%d",
160 Client_ID(User), Client_Hops(User) + 1))
162 if (! Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
163 Client_ID(User), Client_User(User),
164 Client_Hostname(User),
165 Client_ID(Client_Introducer(User)),
168 modes = Client_Modes(User);
170 return Conn_WriteStr(conn, ":%s MODE %s +%s",
171 Client_ID(User), Client_ID(User),
176 /* RFC 2813 mode: one combined NICK or SERVICE command */
177 if (Client_Type(User) == CLIENT_SERVICE
178 && strchr(Client_Flags(Client), 'S'))
179 return IRC_WriteStrClient(Client,
180 "SERVICE %s %d * +%s %d :%s", Client_Mask(User),
181 Client_MyToken(Client_Introducer(User)),
182 Client_Modes(User), Client_Hops(User) + 1,
185 return IRC_WriteStrClient(Client,
186 "NICK %s %d %s %s %d +%s :%s",
187 Client_ID(User), Client_Hops(User) + 1,
188 Client_User(User), Client_Hostname(User),
189 Client_MyToken(Client_Introducer(User)),
190 Client_Modes(User), Client_Info(User));
192 } /* Announce_User */
198 * Synchronize invite, ban, G- and K-Line lists between servers.
200 * @param Client New server.
201 * @return CONNECTED or DISCONNECTED.
204 Synchronize_Lists(CLIENT * Client)
207 struct list_head *head;
208 struct list_elem *elem;
210 assert(Client != NULL);
213 head = Class_GetList(CLASS_GLINE);
214 elem = Lists_GetFirst(head);
216 if (!IRC_WriteStrClient(Client, "GLINE %s %ld :%s",
218 Lists_GetValidity(elem) - time(NULL),
219 Lists_GetReason(elem)))
221 elem = Lists_GetNext(elem);
227 head = Channel_GetListBans(c);
228 elem = Lists_GetFirst(head);
230 if (!IRC_WriteStrClient(Client, "MODE %s +b %s",
232 Lists_GetMask(elem))) {
235 elem = Lists_GetNext(elem);
239 head = Channel_GetListInvites(c);
240 elem = Lists_GetFirst(head);
242 if (!IRC_WriteStrClient(Client, "MODE %s +I %s",
244 Lists_GetMask(elem))) {
247 elem = Lists_GetNext(elem);
257 * Send CHANINFO commands to a new server (inform it about existing channels).
258 * @param Client New server
259 * @param Chan Channel
262 Send_CHANINFO(CLIENT * Client, CHANNEL * Chan)
268 Log(LOG_DEBUG, "Sending CHANINFO commands ...");
271 modes = Channel_Modes(Chan);
272 topic = Channel_Topic(Chan);
274 if (!*modes && !*topic)
277 has_k = strchr(modes, 'k') != NULL;
278 has_l = strchr(modes, 'l') != NULL;
281 if (!has_k && !has_l) {
283 /* "CHANINFO <chan> +<modes>" */
284 return IRC_WriteStrClient(Client, "CHANINFO %s +%s",
285 Channel_Name(Chan), modes);
287 /* "CHANINFO <chan> +<modes> :<topic>" */
288 return IRC_WriteStrClient(Client, "CHANINFO %s +%s :%s",
289 Channel_Name(Chan), modes, topic);
291 /* "CHANINFO <chan> +<modes> <key> <limit> :<topic>" */
292 return IRC_WriteStrClient(Client, "CHANINFO %s +%s %s %lu :%s",
293 Channel_Name(Chan), modes,
294 has_k ? Channel_Key(Chan) : "*",
295 has_l ? Channel_MaxUsers(Chan) : 0, topic);
296 } /* Send_CHANINFO */
302 * Handle ENDOFMOTD (376) numeric and login remote server.
303 * The peer is either an IRC server (no IRC+ protocol), or we got the
304 * ENDOFMOTD numeric from an IRC+ server. We have to register the new server.
307 IRC_Num_ENDOFMOTD(CLIENT * Client, UNUSED REQUEST * Req)
313 Client_SetType(Client, CLIENT_SERVER);
315 Log(LOG_NOTICE | LOG_snotice,
316 "Server \"%s\" registered (connection %d, 1 hop - direct link).",
317 Client_ID(Client), Client_Conn(Client));
319 /* Get highest hop count */
323 if (Client_Hops(c) > max_hops)
324 max_hops = Client_Hops(c);
328 /* Inform the new server about all other servers, and announce the
329 * new server to all the already registered ones. Important: we have
330 * to do this "in order" and can't introduce servers of which the
331 * "toplevel server" isn't known already. */
332 for (i = 0; i < (max_hops + 1); i++) {
333 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
334 if (Client_Type(c) != CLIENT_SERVER)
335 continue; /* not a server */
336 if (Client_Hops(c) != i)
337 continue; /* not actual "nesting level" */
338 if (c == Client || c == Client_ThisServer())
339 continue; /* that's us or the peer! */
341 if (!Announce_Server(Client, c))
346 /* Announce all the users to the new server */
349 if (Client_Type(c) == CLIENT_USER ||
350 Client_Type(c) == CLIENT_SERVICE) {
351 if (!Announce_User(Client, c))
357 /* Announce all channels to the new server */
358 chan = Channel_First();
360 if (Channel_IsLocal(chan)) {
361 chan = Channel_Next(chan);
365 /* Send CHANINFO if the peer supports it */
366 if (strchr(Client_Flags(Client), 'C')) {
367 if (!Send_CHANINFO(Client, chan))
372 if (!Announce_Channel(Client, chan))
375 /* Get next channel ... */
376 chan = Channel_Next(chan);
380 if (strchr(Client_Flags(Client), 'L')) {
381 LogDebug("Synchronizing INVITE- and BAN-lists ...");
382 if (!Synchronize_Lists(Client))
387 if (!IRC_WriteStrClient(Client, "PING :%s",
388 Client_ID(Client_ThisServer())))
392 } /* IRC_Num_ENDOFMOTD */
396 * Handle ISUPPORT (005) numeric.
399 IRC_Num_ISUPPORT(CLIENT * Client, REQUEST * Req)
404 for (i = 1; i < Req->argc - 1; i++) {
406 value = strchr(key, '=');
412 if (strcmp("NICKLEN", key) == 0) {
413 if ((unsigned int)atol(value) == Conf_MaxNickLength - 1)
416 /* Nick name length settings are different! */
418 "Peer uses incompatible nick name length (%d/%d)! Disconnecting ...",
419 Conf_MaxNickLength - 1, atoi(value));
420 Conn_Close(Client_Conn(Client),
421 "Incompatible nick name length",
428 } /* IRC_Num_ISUPPORT */