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 "../connectionlistener.h"
15 #include "../annotationshandler.h"
16 #include "../disco.h"
17 #include "../annotations.h"
18 using namespace gloox;
19 
20 #include <ctime>
21 #include <clocale>
22 #include <string>
23 
24 #include <cstdio> // [s]print[f]
25 
26 class AnnotationsTest : public AnnotationsHandler, ConnectionListener
27 {
28   public:
AnnotationsTest()29     AnnotationsTest() {}
~AnnotationsTest()30     virtual ~AnnotationsTest() {}
31 
start()32     void start()
33     {
34 
35       JID jid( "hurkhurk@example.org/gloox" );
36       j = new Client( jid, "hurkhurks" );
37 
38       j->registerConnectionListener(this );
39       j->disco()->setVersion( "annotationsTest", GLOOX_VERSION );
40       j->disco()->setIdentity( "client", "bot" );
41 
42       a = new Annotations( j );
43       a->registerAnnotationsHandler( this );
44 
45       j->connect();
46 
47       delete( a );
48       delete( j );
49     }
50 
onConnect()51     virtual void onConnect()
52     {
53       a->requestAnnotations();
54     }
55 
onDisconnect(ConnectionError)56     virtual void onDisconnect( ConnectionError /*e*/ ) { printf( "annotations_test: disconnected\n" ); }
57 
onTLSConnect(const CertInfo & info)58     virtual bool onTLSConnect( const CertInfo& info )
59     {
60       time_t from( info.date_from );
61       time_t to( info.date_to );
62 
63       printf( "status: %d\nissuer: %s\npeer: %s\nprotocol: %s\nmac: %s\ncipher: %s\ncompression: %s\n",
64               info.status, info.issuer.c_str(), info.server.c_str(),
65               info.protocol.c_str(), info.mac.c_str(), info.cipher.c_str(),
66               info.compression.c_str() );
67       printf( "from: %s", ctime( &from ) );
68       printf( "to:   %s", ctime( &to ) );
69       return true;
70     }
71 
handleAnnotations(const AnnotationsList & aList)72     virtual void handleAnnotations( const AnnotationsList &aList )
73     {
74       printf( "received notes...\n" );
75       AnnotationsList::const_iterator it = aList.begin();
76       for( ; it != aList.end(); it++ )
77       {
78         printf( "jid: %s, note: %s, cdate: %s, mdate: %s\n", (*it).jid.c_str(),
79                 (*it).note.c_str(), (*it).cdate.c_str(), (*it).mdate.c_str() );
80       }
81 
82       AnnotationsList mybList;
83 
84       AnnotationsListItem bItem;
85       bItem.jid = "romeo@montague.org";
86       bItem.note = "my lover & friend. 2 > 3";
87       mybList.push_back( bItem );
88 
89       bItem.jid = "juliet@capulet.com";
90       bItem.note = "oh my sweetest love...";
91       bItem.cdate = "20040924T15:23:21";
92       bItem.mdate = "20040924T15:23:21";
93       mybList.push_back( bItem );
94 
95       a->storeAnnotations( mybList );
96     }
97 
98   private:
99     Client *j;
100     Annotations *a;
101 };
102 
main(int,char **)103 int main( int /*argc*/, char** /*argv*/ )
104 {
105   AnnotationsTest *t = new AnnotationsTest();
106   t->start();
107   delete( t );
108   return 0;
109 }
110