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 new server in the network
42 * @param Client New server
43 * @param Server Existing server in the network
46 Announce_Server(CLIENT * Client, CLIENT * Server)
50 if (Client_Conn(Server) > NONE) {
51 /* Announce the new server to the one already registered
52 * which is directly connected to the local server */
53 if (!IRC_WriteStrClient
54 (Server, "SERVER %s %d %d :%s", Client_ID(Client),
55 Client_Hops(Client) + 1, Client_MyToken(Client),
60 if (Client_Hops(Server) == 1)
61 c = Client_ThisServer();
63 c = Client_Introducer(Server);
65 /* Inform new server about the one already registered in the network */
66 return IRC_WriteStrClientPrefix(Client, c, "SERVER %s %d %d :%s",
67 Client_ID(Server), Client_Hops(Server) + 1,
68 Client_MyToken(Server), Client_Info(Server));
69 } /* Announce_Server */
73 * Announce existing user to a new server
74 * @param Client New server
75 * @param User Existing user in the network
78 Announce_User(CLIENT * Client, CLIENT * User)
83 conn = Client_Conn(Client);
84 if (Conn_Options(conn) & CONN_RFC1459) {
85 /* RFC 1459 mode: separate NICK and USER commands */
86 if (! Conn_WriteStr(conn, "NICK %s :%d",
87 Client_ID(User), Client_Hops(User) + 1))
89 if (! Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
90 Client_ID(User), Client_User(User),
91 Client_Hostname(User),
92 Client_ID(Client_Introducer(User)),
95 modes = Client_Modes(User);
97 return Conn_WriteStr(conn, ":%s MODE %s +%s",
98 Client_ID(User), Client_ID(User),
103 /* RFC 2813 mode: one combined NICK or SERVICE command */
104 if (Client_Type(User) == CLIENT_SERVICE
105 && strchr(Client_Flags(Client), 'S'))
106 return IRC_WriteStrClient(Client,
107 "SERVICE %s %d * +%s %d :%s", Client_Mask(User),
108 Client_MyToken(Client_Introducer(User)),
109 Client_Modes(User), Client_Hops(User) + 1,
112 return IRC_WriteStrClient(Client,
113 "NICK %s %d %s %s %d +%s :%s",
114 Client_ID(User), Client_Hops(User) + 1,
115 Client_User(User), Client_Hostname(User),
116 Client_MyToken(Client_Introducer(User)),
117 Client_Modes(User), Client_Info(User));
119 } /* Announce_User */
125 * Synchronize invite and ban lists between servers
126 * @param Client New server
129 Synchronize_Lists(CLIENT * Client)
132 struct list_head *head;
133 struct list_elem *elem;
135 assert(Client != NULL);
140 head = Channel_GetListBans(c);
141 elem = Lists_GetFirst(head);
143 if (!IRC_WriteStrClient(Client, "MODE %s +b %s",
145 Lists_GetMask(elem))) {
148 elem = Lists_GetNext(elem);
152 head = Channel_GetListInvites(c);
153 elem = Lists_GetFirst(head);
155 if (!IRC_WriteStrClient(Client, "MODE %s +I %s",
157 Lists_GetMask(elem))) {
160 elem = Lists_GetNext(elem);
170 * Send CHANINFO commands to a new server (inform it about existing channels).
171 * @param Client New server
172 * @param Chan Channel
175 Send_CHANINFO(CLIENT * Client, CHANNEL * Chan)
181 Log(LOG_DEBUG, "Sending CHANINFO commands ...");
184 modes = Channel_Modes(Chan);
185 topic = Channel_Topic(Chan);
187 if (!*modes && !*topic)
190 has_k = strchr(modes, 'k') != NULL;
191 has_l = strchr(modes, 'l') != NULL;
194 if (!has_k && !has_l) {
196 /* "CHANINFO <chan> +<modes>" */
197 return IRC_WriteStrClient(Client, "CHANINFO %s +%s",
198 Channel_Name(Chan), modes);
200 /* "CHANINFO <chan> +<modes> :<topic>" */
201 return IRC_WriteStrClient(Client, "CHANINFO %s +%s :%s",
202 Channel_Name(Chan), modes, topic);
204 /* "CHANINFO <chan> +<modes> <key> <limit> :<topic>" */
205 return IRC_WriteStrClient(Client, "CHANINFO %s +%s %s %lu :%s",
206 Channel_Name(Chan), modes,
207 has_k ? Channel_Key(Chan) : "*",
208 has_l ? Channel_MaxUsers(Chan) : 0, topic);
209 } /* Send_CHANINFO */
215 * Handle ENDOFMOTD (376) numeric and login remote server.
216 * The peer is either an IRC server (no IRC+ protocol), or we got the
217 * ENDOFMOTD numeric from an IRC+ server. We have to register the new server.
220 IRC_Num_ENDOFMOTD(CLIENT * Client, UNUSED REQUEST * Req)
228 Client_SetType(Client, CLIENT_SERVER);
230 Log(LOG_NOTICE | LOG_snotice,
231 "Server \"%s\" registered (connection %d, 1 hop - direct link).",
232 Client_ID(Client), Client_Conn(Client));
234 /* Get highest hop count */
238 if (Client_Hops(c) > max_hops)
239 max_hops = Client_Hops(c);
243 /* Inform the new server about all other servers, and announce the
244 * new server to all the already registered ones. Important: we have
245 * to do this "in order" and can't introduce servers of which the
246 * "toplevel server" isn't known already. */
247 for (i = 0; i < (max_hops + 1); i++) {
248 for (c = Client_First(); c != NULL; c = Client_Next(c)) {
249 if (Client_Type(c) != CLIENT_SERVER)
250 continue; /* not a server */
251 if (Client_Hops(c) != i)
252 continue; /* not actual "nesting level" */
253 if (c == Client || c == Client_ThisServer())
254 continue; /* that's us or the peer! */
256 if (!Announce_Server(Client, c))
261 /* Announce all the users to the new server */
264 if (Client_Type(c) == CLIENT_USER ||
265 Client_Type(c) == CLIENT_SERVICE) {
266 if (!Announce_User(Client, c))
272 /* Announce all channels to the new server */
273 chan = Channel_First();
276 /* Send CHANINFO if the peer supports it */
277 if (strchr(Client_Flags(Client), 'C')) {
278 if (!Send_CHANINFO(Client, chan))
283 /* Get all the members of this channel */
284 cl2chan = Channel_FirstMember(chan);
285 snprintf(str, sizeof(str), "NJOIN %s :", Channel_Name(chan));
287 cl = Channel_GetClient(cl2chan);
290 /* Nick name, with modes (if applicable) */
291 if (str[strlen(str) - 1] != ':')
292 strlcat(str, ",", sizeof(str));
293 if (strchr(Channel_UserModes(chan, cl), 'v'))
294 strlcat(str, "+", sizeof(str));
295 if (strchr(Channel_UserModes(chan, cl), 'o'))
296 strlcat(str, "@", sizeof(str));
297 strlcat(str, Client_ID(cl), sizeof(str));
299 /* Send the data if the buffer is "full" */
300 if (strlen(str) > (LINE_LEN - CLIENT_NICK_LEN - 8)) {
301 if (!IRC_WriteStrClient(Client, "%s", str))
303 snprintf(str, sizeof(str), "NJOIN %s :",
307 cl2chan = Channel_NextMember(chan, cl2chan);
310 /* Data left in the buffer? */
311 if (str[strlen(str) - 1] != ':') {
312 /* Yes, send it ... */
313 if (!IRC_WriteStrClient(Client, "%s", str))
317 /* Get next channel ... */
318 chan = Channel_Next(chan);
322 if (strchr(Client_Flags(Client), 'L')) {
323 LogDebug("Synchronizing INVITE- and BAN-lists ...");
324 if (!Synchronize_Lists(Client))
330 } /* IRC_Num_ENDOFMOTD */
334 * Handle ISUPPORT (005) numeric.
337 IRC_Num_ISUPPORT(CLIENT * Client, REQUEST * Req)
342 for (i = 1; i < Req->argc - 1; i++) {
344 value = strchr(key, '=');
350 if (strcmp("NICKLEN", key) == 0) {
351 if ((unsigned int)atol(value) == Conf_MaxNickLength - 1)
354 /* Nick name length settings are different! */
356 "Peer uses incompatible nick name length (%d/%d)! Disconnecting ...",
357 Conf_MaxNickLength - 1, atoi(value));
358 Conn_Close(Client_Conn(Client),
359 "Incompatible nick name length",
366 } /* IRC_Num_ISUPPORT */