2
#define CLIENTBASE_TEST
4
#include "../messagesessionhandler.h"
5
#include "../messageeventhandler.h"
6
#include "../messageeventfilter.h"
7
#include "../chatstatehandler.h"
8
#include "../chatstatefilter.h"
9
#include "../connectionlistener.h"
11
#include "../message.h"
13
#include "../lastactivity.h"
14
#include "../loghandler.h"
15
#include "../logsink.h"
16
#include "../connectiontcpclient.h"
17
#include "../connectionsocks5proxy.h"
18
#include "../connectionhttpproxy.h"
19
#include "../messagehandler.h"
20
using namespace gloox;
29
#include <cstdio> // [s]print[f]
31
#if defined( WIN32 ) || defined( _WIN32 )
35
class MessageTest : public MessageSessionHandler, ConnectionListener, LogHandler,
36
MessageEventHandler, MessageHandler, ChatStateHandler
39
MessageTest() : m_session( 0 ), m_reconnect( false ) {}
41
virtual ~MessageTest() {}
46
JID jid( "admin@jabba.us/gloox" );
47
j = new Client( jid, "test" );
48
j->registerConnectionListener( this );
49
j->registerMessageSessionHandler( this, 0 );
50
j->disco()->setVersion( "reconnectTest", GLOOX_VERSION, "Linux" );
51
j->disco()->setIdentity( "client", "bot" );
52
j->disco()->addFeature( XMLNS_CHAT_STATES );
53
// j->setTls( TLSDisabled );
54
j->setCompression( false );
55
j->setStreamManagement( true, true );
57
ca.push_back( "/path/to/cacert.crt" );
60
j->logInstance().registerLogHandler( LogLevelDebug, LogAreaAll, this );
63
// this code connects to a jabber server through a SOCKS5 proxy
65
// ConnectionSOCKS5Proxy* conn = new ConnectionSOCKS5Proxy( j,
66
// new ConnectionTCP( j->logInstance(),
67
// "sockshost", 1080 ),
68
// j->logInstance(), "example.net" );
69
// conn->setProxyAuth( "socksuser", "sockspwd" );
70
// j->setConnectionImpl( conn );
73
// this code connects to a jabber server through a HTTP proxy through a SOCKS5 proxy
75
// ConnectionTCP* conn0 = new ConnectionTCP( j->logInstance(), "old", 1080 );
76
// ConnectionSOCKS5Proxy* conn1 = new ConnectionSOCKS5Proxy( conn0, j->logInstance(), "old", 8080 );
77
// conn1->setProxyAuth( "socksuser", "sockspwd" );
78
// ConnectionHTTPProxy* conn2 = new ConnectionHTTPProxy( j, conn1, j->logInstance(), "jabber.cc" );
79
// conn2->setProxyAuth( "httpuser", "httppwd" );
80
// j->setConnectionImpl( conn2 );
83
ConnectionError ce = ConnNoError;
84
if( j->connect( false ) )
86
while( ce == ConnNoError )
90
printf( "ce: %d\n", ce );
95
if( j->connect( false ) )
97
while( ce == ConnNoError )
101
printf( "ce: %d\n", ce );
107
virtual void onConnect()
109
printf( "connected!!!\n" );
112
virtual void onDisconnect( ConnectionError e )
114
printf( "message_test: disconnected: %d\n", e );
115
if( e == ConnAuthenticationFailed )
116
printf( "auth failed. reason: %d\n", j->authError() );
119
virtual bool onTLSConnect( const CertInfo& info )
121
time_t from( info.date_from );
122
time_t to( info.date_to );
124
printf( "status: %d\nissuer: %s\npeer: %s\nprotocol: %s\nmac: %s\ncipher: %s\ncompression: %s\n"
125
"from: %s\nto: %s\n",
126
info.status, info.issuer.c_str(), info.server.c_str(),
127
info.protocol.c_str(), info.mac.c_str(), info.cipher.c_str(),
128
info.compression.c_str(), ctime( &from ), ctime( &to ) );
132
virtual void handleMessage( const Message& msg, MessageSession * /*session*/ )
134
printf( "type: %d, subject: %s, message: %s, thread id: %s\n", msg.subtype(),
135
msg.subject().c_str(), msg.body().c_str(), msg.thread().c_str() );
138
if( msg.body().substr( 0, 3 ) == "ack" ) // using substr() to work around some stupid clients
139
j->ackStreamManagement();
140
else if( msg.body().substr( 0, 3 ) == "req" )
141
j->reqStreamManagement();
142
else if( msg.body().substr( 0, 4 ) == "quit" )
146
std::string re = "You said:\n> " + msg.body() + "\nI like that statement.";
147
m_session->send( re, gloox::EmptyString );
151
virtual void handleMessageEvent( const JID& from, MessageEventType event )
153
printf( "received event: %d from: %s\n", event, from.full().c_str() );
156
virtual void handleChatState( const JID& from, ChatStateType state )
158
printf( "received state: %d from: %s\n", state, from.full().c_str() );
161
virtual void handleMessageSession( MessageSession *session )
163
printf( "got new session\n");
164
// this example can handle only one session. so we get rid of the old session
165
j->disposeMessageSession( m_session );
167
m_session->registerMessageHandler( this );
170
virtual void handleLog( LogLevel level, LogArea area, const std::string& message )
172
printf("log: level: %d, area: %d, %s\n", level, area, message.c_str() );
174
if( message.substr( 0, 10 ) == "<stream:er" )
175
printf( "something's foul\n" );
177
if( !m_reconnect && j->m_smHandled > 10 )
178
j->disconnect( ConnTlsFailed ); // fake disconnect reason so that no </stream:stream> is sent
183
MessageSession *m_session;
187
int main( int /*argc*/, char** /*argv*/ )
189
MessageTest *r = new MessageTest();