您的位置:首页 > 运维架构

gloox登录账号(OPENfire服务器)

2014-09-06 18:46 991 查看
#include "stdafx.h"
#include "gloox/client.h"
#include "gloox/connectionlistener.h"
#include "gloox/disco.h"
#include "gloox/message.h"
#include "gloox/gloox.h"
#include "gloox/loghandler.h"
#include "gloox/tlshandler.h"
#include "gloox/tlsdefault.h"
#include "gloox/logsink.h"
#include "gloox/messagehandler.h"
#include "gloox/base64.h"
#include "gloox/stanza.h"
#include "gloox/lastactivity.h"
#include "gloox/connectiontcpclient.h"
#include "gloox/registrationhandler.h"
#include "gloox/registration.h"
#include "gloox/messageeventhandler.h"
#include "gloox/chatstatehandler.h"
#include "gloox/messagesessionhandler.h"
using namespace gloox;
#pragma  comment(lib,"gloox-1.0.lib")
#ifndef _WIN32
# include <unistd.h>
#endif
#include <stdio.h>
#include <string>
#if defined( WIN32 ) || defined( _WIN32 )
# include <windows.h>
#endif
using namespace gloox;

#include <stdio.h>
#include <locale.h>
#include <string>

#include <cstdio> // [s]print[f]

class MessageTest : public MessageSessionHandler, ConnectionListener, LogHandler,
	MessageEventHandler, MessageHandler, ChatStateHandler
{
public:
	MessageTest() : m_session( 0 ), m_reconnect( false ) {}

	virtual ~MessageTest() {}

	void start()
	{

		JID jid( <a target=_blank href="mailto:bojie2017@121.42.13.133">bojie2017@121.42.13.133</a> );//账号匿名+创建的服务器名字
		j = new Client( jid, "12345" );//账号密码
		j->registerConnectionListener( this );
		j->registerMessageSessionHandler( this, 0 );
		j->disco()->setVersion( "reconnectTest", GLOOX_VERSION, "Linux" );
		j->disco()->setIdentity( "client", "bot" );
		j->disco()->addFeature( XMLNS_CHAT_STATES );
		//       j->setTls( TLSDisabled );
		j->setCompression( false );
		j->setStreamManagement( true, true );
		StringList ca;
		ca.push_back( "/path/to/cacert.crt" );
		j->setCACerts( ca );

		j->logInstance().registerLogHandler( LogLevelDebug, LogAreaAll, this );

		//
		// this code connects to a jabber server through a SOCKS5 proxy
		//
		//       ConnectionSOCKS5Proxy* conn = new ConnectionSOCKS5Proxy( j,
		//                                   new ConnectionTCP( j->logInstance(),
		//                                                      "sockshost", 1080 ),
		//                                   j->logInstance(), "example.net" );
		//       conn->setProxyAuth( "socksuser", "sockspwd" );
		//       j->setConnectionImpl( conn );

		//
		// this code connects to a jabber server through a HTTP proxy through a SOCKS5 proxy
		//
		//       ConnectionTCP* conn0 = new ConnectionTCP( j->logInstance(), "old", 1080 );
		//       ConnectionSOCKS5Proxy* conn1 = new ConnectionSOCKS5Proxy( conn0, j->logInstance(), "old", 8080 );
		//       conn1->setProxyAuth( "socksuser", "sockspwd" );
		//       ConnectionHTTPProxy* conn2 = new ConnectionHTTPProxy( j, conn1, j->logInstance(), "jabber.cc" );
		//       conn2->setProxyAuth( "httpuser", "httppwd" );
		//       j->setConnectionImpl( conn2 );

		ConnectionError ce = ConnNoError;
		if( j->connect( false ) )
		{
			while( ce == ConnNoError )
			{
				ce = j->recv();
			}
			printf( "ce: %d\n", ce );
		}

		m_reconnect = true;
		ce = ConnNoError;
		if( j->connect( false ) )
		{
			while( ce == ConnNoError )
			{
				ce = j->recv();
			}
			printf( "ce: %d\n", ce );
		}

		delete( j );
	}

	virtual void onConnect()
	{
		printf( "connected!!!\n" );
	}

	virtual void onDisconnect( ConnectionError e )
	{
		printf( "message_test: disconnected: %d\n", e );
		if( e == ConnAuthenticationFailed )
			printf( "auth failed. reason: %d\n", j->authError() );
	}

	virtual bool onTLSConnect( const CertInfo& info )
	{
		time_t from( info.date_from );
		time_t to( info.date_to );

		printf( "status: %d\nissuer: %s\npeer: %s\nprotocol: %s\nmac: %s\ncipher: %s\ncompression: %s\n"
			"from: %s\nto: %s\n",
			info.status, info.issuer.c_str(), info.server.c_str(),
			info.protocol.c_str(), info.mac.c_str(), info.cipher.c_str(),
			info.compression.c_str(), ctime( &from ), ctime( &to ) );
		return true;
	}

	virtual void handleMessage( const Message& msg, MessageSession * /*session*/ )
	{
		printf( "type: %d, subject: %s, message: %s, thread id: %s\n", msg.subtype(),
			msg.subject().c_str(), msg.body().c_str(), msg.thread().c_str() );

		if( msg.body().substr( 0, 3 ) == "ack" ) // using substr() to work around some stupid clients
			j->ackStreamManagement();
		else if( msg.body().substr( 0, 3 ) == "req" )
			j->reqStreamManagement();
		else if( msg.body().substr( 0, 4 ) == "quit" )
			j->disconnect();
		else
		{
			std::string re = "You said:\n> " + msg.body() + "\nI like that statement.";
			m_session->send( re, gloox::EmptyString );
		}
	}

	virtual void handleMessageEvent( const JID& from, MessageEventType event )
	{
		printf( "received event: %d from: %s\n", event, from.full().c_str() );
	}

	virtual void handleChatState( const JID& from, ChatStateType state )
	{
		printf( "received state: %d from: %s\n", state, from.full().c_str() );
	}

	virtual void handleMessageSession( MessageSession *session )
	{
		printf( "got new session\n");
		// this example can handle only one session. so we get rid of the old session
		j->disposeMessageSession( m_session );
		m_session = session;
		m_session->registerMessageHandler( this );
	}

	virtual void handleLog( LogLevel level, LogArea area, const std::string& message )
	{
		printf("log: level: %d, area: %d, %s\n", level, area, message.c_str() );

		if( message.substr( 0, 10 ) == "<stream:er" )
			printf( "something's foul\n" );

		//if( !m_reconnect && j->m_smHandled > 10 )
		//	j->disconnect( ConnTlsFailed ); // fake disconnect reason so that no </stream:stream> is sent
	}

private:
	Client *j;
	MessageSession *m_session;
	bool m_reconnect;
};

int main( int /*argc*/, char** /*argv*/ )
{
	MessageTest *r = new MessageTest();
	r->start();
	delete( r );
	scanf("%s");
	return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐