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.
11 * IRC commands for server links
27 #include "conn-func.h"
32 #include "irc-write.h"
42 #include "irc-server.h"
46 * Handler for the IRC command "SERVER".
47 * See RFC 2813 section 4.1.2.
50 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 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
74 /* Ist this server configured on out side? */
75 for( i = 0; i < MAX_SERVERS; i++ ) if( strcasecmp( Req->argv[0], Conf_Server[i].name ) == 0 ) break;
76 if( i >= MAX_SERVERS )
78 Log( LOG_ERR, "Connection %d: Server \"%s\" not configured here!", Client_Conn( Client ), Req->argv[0] );
79 Conn_Close( Client_Conn( Client ), NULL, "Server not configured here", true);
82 if( strcmp( Client_Password( Client ), Conf_Server[i].pwd_in ) != 0 )
85 Log( LOG_ERR, "Connection %d: Got bad password from server \"%s\"!", Client_Conn( Client ), Req->argv[0] );
86 Conn_Close( Client_Conn( Client ), NULL, "Bad password", true);
90 /* Is there a registered server with this ID? */
91 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
93 Client_SetID( Client, Req->argv[0] );
94 Client_SetHops( Client, 1 );
95 Client_SetInfo( Client, Req->argv[Req->argc - 1] );
97 /* Is this server registering on our side, or are we connecting to
99 con = Client_Conn(Client);
100 if (Client_Token(Client) != TOKEN_OUTBOUND) {
101 /* Incoming connection, send user/pass */
102 if (!IRC_WriteStrClient(Client, "PASS %s %s",
103 Conf_Server[i].pwd_out,
105 || !IRC_WriteStrClient(Client, "SERVER %s 1 :%s",
108 Conn_Close(con, "Unexpected server behavior!",
112 Client_SetIntroducer(Client, Client);
113 Client_SetToken(Client, 1);
115 /* outgoing connect, we already sent a SERVER and PASS
116 * command to the peer */
117 Client_SetToken(Client, atoi(Req->argv[1]));
120 /* Mark this connection as belonging to an configured server */
121 Conf_SetServer(i, con);
123 /* Check protocol level */
124 if (Client_Type(Client) == CLIENT_GOTPASS) {
125 /* We got a "simple" PASS command, so the peer is
126 * using the protocol as defined in RFC 1459. */
127 if (! (Conn_Options(con) & CONN_RFC1459))
129 "Switching connection %d (\"%s\") to RFC 1459 compatibility mode.",
130 con, Client_ID(Client));
131 Conn_SetOption(con, CONN_RFC1459);
134 Client_SetType(Client, CLIENT_UNKNOWNSERVER);
137 if (strchr(Client_Flags(Client), 'Z') && !Zip_InitConn(con)) {
138 Conn_Close( con, "Can't inizialize compression (zlib)!", NULL, false );
144 if (strchr(Client_Flags(Client), 'H')) {
145 LogDebug("Peer supports IRC+ extended server handshake ...");
146 if (!IRC_Send_ISUPPORT(Client))
148 return IRC_WriteStrClient(Client, RPL_ENDOFMOTD_MSG,
152 if (Conf_MaxNickLength != CLIENT_NICK_LEN_DEFAULT)
154 "Attention: this server uses a non-standard nick length, but the peer doesn't support the IRC+ extended server handshake!");
159 return IRC_Num_ENDOFMOTD(Client, Req);
161 else if( Client_Type( Client ) == CLIENT_SERVER )
163 /* New server is being introduced to the network */
165 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
167 /* check for existing server with same ID */
168 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
170 from = Client_Search( Req->prefix );
173 /* Uh, Server, that introduced the new server is unknown?! */
174 Log( LOG_ALERT, "Unknown ID in prefix of SERVER: \"%s\"! (on connection %d)", Req->prefix, Client_Conn( Client ));
175 Conn_Close( Client_Conn( Client ), NULL, "Unknown ID in prefix of SERVER", true);
179 c = Client_NewRemoteServer(Client, Req->argv[0], from, atoi(Req->argv[1]), atoi(Req->argv[2]), Req->argv[3], true);
181 Log( LOG_ALERT, "Can't create client structure for server! (on connection %d)", Client_Conn( Client ));
182 Conn_Close( Client_Conn( Client ), NULL, "Can't allocate client structure for remote server", true);
186 if(( Client_Hops( c ) > 1 ) && ( Req->prefix[0] )) snprintf( str, sizeof( str ), "connected to %s, ", Client_ID( from ));
187 else strcpy( str, "" );
188 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": "" );
190 /* notify other servers */
191 IRC_WriteStrServersPrefix( Client, from, "SERVER %s %d %d :%s", Client_ID( c ), Client_Hops( c ) + 1, Client_MyToken( c ), Client_Info( c ));
195 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
196 Client_ID(Client), Req->command);
201 IRC_NJOIN( CLIENT *Client, REQUEST *Req )
203 char nick_in[COMMAND_LEN], nick_out[COMMAND_LEN], *channame, *ptr, modes[8];
204 bool is_op, is_voiced;
208 assert( Client != NULL );
209 assert( Req != NULL );
211 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
213 strlcpy( nick_in, Req->argv[1], sizeof( nick_in ));
214 strcpy( nick_out, "" );
216 channame = Req->argv[0];
217 ptr = strtok( nick_in, "," );
220 is_op = is_voiced = false;
222 /* cut off prefixes */
223 while(( *ptr == '@' ) || ( *ptr == '+' ))
225 if( *ptr == '@' ) is_op = true;
226 if( *ptr == '+' ) is_voiced = true;
230 c = Client_Search( ptr );
233 Channel_Join( c, channame );
234 chan = Channel_Search( channame );
235 assert( chan != NULL );
237 if( is_op ) Channel_UserModeAdd( chan, c, 'o' );
238 if( is_voiced ) Channel_UserModeAdd( chan, c, 'v' );
240 /* announce to channel... */
241 IRC_WriteStrChannelPrefix( Client, chan, c, false, "JOIN :%s", channame );
243 /* set Channel-User-Modes */
244 strlcpy( modes, Channel_UserModes( chan, c ), sizeof( modes ));
247 /* send modes to channel */
248 IRC_WriteStrChannelPrefix( Client, chan, Client, false, "MODE %s +%s %s", channame, modes, Client_ID( c ));
251 if( nick_out[0] != '\0' ) strlcat( nick_out, ",", sizeof( nick_out ));
252 if( is_op ) strlcat( nick_out, "@", sizeof( nick_out ));
253 if( is_voiced ) strlcat( nick_out, "+", sizeof( nick_out ));
254 strlcat( nick_out, ptr, sizeof( nick_out ));
256 else Log( LOG_ERR, "Got NJOIN for unknown nick \"%s\" for channel \"%s\"!", ptr, channame );
258 /* search for next Nick */
259 ptr = strtok( NULL, "," );
262 /* forward to other servers */
263 if( nick_out[0] != '\0' ) IRC_WriteStrServersPrefix( Client, Client_ThisServer( ), "NJOIN %s :%s", Req->argv[0], nick_out );
270 IRC_SQUIT( CLIENT *Client, REQUEST *Req )
273 char msg[LINE_LEN + 64];
275 assert( Client != NULL );
276 assert( Req != NULL );
278 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
280 Log( LOG_DEBUG, "Got SQUIT from %s for \"%s\": \"%s\" ...", Client_ID( Client ), Req->argv[0], Req->argv[1] );
282 target = Client_Search( Req->argv[0] );
285 Log( LOG_WARNING, "Got SQUIT from %s for unknown server \"%s\"!?", Client_ID( Client ), Req->argv[0] );
289 if( Req->argv[1][0] )
291 if( strlen( Req->argv[1] ) > LINE_LEN ) Req->argv[1][LINE_LEN] = '\0';
292 snprintf( msg, sizeof( msg ), "%s (SQUIT from %s).", Req->argv[1], Client_ID( Client ));
294 else snprintf( msg, sizeof( msg ), "Got SQUIT from %s.", Client_ID( Client ));
296 if( Client_Conn( target ) > NONE )
298 /* This server has the connection */
299 if( Req->argv[1][0] ) Conn_Close( Client_Conn( target ), msg, Req->argv[1], true);
300 else Conn_Close( Client_Conn( target ), msg, NULL, true);
305 /* connection was on another server */
306 Client_Destroy( target, msg, Req->argv[1], false );