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 "../mucroomhandler.h"
16 #include "../mucroom.h"
17 #include "../disco.h"
18 #include "../presence.h"
19 #include "../message.h"
20 #include "../dataform.h"
21 #include "../gloox.h"
22 #include "../lastactivity.h"
23 #include "../loghandler.h"
24 #include "../logsink.h"
25 using namespace gloox;
26 
27 #include <stdio.h>
28 #include <locale.h>
29 #include <string>
30 
31 #include <cstdio> // [s]print[f]
32 
33 #ifdef WIN32
34 #include <windows.h>
35 #endif
36 
37 class MessageTest : public ConnectionListener, LogHandler, MUCRoomHandler
38 {
39   public:
MessageTest()40     MessageTest() {}
41 
~MessageTest()42     virtual ~MessageTest() {}
43 
start()44     void start()
45     {
46       JID jid( "hurkhurk@example.net/gloox" );
47       j = new Client( jid, "hurkhurks" );
48       j->registerConnectionListener( this );
49       j->setPresence( Presence::Available, -1 );
50       j->disco()->setVersion( "gloox muc_example", GLOOX_VERSION, "Linux" );
51       j->disco()->setIdentity( "client", "bot" );
52       j->setCompression( false );
53       StringList ca;
54       ca.push_back( "/path/to/cacert.crt" );
55       j->setCACerts( ca );
56 
57       j->logInstance().registerLogHandler( LogLevelDebug, LogAreaAll, this );
58 
59       JID nick( "test@conference.jabber.org/glooxmuctest" );
60       m_room = new MUCRoom( j, nick, this, 0 );
61 
62       if( j->connect( false ) )
63       {
64         ConnectionError ce = ConnNoError;
65         while( ce == ConnNoError )
66         {
67           ce = j->recv();
68         }
69         printf( "ce: %d\n", ce );
70       }
71 
72       // cleanup
73       delete m_room;
74       delete j;
75     }
76 
onConnect()77     virtual void onConnect()
78     {
79       printf( "connected!!!\n" );
80       m_room->join();
81       m_room->getRoomInfo();
82       m_room->getRoomItems();
83     }
84 
onDisconnect(ConnectionError e)85     virtual void onDisconnect( ConnectionError e )
86     {
87       printf( "message_test: disconnected: %d\n", e );
88       if( e == ConnAuthenticationFailed )
89         printf( "auth failed. reason: %d\n", j->authError() );
90     }
91 
onTLSConnect(const CertInfo & info)92     virtual bool onTLSConnect( const CertInfo& info )
93     {
94       time_t from( info.date_from );
95       time_t to( info.date_to );
96 
97       printf( "status: %d\nissuer: %s\npeer: %s\nprotocol: %s\nmac: %s\ncipher: %s\ncompression: %s\n",
98               info.status, info.issuer.c_str(), info.server.c_str(),
99               info.protocol.c_str(), info.mac.c_str(), info.cipher.c_str(),
100               info.compression.c_str() );
101       printf( "from: %s", ctime( &from ) );
102       printf( "to:   %s", ctime( &to ) );
103       return true;
104     }
105 
handleLog(LogLevel level,LogArea area,const std::string & message)106     virtual void handleLog( LogLevel level, LogArea area, const std::string& message )
107     {
108       printf("log: level: %d, area: %d, %s\n", level, area, message.c_str() );
109     }
110 
handleMUCParticipantPresence(MUCRoom *,const MUCRoomParticipant participant,const Presence & presence)111     virtual void handleMUCParticipantPresence( MUCRoom * /*room*/, const MUCRoomParticipant participant,
112                                             const Presence& presence )
113     {
114       if( presence.presence() == Presence::Available )
115         printf( "!!!!!!!!!!!!!!!! %s is in the room, too\n", participant.nick->resource().c_str() );
116       else if( presence.presence() == Presence::Unavailable )
117         printf( "!!!!!!!!!!!!!!!! %s left the room\n", participant.nick->resource().c_str() );
118       else
119         printf( "Presence is %d of %s\n", presence.presence(), participant.nick->resource().c_str() );
120     }
121 
handleMUCMessage(MUCRoom *,const Message & msg,bool priv)122     virtual void handleMUCMessage( MUCRoom* /*room*/, const Message& msg, bool priv )
123     {
124       printf( "%s said: '%s' (history: %s, private: %s)\n", msg.from().resource().c_str(), msg.body().c_str(),
125               msg.when() ? "yes" : "no", priv ? "yes" : "no" );
126     }
127 
handleMUCSubject(MUCRoom *,const std::string & nick,const std::string & subject)128     virtual void handleMUCSubject( MUCRoom * /*room*/, const std::string& nick, const std::string& subject )
129     {
130       if( nick.empty() )
131         printf( "Subject: %s\n", subject.c_str() );
132       else
133         printf( "%s has set the subject to: '%s'\n", nick.c_str(), subject.c_str() );
134     }
135 
handleMUCError(MUCRoom *,StanzaError error)136     virtual void handleMUCError( MUCRoom * /*room*/, StanzaError error )
137     {
138       printf( "!!!!!!!!got an error: %d", error );
139     }
140 
handleMUCInfo(MUCRoom *,int features,const std::string & name,const DataForm * infoForm)141     virtual void handleMUCInfo( MUCRoom * /*room*/, int features, const std::string& name,
142                                     const DataForm* infoForm )
143     {
144       printf( "features: %d, name: %s, form xml: %s\n",
145               features, name.c_str(), infoForm->tag()->xml().c_str() );
146     }
147 
handleMUCItems(MUCRoom *,const Disco::ItemList & items)148     virtual void handleMUCItems( MUCRoom * /*room*/, const Disco::ItemList& items )
149     {
150       Disco::ItemList::const_iterator it = items.begin();
151       for( ; it != items.end(); ++it )
152       {
153         printf( "%s -- %s is an item here\n", (*it)->jid().full().c_str(), (*it)->name().c_str() );
154       }
155     }
156 
handleMUCInviteDecline(MUCRoom *,const JID & invitee,const std::string & reason)157     virtual void handleMUCInviteDecline( MUCRoom * /*room*/, const JID& invitee, const std::string& reason )
158     {
159       printf( "Invitee %s declined invitation. reason given: %s\n", invitee.full().c_str(), reason.c_str() );
160     }
161 
handleMUCRoomCreation(MUCRoom * room)162     virtual bool handleMUCRoomCreation( MUCRoom *room )
163     {
164       printf( "room %s didn't exist, beeing created.\n", room->name().c_str() );
165       return true;
166     }
167 
168   private:
169     Client *j;
170     MUCRoom *m_room;
171 };
172 
main(int,char **)173 int main( int /*argc*/, char** /*argv*/ )
174 {
175   MessageTest *r = new MessageTest();
176   r->start();
177   delete( r );
178   return 0;
179 }
180