2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2007 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 * IRC commands for server links
28 #include "conn-func.h"
32 #include "irc-write.h"
43 #include "irc-server.h"
47 * Handler for the IRC command "SERVER".
48 * See RFC 2813 section 4.1.2.
51 IRC_SERVER( CLIENT *Client, REQUEST *Req )
57 assert( Client != NULL );
58 assert( Req != NULL );
60 /* Return an error if this is not a local client */
61 if (Client_Conn(Client) <= NONE)
62 return IRC_WriteStrClient(Client, ERR_UNKNOWNCOMMAND_MSG,
63 Client_ID(Client), Req->command);
65 if (Client_Type(Client) == CLIENT_GOTPASS ||
66 Client_Type(Client) == CLIENT_GOTPASS_2813) {
67 /* We got a PASS command from the peer, and now a SERVER
68 * command: the peer tries to register itself as a server. */
69 LogDebug("Connection %d: got SERVER command (new server link) ...",
72 if (Req->argc != 2 && Req->argc != 3)
73 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
77 /* Get configuration index of new remote server ... */
78 for (i = 0; i < MAX_SERVERS; i++)
79 if (strcasecmp(Req->argv[0], Conf_Server[i].name) == 0)
82 /* Makre sure the remote server is configured here */
83 if (i >= MAX_SERVERS) {
85 "Connection %d: Server \"%s\" not configured here!",
86 Client_Conn(Client), Req->argv[0]);
87 Conn_Close(Client_Conn(Client), NULL,
88 "Server not configured here", true);
92 /* Check server password */
93 if (strcmp(Conn_Password(Client_Conn(Client)),
94 Conf_Server[i].pwd_in) != 0) {
96 "Connection %d: Got bad password from server \"%s\"!",
97 Client_Conn(Client), Req->argv[0]);
98 Conn_Close(Client_Conn(Client), NULL,
99 "Bad password", true);
103 /* Is there a registered server with this ID? */
104 if (!Client_CheckID(Client, Req->argv[0]))
107 /* Mark this connection as belonging to an configured server */
108 if (!Conf_SetServer(i, Client_Conn(Client)))
111 Client_SetID( Client, Req->argv[0] );
112 Client_SetHops( Client, 1 );
113 Client_SetInfo( Client, Req->argv[Req->argc - 1] );
115 /* Is this server registering on our side, or are we connecting to
116 * a remote server? */
117 if (Client_Token(Client) != TOKEN_OUTBOUND) {
118 /* Incoming connection, send user/pass */
119 if (!IRC_WriteStrClient(Client, "PASS %s %s",
120 Conf_Server[i].pwd_out,
122 || !IRC_WriteStrClient(Client, "SERVER %s 1 :%s",
125 Conn_Close(Client_Conn(Client),
126 "Unexpected server behavior!",
130 Client_SetIntroducer(Client, Client);
131 Client_SetToken(Client, 1);
133 /* outgoing connect, we already sent a SERVER and PASS
134 * command to the peer */
135 Client_SetToken(Client, atoi(Req->argv[1]));
138 /* Check protocol level */
139 if (Client_Type(Client) == CLIENT_GOTPASS) {
140 /* We got a "simple" PASS command, so the peer is
141 * using the protocol as defined in RFC 1459. */
142 if (!Conn_Options(Client_Conn(Client)) & CONN_RFC1459)
144 "Switching connection %d (\"%s\") to RFC 1459 compatibility mode.",
145 Client_Conn(Client), Client_ID(Client));
146 Conn_SetOption(Client_Conn(Client), CONN_RFC1459);
149 Client_SetType(Client, CLIENT_UNKNOWNSERVER);
152 if (strchr(Client_Flags(Client), 'Z')
153 && !Zip_InitConn(Client_Conn(Client))) {
154 Conn_Close(Client_Conn(Client),
155 "Can't inizialize compression (zlib)!",
162 if (strchr(Client_Flags(Client), 'H')) {
163 LogDebug("Peer supports IRC+ extended server handshake ...");
164 if (!IRC_Send_ISUPPORT(Client))
166 return IRC_WriteStrClient(Client, RPL_ENDOFMOTD_MSG,
170 if (Conf_MaxNickLength != CLIENT_NICK_LEN_DEFAULT)
172 "Attention: this server uses a non-standard nick length, but the peer doesn't support the IRC+ extended server handshake!");
177 return IRC_Num_ENDOFMOTD(Client, Req);
179 else if( Client_Type( Client ) == CLIENT_SERVER )
181 /* New server is being introduced to the network */
183 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
185 /* check for existing server with same ID */
186 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
188 from = Client_Search( Req->prefix );
191 /* Uh, Server, that introduced the new server is unknown?! */
192 Log( LOG_ALERT, "Unknown ID in prefix of SERVER: \"%s\"! (on connection %d)", Req->prefix, Client_Conn( Client ));
193 Conn_Close( Client_Conn( Client ), NULL, "Unknown ID in prefix of SERVER", true);
197 c = Client_NewRemoteServer(Client, Req->argv[0], from, atoi(Req->argv[1]), atoi(Req->argv[2]), Req->argv[3], true);
199 Log( LOG_ALERT, "Can't create client structure for server! (on connection %d)", Client_Conn( Client ));
200 Conn_Close( Client_Conn( Client ), NULL, "Can't allocate client structure for remote server", true);
204 if(( Client_Hops( c ) > 1 ) && ( Req->prefix[0] )) snprintf( str, sizeof( str ), "connected to %s, ", Client_ID( from ));
205 else strcpy( str, "" );
206 Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" registered (via %s, %s%d hop%s).", Client_ID( c ), Client_ID( Client ), str, Client_Hops( c ), Client_Hops( c ) > 1 ? "s": "" );
208 /* notify other servers */
209 IRC_WriteStrServersPrefix( Client, from, "SERVER %s %d %d :%s", Client_ID( c ), Client_Hops( c ) + 1, Client_MyToken( c ), Client_Info( c ));
213 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
214 Client_ID(Client), Req->command);
219 IRC_NJOIN( CLIENT *Client, REQUEST *Req )
221 char nick_in[COMMAND_LEN], nick_out[COMMAND_LEN], *channame, *ptr, modes[8];
222 bool is_owner, is_chanadmin, is_op, is_halfop, is_voiced;
226 assert( Client != NULL );
227 assert( Req != NULL );
229 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
231 strlcpy( nick_in, Req->argv[1], sizeof( nick_in ));
232 strcpy( nick_out, "" );
234 channame = Req->argv[0];
235 ptr = strtok( nick_in, "," );
238 is_owner = is_chanadmin = is_op = is_halfop = is_voiced = false;
240 /* cut off prefixes */
241 while(( *ptr == '~') || ( *ptr == '&' ) || ( *ptr == '@' ) ||
242 ( *ptr == '%') || ( *ptr == '+' ))
244 if( *ptr == '~' ) is_owner = true;
245 if( *ptr == '&' ) is_chanadmin = true;
246 if( *ptr == '@' ) is_op = true;
247 if( *ptr == 'h' ) is_halfop = true;
248 if( *ptr == '+' ) is_voiced = true;
252 c = Client_Search( ptr );
255 Channel_Join( c, channame );
256 chan = Channel_Search( channame );
257 assert( chan != NULL );
259 if( is_owner ) Channel_UserModeAdd( chan, c, 'q' );
260 if( is_chanadmin ) Channel_UserModeAdd( chan, c, 'a' );
261 if( is_op ) Channel_UserModeAdd( chan, c, 'o' );
262 if( is_halfop ) Channel_UserModeAdd( chan, c, 'h' );
263 if( is_voiced ) Channel_UserModeAdd( chan, c, 'v' );
265 /* announce to channel... */
266 IRC_WriteStrChannelPrefix( Client, chan, c, false, "JOIN :%s", channame );
268 /* set Channel-User-Modes */
269 strlcpy( modes, Channel_UserModes( chan, c ), sizeof( modes ));
272 /* send modes to channel */
273 IRC_WriteStrChannelPrefix( Client, chan, Client, false, "MODE %s +%s %s", channame, modes, Client_ID( c ));
276 if( nick_out[0] != '\0' ) strlcat( nick_out, ",", sizeof( nick_out ));
277 if( is_owner ) strlcat( nick_out, "~", sizeof( nick_out ));
278 if( is_chanadmin ) strlcat( nick_out, "&", sizeof( nick_out ));
279 if( is_op ) strlcat( nick_out, "@", sizeof( nick_out ));
280 if( is_halfop ) strlcat( nick_out, "%", sizeof( nick_out ));
281 if( is_voiced ) strlcat( nick_out, "+", sizeof( nick_out ));
282 strlcat( nick_out, ptr, sizeof( nick_out ));
284 else Log( LOG_ERR, "Got NJOIN for unknown nick \"%s\" for channel \"%s\"!", ptr, channame );
286 /* search for next Nick */
287 ptr = strtok( NULL, "," );
290 /* forward to other servers */
291 if( nick_out[0] != '\0' ) IRC_WriteStrServersPrefix( Client, Client_ThisServer( ), "NJOIN %s :%s", Req->argv[0], nick_out );
298 * Handler for the IRC command "SQUIT".
299 * See RFC 2813 section 4.1.2 and RFC 2812 section 3.1.8.
302 IRC_SQUIT(CLIENT * Client, REQUEST * Req)
304 char msg[COMMAND_LEN], logmsg[COMMAND_LEN];
305 CLIENT *from, *target;
309 assert(Client != NULL);
312 if (Client_Type(Client) != CLIENT_SERVER
313 && !Client_HasMode(Client, 'o'))
314 return Op_NoPrivileges(Client, Req);
316 /* Bad number of arguments? */
318 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
319 Client_ID(Client), Req->command);
321 if (Client_Type(Client) == CLIENT_SERVER && Req->prefix) {
322 from = Client_Search(Req->prefix);
323 if (Client_Type(from) != CLIENT_SERVER
324 && !Op_Check(Client, Req))
325 return Op_NoPrivileges(Client, Req);
329 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
330 Client_ID(Client), Req->prefix);
332 if (Client_Type(Client) == CLIENT_USER)
333 loglevel = LOG_NOTICE | LOG_snotice;
335 loglevel = LOG_DEBUG;
336 Log(loglevel, "Got SQUIT from %s for \"%s\": \"%s\" ...",
337 Client_ID(from), Req->argv[0], Req->argv[1]);
339 target = Client_Search(Req->argv[0]);
340 if (Client_Type(Client) != CLIENT_SERVER &&
341 target == Client_ThisServer())
342 return Op_NoPrivileges(Client, Req);
344 /* The server is (already) unknown */
346 "Got SQUIT from %s for unknown server \"%s\"!?",
347 Client_ID(Client), Req->argv[0]);
351 con = Client_Conn(target);
354 if (Client_NextHop(from) != Client || con > NONE)
355 snprintf(msg, sizeof(msg), "%s (SQUIT from %s)",
356 Req->argv[1], Client_ID(from));
358 strlcpy(msg, Req->argv[1], sizeof(msg));
360 snprintf(msg, sizeof(msg), "Got SQUIT from %s",
364 /* We are directly connected to the target server, so we
365 * have to tear down the connection and to inform all the
366 * other remaining servers in the network */
367 IRC_SendWallops(Client_ThisServer(), Client_ThisServer(),
368 "Received SQUIT %s from %s: %s",
369 Req->argv[0], Client_ID(from),
370 Req->argv[1][0] ? Req->argv[1] : "-");
371 Conn_Close(con, NULL, msg, true);
372 if (con == Client_Conn(Client))
375 /* This server is not directly connected, so the SQUIT must
376 * be forwarded ... */
377 if (Client_Type(from) != CLIENT_SERVER) {
378 /* The origin is not an IRC server, so don't evaluate
379 * this SQUIT but simply forward it */
380 IRC_WriteStrClientPrefix(Client_NextHop(target),
381 from, "SQUIT %s :%s", Req->argv[0], Req->argv[1]);
383 /* SQUIT has been generated by another server, so
384 * remove the target server from the network! */
386 if (!strchr(msg, '('))
387 snprintf(logmsg, sizeof(logmsg),
388 "%s (SQUIT from %s)", Req->argv[1],
390 Client_Destroy(target, logmsg[0] ? logmsg : msg,