error.cpp
/*
Copyright (c) 2007-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 "error.h"
#include "tag.h"
#include "util.h"
namespace gloox
{
/* Error type values */
static const char* errValues [] = {
"auth",
"cancel",
"continue",
"modify",
"wait"
};
/* Stanza error values */
static const char* stanzaErrValues [] = {
"bad-request",
"conflict",
"feature-not-implemented",
"forbidden",
"gone",
"internal-server-error",
"item-not-found",
"jid-malformed",
"not-acceptable",
"not-allowed",
"not-authorized",
"not-modified",
"payment-required",
"recipient-unavailable",
"redirect",
"registration-required",
"remote-server-not-found",
"remote-server-timeout",
"resource-constraint",
"service-unavailable",
"subscription-required",
"undefined-condition",
"unexpected-request",
"unknown-sender"
};
static inline StanzaErrorType stanzaErrorType( const std::string& type )
{
return static_cast<StanzaErrorType>( util::lookup( type, errValues ) );
}
static inline StanzaError stanzaError( const std::string& type )
{
return static_cast<StanzaError>( util::lookup( type, stanzaErrValues ) );
}
Error::Error( const Tag* tag )
: StanzaExtension( ExtError ),
m_error( StanzaErrorUndefined ), m_appError( 0 )
{
if( !tag || tag->name() != "error" )
return;
m_type = stanzaErrorType( tag->findAttribute( TYPE ) );
TagList::const_iterator it = tag->children().begin();
for( ; it != tag->children().end(); ++it )
{
StanzaError srt = gloox::stanzaError( (*it)->name() );
if( srt != StanzaErrorUndefined )
m_error = srt;
else if( (*it)->name() == "text" )
m_text[(*it)->findAttribute("xml:lang")] = (*it)->cdata();
else
m_appError = (*it)->clone();
}
}
Error::Error( const Error& error )
: StanzaExtension( ExtError ), m_type( error.m_type ),
m_error( error.m_error ), m_appError( error.m_appError ? error.m_appError->clone() : 0 )
{}
Error::~Error()
{
delete m_appError;
}
const std::string& Error::filterString() const
{
static const std::string filter = "/iq/error"
"|/message/error"
"|/presence/error"
"|/subscription/error";
return filter;
}
Tag* Error::tag() const
{
if( m_type == StanzaErrorTypeUndefined || m_error == StanzaErrorUndefined )
return 0;
Tag* error = new Tag( "error", TYPE, util::lookup( m_type, errValues ) );
new Tag( error, util::lookup( m_error, stanzaErrValues ), XMLNS, XMLNS_XMPP_STANZAS );
StringMap::const_iterator it = m_text.begin();
for( ; it != m_text.end(); ++it )
{
Tag* txt = new Tag( error, "text" );
txt->setXmlns( XMLNS_XMPP_STANZAS );
txt->addAttribute( "xml:lang", (*it).first );
txt->setCData( (*it).second );
}
if( m_appError )
error->addChild( m_appError->clone() );
return error;
}
const std::string& Error::text( const std::string& lang ) const
{
StringMap::const_iterator it = m_text.find( lang );
return it != m_text.end() ? (*it).second : EmptyString;
}
void Error::setAppError( Tag* appError )
{
delete m_appError;
m_appError = appError;
}
}
gloox::Error
A stanza error abstraction implemented as a StanzaExtension.
Definition error.h:35
gloox::Error::appError
const Tag * appError() const
Definition error.h:83
gloox::Error::Error
Error(const Tag *tag=0)
Definition error.cpp:67
gloox::Error::setAppError
void setAppError(Tag *appError)
Definition error.cpp:138
gloox::Error::filterString
virtual const std::string & filterString() const
Definition error.cpp:99
gloox::Error::~Error
virtual ~Error()
Definition error.cpp:94
gloox::Error::text
const std::string & text(const std::string &lang=EmptyString) const
Definition error.cpp:132
gloox::Error::error
StanzaError error() const
Definition error.h:75
gloox::Error::tag
virtual Tag * tag() const
Definition error.cpp:109
gloox::StanzaExtension
This class abstracts a stanza extension, which is usually an XML child element in a specific namespac...
Definition stanzaextension.h:204
gloox::StanzaExtension::StanzaExtension
StanzaExtension(int type)
Definition stanzaextension.h:211
gloox::Tag
This is an abstraction of an XML element.
Definition tag.h:47
gloox::Tag::setCData
bool setCData(const std::string &cdata)
Definition tag.cpp:447
gloox::Tag::addAttribute
bool addAttribute(Attribute *attr)
Definition tag.cpp:354
gloox::Tag::setXmlns
bool setXmlns(const std::string &xmlns, const std::string &prefix=EmptyString)
Definition tag.cpp:522
gloox
The namespace for the gloox library.
Definition adhoc.cpp:28
gloox::XMLNS_XMPP_STANZAS
const std::string XMLNS_XMPP_STANZAS
Definition gloox.cpp:86
gloox::EmptyString
const std::string EmptyString
Definition gloox.cpp:124
gloox::XMLNS
const std::string XMLNS
Definition gloox.cpp:122
gloox::TYPE
const std::string TYPE
Definition gloox.cpp:123
gloox::StanzaError
StanzaError
Definition gloox.h:872
gloox::StanzaErrorUndefined
@ StanzaErrorUndefined
Definition gloox.h:952
gloox::StanzaErrorType
StanzaErrorType
Definition gloox.h:857
gloox::StanzaErrorTypeUndefined
@ StanzaErrorTypeUndefined
Definition gloox.h:864
gloox::ExtError
@ ExtError
Definition stanzaextension.h:47