gloox  1.0
error.cpp
1 /*
2  Copyright (c) 2007-2009 by Jakob Schroeter <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 (StanzaErrorType)util::lookup( type, errValues );
60  }
61 
62  static inline StanzaError stanzaError( const std::string& type )
63  {
64  return (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 ? 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 
138 }