Hi, I am having some problems getting to work with auto subscription I
am using
rosterlistener.h etc like some people mention to me last week, I got
my code to compile with no error but I think is not doing nothing at all.
I am attaching the code, please someone take a look and let me know what
I'm doing wrong, please be patience I am a newbee.
Thanks so much
#include <gloox/client.h>
#include <gloox/messagehandler.h>
#include <gloox/presencehandler.h>
#include <gloox/connectionlistener.h>
#include <gloox/rostermanager.h>
#include <gloox/rosterlistener.h>
#include <string>
#include <stdio.h>
#include <iostream>
#include "pstream.h"
using namespace gloox;
std::string Free_mem();
std::string Free_disk();
class Bot : public ConnectionListener, MessageHandler {
public:
void start();
virtual void handleMessage(Stanza *stanza);
virtual void onConnect();
virtual bool onTLSConnect( const CertInfo& info );
virtual void onDisconnect( ConnectionError e );
virtual bool subscriptionRequest(const std::string& JID, const
std::string& MSG);
private:
Client *client;
};
void Bot::start() {
JID jid( "bot@myserver/gloox" );
client = new Client( jid, "mypassword" );
client->registerConnectionListener( this );
client->registerMessageHandler( this );
client->setInitialPriority( 5 );
client->setAutoPresence( true );
client->connect();
}
void Bot::onConnect() {
std::cout << "connected..." << std::endl;
}
bool Bot::onTLSConnect( const CertInfo& info ) {
// examine certificate info
}
void Bot::handleMessage(Stanza *stanza) {
std::string input = stanza->body();
if ( input != "") {
if ( input == "lololo" ) {
Stanza *a = Stanza::createMessageStanza(
stanza->from().full(), "ohhh so like to copy me?!!!" );
client->send( a );
}
if ( input == "disk" ) {
Stanza *a = Stanza::createMessageStanza(
stanza->from().full(), Free_disk() );
client->send( a );
}
if ( input == "free" ) {
Stanza *b = Stanza::createMessageStanza(
stanza->from().full(), Free_mem() );
client->send( b );
} else {
Stanza *c = Stanza::createMessageStanza(
stanza->from().full(), "lololo!!!" );
client->send( c );
//client->send( Stanza::createMessageStanza(
stanza->from().full(), input) );
}
} else {
Stanza *d = Stanza::createMessageStanza(
stanza->from().full(), "you have not say anything to me..." );
client->send( d );
}
}
void Bot::onDisconnect( ConnectionError e ) {
std::cout << "message_test: disconnected: " << e << std::endl;
if( e == ConnAuthenticationFailed )
std::cout << "auth failed. reason: " << client->authError();
}
std::string Free_mem() {
std::string str;
std::string free_mem;
redi::ipstream in("free -m", redi::pstreambuf::pstderr);
while (std::getline(in, str)) {
free_mem.append(str);
free_mem.append("\n");
}
return free_mem;
}
std::string Free_disk() {
std::string str;
std::string free_disk;
redi::ipstream in("df -m", redi::pstreambuf::pstderr);
while (std::getline(in, str)) {
free_disk.append(str);
free_disk.append("\n");
}
return free_disk;
}
bool Bot::subscriptionRequest(const std::string& JID, const std::string& MSG)
{
printf("subscription: %s\n", JID.c_str());
StringList groups;
client->rosterManager()->subscribe( JID, "", groups, "" );
return true;
}
main() {
Bot *lolo = new Bot();
lolo->start();
delete ( lolo );
return 0;
}