gloox  1.0.16
jid.cpp
1 /*
2  Copyright (c) 2005-2015 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 "jid.h"
14 #include "prep.h"
15 #include "gloox.h"
16 #include "util.h"
17 
18 namespace gloox
19 {
20 
21  bool JID::setJID( const std::string& jid )
22  {
23  if ( jid.empty() )
24  {
25  m_bare = m_full = m_server = m_username = m_serverRaw = m_resource = EmptyString;
26  m_valid = false;
27  return false;
28  }
29 
30  const std::string::size_type at = jid.find( '@' );
31  const std::string::size_type slash = jid.find( '/', at == std::string::npos ? 0 : at );
32 
33  if( at != std::string::npos && !( m_valid = prep::nodeprep( jid.substr( 0, at ), m_username ) ) )
34  return false;
35 
36  m_serverRaw = jid.substr( at == std::string::npos ? 0 : at + 1, slash - at - 1 );
37  if( !( m_valid = prep::nameprep( m_serverRaw, m_server ) ) )
38  return false;
39 
40  if( slash != std::string::npos
41  && !( m_valid = prep::resourceprep( jid.substr( slash + 1 ), m_resource ) ) )
42  return false;
43 
44  setStrings();
45 
46  return m_valid;
47  }
48 
49  bool JID::setUsername( const std::string& uname )
50  {
51  m_valid = prep::nodeprep( uname, m_username );
52  setStrings();
53  return m_valid;
54  }
55 
56  bool JID::setServer( const std::string& serv )
57  {
58  m_serverRaw = serv;
59  m_valid = prep::nameprep( m_serverRaw, m_server );
60  setStrings();
61  return m_valid;
62  }
63 
64  bool JID::setResource( const std::string& res )
65  {
66  m_valid = prep::resourceprep( res, m_resource );
67  setFull();
68  return m_valid;
69  }
70 
71  void JID::setFull()
72  {
73  m_full = bare();
74  if( !m_resource.empty() )
75  m_full += '/' + m_resource;
76  }
77 
78  void JID::setBare()
79  {
80  if( !m_username.empty() )
81  m_bare = m_username + '@';
82  else
83  m_bare = ""/*EmptyString*/;
84  m_bare += m_server;
85  }
86 
87  std::string JID::escapeNode( const std::string& node )
88  {
89  std::string escaped = node;
90 
91  util::replaceAll( escaped, "\\", "\\5c" );
92  util::replaceAll( escaped, " ", "\\20" );
93  util::replaceAll( escaped, "\"", "\\22" );
94  util::replaceAll( escaped, "&", "\\26" );
95  util::replaceAll( escaped, "'", "\\27" );
96  util::replaceAll( escaped, "/", "\\2f" );
97  util::replaceAll( escaped, ":", "\\3a" );
98  util::replaceAll( escaped, "<", "\\3c" );
99  util::replaceAll( escaped, ">", "\\3e" );
100  util::replaceAll( escaped, "@", "\\40" );
101 
102  return escaped;
103  }
104 
105  std::string JID::unescapeNode( const std::string& node )
106  {
107  std::string unescaped = node;
108 
109  util::replaceAll( unescaped, "\\20", " " );
110  util::replaceAll( unescaped, "\\22", "\"" );
111  util::replaceAll( unescaped, "\\26", "&" );
112  util::replaceAll( unescaped, "\\27", "'" );
113  util::replaceAll( unescaped, "\\2f", "/" );
114  util::replaceAll( unescaped, "\\3a", ":" );
115  util::replaceAll( unescaped, "\\3c", "<" );
116  util::replaceAll( unescaped, "\\3e", ">" );
117  util::replaceAll( unescaped, "\\40", "@" );
118  util::replaceAll( unescaped, "\\5c", "\\" );
119 
120  return unescaped;
121  }
122 
123 }
void replaceAll(std::string &target, const std::string &find, const std::string &replace)
Definition: util.cpp:177
bool setServer(const std::string &server)
Definition: jid.cpp:56
bool setResource(const std::string &resource)
Definition: jid.cpp:64
bool nameprep(const std::string &domain, std::string &out)
Definition: prep.cpp:71
bool setJID(const std::string &jid)
Definition: jid.cpp:21
const std::string & bare() const
Definition: jid.h:67
The namespace for the gloox library.
Definition: adhoc.cpp:27
bool setUsername(const std::string &username)
Definition: jid.cpp:49
static std::string unescapeNode(const std::string &node)
Definition: jid.cpp:105
bool nodeprep(const std::string &node, std::string &out)
Definition: prep.cpp:59
bool resourceprep(const std::string &resource, std::string &out)
Definition: prep.cpp:83
static std::string escapeNode(const std::string &node)
Definition: jid.cpp:87
const std::string EmptyString
Definition: gloox.cpp:123