gloox  1.0.21
error.cpp
1 /*
2  Copyright (c) 2007-2017 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 "error.h"
14 #include "tag.h"
15 #include "util.h"
16 
17 namespace gloox
18 {
19 
20  /* Error type values */
21  static const char* errValues [] = {
22  "auth",
23  "cancel",
24  "continue",
25  "modify",
26  "wait"
27  };
28 
29  /* Stanza error values */
30  static const char* stanzaErrValues [] = {
31  "bad-request",
32  "conflict",
33  "feature-not-implemented",
34  "forbidden",
35  "gone",
36  "internal-server-error",
37  "item-not-found",
38  "jid-malformed",
39  "not-acceptable",
40  "not-allowed",
41  "not-authorized",
42  "not-modified",
43  "payment-required",
44  "recipient-unavailable",
45  "redirect",
46  "registration-required",
47  "remote-server-not-found",
48  "remote-server-timeout",
49  "resource-constraint",
50  "service-unavailable",
51  "subscription-required",
52  "undefined-condition",
53  "unexpected-request",
54  "unknown-sender"
55  };
56 
57  static inline StanzaErrorType stanzaErrorType( const std::string& type )
58  {
59  return static_cast<StanzaErrorType>( util::lookup( type, errValues ) );
60  }
61 
62  static inline StanzaError stanzaError( const std::string& type )
63  {
64  return static_cast<StanzaError>( util::lookup( type, stanzaErrValues ) );
65  }
66 
67  Error::Error( const Tag* tag )
69  m_error( StanzaErrorUndefined ), m_appError( 0 )
70  {
71  if( !tag || tag->name() != "error" )
72  return;
73 
74  m_type = stanzaErrorType( tag->findAttribute( TYPE ) );
75 
76  TagList::const_iterator it = tag->children().begin();
77  for( ; it != tag->children().end(); ++it )
78  {
79  StanzaError srt = gloox::stanzaError( (*it)->name() );
80  if( srt != StanzaErrorUndefined )
81  m_error = srt;
82  else if( (*it)->name() == "text" )
83  m_text[(*it)->findAttribute("xml:lang")] = (*it)->cdata();
84  else
85  m_appError = (*it)->clone();
86  }
87  }
88 
89  Error::Error( const Error& error )
90  : StanzaExtension( ExtError ), m_type( error.m_type ),
91  m_error( error.m_error ), m_appError( error.m_appError ? error.m_appError->clone() : 0 )
92  {}
93 
95  {
96  delete m_appError;
97  }
98 
99  const std::string& Error::filterString() const
100  {
101  static const std::string filter = "/iq/error"
102  "|/message/error"
103  "|/presence/error"
104  "|/subscription/error";
105  return filter;
106  }
107 
108 
109  Tag* Error::tag() const
110  {
111  if( m_type == StanzaErrorTypeUndefined || m_error == StanzaErrorUndefined )
112  return 0;
113 
114  Tag* error = new Tag( "error", TYPE, util::lookup( m_type, errValues ) );
115  new Tag( error, util::lookup( m_error, stanzaErrValues ), XMLNS, XMLNS_XMPP_STANZAS );
116 
117  StringMap::const_iterator it = m_text.begin();
118  for( ; it != m_text.end(); ++it )
119  {
120  Tag* txt = new Tag( error, "text" );
122  txt->addAttribute( "xml:lang", (*it).first );
123  txt->setCData( (*it).second );
124  }
125 
126  if( m_appError )
127  error->addChild( m_appError->clone() );
128 
129  return error;
130  }
131 
132  const std::string& Error::text( const std::string& lang ) const
133  {
134  StringMap::const_iterator it = m_text.find( lang );
135  return it != m_text.end() ? (*it).second : EmptyString;
136  }
137 
139  {
140  delete m_appError;
141  m_appError = appError;
142  }
143 
144 }
Tag * clone() const
Definition: tag.cpp:670
bool setXmlns(const std::string &xmlns, const std::string &prefix=EmptyString)
Definition: tag.cpp:522
const std::string & text(const std::string &lang=EmptyString) const
Definition: error.cpp:132
const std::string XMLNS
Definition: gloox.cpp:123
const std::string XMLNS_XMPP_STANZAS
Definition: gloox.cpp:86
const std::string & name() const
Definition: tag.h:394
const std::string TYPE
Definition: gloox.cpp:124
virtual Tag * tag() const
Definition: error.cpp:109
A stanza error abstraction implemented as a StanzaExtension.
Definition: error.h:34
bool setCData(const std::string &cdata)
Definition: tag.cpp:447
void addChild(Tag *child)
Definition: tag.cpp:424
StanzaError
Definition: gloox.h:871
The namespace for the gloox library.
Definition: adhoc.cpp:27
This class abstracts a stanza extension, which is usually an XML child element in a specific namespac...
virtual ~Error()
Definition: error.cpp:94
void setAppError(Tag *appError)
Definition: error.cpp:138
virtual const std::string & filterString() const
Definition: error.cpp:99
bool addAttribute(Attribute *attr)
Definition: tag.cpp:354
StanzaErrorType
Definition: gloox.h:856
const std::string & findAttribute(const std::string &name) const
Definition: tag.cpp:589
StanzaError error() const
Definition: error.h:75
const TagList & children() const
Definition: tag.cpp:510
const Tag * appError() const
Definition: error.h:83
Error(const Tag *tag=0)
Definition: error.cpp:67
const std::string EmptyString
Definition: gloox.cpp:125
This is an abstraction of an XML element.
Definition: tag.h:46