flexoff.cpp
/*
Copyright (c) 2005-2023 by Jakob Schröter <js@camaya.net>
This file is part of the gloox library. http://camaya.net/gloox
This software is distributed under a license. The full license
agreement can be found in the file LICENSE in this distribution.
This software may not be copied, modified, sold or distributed
other than expressed in the named license agreement.
This software is distributed without any warranty.
*/
#include "flexoff.h"
#include "dataform.h"
#include "disco.h"
#include "error.h"
#include <cstdlib>
namespace gloox
{
// ---- FlexibleOffline::Offline ----
FlexibleOffline::Offline::Offline( const Tag* /*tag*/ )
: StanzaExtension( ExtFlexOffline )
{
// FIXME what to do here?
}
FlexibleOffline::Offline::Offline( int context, const StringList& msgs )
: StanzaExtension( ExtFlexOffline ), m_context( context ), m_msgs( msgs )
{
}
FlexibleOffline::Offline::~Offline()
{
}
const std::string& FlexibleOffline::Offline::filterString() const
{
static const std::string filter = "/iq/offline[@xmlns='" + XMLNS_OFFLINE + "']";
return filter;
}
Tag* FlexibleOffline::Offline::tag() const
{
Tag* t = new Tag( "offline" );
t->setXmlns( XMLNS_OFFLINE );
if( m_msgs.empty() )
new Tag( t, m_context == FORequestMsgs ? "fetch" : "purge" );
else
{
const std::string action = m_context == FORequestMsgs ? "view" : "remove";
StringList::const_iterator it = m_msgs.begin();
for( ; it != m_msgs.end(); ++it )
{
Tag* i = new Tag( t, "item", "action", action );
i->addAttribute( "node", (*it) );
}
}
return t;
}
// ---- ~FlexibleOffline::Offline ----
// ---- FlexibleOffline ----
FlexibleOffline::FlexibleOffline( ClientBase* parent )
: m_parent( parent ), m_flexibleOfflineHandler( 0 )
{
if( m_parent )
m_parent->registerStanzaExtension( new Offline() );
}
FlexibleOffline::~FlexibleOffline()
{
if( m_parent )
m_parent->removeIDHandler( this );
}
void FlexibleOffline::checkSupport()
{
m_parent->disco()->getDiscoInfo( m_parent->jid().server(), EmptyString, this, FOCheckSupport );
}
void FlexibleOffline::getMsgCount()
{
m_parent->disco()->getDiscoInfo( m_parent->jid().server(), XMLNS_OFFLINE, this, FORequestNum );
}
void FlexibleOffline::fetchHeaders()
{
m_parent->disco()->getDiscoItems( m_parent->jid().server(), XMLNS_OFFLINE, this, FORequestHeaders );
}
void FlexibleOffline::messageOperation( int context, const StringList& msgs )
{
const std::string& id = m_parent->getID();
IQ::IqType iqType = context == FORequestMsgs ? IQ::Get : IQ::Set;
IQ iq( iqType, JID(), id );
iq.addExtension( new Offline( context, msgs ) );
m_parent->send( iq, this, context );
}
void FlexibleOffline::registerFlexibleOfflineHandler( FlexibleOfflineHandler* foh )
{
m_flexibleOfflineHandler = foh;
}
void FlexibleOffline::removeFlexibleOfflineHandler()
{
m_flexibleOfflineHandler = 0;
}
void FlexibleOffline::handleDiscoInfo( const JID& /*from*/, const Disco::Info& info, int context )
{
if( !m_flexibleOfflineHandler )
return;
switch( context )
{
case FOCheckSupport:
m_flexibleOfflineHandler->handleFlexibleOfflineSupport( info.hasFeature( XMLNS_OFFLINE ) );
break;
case FORequestNum:
int num = -1;
if( info.form() && info.form()->hasField( "number_of_messages" ) )
num = atoi( info.form()->field( "number_of_messages" )->value().c_str() );
m_flexibleOfflineHandler->handleFlexibleOfflineMsgNum( num );
break;
}
}
void FlexibleOffline::handleDiscoItems( const JID& /*from*/, const Disco::Items& items, int context )
{
if( context == FORequestHeaders && m_flexibleOfflineHandler )
{
if( items.node() == XMLNS_OFFLINE )
m_flexibleOfflineHandler->handleFlexibleOfflineMessageHeaders( items.items() );
}
}
void FlexibleOffline::handleDiscoError( const JID& /*from*/, const Error* /*error*/, int /*context*/ )
{
}
void FlexibleOffline::handleIqID( const IQ& iq, int context )
{
if( !m_flexibleOfflineHandler )
return;
switch( context )
{
case FORequestMsgs:
switch( iq.subtype() )
{
case IQ::Result:
m_flexibleOfflineHandler->handleFlexibleOfflineResult( FomrRequestSuccess );
break;
case IQ::Error:
switch( iq.error()->error() )
{
case StanzaErrorForbidden:
m_flexibleOfflineHandler->handleFlexibleOfflineResult( FomrForbidden );
break;
case StanzaErrorItemNotFound:
m_flexibleOfflineHandler->handleFlexibleOfflineResult( FomrItemNotFound );
break;
default:
m_flexibleOfflineHandler->handleFlexibleOfflineResult( FomrUnknownError );
break;
}
break;
default:
break;
}
break;
case FORemoveMsgs:
switch( iq.subtype() )
{
case IQ::Result:
m_flexibleOfflineHandler->handleFlexibleOfflineResult( FomrRemoveSuccess );
break;
case IQ::Error:
switch( iq.error()->error() )
{
case StanzaErrorForbidden:
m_flexibleOfflineHandler->handleFlexibleOfflineResult( FomrForbidden );
break;
case StanzaErrorItemNotFound:
m_flexibleOfflineHandler->handleFlexibleOfflineResult( FomrItemNotFound );
break;
default:
m_flexibleOfflineHandler->handleFlexibleOfflineResult( FomrUnknownError );
break;
}
break;
default:
break;
}
break;
}
}
}
gloox::ClientBase
This is the common base class for a Jabber/XMPP Client and a Jabber Component.
Definition clientbase.h:79
gloox::ClientBase::getID
const std::string getID()
Definition clientbase.cpp:1168
gloox::ClientBase::send
void send(Tag *tag)
Definition clientbase.cpp:1004
gloox::DataFormFieldContainer::field
DataFormField * field(const std::string &field) const
Definition dataformfieldcontainer.cpp:39
gloox::DataFormFieldContainer::hasField
bool hasField(const std::string &field) const
Definition dataformfieldcontainer.h:64
gloox::DataFormField::value
const std::string & value() const
Definition dataformfield.h:189
gloox::Disco::Info
An abstraction of a Disco Info element (from Service Discovery, XEP-0030) as a StanzaExtension.
Definition disco.h:66
gloox::Disco::Info::form
const DataForm * form() const
Definition disco.h:99
gloox::Disco::Info::hasFeature
bool hasFeature(const std::string &feature) const
Definition disco.cpp:121
gloox::Disco::Items
An abstraction of a Disco query element (from Service Discovery, XEP-0030) in the disco::items namesp...
Definition disco.h:276
gloox::Disco::Items::node
const std::string & node() const
Definition disco.h:305
gloox::Disco::Items::items
const ItemList & items() const
Definition disco.h:311
gloox::Error
A stanza error abstraction implemented as a StanzaExtension.
Definition error.h:35
gloox::Error::error
StanzaError error() const
Definition error.h:75
gloox::FlexibleOfflineHandler
Implementation of this virtual interface allows for retrieval of offline messages following XEP-0030.
Definition flexoffhandler.h:44
gloox::FlexibleOffline::checkSupport
void checkSupport()
Definition flexoff.cpp:81
gloox::FlexibleOffline::removeFlexibleOfflineHandler
void removeFlexibleOfflineHandler()
Definition flexoff.cpp:110
gloox::FlexibleOffline::handleIqID
virtual void handleIqID(const IQ &iq, int context)
Definition flexoff.cpp:149
gloox::FlexibleOffline::handleDiscoInfo
virtual void handleDiscoInfo(const JID &from, const Disco::Info &info, int context)
Definition flexoff.cpp:115
gloox::FlexibleOffline::getMsgCount
void getMsgCount()
Definition flexoff.cpp:86
gloox::FlexibleOffline::FlexibleOffline
FlexibleOffline(ClientBase *parent)
Definition flexoff.cpp:68
gloox::FlexibleOffline::registerFlexibleOfflineHandler
void registerFlexibleOfflineHandler(FlexibleOfflineHandler *foh)
Definition flexoff.cpp:105
gloox::FlexibleOffline::handleDiscoError
virtual void handleDiscoError(const JID &from, const Error *error, int context)
Definition flexoff.cpp:145
gloox::FlexibleOffline::handleDiscoItems
virtual void handleDiscoItems(const JID &from, const Disco::Items &items, int context)
Definition flexoff.cpp:136
gloox::FlexibleOffline::~FlexibleOffline
virtual ~FlexibleOffline()
Definition flexoff.cpp:75
gloox::FlexibleOffline::fetchHeaders
void fetchHeaders()
Definition flexoff.cpp:91
gloox::IQ
An abstraction of an IQ stanza.
Definition iq.h:34
gloox::IQ::subtype
IqType subtype() const
Definition iq.h:74
gloox::IQ::IqType
IqType
Definition iq.h:44
gloox::IQ::Set
@ Set
Definition iq.h:46
gloox::IQ::Error
@ Error
Definition iq.h:49
gloox::IQ::Result
@ Result
Definition iq.h:48
gloox::IQ::Get
@ Get
Definition iq.h:45
gloox::JID
An abstraction of a JID.
Definition jid.h:31
gloox::StanzaExtension
This class abstracts a stanza extension, which is usually an XML child element in a specific namespac...
Definition stanzaextension.h:204
gloox::Stanza::error
const Error * error() const
Definition stanza.cpp:47
gloox::Tag
This is an abstraction of an XML element.
Definition tag.h:47
gloox
The namespace for the gloox library.
Definition adhoc.cpp:28
gloox::FomrItemNotFound
@ FomrItemNotFound
Definition flexoffhandler.h:32
gloox::FomrUnknownError
@ FomrUnknownError
Definition flexoffhandler.h:33
gloox::FomrForbidden
@ FomrForbidden
Definition flexoffhandler.h:30
gloox::FomrRequestSuccess
@ FomrRequestSuccess
Definition flexoffhandler.h:29
gloox::FomrRemoveSuccess
@ FomrRemoveSuccess
Definition flexoffhandler.h:28
gloox::StringList
std::list< std::string > StringList
Definition gloox.h:1251
gloox::EmptyString
const std::string EmptyString
Definition gloox.cpp:124
gloox::StanzaErrorItemNotFound
@ StanzaErrorItemNotFound
Definition gloox.h:893
gloox::StanzaErrorForbidden
@ StanzaErrorForbidden
Definition gloox.h:884
gloox::ExtFlexOffline
@ ExtFlexOffline
Definition stanzaextension.h:81
gloox::XMLNS_OFFLINE
const std::string XMLNS_OFFLINE
Definition gloox.cpp:29