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
17 static char UNUSED id[] = "$Id: irc-server.c,v 1.46 2007/11/21 12:16:36 alex Exp $";
33 #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 )
53 char str[LINE_LEN], *ptr;
59 assert( Client != NULL );
60 assert( Req != NULL );
62 /* Return an error if this is not a local client */
63 if (Client_Conn(Client) <= NONE)
64 return IRC_WriteStrClient(Client, ERR_UNKNOWNCOMMAND_MSG,
65 Client_ID(Client), Req->command);
67 if (Client_Type(Client) == CLIENT_GOTPASS) {
68 /* We got a PASS command from the peer, and now a SERVER
69 * command: the peer tries to register itself as a server. */
70 LogDebug("Connection %d: got SERVER command (new server link) ...",
73 /* Falsche Anzahl Parameter? */
74 if(( Req->argc != 2 ) && ( Req->argc != 3 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
76 /* Ist dieser Server bei uns konfiguriert? */
77 for( i = 0; i < MAX_SERVERS; i++ ) if( strcasecmp( Req->argv[0], Conf_Server[i].name ) == 0 ) break;
78 if( i >= MAX_SERVERS )
80 /* Server ist nicht konfiguriert! */
81 Log( LOG_ERR, "Connection %d: Server \"%s\" not configured here!", Client_Conn( Client ), Req->argv[0] );
82 Conn_Close( Client_Conn( Client ), NULL, "Server not configured here", true);
85 if( strcmp( Client_Password( Client ), Conf_Server[i].pwd_in ) != 0 )
87 /* Falsches Passwort */
88 Log( LOG_ERR, "Connection %d: Got bad password from server \"%s\"!", Client_Conn( Client ), Req->argv[0] );
89 Conn_Close( Client_Conn( Client ), NULL, "Bad password", true);
93 /* Ist ein Server mit dieser ID bereits registriert? */
94 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
96 /* Server-Strukturen fuellen ;-) */
97 Client_SetID( Client, Req->argv[0] );
98 Client_SetHops( Client, 1 );
99 Client_SetInfo( Client, Req->argv[Req->argc - 1] );
101 /* Meldet sich der Server bei uns an (d.h., bauen nicht wir
102 * selber die Verbindung zu einem anderen Server auf)? */
103 con = Client_Conn( Client );
104 if( Client_Token( Client ) != TOKEN_OUTBOUND )
106 /* Eingehende Verbindung: Unseren SERVER- und PASS-Befehl senden */
108 if( ! IRC_WriteStrClient( Client, "PASS %s %s", Conf_Server[i].pwd_out, NGIRCd_ProtoID )) ok = false;
109 else ok = IRC_WriteStrClient( Client, "SERVER %s 1 :%s", Conf_ServerName, Conf_ServerInfo );
112 Conn_Close( con, "Unexpected server behavior!", NULL, false );
115 Client_SetIntroducer( Client, Client );
116 Client_SetToken( Client, 1 );
120 /* Ausgehende verbindung, SERVER und PASS wurden von uns bereits
121 * an die Gegenseite uerbermittelt */
122 Client_SetToken( Client, atoi( Req->argv[1] ));
125 /* Mark this connection as belonging to an configured server */
126 Conf_SetServer(i, con);
128 Client_SetType(Client, CLIENT_UNKNOWNSERVER);
131 /* Kompression initialisieren, wenn erforderlich */
132 if( strchr( Client_Flags( Client ), 'Z' ))
134 if( ! Zip_InitConn( con ))
137 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 /* Neuer Server wird im Netz angekuendigt */
165 /* Falsche Anzahl Parameter? */
166 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
168 /* Ist ein Server mit dieser ID bereits registriert? */
169 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
171 /* Ueberfluessige Hostnamen aus Info-Text entfernen */
172 ptr = strchr( Req->argv[3] + 2, '[' );
173 if( ! ptr ) ptr = Req->argv[3];
175 from = Client_Search( Req->prefix );
178 /* Hm, Server, der diesen einfuehrt, ist nicht bekannt!? */
179 Log( LOG_ALERT, "Unknown ID in prefix of SERVER: \"%s\"! (on connection %d)", Req->prefix, Client_Conn( Client ));
180 Conn_Close( Client_Conn( Client ), NULL, "Unknown ID in prefix of SERVER", true);
184 /* Neue Client-Struktur anlegen */
185 c = Client_NewRemoteServer( Client, Req->argv[0], from, atoi( Req->argv[1] ), atoi( Req->argv[2] ), ptr, true);
188 /* Neue Client-Struktur konnte nicht angelegt werden */
189 Log( LOG_ALERT, "Can't create client structure for server! (on connection %d)", Client_Conn( Client ));
190 Conn_Close( Client_Conn( Client ), NULL, "Can't allocate client structure for remote server", true);
194 /* Log-Meldung zusammenbauen und ausgeben */
195 if(( Client_Hops( c ) > 1 ) && ( Req->prefix[0] )) snprintf( str, sizeof( str ), "connected to %s, ", Client_ID( from ));
196 else strcpy( str, "" );
197 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": "" );
199 /* Andere Server informieren */
200 IRC_WriteStrServersPrefix( Client, from, "SERVER %s %d %d :%s", Client_ID( c ), Client_Hops( c ) + 1, Client_MyToken( c ), Client_Info( c ));
204 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
205 Client_ID(Client), Req->command);
210 IRC_NJOIN( CLIENT *Client, REQUEST *Req )
212 char nick_in[COMMAND_LEN], nick_out[COMMAND_LEN], *channame, *ptr, modes[8];
213 bool is_op, is_voiced;
217 assert( Client != NULL );
218 assert( Req != NULL );
220 /* Falsche Anzahl Parameter? */
221 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
223 strlcpy( nick_in, Req->argv[1], sizeof( nick_in ));
224 strcpy( nick_out, "" );
226 channame = Req->argv[0];
227 ptr = strtok( nick_in, "," );
230 is_op = is_voiced = false;
232 /* Prefixe abschneiden */
233 while(( *ptr == '@' ) || ( *ptr == '+' ))
235 if( *ptr == '@' ) is_op = true;
236 if( *ptr == '+' ) is_voiced = true;
240 c = Client_Search( ptr );
243 Channel_Join( c, channame );
244 chan = Channel_Search( channame );
245 assert( chan != NULL );
247 if( is_op ) Channel_UserModeAdd( chan, c, 'o' );
248 if( is_voiced ) Channel_UserModeAdd( chan, c, 'v' );
250 /* im Channel bekannt machen */
251 IRC_WriteStrChannelPrefix( Client, chan, c, false, "JOIN :%s", channame );
253 /* Channel-User-Modes setzen */
254 strlcpy( modes, Channel_UserModes( chan, c ), sizeof( modes ));
257 /* Modes im Channel bekannt machen */
258 IRC_WriteStrChannelPrefix( Client, chan, Client, false, "MODE %s +%s %s", channame, modes, Client_ID( c ));
261 if( nick_out[0] != '\0' ) strlcat( nick_out, ",", sizeof( nick_out ));
262 if( is_op ) strlcat( nick_out, "@", sizeof( nick_out ));
263 if( is_voiced ) strlcat( nick_out, "+", sizeof( nick_out ));
264 strlcat( nick_out, ptr, sizeof( nick_out ));
266 else Log( LOG_ERR, "Got NJOIN for unknown nick \"%s\" for channel \"%s\"!", ptr, channame );
268 /* naechsten Nick suchen */
269 ptr = strtok( NULL, "," );
272 /* an andere Server weiterleiten */
273 if( nick_out[0] != '\0' ) IRC_WriteStrServersPrefix( Client, Client_ThisServer( ), "NJOIN %s :%s", Req->argv[0], nick_out );
280 IRC_SQUIT( CLIENT *Client, REQUEST *Req )
283 char msg[LINE_LEN + 64];
285 assert( Client != NULL );
286 assert( Req != NULL );
288 /* Falsche Anzahl Parameter? */
289 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
291 Log( LOG_DEBUG, "Got SQUIT from %s for \"%s\": \"%s\" ...", Client_ID( Client ), Req->argv[0], Req->argv[1] );
293 target = Client_Search( Req->argv[0] );
296 /* Den Server kennen wir nicht (mehr), also nichts zu tun. */
297 Log( LOG_WARNING, "Got SQUIT from %s for unknown server \"%s\"!?", Client_ID( Client ), Req->argv[0] );
301 if( Req->argv[1][0] )
303 if( strlen( Req->argv[1] ) > LINE_LEN ) Req->argv[1][LINE_LEN] = '\0';
304 snprintf( msg, sizeof( msg ), "%s (SQUIT from %s).", Req->argv[1], Client_ID( Client ));
306 else snprintf( msg, sizeof( msg ), "Got SQUIT from %s.", Client_ID( Client ));
308 if( Client_Conn( target ) > NONE )
310 /* dieser Server hat die Connection */
311 if( Req->argv[1][0] ) Conn_Close( Client_Conn( target ), msg, Req->argv[1], true);
312 else Conn_Close( Client_Conn( target ), msg, NULL, true);
317 /* Verbindung hielt anderer Server */
318 Client_Destroy( target, msg, Req->argv[1], false );