1 /*
2 * Copyright (c) 2004-2019 by Jakob Schröter <js@camaya.net>
3 * This file is part of the gloox library. http://camaya.net/gloox
4 *
5 * This software is distributed under a license. The full license
6 * agreement can be found in the file LICENSE in this distribution.
7 * This software may not be copied, modified, sold or distributed
8 * other than expressed in the named license agreement.
9 *
10 * This software is distributed without any warranty.
11 */
12
13 #include "../client.h"
14 #include "../messagehandler.h"
15 #include "../connectionlistener.h"
16 #include "../disco.h"
17 #include "../message.h"
18 #include "../gloox.h"
19 #include "../lastactivity.h"
20 #include "../flexoff.h"
21 #include "../flexoffhandler.h"
22 #include "../loghandler.h"
23 #include "../logsink.h"
24 using namespace gloox;
25
26 #include <stdio.h>
27 #include <locale.h>
28 #include <string>
29
30 #include <cstdio> // [s]print[f]
31
32 class FlexOffTest : public MessageHandler, ConnectionListener, FlexibleOfflineHandler,
33 LogHandler
34 {
35 public:
FlexOffTest()36 FlexOffTest() {}
~FlexOffTest()37 virtual ~FlexOffTest() {}
38
start()39 void start()
40 {
41
42 JID jid( "hurkhurk@example.org/gloox" );
43 j = new Client( jid, "hurkhurks" );
44 j->registerConnectionListener( this );
45 j->registerMessageHandler( this );
46 j->disco()->setVersion( "messageTest", GLOOX_VERSION, "Linux" );
47 j->disco()->setIdentity( "client", "bot" );
48 StringList ca;
49 ca.push_back( "/path/to/cacert.crt" );
50 j->setCACerts( ca );
51
52 f = new FlexibleOffline( j );
53 f->registerFlexibleOfflineHandler( this );
54
55 j->logInstance().registerLogHandler( LogLevelDebug, LogAreaAll, this );
56
57 j->connect();
58
59 delete( j );
60 }
61
onConnect()62 virtual void onConnect()
63 {
64 f->checkSupport();
65 }
66
onDisconnect(ConnectionError e)67 virtual void onDisconnect( ConnectionError e )
68 {
69 printf( "message_test: disconnected: %d\n", e );
70 if( e == ConnAuthenticationFailed )
71 printf( "auth failed. reason: %d\n", j->authError() );
72 }
73
onTLSConnect(const CertInfo & info)74 virtual bool onTLSConnect( const CertInfo& info )
75 {
76 time_t from( info.date_from );
77 time_t to( info.date_to );
78
79 printf( "status: %d\nissuer: %s\npeer: %s\nprotocol: %s\nmac: %s\ncipher: %s\ncompression: %s\n",
80 info.status, info.issuer.c_str(), info.server.c_str(),
81 info.protocol.c_str(), info.mac.c_str(), info.cipher.c_str(),
82 info.compression.c_str() );
83 printf( "from: %s", ctime( &from ) );
84 printf( "to: %s", ctime( &to ) );
85 return true;
86 }
87
handleMessage(const Message & msg,MessageSession *)88 virtual void handleMessage( const Message& msg, MessageSession * /*session*/ )
89 {
90 printf( "type: %d, subject: %s, message: %s, thread id: %s\n", msg.subtype(),
91 msg.subject().c_str(), msg.body().c_str(), msg.thread().c_str() );
92 Tag *m = new Tag( "message" );
93 m->addAttribute( "from", j->jid().full() );
94 m->addAttribute( "to", msg.from().full() );
95 m->addAttribute( "type", "chat" );
96 Tag *b = new Tag( "body", "You said:\n> " + msg.body() + "\nI like that statement." );
97 m->addChild( b );
98 if( !msg.subject().empty() )
99 {
100 Tag *s = new Tag( "subject", "Re:" + msg.subject() );
101 m->addChild( s );
102 }
103 j->send( m );
104 }
105
handleFlexibleOfflineSupport(bool support)106 virtual void handleFlexibleOfflineSupport( bool support )
107 {
108 if( support )
109 {
110 printf( "FlexOff: supported\n" );
111 f->getMsgCount();
112 }
113 else
114 {
115 printf( "FlexOff: not supported\n" );
116 j->disconnect();
117 }
118 }
119
handleFlexibleOfflineMsgNum(int num)120 virtual void handleFlexibleOfflineMsgNum( int num )
121 {
122 printf( "FlexOff messgaes: %d\n", num );
123 f->fetchHeaders();
124 }
125
handleFlexibleOfflineMessageHeaders(const Disco::ItemList & headers)126 virtual void handleFlexibleOfflineMessageHeaders( const Disco::ItemList& headers )
127 {
128 printf( "FlexOff: %d headers received.\n", headers.size() );
129 StringList l;
130 l.push_back( "Fdd" );
131 l.push_back( (*(headers.begin()))->node() );
132 f->fetchMessages( l );
133 f->removeMessages( l );
134 }
135
handleFlexibleOfflineResult(FlexibleOfflineResult result)136 virtual void handleFlexibleOfflineResult( FlexibleOfflineResult result )
137 {
138 printf( "FlexOff: result: %d\n", result );
139 }
140
handleLog(LogLevel level,LogArea area,const std::string & message)141 virtual void handleLog( LogLevel level, LogArea area, const std::string& message )
142 {
143 printf("log: level: %d, area: %d, %s\n", level, area, message.c_str() );
144 }
145
146 private:
147 Client *j;
148 FlexibleOffline *f;
149 };
150
main(int,char **)151 int main( int /*argc*/, char** /*argv*/ )
152 {
153 FlexOffTest *r = new FlexOffTest();
154 r->start();
155 delete( r );
156 return 0;
157 }
158