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 )
52 char str[LINE_LEN], *ptr;
58 assert( Client != NULL );
59 assert( Req != NULL );
61 /* Return an error if this is not a local client */
62 if (Client_Conn(Client) <= NONE)
63 return IRC_WriteStrClient(Client, ERR_UNKNOWNCOMMAND_MSG,
64 Client_ID(Client), Req->command);
66 if (Client_Type(Client) == CLIENT_GOTPASS ||
67 Client_Type(Client) == CLIENT_GOTPASS_2813) {
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 /* Check protocol level */
129 if (Client_Type(Client) == CLIENT_GOTPASS) {
130 /* We got a "simple" PASS command, so the peer is
131 * using the protocol as defined in RFC 1459. */
132 if (! (Conn_Options(con) & CONN_RFC1459))
134 "Switching connection %d (\"%s\") to RFC 1459 compatibility mode.",
135 con, Client_ID(Client));
136 Conn_SetOption(con, CONN_RFC1459);
139 Client_SetType(Client, CLIENT_UNKNOWNSERVER);
142 /* Kompression initialisieren, wenn erforderlich */
143 if( strchr( Client_Flags( Client ), 'Z' ))
145 if( ! Zip_InitConn( con ))
148 Conn_Close( con, "Can't inizialize compression (zlib)!", NULL, false );
155 if (strchr(Client_Flags(Client), 'H')) {
156 LogDebug("Peer supports IRC+ extended server handshake ...");
157 if (!IRC_Send_ISUPPORT(Client))
159 return IRC_WriteStrClient(Client, RPL_ENDOFMOTD_MSG,
163 if (Conf_MaxNickLength != CLIENT_NICK_LEN_DEFAULT)
165 "Attention: this server uses a non-standard nick length, but the peer doesn't support the IRC+ extended server handshake!");
170 return IRC_Num_ENDOFMOTD(Client, Req);
172 else if( Client_Type( Client ) == CLIENT_SERVER )
174 /* Neuer Server wird im Netz angekuendigt */
176 /* Falsche Anzahl Parameter? */
177 if( Req->argc != 4 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
179 /* Ist ein Server mit dieser ID bereits registriert? */
180 if( ! Client_CheckID( Client, Req->argv[0] )) return DISCONNECTED;
182 /* Ueberfluessige Hostnamen aus Info-Text entfernen */
183 ptr = strchr( Req->argv[3] + 2, '[' );
184 if( ! ptr ) ptr = Req->argv[3];
186 from = Client_Search( Req->prefix );
189 /* Hm, Server, der diesen einfuehrt, ist nicht bekannt!? */
190 Log( LOG_ALERT, "Unknown ID in prefix of SERVER: \"%s\"! (on connection %d)", Req->prefix, Client_Conn( Client ));
191 Conn_Close( Client_Conn( Client ), NULL, "Unknown ID in prefix of SERVER", true);
195 /* Neue Client-Struktur anlegen */
196 c = Client_NewRemoteServer( Client, Req->argv[0], from, atoi( Req->argv[1] ), atoi( Req->argv[2] ), ptr, true);
199 /* Neue Client-Struktur konnte nicht angelegt werden */
200 Log( LOG_ALERT, "Can't create client structure for server! (on connection %d)", Client_Conn( Client ));
201 Conn_Close( Client_Conn( Client ), NULL, "Can't allocate client structure for remote server", true);
205 /* Log-Meldung zusammenbauen und ausgeben */
206 if(( Client_Hops( c ) > 1 ) && ( Req->prefix[0] )) snprintf( str, sizeof( str ), "connected to %s, ", Client_ID( from ));
207 else strcpy( str, "" );
208 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": "" );
210 /* Andere Server informieren */
211 IRC_WriteStrServersPrefix( Client, from, "SERVER %s %d %d :%s", Client_ID( c ), Client_Hops( c ) + 1, Client_MyToken( c ), Client_Info( c ));
215 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
216 Client_ID(Client), Req->command);
221 IRC_NJOIN( CLIENT *Client, REQUEST *Req )
223 char nick_in[COMMAND_LEN], nick_out[COMMAND_LEN], *channame, *ptr, modes[8];
224 bool is_op, is_voiced;
228 assert( Client != NULL );
229 assert( Req != NULL );
231 /* Falsche Anzahl Parameter? */
232 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
234 strlcpy( nick_in, Req->argv[1], sizeof( nick_in ));
235 strcpy( nick_out, "" );
237 channame = Req->argv[0];
238 ptr = strtok( nick_in, "," );
241 is_op = is_voiced = false;
243 /* Prefixe abschneiden */
244 while(( *ptr == '@' ) || ( *ptr == '+' ))
246 if( *ptr == '@' ) is_op = true;
247 if( *ptr == '+' ) is_voiced = true;
251 c = Client_Search( ptr );
254 Channel_Join( c, channame );
255 chan = Channel_Search( channame );
256 assert( chan != NULL );
258 if( is_op ) Channel_UserModeAdd( chan, c, 'o' );
259 if( is_voiced ) Channel_UserModeAdd( chan, c, 'v' );
261 /* im Channel bekannt machen */
262 IRC_WriteStrChannelPrefix( Client, chan, c, false, "JOIN :%s", channame );
264 /* Channel-User-Modes setzen */
265 strlcpy( modes, Channel_UserModes( chan, c ), sizeof( modes ));
268 /* Modes im Channel bekannt machen */
269 IRC_WriteStrChannelPrefix( Client, chan, Client, false, "MODE %s +%s %s", channame, modes, Client_ID( c ));
272 if( nick_out[0] != '\0' ) strlcat( nick_out, ",", sizeof( nick_out ));
273 if( is_op ) strlcat( nick_out, "@", sizeof( nick_out ));
274 if( is_voiced ) strlcat( nick_out, "+", sizeof( nick_out ));
275 strlcat( nick_out, ptr, sizeof( nick_out ));
277 else Log( LOG_ERR, "Got NJOIN for unknown nick \"%s\" for channel \"%s\"!", ptr, channame );
279 /* naechsten Nick suchen */
280 ptr = strtok( NULL, "," );
283 /* an andere Server weiterleiten */
284 if( nick_out[0] != '\0' ) IRC_WriteStrServersPrefix( Client, Client_ThisServer( ), "NJOIN %s :%s", Req->argv[0], nick_out );
291 IRC_SQUIT( CLIENT *Client, REQUEST *Req )
294 char msg[LINE_LEN + 64];
296 assert( Client != NULL );
297 assert( Req != NULL );
299 /* Falsche Anzahl Parameter? */
300 if( Req->argc != 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
302 Log( LOG_DEBUG, "Got SQUIT from %s for \"%s\": \"%s\" ...", Client_ID( Client ), Req->argv[0], Req->argv[1] );
304 target = Client_Search( Req->argv[0] );
307 /* Den Server kennen wir nicht (mehr), also nichts zu tun. */
308 Log( LOG_WARNING, "Got SQUIT from %s for unknown server \"%s\"!?", Client_ID( Client ), Req->argv[0] );
312 if( Req->argv[1][0] )
314 if( strlen( Req->argv[1] ) > LINE_LEN ) Req->argv[1][LINE_LEN] = '\0';
315 snprintf( msg, sizeof( msg ), "%s (SQUIT from %s).", Req->argv[1], Client_ID( Client ));
317 else snprintf( msg, sizeof( msg ), "Got SQUIT from %s.", Client_ID( Client ));
319 if( Client_Conn( target ) > NONE )
321 /* dieser Server hat die Connection */
322 if( Req->argv[1][0] ) Conn_Close( Client_Conn( target ), msg, Req->argv[1], true);
323 else Conn_Close( Client_Conn( target ), msg, NULL, true);
328 /* Verbindung hielt anderer Server */
329 Client_Destroy( target, msg, Req->argv[1], false );