gloox 1.0
|
00001 /* 00002 Copyright (c) 2007-2009 by Jakob Schroeter <js@camaya.net> 00003 This file is part of the gloox library. http://camaya.net/gloox 00004 00005 This software is distributed under a license. The full license 00006 agreement can be found in the file LICENSE in this distribution. 00007 This software may not be copied, modified, sold or distributed 00008 other than expressed in the named license agreement. 00009 00010 This software is distributed without any warranty. 00011 */ 00012 00013 #include "error.h" 00014 #include "tag.h" 00015 #include "util.h" 00016 00017 namespace gloox 00018 { 00019 00020 /* Error type values */ 00021 static const char* errValues [] = { 00022 "auth", 00023 "cancel", 00024 "continue", 00025 "modify", 00026 "wait" 00027 }; 00028 00029 /* Stanza error values */ 00030 static const char* stanzaErrValues [] = { 00031 "bad-request", 00032 "conflict", 00033 "feature-not-implemented", 00034 "forbidden", 00035 "gone", 00036 "internal-server-error", 00037 "item-not-found", 00038 "jid-malformed", 00039 "not-acceptable", 00040 "not-allowed", 00041 "not-authorized", 00042 "not-modified", 00043 "payment-required", 00044 "recipient-unavailable", 00045 "redirect", 00046 "registration-required", 00047 "remote-server-not-found", 00048 "remote-server-timeout", 00049 "resource-constraint", 00050 "service-unavailable", 00051 "subscription-required", 00052 "undefined-condition", 00053 "unexpected-request", 00054 "unknown-sender" 00055 }; 00056 00057 static inline StanzaErrorType stanzaErrorType( const std::string& type ) 00058 { 00059 return (StanzaErrorType)util::lookup( type, errValues ); 00060 } 00061 00062 static inline StanzaError stanzaError( const std::string& type ) 00063 { 00064 return (StanzaError)util::lookup( type, stanzaErrValues ); 00065 } 00066 00067 Error::Error( const Tag* tag ) 00068 : StanzaExtension( ExtError ), 00069 m_error( StanzaErrorUndefined ), m_appError( 0 ) 00070 { 00071 if( !tag || tag->name() != "error" ) 00072 return; 00073 00074 m_type = stanzaErrorType( tag->findAttribute( TYPE ) ); 00075 00076 TagList::const_iterator it = tag->children().begin(); 00077 for( ; it != tag->children().end(); ++it ) 00078 { 00079 StanzaError srt = gloox::stanzaError( (*it)->name() ); 00080 if( srt != StanzaErrorUndefined ) 00081 m_error = srt; 00082 else if( (*it)->name() == "text" ) 00083 m_text[(*it)->findAttribute("xml:lang")] = (*it)->cdata(); 00084 else 00085 m_appError = (*it)->clone(); 00086 } 00087 } 00088 00089 Error::Error( const Error& error ) 00090 : StanzaExtension( ExtError ), m_type( error.m_type ), 00091 m_error( error.m_error ), m_appError( error.m_appError ? m_appError->clone() : 0 ) 00092 {} 00093 00094 Error::~Error() 00095 { 00096 delete m_appError; 00097 } 00098 00099 const std::string& Error::filterString() const 00100 { 00101 static const std::string filter = "/iq/error" 00102 "|/message/error" 00103 "|/presence/error" 00104 "|/subscription/error"; 00105 return filter; 00106 } 00107 00108 00109 Tag* Error::tag() const 00110 { 00111 if( m_type == StanzaErrorTypeUndefined || m_error == StanzaErrorUndefined ) 00112 return 0; 00113 00114 Tag* error = new Tag( "error", TYPE, util::lookup( m_type, errValues ) ); 00115 new Tag( error, util::lookup( m_error, stanzaErrValues ), XMLNS, XMLNS_XMPP_STANZAS ); 00116 00117 StringMap::const_iterator it = m_text.begin(); 00118 for( ; it != m_text.end(); ++it ) 00119 { 00120 Tag* txt = new Tag( error, "text" ); 00121 txt->setXmlns( XMLNS_XMPP_STANZAS ); 00122 txt->addAttribute( "xml:lang", (*it).first ); 00123 txt->setCData( (*it).second ); 00124 } 00125 00126 if( m_appError ) 00127 error->addChild( m_appError->clone() ); 00128 00129 return error; 00130 } 00131 00132 const std::string& Error::text( const std::string& lang ) const 00133 { 00134 StringMap::const_iterator it = m_text.find( lang ); 00135 return it != m_text.end() ? (*it).second : EmptyString; 00136 } 00137 00138 }