#include "clientstream_test.h"

ClientStreamTest::ClientStreamTest(int argc, char ** argv) : TQApplication( argc, argv )
{
	// set up client stream
	myConnector = new KNetworkConnector( 0 );
	//myConnector->setOptHostPort( "localhost", 8300 );
	myConnector->setOptHostPort( "reiser.suse.de", 8300 );
	myConnector->setOptSSL( true );
	Q_ASSERT( TQCA::isSupported(TQCA::CAP_TLS) );
	myTLS = new TQCA::TLS;
	myTLSHandler = new TQCATLSHandler( myTLS );
	myTestObject = new ClientStream( myConnector, myTLSHandler, 0);
	// notify when the transport layer is connected
	connect( myTestObject, TQ_SIGNAL( connected() ), TQ_SLOT( slotConnected() ) );
	// it's necessary to catch this signal and tell the TLS handler to proceed, even if we don't check cert validity
	connect( myTLSHandler, TQ_SIGNAL(tlsHandshaken()), TQ_SLOT(slotTLSHandshaken()) );
	// notify and start sending
	connect( myTestObject, TQ_SIGNAL( securityLayerActivated(int) ), TQ_SLOT( slotSend(int) ) );
	connect( myTestObject, TQ_SIGNAL( warning(int) ), TQ_SLOT( slotWarning(int) ) );

	// do test once the event loop is running
	TQTimer::singleShot( 0, this, TQ_SLOT( slotDoTest() ) );
}

ClientStreamTest::~ClientStreamTest()
{
	delete myTestObject;
	delete myTLSHandler;
	delete myTLS;
	delete myConnector;
}

void ClientStreamTest::slotDoTest()
{
	NovellDN dn;
	dn.dn = "maeuschen";
	dn.server = "reiser.suse.de";
	// connect to server
	tqDebug( "connecting to server ");
	myTestObject->connectToServer( dn, true ); // fine up to here...
}

void ClientStreamTest::slotConnected()
{
	tqDebug( "connection is up");
}

void ClientStreamTest::slotWarning(int warning)
{
	tqDebug( "warning: %i", warning);
}

void ClientStreamTest::slotsend(int layer)
{
	tqDebug( "security layer is up: %i", layer);
	RequestFactory testFactory;
	// we're not connecting...
	tqDebug( "sending request" );
	// send a request
	TQCString command("login");
	Request * firstRequest = testFactory.request( command );
	Field::FieldList lst;
	lst.append( new Field::SingleField( NM_A_SZ_USERID, 0, NMFIELD_TYPE_UTF8, "maeuschen" ) );
	lst.append( new Field::SingleField( NM_A_SZ_CREDENTIALS, 0, NMFIELD_TYPE_UTF8, "maeuschen" ) );
	lst.append( new Field::SingleField( NM_A_SZ_USER_AGENT, 0, NMFIELD_TYPE_UTF8, "libgroupwise/0.1 (linux, 2.6.5-7.97-smp)" ) );
	lst.append( new Field::SingleField( NM_A_UD_BUILD, 0, NMFIELD_TYPE_UDWORD, 2 ) );
	lst.append( new Field::SingleField( NM_A_IP_ADDRESS, 0, NMFIELD_TYPE_UTF8, "10.10.11.103" ) );
	firstRequest->setFields( lst );
	myTestObject->write( firstRequest );
	tqDebug( "done");
}
	
void ClientStreamTest::slotTLSHandshaken()
{
	tqDebug( "TLS handshake complete" );
	int validityResult = myTLS->certificateValidityResult ();

	if( validityResult == TQCA::TLS::Valid )
	{
		tqDebug( "Certificate is valid, continuing.");
		// valid certificate, continue
		myTLSHandler->continueAfterHandshake ();
	}
	else
	{
		tqDebug( "Certificate is not valid, continuing" );
		// certificate is not valid, query the user
		/*			if(handleTLSWarning (validityResult, server (), myself()->contactId ()) == KMessageBox::Continue)
					{*/
		myTLSHandler->continueAfterHandshake ();
		/*			}
					else
					{
					disconnect ( Kopete::Account::Manual );
					}*/
	}

}
int main(int argc, char ** argv)
{
	ClientStreamTest a( argc, argv );
	a.exec();
	return 0;
}

#include "clientstream_test.moc"
