gloox  0.9.9.12
jid.cpp
1 /*
2  Copyright (c) 2005-2008 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 
14 #include "jid.h"
15 
16 #include "prep.h"
17 
18 namespace gloox
19 {
20 
21  void 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 = "";
26  return;
27  }
28 
29  size_t at = jid.find( "@", 0 );
30  size_t slash = jid.find( "/", 0 );
31 
32  if( at == std::string::npos )
33  {
34  if( slash == std::string::npos )
35  {
36  m_serverRaw = jid;
37  }
38  else
39  {
40  m_serverRaw = jid.substr( 0, slash );
41  m_resource = prep::resourceprep( jid.substr( slash + 1 ) );
42  }
43  }
44  else
45  {
46  m_username = prep::nodeprep( jid.substr( 0, at ) );
47  if( slash != std::string::npos )
48  {
49  m_serverRaw = jid.substr( at + 1, slash - at - 1 );
50  m_resource = prep::resourceprep( jid.substr( slash + 1 ) );
51  }
52  else
53  {
54  m_serverRaw = jid.substr( at + 1 );
55  }
56  }
57  m_server = prep::nameprep( m_serverRaw );
58  setStrings();
59  }
60 
61  void JID::setUsername( const std::string& username )
62  {
63  m_username = prep::nodeprep( username );
64  setStrings();
65  }
66 
67  void JID::setServer( const std::string& server )
68  {
69  m_serverRaw = server;
70  m_server = prep::nameprep( m_serverRaw );
71  setStrings();
72  }
73 
74  void JID::setResource( const std::string& resource )
75  {
76  m_resource = prep::resourceprep( resource );
77  setFull();
78  }
79 
80  void JID::setFull()
81  {
82  m_full = bare();
83  if( !m_resource.empty() )
84  m_full += '/' + m_resource;
85  }
86 
87  void JID::setBare()
88  {
89  if( !m_username.empty() )
90  m_bare = m_username + '@';
91  else
92  m_bare = "";
93  m_bare += m_server;
94  }
95 
96 }